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/src/os.ts ADDED
@@ -0,0 +1,259 @@
1
+
2
+ /// <reference path="./in_fake_node.d.ts" />
3
+
4
+ export const EOL = '\n';
5
+
6
+ export function availableParallelism(): number {
7
+ return 1;
8
+ }
9
+
10
+ export function arch(): string {
11
+ return 'web';
12
+ }
13
+
14
+ enum signals {
15
+ SIGHUP = 1,
16
+ SIGINT,
17
+ SIGQUIT,
18
+ SIGILL,
19
+ SIGTRAP,
20
+ SIGABRT,
21
+ SIGIOT = SIGABRT,
22
+ SIGBUS,
23
+ SIGFPE,
24
+ SIGKILL,
25
+ SIGUSR1,
26
+ SIGUSR2,
27
+ SIGSEGV,
28
+ SIGPIPE,
29
+ SIGALRM,
30
+ SIGTERM,
31
+ SIGCHLD,
32
+ SIGSTKFLT,
33
+ SIGCONT,
34
+ SIGSTOP,
35
+ SIGTSTP,
36
+ SIGBREAK,
37
+ SIGTTIN,
38
+ SIGTTOU,
39
+ SIGURG,
40
+ SIGXCPU,
41
+ SIGXFSZ,
42
+ SIGVTALRM,
43
+ SIGPROF,
44
+ SIGWINCH,
45
+ SIGIO,
46
+ SIGPOLL = SIGIO,
47
+ SIGLOST,
48
+ SIGPWR,
49
+ SIGINFO = SIGPWR,
50
+ SIGSYS,
51
+ SIGUNUSED = SIGSYS,
52
+ }
53
+
54
+ enum errno {
55
+ E2BIG,
56
+ EACCES,
57
+ EADDRINUSE,
58
+ EADDRNONOTAVAIL,
59
+ EAFNOSUPPORT,
60
+ EAGAIN,
61
+ EALREADY,
62
+ EBADF,
63
+ EBADMSG,
64
+ EBUSY,
65
+ ECANCELED,
66
+ ECHILD,
67
+ ECONNABORTED,
68
+ ECONNREFUSED,
69
+ ECONNRESET,
70
+ EDEADLK,
71
+ EDESTADDRREQ,
72
+ EDOM,
73
+ EDQUOT,
74
+ EEXIST,
75
+ EFAULT,
76
+ EFBIG,
77
+ EHOSTUNREACH,
78
+ EIDRM,
79
+ EILSEQ,
80
+ EINPROGRESS,
81
+ EINTR,
82
+ EINVAL,
83
+ EIO,
84
+ EISCONN,
85
+ EISDIR,
86
+ ELOOP,
87
+ EMFILE,
88
+ EMLINK,
89
+ EMGSIZE,
90
+ EMULTIHOP,
91
+ ENAMETOOLONG,
92
+ ENETDOWN,
93
+ ENETRESET,
94
+ ENETUNREACH,
95
+ ENFILE,
96
+ ENOBUFS,
97
+ ENODATA,
98
+ ENODEV,
99
+ ENOENT,
100
+ ENOEXEC,
101
+ ENOLCK,
102
+ ENOLINK,
103
+ ENOMEM,
104
+ ENOMSG,
105
+ ENOPROTOOPT,
106
+ ENOSPC,
107
+ ENOSR,
108
+ ENOSTR,
109
+ ENOSYS,
110
+ ENOTCONN,
111
+ ENOTDIR,
112
+ ENOTEMPTY,
113
+ ENOTSOCK,
114
+ ENOTSUP,
115
+ ENOTTY,
116
+ ENXIO,
117
+ EOPNOTSUPP,
118
+ EOVERFLOW,
119
+ EPERM,
120
+ EPIPE,
121
+ EPROTO,
122
+ EPROTONOSUPPORT,
123
+ EPROTOTYPE,
124
+ ERANGE,
125
+ EROFS,
126
+ ESPIPE,
127
+ ESRCH,
128
+ ESTALE,
129
+ ETIME,
130
+ ETIMEDOUT,
131
+ ETXTBSY,
132
+ EWOULDBLOCK,
133
+ EXDEV,
134
+ }
135
+
136
+ enum dlopen {
137
+ RTLD_LAZY = 1,
138
+ RTLD_NOW,
139
+ RTLD_GLOBAL,
140
+ RTLD_LOCAL,
141
+ RTLD_DEEPBIND,
142
+ }
143
+
144
+ enum priority {
145
+ PRIORITY_LOW = 1,
146
+ PRIORITY_BELOW_NORMAL,
147
+ PRIORITY_NORMAL,
148
+ PRIORITY_ABOVE_NORMAL,
149
+ PRIORITY_HIGH,
150
+ PRIORITY_HIGHEST,
151
+ }
152
+
153
+ export const constants = {signals, errno, dlopen, priority};
154
+
155
+ export function cpus(): {model: string, speed: number, times: {user: number, nice: number, sys: number, idle: number, irq: number}}[] {
156
+ return [];
157
+ }
158
+
159
+ export const devNull = '/dev/null';
160
+
161
+ export function endianness(): string {
162
+ return 'LE';
163
+ }
164
+
165
+ export function freemem(): number {
166
+ return Infinity;
167
+ }
168
+
169
+ export function getPriority(pid: number = -1): number {
170
+ if (pid === -1) {
171
+ return __fakeNode_process__.priority;
172
+ }
173
+ const process = __fakeNode__.processes.get(pid);
174
+ if (process === undefined) {
175
+ throw new TypeError(`invalid PID: ${pid}`);
176
+ }
177
+ return process.priority;
178
+ }
179
+
180
+ export function homedir(): string {
181
+ return '/home/' + __fakeNode__.getUserFromUID(__fakeNode_process__.uid);
182
+ }
183
+
184
+ export function hostname(): string {
185
+ return __fakeNode__.globalenv.HOSTNAME;
186
+ }
187
+
188
+ export function loadavg(): [number, number, number] {
189
+ return [-1, -1, -1];
190
+ }
191
+
192
+ export function machine(): string {
193
+ return 'x86_64';
194
+ }
195
+
196
+ export function networkInterfaces(): {[key: string]: {address: string, netmask: string, family: 'IPV4' | 'IPV6', mac: string, internal: boolean, scopeid: number, cidr: string}} {
197
+ return {};
198
+ }
199
+
200
+ export function platform(): string {
201
+ return __fakeNode__.getPlatform();
202
+ }
203
+
204
+ export function release(): string {
205
+ return platform();
206
+ }
207
+
208
+ export function setPriority(priority: number): void;
209
+ export function setPriority(pid: number, priority: number): void;
210
+ export function setPriority(pid_or_priority: number, priority?: number): void {
211
+ if (priority === undefined) {
212
+ __fakeNode_process__.priority = pid_or_priority;
213
+ } else {
214
+ const process = __fakeNode__.processes.get(pid_or_priority);
215
+ if (process === undefined) {
216
+ throw new TypeError(`invalid PID: ${pid_or_priority}`);
217
+ }
218
+ process.priority = priority;
219
+ }
220
+ }
221
+
222
+ export function tmpdir(): string {
223
+ return __fakeNode__.globalenv.TMPDIR;
224
+ }
225
+
226
+ export function totalmem(): number {
227
+ return Infinity;
228
+ }
229
+
230
+ export function type(): string {
231
+ const data = navigator.userAgent.slice('Mozilla/5.0 ('.length, navigator.userAgent.indexOf(')'));
232
+ if (data.includes('Windows NT')) {
233
+ return 'Windows_NT';
234
+ } else if (data.includes('Linux')) {
235
+ return 'Linux';
236
+ } else if (data.includes('Mac')) {
237
+ return 'Darwin';
238
+ } else {
239
+ return 'unknown';
240
+ }
241
+ }
242
+
243
+ export function uptime(): number {
244
+ return (__fakeNode__.window.performance.now() - __fakeNode__.window.performance.timeOrigin) / 1000;
245
+ }
246
+
247
+ export function userInfo(): {username: string, uid: number, gid: number, shell: string, homedir: string} {
248
+ return {
249
+ username: __fakeNode_process__.fakeNode.getUserFromUID(__fakeNode_process__.uid),
250
+ uid: __fakeNode_process__.uid,
251
+ gid: __fakeNode_process__.gid,
252
+ shell: '/bin/sh',
253
+ homedir: homedir(),
254
+ }
255
+ }
256
+
257
+ export function version(): string {
258
+ return __fakeNode__.version;
259
+ }
package/src/path.ts ADDED
@@ -0,0 +1,141 @@
1
+
2
+ /// <reference path="./in_fake_node.d.ts" />
3
+
4
+
5
+ export interface PathObject {
6
+ dir: string;
7
+ root: string;
8
+ base: string;
9
+ name: string;
10
+ ext: string;
11
+ };
12
+
13
+
14
+ abstract class PathModule {
15
+
16
+ abstract sep: string;
17
+ abstract delimiter: string;
18
+ abstract _normalizeSplit: string | RegExp;
19
+
20
+ basename(path: string, suffix?: string): string {
21
+ const split = path.split(this.sep);
22
+ let out = split[split.length - 1];
23
+ if (suffix !== undefined && out.endsWith(suffix)) {
24
+ out = out.slice(0, out.length - suffix.length);
25
+ }
26
+ return out;
27
+ }
28
+
29
+ dirname(path: string): string {
30
+ return path.split('/').slice(0, -1).join('/');
31
+ }
32
+
33
+ extname(path: string): string {
34
+ let pos = path.indexOf('.');
35
+ if (pos === -1) {
36
+ return '';
37
+ } else {
38
+ return path.slice(pos);
39
+ }
40
+ }
41
+
42
+ format(path: string): string {
43
+ throw new TypeError('path.format is not supported in fake-node');
44
+ }
45
+
46
+ matchesGlob(path: string, pattern: string): boolean {
47
+ throw new TypeError('path.matchesGlob is not supported in fake-node');
48
+ }
49
+
50
+ abstract isAbsolute(path: string): boolean;
51
+
52
+ join(...paths: string[]) {
53
+ return this.normalize(paths.join(this.sep));
54
+ }
55
+
56
+ normalize(path: string): string {
57
+ let out: string[] = [];
58
+ for (const segment of path.split(this._normalizeSplit)) {
59
+ if (segment === '' || segment === '.') {
60
+ continue;
61
+ } else if (segment === '..') {
62
+ out.pop();
63
+ } else {
64
+ out.push(segment);
65
+ }
66
+ }
67
+ return out.join(this.sep);
68
+ }
69
+
70
+ parse(path: string): PathObject {
71
+ throw new TypeError('path.parse is not supported in fake-node');
72
+ }
73
+
74
+ relative(from: string, to: string): string {
75
+ throw new TypeError('path.relative is not supported in fake-node');
76
+ }
77
+
78
+ resolve(...paths: string[]): string {
79
+ let out = '';
80
+ for (let i = paths.length - 1; i > 0; i--) {
81
+ out += paths[i];
82
+ if (this.isAbsolute(out)) {
83
+ return out;
84
+ }
85
+ }
86
+ return __fakeNode_process__.cwd + out;
87
+ }
88
+
89
+ abstract toNamespacedPath(path: string): string;
90
+
91
+ };
92
+
93
+
94
+ export const posix = new class extends PathModule {
95
+
96
+ sep = '/';
97
+ delimiter = ':';
98
+ _normalizeSplit = '/';
99
+
100
+ isAbsolute(path: string): boolean {
101
+ return path.startsWith(this.sep);
102
+ }
103
+
104
+ toNamespacedPath(path: string): string {
105
+ return path;
106
+ }
107
+
108
+ };
109
+
110
+
111
+ export const win32 = new class extends PathModule {
112
+
113
+ sep = '\\';
114
+ delimiter = ';';
115
+ _normalizeSplit = /\/|\\/;
116
+
117
+ isAbsolute(path: string): boolean {
118
+ return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.includes(path[0]) && path[1] === ':' && path[2] === this.sep;
119
+ }
120
+
121
+ toNamespacedPath(path: string): string {
122
+ throw new TypeError('path.windows.toNamespacedPath is not supported in fake-node');
123
+ }
124
+
125
+ };
126
+
127
+
128
+ export const basename = posix.basename;
129
+ export const delimiter = posix.delimiter;
130
+ export const dirname = posix.dirname;
131
+ export const extname = posix.extname;
132
+ export const format = posix.format;
133
+ export const matchesGlob = posix.matchesGlob;
134
+ export const isAbsolute = posix.isAbsolute;
135
+ export const join = posix.join;
136
+ export const normalize = posix.normalize;
137
+ export const parse = posix.parse;
138
+ export const relative = posix.relative;
139
+ export const resolve = posix.resolve;
140
+ export const sep = posix.sep;
141
+ export const toNamespacedPath = posix.toNamespacedPath;
package/src/process.ts ADDED
@@ -0,0 +1,245 @@
1
+
2
+ /// <reference path="./in_fake_node.d.ts" />
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 channel = undefined;
13
+
14
+ export function chdir(path: string): void {
15
+ __fakeNode_process__.cwd = path;
16
+ }
17
+
18
+ export const config = {};
19
+
20
+ export const connected = undefined;
21
+
22
+ export function constrainedMemory(): number {
23
+ return 0;
24
+ }
25
+
26
+ export function availableMemory(): number {
27
+ throw new TypeError('process.availableMemory is not available in fake-node');
28
+ }
29
+
30
+ export function cpuUsage(previousValue?: {user: number, system: number}): {user: number, system: number} {
31
+ throw new TypeError('process.cpuUsage is not available in fake-node');
32
+ }
33
+
34
+ export function cwd(): string {
35
+ return __fakeNode_process__.cwd;
36
+ }
37
+
38
+ export let debugPort = -1;
39
+
40
+ export const disconnect = undefined;
41
+
42
+ export function dlopen(module: object, filename: string, flags: any /* typeof os.constants.dlopen[keyof typeof os.constants.dlopen] = os.constants.dlopen.RTLD_LAZY */): void {
43
+ throw new TypeError('process.dlopen is not supported in fake-node');
44
+ }
45
+
46
+ 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 {
47
+ throw new TypeError('process.emitWarning is not supported in fake-node');
48
+ }
49
+
50
+ export function exit(code: number = 0): void {
51
+ window.console.log('Exit code', code);
52
+ window.close();
53
+ }
54
+
55
+ export let exitCode = 0;
56
+
57
+ export const features = {
58
+ cached_builtins: true,
59
+ debug: false,
60
+ inspector: false,
61
+ ipv6: true,
62
+ require_module: true,
63
+ tls: false,
64
+ tls_alpn: false,
65
+ tls_ocsp: false,
66
+ tls_sni: false,
67
+ typescript: false,
68
+ uv: false,
69
+ };
70
+
71
+ export const finalization = {
72
+
73
+ register(ref: object, callback: (ref: object, event: string) => void): void {
74
+ throw new TypeError('process.finalization is not supported in fake-node');
75
+ },
76
+
77
+ registerBeforeExit(ref: object, callback: (ref: object, event: string) => void): void {
78
+ throw new TypeError('process.finalization is not supported in fake-node');
79
+ },
80
+
81
+ unregister(ref: object): void {
82
+ throw new TypeError('process.finalization is not supported in fake-node');
83
+ },
84
+
85
+ };
86
+
87
+ export function getActiveResourcesInfo(): string[] {
88
+ return [];
89
+ }
90
+
91
+ export function getBuiltinModule(id: string): any {
92
+ return __fakeNode_process__.fakeNode.modules.get(id);
93
+ }
94
+
95
+ export function getegid(): number {
96
+ return __fakeNode_process__.gid;
97
+ }
98
+
99
+ export function geteuid(): number {
100
+ return __fakeNode_process__.uid;
101
+ }
102
+
103
+ export function getgid(): number {
104
+ return __fakeNode_process__.gid;
105
+ }
106
+
107
+ export function getgroups(): number[] {
108
+ return [__fakeNode_process__.gid].concat(__fakeNode_process__.groups);
109
+ }
110
+
111
+ export function getuid(): number {
112
+ return __fakeNode_process__.uid;
113
+ }
114
+
115
+ let errorCallback: null | number = null;
116
+
117
+ export function hasUncaughtExecptionCaptureCallback(): boolean {
118
+ return errorCallback !== null;
119
+ }
120
+
121
+ export function hrtime(time?: [number, number]): [number, number] {
122
+ let value = window.performance.now();
123
+ if (time !== undefined) {
124
+ value -= time[0] + time[1] / 1000000;
125
+ }
126
+ return [Math.floor(value), (value - Math.floor(value) * 1000000)];
127
+ }
128
+
129
+ hrtime.bigint = function(): bigint {
130
+ return BigInt(window.performance.now());
131
+ }
132
+
133
+ export function initgroups(user: string | number, extraGroup: string | number): void {
134
+ throw new TypeError('process.initgroups is not supported in fake-node');
135
+ }
136
+
137
+ export function kill(pid: number, signal: string | number): void {
138
+ throw new TypeError('process.kill is not supported in fake-node');
139
+ }
140
+
141
+ export function loadEnvFile(path: string = './.env'): void {
142
+ throw new TypeError('process.loadEnvFile is not supported in fake-node');
143
+ }
144
+
145
+ export function memoryUsage(): {rss: number, heapTotal: number, heapUsed: number, external: number, arrayBuffers: number} {
146
+ throw new TypeError('process.memoryUsage is not supported in fake-node');
147
+ }
148
+
149
+ memoryUsage.rss = function(): number {
150
+ throw new TypeError('process.memoryUsage.rss is not supported in fake-node');
151
+ }
152
+
153
+ export function nextTick(callback: Function, ...args: any[]): void {
154
+ window.setTimeout(callback, 0, ...args);
155
+ }
156
+
157
+ export const noDeprecation = false;
158
+
159
+ export const permission = {
160
+
161
+ has(scope: string, reference?: string): void {
162
+ throw new TypeError('process.permission.has is not supported in fake-node');
163
+ }
164
+
165
+ };
166
+
167
+ export function ref(maybeRefable: any): void {
168
+ throw new TypeError('process.ref is not supported in fake-node');
169
+ }
170
+
171
+ export const pid = 1;
172
+
173
+ export const ppid = 1;
174
+
175
+ export const release = {
176
+ name: 'fake-node',
177
+ sourceUrl: '', // todo: add something here
178
+ headersUrl: '',
179
+ lts: 'Hydrogen',
180
+ };
181
+
182
+ export function setegid(id: string | number): void {
183
+ __fakeNode_process__.gid = __fakeNode__.getGIDFromGroup(id);
184
+ }
185
+
186
+ export function seteuid(id: string | number): void {
187
+ __fakeNode_process__.uid = __fakeNode__.getUIDFromUser(id);
188
+ }
189
+
190
+ export function setgid(id: string | number): void {
191
+ __fakeNode_process__.gid = __fakeNode__.getGIDFromGroup(id);
192
+ }
193
+
194
+ export function setgroups(groups: (string | number)[]): void {
195
+ __fakeNode_process__.groups = groups.map(__fakeNode__.getGIDFromGroup);
196
+ }
197
+
198
+ export function setuid(id: string | number): void {
199
+ __fakeNode_process__.uid = __fakeNode__.getUIDFromUser(id);
200
+ }
201
+
202
+ export function setSourceMapsEnabledVal(val: boolean): void {
203
+ throw new TypeError('process.setSourceMapsEnabledVal is not supported in fake-node');
204
+ }
205
+
206
+ export function setUncaughtExceptionCaptureCallback(func: Function | null): void {
207
+ if (errorCallback !== null) {
208
+ __fakeNode__.removeErrorCallback(errorCallback);
209
+ }
210
+ if (func !== null) {
211
+ errorCallback = __fakeNode__.addErrorCallback(func);
212
+ }
213
+ }
214
+
215
+ export const sourceMapsEnabled = false;
216
+
217
+ export const stderr = undefined; // todo: put stuff here
218
+
219
+ export const stdin = undefined;
220
+
221
+ export const stdout = undefined;
222
+
223
+ export let throwDeprecation = false;
224
+
225
+ export let title = ''; // todo: put something here
226
+
227
+ export const traceDeprecation = false;
228
+
229
+ export function umask(): number;
230
+ export function umask(mask: number): void;
231
+ export function umask(mask?: number): number | void {
232
+ if (mask === undefined) {
233
+ return __fakeNode_process__.umask;
234
+ } else {
235
+ __fakeNode_process__.umask = mask;
236
+ }
237
+ }
238
+
239
+ export function unref(maybeRefable: any): void {
240
+ throw new TypeError('process.unref is not supported in fake-node');
241
+ }
242
+
243
+ export function uptime(): number {
244
+ return __fakeNode_process__.fakeNode.window.performance.now() / 1000;
245
+ }
@@ -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
+ }