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 fs = require("fs"); |
| 21 let path = require("path"); |
| 22 let SandboxedModule = require("sandboxed-module"); |
| 23 |
| 24 let globals = { |
| 25 atob: data => new Buffer(data, "base64").toString("binary"), |
| 26 btoa: data => new Buffer(data, "binary").toString("base64"), |
| 27 Ci: { |
| 28 }, |
| 29 Cu: { |
| 30 import: () => |
| 31 { |
| 32 } |
| 33 }, |
| 34 navigator: { |
| 35 }, |
| 36 onShutdown: { |
| 37 add: () => |
| 38 { |
| 39 } |
| 40 }, |
| 41 Services: { |
| 42 obs: { |
| 43 addObserver: () => |
| 44 { |
| 45 } |
| 46 } |
| 47 }, |
| 48 XPCOMUtils: { |
| 49 generateQI: () => |
| 50 { |
| 51 } |
| 52 } |
| 53 }; |
| 54 |
| 55 let knownModules = new Map(); |
| 56 for (let dir of [path.join("test", "stub-modules"), "lib"]) |
| 57 for (let file of fs.readdirSync(path.resolve(dir))) |
| 58 if (path.extname(file) == ".js") |
| 59 knownModules[path.basename(file, ".js")] = path.resolve(dir, file); |
| 60 |
| 61 function addExports(exports) |
| 62 { |
| 63 return function(source) |
| 64 { |
| 65 let extraExports = exports[path.basename(this.filename, ".js")]; |
| 66 if (extraExports) |
| 67 for (let name of extraExports) |
| 68 source += ` |
| 69 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
| 70 return source; |
| 71 }; |
| 72 } |
| 73 |
| 74 function rewriteRequires(source) |
| 75 { |
| 76 function escapeString(str) |
| 77 { |
| 78 return str.replace(/(["'\\])/g, "\\$1"); |
| 79 } |
| 80 |
| 81 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) =
> |
| 82 { |
| 83 if (request in knownModules) |
| 84 return prefix + escapeString(knownModules[request]); |
| 85 return match; |
| 86 }); |
| 87 } |
| 88 |
| 89 exports.createSandbox = function(extraExports) |
| 90 { |
| 91 let sourceTransformers = [rewriteRequires]; |
| 92 if (extraExports) |
| 93 sourceTransformers.push(addExports(extraExports)); |
| 94 |
| 95 // This module loads itself into a sandbox, keeping track of the require |
| 96 // function which can be used to load further modules into the sandbox. |
| 97 // (Before returning the require function we wrap it, allowing paths for known |
| 98 // modules to be omitted for convenience.) |
| 99 let sandboxedRequire = SandboxedModule.require("./common", { |
| 100 globals: globals, |
| 101 sourceTransformers: sourceTransformers |
| 102 }).require; |
| 103 return module => sandboxedRequire(knownModules[module] || module); |
| 104 }; |
| 105 |
| 106 exports.require = require; |
OLD | NEW |