@run0/jiki 0.1.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 (152) hide show
  1. package/dist/browser-bundle.d.ts +40 -0
  2. package/dist/builtins.d.ts +22 -0
  3. package/dist/code-transform.d.ts +7 -0
  4. package/dist/config/cdn.d.ts +13 -0
  5. package/dist/container.d.ts +101 -0
  6. package/dist/dev-server.d.ts +69 -0
  7. package/dist/errors.d.ts +19 -0
  8. package/dist/frameworks/code-transforms.d.ts +32 -0
  9. package/dist/frameworks/next-api-handler.d.ts +72 -0
  10. package/dist/frameworks/next-dev-server.d.ts +141 -0
  11. package/dist/frameworks/next-html-generator.d.ts +36 -0
  12. package/dist/frameworks/next-route-resolver.d.ts +19 -0
  13. package/dist/frameworks/next-shims.d.ts +78 -0
  14. package/dist/frameworks/remix-dev-server.d.ts +47 -0
  15. package/dist/frameworks/sveltekit-dev-server.d.ts +43 -0
  16. package/dist/frameworks/vite-dev-server.d.ts +50 -0
  17. package/dist/fs-errors.d.ts +36 -0
  18. package/dist/index.cjs +14916 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.ts +61 -0
  21. package/dist/index.mjs +14898 -0
  22. package/dist/index.mjs.map +1 -0
  23. package/dist/kernel.d.ts +48 -0
  24. package/dist/memfs.d.ts +144 -0
  25. package/dist/metrics.d.ts +78 -0
  26. package/dist/module-resolver.d.ts +60 -0
  27. package/dist/network-interceptor.d.ts +71 -0
  28. package/dist/npm/cache.d.ts +76 -0
  29. package/dist/npm/index.d.ts +60 -0
  30. package/dist/npm/lockfile-reader.d.ts +32 -0
  31. package/dist/npm/pnpm.d.ts +18 -0
  32. package/dist/npm/registry.d.ts +45 -0
  33. package/dist/npm/resolver.d.ts +39 -0
  34. package/dist/npm/sync-installer.d.ts +18 -0
  35. package/dist/npm/tarball.d.ts +4 -0
  36. package/dist/npm/workspaces.d.ts +46 -0
  37. package/dist/persistence.d.ts +94 -0
  38. package/dist/plugin.d.ts +156 -0
  39. package/dist/polyfills/assert.d.ts +30 -0
  40. package/dist/polyfills/child_process.d.ts +116 -0
  41. package/dist/polyfills/chokidar.d.ts +18 -0
  42. package/dist/polyfills/crypto.d.ts +49 -0
  43. package/dist/polyfills/events.d.ts +28 -0
  44. package/dist/polyfills/fs.d.ts +82 -0
  45. package/dist/polyfills/http.d.ts +147 -0
  46. package/dist/polyfills/module.d.ts +29 -0
  47. package/dist/polyfills/net.d.ts +53 -0
  48. package/dist/polyfills/os.d.ts +91 -0
  49. package/dist/polyfills/path.d.ts +96 -0
  50. package/dist/polyfills/perf_hooks.d.ts +21 -0
  51. package/dist/polyfills/process.d.ts +99 -0
  52. package/dist/polyfills/querystring.d.ts +15 -0
  53. package/dist/polyfills/readdirp.d.ts +18 -0
  54. package/dist/polyfills/readline.d.ts +32 -0
  55. package/dist/polyfills/stream.d.ts +106 -0
  56. package/dist/polyfills/stubs.d.ts +737 -0
  57. package/dist/polyfills/tty.d.ts +25 -0
  58. package/dist/polyfills/url.d.ts +41 -0
  59. package/dist/polyfills/util.d.ts +61 -0
  60. package/dist/polyfills/v8.d.ts +43 -0
  61. package/dist/polyfills/vm.d.ts +76 -0
  62. package/dist/polyfills/worker-threads.d.ts +77 -0
  63. package/dist/polyfills/ws.d.ts +32 -0
  64. package/dist/polyfills/zlib.d.ts +87 -0
  65. package/dist/runtime-helpers.d.ts +4 -0
  66. package/dist/runtime-interface.d.ts +39 -0
  67. package/dist/sandbox.d.ts +69 -0
  68. package/dist/server-bridge.d.ts +55 -0
  69. package/dist/shell-commands.d.ts +2 -0
  70. package/dist/shell.d.ts +101 -0
  71. package/dist/transpiler.d.ts +47 -0
  72. package/dist/type-checker.d.ts +57 -0
  73. package/dist/types/package-json.d.ts +17 -0
  74. package/dist/utils/binary-encoding.d.ts +4 -0
  75. package/dist/utils/hash.d.ts +6 -0
  76. package/dist/utils/safe-path.d.ts +6 -0
  77. package/dist/worker-runtime.d.ts +34 -0
  78. package/package.json +59 -0
  79. package/src/browser-bundle.ts +498 -0
  80. package/src/builtins.ts +222 -0
  81. package/src/code-transform.ts +183 -0
  82. package/src/config/cdn.ts +17 -0
  83. package/src/container.ts +343 -0
  84. package/src/dev-server.ts +322 -0
  85. package/src/errors.ts +604 -0
  86. package/src/frameworks/code-transforms.ts +667 -0
  87. package/src/frameworks/next-api-handler.ts +366 -0
  88. package/src/frameworks/next-dev-server.ts +1252 -0
  89. package/src/frameworks/next-html-generator.ts +585 -0
  90. package/src/frameworks/next-route-resolver.ts +521 -0
  91. package/src/frameworks/next-shims.ts +1084 -0
  92. package/src/frameworks/remix-dev-server.ts +163 -0
  93. package/src/frameworks/sveltekit-dev-server.ts +197 -0
  94. package/src/frameworks/vite-dev-server.ts +370 -0
  95. package/src/fs-errors.ts +118 -0
  96. package/src/index.ts +188 -0
  97. package/src/kernel.ts +381 -0
  98. package/src/memfs.ts +1006 -0
  99. package/src/metrics.ts +140 -0
  100. package/src/module-resolver.ts +511 -0
  101. package/src/network-interceptor.ts +143 -0
  102. package/src/npm/cache.ts +172 -0
  103. package/src/npm/index.ts +377 -0
  104. package/src/npm/lockfile-reader.ts +105 -0
  105. package/src/npm/pnpm.ts +108 -0
  106. package/src/npm/registry.ts +120 -0
  107. package/src/npm/resolver.ts +339 -0
  108. package/src/npm/sync-installer.ts +217 -0
  109. package/src/npm/tarball.ts +136 -0
  110. package/src/npm/workspaces.ts +255 -0
  111. package/src/persistence.ts +235 -0
  112. package/src/plugin.ts +293 -0
  113. package/src/polyfills/assert.ts +164 -0
  114. package/src/polyfills/child_process.ts +535 -0
  115. package/src/polyfills/chokidar.ts +52 -0
  116. package/src/polyfills/crypto.ts +433 -0
  117. package/src/polyfills/events.ts +178 -0
  118. package/src/polyfills/fs.ts +297 -0
  119. package/src/polyfills/http.ts +478 -0
  120. package/src/polyfills/module.ts +97 -0
  121. package/src/polyfills/net.ts +123 -0
  122. package/src/polyfills/os.ts +108 -0
  123. package/src/polyfills/path.ts +169 -0
  124. package/src/polyfills/perf_hooks.ts +30 -0
  125. package/src/polyfills/process.ts +349 -0
  126. package/src/polyfills/querystring.ts +66 -0
  127. package/src/polyfills/readdirp.ts +72 -0
  128. package/src/polyfills/readline.ts +80 -0
  129. package/src/polyfills/stream.ts +610 -0
  130. package/src/polyfills/stubs.ts +600 -0
  131. package/src/polyfills/tty.ts +43 -0
  132. package/src/polyfills/url.ts +97 -0
  133. package/src/polyfills/util.ts +173 -0
  134. package/src/polyfills/v8.ts +62 -0
  135. package/src/polyfills/vm.ts +111 -0
  136. package/src/polyfills/worker-threads.ts +189 -0
  137. package/src/polyfills/ws.ts +73 -0
  138. package/src/polyfills/zlib.ts +244 -0
  139. package/src/runtime-helpers.ts +83 -0
  140. package/src/runtime-interface.ts +46 -0
  141. package/src/sandbox.ts +178 -0
  142. package/src/server-bridge.ts +473 -0
  143. package/src/service-worker.ts +153 -0
  144. package/src/shell-commands.ts +708 -0
  145. package/src/shell.ts +795 -0
  146. package/src/transpiler.ts +282 -0
  147. package/src/type-checker.ts +241 -0
  148. package/src/types/package-json.ts +17 -0
  149. package/src/utils/binary-encoding.ts +38 -0
  150. package/src/utils/hash.ts +24 -0
  151. package/src/utils/safe-path.ts +38 -0
  152. package/src/worker-runtime.ts +42 -0
@@ -0,0 +1,478 @@
1
+ import { EventEmitter } from "./events";
2
+ import { Readable, Writable, BufferImpl } from "./stream";
3
+
4
+ type ServerListenCallback = (port: number, server: Server) => void;
5
+ type ServerCloseCallback = (port: number) => void;
6
+
7
+ let _onServerListen: ServerListenCallback | null = null;
8
+ let _onServerClose: ServerCloseCallback | null = null;
9
+
10
+ export function setServerListenCallback(cb: ServerListenCallback): void {
11
+ _onServerListen = cb;
12
+ }
13
+
14
+ export function setServerCloseCallback(cb: ServerCloseCallback): void {
15
+ _onServerClose = cb;
16
+ }
17
+
18
+ export function getServer(_port: number): Server | undefined {
19
+ return undefined;
20
+ }
21
+
22
+ export const METHODS = [
23
+ "GET",
24
+ "POST",
25
+ "PUT",
26
+ "DELETE",
27
+ "PATCH",
28
+ "HEAD",
29
+ "OPTIONS",
30
+ ];
31
+ export const STATUS_CODES: Record<number, string> = {
32
+ 200: "OK",
33
+ 201: "Created",
34
+ 204: "No Content",
35
+ 301: "Moved Permanently",
36
+ 302: "Found",
37
+ 304: "Not Modified",
38
+ 400: "Bad Request",
39
+ 401: "Unauthorized",
40
+ 403: "Forbidden",
41
+ 404: "Not Found",
42
+ 405: "Method Not Allowed",
43
+ 500: "Internal Server Error",
44
+ 502: "Bad Gateway",
45
+ 503: "Service Unavailable",
46
+ };
47
+
48
+ export class IncomingMessage extends Readable {
49
+ method: string;
50
+ url: string;
51
+ headers: Record<string, string | string[] | undefined>;
52
+ httpVersion = "1.1";
53
+ statusCode?: number;
54
+ statusMessage?: string;
55
+ socket = { remoteAddress: "127.0.0.1", remotePort: 0, destroy() {} };
56
+ complete = true;
57
+ aborted = false;
58
+ connection = { remoteAddress: "127.0.0.1" };
59
+
60
+ rawHeaders: string[] = [];
61
+ trailers: Record<string, string> = {};
62
+ rawTrailers: string[] = [];
63
+
64
+ constructor(opts?: {
65
+ method?: string;
66
+ url?: string;
67
+ headers?: Record<string, string>;
68
+ }) {
69
+ super();
70
+ this.method = opts?.method || "GET";
71
+ this.url = opts?.url || "/";
72
+ this.headers = opts?.headers || {};
73
+ // Build rawHeaders from headers
74
+ if (opts?.headers) {
75
+ for (const [key, value] of Object.entries(opts.headers)) {
76
+ this.rawHeaders.push(key, value);
77
+ }
78
+ }
79
+ }
80
+
81
+ setTimeout(_ms: number, _cb?: () => void): this {
82
+ if (_cb) setTimeout(_cb, 0);
83
+ return this;
84
+ }
85
+ }
86
+
87
+ export class ServerResponse extends Writable {
88
+ statusCode = 200;
89
+ statusMessage = "";
90
+ headersSent = false;
91
+ finished = false;
92
+ private _headers: Record<string, string | string[]> = {};
93
+ private _body: Uint8Array[] = [];
94
+
95
+ setHeader(name: string, value: string | string[]): this {
96
+ this._headers[name.toLowerCase()] = value;
97
+ return this;
98
+ }
99
+ getHeader(name: string): string | string[] | undefined {
100
+ return this._headers[name.toLowerCase()];
101
+ }
102
+ removeHeader(name: string): void {
103
+ delete this._headers[name.toLowerCase()];
104
+ }
105
+ getHeaders(): Record<string, string | string[]> {
106
+ return { ...this._headers };
107
+ }
108
+ hasHeader(name: string): boolean {
109
+ return name.toLowerCase() in this._headers;
110
+ }
111
+ writeHead(
112
+ statusCode: number,
113
+ statusMessage?: string | Record<string, string | string[]>,
114
+ headers?: Record<string, string | string[]>,
115
+ ): this {
116
+ this.statusCode = statusCode;
117
+ if (typeof statusMessage === "string") this.statusMessage = statusMessage;
118
+ const hdrs = typeof statusMessage === "object" ? statusMessage : headers;
119
+ if (hdrs) Object.entries(hdrs).forEach(([k, v]) => this.setHeader(k, v));
120
+ this.headersSent = true;
121
+ return this;
122
+ }
123
+
124
+ _write(chunk: unknown, _encoding: string, callback: () => void): void {
125
+ if (typeof chunk === "string")
126
+ this._body.push(new TextEncoder().encode(chunk));
127
+ else if (chunk instanceof Uint8Array) this._body.push(chunk);
128
+ callback();
129
+ }
130
+
131
+ end(
132
+ chunkOrCb?: unknown,
133
+ encodingOrCb?: string | (() => void),
134
+ cb?: () => void,
135
+ ): this {
136
+ this.finished = true;
137
+ return super.end(chunkOrCb, encodingOrCb as string, cb) as this;
138
+ }
139
+
140
+ getBody(): Uint8Array {
141
+ const total = this._body.reduce((s, b) => s + b.length, 0);
142
+ const combined = new Uint8Array(total);
143
+ let offset = 0;
144
+ for (const chunk of this._body) {
145
+ combined.set(chunk, offset);
146
+ offset += chunk.length;
147
+ }
148
+ return combined;
149
+ }
150
+
151
+ getBodyString(): string {
152
+ return new TextDecoder().decode(this.getBody());
153
+ }
154
+
155
+ addTrailers(headers: Record<string, string>): void {
156
+ for (const [key, value] of Object.entries(headers)) {
157
+ this.setHeader(key, value);
158
+ }
159
+ }
160
+
161
+ flushHeaders(): void {
162
+ this.headersSent = true;
163
+ }
164
+
165
+ cork(): void {
166
+ /* no-op in browser */
167
+ }
168
+ uncork(): void {
169
+ /* no-op in browser */
170
+ }
171
+ }
172
+
173
+ type RequestHandler = (req: IncomingMessage, res: ServerResponse) => void;
174
+
175
+ export class Server extends EventEmitter {
176
+ private handler: RequestHandler | null = null;
177
+ private _port = 0;
178
+ listening = false;
179
+ keepAliveTimeout = 5000;
180
+ headersTimeout = 60000;
181
+ requestTimeout = 300000;
182
+ timeout = 0;
183
+
184
+ constructor(handler?: RequestHandler) {
185
+ super();
186
+ if (handler) this.handler = handler;
187
+ }
188
+
189
+ setTimeout(ms?: number, _cb?: () => void): this {
190
+ this.timeout = ms ?? 0;
191
+ return this;
192
+ }
193
+
194
+ listen(
195
+ portOrOpts?: number | Record<string, unknown>,
196
+ hostOrCb?: string | (() => void),
197
+ cb?: () => void,
198
+ ): this {
199
+ if (typeof portOrOpts === "object" && portOrOpts !== null) {
200
+ this._port = (portOrOpts.port as number) || 3000;
201
+ const callback =
202
+ typeof portOrOpts.cb === "function"
203
+ ? (portOrOpts.cb as () => void)
204
+ : undefined;
205
+ if (callback) setTimeout(callback, 0);
206
+ } else {
207
+ this._port = portOrOpts || 3000;
208
+ const callback = typeof hostOrCb === "function" ? hostOrCb : cb;
209
+ if (callback) setTimeout(callback, 0);
210
+ }
211
+ this.listening = true;
212
+ this.emit("listening");
213
+ if (_onServerListen) _onServerListen(this._port, this);
214
+ return this;
215
+ }
216
+
217
+ close(cb?: () => void): this {
218
+ const port = this._port;
219
+ this.listening = false;
220
+ if (cb) setTimeout(cb, 0);
221
+ this.emit("close");
222
+ if (_onServerClose) _onServerClose(port);
223
+ return this;
224
+ }
225
+
226
+ address(): { port: number; family: string; address: string } | null {
227
+ return this.listening
228
+ ? { port: this._port, family: "IPv4", address: "0.0.0.0" }
229
+ : null;
230
+ }
231
+
232
+ async handleRequest(
233
+ methodOrReq: string | IncomingMessage,
234
+ urlOrRes?: string | ServerResponse,
235
+ headers?: Record<string, string>,
236
+ body?: BufferImpl | string,
237
+ ): Promise<{
238
+ statusCode: number;
239
+ statusMessage: string;
240
+ headers: Record<string, string>;
241
+ body: BufferImpl;
242
+ }> {
243
+ const handler =
244
+ this.handler || (this.listeners("request")[0] as RequestHandler);
245
+ if (!handler) {
246
+ return {
247
+ statusCode: 503,
248
+ statusMessage: "No Handler",
249
+ headers: {},
250
+ body: BufferImpl.from(""),
251
+ };
252
+ }
253
+
254
+ if (
255
+ methodOrReq instanceof IncomingMessage &&
256
+ urlOrRes instanceof ServerResponse
257
+ ) {
258
+ handler(methodOrReq, urlOrRes);
259
+ return {
260
+ statusCode: urlOrRes.statusCode,
261
+ statusMessage: urlOrRes.statusMessage || "OK",
262
+ headers: Object.fromEntries(
263
+ Object.entries(urlOrRes.getHeaders()).map(([k, v]) => [
264
+ k,
265
+ Array.isArray(v) ? v.join(", ") : String(v),
266
+ ]),
267
+ ),
268
+ body: BufferImpl.from(urlOrRes.getBody()),
269
+ };
270
+ }
271
+
272
+ const req = new IncomingMessage({
273
+ method: methodOrReq as string,
274
+ url: urlOrRes as string,
275
+ headers: headers || {},
276
+ });
277
+ if (body) {
278
+ const data =
279
+ typeof body === "string" ? new TextEncoder().encode(body) : body;
280
+ req.push(BufferImpl.from(data));
281
+ req.push(null);
282
+ } else {
283
+ req.push(null);
284
+ }
285
+
286
+ const res = new ServerResponse();
287
+ await new Promise<void>(resolve => {
288
+ res.on("finish", resolve);
289
+ handler(req, res);
290
+ if (res.finished) resolve();
291
+ });
292
+
293
+ return {
294
+ statusCode: res.statusCode,
295
+ statusMessage: res.statusMessage || "OK",
296
+ headers: Object.fromEntries(
297
+ Object.entries(res.getHeaders()).map(([k, v]) => [
298
+ k,
299
+ Array.isArray(v) ? v.join(", ") : String(v),
300
+ ]),
301
+ ),
302
+ body: BufferImpl.from(res.getBody()),
303
+ };
304
+ }
305
+ }
306
+
307
+ export function createServer(handler?: RequestHandler): Server {
308
+ return new Server(handler);
309
+ }
310
+
311
+ export class ClientRequest extends Writable {
312
+ method: string;
313
+ path: string;
314
+ private _headers: Record<string, string> = {};
315
+ private _body: Uint8Array[] = [];
316
+ private _url: string;
317
+
318
+ constructor(
319
+ options:
320
+ | string
321
+ | {
322
+ hostname?: string;
323
+ host?: string;
324
+ port?: number;
325
+ path?: string;
326
+ method?: string;
327
+ headers?: Record<string, string>;
328
+ protocol?: string;
329
+ },
330
+ ) {
331
+ super();
332
+ const opts = typeof options === "string" ? new URL(options) : options;
333
+ const hostname =
334
+ ("hostname" in opts ? opts.hostname : "") ||
335
+ ("host" in opts ? opts.host : "") ||
336
+ "localhost";
337
+ const port = ("port" in opts ? opts.port : undefined) || "";
338
+ const protocol = ("protocol" in opts ? opts.protocol : "http:") || "http:";
339
+ this.path =
340
+ ("pathname" in opts ? opts.pathname : "") ||
341
+ ("path" in opts ? opts.path : "") ||
342
+ "/";
343
+ this.method = ("method" in opts ? opts.method : "") || "GET";
344
+ if ("headers" in opts && opts.headers)
345
+ this._headers = opts.headers as Record<string, string>;
346
+ this._url = `${protocol}//${hostname}${port ? ":" + port : ""}${this.path}`;
347
+ }
348
+
349
+ aborted = false;
350
+
351
+ setHeader(name: string, value: string): void {
352
+ this._headers[name] = value;
353
+ }
354
+ getHeader(name: string): string | undefined {
355
+ return this._headers[name];
356
+ }
357
+ removeHeader(name: string): void {
358
+ delete this._headers[name];
359
+ }
360
+
361
+ abort(): void {
362
+ this.aborted = true;
363
+ this.emit("abort");
364
+ }
365
+
366
+ setTimeout(_ms: number, _cb?: () => void): this {
367
+ if (_cb) setTimeout(_cb, 0);
368
+ return this;
369
+ }
370
+
371
+ override destroy(error?: Error): this {
372
+ this.aborted = true;
373
+ if (error) this.emit("error", error);
374
+ this.emit("close");
375
+ return this;
376
+ }
377
+
378
+ _write(chunk: unknown, _encoding: string, callback: () => void): void {
379
+ if (typeof chunk === "string")
380
+ this._body.push(new TextEncoder().encode(chunk));
381
+ else if (chunk instanceof Uint8Array) this._body.push(chunk);
382
+ callback();
383
+ }
384
+
385
+ end(
386
+ chunkOrCb?: unknown,
387
+ encodingOrCb?: string | (() => void),
388
+ cb?: () => void,
389
+ ): this {
390
+ if (typeof chunkOrCb === "string" || chunkOrCb instanceof Uint8Array) {
391
+ this._write(chunkOrCb, "utf8", () => {});
392
+ }
393
+ const callback =
394
+ typeof chunkOrCb === "function"
395
+ ? chunkOrCb
396
+ : typeof encodingOrCb === "function"
397
+ ? encodingOrCb
398
+ : cb;
399
+
400
+ const body =
401
+ this._body.length > 0 ? BufferImpl.concat(this._body) : undefined;
402
+ fetch(this._url, {
403
+ method: this.method,
404
+ headers: this._headers,
405
+ body: this.method !== "GET" && this.method !== "HEAD" ? body : undefined,
406
+ })
407
+ .then(async response => {
408
+ const incoming = new IncomingMessage();
409
+ incoming.statusCode = response.status;
410
+ incoming.statusMessage = response.statusText;
411
+ const hdrs: Record<string, string> = {};
412
+ response.headers.forEach((v, k) => {
413
+ hdrs[k] = v;
414
+ });
415
+ incoming.headers = hdrs;
416
+ const data = new Uint8Array(await response.arrayBuffer());
417
+ incoming.push(BufferImpl.from(data));
418
+ incoming.push(null);
419
+ this.emit("response", incoming);
420
+ })
421
+ .catch(err => this.emit("error", err));
422
+
423
+ if (typeof callback === "function") setTimeout(callback, 0);
424
+ return this;
425
+ }
426
+ }
427
+
428
+ export function request(
429
+ options: unknown,
430
+ cb?: (res: IncomingMessage) => void,
431
+ ): ClientRequest {
432
+ const req = new ClientRequest(options as string);
433
+ if (cb) req.on("response", cb);
434
+ return req;
435
+ }
436
+
437
+ export function get(
438
+ options: unknown,
439
+ cb?: (res: IncomingMessage) => void,
440
+ ): ClientRequest {
441
+ const req = request(options, cb);
442
+ req.end();
443
+ return req;
444
+ }
445
+
446
+ export const Agent = class Agent {
447
+ constructor(_opts?: unknown) {}
448
+ };
449
+ export const globalAgent = new Agent();
450
+
451
+ export default {
452
+ METHODS,
453
+ STATUS_CODES,
454
+ IncomingMessage,
455
+ ServerResponse,
456
+ Server,
457
+ ClientRequest,
458
+ createServer,
459
+ request,
460
+ get,
461
+ Agent,
462
+ globalAgent,
463
+ setServerListenCallback,
464
+ setServerCloseCallback,
465
+ getServer,
466
+ };
467
+
468
+ export const httpsModule = {
469
+ createServer,
470
+ request,
471
+ get,
472
+ Server,
473
+ Agent,
474
+ globalAgent,
475
+ IncomingMessage,
476
+ ServerResponse,
477
+ ClientRequest,
478
+ };
@@ -0,0 +1,97 @@
1
+ import * as pathShim from "./path";
2
+
3
+ export const builtinModules = [
4
+ "assert",
5
+ "buffer",
6
+ "child_process",
7
+ "cluster",
8
+ "console",
9
+ "crypto",
10
+ "dgram",
11
+ "dns",
12
+ "domain",
13
+ "events",
14
+ "fs",
15
+ "http",
16
+ "http2",
17
+ "https",
18
+ "inspector",
19
+ "module",
20
+ "net",
21
+ "os",
22
+ "path",
23
+ "perf_hooks",
24
+ "process",
25
+ "querystring",
26
+ "readline",
27
+ "stream",
28
+ "string_decoder",
29
+ "timers",
30
+ "tls",
31
+ "tty",
32
+ "url",
33
+ "util",
34
+ "v8",
35
+ "vm",
36
+ "worker_threads",
37
+ "zlib",
38
+ ];
39
+
40
+ export function isBuiltin(moduleName: string): boolean {
41
+ const name = moduleName.startsWith("node:")
42
+ ? moduleName.slice(5)
43
+ : moduleName;
44
+ return builtinModules.includes(name);
45
+ }
46
+
47
+ let _requireFactory: ((dir: string) => (id: string) => unknown) | null = null;
48
+
49
+ export function initModule(
50
+ requireFactory: (dir: string) => (id: string) => unknown,
51
+ ): void {
52
+ _requireFactory = requireFactory;
53
+ }
54
+
55
+ export function createRequire(filename: string): (id: string) => unknown {
56
+ if (_requireFactory) {
57
+ const dir = pathShim.dirname(filename);
58
+ return _requireFactory(dir);
59
+ }
60
+ return (_id: string) => {
61
+ throw new Error(`createRequire is not supported in browser runtime`);
62
+ };
63
+ }
64
+
65
+ export class Module {
66
+ id: string;
67
+ filename: string;
68
+ exports: unknown = {};
69
+ loaded = false;
70
+ children: Module[] = [];
71
+ paths: string[] = [];
72
+ parent: Module | null = null;
73
+
74
+ constructor(id?: string) {
75
+ this.id = id || "";
76
+ this.filename = id || "";
77
+ }
78
+
79
+ static _resolveFilename(request: string, _parent?: unknown): string {
80
+ return request;
81
+ }
82
+ static _cache: Record<string, Module> = {};
83
+ static _extensions: Record<string, unknown> = {
84
+ ".js": null,
85
+ ".json": null,
86
+ ".node": null,
87
+ };
88
+ static builtinModules = builtinModules;
89
+ static isBuiltin = isBuiltin;
90
+ static createRequire = createRequire;
91
+ static wrap(code: string): string {
92
+ return `(function (exports, require, module, __filename, __dirname) { ${code}\n});`;
93
+ }
94
+ static Module = Module;
95
+ }
96
+
97
+ export default { Module, builtinModules, isBuiltin, createRequire };
@@ -0,0 +1,123 @@
1
+ import { Duplex } from "./stream";
2
+ import { EventEmitter } from "./events";
3
+
4
+ export class Socket extends Duplex {
5
+ remoteAddress = "127.0.0.1";
6
+ remotePort = 0;
7
+ localAddress = "127.0.0.1";
8
+ localPort = 0;
9
+ connecting = false;
10
+ destroyed = false;
11
+ timeout = 0;
12
+ allowHalfOpen = false;
13
+
14
+ connect(...args: unknown[]): this {
15
+ console.warn(
16
+ "[jiki] net.Socket.connect() is a no-op in browser runtime. TCP connections are not supported.",
17
+ );
18
+ const cb =
19
+ typeof args[args.length - 1] === "function"
20
+ ? (args[args.length - 1] as () => void)
21
+ : undefined;
22
+ if (cb) setTimeout(cb, 0);
23
+ setTimeout(() => this.emit("connect"), 0);
24
+ return this;
25
+ }
26
+
27
+ destroy(error?: Error): this {
28
+ this.destroyed = true;
29
+ this.emit("close", !!error);
30
+ return this;
31
+ }
32
+ setTimeout(timeout: number, cb?: () => void): this {
33
+ this.timeout = timeout;
34
+ if (cb) this.once("timeout", cb);
35
+ return this;
36
+ }
37
+ setNoDelay(): this {
38
+ return this;
39
+ }
40
+ setKeepAlive(): this {
41
+ return this;
42
+ }
43
+ ref(): this {
44
+ return this;
45
+ }
46
+ unref(): this {
47
+ return this;
48
+ }
49
+ address(): { port: number; family: string; address: string } {
50
+ return { port: this.localPort, family: "IPv4", address: this.localAddress };
51
+ }
52
+ }
53
+
54
+ export class Server extends EventEmitter {
55
+ listening = false;
56
+ listen(
57
+ port?: number,
58
+ hostOrCb?: string | (() => void),
59
+ cb?: () => void,
60
+ ): this {
61
+ this.listening = true;
62
+ const callback = typeof hostOrCb === "function" ? hostOrCb : cb;
63
+ if (callback) setTimeout(callback, 0);
64
+ this.emit("listening");
65
+ return this;
66
+ }
67
+ close(cb?: () => void): this {
68
+ this.listening = false;
69
+ if (cb) setTimeout(cb, 0);
70
+ return this;
71
+ }
72
+ address(): { port: number; family: string; address: string } | null {
73
+ return this.listening
74
+ ? { port: 0, family: "IPv4", address: "0.0.0.0" }
75
+ : null;
76
+ }
77
+ ref(): this {
78
+ return this;
79
+ }
80
+ unref(): this {
81
+ return this;
82
+ }
83
+ }
84
+
85
+ export function createServer(
86
+ _options?: unknown,
87
+ _connectionListener?: unknown,
88
+ ): Server {
89
+ console.warn(
90
+ "[jiki] net.createServer() is a stub in browser runtime. TCP server is not supported.",
91
+ );
92
+ return new Server();
93
+ }
94
+ export function createConnection(_options: unknown, cb?: () => void): Socket {
95
+ const s = new Socket();
96
+ if (cb) s.connect({}, cb);
97
+ return s;
98
+ }
99
+ export function connect(_options: unknown, cb?: () => void): Socket {
100
+ return createConnection(_options, cb);
101
+ }
102
+ export function isIP(input: string): number {
103
+ if (/^\d+\.\d+\.\d+\.\d+$/.test(input)) return 4;
104
+ if (input.includes(":")) return 6;
105
+ return 0;
106
+ }
107
+ export function isIPv4(input: string): boolean {
108
+ return isIP(input) === 4;
109
+ }
110
+ export function isIPv6(input: string): boolean {
111
+ return isIP(input) === 6;
112
+ }
113
+
114
+ export default {
115
+ Socket,
116
+ Server,
117
+ createServer,
118
+ createConnection,
119
+ connect,
120
+ isIP,
121
+ isIPv4,
122
+ isIPv6,
123
+ };