OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 let path = require("path"); |
| 21 let SandboxedModule = require("sandboxed-module"); |
| 22 |
| 23 let globals = { |
| 24 atob: data => new Buffer(data, "base64").toString("binary"), |
| 25 btoa: data => new Buffer(data, "binary").toString("base64"), |
| 26 Ci: { |
| 27 }, |
| 28 Cu: { |
| 29 import: () => |
| 30 { |
| 31 } |
| 32 }, |
| 33 navigator: { |
| 34 }, |
| 35 onShutdown: { |
| 36 add: () => |
| 37 { |
| 38 } |
| 39 }, |
| 40 Services: { |
| 41 obs: { |
| 42 addObserver: () => |
| 43 { |
| 44 } |
| 45 } |
| 46 }, |
| 47 XPCOMUtils: { |
| 48 generateQI: () => |
| 49 { |
| 50 } |
| 51 } |
| 52 }; |
| 53 |
| 54 function libPath(moduleId) |
| 55 { |
| 56 return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js"); |
| 57 } |
| 58 |
| 59 function addExports(exports) |
| 60 { |
| 61 return function(source) |
| 62 { |
| 63 let extraExports = exports[path.basename(this.filename, ".js")]; |
| 64 if (extraExports) |
| 65 for (let name of extraExports) |
| 66 source += ` |
| 67 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
| 68 return source; |
| 69 }; |
| 70 } |
| 71 |
| 72 function rewriteRequires(source) |
| 73 { |
| 74 let modules = new Set(["events", "io"]); |
| 75 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) =
> |
| 76 { |
| 77 if (modules.has(request)) |
| 78 return prefix + request + ".js"; |
| 79 return match; |
| 80 }); |
| 81 } |
| 82 |
| 83 exports.createSandbox = function(extraExports) |
| 84 { |
| 85 let sourceTransformers = [rewriteRequires]; |
| 86 if (extraExports) |
| 87 sourceTransformers.push(addExports(extraExports)); |
| 88 |
| 89 // This module loads itself in a sandbox, then we return its require function |
| 90 // which our tests can then use to require further modules in the sandbox. |
| 91 return SandboxedModule.require("./common", { |
| 92 globals: globals, |
| 93 sourceTransformers: sourceTransformers |
| 94 }).require; |
| 95 }; |
| 96 |
| 97 exports.require = require; |
OLD | NEW |