-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
moduleScript.cjs
106 lines (85 loc) · 3.51 KB
/
moduleScript.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { Module } = require("module");
const vm = require("vm");
const { RetrieveGlobals } = require("node-retrieve-globals");
const { ProxyData } = require("./proxyData.cjs");
const { FasterVmContext } = require("./fasterVmContext.cjs");
let fasterVmContextGlobal = new FasterVmContext();
class ModuleScript {
static CJS_MODULE_EXPORTS = "module.exports = ";
static ESM_EXPORT_DEFAULT = "export default ";
static FUNCTION_REGEX = /^(?:async )?function\s?\S*\(/;
static getGlobals() {
let context = {
require: function(target) {
const path = require("path");
// change relative paths to be relative to the root project dir
// module paths are always / and not \\ on Windows, see https://github.com/nodejs/node/issues/6049#issuecomment-205778576
if(target.startsWith("./") || target.startsWith("../")) {
target = path.join(process.cwd(), target);
}
return require(target)
}
};
return context;
}
static async evaluateScriptAndReturnAllGlobals(code, filePath, data) {
let nodeGlobals = new RetrieveGlobals(code, filePath);
// returns promise
return nodeGlobals.getGlobalContext(data, {
reuseGlobal: true, // re-use Node.js `global`, important if you want `console.log` to log to your console as expected.
dynamicImport: true, // allows `import()`
});
}
static async evaluateScriptInline(content, data, errorString, scriptContextKey) {
// no difference yet
return ModuleScript.evaluateScript(content, data, errorString, scriptContextKey);
}
static async evaluateScript(content, data, errorString, scriptContextKey) {
try {
let proxy = new ProxyData();
proxy.addGlobal(ModuleScript.getGlobals());
proxy.addGlobal(global);
let contextData = proxy.getData(data);
let script = new vm.Script(content);
// The downstream code being evaluated here may return a promise!
let returnValue;
if(scriptContextKey) {
returnValue = fasterVmContextGlobal.executeScriptWithData(script, contextData, scriptContextKey);
} else {
returnValue = fasterVmContextGlobal.executeScriptExpensivelyInNewContext(script, contextData);
}
return { returns: await returnValue, context: contextData };
} catch(e) {
// Issue #45: very defensive error message here. We only throw this error when an error is thrown during compilation.
if(e.message === "Unexpected end of input" && content.match(/\bclass\b/) && !content.match(/\bclass\b\s*\{/)) {
throw new Error(`${errorString ? `${errorString} ` : ""}\`class\` is a reserved word in JavaScript. Change \`class\` to \`this.class\` instead!`);
}
throw new Error(`${errorString}
Original error message: ${e.message}`);
}
}
// TODO use the `vm` approach from `evaluateAttribute` above.
static getModule(content, filePath) {
let m = new Module();
// This requires CJS (as the internal render function is technically CJS, too)
m.paths = module.paths;
let trimmed = content.trim();
// replace `export default` with `module.exports`
if(trimmed.startsWith(ModuleScript.ESM_EXPORT_DEFAULT)) {
trimmed = `module.exports = ${trimmed.slice(ModuleScript.ESM_EXPORT_DEFAULT.length)}`
}
// Implied CJS
if(!trimmed.startsWith(ModuleScript.CJS_MODULE_EXPORTS)) {
if(trimmed.match(ModuleScript.FUNCTION_REGEX) != null) {
trimmed = ModuleScript.CJS_MODULE_EXPORTS + trimmed;
}
}
try {
m._compile(trimmed, filePath);
return m.exports;
} catch(e) {
throw new Error(`Error parsing WebC scripted render function (${filePath}): ${e.toString()}\n` + content)
}
}
}
module.exports = { ModuleScript };