-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetavm.js
189 lines (168 loc) · 5.17 KB
/
metavm.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
const vm = require('node:vm');
const fs = require('node:fs');
const fsp = fs.promises;
const path = require('node:path');
const CURDIR = '.' + path.sep;
const RUN_OPTIONS = { timeout: 1000 };
const CONTEXT_OPTIONS = {
codeGeneration: {
strings: false,
wasm: false,
},
};
const MODULE_TYPE = {
METARHIA: 1,
COMMONJS: 2,
ECMA: 3,
};
const DEFAULT = {
AbortController,
AbortSignal,
Event,
EventTarget,
MessageChannel,
MessageEvent,
MessagePort,
Buffer,
Blob,
FormData,
Headers,
Response,
Request,
ByteLengthQueuingStrategy,
URL,
URLSearchParams,
TextDecoder,
TextEncoder,
TextDecoderStream,
TextEncoderStream,
WebAssembly,
queueMicrotask,
setTimeout,
setImmediate,
setInterval,
clearTimeout,
clearImmediate,
clearInterval,
BroadcastChannel,
CompressionStream,
DecompressionStream,
CountQueuingStrategy,
fetch,
};
const NODE = { global, console, process };
const EMPTY_CONTEXT = vm.createContext(Object.freeze({}), CONTEXT_OPTIONS);
const COMMON_CONTEXT = vm.createContext(Object.freeze({ ...DEFAULT }));
const NODE_CONTEXT = vm.createContext(Object.freeze({ ...DEFAULT, ...NODE }));
class MetavmError extends Error {}
const createContext = (context, preventEscape = false) => {
if (context === undefined) return EMPTY_CONTEXT;
const options = preventEscape ? { microtaskMode: 'afterEvaluate' } : {};
return vm.createContext(context, { ...CONTEXT_OPTIONS, ...options });
};
const SRC_BEFORE = '((exports, require, module, __filename, __dirname) => { ';
const SRC_AFTER = '\n});';
const wrapSource = (src) => SRC_BEFORE + src + SRC_AFTER;
const USE_STRICT = `'use strict';\n`;
const useStrict = (src) => (src.startsWith(USE_STRICT) ? '' : USE_STRICT);
const addExt = (name) => {
if (name.toLocaleLowerCase().endsWith('.js')) return name;
return name + '.js';
};
const internalRequire = require;
class MetaScript {
constructor(name, src, options = {}) {
if (options.type === MODULE_TYPE.ECMA) {
throw new Error('ECMAScript modules is not supported');
}
this.name = name;
this.dirname = options.dirname || process.cwd();
this.relative = options.relative || '.';
this.type = options.type || MODULE_TYPE.METARHIA;
this.access = options.access || {};
const common = this.type === MODULE_TYPE.COMMONJS;
const strict = useStrict(src);
const code = common ? wrapSource(src) : `{\n${src}\n}`;
const lineOffset = strict === '' ? -1 : -2;
const scriptOptions = { filename: name, ...options, lineOffset };
this.script = new vm.Script(strict + code, scriptOptions);
this.context = options.context || createContext();
const runOptions = { ...RUN_OPTIONS, ...options };
const exports = this.script.runInContext(this.context, runOptions);
this.exports = common ? this.commonExports(exports) : exports;
}
commonExports(closure) {
const exports = {};
const module = { exports };
const require = this.createRequire();
const __filename = this.name;
const __dirname = path.dirname(__filename);
closure(exports, require, module, __filename, __dirname);
return module.exports || exports;
}
checkAccess(name) {
const dir = path.join(name);
for (const key of Object.keys(this.access)) {
const location = path.join(key);
if (dir.startsWith(location)) {
return Reflect.get(this.access, key);
}
}
return null;
}
createRequire() {
const { context, type } = this;
let { dirname, relative, access } = this;
const require = (module) => {
let name = module;
let lib = this.checkAccess(name);
if (lib instanceof Object) return lib;
const npm = !name.includes('.');
if (!npm) {
name = path.resolve(dirname, relative, addExt(name));
lib = this.checkAccess(CURDIR + path.relative(dirname, name));
if (lib instanceof Object) return lib;
}
if (!lib) throw new MetavmError(`Access denied '${module}'`);
try {
const absolute = internalRequire.resolve(name);
if (npm && absolute === name) return internalRequire(name);
relative = path.dirname(absolute);
if (npm) {
dirname = relative;
access = { ...access, [CURDIR]: true };
relative = '.';
}
const src = fs.readFileSync(absolute, 'utf8');
const opt = { context, type, dirname, relative, access };
const script = new MetaScript(name, src, opt);
return script.exports;
} catch (err) {
if (err instanceof MetavmError) throw err;
throw new MetavmError(`Cannot find module '${module}'`);
}
};
return require;
}
}
const createScript = (name, src, options) => new MetaScript(name, src, options);
const readScript = async (filePath, options) => {
const src = await fsp.readFile(filePath, 'utf8');
if (src === '') throw new SyntaxError(`File ${filePath} is empty`);
const name = options?.filename
? options.filename
: path.basename(filePath, '.js');
const script = new MetaScript(name, src, options);
return script;
};
module.exports = {
createContext,
MetaScript,
createScript,
EMPTY_CONTEXT,
COMMON_CONTEXT,
NODE_CONTEXT,
MODULE_TYPE,
readScript,
};