Index: test/common.js |
diff --git a/test/common.js b/test/common.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..59539b45206fccbb0b90f833a5e523ae54df296d |
--- /dev/null |
+++ b/test/common.js |
@@ -0,0 +1,107 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+let fs = require("fs"); |
+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: () => |
+ { |
+ } |
+ } |
+}; |
+ |
+let knownModules = new Map(); |
+for (let dir of [path.join(__dirname, "stub-modules"), |
+ path.join(__dirname, "..", "lib")]) |
+ for (let file of fs.readdirSync(path.resolve(dir))) |
+ if (path.extname(file) == ".js") |
+ knownModules[path.basename(file, ".js")] = path.resolve(dir, file); |
+ |
+function addExports(exports) |
+{ |
+ return function(source) |
+ { |
+ let extraExports = exports[path.basename(this.filename, ".js")]; |
+ if (extraExports) |
+ for (let name of extraExports) |
Wladimir Palant
2016/10/04 11:02:57
Wrong indentation here, single space instead of tw
kzar
2016/10/04 12:06:22
Whoops, Done.
|
+ source += ` |
+ Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
+ return source; |
+ }; |
+} |
+ |
+function rewriteRequires(source) |
+{ |
+ function escapeString(str) |
+ { |
+ return str.replace(/(["'\\])/g, "\\$1"); |
+ } |
+ |
+ return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) => |
+ { |
+ if (request in knownModules) |
+ return prefix + escapeString(knownModules[request]); |
+ return match; |
+ }); |
+} |
+ |
+exports.createSandbox = function(extraExports) |
+{ |
+ let sourceTransformers = [rewriteRequires]; |
+ if (extraExports) |
+ sourceTransformers.push(addExports(extraExports)); |
+ |
+ // This module loads itself into a sandbox, keeping track of the require |
+ // function which can be used to load further modules into the sandbox. |
+ // (Before returning the require function we wrap it, allowing paths for known |
+ // modules to be omitted for convenience.) |
Wladimir Palant
2016/10/04 11:02:57
I don't consider this a good idea. As I mentioned
kzar
2016/10/04 12:06:22
Done.
|
+ let sandboxedRequire = SandboxedModule.require("./common", { |
+ globals: globals, |
+ sourceTransformers: sourceTransformers |
+ }).require; |
+ return module => sandboxedRequire(knownModules[module] || module); |
+}; |
+ |
+exports.require = require; |