fake-node 0.2.0 → 0.3.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/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/index.d.ts +59 -0
- package/{index.js → lib/index.js} +114 -50
- package/lib/os.d.ts +191 -0
- package/lib/os.js +271 -0
- package/lib/process.d.ts +103 -0
- package/lib/process.js +223 -0
- package/lib/querystring.d.ts +7 -0
- package/lib/querystring.js +38 -0
- package/lib/web_only_globals.json +1049 -0
- package/package.json +9 -13
- package/src/fs.ts +517 -0
- package/src/index.ts +229 -0
- package/src/os.ts +266 -0
- package/src/process.ts +263 -0
- package/src/querystring.ts +36 -0
- package/src/util.ts +408 -0
- package/index.ts +0 -148
- package/tsconfig.json +0 -10
- /package/{web_only_globals.json → src/web_only_globals.json} +0 -0
package/index.ts
DELETED
@@ -1,148 +0,0 @@
|
|
1
|
-
|
2
|
-
import module_assert from 'assert';
|
3
|
-
import * as module_buffer from 'buffer';
|
4
|
-
// @ts-ignore
|
5
|
-
import * as module_path from 'path';
|
6
|
-
import * as module_punycode from 'punycode';
|
7
|
-
import * as module_querystring from 'querystring';
|
8
|
-
// @ts-ignore
|
9
|
-
import * as module_readline from 'readline';
|
10
|
-
// @ts-ignore
|
11
|
-
import * as module_stream from 'stream';
|
12
|
-
// @ts-ignore
|
13
|
-
import * as module_string_decoder from 'string_decoder';
|
14
|
-
import * as module_test from 'test';
|
15
|
-
// @ts-ignore
|
16
|
-
import * as module_url from 'url';
|
17
|
-
// @ts-ignore
|
18
|
-
import * as module_util from 'util';
|
19
|
-
import WEB_ONLY_GLOBALS from './web_only_globals.json';
|
20
|
-
|
21
|
-
|
22
|
-
// todo: properly implement eval
|
23
|
-
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};
|
24
|
-
for (const name of WEB_ONLY_GLOBALS) {
|
25
|
-
Object.defineProperty(DEFAULT_GLOBALS, name, {get: () => {throw new ReferenceError(`${name} is not defined`)}});
|
26
|
-
}
|
27
|
-
|
28
|
-
|
29
|
-
export class Process {
|
30
|
-
|
31
|
-
fakeNode: FakeNode;
|
32
|
-
pid: number;
|
33
|
-
argv: string[] = [];
|
34
|
-
argv0: string = '';
|
35
|
-
module: false | string;
|
36
|
-
code: string;
|
37
|
-
path: string;
|
38
|
-
|
39
|
-
constructor(fakeNode: FakeNode, {path = '', code, module}: {path?: string, code: string, module?: false | string}) {
|
40
|
-
this.fakeNode = fakeNode;
|
41
|
-
this.pid = fakeNode.nextPid;
|
42
|
-
fakeNode.nextPid++;
|
43
|
-
fakeNode.processes.set(this.pid, this);
|
44
|
-
this.code = code;
|
45
|
-
this.path = path;
|
46
|
-
this.module = module ?? false;
|
47
|
-
}
|
48
|
-
|
49
|
-
run(): void {
|
50
|
-
let code: string;
|
51
|
-
if (this.module) {
|
52
|
-
code = `with(${this.fakeNode.globalName}.getGlobals(${this.pid})){__fakeNode_process__.fakeNode.modules.set('${this.module},(function(){${this.code};return module.exports;})());}`;
|
53
|
-
} else {
|
54
|
-
code = `with(${this.fakeNode.globalName}.getGlobals(${this.pid})){(function(){${this.code}})();}`;
|
55
|
-
}
|
56
|
-
let elt = document.createElement('script');
|
57
|
-
elt.textContent = code;
|
58
|
-
document.body.appendChild(elt);
|
59
|
-
}
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
export class FakeNode {
|
65
|
-
|
66
|
-
static nextId: number = 0;
|
67
|
-
id: number;
|
68
|
-
globalName: string;
|
69
|
-
|
70
|
-
modules: Map<string, any> = new Map();
|
71
|
-
builtinModules: Map<string, any> = new Map<string, any>([
|
72
|
-
['assert', module_assert],
|
73
|
-
['buffer', module_buffer],
|
74
|
-
['path', module_path],
|
75
|
-
['punycode', module_punycode],
|
76
|
-
['querystring', module_querystring],
|
77
|
-
['readline', module_readline],
|
78
|
-
['stream', module_stream],
|
79
|
-
['string_decoder', module_string_decoder],
|
80
|
-
['test', module_test],
|
81
|
-
['url', module_url],
|
82
|
-
['util', module_util],
|
83
|
-
]);
|
84
|
-
|
85
|
-
globals: {[key: string]: any};
|
86
|
-
|
87
|
-
processes: Map<number, Process> = new Map();
|
88
|
-
nextPid: number = 3;
|
89
|
-
|
90
|
-
window: Window = window;
|
91
|
-
|
92
|
-
constructor() {
|
93
|
-
this.id = FakeNode.nextId;
|
94
|
-
FakeNode.nextId++;
|
95
|
-
this.globalName = '__fakeNode_' + this.id + '__';
|
96
|
-
// @ts-ignore
|
97
|
-
globalThis[this.globalName] = this;
|
98
|
-
this.globals = Object.create(DEFAULT_GLOBALS);
|
99
|
-
Object.assign(this.globals, {require: this.require, Buffer: module_buffer.Buffer});
|
100
|
-
}
|
101
|
-
|
102
|
-
require(module: string): any {
|
103
|
-
if (module.startsWith('fake-node:')) {
|
104
|
-
module = module.slice('fake-node:'.length);
|
105
|
-
} else if (module.startsWith('node:')) {
|
106
|
-
module = module.slice('node:'.length);
|
107
|
-
}
|
108
|
-
if (this.modules.has(module)) {
|
109
|
-
return this.modules.get(module);
|
110
|
-
} else if (this.builtinModules.has(module)) {
|
111
|
-
return this.builtinModules.get(module);
|
112
|
-
} else {
|
113
|
-
throw new Error(`cannot find module '${module}'`);
|
114
|
-
}
|
115
|
-
}
|
116
|
-
|
117
|
-
getGlobals(pid: number): object {
|
118
|
-
const process = this.processes.get(pid);
|
119
|
-
if (process === undefined) {
|
120
|
-
throw new TypeError(`nonexistent PID in FakeNode.getGlobals call: ${pid}. If you do not know why this occured, it is probably a bug in fake-node. Please report it at https://github.com/speedydelete/fake-node/issues.`);
|
121
|
-
}
|
122
|
-
let scope: {[key: string]: any} = Object.create(this.globals);
|
123
|
-
scope.global = scope;
|
124
|
-
scope.globalThis = scope;
|
125
|
-
scope.__fakeNode_process__ = process;
|
126
|
-
if (process.path !== '') {
|
127
|
-
const pathParts = process.path.split('/');
|
128
|
-
scope.__dirname = pathParts.slice(0, -1).join('/');
|
129
|
-
scope.__filename = pathParts[pathParts.length - 1];
|
130
|
-
}
|
131
|
-
if (process.module !== false) {
|
132
|
-
scope.module = {
|
133
|
-
exports: {}
|
134
|
-
};
|
135
|
-
scope.exports = scope.module.exports;
|
136
|
-
}
|
137
|
-
return scope;
|
138
|
-
}
|
139
|
-
|
140
|
-
run(code: string): void {
|
141
|
-
(new Process(this, {code, path: '/'})).run();
|
142
|
-
}
|
143
|
-
|
144
|
-
addModule(name: string, code: string): void {
|
145
|
-
(new Process(this, {code, path: '/', module: name})).run();
|
146
|
-
}
|
147
|
-
|
148
|
-
}
|
package/tsconfig.json
DELETED
File without changes
|