vercel 31.0.3 → 31.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,469 +0,0 @@
1
- /* global host, data, VMError */
2
-
3
- 'use strict';
4
-
5
- const LocalError = Error;
6
- const LocalTypeError = TypeError;
7
- const LocalWeakMap = WeakMap;
8
-
9
- const {
10
- apply: localReflectApply,
11
- defineProperty: localReflectDefineProperty
12
- } = Reflect;
13
-
14
- const {
15
- set: localWeakMapSet,
16
- get: localWeakMapGet
17
- } = LocalWeakMap.prototype;
18
-
19
- const {
20
- isArray: localArrayIsArray
21
- } = Array;
22
-
23
- function uncurryThis(func) {
24
- return (thiz, ...args) => localReflectApply(func, thiz, args);
25
- }
26
-
27
- const localArrayPrototypeSlice = uncurryThis(Array.prototype.slice);
28
- const localArrayPrototypeIncludes = uncurryThis(Array.prototype.includes);
29
- const localArrayPrototypePush = uncurryThis(Array.prototype.push);
30
- const localArrayPrototypeIndexOf = uncurryThis(Array.prototype.indexOf);
31
- const localArrayPrototypeSplice = uncurryThis(Array.prototype.splice);
32
- const localStringPrototypeStartsWith = uncurryThis(String.prototype.startsWith);
33
- const localStringPrototypeSlice = uncurryThis(String.prototype.slice);
34
- const localStringPrototypeIndexOf = uncurryThis(String.prototype.indexOf);
35
-
36
- const {
37
- argv: optionArgv,
38
- env: optionEnv,
39
- console: optionConsole,
40
- vm,
41
- resolver,
42
- extensions
43
- } = data;
44
-
45
- function ensureSandboxArray(a) {
46
- return localArrayPrototypeSlice(a);
47
- }
48
-
49
- const globalPaths = ensureSandboxArray(resolver.globalPaths);
50
-
51
- class Module {
52
-
53
- constructor(id, path, parent) {
54
- this.id = id;
55
- this.filename = id;
56
- this.path = path;
57
- this.parent = parent;
58
- this.loaded = false;
59
- this.paths = path ? ensureSandboxArray(resolver.genLookupPaths(path)) : [];
60
- this.children = [];
61
- this.exports = {};
62
- }
63
-
64
- _updateChildren(child, isNew) {
65
- const children = this.children;
66
- if (children && (isNew || !localArrayPrototypeIncludes(children, child))) {
67
- localArrayPrototypePush(children, child);
68
- }
69
- }
70
-
71
- require(id) {
72
- return requireImpl(this, id, false);
73
- }
74
-
75
- }
76
-
77
- const originalRequire = Module.prototype.require;
78
- const cacheBuiltins = {__proto__: null};
79
-
80
- function requireImpl(mod, id, direct) {
81
- if (direct && mod.require !== originalRequire) {
82
- return mod.require(id);
83
- }
84
- const filename = resolver.resolve(mod, id, undefined, Module._extensions, direct);
85
- if (localStringPrototypeStartsWith(filename, 'node:')) {
86
- id = localStringPrototypeSlice(filename, 5);
87
- let nmod = cacheBuiltins[id];
88
- if (!nmod) {
89
- nmod = resolver.loadBuiltinModule(vm, id);
90
- if (!nmod) throw new VMError(`Cannot find module '${filename}'`, 'ENOTFOUND');
91
- cacheBuiltins[id] = nmod;
92
- }
93
- return nmod;
94
- }
95
-
96
- const cachedModule = Module._cache[filename];
97
- if (cachedModule !== undefined) {
98
- mod._updateChildren(cachedModule, false);
99
- return cachedModule.exports;
100
- }
101
-
102
- let nmod = cacheBuiltins[id];
103
- if (nmod) return nmod;
104
- nmod = resolver.loadBuiltinModule(vm, id);
105
- if (nmod) {
106
- cacheBuiltins[id] = nmod;
107
- return nmod;
108
- }
109
-
110
- const path = resolver.fs.dirname(filename);
111
- const module = new Module(filename, path, mod);
112
- resolver.registerModule(module, filename, path, mod, direct);
113
- mod._updateChildren(module, true);
114
- try {
115
- Module._cache[filename] = module;
116
- const handler = findBestExtensionHandler(filename);
117
- handler(module, filename);
118
- module.loaded = true;
119
- } catch (e) {
120
- delete Module._cache[filename];
121
- const children = mod.children;
122
- if (localArrayIsArray(children)) {
123
- const index = localArrayPrototypeIndexOf(children, module);
124
- if (index !== -1) {
125
- localArrayPrototypeSplice(children, index, 1);
126
- }
127
- }
128
- throw e;
129
- }
130
-
131
- return module.exports;
132
- }
133
-
134
- Module.builtinModules = ensureSandboxArray(resolver.getBuiltinModulesList());
135
- Module.globalPaths = globalPaths;
136
- Module._extensions = {__proto__: null};
137
- Module._cache = {__proto__: null};
138
-
139
- {
140
- const keys = Object.getOwnPropertyNames(extensions);
141
- for (let i = 0; i < keys.length; i++) {
142
- const key = keys[i];
143
- const handler = extensions[key];
144
- Module._extensions[key] = (mod, filename) => handler(mod, filename);
145
- }
146
- }
147
-
148
- function findBestExtensionHandler(filename) {
149
- const name = resolver.fs.basename(filename);
150
- for (let i = 0; (i = localStringPrototypeIndexOf(name, '.', i + 1)) !== -1;) {
151
- const ext = localStringPrototypeSlice(name, i);
152
- const handler = Module._extensions[ext];
153
- if (handler) return handler;
154
- }
155
- const js = Module._extensions['.js'];
156
- if (js) return js;
157
- const keys = Object.getOwnPropertyNames(Module._extensions);
158
- if (keys.length === 0) throw new VMError(`Failed to load '${filename}': Unknown type.`, 'ELOADFAIL');
159
- return Module._extensions[keys[0]];
160
- }
161
-
162
- function createRequireForModule(mod) {
163
- // eslint-disable-next-line no-shadow
164
- function require(id) {
165
- return requireImpl(mod, id, true);
166
- }
167
- function resolve(id, options) {
168
- return resolver.resolve(mod, id, options, Module._extensions, true);
169
- }
170
- require.resolve = resolve;
171
- function paths(id) {
172
- return ensureSandboxArray(resolver.lookupPaths(mod, id));
173
- }
174
- resolve.paths = paths;
175
-
176
- require.extensions = Module._extensions;
177
-
178
- require.cache = Module._cache;
179
-
180
- return require;
181
- }
182
-
183
- /**
184
- * Prepare sandbox.
185
- */
186
-
187
- const TIMERS = new LocalWeakMap();
188
-
189
- class Timeout {
190
- }
191
-
192
- class Interval {
193
- }
194
-
195
- class Immediate {
196
- }
197
-
198
- function clearTimer(timer) {
199
- const obj = localReflectApply(localWeakMapGet, TIMERS, [timer]);
200
- if (obj) {
201
- obj.clear(obj.value);
202
- }
203
- }
204
-
205
- // This is a function and not an arrow function, since the original is also a function
206
- // eslint-disable-next-line no-shadow
207
- global.setTimeout = function setTimeout(callback, delay, ...args) {
208
- if (typeof callback !== 'function') throw new LocalTypeError('"callback" argument must be a function');
209
- const obj = new Timeout(callback, args);
210
- const cb = () => {
211
- localReflectApply(callback, null, args);
212
- };
213
- const tmr = host.setTimeout(cb, delay);
214
-
215
- const ref = {
216
- __proto__: null,
217
- clear: host.clearTimeout,
218
- value: tmr
219
- };
220
-
221
- localReflectApply(localWeakMapSet, TIMERS, [obj, ref]);
222
- return obj;
223
- };
224
-
225
- // eslint-disable-next-line no-shadow
226
- global.setInterval = function setInterval(callback, interval, ...args) {
227
- if (typeof callback !== 'function') throw new LocalTypeError('"callback" argument must be a function');
228
- const obj = new Interval();
229
- const cb = () => {
230
- localReflectApply(callback, null, args);
231
- };
232
- const tmr = host.setInterval(cb, interval);
233
-
234
- const ref = {
235
- __proto__: null,
236
- clear: host.clearInterval,
237
- value: tmr
238
- };
239
-
240
- localReflectApply(localWeakMapSet, TIMERS, [obj, ref]);
241
- return obj;
242
- };
243
-
244
- // eslint-disable-next-line no-shadow
245
- global.setImmediate = function setImmediate(callback, ...args) {
246
- if (typeof callback !== 'function') throw new LocalTypeError('"callback" argument must be a function');
247
- const obj = new Immediate();
248
- const cb = () => {
249
- localReflectApply(callback, null, args);
250
- };
251
- const tmr = host.setImmediate(cb);
252
-
253
- const ref = {
254
- __proto__: null,
255
- clear: host.clearImmediate,
256
- value: tmr
257
- };
258
-
259
- localReflectApply(localWeakMapSet, TIMERS, [obj, ref]);
260
- return obj;
261
- };
262
-
263
- // eslint-disable-next-line no-shadow
264
- global.clearTimeout = function clearTimeout(timeout) {
265
- clearTimer(timeout);
266
- };
267
-
268
- // eslint-disable-next-line no-shadow
269
- global.clearInterval = function clearInterval(interval) {
270
- clearTimer(interval);
271
- };
272
-
273
- // eslint-disable-next-line no-shadow
274
- global.clearImmediate = function clearImmediate(immediate) {
275
- clearTimer(immediate);
276
- };
277
-
278
- const localProcess = host.process;
279
-
280
- function vmEmitArgs(event, args) {
281
- const allargs = [event];
282
- for (let i = 0; i < args.length; i++) {
283
- if (!localReflectDefineProperty(allargs, i + 1, {
284
- __proto__: null,
285
- value: args[i],
286
- writable: true,
287
- enumerable: true,
288
- configurable: true
289
- })) throw new LocalError('Unexpected');
290
- }
291
- return localReflectApply(vm.emit, vm, allargs);
292
- }
293
-
294
- const LISTENERS = new LocalWeakMap();
295
- const LISTENER_HANDLER = new LocalWeakMap();
296
-
297
- /**
298
- *
299
- * @param {*} name
300
- * @param {*} handler
301
- * @this process
302
- * @return {this}
303
- */
304
- function addListener(name, handler) {
305
- if (name !== 'beforeExit' && name !== 'exit') {
306
- throw new LocalError(`Access denied to listen for '${name}' event.`);
307
- }
308
-
309
- let cb = localReflectApply(localWeakMapGet, LISTENERS, [handler]);
310
- if (!cb) {
311
- cb = () => {
312
- handler();
313
- };
314
- localReflectApply(localWeakMapSet, LISTENER_HANDLER, [cb, handler]);
315
- localReflectApply(localWeakMapSet, LISTENERS, [handler, cb]);
316
- }
317
-
318
- localProcess.on(name, cb);
319
-
320
- return this;
321
- }
322
-
323
- /**
324
- *
325
- * @this process
326
- * @return {this}
327
- */
328
- // eslint-disable-next-line no-shadow
329
- function process() {
330
- return this;
331
- }
332
-
333
- const baseUptime = localProcess.uptime();
334
-
335
- // FIXME wrong class structure
336
- global.process = {
337
- __proto__: process.prototype,
338
- argv: optionArgv !== undefined ? optionArgv : [],
339
- title: localProcess.title,
340
- version: localProcess.version,
341
- versions: localProcess.versions,
342
- arch: localProcess.arch,
343
- platform: localProcess.platform,
344
- env: optionEnv !== undefined ? optionEnv : {},
345
- pid: localProcess.pid,
346
- features: localProcess.features,
347
- nextTick: function nextTick(callback, ...args) {
348
- if (typeof callback !== 'function') {
349
- throw new LocalError('Callback must be a function.');
350
- }
351
-
352
- localProcess.nextTick(()=>{
353
- localReflectApply(callback, null, args);
354
- });
355
- },
356
- hrtime: function hrtime(time) {
357
- return localProcess.hrtime(time);
358
- },
359
- uptime: function uptime() {
360
- return localProcess.uptime() - baseUptime;
361
- },
362
- cwd: function cwd() {
363
- return localProcess.cwd();
364
- },
365
- addListener,
366
- on: addListener,
367
-
368
- once: function once(name, handler) {
369
- if (name !== 'beforeExit' && name !== 'exit') {
370
- throw new LocalError(`Access denied to listen for '${name}' event.`);
371
- }
372
-
373
- let triggered = false;
374
- const cb = () => {
375
- if (triggered) return;
376
- triggered = true;
377
- localProcess.removeListener(name, cb);
378
- handler();
379
- };
380
- localReflectApply(localWeakMapSet, LISTENER_HANDLER, [cb, handler]);
381
-
382
- localProcess.on(name, cb);
383
-
384
- return this;
385
- },
386
-
387
- listeners: function listeners(name) {
388
- if (name !== 'beforeExit' && name !== 'exit') {
389
- // Maybe add ({__proto__:null})[name] to throw when name fails in https://tc39.es/ecma262/#sec-topropertykey.
390
- return [];
391
- }
392
-
393
- // Filter out listeners, which were not created in this sandbox
394
- const all = localProcess.listeners(name);
395
- const filtered = [];
396
- let j = 0;
397
- for (let i = 0; i < all.length; i++) {
398
- const h = localReflectApply(localWeakMapGet, LISTENER_HANDLER, [all[i]]);
399
- if (h) {
400
- if (!localReflectDefineProperty(filtered, j, {
401
- __proto__: null,
402
- value: h,
403
- writable: true,
404
- enumerable: true,
405
- configurable: true
406
- })) throw new LocalError('Unexpected');
407
- j++;
408
- }
409
- }
410
- return filtered;
411
- },
412
-
413
- removeListener: function removeListener(name, handler) {
414
- if (name !== 'beforeExit' && name !== 'exit') {
415
- return this;
416
- }
417
-
418
- const cb = localReflectApply(localWeakMapGet, LISTENERS, [handler]);
419
- if (cb) localProcess.removeListener(name, cb);
420
-
421
- return this;
422
- },
423
-
424
- umask: function umask() {
425
- if (arguments.length) {
426
- throw new LocalError('Access denied to set umask.');
427
- }
428
-
429
- return localProcess.umask();
430
- }
431
- };
432
-
433
- if (optionConsole === 'inherit') {
434
- global.console = host.console;
435
- } else if (optionConsole === 'redirect') {
436
- global.console = {
437
- debug(...args) {
438
- vmEmitArgs('console.debug', args);
439
- },
440
- log(...args) {
441
- vmEmitArgs('console.log', args);
442
- },
443
- info(...args) {
444
- vmEmitArgs('console.info', args);
445
- },
446
- warn(...args) {
447
- vmEmitArgs('console.warn', args);
448
- },
449
- error(...args) {
450
- vmEmitArgs('console.error', args);
451
- },
452
- dir(...args) {
453
- vmEmitArgs('console.dir', args);
454
- },
455
- time() {},
456
- timeEnd() {},
457
- trace(...args) {
458
- vmEmitArgs('console.trace', args);
459
- }
460
- };
461
- }
462
-
463
- return {
464
- __proto__: null,
465
- Module,
466
- jsonParse: JSON.parse,
467
- createRequireForModule,
468
- requireImpl
469
- };