fake-node 0.1.1 → 0.2.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.
- package/index.js +10 -9
- package/index.ts +5 -1
- package/package.json +1 -1
package/index.js
CHANGED
@@ -56,8 +56,10 @@ const module_url = __importStar(require("url"));
|
|
56
56
|
const module_util = __importStar(require("util"));
|
57
57
|
const web_only_globals_json_1 = __importDefault(require("./web_only_globals.json"));
|
58
58
|
// todo: properly implement eval
|
59
|
-
const
|
60
|
-
const
|
59
|
+
const DEFAULT_GLOBALS = { Infinity, NaN, undefined, eval, isFinite, isNaN, parseFloat, parseInt, decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, escape, unescape, Object, Function, Boolean, Symbol, Error, AggregateError, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, Number, BigInt, Math, Date, String, RegExp, Array, Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array, Float32Array, Float64Array, Map, Set, WeakMap, WeakSet, ArrayBuffer, SharedArrayBuffer, DataView, Atomics, JSON, WeakRef, FinalizationRegistry, Promise, Reflect, Proxy, Intl, AbortController, Blob, ByteLengthQueuingStrategy, atob, BroadcastChannel, btoa, clearInterval, clearTimeout, CloseEvent, CompressionStream, console, CountQueuingStrategy, Crypto, crypto, CryptoKey, CustomEvent, DecompressionStream, Event, EventSource, EventTarget, fetch, FormData, Headers, localStorage, MessageChannel, MessageEvent, MessagePort, Navigator, navigator, PerformanceEntry, PerformanceMark, PerformanceMeasure, PerformanceObserver, PerformanceObserverEntryList, performance, queueMicrotask, ReadableByteStreamController, Response, Request, sessionStorage, setInterval, setTimeout, Storage, structuredClone, SubtleCrypto, DOMException, TextDecoder, TextDecoderStream, TextEncoder, TextEncoderStream, TransformStreamDefaultController, URL, URLSearchParams, WebAssembly, WebSocket, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter };
|
60
|
+
for (const name of web_only_globals_json_1.default) {
|
61
|
+
Object.defineProperty(DEFAULT_GLOBALS, name, { get: () => { throw new ReferenceError(`${name} is not defined`); } });
|
62
|
+
}
|
61
63
|
class Process {
|
62
64
|
fakeNode;
|
63
65
|
pid;
|
@@ -117,12 +119,8 @@ class FakeNode {
|
|
117
119
|
this.globalName = '__fakeNode_' + this.id + '__';
|
118
120
|
// @ts-ignore
|
119
121
|
globalThis[this.globalName] = this;
|
120
|
-
this.globals =
|
121
|
-
Object.assign(this.globals,
|
122
|
-
for (const name of web_only_globals_json_1.default) {
|
123
|
-
Object.defineProperty(this.globals, name, { get: () => { throw new ReferenceError(`${name} is not defined`); } });
|
124
|
-
}
|
125
|
-
Object.assign(this.globals, NODE_AND_WEB_GLOBALS);
|
122
|
+
this.globals = Object.create(DEFAULT_GLOBALS);
|
123
|
+
Object.assign(this.globals, { require: this.require, Buffer: module_buffer.Buffer });
|
126
124
|
}
|
127
125
|
require(module) {
|
128
126
|
if (module.startsWith('fake-node:')) {
|
@@ -138,7 +136,7 @@ class FakeNode {
|
|
138
136
|
return this.builtinModules.get(module);
|
139
137
|
}
|
140
138
|
else {
|
141
|
-
throw new Error(`cannot find module '${
|
139
|
+
throw new Error(`cannot find module '${module}'`);
|
142
140
|
}
|
143
141
|
}
|
144
142
|
getGlobals(pid) {
|
@@ -166,5 +164,8 @@ class FakeNode {
|
|
166
164
|
run(code) {
|
167
165
|
(new Process(this, { code, path: '/' })).run();
|
168
166
|
}
|
167
|
+
addModule(name, code) {
|
168
|
+
(new Process(this, { code, path: '/', module: name })).run();
|
169
|
+
}
|
169
170
|
}
|
170
171
|
exports.FakeNode = FakeNode;
|
package/index.ts
CHANGED
@@ -137,8 +137,12 @@ export class FakeNode {
|
|
137
137
|
return scope;
|
138
138
|
}
|
139
139
|
|
140
|
-
run(code: string) {
|
140
|
+
run(code: string): void {
|
141
141
|
(new Process(this, {code, path: '/'})).run();
|
142
142
|
}
|
143
143
|
|
144
|
+
addModule(name: string, code: string): void {
|
145
|
+
(new Process(this, {code, path: '/', module: name})).run();
|
146
|
+
}
|
147
|
+
|
144
148
|
}
|