From 025f079eaecbad389aff6a295f2c8393ea6b64b9 Mon Sep 17 00:00:00 2001 From: aetherwalker Date: Mon, 20 Nov 2017 01:23:36 -0500 Subject: [PATCH 1/7] Adding option to support sandboxing modules that don't have files When requiring a file, if the filesystem doesn't have the file path specified, a missing file error is thrown by "require-like". This adds an option "ignoreMissing" that skips the error with a simple try/catch to allow the sandboxing to continue. --- Readme.md | 2 ++ lib/sandboxed_module.js | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index f1e88cc..f2010d0 100644 --- a/Readme.md +++ b/Readme.md @@ -52,6 +52,8 @@ using the same options that were used for the original sandboxed module. * `sourceTransformersSingleOnly:` If false, the source transformers will not be run against modules required by the sandboxed module. By default it will take the same value as `singleOnly`. +* `ignoreMissing:` If true, injected modules will not be required to have a corresponding file +on the file system. By default this is false. ### SandboxedModule.require(moduleId, [options]) diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js index 6bf483c..7a7095e 100644 --- a/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js @@ -157,7 +157,13 @@ SandboxedModule.prototype._createRecursiveRequireProxy = function() { var cache = Object.create(null); var required = this._getRequires(); for (var key in required) { - var injectedFilename = requireLike(this.filename).resolve(key); + var injectedFilename; + try { + injectedFilename = requireLike(this.filename).resolve(key); + } catch(missing) { + if(!this._options.ignoreMissing) {throw missing;} + injectedFilename = key; + } cache[injectedFilename] = required[key]; } cache[this.filename] = this.exports; From 2f6a8c807642000541f0264a44744102c99cae70 Mon Sep 17 00:00:00 2001 From: aetherwalker Date: Mon, 20 Nov 2017 01:28:24 -0500 Subject: [PATCH 2/7] Adding test case for ignoreMissing option --- test/integration/test-inject-missing.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/integration/test-inject-missing.js diff --git a/test/integration/test-inject-missing.js b/test/integration/test-inject-missing.js new file mode 100644 index 0000000..c2183fc --- /dev/null +++ b/test/integration/test-inject-missing.js @@ -0,0 +1,10 @@ +var assert = require('assert'); +var SandboxedModule = require('../..'); + +var foo = SandboxedModule.require('../fixture/foo', { + "requires": {"doesNotExist": {}}, + "ignoreMissing": true +}); + +assert.strictEqual(foo.foo, 'foo'); +assert.strictEqual(foo.bar, 'bar'); From 2bf19d015418a34d0a90c8b707ce059481052761 Mon Sep 17 00:00:00 2001 From: aetherwalker Date: Mon, 20 Nov 2017 01:34:48 -0500 Subject: [PATCH 3/7] Proposed solution for #64 --- test/integration/test-inject-missing.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/test-inject-missing.js b/test/integration/test-inject-missing.js index c2183fc..357d174 100644 --- a/test/integration/test-inject-missing.js +++ b/test/integration/test-inject-missing.js @@ -5,6 +5,5 @@ var foo = SandboxedModule.require('../fixture/foo', { "requires": {"doesNotExist": {}}, "ignoreMissing": true }); - assert.strictEqual(foo.foo, 'foo'); assert.strictEqual(foo.bar, 'bar'); From 7509b27283a70ba599392dd982235f8b91a3f30a Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 23 Jan 2018 08:11:50 +1100 Subject: [PATCH 4/7] updated readme and package to reflect fork --- Readme.md | 6 ++++-- package.json | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index f1e88cc..307838d 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,8 @@ # sandboxed-module -[![Build Status](https://secure.travis-ci.org/felixge/node-sandboxed-module.png)](http://travis-ci.org/felixge/node-sandboxed-module) +[![Build Status](https://secure.travis-ci.org/log4js-node/node-sandboxed-module.png)](http://travis-ci.org/log4js-node/node-sandboxed-module) + +NOTE: this is a fork of [felixge's original](https://github.com/felixge/node-sandboxed-module), that doesn't seem to be maintained at the moment. If felixge's starts to be maintained again, this module will be deprecated. A sandboxed node.js module loader that lets you inject dependencies into your modules. @@ -8,7 +10,7 @@ modules. ## Installation ``` bash -npm install sandboxed-module +npm install @log4js-node/sandboxed-module ``` ## Usage diff --git a/package.json b/package.json index 767cf90..055cb74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "author": "Felix Geisendörfer (http://debuggable.com/)", - "name": "sandboxed-module", + "name": "@log4js-node/sandboxed-module", "description": "A sandboxed Node.js module loader that lets you inject dependencies into your modules.", "keywords": [ "require", @@ -11,8 +11,8 @@ "testing" ], "license": "MIT", - "version": "2.0.3", - "repository": "felixge/node-sandboxed-module", + "version": "2.1.0", + "repository": "log4js-node/node-sandboxed-module", "main": "./lib/sandboxed_module", "files": [ "lib/" From 32c136d88cc90e8a8a6cb46ea76d1824b5ac5f84 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 23 Jan 2018 08:13:07 +1100 Subject: [PATCH 5/7] updated node versions for travis --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6e5919d..dd338dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: node_js node_js: - - "0.10" + - "4" + - "6" + - "8" From 653ba49621a13ec7fa24c486d7429016cda7ba60 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 24 Jan 2018 08:10:08 +1100 Subject: [PATCH 6/7] builtin modules list gets generated - not sure this should be in git anyway --- lib/builtin_modules.json | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/builtin_modules.json b/lib/builtin_modules.json index ed048ba..ed1a44a 100644 --- a/lib/builtin_modules.json +++ b/lib/builtin_modules.json @@ -1,5 +1,4 @@ [ - "_debug_agent", "_debugger", "_linklist", "assert", @@ -16,18 +15,11 @@ "freelist", "fs", "http", - "_http_agent", - "_http_client", - "_http_common", - "_http_incoming", - "_http_outgoing", - "_http_server", "https", "module", "net", "os", "path", - "process", "punycode", "querystring", "readline", @@ -38,18 +30,13 @@ "_stream_duplex", "_stream_transform", "_stream_passthrough", - "_stream_wrap", "string_decoder", "sys", "timers", "tls", - "_tls_common", - "_tls_legacy", - "_tls_wrap", "tty", "url", "util", - "v8", "vm", "zlib" ] \ No newline at end of file From 5975f80f37f3bf93996a7435de1c23883bae2784 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 24 Jan 2018 08:16:54 +1100 Subject: [PATCH 7/7] rolled back travis build to 0.10, because 4,6,8 not passing --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index dd338dd..6e5919d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,3 @@ language: node_js node_js: - - "4" - - "6" - - "8" + - "0.10"