123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- var fs = require('fs')
- , path = require('path')
- , join = path.join
- , dirname = path.dirname
- , exists = fs.existsSync || path.existsSync
- , defaults = {
- arrow: process.env.NODE_BINDINGS_ARROW || ' → '
- , compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled'
- , platform: process.platform
- , arch: process.arch
- , version: process.versions.node
- , bindings: 'bindings.node'
- , try: [
-
- [ 'module_root', 'build', 'bindings' ]
-
- , [ 'module_root', 'build', 'Debug', 'bindings' ]
- , [ 'module_root', 'build', 'Release', 'bindings' ]
-
- , [ 'module_root', 'out', 'Debug', 'bindings' ]
- , [ 'module_root', 'Debug', 'bindings' ]
-
- , [ 'module_root', 'out', 'Release', 'bindings' ]
- , [ 'module_root', 'Release', 'bindings' ]
-
- , [ 'module_root', 'build', 'default', 'bindings' ]
-
- , [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ]
- ]
- }
- function bindings (opts) {
-
- if (typeof opts == 'string') {
- opts = { bindings: opts }
- } else if (!opts) {
- opts = {}
- }
- opts.__proto__ = defaults
-
- if (!opts.module_root) {
- opts.module_root = exports.getRoot(exports.getFileName())
- }
-
- if (path.extname(opts.bindings) != '.node') {
- opts.bindings += '.node'
- }
- var tries = []
- , i = 0
- , l = opts.try.length
- , n
- , b
- , err
- for (; i<l; i++) {
- n = join.apply(null, opts.try[i].map(function (p) {
- return opts[p] || p
- }))
- tries.push(n)
- try {
- b = opts.path ? require.resolve(n) : require(n)
- if (!opts.path) {
- b.path = n
- }
- return b
- } catch (e) {
- if (!/not find/i.test(e.message)) {
- throw e
- }
- }
- }
- err = new Error('Could not locate the bindings file. Tried:\n'
- + tries.map(function (a) { return opts.arrow + a }).join('\n'))
- err.tries = tries
- throw err
- }
- module.exports = exports = bindings
- exports.getFileName = function getFileName (calling_file) {
- var origPST = Error.prepareStackTrace
- , origSTL = Error.stackTraceLimit
- , dummy = {}
- , fileName
- Error.stackTraceLimit = 10
- Error.prepareStackTrace = function (e, st) {
- for (var i=0, l=st.length; i<l; i++) {
- fileName = st[i].getFileName()
- if (fileName !== __filename) {
- if (calling_file) {
- if (fileName !== calling_file) {
- return
- }
- } else {
- return
- }
- }
- }
- }
-
- Error.captureStackTrace(dummy)
- dummy.stack
-
- Error.prepareStackTrace = origPST
- Error.stackTraceLimit = origSTL
- return fileName
- }
- exports.getRoot = function getRoot (file) {
- var dir = dirname(file)
- , prev
- while (true) {
- if (dir === '.') {
-
- dir = process.cwd()
- }
- if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {
-
- return dir
- }
- if (prev === dir) {
-
- throw new Error('Could not find module root given file: "' + file
- + '". Do you have a `package.json` file? ')
- }
-
- prev = dir
- dir = join(dir, '..')
- }
- }
|