Index: test/stub-modules/common.js |
diff --git a/test/stub-modules/common.js b/test/stub-modules/common.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8871003a84ed2ff79137f920b710a2a62d010b41 |
--- /dev/null |
+++ b/test/stub-modules/common.js |
@@ -0,0 +1,55 @@ |
+let path = require("path"); |
+let SandboxedModule = require("sandboxed-module"); |
+ |
+let globals = { |
+ atob: data => new Buffer(data, "base64").toString("binary"), |
+ btoa: data => new Buffer(data, "binary").toString("base64"), |
+ Ci: {}, |
+ Cu: {import: () => { }}, |
+ navigator: {}, |
+ onShutdown: {add: () => { }}, |
+ Services: {obs: {addObserver: () => { }}}, |
+ XPCOMUtils: {generateQI: () => { }} |
+}; |
+ |
+function bodgeExports(exports) |
+{ |
+ return src => src + "\n" + exports. |
+ map(name => "Object.defineProperty(exports, '" + name + "'," + |
+ "{get: () => " + name + "});").join("\n"); |
+} |
+ |
+function libPath(moduleId) |
+{ |
+ return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js"); |
+} |
+ |
+exports.createSandbox = function(extraExports) |
+{ |
+ // Shared require cache for this sandbox |
+ let cache = {}; |
+ |
+ // For any modules which we need extra exports we must transform them now. |
+ for (let moduleId in extraExports) |
+ cache[libPath(moduleId)] = SandboxedModule.require(moduleId, { |
+ cache: cache, |
+ globals: globals, |
+ sourceTransformers: [bodgeExports(extraExports[moduleId])], |
+ sourceTransformersSingleOnly: true, |
+ singleOnly: false |
+ }); |
+ |
+ return (moduleId, exports) => |
+ { |
+ let key = libPath(moduleId); |
+ |
+ // Only load modules which aren't already cached for this sandbox |
+ if (!(key in cache)) |
+ cache[key] = SandboxedModule.require(moduleId, { |
+ cache: cache, |
+ globals: globals |
+ }); |
+ |
+ return cache[key]; |
+ }; |
+}; |