From 025f079eaecbad389aff6a295f2c8393ea6b64b9 Mon Sep 17 00:00:00 2001 From: aetherwalker Date: Mon, 20 Nov 2017 01:23:36 -0500 Subject: [PATCH 1/3] 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/3] 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/3] 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');