Newer
Older
{"ast":null,"code":"/**\n * Module dependencies.\n */\nvar fs = require('fs'),\n path = require('path'),\n join = path.join,\n dirname = path.dirname,\n exists = fs.accessSync && function (path) {\n try {\n fs.accessSync(path);\n } catch (e) {\n return false;\n }\n\n return true;\n} || fs.existsSync || path.existsSync,\n defaults = {\n arrow: process.env.NODE_BINDINGS_ARROW || ' → ',\n compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled',\n platform: process.platform,\n arch: process.arch,\n version: process.versions.node,\n bindings: 'bindings.node',\n try: [// node-gyp's linked version in the \"build\" dir\n ['module_root', 'build', 'bindings'] // node-waf and gyp_addon (a.k.a node-gyp)\n , ['module_root', 'build', 'Debug', 'bindings'], ['module_root', 'build', 'Release', 'bindings'] // Debug files, for development (legacy behavior, remove for node v0.9)\n , ['module_root', 'out', 'Debug', 'bindings'], ['module_root', 'Debug', 'bindings'] // Release files, but manually compiled (legacy behavior, remove for node v0.9)\n , ['module_root', 'out', 'Release', 'bindings'], ['module_root', 'Release', 'bindings'] // Legacy from node-waf, node <= 0.4.x\n , ['module_root', 'build', 'default', 'bindings'] // Production \"Release\" buildtype binary (meh...)\n , ['module_root', 'compiled', 'version', 'platform', 'arch', 'bindings']]\n /**\n * The main `bindings()` function loads the compiled bindings for a given module.\n * It uses V8's Error API to determine the parent filename that this function is\n * being invoked from, which is then used to find the root directory.\n */\n\n};\n\nfunction bindings(opts) {\n // Argument surgery\n if (typeof opts == 'string') {\n opts = {\n bindings: opts\n };\n } else if (!opts) {\n opts = {};\n } // maps `defaults` onto `opts` object\n\n\n Object.keys(defaults).map(function (i) {\n if (!(i in opts)) opts[i] = defaults[i];\n }); // Get the module root\n\n if (!opts.module_root) {\n opts.module_root = exports.getRoot(exports.getFileName());\n } // Ensure the given bindings name ends with .node\n\n\n if (path.extname(opts.bindings) != '.node') {\n opts.bindings += '.node';\n } // https://github.com/webpack/webpack/issues/4175#issuecomment-342931035\n\n\n var requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;\n var tries = [],\n i = 0,\n l = opts.try.length,\n n,\n b,\n err;\n\n for (; i < l; i++) {\n n = join.apply(null, opts.try[i].map(function (p) {\n return opts[p] || p;\n }));\n tries.push(n);\n\n try {\n b = opts.path ? requireFunc.resolve(n) : requireFunc(n);\n\n if (!opts.path) {\n b.path = n;\n }\n\n return b;\n } catch (e) {\n if (!/not find/i.test(e.message)) {\n throw e;\n }\n }\n }\n\n err = new Error('Could not locate the bindings file. Tried:\\n' + tries.map(function (a) {\n return opts.arrow + a;\n }).join('\\n'));\n err.tries = tries;\n throw err;\n}\n\nmodule.exports = exports = bindings;\n/**\n * Gets the filename of the JavaScript file that invokes this function.\n * Used to help find the root directory of a module.\n * Optionally accepts an filename argument to skip when searching for the invoking filename\n */\n\nexports.getFileName = function getFileName(calling_file) {\n var origPST = Error.prepareStackTrace,\n origSTL = Error.stackTraceLimit,\n dummy = {},\n fileName;\n Error.stackTraceLimit = 10;\n\n Error.prepareStackTrace = function (e, st) {\n for (var i = 0, l = st.length; i < l; i++) {\n fileName = st[i].getFileName();\n\n if (fileName !== __filename) {\n if (calling_file) {\n if (fileName !== calling_file) {\n return;\n }\n } else {\n return;\n }\n }\n }\n }; // run the 'prepareStackTrace' function above\n\n\n Error.captureStackTrace(dummy);\n dummy.stack; // cleanup\n\n Error.prepareStackTrace = origPST;\n Error.stackTraceLimit = origSTL;\n return fileName;\n};\n/**\n * Gets the root directory of a module, given an arbitrary filename\n * somewhere in the module tree. The \"root directory\" is the directory\n * containing the `package.json` file.\n *\n * In: /home/nate/node-native-module/lib/index.js\n * Out: /home/nate/node-native-module\n */\n\n\nexports.getRoot = function getRoot(file) {\n var dir = dirname(file),\n prev;\n\n while (true) {\n if (dir === '.') {\n // Avoids an infinite loop in rare cases, like the REPL\n dir = process.cwd();\n }\n\n if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {\n // Found the 'package.json' file or 'node_modules' dir; we're done\n return dir;\n }\n\n if (prev === dir) {\n // Got to the top\n throw new Error('Could not find module root given file: \"' + file + '\". Do you have a `package.json` file? ');\n } // Try the parent dir next\n\n\n prev = dir;\n dir = join(dir, '..');\n }\n};","map":null,"metadata":{},"sourceType":"script"}