bindings.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Module dependencies.
  3. */
  4. var fs = require('fs')
  5. , path = require('path')
  6. , join = path.join
  7. , dirname = path.dirname
  8. , exists = fs.existsSync || path.existsSync
  9. , defaults = {
  10. arrow: process.env.NODE_BINDINGS_ARROW || ' → '
  11. , compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled'
  12. , platform: process.platform
  13. , arch: process.arch
  14. , version: process.versions.node
  15. , bindings: 'bindings.node'
  16. , try: [
  17. // node-gyp's linked version in the "build" dir
  18. [ 'module_root', 'build', 'bindings' ]
  19. // node-waf and gyp_addon (a.k.a node-gyp)
  20. , [ 'module_root', 'build', 'Debug', 'bindings' ]
  21. , [ 'module_root', 'build', 'Release', 'bindings' ]
  22. // Debug files, for development (legacy behavior, remove for node v0.9)
  23. , [ 'module_root', 'out', 'Debug', 'bindings' ]
  24. , [ 'module_root', 'Debug', 'bindings' ]
  25. // Release files, but manually compiled (legacy behavior, remove for node v0.9)
  26. , [ 'module_root', 'out', 'Release', 'bindings' ]
  27. , [ 'module_root', 'Release', 'bindings' ]
  28. // Legacy from node-waf, node <= 0.4.x
  29. , [ 'module_root', 'build', 'default', 'bindings' ]
  30. // Production "Release" buildtype binary (meh...)
  31. , [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ]
  32. ]
  33. }
  34. /**
  35. * The main `bindings()` function loads the compiled bindings for a given module.
  36. * It uses V8's Error API to determine the parent filename that this function is
  37. * being invoked from, which is then used to find the root directory.
  38. */
  39. function bindings (opts) {
  40. // Argument surgery
  41. if (typeof opts == 'string') {
  42. opts = { bindings: opts }
  43. } else if (!opts) {
  44. opts = {}
  45. }
  46. opts.__proto__ = defaults
  47. // Get the module root
  48. if (!opts.module_root) {
  49. opts.module_root = exports.getRoot(exports.getFileName())
  50. }
  51. // Ensure the given bindings name ends with .node
  52. if (path.extname(opts.bindings) != '.node') {
  53. opts.bindings += '.node'
  54. }
  55. var tries = []
  56. , i = 0
  57. , l = opts.try.length
  58. , n
  59. , b
  60. , err
  61. for (; i<l; i++) {
  62. n = join.apply(null, opts.try[i].map(function (p) {
  63. return opts[p] || p
  64. }))
  65. tries.push(n)
  66. try {
  67. b = opts.path ? require.resolve(n) : require(n)
  68. if (!opts.path) {
  69. b.path = n
  70. }
  71. return b
  72. } catch (e) {
  73. if (!/not find/i.test(e.message)) {
  74. throw e
  75. }
  76. }
  77. }
  78. err = new Error('Could not locate the bindings file. Tried:\n'
  79. + tries.map(function (a) { return opts.arrow + a }).join('\n'))
  80. err.tries = tries
  81. throw err
  82. }
  83. module.exports = exports = bindings
  84. /**
  85. * Gets the filename of the JavaScript file that invokes this function.
  86. * Used to help find the root directory of a module.
  87. * Optionally accepts an filename argument to skip when searching for the invoking filename
  88. */
  89. exports.getFileName = function getFileName (calling_file) {
  90. var origPST = Error.prepareStackTrace
  91. , origSTL = Error.stackTraceLimit
  92. , dummy = {}
  93. , fileName
  94. Error.stackTraceLimit = 10
  95. Error.prepareStackTrace = function (e, st) {
  96. for (var i=0, l=st.length; i<l; i++) {
  97. fileName = st[i].getFileName()
  98. if (fileName !== __filename) {
  99. if (calling_file) {
  100. if (fileName !== calling_file) {
  101. return
  102. }
  103. } else {
  104. return
  105. }
  106. }
  107. }
  108. }
  109. // run the 'prepareStackTrace' function above
  110. Error.captureStackTrace(dummy)
  111. dummy.stack
  112. // cleanup
  113. Error.prepareStackTrace = origPST
  114. Error.stackTraceLimit = origSTL
  115. return fileName
  116. }
  117. /**
  118. * Gets the root directory of a module, given an arbitrary filename
  119. * somewhere in the module tree. The "root directory" is the directory
  120. * containing the `package.json` file.
  121. *
  122. * In: /home/nate/node-native-module/lib/index.js
  123. * Out: /home/nate/node-native-module
  124. */
  125. exports.getRoot = function getRoot (file) {
  126. var dir = dirname(file)
  127. , prev
  128. while (true) {
  129. if (dir === '.') {
  130. // Avoids an infinite loop in rare cases, like the REPL
  131. dir = process.cwd()
  132. }
  133. if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {
  134. // Found the 'package.json' file or 'node_modules' dir; we're done
  135. return dir
  136. }
  137. if (prev === dir) {
  138. // Got to the top
  139. throw new Error('Could not find module root given file: "' + file
  140. + '". Do you have a `package.json` file? ')
  141. }
  142. // Try the parent dir next
  143. prev = dir
  144. dir = join(dir, '..')
  145. }
  146. }