fake-node 0.1.1 → 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} +117 -52
- 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 -144
- package/tsconfig.json +0 -10
- /package/{web_only_globals.json → src/web_only_globals.json} +0 -0
package/src/process.ts
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
|
2
|
+
import {platform as _platform} from './os';
|
3
|
+
|
4
|
+
export function abort(): void {
|
5
|
+
__fakeNode__.window.close();
|
6
|
+
}
|
7
|
+
|
8
|
+
export const allowedNodeEnvironmentFlags = new Set<never>();
|
9
|
+
|
10
|
+
export const arch = 'fake';
|
11
|
+
|
12
|
+
// export const argv = __fakeNode_process__.argv;
|
13
|
+
|
14
|
+
// export const argv0 = __fakeNode_process__.argv0;
|
15
|
+
|
16
|
+
export const channel = undefined;
|
17
|
+
|
18
|
+
export function chdir(path: string): void {
|
19
|
+
__fakeNode_process__.cwd = path;
|
20
|
+
}
|
21
|
+
|
22
|
+
export const config = {};
|
23
|
+
|
24
|
+
export const connected = undefined;
|
25
|
+
|
26
|
+
export function constrainedMemory(): number {
|
27
|
+
return 0;
|
28
|
+
}
|
29
|
+
|
30
|
+
export function availableMemory(): number {
|
31
|
+
throw new TypeError('process.availableMemory is not available in fake-node');
|
32
|
+
}
|
33
|
+
|
34
|
+
export function cpuUsage(previousValue?: {user: number, system: number}): {user: number, system: number} {
|
35
|
+
throw new TypeError('process.cpuUsage is not available in fake-node');
|
36
|
+
}
|
37
|
+
|
38
|
+
export function cwd(): string {
|
39
|
+
return __fakeNode_process__.cwd;
|
40
|
+
}
|
41
|
+
|
42
|
+
export let debugPort = -1;
|
43
|
+
|
44
|
+
export const disconnect = undefined;
|
45
|
+
|
46
|
+
export function dlopen(module: object, filename: string, flags: any /* typeof os.constants.dlopen[keyof typeof os.constants.dlopen] = os.constants.dlopen.RTLD_LAZY */): void {
|
47
|
+
throw new TypeError('process.dlopen is not supported in fake-node');
|
48
|
+
}
|
49
|
+
|
50
|
+
export function emitWarning(warning: string | Error, type_or_options: string | {type?: string, code?: string, ctor?: Function, detail?: string}, code?: string, ctor: Function = emitWarning, detail?: string): void {
|
51
|
+
throw new TypeError('process.emitWarning is not supported in fake-node');
|
52
|
+
}
|
53
|
+
|
54
|
+
// export const env = __fakeNode_process__.env;
|
55
|
+
|
56
|
+
// export const execArgv = __fakeNode_process__.execArgv;
|
57
|
+
|
58
|
+
// export const execPath = __fakeNode_process__.execPath;
|
59
|
+
|
60
|
+
export function exit(code: number = 0): void {
|
61
|
+
window.console.log('Exit code', code);
|
62
|
+
window.close();
|
63
|
+
}
|
64
|
+
|
65
|
+
export let exitCode = 0;
|
66
|
+
|
67
|
+
export const features = {
|
68
|
+
cached_builtins: true,
|
69
|
+
debug: false,
|
70
|
+
inspector: false,
|
71
|
+
ipv6: true,
|
72
|
+
require_module: true,
|
73
|
+
tls: false,
|
74
|
+
tls_alpn: false,
|
75
|
+
tls_ocsp: false,
|
76
|
+
tls_sni: false,
|
77
|
+
typescript: false,
|
78
|
+
uv: false,
|
79
|
+
};
|
80
|
+
|
81
|
+
export const finalization = {
|
82
|
+
|
83
|
+
register(ref: object, callback: (ref: object, event: string) => void): void {
|
84
|
+
throw new TypeError('process.finalization is not supported in fake-node');
|
85
|
+
},
|
86
|
+
|
87
|
+
registerBeforeExit(ref: object, callback: (ref: object, event: string) => void): void {
|
88
|
+
throw new TypeError('process.finalization is not supported in fake-node');
|
89
|
+
},
|
90
|
+
|
91
|
+
unregister(ref: object): void {
|
92
|
+
throw new TypeError('process.finalization is not supported in fake-node');
|
93
|
+
},
|
94
|
+
|
95
|
+
};
|
96
|
+
|
97
|
+
export function getActiveResourcesInfo(): string[] {
|
98
|
+
return [];
|
99
|
+
}
|
100
|
+
|
101
|
+
export function getBuiltinModule(id: string): any {
|
102
|
+
return __fakeNode_process__.fakeNode.modules.get(id);
|
103
|
+
}
|
104
|
+
|
105
|
+
export function getegid(): number {
|
106
|
+
return __fakeNode_process__.gid;
|
107
|
+
}
|
108
|
+
|
109
|
+
export function geteuid(): number {
|
110
|
+
return __fakeNode_process__.uid;
|
111
|
+
}
|
112
|
+
|
113
|
+
export function getgid(): number {
|
114
|
+
return __fakeNode_process__.gid;
|
115
|
+
}
|
116
|
+
|
117
|
+
export function getgroups(): number[] {
|
118
|
+
return [__fakeNode_process__.gid].concat(__fakeNode_process__.groups);
|
119
|
+
}
|
120
|
+
|
121
|
+
export function getuid(): number {
|
122
|
+
return __fakeNode_process__.uid;
|
123
|
+
}
|
124
|
+
|
125
|
+
let errorCallback: null | number = null;
|
126
|
+
|
127
|
+
export function hasUncaughtExecptionCaptureCallback(): boolean {
|
128
|
+
return errorCallback !== null;
|
129
|
+
}
|
130
|
+
|
131
|
+
export function hrtime(time?: [number, number]): [number, number] {
|
132
|
+
let value = window.performance.now();
|
133
|
+
if (time !== undefined) {
|
134
|
+
value -= time[0] + time[1] / 1000000;
|
135
|
+
}
|
136
|
+
return [Math.floor(value), (value - Math.floor(value) * 1000000)];
|
137
|
+
}
|
138
|
+
|
139
|
+
hrtime.bigint = function(): bigint {
|
140
|
+
return BigInt(window.performance.now());
|
141
|
+
}
|
142
|
+
|
143
|
+
export function initgroups(user: string | number, extraGroup: string | number): void {
|
144
|
+
throw new TypeError('process.initgroups is not supported in fake-node');
|
145
|
+
}
|
146
|
+
|
147
|
+
export function kill(pid: number, signal: string | number): void {
|
148
|
+
throw new TypeError('process.kill is not supported in fake-node');
|
149
|
+
}
|
150
|
+
|
151
|
+
export function loadEnvFile(path: string = './.env'): void {
|
152
|
+
throw new TypeError('process.loadEnvFile is not supported in fake-node');
|
153
|
+
}
|
154
|
+
|
155
|
+
// export const mainModule = __fakeNode_process__.path === '' ? undefined : __fakeNode_process__.path;
|
156
|
+
|
157
|
+
export function memoryUsage(): {rss: number, heapTotal: number, heapUsed: number, external: number, arrayBuffers: number} {
|
158
|
+
throw new TypeError('process.memoryUsage is not supported in fake-node');
|
159
|
+
}
|
160
|
+
|
161
|
+
memoryUsage.rss = function(): number {
|
162
|
+
throw new TypeError('process.memoryUsage.rss is not supported in fake-node');
|
163
|
+
}
|
164
|
+
|
165
|
+
export function nextTick(callback: Function, ...args: any[]): void {
|
166
|
+
window.setTimeout(callback, 0, ...args);
|
167
|
+
}
|
168
|
+
|
169
|
+
export const noDeprecation = false;
|
170
|
+
|
171
|
+
export const permission = {
|
172
|
+
|
173
|
+
has(scope: string, reference?: string): void {
|
174
|
+
throw new TypeError('process.permission.has is not supported in fake-node');
|
175
|
+
}
|
176
|
+
|
177
|
+
};
|
178
|
+
|
179
|
+
export function ref(maybeRefable: any): void {
|
180
|
+
throw new TypeError('process.ref is not supported in fake-node');
|
181
|
+
}
|
182
|
+
|
183
|
+
export const pid = 1;
|
184
|
+
|
185
|
+
// export const platform = _platform();
|
186
|
+
|
187
|
+
export const ppid = 1;
|
188
|
+
|
189
|
+
export const release = {
|
190
|
+
name: 'fake-node',
|
191
|
+
sourceUrl: '', // todo: add something here
|
192
|
+
headersUrl: '',
|
193
|
+
lts: 'Hydrogen',
|
194
|
+
};
|
195
|
+
|
196
|
+
export function setegid(id: string | number): void {
|
197
|
+
__fakeNode_process__.gid = __fakeNode__.getGIDFromGroup(id);
|
198
|
+
}
|
199
|
+
|
200
|
+
export function seteuid(id: string | number): void {
|
201
|
+
__fakeNode_process__.uid = __fakeNode__.getUIDFromUser(id);
|
202
|
+
}
|
203
|
+
|
204
|
+
export function setgid(id: string | number): void {
|
205
|
+
__fakeNode_process__.gid = __fakeNode__.getGIDFromGroup(id);
|
206
|
+
}
|
207
|
+
|
208
|
+
export function setgroups(groups: (string | number)[]): void {
|
209
|
+
__fakeNode_process__.groups = groups.map(__fakeNode__.getGIDFromGroup);
|
210
|
+
}
|
211
|
+
|
212
|
+
export function setuid(id: string | number): void {
|
213
|
+
__fakeNode_process__.uid = __fakeNode__.getUIDFromUser(id);
|
214
|
+
}
|
215
|
+
|
216
|
+
export function setSourceMapsEnabledVal(val: boolean): void {
|
217
|
+
throw new TypeError('process.setSourceMapsEnabledVal is not supported in fake-node');
|
218
|
+
}
|
219
|
+
|
220
|
+
export function setUncaughtExceptionCaptureCallback(func: Function | null): void {
|
221
|
+
if (errorCallback !== null) {
|
222
|
+
__fakeNode__.removeErrorCallback(errorCallback);
|
223
|
+
}
|
224
|
+
if (func !== null) {
|
225
|
+
errorCallback = __fakeNode__.addErrorCallback(func);
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
export const sourceMapsEnabled = false;
|
230
|
+
|
231
|
+
export const stderr = undefined; // todo: put stuff here
|
232
|
+
|
233
|
+
export const stdin = undefined;
|
234
|
+
|
235
|
+
export const stdout = undefined;
|
236
|
+
|
237
|
+
export let throwDeprecation = false;
|
238
|
+
|
239
|
+
export let title = ''; // todo: put something here
|
240
|
+
|
241
|
+
export const traceDeprecation = false;
|
242
|
+
|
243
|
+
export function umask(): number;
|
244
|
+
export function umask(mask: number): void;
|
245
|
+
export function umask(mask?: number): number | void {
|
246
|
+
if (mask === undefined) {
|
247
|
+
return __fakeNode_process__.umask;
|
248
|
+
} else {
|
249
|
+
__fakeNode_process__.umask = mask;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
export function unref(maybeRefable: any): void {
|
254
|
+
throw new TypeError('process.unref is not supported in fake-node');
|
255
|
+
}
|
256
|
+
|
257
|
+
export function uptime(): number {
|
258
|
+
return __fakeNode_process__.fakeNode.window.performance.now() / 1000;
|
259
|
+
}
|
260
|
+
|
261
|
+
// export const version = __fakeNode__.version;
|
262
|
+
|
263
|
+
// export const versions = [version];
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
export function parse(str: string): {[key: string]: string | string[]} {
|
3
|
+
let out: {[key: string]: string | string[]} = {}
|
4
|
+
for (const [key, value] of (new URLSearchParams(str)).entries()) {
|
5
|
+
if (key in out) {
|
6
|
+
if (typeof out[key] === 'string') {
|
7
|
+
out[key] = [out[key], value];
|
8
|
+
} else {
|
9
|
+
out[key].push(value);
|
10
|
+
}
|
11
|
+
} else {
|
12
|
+
out[key] = value;
|
13
|
+
}
|
14
|
+
}
|
15
|
+
return out;
|
16
|
+
}
|
17
|
+
|
18
|
+
export function stringify(obj: {[key: string]: string | number | bigint | boolean | string[] | number[] | bigint[] | boolean[]}): string {
|
19
|
+
let params = new URLSearchParams();
|
20
|
+
for (const key in Object.entries(obj)) {
|
21
|
+
const value = obj[key];
|
22
|
+
if (typeof value === 'string') {
|
23
|
+
params.set(key, value);
|
24
|
+
} else if (value instanceof Array) {
|
25
|
+
for (const item of value) {
|
26
|
+
params.append(key, String(item));
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
return params.toString();
|
31
|
+
}
|
32
|
+
|
33
|
+
export {
|
34
|
+
parse as decode,
|
35
|
+
stringify as encode,
|
36
|
+
}
|
package/src/util.ts
ADDED
@@ -0,0 +1,408 @@
|
|
1
|
+
|
2
|
+
import {env, emitWarning} from './process';
|
3
|
+
|
4
|
+
|
5
|
+
type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array;
|
6
|
+
type Callback = (error: Error | null, value: any) => void;
|
7
|
+
|
8
|
+
function shouldBeCloneable(value: unknown): boolean {
|
9
|
+
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;
|
10
|
+
}
|
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 env.NODE_DEBUG === 'string' && env.NODE_DEBUG.includes(section)) {
|
23
|
+
return Object.assign(function(message: string): void {
|
24
|
+
callback(`${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
|
+
type InspectOptions = {};
|
136
|
+
|
137
|
+
export function inspect(value: unknown, options: InspectOptions = {}): string {
|
138
|
+
return String(value);
|
139
|
+
}
|
140
|
+
|
141
|
+
export function isDeepStrictEqual(val1: unknown, val2: unknown): boolean {
|
142
|
+
if ((typeof val1 === 'object' && val1 !== null) || typeof val1 === 'function') {
|
143
|
+
if (!(typeof val2 === 'object' && val2 !== null) && !(typeof val2 === 'function')) {
|
144
|
+
return false;
|
145
|
+
}
|
146
|
+
if ((val1[Symbol.toStringTag] !== val2[Symbol.toStringTag]) || Object.getPrototypeOf(val1) !== Object.getPrototypeOf(val2)) {
|
147
|
+
return false;
|
148
|
+
}
|
149
|
+
if (val1 instanceof Map) {
|
150
|
+
if (!(val2 instanceof Map)) {
|
151
|
+
throw new TypeError(`this error should not occur`);
|
152
|
+
}
|
153
|
+
for (const [key, value] of val1.entries()) {
|
154
|
+
if (!val2.has(key)) {
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
return isDeepStrictEqual(value, val2.get(key));
|
158
|
+
}
|
159
|
+
}
|
160
|
+
if (val1 instanceof Set) {
|
161
|
+
if (!(val2 instanceof Set)) {
|
162
|
+
throw new TypeError(`this error should not occur`);
|
163
|
+
}
|
164
|
+
for (const item of val1) {
|
165
|
+
let found = false;
|
166
|
+
for (const item2 of val2) {
|
167
|
+
if (item === item2) {
|
168
|
+
found = true;
|
169
|
+
break;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
if (!found) {
|
173
|
+
return false;
|
174
|
+
}
|
175
|
+
}
|
176
|
+
return true;
|
177
|
+
}
|
178
|
+
const properties = (Object.getOwnPropertyNames(val1) as (string | symbol)[]).concat(Object.getOwnPropertySymbols(val1));
|
179
|
+
const val2Properties = (Object.getOwnPropertyNames(val2) as (string | symbol)[]).concat(Object.getOwnPropertySymbols(val2));
|
180
|
+
if (!properties.every((prop: string | symbol) => val2Properties.includes(prop))) {
|
181
|
+
return false;
|
182
|
+
}
|
183
|
+
for (const property of properties) {
|
184
|
+
if (!isDeepStrictEqual(val1[property], val2[property])) {
|
185
|
+
return false;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
if (types.isBoxedPrimitive(val1)) {
|
189
|
+
if (!types.isBoxedPrimitive(val2)) {
|
190
|
+
return false;
|
191
|
+
}
|
192
|
+
return Object.is(val1.constructor(val1), val2.constructor(val2));
|
193
|
+
}
|
194
|
+
return true;
|
195
|
+
} else if ((typeof val2 === 'object' && val2 !== null) || typeof val2 === 'function') {
|
196
|
+
return false;
|
197
|
+
} else {
|
198
|
+
return Object.is(val1, val2);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
export const types = {
|
203
|
+
|
204
|
+
isAnyArrayBuffer(value: unknown): value is (ArrayBuffer | SharedArrayBuffer) {
|
205
|
+
return types.isArrayBuffer(value) || types.isSharedArrayBuffer(value);
|
206
|
+
},
|
207
|
+
|
208
|
+
isArrayBufferView(value: unknown): value is (DataView | TypedArray) {
|
209
|
+
return ArrayBuffer.isView(value);
|
210
|
+
},
|
211
|
+
|
212
|
+
isArgumentsObject(value: unknown): value is typeof arguments {
|
213
|
+
if (typeof value !== 'object' || value === null || !Object.hasOwn(value, 'length') || !Object.hasOwn(value, 'callee')) {
|
214
|
+
return false;
|
215
|
+
}
|
216
|
+
try {
|
217
|
+
// @ts-ignore
|
218
|
+
value.callee;
|
219
|
+
return false;
|
220
|
+
} catch (error) {
|
221
|
+
if (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.") {
|
222
|
+
return types.isProxy(value) !== true;
|
223
|
+
} else {
|
224
|
+
throw error;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
},
|
228
|
+
|
229
|
+
isArrayBuffer(value: unknown): value is ArrayBuffer {
|
230
|
+
return value instanceof ArrayBuffer;
|
231
|
+
},
|
232
|
+
|
233
|
+
isAsyncFunction(value: unknown): value is (...args: unknown[]) => Promise<unknown> {
|
234
|
+
return value instanceof (async () => {}).constructor;
|
235
|
+
},
|
236
|
+
|
237
|
+
isBigInt64Array(value: unknown): value is BigInt64Array {
|
238
|
+
return value instanceof BigInt64Array;
|
239
|
+
},
|
240
|
+
|
241
|
+
isBigIntObject(value: unknown): boolean {
|
242
|
+
return value instanceof Object(0n).constructor;
|
243
|
+
},
|
244
|
+
|
245
|
+
isBigUint64Array(value: unknown): value is BigUint64Array {
|
246
|
+
return value instanceof BigUint64Array;
|
247
|
+
},
|
248
|
+
|
249
|
+
isBooleanObject(value: unknown): value is Boolean {
|
250
|
+
return value instanceof Boolean;
|
251
|
+
},
|
252
|
+
|
253
|
+
isBoxedPrimitive(value: unknown): value is Boolean | Number | String {
|
254
|
+
return value instanceof Boolean || value instanceof Number || value instanceof String || value instanceof Object(0n).constructor || value instanceof Object(Symbol()).constructor;
|
255
|
+
},
|
256
|
+
|
257
|
+
isCryptoKey(value: unknown): value is CryptoKey {
|
258
|
+
return value instanceof CryptoKey;
|
259
|
+
},
|
260
|
+
|
261
|
+
isDataView(value: unknown): value is DataView {
|
262
|
+
return value instanceof DataView;
|
263
|
+
},
|
264
|
+
|
265
|
+
isDate(value: unknown): value is Date {
|
266
|
+
return value instanceof Date;
|
267
|
+
},
|
268
|
+
|
269
|
+
isExternal(value: unknown): boolean {
|
270
|
+
return false;
|
271
|
+
},
|
272
|
+
|
273
|
+
isFloat32Array(value: unknown): value is Float32Array {
|
274
|
+
return value instanceof Float32Array;
|
275
|
+
},
|
276
|
+
|
277
|
+
isFloat64Array(value: unknown): value is Float64Array {
|
278
|
+
return value instanceof Float64Array;
|
279
|
+
},
|
280
|
+
|
281
|
+
isGeneratorFunction(value: unknown): value is GeneratorFunction {
|
282
|
+
return value instanceof (function*() {}).constructor;
|
283
|
+
},
|
284
|
+
|
285
|
+
isGeneratorObject(value: unknown): value is Generator {
|
286
|
+
return value?.constructor === (function*() {}).constructor.prototype;
|
287
|
+
},
|
288
|
+
|
289
|
+
isInt8Array(value: unknown): value is Int8Array {
|
290
|
+
return value instanceof Int8Array;
|
291
|
+
},
|
292
|
+
|
293
|
+
isInt16Array(value: unknown): value is Int16Array {
|
294
|
+
return value instanceof Int16Array;
|
295
|
+
},
|
296
|
+
|
297
|
+
isInt32Array(value: unknown): value is Int32Array {
|
298
|
+
return value instanceof Int32Array;
|
299
|
+
},
|
300
|
+
|
301
|
+
isKeyObject(value: unknown): boolean {
|
302
|
+
return false;
|
303
|
+
},
|
304
|
+
|
305
|
+
isMap(value: unknown): value is Map<unknown, unknown> {
|
306
|
+
return value instanceof Map;
|
307
|
+
},
|
308
|
+
|
309
|
+
isMapIterator(value: unknown): value is MapIterator<unknown> {
|
310
|
+
return value instanceof (new Map()).keys().constructor;
|
311
|
+
},
|
312
|
+
|
313
|
+
isModuleNamespaceObject(value: unknown): boolean {
|
314
|
+
if (typeof value !== 'object' || value === null) {
|
315
|
+
return false;
|
316
|
+
}
|
317
|
+
return Object.isSealed(value) && Object.getOwnPropertyDescriptor(value, Object.keys(value)[0])?.writable === true;
|
318
|
+
},
|
319
|
+
|
320
|
+
isNativeError(value: unknown): value is Error {
|
321
|
+
return value instanceof Error;
|
322
|
+
},
|
323
|
+
|
324
|
+
isNumberObject(value: unknown): value is Number {
|
325
|
+
return value instanceof Number;
|
326
|
+
},
|
327
|
+
|
328
|
+
isPromise(value: unknown): value is Promise<unknown> {
|
329
|
+
return value instanceof Promise;
|
330
|
+
},
|
331
|
+
|
332
|
+
isProxy(value: unknown): boolean | 'maybe' {
|
333
|
+
try {
|
334
|
+
structuredClone(value);
|
335
|
+
return false;
|
336
|
+
} catch (error) {
|
337
|
+
if (!(error instanceof DOMException && error.name === 'DataCloneError')) {
|
338
|
+
throw error;
|
339
|
+
}
|
340
|
+
}
|
341
|
+
if (shouldBeCloneable(value)) {
|
342
|
+
return true;
|
343
|
+
} else {
|
344
|
+
return 'maybe';
|
345
|
+
}
|
346
|
+
},
|
347
|
+
|
348
|
+
isRegExp(value: unknown): value is RegExp {
|
349
|
+
return value instanceof RegExp;
|
350
|
+
},
|
351
|
+
|
352
|
+
isSet(value: unknown): value is Set<unknown> {
|
353
|
+
return value instanceof Set;
|
354
|
+
},
|
355
|
+
|
356
|
+
isSetIterator(value: unknown): value is SetIterator<unknown> {
|
357
|
+
return value instanceof (new Set()).keys().constructor;
|
358
|
+
},
|
359
|
+
|
360
|
+
isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer {
|
361
|
+
return value instanceof SharedArrayBuffer;
|
362
|
+
},
|
363
|
+
|
364
|
+
isStringObject(value: unknown): value is String {
|
365
|
+
return value instanceof String;
|
366
|
+
},
|
367
|
+
|
368
|
+
isSymbolObject(value: unknown): boolean {
|
369
|
+
return value instanceof Object(Symbol()).constructor;
|
370
|
+
},
|
371
|
+
|
372
|
+
isTypedArray(value: unknown): value is TypedArray {
|
373
|
+
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;
|
374
|
+
},
|
375
|
+
|
376
|
+
isUint8Array(value: unknown): value is Uint8Array {
|
377
|
+
return value instanceof Uint8Array;
|
378
|
+
},
|
379
|
+
|
380
|
+
isUint8ClampedArray(value: unknown): value is Uint8ClampedArray {
|
381
|
+
return value instanceof Uint8ClampedArray;
|
382
|
+
},
|
383
|
+
|
384
|
+
isUint16Array(value: unknown): value is Uint16Array {
|
385
|
+
return value instanceof Uint16Array;
|
386
|
+
},
|
387
|
+
|
388
|
+
isUint32Array(value: unknown): value is Uint32Array {
|
389
|
+
return value instanceof Uint32Array;
|
390
|
+
},
|
391
|
+
|
392
|
+
isWeakMap(value: unknown): value is WeakMap<WeakKey, unknown> {
|
393
|
+
return value instanceof WeakMap;
|
394
|
+
},
|
395
|
+
|
396
|
+
isWeakSet(value: unknown): value is WeakSet<WeakKey> {
|
397
|
+
return value instanceof WeakSet;
|
398
|
+
},
|
399
|
+
|
400
|
+
};
|
401
|
+
|
402
|
+
export function _extend(target: Object, source: Object): Object {
|
403
|
+
return Object.assign(target, source);
|
404
|
+
}
|
405
|
+
|
406
|
+
export function isArray(object: unknown): boolean {
|
407
|
+
return Array.isArray(object);
|
408
|
+
}
|