diff options
Diffstat (limited to 'node_modules/natives')
-rw-r--r-- | node_modules/natives/README.md | 64 | ||||
-rw-r--r-- | node_modules/natives/index.js | 115 | ||||
-rw-r--r-- | node_modules/natives/package.json | 90 |
3 files changed, 269 insertions, 0 deletions
diff --git a/node_modules/natives/README.md b/node_modules/natives/README.md new file mode 100644 index 000000000..6fc442435 --- /dev/null +++ b/node_modules/natives/README.md @@ -0,0 +1,64 @@ +# natives + +Do stuff with Node.js's native JavaScript modules + +## Caveat + +Dear Beloved User, + +I feel compelled to give you a word of warning if you are considering +using this module. + +This module lets you do some creative things with the JavaScript code +in Node.js. There are some things here that are basically a recipe +for memory leaks, or at the very least, being broken with each new +release of Node, since none of these API surfaces are "technically" +"supported" by the team managing the Node.js project. + +This module does not ship a _copy_ of Node's internals. It does its +thing by using the exposed source code that lives in Node.js itself. +So, running this on different versions of Node.js will produce +different results. + +When your program is broken by changes to Node's internals, or when +Node changes in such a way that this module becomes fundamentally +broken, you will likely get little sympathy. Many people in the Node +community consider this sort of behavior to be unwise and unseemly, if +not outright hostile and morally wrong. + +At the very least, you probably just want to run Node with the +(undocumented!) `--expose-internals` flag, rather than go to such +lengths. + +Don't use unless you know what you're doing, or at least, are ok with +the risks. Don't say I didn't warn you. + +Eternally Yours in OSS, +Isaac Z. Schlueter + +## USAGE + +```javascript +var natives = require('natives') + +// get the source code +var fsCode = natives.source('fs') + +// get a evaluated copy of the module +var fsCopy = natives.require('fs') + +// you can pass in a whitelist to NOT shim certain things +var fsCopyWithNativeStreams = natives.require('fs', ['stream']) + +// note that this is not the same as the "real" fs +assert(fsCopy !== require('fs')) + +// but it does have all the same entries +fsCopy.readFileSync(__filename, 'utf8') // etc +``` + +## Another Caveat + +You can't use this to override `require("buffer")` because everything +depends on `Buffer.isBuffer` working properly, so it's important for +that one to be given a pass. diff --git a/node_modules/natives/index.js b/node_modules/natives/index.js new file mode 100644 index 000000000..67513909e --- /dev/null +++ b/node_modules/natives/index.js @@ -0,0 +1,115 @@ +var natives = process.binding('natives') +var module = require('module') +var normalRequire = require +exports.source = src +exports.require = req +var vm = require('vm') + +// fallback for 0.x support +var runInThisContext, ContextifyScript, Script +/*istanbul ignore next*/ +try { + ContextifyScript = process.binding('contextify').ContextifyScript; + runInThisContext = function runInThisContext(code, options) { + var script = new ContextifyScript(code, options); + return script.runInThisContext(); + } +} catch (er) { + Script = process.binding('evals').NodeScript; + runInThisContext = Script.runInThisContext; +} + +var wrap = [ + '(function (exports, require, module, __filename, __dirname) { ', + '\n});' +]; + + +// Basically the same functionality as node's (buried deep) +// NativeModule class, but without caching, or internal/ blocking, +// or a class, since that's not really necessary. I assume that if +// you're loading something with this module, it's because you WANT +// a separate copy. However, to preserve semantics, any require() +// calls made throughout the internal module load IS cached. +function req (id, whitelist) { + var cache = Object.create(null) + + if (Array.isArray(whitelist)) { + // a whitelist of things to pull from the "actual" native modules + whitelist.forEach(function (id) { + cache[id] = { + loading: false, + loaded: true, + filename: id + '.js', + exports: require(id) + } + }) + } + + return req_(id, cache) +} + +function req_ (id, cache) { + // Buffer is special, because it's a type rather than a "normal" + // class, and many things depend on `Buffer.isBuffer` working. + if (id === 'buffer') { + return require('buffer') + } + + // native_module isn't actually a natives binding. + // weird, right? + if (id === 'native_module') { + return { + getSource: src, + wrap: function (script) { + return wrap[0] + script + wrap[1] + }, + wrapper: wrap, + _cache: cache + } + } + + var source = src(id) + if (!source) { + return undefined + } + source = wrap[0] + source + wrap[1] + + var cachingRequire = function require (id) { + if (cache[id]) { + return cache[id].exports + } + return req_(id, cache) + } + + var nm = { + exports: {}, + loading: true, + loaded: false, + filename: id + '.js' + } + cache[id] = nm + var fn + try { + /* istanbul ignore else */ + if (ContextifyScript) { + fn = runInThisContext(source, { + filename: nm.filename, + lineOffset: 0, + displayErrors: true + }); + } else { + fn = runInThisContext(source, nm.filename, true); + } + fn(nm.exports, cachingRequire, nm, nm.filename) + nm.loaded = true + } finally { + nm.loading = false + } + + return nm.exports +} + +function src (id) { + return natives[id] +} diff --git a/node_modules/natives/package.json b/node_modules/natives/package.json new file mode 100644 index 000000000..aad63431e --- /dev/null +++ b/node_modules/natives/package.json @@ -0,0 +1,90 @@ +{ + "_args": [ + [ + { + "raw": "natives@^1.1.0", + "scope": null, + "escapedName": "natives", + "name": "natives", + "rawSpec": "^1.1.0", + "spec": ">=1.1.0 <2.0.0", + "type": "range" + }, + "/home/dold/repos/taler/wallet-webex/node_modules/gulp/node_modules/graceful-fs" + ] + ], + "_from": "natives@>=1.1.0 <2.0.0", + "_id": "natives@1.1.0", + "_inCache": true, + "_location": "/natives", + "_nodeVersion": "6.5.0", + "_npmOperationalInternal": { + "host": "packages-16-east.internal.npmjs.com", + "tmp": "tmp/natives-1.1.0.tgz_1472583645072_0.9239758928306401" + }, + "_npmUser": { + "name": "isaacs", + "email": "i@izs.me" + }, + "_npmVersion": "3.10.7", + "_phantomChildren": {}, + "_requested": { + "raw": "natives@^1.1.0", + "scope": null, + "escapedName": "natives", + "name": "natives", + "rawSpec": "^1.1.0", + "spec": ">=1.1.0 <2.0.0", + "type": "range" + }, + "_requiredBy": [ + "/gulp/graceful-fs" + ], + "_resolved": "https://registry.npmjs.org/natives/-/natives-1.1.0.tgz", + "_shasum": "e9ff841418a6b2ec7a495e939984f78f163e6e31", + "_shrinkwrap": null, + "_spec": "natives@^1.1.0", + "_where": "/home/dold/repos/taler/wallet-webex/node_modules/gulp/node_modules/graceful-fs", + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "http://blog.izs.me/" + }, + "bugs": { + "url": "https://github.com/isaacs/natives/issues" + }, + "dependencies": {}, + "description": "Do stuff with Node.js's native JavaScript modules", + "devDependencies": { + "tap": "^7.0.0" + }, + "directories": {}, + "dist": { + "shasum": "e9ff841418a6b2ec7a495e939984f78f163e6e31", + "tarball": "https://registry.npmjs.org/natives/-/natives-1.1.0.tgz" + }, + "files": [ + "index.js" + ], + "gitHead": "dc1297cdce008350b679dbb86be5bf5bbf8cacbf", + "homepage": "https://github.com/isaacs/natives#readme", + "license": "ISC", + "main": "index.js", + "maintainers": [ + { + "name": "isaacs", + "email": "i@izs.me" + } + ], + "name": "natives", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git+https://github.com/isaacs/natives.git" + }, + "scripts": { + "test": "tap test/*.js --100" + }, + "version": "1.1.0" +} |