OLD | NEW |
(Empty) | |
| 1 let path = require("path"); |
| 2 let SandboxedModule = require("sandboxed-module"); |
| 3 |
| 4 let globals = { |
| 5 atob: data => new Buffer(data, "base64").toString("binary"), |
| 6 btoa: data => new Buffer(data, "binary").toString("base64"), |
| 7 Ci: {}, |
| 8 Cu: {import: () => { }}, |
| 9 navigator: {}, |
| 10 onShutdown: {add: () => { }}, |
| 11 Services: {obs: {addObserver: () => { }}}, |
| 12 XPCOMUtils: {generateQI: () => { }} |
| 13 }; |
| 14 |
| 15 function bodgeExports(exports) |
| 16 { |
| 17 return src => src + "\n" + exports. |
| 18 map(name => "Object.defineProperty(exports, '" + name + "'," + |
| 19 "{get: () => " + name + "});").join("\n"); |
| 20 } |
| 21 |
| 22 function libPath(moduleId) |
| 23 { |
| 24 return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js"); |
| 25 } |
| 26 |
| 27 exports.createSandbox = function(extraExports) |
| 28 { |
| 29 // Shared require cache for this sandbox |
| 30 let cache = {}; |
| 31 |
| 32 // For any modules which we need extra exports we must transform them now. |
| 33 for (let moduleId in extraExports) |
| 34 cache[libPath(moduleId)] = SandboxedModule.require(moduleId, { |
| 35 cache: cache, |
| 36 globals: globals, |
| 37 sourceTransformers: [bodgeExports(extraExports[moduleId])], |
| 38 sourceTransformersSingleOnly: true, |
| 39 singleOnly: false |
| 40 }); |
| 41 |
| 42 return (moduleId, exports) => |
| 43 { |
| 44 let key = libPath(moduleId); |
| 45 |
| 46 // Only load modules which aren't already cached for this sandbox |
| 47 if (!(key in cache)) |
| 48 cache[key] = SandboxedModule.require(moduleId, { |
| 49 cache: cache, |
| 50 globals: globals |
| 51 }); |
| 52 |
| 53 return cache[key]; |
| 54 }; |
| 55 }; |
OLD | NEW |