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.
Files changed (3) hide show
  1. package/index.js +10 -9
  2. package/index.ts +5 -1
  3. 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 ES_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 };
60
- const NODE_AND_WEB_GLOBALS = { 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 };
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 = { require: this.require, Buffer: module_buffer.Buffer };
121
- Object.assign(this.globals, ES_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 '${name}'`);
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-node",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "A fake version of Node that works in the browser",
5
5
  "license": "MIT",
6
6
  "author": "speedydelete",