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 }, |
| 9 Cu: { |
| 10 import: () => |
| 11 { |
| 12 } |
| 13 }, |
| 14 navigator: { |
| 15 }, |
| 16 onShutdown: { |
| 17 add: () => |
| 18 { |
| 19 } |
| 20 }, |
| 21 Services: { |
| 22 obs: { |
| 23 addObserver: () => |
| 24 { |
| 25 } |
| 26 } |
| 27 }, |
| 28 XPCOMUtils: { |
| 29 generateQI: () => |
| 30 { |
| 31 } |
| 32 } |
| 33 }; |
| 34 |
| 35 function libPath(moduleId) |
| 36 { |
| 37 return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js"); |
| 38 } |
| 39 |
| 40 function addExports(exports) |
| 41 { |
| 42 return function(source) |
| 43 { |
| 44 let extraExports = exports[path.basename(this.filename, ".js")]; |
| 45 if (extraExports) |
| 46 for (let name of extraExports) |
| 47 source += ` |
| 48 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
| 49 return source; |
| 50 }; |
| 51 } |
| 52 |
| 53 function rewriteRequires(source) |
| 54 { |
| 55 let modules = new Set(["events", "io"]); |
| 56 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) =
> |
| 57 { |
| 58 if (modules.has(request)) |
| 59 return prefix + request + ".js"; |
| 60 return match; |
| 61 }); |
| 62 } |
| 63 |
| 64 exports.createSandbox = function(extraExports) |
| 65 { |
| 66 let sourceTransformers = [rewriteRequires]; |
| 67 if (extraExports) |
| 68 sourceTransformers.push(addExports(extraExports)); |
| 69 |
| 70 // This module loads itself in a sandbox, then we return its require function |
| 71 // which our tests can then use to require further modules in the sandbox. |
| 72 return SandboxedModule.require("common", { |
| 73 globals: globals, |
| 74 sourceTransformers: sourceTransformers |
| 75 }).require; |
| 76 }; |
| 77 |
| 78 exports.require = require; |
OLD | NEW |