Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/stub-modules/common.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Migrate filterStorage tests Created Sept. 29, 2016, 7:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 let path = require("path");
Wladimir Palant 2016/09/30 09:37:54 I realized that this module doesn't belong into st
kzar 2016/10/03 13:46:16 You're right but nodeunit considers all JS files i
kzar 2016/10/03 16:43:14 Well I've had a go at this, I moved common.js and
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: () => { }},
Wladimir Palant 2016/09/30 09:37:54 I don't think that eslint will accept this. Accord
kzar 2016/10/03 13:46:17 Done.
9 navigator: {},
10 onShutdown: {add: () => { }},
11 Services: {obs: {addObserver: () => { }}},
12 XPCOMUtils: {generateQI: () => { }}
13 };
14
15 function addExports(exports)
16 {
17 // Unfortunately there's not a way for a source transformer to know the name
18 // of the module being transformed. This means we must add all extra exports
Wladimir Palant 2016/09/30 09:37:54 Well, source transformers are being bound to the S
kzar 2016/10/03 13:46:15 Oh cool, Done.
19 // to all modules, not ideal!
20 let code = "if (!('EXPORT' in exports))\n" +
21 " Object.defineProperty(exports, 'EXPORT', {get: () => EXPORT});";
Wladimir Palant 2016/09/30 09:37:55 Template literals allow multi-line input and are e
kzar 2016/10/03 13:46:16 Done.
22
23 return src => src + "\n" + exports.
24 map(name => code.replace(/EXPORT/g, name)).join("\n");
25 }
26
27 function rewriteRequires(source)
28 {
29 // Taken from EasyPasswords
30 // https://github.com/palant/easypasswords/blob/master/gulp-utils.js
Wladimir Palant 2016/09/30 09:37:55 I'm donating this code to the Adblock Plus project
kzar 2016/10/03 13:46:15 Acknowledged.
31 let modules = new Set(["events", "io"]);
Wladimir Palant 2016/09/30 09:37:55 Why only these two modules? I'd suggest that you g
kzar 2016/10/03 13:46:17 Still thinking about this one. (There's also the p
kzar 2016/10/03 17:47:43 Done.
32 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) = >
33 {
34 if (modules.has(request))
35 return prefix + request + ".js";
36 return match;
37 });
38 }
39
40 function libPath(moduleId)
41 {
42 return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js");
43 }
44
45 exports.createSandbox = function(extraExports)
46 {
47 // Shared require cache for this sandbox
48 let cache = {};
49
50 let options = {
51 cache: cache,
52 globals: globals,
53 sourceTransformers: [rewriteRequires]
54 };
55 if (extraExports)
56 options.sourceTransformers.push(addExports(extraExports));
57
58 return (moduleId, exports) =>
Wladimir Palant 2016/09/30 09:37:55 exports parameter is unused.
kzar 2016/10/03 13:46:16 Done.
59 {
60 let key = libPath(moduleId);
61
62 // Only load modules which aren't already cached for this sandbox
63 if (!(key in cache))
64 cache[key] = SandboxedModule.require(moduleId, options);
65
66 return cache[key];
Wladimir Palant 2016/09/30 09:37:54 Ok, if you need to cache modules manually then thi
kzar 2016/10/03 13:46:16 Ah really cool idea. We don't even need that pull
67 };
68 };
OLDNEW
« test/filterListener.js ('K') | « test/signatures.js ('k') | test/stub-modules/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld