@bytecodealliance/preview2-shim 0.18.1 → 0.20.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 (82) hide show
  1. package/README.md +15 -15
  2. package/dist/browser/cli.d.ts +16 -0
  3. package/dist/browser/cli.js +109 -0
  4. package/dist/browser/clocks.d.ts +3 -0
  5. package/dist/browser/clocks.js +36 -0
  6. package/dist/browser/config.d.ts +2 -0
  7. package/{lib → dist}/browser/config.js +2 -4
  8. package/dist/browser/environment.d.ts +5 -0
  9. package/dist/browser/environment.js +24 -0
  10. package/dist/browser/filesystem.d.ts +132 -0
  11. package/dist/browser/filesystem.js +488 -0
  12. package/dist/browser/http.d.ts +4 -0
  13. package/dist/browser/http.js +643 -0
  14. package/dist/browser/index.d.ts +7 -0
  15. package/dist/browser/index.js +7 -0
  16. package/dist/browser/io.d.ts +58 -0
  17. package/dist/browser/io.js +205 -0
  18. package/dist/browser/random.d.ts +4 -0
  19. package/dist/browser/random.js +49 -0
  20. package/dist/browser/sockets.d.ts +8 -0
  21. package/dist/browser/sockets.js +66 -0
  22. package/dist/common/instantiation.d.ts +97 -0
  23. package/dist/common/instantiation.js +228 -0
  24. package/dist/io/calls.d.ts +91 -0
  25. package/{lib → dist}/io/calls.js +4 -19
  26. package/dist/io/worker-http.d.ts +17 -0
  27. package/dist/io/worker-http.js +192 -0
  28. package/dist/io/worker-io.d.ts +84 -0
  29. package/dist/io/worker-io.js +381 -0
  30. package/dist/io/worker-socket-tcp.d.ts +34 -0
  31. package/dist/io/worker-socket-tcp.js +251 -0
  32. package/dist/io/worker-socket-udp.d.ts +54 -0
  33. package/dist/io/worker-socket-udp.js +493 -0
  34. package/dist/io/worker-sockets.d.ts +32 -0
  35. package/dist/io/worker-sockets.js +265 -0
  36. package/dist/io/worker-thread.d.ts +73 -0
  37. package/dist/io/worker-thread.js +794 -0
  38. package/dist/nodejs/cli.d.ts +30 -0
  39. package/dist/nodejs/cli.js +86 -0
  40. package/dist/nodejs/clocks.d.ts +3 -0
  41. package/dist/nodejs/clocks.js +61 -0
  42. package/dist/nodejs/filesystem.d.ts +113 -0
  43. package/dist/nodejs/filesystem.js +761 -0
  44. package/dist/nodejs/http.d.ts +82 -0
  45. package/dist/nodejs/http.js +674 -0
  46. package/dist/nodejs/index.d.ts +7 -0
  47. package/dist/nodejs/index.js +7 -0
  48. package/dist/nodejs/io.d.ts +1 -0
  49. package/dist/nodejs/io.js +1 -0
  50. package/dist/nodejs/random.d.ts +4 -0
  51. package/dist/nodejs/random.js +42 -0
  52. package/dist/nodejs/sockets.d.ts +21 -0
  53. package/dist/nodejs/sockets.js +482 -0
  54. package/dist/synckit/index.d.ts +2 -0
  55. package/dist/synckit/index.js +117 -0
  56. package/package.json +28 -15
  57. package/types/instantiation.d.ts +7 -7
  58. package/lib/browser/cli.js +0 -135
  59. package/lib/browser/clocks.js +0 -39
  60. package/lib/browser/environment.js +0 -29
  61. package/lib/browser/filesystem.js +0 -503
  62. package/lib/browser/http.js +0 -707
  63. package/lib/browser/index.js +0 -9
  64. package/lib/browser/io.js +0 -262
  65. package/lib/browser/random.js +0 -56
  66. package/lib/browser/sockets.js +0 -88
  67. package/lib/common/instantiation.js +0 -253
  68. package/lib/io/worker-http.js +0 -206
  69. package/lib/io/worker-io.js +0 -468
  70. package/lib/io/worker-socket-tcp.js +0 -331
  71. package/lib/io/worker-socket-udp.js +0 -587
  72. package/lib/io/worker-sockets.js +0 -364
  73. package/lib/io/worker-thread.js +0 -1051
  74. package/lib/nodejs/cli.js +0 -109
  75. package/lib/nodejs/clocks.js +0 -67
  76. package/lib/nodejs/filesystem.js +0 -829
  77. package/lib/nodejs/http.js +0 -764
  78. package/lib/nodejs/index.js +0 -9
  79. package/lib/nodejs/io.js +0 -1
  80. package/lib/nodejs/random.js +0 -49
  81. package/lib/nodejs/sockets.js +0 -565
  82. package/lib/synckit/index.js +0 -127
@@ -0,0 +1,674 @@
1
+ import { FUTURE_DISPOSE, FUTURE_SUBSCRIBE, FUTURE_TAKE_VALUE, HTTP_CREATE_REQUEST, HTTP_OUTGOING_BODY_DISPOSE, HTTP_OUTPUT_STREAM_FINISH, HTTP_SERVER_CLEAR_OUTGOING_RESPONSE, HTTP_SERVER_SET_OUTGOING_RESPONSE, HTTP_SERVER_START, HTTP_SERVER_STOP, HTTP_SERVER_GET_ADDRESS, OUTPUT_STREAM_CREATE, OUTPUT_STREAM_DISPOSE, } from "../io/calls.js";
2
+ import { earlyDispose, inputStreamCreate, ioCall, outputStreamCreate, pollableCreate, createPoll, registerDispose, registerIncomingHttpHandler, } from "../io/worker-io.js";
3
+ import { CLOCKS_DURATION_SUBSCRIBE, HTTP } from "../io/calls.js";
4
+ import * as http from "node:http";
5
+ const { validateHeaderName = () => { }, validateHeaderValue = () => { } } = http;
6
+ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
7
+ export const _forbiddenHeaders = new Set(["connection", "keep-alive", "host"]);
8
+ class IncomingBody {
9
+ #finished = false;
10
+ #stream = null;
11
+ stream() {
12
+ if (!this.#stream) {
13
+ throw undefined;
14
+ }
15
+ const stream = this.#stream;
16
+ this.#stream = null;
17
+ return stream;
18
+ }
19
+ static finish(incomingBody) {
20
+ if (incomingBody.#finished) {
21
+ throw new Error("incoming body already finished");
22
+ }
23
+ incomingBody.#finished = true;
24
+ return futureTrailersCreate();
25
+ }
26
+ [symbolDispose]() { }
27
+ static _create(streamId) {
28
+ const incomingBody = new IncomingBody();
29
+ incomingBody.#stream = inputStreamCreate(HTTP, streamId);
30
+ return incomingBody;
31
+ }
32
+ }
33
+ const incomingBodyCreate = IncomingBody._create;
34
+ // @ts-expect-error - Deleting static method
35
+ delete IncomingBody._create;
36
+ class IncomingRequest {
37
+ #method;
38
+ #pathWithQuery;
39
+ #scheme;
40
+ #authority;
41
+ #headers;
42
+ #streamId;
43
+ method() {
44
+ return this.#method;
45
+ }
46
+ pathWithQuery() {
47
+ return this.#pathWithQuery;
48
+ }
49
+ scheme() {
50
+ return this.#scheme;
51
+ }
52
+ authority() {
53
+ return this.#authority;
54
+ }
55
+ headers() {
56
+ return this.#headers;
57
+ }
58
+ consume() {
59
+ return incomingBodyCreate(this.#streamId);
60
+ }
61
+ [symbolDispose]() { }
62
+ static _create(method, pathWithQuery, scheme, authority, headers, streamId) {
63
+ const incomingRequest = new IncomingRequest();
64
+ incomingRequest.#method = method;
65
+ incomingRequest.#pathWithQuery = pathWithQuery;
66
+ incomingRequest.#scheme = scheme;
67
+ incomingRequest.#authority = authority;
68
+ incomingRequest.#headers = headers;
69
+ incomingRequest.#streamId = streamId;
70
+ return incomingRequest;
71
+ }
72
+ }
73
+ const incomingRequestCreate = IncomingRequest._create;
74
+ // @ts-expect-error - Deleting static method
75
+ delete IncomingRequest._create;
76
+ class FutureTrailers {
77
+ #requested = false;
78
+ subscribe() {
79
+ return createPoll(CLOCKS_DURATION_SUBSCRIBE, null, 0n);
80
+ }
81
+ get() {
82
+ if (this.#requested) {
83
+ return { tag: "err", val: undefined };
84
+ }
85
+ this.#requested = true;
86
+ return {
87
+ tag: "ok",
88
+ val: {
89
+ tag: "ok",
90
+ val: fieldsLock(fieldsFromEntriesChecked([])),
91
+ },
92
+ };
93
+ }
94
+ static _create() {
95
+ const res = new FutureTrailers();
96
+ return res;
97
+ }
98
+ }
99
+ const futureTrailersCreate = FutureTrailers._create;
100
+ // @ts-expect-error - Deleting static method
101
+ delete FutureTrailers._create;
102
+ class OutgoingResponse {
103
+ #body;
104
+ /** @type {number} */ #statusCode = 200;
105
+ /** @type {Fields} */ #headers;
106
+ /**
107
+ * @param {number} statusCode
108
+ * @param {Fields} headers
109
+ */
110
+ constructor(headers) {
111
+ fieldsLock(headers);
112
+ this.#headers = headers;
113
+ }
114
+ statusCode() {
115
+ return this.#statusCode;
116
+ }
117
+ setStatusCode(statusCode) {
118
+ this.#statusCode = statusCode;
119
+ }
120
+ headers() {
121
+ return this.#headers;
122
+ }
123
+ body() {
124
+ let contentLengthValues = this.#headers.get("content-length");
125
+ let contentLength;
126
+ if (contentLengthValues.length > 0) {
127
+ contentLength = Number(new TextDecoder().decode(contentLengthValues[0]));
128
+ }
129
+ this.#body = outgoingBodyCreate(contentLength);
130
+ return this.#body;
131
+ }
132
+ static _body(outgoingResponse) {
133
+ return outgoingResponse.#body;
134
+ }
135
+ }
136
+ const outgoingResponseBody = OutgoingResponse._body;
137
+ // @ts-expect-error - Deleting static method
138
+ delete OutgoingResponse._body;
139
+ class ResponseOutparam {
140
+ #setListener;
141
+ static set(param, response) {
142
+ param.#setListener(response);
143
+ }
144
+ static _create(setListener) {
145
+ const responseOutparam = new ResponseOutparam();
146
+ responseOutparam.#setListener = setListener;
147
+ return responseOutparam;
148
+ }
149
+ }
150
+ const responseOutparamCreate = ResponseOutparam._create;
151
+ // @ts-expect-error - Deleting static method
152
+ delete ResponseOutparam._create;
153
+ const defaultHttpTimeout = 600000000000n;
154
+ class RequestOptions {
155
+ #connectTimeout = defaultHttpTimeout;
156
+ #firstByteTimeout = defaultHttpTimeout;
157
+ #betweenBytesTimeout = defaultHttpTimeout;
158
+ connectTimeout() {
159
+ return this.#connectTimeout;
160
+ }
161
+ setConnectTimeout(duration) {
162
+ this.#connectTimeout = duration;
163
+ }
164
+ firstByteTimeout() {
165
+ return this.#firstByteTimeout;
166
+ }
167
+ setFirstByteTimeout(duration) {
168
+ this.#firstByteTimeout = duration;
169
+ }
170
+ betweenBytesTimeout() {
171
+ return this.#betweenBytesTimeout;
172
+ }
173
+ setBetweenBytesTimeout(duration) {
174
+ this.#betweenBytesTimeout = duration;
175
+ }
176
+ }
177
+ class OutgoingRequest {
178
+ #method = { tag: "get" };
179
+ #scheme = undefined;
180
+ #pathWithQuery = undefined;
181
+ #authority = undefined;
182
+ #headers;
183
+ #body;
184
+ #bodyRequested = false;
185
+ constructor(headers) {
186
+ fieldsLock(headers);
187
+ this.#headers = headers;
188
+ let contentLengthValues = this.#headers.get("content-length");
189
+ if (contentLengthValues.length === 0) {
190
+ contentLengthValues = this.#headers.get("Content-Length");
191
+ }
192
+ let contentLength;
193
+ if (contentLengthValues.length > 0) {
194
+ contentLength = Number(new TextDecoder().decode(contentLengthValues[0]));
195
+ }
196
+ this.#body = outgoingBodyCreate(contentLength);
197
+ }
198
+ body() {
199
+ if (this.#bodyRequested) {
200
+ throw new Error("Body already requested");
201
+ }
202
+ this.#bodyRequested = true;
203
+ return this.#body;
204
+ }
205
+ method() {
206
+ return this.#method;
207
+ }
208
+ setMethod(method) {
209
+ if (method.tag === "other" && !method.val.match(/^[a-zA-Z-]+$/)) {
210
+ throw undefined;
211
+ }
212
+ this.#method = method;
213
+ }
214
+ pathWithQuery() {
215
+ return this.#pathWithQuery;
216
+ }
217
+ setPathWithQuery(pathWithQuery) {
218
+ if (pathWithQuery && !pathWithQuery.match(/^[a-zA-Z0-9.\-_~!$&'()*+,;=:@%?/]+$/)) {
219
+ throw undefined;
220
+ }
221
+ this.#pathWithQuery = pathWithQuery;
222
+ }
223
+ scheme() {
224
+ return this.#scheme;
225
+ }
226
+ setScheme(scheme) {
227
+ if (scheme?.tag === "other" && !scheme.val.match(/^[a-zA-Z]+$/)) {
228
+ throw undefined;
229
+ }
230
+ this.#scheme = scheme;
231
+ }
232
+ authority() {
233
+ return this.#authority;
234
+ }
235
+ setAuthority(authority) {
236
+ if (authority) {
237
+ const [host, port, ...extra] = authority.split(":");
238
+ const portNum = Number(port);
239
+ if (extra.length ||
240
+ (port !== undefined && (portNum.toString() !== port || portNum > 65535)) ||
241
+ !host.match(/^[a-zA-Z0-9-.]+$/)) {
242
+ throw undefined;
243
+ }
244
+ }
245
+ this.#authority = authority;
246
+ }
247
+ headers() {
248
+ return this.#headers;
249
+ }
250
+ [symbolDispose]() { }
251
+ static _handle(request, options) {
252
+ const connectTimeout = options?.connectTimeout();
253
+ const betweenBytesTimeout = options?.betweenBytesTimeout();
254
+ const firstByteTimeout = options?.firstByteTimeout();
255
+ const scheme = schemeString(request.#scheme);
256
+ // note: host header is automatically added by Node.js
257
+ const headers = [];
258
+ const decoder = new TextDecoder();
259
+ for (const [key, value] of request.#headers.entries()) {
260
+ headers.push([key, decoder.decode(value)]);
261
+ }
262
+ if (!request.#pathWithQuery) {
263
+ throw { tag: "HTTP-request-URI-invalid" };
264
+ }
265
+ return futureIncomingResponseCreate(request.#method.val || request.#method.tag, scheme, request.#authority, request.#pathWithQuery, headers, outgoingBodyOutputStreamId(request.#body), connectTimeout, betweenBytesTimeout, firstByteTimeout);
266
+ }
267
+ }
268
+ const outgoingRequestHandle = OutgoingRequest._handle;
269
+ // @ts-expect-error - Deleting static method
270
+ delete OutgoingRequest._handle;
271
+ class OutgoingBody {
272
+ #outputStream = null;
273
+ #outputStreamId = null;
274
+ #contentLength = undefined;
275
+ #finalizer;
276
+ write() {
277
+ // can only call write once
278
+ const outputStream = this.#outputStream;
279
+ if (outputStream === null) {
280
+ throw undefined;
281
+ }
282
+ this.#outputStream = null;
283
+ return outputStream;
284
+ }
285
+ static finish(body, trailers) {
286
+ if (trailers) {
287
+ throw { tag: "internal-error", val: "trailers unsupported" };
288
+ }
289
+ // this will verify content length, and also verify not already finished
290
+ // throwing errors as appropriate
291
+ ioCall(HTTP_OUTPUT_STREAM_FINISH, body.#outputStreamId, null);
292
+ }
293
+ static _outputStreamId(outgoingBody) {
294
+ return outgoingBody.#outputStreamId;
295
+ }
296
+ static _create(contentLength) {
297
+ const outgoingBody = new OutgoingBody();
298
+ outgoingBody.#contentLength = contentLength;
299
+ outgoingBody.#outputStreamId = ioCall(OUTPUT_STREAM_CREATE | HTTP, null, outgoingBody.#contentLength);
300
+ outgoingBody.#outputStream = outputStreamCreate(HTTP, outgoingBody.#outputStreamId);
301
+ outgoingBody.#finalizer = registerDispose(outgoingBody, null, outgoingBody.#outputStreamId, outgoingBodyDispose);
302
+ return outgoingBody;
303
+ }
304
+ [symbolDispose]() {
305
+ if (this.#finalizer) {
306
+ earlyDispose(this.#finalizer);
307
+ this.#finalizer = null;
308
+ }
309
+ }
310
+ }
311
+ function outgoingBodyDispose(id) {
312
+ ioCall(HTTP_OUTGOING_BODY_DISPOSE, id, null);
313
+ }
314
+ const outgoingBodyOutputStreamId = OutgoingBody._outputStreamId;
315
+ // @ts-expect-error - Deleting static method
316
+ delete OutgoingBody._outputStreamId;
317
+ const outgoingBodyCreate = OutgoingBody._create;
318
+ // @ts-expect-error - Deleting static method
319
+ delete OutgoingBody._create;
320
+ class IncomingResponse {
321
+ #headers;
322
+ #status = 0;
323
+ #bodyStream;
324
+ status() {
325
+ return this.#status;
326
+ }
327
+ headers() {
328
+ return this.#headers;
329
+ }
330
+ consume() {
331
+ if (this.#bodyStream === undefined) {
332
+ throw undefined;
333
+ }
334
+ const bodyStream = this.#bodyStream;
335
+ this.#bodyStream = undefined;
336
+ return bodyStream;
337
+ }
338
+ [symbolDispose]() {
339
+ if (this.#bodyStream) {
340
+ this.#bodyStream[symbolDispose]();
341
+ }
342
+ }
343
+ static _create(status, headers, bodyStreamId) {
344
+ const res = new IncomingResponse();
345
+ res.#status = status;
346
+ res.#headers = headers;
347
+ res.#bodyStream = incomingBodyCreate(bodyStreamId);
348
+ return res;
349
+ }
350
+ }
351
+ const incomingResponseCreate = IncomingResponse._create;
352
+ // @ts-expect-error - Deleting static method
353
+ delete IncomingResponse._create;
354
+ class FutureIncomingResponse {
355
+ #id;
356
+ #finalizer;
357
+ subscribe() {
358
+ return pollableCreate(ioCall(FUTURE_SUBSCRIBE | HTTP, this.#id, null), this);
359
+ }
360
+ get() {
361
+ const ret = ioCall(FUTURE_TAKE_VALUE | HTTP, this.#id, null);
362
+ if (ret === undefined) {
363
+ return undefined;
364
+ }
365
+ if (ret.tag === "ok" && ret.val.tag === "ok") {
366
+ const textEncoder = new TextEncoder();
367
+ const { status, headers, bodyStreamId } = ret.val.val;
368
+ ret.val.val = incomingResponseCreate(status, fieldsFromEntriesChecked(headers.map(([key, val]) => [key, textEncoder.encode(val)])), bodyStreamId);
369
+ }
370
+ return ret;
371
+ }
372
+ static _create(method, scheme, authority, pathWithQuery, headers, body, connectTimeout, betweenBytesTimeout, firstByteTimeout) {
373
+ const res = new FutureIncomingResponse();
374
+ res.#id = ioCall(HTTP_CREATE_REQUEST, null, {
375
+ method,
376
+ scheme,
377
+ authority,
378
+ pathWithQuery,
379
+ headers,
380
+ body,
381
+ connectTimeout,
382
+ betweenBytesTimeout,
383
+ firstByteTimeout,
384
+ });
385
+ res.#finalizer = registerDispose(res, null, res.#id, futureIncomingResponseDispose);
386
+ return res;
387
+ }
388
+ [symbolDispose]() {
389
+ if (this.#finalizer) {
390
+ earlyDispose(this.#finalizer);
391
+ this.#finalizer = null;
392
+ }
393
+ }
394
+ }
395
+ function futureIncomingResponseDispose(id) {
396
+ ioCall(FUTURE_DISPOSE | HTTP, id, null);
397
+ }
398
+ const futureIncomingResponseCreate = FutureIncomingResponse._create;
399
+ // @ts-expect-error - Deleting static method
400
+ delete FutureIncomingResponse._create;
401
+ class Fields {
402
+ #immutable = false;
403
+ #entries = [];
404
+ #table = new Map();
405
+ /**
406
+ * @param {[string, Uint8Array[][]][]} entries
407
+ */
408
+ static fromList(entries) {
409
+ const fields = new Fields();
410
+ for (const [key, value] of entries) {
411
+ fields.append(key, value);
412
+ }
413
+ return fields;
414
+ }
415
+ get(name) {
416
+ const tableEntries = this.#table.get(name.toLowerCase());
417
+ if (!tableEntries) {
418
+ return [];
419
+ }
420
+ return tableEntries.map(([, v]) => v);
421
+ }
422
+ /**
423
+ * WIT spec (https://github.com/WebAssembly/WASI/blob/91bb44c3c3a9b1e09187db23c85fa844d1fd6b15/proposals/http/wit/types.wit#L215-L223):
424
+ *
425
+ * > Set all of the values for a name. Clears any existing values for that
426
+ * > name, if they have been set.
427
+ * >
428
+ * > Fails with `header-error.immutable` if the `fields` are immutable.
429
+ * >
430
+ * > Fails with `header-error.invalid-syntax` if the `field-name` or any of
431
+ * > the `field-value`s are syntactically invalid.
432
+ *
433
+ * The existing-branch splice/reuse is an allocation optimization — values are cleared, not retained.
434
+ */
435
+ set(name, values) {
436
+ if (this.#immutable) {
437
+ throw { tag: "immutable" };
438
+ }
439
+ try {
440
+ validateHeaderName(name);
441
+ }
442
+ catch {
443
+ throw { tag: "invalid-syntax" };
444
+ }
445
+ for (const value of values) {
446
+ try {
447
+ validateHeaderValue(name, new TextDecoder().decode(value));
448
+ }
449
+ catch {
450
+ throw { tag: "invalid-syntax" };
451
+ }
452
+ }
453
+ const lowercased = name.toLowerCase();
454
+ if (_forbiddenHeaders.has(lowercased)) {
455
+ throw { tag: "forbidden" };
456
+ }
457
+ let tableEntries = this.#table.get(lowercased);
458
+ if (tableEntries) {
459
+ this.#entries = this.#entries.filter((entry) => !tableEntries?.includes(entry));
460
+ tableEntries.splice(0, tableEntries.length);
461
+ }
462
+ else {
463
+ this.#table.set(lowercased, []);
464
+ tableEntries = this.#table.get(lowercased);
465
+ }
466
+ for (const value of values) {
467
+ const entry = [name, value];
468
+ this.#entries.push(entry);
469
+ tableEntries?.push(entry);
470
+ }
471
+ }
472
+ has(name) {
473
+ return this.#table.has(name.toLowerCase());
474
+ }
475
+ delete(name) {
476
+ if (this.#immutable) {
477
+ throw { tag: "immutable" };
478
+ }
479
+ const lowercased = name.toLowerCase();
480
+ const tableEntries = this.#table.get(lowercased);
481
+ if (tableEntries) {
482
+ this.#entries = this.#entries.filter((entry) => !tableEntries.includes(entry));
483
+ this.#table.delete(lowercased);
484
+ }
485
+ }
486
+ append(name, value) {
487
+ if (this.#immutable) {
488
+ throw { tag: "immutable" };
489
+ }
490
+ try {
491
+ validateHeaderName(name);
492
+ }
493
+ catch {
494
+ throw { tag: "invalid-syntax" };
495
+ }
496
+ try {
497
+ validateHeaderValue(name, new TextDecoder().decode(value));
498
+ }
499
+ catch {
500
+ throw { tag: "invalid-syntax" };
501
+ }
502
+ const lowercased = name.toLowerCase();
503
+ if (_forbiddenHeaders.has(lowercased)) {
504
+ throw { tag: "forbidden" };
505
+ }
506
+ const entry = [name, value];
507
+ this.#entries.push(entry);
508
+ const tableEntries = this.#table.get(lowercased);
509
+ if (tableEntries) {
510
+ tableEntries.push(entry);
511
+ }
512
+ else {
513
+ this.#table.set(lowercased, [entry]);
514
+ }
515
+ }
516
+ entries() {
517
+ return this.#entries;
518
+ }
519
+ clone() {
520
+ return fieldsFromEntriesChecked(this.#entries);
521
+ }
522
+ static _lock(fields) {
523
+ fields.#immutable = true;
524
+ return fields;
525
+ }
526
+ // assumes entries are already validated
527
+ static _fromEntriesChecked(entries) {
528
+ const fields = new Fields();
529
+ fields.#entries = entries;
530
+ for (const entry of entries) {
531
+ const lowercase = entry[0].toLowerCase();
532
+ const existing = fields.#table.get(lowercase);
533
+ if (existing) {
534
+ existing.push(entry);
535
+ }
536
+ else {
537
+ fields.#table.set(lowercase, [entry]);
538
+ }
539
+ }
540
+ return fields;
541
+ }
542
+ }
543
+ const fieldsLock = Fields._lock;
544
+ // @ts-expect-error - Deleting static method
545
+ delete Fields._lock;
546
+ const fieldsFromEntriesChecked = Fields._fromEntriesChecked;
547
+ // @ts-expect-error - Deleting static method
548
+ delete Fields._fromEntriesChecked;
549
+ export const outgoingHandler = {
550
+ /**
551
+ * @param {OutgoingRequest} request
552
+ * @param {RequestOptions | undefined} options
553
+ * @returns {FutureIncomingResponse}
554
+ */
555
+ handle: outgoingRequestHandle,
556
+ };
557
+ function httpErrorCode(err) {
558
+ if (err.payload) {
559
+ return err.payload;
560
+ }
561
+ return {
562
+ tag: "internal-error",
563
+ val: err.message,
564
+ };
565
+ }
566
+ export const types = {
567
+ Fields,
568
+ FutureIncomingResponse,
569
+ FutureTrailers,
570
+ IncomingBody,
571
+ IncomingRequest,
572
+ IncomingResponse,
573
+ OutgoingBody,
574
+ OutgoingRequest,
575
+ OutgoingResponse,
576
+ ResponseOutparam,
577
+ RequestOptions,
578
+ httpErrorCode,
579
+ };
580
+ function schemeString(scheme) {
581
+ if (!scheme) {
582
+ return "https:";
583
+ }
584
+ switch (scheme.tag) {
585
+ case "HTTP":
586
+ return "http:";
587
+ case "HTTPS":
588
+ return "https:";
589
+ case "other":
590
+ return scheme.val.toLowerCase() + ":";
591
+ }
592
+ }
593
+ const supportedMethods = [
594
+ "get",
595
+ "head",
596
+ "post",
597
+ "put",
598
+ "delete",
599
+ "connect",
600
+ "options",
601
+ "trace",
602
+ "patch",
603
+ ];
604
+ function parseMethod(method) {
605
+ const lowercase = method.toLowerCase();
606
+ if (supportedMethods.includes(method.toLowerCase())) {
607
+ return { tag: lowercase };
608
+ }
609
+ return { tag: "other", val: lowercase };
610
+ }
611
+ const httpServers = new Map();
612
+ let httpServerCnt = 0;
613
+ export class HTTPServer {
614
+ #id = ++httpServerCnt;
615
+ #stopped = false;
616
+ #liveEventLoopInterval;
617
+ constructor(incomingHandler) {
618
+ httpServers.set(this.#id, this);
619
+ if (typeof incomingHandler?.handle !== "function") {
620
+ console.error("Not a valid HTTP server component to execute.");
621
+ process.exit(1);
622
+ }
623
+ registerIncomingHttpHandler(this.#id, ({ method, pathWithQuery, host, headers, responseId, streamId }) => {
624
+ const textEncoder = new TextEncoder();
625
+ const request = incomingRequestCreate(parseMethod(method), pathWithQuery, { tag: "HTTP" }, host, fieldsLock(fieldsFromEntriesChecked(headers
626
+ .filter(([key]) => !_forbiddenHeaders.has(key))
627
+ .map(([key, val]) => [key, textEncoder.encode(val)]))), streamId);
628
+ let outgoingBodyStreamId;
629
+ const responseOutparam = responseOutparamCreate((response) => {
630
+ if (response.tag === "ok") {
631
+ const outgoingResponse = response.val;
632
+ const statusCode = outgoingResponse.statusCode();
633
+ const headers = outgoingResponse.headers().entries();
634
+ const body = outgoingResponseBody(outgoingResponse);
635
+ outgoingBodyStreamId = outgoingBodyOutputStreamId(body);
636
+ ioCall(HTTP_SERVER_SET_OUTGOING_RESPONSE, responseId, {
637
+ statusCode,
638
+ headers,
639
+ streamId: outgoingBodyStreamId,
640
+ });
641
+ }
642
+ else {
643
+ ioCall(HTTP_SERVER_CLEAR_OUTGOING_RESPONSE, responseId, null);
644
+ console.error(response.val);
645
+ process.exit(1);
646
+ }
647
+ });
648
+ incomingHandler.handle(request, responseOutparam);
649
+ if (outgoingBodyStreamId) {
650
+ ioCall(OUTPUT_STREAM_DISPOSE, outgoingBodyStreamId, null);
651
+ }
652
+ });
653
+ }
654
+ listen(port, host) {
655
+ // set a dummy interval, to keep the process alive since the server is off-thread
656
+ this.#liveEventLoopInterval = setInterval(() => { }, 10_000);
657
+ ioCall(HTTP_SERVER_START, this.#id, { port, host });
658
+ }
659
+ address() {
660
+ return ioCall(HTTP_SERVER_GET_ADDRESS, this.#id);
661
+ }
662
+ stop() {
663
+ if (this.#stopped) {
664
+ return;
665
+ }
666
+ clearInterval(this.#liveEventLoopInterval);
667
+ ioCall(HTTP_SERVER_STOP, this.#id, null);
668
+ httpServers.delete(this.#id);
669
+ this.#stopped = true;
670
+ }
671
+ close() {
672
+ this.stop();
673
+ }
674
+ }
@@ -0,0 +1,7 @@
1
+ export * as cli from "./cli.js";
2
+ export * as clocks from "./clocks.js";
3
+ export * as filesystem from "./filesystem.js";
4
+ export * as http from "./http.js";
5
+ export * as io from "./io.js";
6
+ export * as random from "./random.js";
7
+ export * as sockets from "./sockets.js";
@@ -0,0 +1,7 @@
1
+ export * as cli from "./cli.js";
2
+ export * as clocks from "./clocks.js";
3
+ export * as filesystem from "./filesystem.js";
4
+ export * as http from "./http.js";
5
+ export * as io from "./io.js";
6
+ export * as random from "./random.js";
7
+ export * as sockets from "./sockets.js";
@@ -0,0 +1 @@
1
+ export { error, streams, poll, inputStreamCreate, outputStreamCreate } from "../io/worker-io.js";
@@ -0,0 +1 @@
1
+ export { error, streams, poll, inputStreamCreate, outputStreamCreate } from "../io/worker-io.js";
@@ -0,0 +1,4 @@
1
+ import type { insecure as InsecureNamespace, insecureSeed as InsecureSeedNamespace, random as RandomNamespace } from "../../types/random.js";
2
+ export declare const insecure: typeof InsecureNamespace;
3
+ export declare const insecureSeed: typeof InsecureSeedNamespace;
4
+ export declare const random: typeof RandomNamespace;