summaryrefslogtreecommitdiff
path: root/node_modules/node-retrieve-globals/retrieveGlobals.js
blob: 64571a826fbc811bf94f9e7f7f3503c5069f0856 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import vm from "vm";
import * as acorn from "acorn";
import * as walk from "acorn-walk";
import { ImportTransformer } from "esm-import-transformer";
import { createRequire, Module } from "module";

import { getWorkingDirectory } from "./util/getWorkingDirectory.js";
import { isSupported } from "./util/vmModules.js";

const IS_VM_MODULES_SUPPORTED = isSupported();

// `import` and `require` should both be relative to working directory (not this file)
const WORKING_DIRECTORY = getWorkingDirectory();

// TODO (feature) option to change `require` home base
const customRequire = createRequire(WORKING_DIRECTORY);

class RetrieveGlobals {
	constructor(code, options) {
		this.originalCode = code;

		// backwards compat
		if(typeof options === "string") {
			options = {
				filePath: options
			};
		}

		this.options = Object.assign({
			filePath: null,
			transformEsmImports: false,
		}, options);

		if(IS_VM_MODULES_SUPPORTED) {
			// Override: no code transformations if vm.Module works
			this.options.transformEsmImports = false;
		}

		// set defaults
		let acornOptions = {};
		if(IS_VM_MODULES_SUPPORTED || this.options.transformEsmImports) {
			acornOptions.sourceType = "module";
		}

		this.setAcornOptions(acornOptions);
		this.setCreateContextOptions();

		// transform `import ___ from ___` to `const ___ = await import(___)` to emulate *some* import syntax.
		// Doesn’t currently work with aliases (mod as name) or namespaced imports (* as name).
		if(this.options.transformEsmImports) {
			this.code = this.transformer.transformToDynamicImport();
		} else {
			this.code = this.originalCode;
		}
	}

	get transformer() {
		if(!this._transformer) {
			this._transformer =  new ImportTransformer(this.originalCode);
		}
		return this._transformer;
	}

	setAcornOptions(acornOptions) {
		this.acornOptions = Object.assign({
			ecmaVersion: "latest",
		}, acornOptions );
	}

	setCreateContextOptions(contextOptions) {
		this.createContextOptions = Object.assign({
			codeGeneration: {
				strings: false,
				wasm: false,
			}
		}, contextOptions );
	}

	static _getProxiedContext(context = {}, options = {}) {
		return new Proxy(context, {
			get(target, propertyName) {
				if(Reflect.has(target, propertyName)) {
					return Reflect.get(target, propertyName);
				}

				if(options.reuseGlobal && Reflect.has(global, propertyName)) {
					return global[propertyName];
				}
				if(options.addRequire && propertyName === "require") {
					return customRequire;
				}
			}
		});
	}

	// We prune function and variable declarations that aren’t globally declared
	// (our acorn walker could be improved to skip non-global declarations, but this method is easier for now)
	static _getGlobalVariablesReturnString(names, mode = "cjs") {
		let s = [`let __globals = {};`];
		for(let name of names) {
			s.push(`if( typeof ${name} !== "undefined") { __globals.${name} = ${name}; }`);
		}
		return `${s.join("\n")};${mode === "esm" ? "\nexport default __globals;" : "return __globals;"}`
	}

	_setContextPrototype(context) {
		// Context will fail isPlainObject and won’t be merged in the data cascade properly without this prototype set
		// See https://github.com/11ty/eleventy-utils/blob/main/src/IsPlainObject.js
		if(!context || typeof context !== "object" || Array.isArray(context)) {
			return;
		}
		if(context instanceof Date) {
			return;
		}

		if(!Object.getPrototypeOf(context).isPrototypeOf(Object.create({}))) {
			Object.setPrototypeOf(context, Object.prototype);
			// Go deep
			for(let key in context) {
				this._setContextPrototype(context[key]);
			}
		}
	}

	_getCode(code, options) {
		let { async: isAsync, globalNames, experimentalModuleApi, data } = Object.assign({
			async: true
		}, options);

		if(IS_VM_MODULES_SUPPORTED) {
			return `${code}

${globalNames ? RetrieveGlobals._getGlobalVariablesReturnString(globalNames, "esm") : ""}`;
		}

		let prefix = [];
		let argKeys = "";
		let argValues = "";

		// Don’t use this when vm.Module is stable (or if the code doesn’t have any imports!)
		if(experimentalModuleApi) {
			prefix = "module.exports = ";

			if(typeof data === "object") {
				let dataKeys = Object.keys(data);
				if(dataKeys) {
					argKeys = `{${dataKeys.join(",")}}`;
					argValues = JSON.stringify(data, function replacer(key, value) {
						if(typeof value === "function") {
							throw new Error(`When using \`experimentalModuleApi\`, context data must be JSON.stringify friendly. The "${key}" property was type \`function\`.`);
						}
						return value;
					});
				}
			}
		}

		return `${prefix}(${isAsync ? "async " : ""}function(${argKeys}) {
	${code}
	${globalNames ? RetrieveGlobals._getGlobalVariablesReturnString(globalNames, "cjs") : ""}
})(${argValues});`;
	}

	getGlobalNames(parsedAst) {
		let globalNames = new Set();

		let types = {
			FunctionDeclaration(node) {
				globalNames.add(node.id.name);
			},
			VariableDeclarator(node) {
				// destructuring assignment Array
				if(node.id.type === "ArrayPattern") {
					for(let prop of node.id.elements) {
						if(prop.type === "Identifier") {
							globalNames.add(prop.name);
						}
					}
				} else if(node.id.type === "ObjectPattern") {
					// destructuring assignment Object
					for(let prop of node.id.properties) {
						if(prop.type === "Property") {
							globalNames.add(prop.value.name);
						}
					}
				} else if(node.id.name) {
					globalNames.add(node.id.name);
				}
			},
			// if imports aren’t being transformed to variables assignment, we need those too
			ImportSpecifier(node) {
				globalNames.add(node.imported.name);
			}
		};

		walk.simple(parsedAst, types);

		return globalNames;
	}

	_getParseError(code, err) {
		// Acorn parsing error on script
		let metadata = [];
		if(this.options.filePath) {
			metadata.push(`file: ${this.options.filePath}`);
		}
		if(err?.loc?.line) {
			metadata.push(`line: ${err.loc.line}`);
		}
		if(err?.loc?.column) {
			metadata.push(`column: ${err.loc.column}`);
		}

		return new Error(`Had trouble parsing with "acorn"${metadata.length ? ` (${metadata.join(", ")})` : ""}:
Message: ${err.message}

${code}`);
	}

	async _getGlobalContext(data, options) {
		let {
			async: isAsync,
			reuseGlobal,
			dynamicImport,
			addRequire,
			experimentalModuleApi,
		} = Object.assign({
			// defaults
			async: true,

			reuseGlobal: false,

			// adds support for `require`
			addRequire: false,

			// allows dynamic import in `vm` (requires --experimental-vm-modules in Node v20.10+)
			// https://github.com/nodejs/node/issues/51154
			// TODO Another workaround possibility: We could use `import` outside of `vm` and inject the dependencies into context `data`
			dynamicImport: false,

			// Use Module._compile instead of vm
			// Workaround for: https://github.com/zachleat/node-retrieve-globals/issues/2
			// Warning: This method requires input `data` to be JSON stringify friendly.
			// Don’t use this if vm.Module is supported
			// Don’t use this if the code does not contain `import`s
			experimentalModuleApi: !IS_VM_MODULES_SUPPORTED && this.transformer.hasImports(),
		}, options);

		if(IS_VM_MODULES_SUPPORTED) {
			// Override: don’t use this when modules are allowed.
			experimentalModuleApi = false;
		}

		// These options are already supported by Module._compile
		if(experimentalModuleApi) {
			addRequire = false;
			dynamicImport = false;
		}

		if(reuseGlobal || addRequire) {
			// Re-use the parent `global` https://nodejs.org/api/globals.html
			data = RetrieveGlobals._getProxiedContext(data || {}, {
				reuseGlobal,
				addRequire,
			});
		}

		if(!data) {
			data = {};
		}

		let context;
		if(experimentalModuleApi || vm.isContext(data)) {
			context = data;
		} else {
			context = vm.createContext(data, this.createContextOptions);
		}

		let parseCode;
		let globalNames;

		try {
			parseCode = this._getCode(this.code, {
				async: isAsync,
			});

			let parsedAst = acorn.parse(parseCode, this.acornOptions);
			globalNames = this.getGlobalNames(parsedAst);
		} catch(e) {
			throw this._getParseError(parseCode, e);
		}

		try {
			let execCode = this._getCode(this.code, {
				async: isAsync,
				globalNames,
				experimentalModuleApi,
				data: context,
			});

			if(experimentalModuleApi) {
				let m = new Module();
				m._compile(execCode, WORKING_DIRECTORY);
				return m.exports;
			}

			let execOptions = {};
			if(dynamicImport) {
				// Warning: this option is part of the experimental modules API
				execOptions.importModuleDynamically = (specifier) => import(specifier);
			}

			if(IS_VM_MODULES_SUPPORTED) {
				// options.initializeImportMeta
				let m = new vm.SourceTextModule(execCode, {
					context,
					initializeImportMeta: (meta, module) => {
						meta.url = this.options.filePath || WORKING_DIRECTORY || module.identifier;
					},
					...execOptions,
				});

				// Thank you! https://stackoverflow.com/a/73282303/16711
				await m.link(async (specifier, referencingModule) => {
					const mod = await import(specifier);
					const exportNames = Object.keys(mod);
					return new vm.SyntheticModule(
						exportNames,
						function () {
							exportNames.forEach(key => {
								this.setExport(key, mod[key])
							});
						},
						{
							identifier: specifier,
							context: referencingModule.context
						}
					);
				});

				await m.evaluate();

				// TODO (feature) incorporate other esm `exports` here
				return m.namespace.default;
			}

			return vm.runInContext(execCode, context, execOptions);
		} catch(e) {
			let type = "cjs";
			if(IS_VM_MODULES_SUPPORTED) {
				type = "esm";
			} else if(experimentalModuleApi) {
				type = "cjs-experimental";
			}

			throw new Error(`Had trouble executing Node script (type: ${type}):
Message: ${e.message}

${this.code}`);
		}
	}

	async getGlobalContext(data, options) {
		let ret = await this._getGlobalContext(data, Object.assign({
			// whether or not the target code is executed asynchronously
			// note that vm.Module will always be async-friendly
			async: true,
		}, options));

		this._setContextPrototype(ret);

		return ret;
	}
}

export { RetrieveGlobals };