fake-node 0.2.0 → 0.4.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/_fs.d.ts +254 -0
- package/lib/_fs.js +750 -0
- package/lib/_fs.js.map +1 -0
- package/lib/buffer.d.ts +9 -0
- package/lib/buffer.js +12 -0
- package/lib/buffer.js.map +1 -0
- package/lib/fs.d.ts +110 -0
- package/lib/fs.js +223 -0
- package/lib/fs.js.map +1 -0
- package/lib/index.d.ts +80 -0
- package/lib/index.js +311 -0
- package/lib/index.js.map +1 -0
- package/lib/os.d.ts +191 -0
- package/lib/os.js +261 -0
- package/lib/os.js.map +1 -0
- package/lib/path.d.ts +55 -0
- package/lib/path.js +105 -0
- package/lib/path.js.map +1 -0
- package/lib/process.d.ts +103 -0
- package/lib/process.js +216 -0
- package/lib/process.js.map +1 -0
- package/lib/querystring.d.ts +7 -0
- package/lib/querystring.js +39 -0
- package/lib/querystring.js.map +1 -0
- package/lib/util.d.ts +145 -0
- package/lib/util.js +460 -0
- package/lib/util.js.map +1 -0
- package/lib/web_only_globals.json +1049 -0
- package/package.json +12 -13
- package/src/_fs.ts +852 -0
- package/src/buffer.ts +13 -0
- package/src/fs.ts +230 -0
- package/src/in_fake_node.d.ts +12 -0
- package/src/index.ts +321 -0
- package/src/os.ts +259 -0
- package/src/path.ts +141 -0
- package/src/process.ts +245 -0
- package/src/querystring.ts +36 -0
- package/src/util.ts +521 -0
- package/index.js +0 -171
- package/index.ts +0 -148
- package/tsconfig.json +0 -10
- /package/{web_only_globals.json → src/web_only_globals.json} +0 -0
package/src/util.ts
ADDED
@@ -0,0 +1,521 @@
|
|
1
|
+
|
2
|
+
/// <reference path="./in_fake_node.d.ts" />
|
3
|
+
import {emitWarning} from './process';
|
4
|
+
|
5
|
+
export type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array;
|
6
|
+
|
7
|
+
export type Callback = (error: Error | null, value: unknown) => void;
|
8
|
+
|
9
|
+
function shouldBeCloneable(value: unknown): boolean {
|
10
|
+
return value?.constructor === Object || value === undefined || value === null || typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string' || typeof value === 'bigint' || Array.isArray(value) || value instanceof ArrayBuffer || value instanceof Boolean || value instanceof DataView || value instanceof Date || value instanceof Error || value instanceof Map || value instanceof Number || value instanceof RegExp || value instanceof Set || value instanceof String || value instanceof Object.getPrototypeOf(Uint8Array) || value instanceof AudioData || value instanceof Blob || value instanceof CryptoKey || value instanceof DOMException || value instanceof DOMMatrix || value instanceof DOMMatrixReadOnly || value instanceof DOMPoint || value instanceof DOMPointReadOnly || value instanceof DOMQuad || value instanceof DOMRect || value instanceof DOMRectReadOnly || value instanceof EncodedAudioChunk || value instanceof EncodedVideoChunk || value instanceof File || value instanceof FileList || value instanceof FileSystemDirectoryHandle || value instanceof FileSystemFileHandle || value instanceof FileSystemHandle || value instanceof ImageBitmap || value instanceof ImageData || value instanceof RTCCertificate || value instanceof RTCEncodedAudioFrame || value instanceof RTCEncodedVideoFrame || value instanceof VideoFrame || value instanceof WebTransportError;
|
11
|
+
}
|
12
|
+
|
13
|
+
export function callbackify(original: (...args: any[]) => Promise<any>): (...args: [...any[], Callback]) => void {
|
14
|
+
return function(...args: [...any[], Callback]): void {
|
15
|
+
const callback = args[args.length - 1];
|
16
|
+
original(...args.slice(0, -1)).then((value: any) => callback(null, value)).catch((reason) => callback(reason instanceof Error ?
|
17
|
+
reason : new Error(reason), null));
|
18
|
+
};
|
19
|
+
}
|
20
|
+
|
21
|
+
export function debuglog(section: string, callback: (message: string) => void = console.log): ((message: string) => void) & {enabled: boolean} {
|
22
|
+
if (typeof __fakeNode_process__.env.NODE_DEBUG === 'string' && __fakeNode_process__.env.NODE_DEBUG.includes(section)) {
|
23
|
+
return Object.assign(function(message: string): void {
|
24
|
+
callback(`${__fakeNode_process__.env.NODE_DEBUG.toUpperCase()} ${__fakeNode_process__.pid}: ${message}`);
|
25
|
+
}, {enabled: true});
|
26
|
+
} else {
|
27
|
+
return Object.assign(() => {}, {enabled: true});
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
export {debuglog as debug};
|
32
|
+
|
33
|
+
export function deprecate(fn: Function, msg: string, code?: string): Function {
|
34
|
+
return function(...args: any[]): any {
|
35
|
+
emitWarning('DeprecationWarning', {type: msg, code: code});
|
36
|
+
return fn(...args);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
export function format(format: string, ...args: unknown[]) {
|
41
|
+
return formatWithOptions({}, format, ...args);
|
42
|
+
}
|
43
|
+
|
44
|
+
export function formatWithOptions(inspectOptions: InspectOptions, format: string, ...args: unknown[]): string {
|
45
|
+
let out = '';
|
46
|
+
let i = 0;
|
47
|
+
let j = 0;
|
48
|
+
while (i < format.length && j < args.length) {
|
49
|
+
if (format[i] === '%') {
|
50
|
+
i++;
|
51
|
+
const type = format[i];
|
52
|
+
const value = args[j];
|
53
|
+
j++;
|
54
|
+
if (type === 's') {
|
55
|
+
if (typeof value === 'bigint') {
|
56
|
+
out += value + 'n';
|
57
|
+
} else if (value === 0 && 1 / value === -Infinity) {
|
58
|
+
out += '-0';
|
59
|
+
} else if ((typeof value === 'object' && value !== null) || typeof value === 'function') {
|
60
|
+
out += inspect(value, {depth: 0, colors: false, compat: 3});
|
61
|
+
} else {
|
62
|
+
out += String(i);
|
63
|
+
}
|
64
|
+
} else if (type === 'd') {
|
65
|
+
if (typeof value === 'bigint') {
|
66
|
+
out += value + 'n';
|
67
|
+
} else if (typeof value === 'symbol') {
|
68
|
+
out += value.toString();
|
69
|
+
} else {
|
70
|
+
out += Number(value);
|
71
|
+
}
|
72
|
+
} else if (type === 'i') {
|
73
|
+
if (typeof value === 'bigint') {
|
74
|
+
out += value + 'n';
|
75
|
+
} else if (typeof value === 'symbol') {
|
76
|
+
out += value.toString();
|
77
|
+
} else {
|
78
|
+
out += parseInt(String(value), 10);
|
79
|
+
}
|
80
|
+
} else if (type === 'f') {
|
81
|
+
if (typeof value === 'symbol') {
|
82
|
+
out += value.toString();
|
83
|
+
} else {
|
84
|
+
out += parseFloat(String(value));
|
85
|
+
}
|
86
|
+
} else if (type === 'j') {
|
87
|
+
try {
|
88
|
+
out += JSON.stringify(value);
|
89
|
+
} catch (error) {
|
90
|
+
if (error instanceof TypeError && (error.message.includes('circular') || error.message.includes('cyclic'))) {
|
91
|
+
out += '[Circular]';
|
92
|
+
} else {
|
93
|
+
throw error;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
} else if (type === 'o') {
|
97
|
+
out += inspect(value, {showHidden: true, showProxy: true});
|
98
|
+
} else if (type === 'O') {
|
99
|
+
out += inspect(value, inspectOptions);
|
100
|
+
} else {
|
101
|
+
j--;
|
102
|
+
}
|
103
|
+
} else {
|
104
|
+
out += format[i];
|
105
|
+
}
|
106
|
+
i++;
|
107
|
+
}
|
108
|
+
return out + format.slice(i);
|
109
|
+
}
|
110
|
+
|
111
|
+
type CallSites = {functionName: string, scriptName: string, scriptId: string, lineNumber: number, columnNumber: number}[];
|
112
|
+
|
113
|
+
export function getCallSites(options?: {sourceMap?: boolean}): CallSites;
|
114
|
+
export function getCallSites(frameCount: number, options?: {sourceMap?: boolean}): CallSites;
|
115
|
+
export function getCallSites(frameCountOrOptions?: number | {sourceMap?: boolean}, options: {sourceMap?: boolean} = {}): CallSites {
|
116
|
+
throw new TypeError('util.getCallSites is not supported in fake-node');
|
117
|
+
}
|
118
|
+
|
119
|
+
export function getSystemErrorName(error: number): string {
|
120
|
+
throw new TypeError('util.getSystemErrorName is not supported in fake-node');
|
121
|
+
}
|
122
|
+
|
123
|
+
export function getSystemErrorMap(): Map<number, string> {
|
124
|
+
throw new TypeError('util.getSystemErrorMap is not supported in fake-node');
|
125
|
+
}
|
126
|
+
|
127
|
+
export function getSystemErrorMessage(error: number): string {
|
128
|
+
throw new TypeError('util.getSystemErrorMessage is not supported in fake-node');
|
129
|
+
}
|
130
|
+
|
131
|
+
export function inherits(constructor: Function, superConstructor: Function) {
|
132
|
+
Object.setPrototypeOf(constructor.prototype, superConstructor.prototype);
|
133
|
+
}
|
134
|
+
|
135
|
+
interface InspectOptions {
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
export function inspect(value: unknown, options: InspectOptions = {}): string {
|
140
|
+
throw new TypeError('util.inspect is not supported in fake-node');
|
141
|
+
}
|
142
|
+
|
143
|
+
export function isDeepStrictEqual(val1: unknown, val2: unknown): boolean {
|
144
|
+
if ((typeof val1 === 'object' && val1 !== null) || typeof val1 === 'function') {
|
145
|
+
if (!(typeof val2 === 'object' && val2 !== null) && !(typeof val2 === 'function')) {
|
146
|
+
return false;
|
147
|
+
}
|
148
|
+
if ((Symbol.toStringTag in val1 && Symbol.toStringTag in val2) && val1[Symbol.toStringTag] !== val2[Symbol.toStringTag]) {
|
149
|
+
return false;
|
150
|
+
}
|
151
|
+
if (Object.getPrototypeOf(val1) !== Object.getPrototypeOf(val2)) {
|
152
|
+
return false;
|
153
|
+
}
|
154
|
+
if (val1 instanceof Map) {
|
155
|
+
if (!(val2 instanceof Map)) {
|
156
|
+
throw new TypeError(`this error should not occur`);
|
157
|
+
}
|
158
|
+
for (const [key, value] of val1.entries()) {
|
159
|
+
if (!val2.has(key)) {
|
160
|
+
return false;
|
161
|
+
}
|
162
|
+
return isDeepStrictEqual(value, val2.get(key));
|
163
|
+
}
|
164
|
+
}
|
165
|
+
if (val1 instanceof Set) {
|
166
|
+
if (!(val2 instanceof Set)) {
|
167
|
+
throw new TypeError(`this error should not occur`);
|
168
|
+
}
|
169
|
+
for (const item of val1) {
|
170
|
+
let found = false;
|
171
|
+
for (const item2 of val2) {
|
172
|
+
if (item === item2) {
|
173
|
+
found = true;
|
174
|
+
break;
|
175
|
+
}
|
176
|
+
}
|
177
|
+
if (!found) {
|
178
|
+
return false;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
return true;
|
182
|
+
}
|
183
|
+
const properties = Reflect.ownKeys(val1);
|
184
|
+
const val2Properties = (Object.getOwnPropertyNames(val2) as (string | symbol)[]).concat(Object.getOwnPropertySymbols(val2));
|
185
|
+
if (!properties.every((prop: string | symbol) => val2Properties.includes(prop))) {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
for (const property of properties) {
|
189
|
+
// @ts-ignore
|
190
|
+
if (!isDeepStrictEqual(val1[property], val2[property])) {
|
191
|
+
return false;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
if (types.isBoxedPrimitive(val1)) {
|
195
|
+
if (!types.isBoxedPrimitive(val2)) {
|
196
|
+
return false;
|
197
|
+
}
|
198
|
+
return Object.is(val1.constructor(val1), val2.constructor(val2));
|
199
|
+
}
|
200
|
+
return true;
|
201
|
+
} else if ((typeof val2 === 'object' && val2 !== null) || typeof val2 === 'function') {
|
202
|
+
return false;
|
203
|
+
} else {
|
204
|
+
return Object.is(val1, val2);
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
export class MIMEType {
|
209
|
+
|
210
|
+
constructor() {
|
211
|
+
throw new TypeError('util.MIMEType is not supported in fake-node');
|
212
|
+
}
|
213
|
+
|
214
|
+
}
|
215
|
+
|
216
|
+
export class MIMEParams {
|
217
|
+
|
218
|
+
constructor() {
|
219
|
+
throw new TypeError('util.MIMEParams is not supported in fake-node');
|
220
|
+
}
|
221
|
+
|
222
|
+
}
|
223
|
+
|
224
|
+
interface ParseArgsConfig {
|
225
|
+
args?: string[];
|
226
|
+
options: {
|
227
|
+
[key: string]: {
|
228
|
+
short?: string;
|
229
|
+
} & (({multiple: false} & ({
|
230
|
+
type: 'string';
|
231
|
+
default?: string;
|
232
|
+
} | {
|
233
|
+
type: 'boolean';
|
234
|
+
default?: boolean;
|
235
|
+
})) | ({multiple: true} & ({
|
236
|
+
type: 'string';
|
237
|
+
default?: string[];
|
238
|
+
} | {
|
239
|
+
type: 'boolean';
|
240
|
+
default?: boolean[];
|
241
|
+
}))
|
242
|
+
);
|
243
|
+
};
|
244
|
+
strict?: boolean;
|
245
|
+
allowPositionals?: boolean;
|
246
|
+
allowNegative?: boolean;
|
247
|
+
tokens?: boolean;
|
248
|
+
}
|
249
|
+
|
250
|
+
export function parseArgs(config?: ParseArgsConfig): {values: {[key: string]: string | boolean}, positionals: string[], tokens?: object[]} {
|
251
|
+
throw new TypeError('util.parseArgs is not supported in fake-node');
|
252
|
+
}
|
253
|
+
|
254
|
+
export function parseEnv(content: string): {[key: string]: string} {
|
255
|
+
throw new TypeError('util.parseEnv is not supported in fake-node');
|
256
|
+
}
|
257
|
+
|
258
|
+
export function promisify(original: ((...args: [...any[], Callback]) => void) & {[func: typeof promisify.custom]: (...args: any[]) => Promise<any>}): (...args: any[]) => Promise<any> {
|
259
|
+
if (promisify.custom in original) {
|
260
|
+
return original[promisify.custom];
|
261
|
+
}
|
262
|
+
return function(...args: any[]): Promise<any> {
|
263
|
+
return new Promise((resolve, reject) => {
|
264
|
+
original(args, (error: Error | null, value: unknown) => {
|
265
|
+
if (error instanceof Error) {
|
266
|
+
reject(error);
|
267
|
+
} else {
|
268
|
+
resolve(value);
|
269
|
+
}
|
270
|
+
});
|
271
|
+
});
|
272
|
+
};
|
273
|
+
}
|
274
|
+
promisify.custom = Symbol.for('nodejs.util.promisify.custom');
|
275
|
+
|
276
|
+
export function stripVTControlCharacters(str: string): string {
|
277
|
+
throw new TypeError('util.stripVTControlCharacters is not supported in fake-node');
|
278
|
+
}
|
279
|
+
|
280
|
+
export function styleText(format: string | Array<unknown>, text: string, options?: {validateStream: boolean, stream: unknown}): string {
|
281
|
+
throw new TypeError('util.styleText is not supported in fake-node');
|
282
|
+
}
|
283
|
+
|
284
|
+
const TextDecoder_ = TextDecoder;
|
285
|
+
const TextEncoder_ = TextEncoder;
|
286
|
+
export {
|
287
|
+
TextDecoder_ as TextDecoder,
|
288
|
+
TextEncoder_ as TextEncoder,
|
289
|
+
};
|
290
|
+
|
291
|
+
export function toUSVString(string: string): string {
|
292
|
+
let out = '';
|
293
|
+
for (let i = 0; i < string.length; i++) {
|
294
|
+
const code = string.charCodeAt(i);
|
295
|
+
if (code >= 0xD800 && code < 0xDFFF) {
|
296
|
+
out += '\uFFFD';
|
297
|
+
} else {
|
298
|
+
out += string[i];
|
299
|
+
}
|
300
|
+
}
|
301
|
+
return out;
|
302
|
+
}
|
303
|
+
|
304
|
+
export function transferableAbortController(): AbortController {
|
305
|
+
throw new TypeError('util.transferableAbortController is not supported in fake-node');
|
306
|
+
}
|
307
|
+
|
308
|
+
export function transferableAbortSignal(): AbortSignal {
|
309
|
+
throw new TypeError('util.transferableAbortSignal is not supported in fake-node');
|
310
|
+
}
|
311
|
+
|
312
|
+
export function aborted(signal: AbortSignal, resource: object): Promise<undefined> {
|
313
|
+
throw new TypeError('util.aborted is not supported in fake-node');
|
314
|
+
}
|
315
|
+
|
316
|
+
export const types = {
|
317
|
+
|
318
|
+
isAnyArrayBuffer(value: unknown): value is (ArrayBuffer | SharedArrayBuffer) {
|
319
|
+
return types.isArrayBuffer(value) || types.isSharedArrayBuffer(value);
|
320
|
+
},
|
321
|
+
|
322
|
+
isArrayBufferView(value: unknown): value is (DataView | TypedArray) {
|
323
|
+
return ArrayBuffer.isView(value);
|
324
|
+
},
|
325
|
+
|
326
|
+
isArgumentsObject(value: unknown): value is typeof arguments {
|
327
|
+
if (typeof value !== 'object' || value === null || !('length' in value) || !('callee' in value)) {
|
328
|
+
return false;
|
329
|
+
}
|
330
|
+
try {
|
331
|
+
value.callee;
|
332
|
+
return false;
|
333
|
+
} catch (error) {
|
334
|
+
if (error instanceof Error && (error.message === "TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them" || error.message === "TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context.")) {
|
335
|
+
return types.isProxy(value) !== true;
|
336
|
+
} else {
|
337
|
+
throw error;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
},
|
341
|
+
|
342
|
+
isArrayBuffer(value: unknown): value is ArrayBuffer {
|
343
|
+
return value instanceof ArrayBuffer;
|
344
|
+
},
|
345
|
+
|
346
|
+
isAsyncFunction(value: unknown): value is (...args: unknown[]) => Promise<unknown> {
|
347
|
+
return value instanceof (async () => {}).constructor;
|
348
|
+
},
|
349
|
+
|
350
|
+
isBigInt64Array(value: unknown): value is BigInt64Array {
|
351
|
+
return value instanceof BigInt64Array;
|
352
|
+
},
|
353
|
+
|
354
|
+
isBigIntObject(value: unknown): boolean {
|
355
|
+
return value instanceof Object(0n).constructor;
|
356
|
+
},
|
357
|
+
|
358
|
+
isBigUint64Array(value: unknown): value is BigUint64Array {
|
359
|
+
return value instanceof BigUint64Array;
|
360
|
+
},
|
361
|
+
|
362
|
+
isBooleanObject(value: unknown): value is Boolean {
|
363
|
+
return value instanceof Boolean;
|
364
|
+
},
|
365
|
+
|
366
|
+
isBoxedPrimitive(value: unknown): value is Boolean | Number | String {
|
367
|
+
return value instanceof Boolean || value instanceof Number || value instanceof String || value instanceof Object(0n).constructor || value instanceof Object(Symbol()).constructor;
|
368
|
+
},
|
369
|
+
|
370
|
+
isCryptoKey(value: unknown): value is CryptoKey {
|
371
|
+
return value instanceof CryptoKey;
|
372
|
+
},
|
373
|
+
|
374
|
+
isDataView(value: unknown): value is DataView {
|
375
|
+
return value instanceof DataView;
|
376
|
+
},
|
377
|
+
|
378
|
+
isDate(value: unknown): value is Date {
|
379
|
+
return value instanceof Date;
|
380
|
+
},
|
381
|
+
|
382
|
+
isExternal(value: unknown): boolean {
|
383
|
+
return false;
|
384
|
+
},
|
385
|
+
|
386
|
+
isFloat32Array(value: unknown): value is Float32Array {
|
387
|
+
return value instanceof Float32Array;
|
388
|
+
},
|
389
|
+
|
390
|
+
isFloat64Array(value: unknown): value is Float64Array {
|
391
|
+
return value instanceof Float64Array;
|
392
|
+
},
|
393
|
+
|
394
|
+
isGeneratorFunction(value: unknown): value is GeneratorFunction {
|
395
|
+
return value instanceof (function*() {}).constructor;
|
396
|
+
},
|
397
|
+
|
398
|
+
isGeneratorObject(value: unknown): value is Generator {
|
399
|
+
return value?.constructor === (function*() {}).constructor.prototype;
|
400
|
+
},
|
401
|
+
|
402
|
+
isInt8Array(value: unknown): value is Int8Array {
|
403
|
+
return value instanceof Int8Array;
|
404
|
+
},
|
405
|
+
|
406
|
+
isInt16Array(value: unknown): value is Int16Array {
|
407
|
+
return value instanceof Int16Array;
|
408
|
+
},
|
409
|
+
|
410
|
+
isInt32Array(value: unknown): value is Int32Array {
|
411
|
+
return value instanceof Int32Array;
|
412
|
+
},
|
413
|
+
|
414
|
+
isKeyObject(value: unknown): boolean {
|
415
|
+
return false;
|
416
|
+
},
|
417
|
+
|
418
|
+
isMap(value: unknown): value is Map<unknown, unknown> {
|
419
|
+
return value instanceof Map;
|
420
|
+
},
|
421
|
+
|
422
|
+
isMapIterator(value: unknown): value is MapIterator<unknown> {
|
423
|
+
return value instanceof (new Map()).keys().constructor;
|
424
|
+
},
|
425
|
+
|
426
|
+
isModuleNamespaceObject(value: unknown): boolean {
|
427
|
+
if (typeof value !== 'object' || value === null) {
|
428
|
+
return false;
|
429
|
+
}
|
430
|
+
return Object.isSealed(value) && Object.getOwnPropertyDescriptor(value, Object.keys(value)[0])?.writable === true;
|
431
|
+
},
|
432
|
+
|
433
|
+
isNativeError(value: unknown): value is Error {
|
434
|
+
return value instanceof Error;
|
435
|
+
},
|
436
|
+
|
437
|
+
isNumberObject(value: unknown): value is Number {
|
438
|
+
return value instanceof Number;
|
439
|
+
},
|
440
|
+
|
441
|
+
isPromise(value: unknown): value is Promise<unknown> {
|
442
|
+
return value instanceof Promise;
|
443
|
+
},
|
444
|
+
|
445
|
+
isProxy(value: unknown): boolean | 'maybe' {
|
446
|
+
try {
|
447
|
+
structuredClone(value);
|
448
|
+
return false;
|
449
|
+
} catch (error) {
|
450
|
+
if (!(error instanceof DOMException && error.name === 'DataCloneError')) {
|
451
|
+
throw error;
|
452
|
+
}
|
453
|
+
}
|
454
|
+
if (shouldBeCloneable(value)) {
|
455
|
+
return true;
|
456
|
+
} else {
|
457
|
+
return 'maybe';
|
458
|
+
}
|
459
|
+
},
|
460
|
+
|
461
|
+
isRegExp(value: unknown): value is RegExp {
|
462
|
+
return value instanceof RegExp;
|
463
|
+
},
|
464
|
+
|
465
|
+
isSet(value: unknown): value is Set<unknown> {
|
466
|
+
return value instanceof Set;
|
467
|
+
},
|
468
|
+
|
469
|
+
isSetIterator(value: unknown): value is SetIterator<unknown> {
|
470
|
+
return value instanceof (new Set()).keys().constructor;
|
471
|
+
},
|
472
|
+
|
473
|
+
isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer {
|
474
|
+
return value instanceof SharedArrayBuffer;
|
475
|
+
},
|
476
|
+
|
477
|
+
isStringObject(value: unknown): value is String {
|
478
|
+
return value instanceof String;
|
479
|
+
},
|
480
|
+
|
481
|
+
isSymbolObject(value: unknown): boolean {
|
482
|
+
return value instanceof Object(Symbol()).constructor;
|
483
|
+
},
|
484
|
+
|
485
|
+
isTypedArray(value: unknown): value is TypedArray {
|
486
|
+
return value instanceof Int8Array || value instanceof Uint8Array || value instanceof Int16Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array || value instanceof Float32Array || value instanceof Float64Array || value instanceof BigInt64Array;
|
487
|
+
},
|
488
|
+
|
489
|
+
isUint8Array(value: unknown): value is Uint8Array {
|
490
|
+
return value instanceof Uint8Array;
|
491
|
+
},
|
492
|
+
|
493
|
+
isUint8ClampedArray(value: unknown): value is Uint8ClampedArray {
|
494
|
+
return value instanceof Uint8ClampedArray;
|
495
|
+
},
|
496
|
+
|
497
|
+
isUint16Array(value: unknown): value is Uint16Array {
|
498
|
+
return value instanceof Uint16Array;
|
499
|
+
},
|
500
|
+
|
501
|
+
isUint32Array(value: unknown): value is Uint32Array {
|
502
|
+
return value instanceof Uint32Array;
|
503
|
+
},
|
504
|
+
|
505
|
+
isWeakMap(value: unknown): value is WeakMap<WeakKey, unknown> {
|
506
|
+
return value instanceof WeakMap;
|
507
|
+
},
|
508
|
+
|
509
|
+
isWeakSet(value: unknown): value is WeakSet<WeakKey> {
|
510
|
+
return value instanceof WeakSet;
|
511
|
+
},
|
512
|
+
|
513
|
+
};
|
514
|
+
|
515
|
+
export function _extend(target: Object, source: Object): Object {
|
516
|
+
return Object.assign(target, source);
|
517
|
+
}
|
518
|
+
|
519
|
+
export function isArray(object: unknown): boolean {
|
520
|
+
return Array.isArray(object);
|
521
|
+
}
|
package/index.js
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
-
}) : function(o, v) {
|
16
|
-
o["default"] = v;
|
17
|
-
});
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
19
|
-
var ownKeys = function(o) {
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
-
var ar = [];
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
-
return ar;
|
24
|
-
};
|
25
|
-
return ownKeys(o);
|
26
|
-
};
|
27
|
-
return function (mod) {
|
28
|
-
if (mod && mod.__esModule) return mod;
|
29
|
-
var result = {};
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
-
__setModuleDefault(result, mod);
|
32
|
-
return result;
|
33
|
-
};
|
34
|
-
})();
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
37
|
-
};
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
-
exports.FakeNode = exports.Process = void 0;
|
40
|
-
const assert_1 = __importDefault(require("assert"));
|
41
|
-
const module_buffer = __importStar(require("buffer"));
|
42
|
-
// @ts-ignore
|
43
|
-
const module_path = __importStar(require("path"));
|
44
|
-
const module_punycode = __importStar(require("punycode"));
|
45
|
-
const module_querystring = __importStar(require("querystring"));
|
46
|
-
// @ts-ignore
|
47
|
-
const module_readline = __importStar(require("readline"));
|
48
|
-
// @ts-ignore
|
49
|
-
const module_stream = __importStar(require("stream"));
|
50
|
-
// @ts-ignore
|
51
|
-
const module_string_decoder = __importStar(require("string_decoder"));
|
52
|
-
const module_test = __importStar(require("test"));
|
53
|
-
// @ts-ignore
|
54
|
-
const module_url = __importStar(require("url"));
|
55
|
-
// @ts-ignore
|
56
|
-
const module_util = __importStar(require("util"));
|
57
|
-
const web_only_globals_json_1 = __importDefault(require("./web_only_globals.json"));
|
58
|
-
// todo: properly implement eval
|
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
|
-
}
|
63
|
-
class Process {
|
64
|
-
fakeNode;
|
65
|
-
pid;
|
66
|
-
argv = [];
|
67
|
-
argv0 = '';
|
68
|
-
module;
|
69
|
-
code;
|
70
|
-
path;
|
71
|
-
constructor(fakeNode, { path = '', code, module }) {
|
72
|
-
this.fakeNode = fakeNode;
|
73
|
-
this.pid = fakeNode.nextPid;
|
74
|
-
fakeNode.nextPid++;
|
75
|
-
fakeNode.processes.set(this.pid, this);
|
76
|
-
this.code = code;
|
77
|
-
this.path = path;
|
78
|
-
this.module = module ?? false;
|
79
|
-
}
|
80
|
-
run() {
|
81
|
-
let code;
|
82
|
-
if (this.module) {
|
83
|
-
code = `with(${this.fakeNode.globalName}.getGlobals(${this.pid})){__fakeNode_process__.fakeNode.modules.set('${this.module},(function(){${this.code};return module.exports;})());}`;
|
84
|
-
}
|
85
|
-
else {
|
86
|
-
code = `with(${this.fakeNode.globalName}.getGlobals(${this.pid})){(function(){${this.code}})();}`;
|
87
|
-
}
|
88
|
-
let elt = document.createElement('script');
|
89
|
-
elt.textContent = code;
|
90
|
-
document.body.appendChild(elt);
|
91
|
-
}
|
92
|
-
}
|
93
|
-
exports.Process = Process;
|
94
|
-
class FakeNode {
|
95
|
-
static nextId = 0;
|
96
|
-
id;
|
97
|
-
globalName;
|
98
|
-
modules = new Map();
|
99
|
-
builtinModules = new Map([
|
100
|
-
['assert', assert_1.default],
|
101
|
-
['buffer', module_buffer],
|
102
|
-
['path', module_path],
|
103
|
-
['punycode', module_punycode],
|
104
|
-
['querystring', module_querystring],
|
105
|
-
['readline', module_readline],
|
106
|
-
['stream', module_stream],
|
107
|
-
['string_decoder', module_string_decoder],
|
108
|
-
['test', module_test],
|
109
|
-
['url', module_url],
|
110
|
-
['util', module_util],
|
111
|
-
]);
|
112
|
-
globals;
|
113
|
-
processes = new Map();
|
114
|
-
nextPid = 3;
|
115
|
-
window = window;
|
116
|
-
constructor() {
|
117
|
-
this.id = FakeNode.nextId;
|
118
|
-
FakeNode.nextId++;
|
119
|
-
this.globalName = '__fakeNode_' + this.id + '__';
|
120
|
-
// @ts-ignore
|
121
|
-
globalThis[this.globalName] = this;
|
122
|
-
this.globals = Object.create(DEFAULT_GLOBALS);
|
123
|
-
Object.assign(this.globals, { require: this.require, Buffer: module_buffer.Buffer });
|
124
|
-
}
|
125
|
-
require(module) {
|
126
|
-
if (module.startsWith('fake-node:')) {
|
127
|
-
module = module.slice('fake-node:'.length);
|
128
|
-
}
|
129
|
-
else if (module.startsWith('node:')) {
|
130
|
-
module = module.slice('node:'.length);
|
131
|
-
}
|
132
|
-
if (this.modules.has(module)) {
|
133
|
-
return this.modules.get(module);
|
134
|
-
}
|
135
|
-
else if (this.builtinModules.has(module)) {
|
136
|
-
return this.builtinModules.get(module);
|
137
|
-
}
|
138
|
-
else {
|
139
|
-
throw new Error(`cannot find module '${module}'`);
|
140
|
-
}
|
141
|
-
}
|
142
|
-
getGlobals(pid) {
|
143
|
-
const process = this.processes.get(pid);
|
144
|
-
if (process === undefined) {
|
145
|
-
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.`);
|
146
|
-
}
|
147
|
-
let scope = Object.create(this.globals);
|
148
|
-
scope.global = scope;
|
149
|
-
scope.globalThis = scope;
|
150
|
-
scope.__fakeNode_process__ = process;
|
151
|
-
if (process.path !== '') {
|
152
|
-
const pathParts = process.path.split('/');
|
153
|
-
scope.__dirname = pathParts.slice(0, -1).join('/');
|
154
|
-
scope.__filename = pathParts[pathParts.length - 1];
|
155
|
-
}
|
156
|
-
if (process.module !== false) {
|
157
|
-
scope.module = {
|
158
|
-
exports: {}
|
159
|
-
};
|
160
|
-
scope.exports = scope.module.exports;
|
161
|
-
}
|
162
|
-
return scope;
|
163
|
-
}
|
164
|
-
run(code) {
|
165
|
-
(new Process(this, { code, path: '/' })).run();
|
166
|
-
}
|
167
|
-
addModule(name, code) {
|
168
|
-
(new Process(this, { code, path: '/', module: name })).run();
|
169
|
-
}
|
170
|
-
}
|
171
|
-
exports.FakeNode = FakeNode;
|