@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,84 @@
1
+ import type { streams as StreamsNamespace } from "../../types/io.js";
2
+ declare const symbolDispose: symbol;
3
+ type IInputStream = StreamsNamespace.InputStream;
4
+ type IOutputStream = StreamsNamespace.OutputStream;
5
+ export declare function registerIncomingHttpHandler(id: any, handler: any): void;
6
+ /**
7
+ * @type {(call: number, id: number | null, payload: any) -> any}
8
+ */
9
+ export declare let ioCall: (...args: any[]) => any;
10
+ export declare function registerDispose(resource: any, parentResource: any, id: number, disposeFn: (id: number) => void): () => void;
11
+ export declare function earlyDispose(finalizer: any): void;
12
+ declare class InputStream implements IInputStream {
13
+ #private;
14
+ [symbolDispose]: () => void;
15
+ read(len: any): any;
16
+ blockingRead(len: any): any;
17
+ skip(len: any): any;
18
+ blockingSkip(len: any): any;
19
+ subscribe(): Pollable;
20
+ static _id(stream: any): any;
21
+ /**
22
+ * @param {FILE | SOCKET_TCP | STDIN | HTTP} streamType
23
+ */
24
+ static _create(streamType: any, id: any): InputStream;
25
+ }
26
+ export declare const inputStreamCreate: typeof InputStream._create;
27
+ export declare const inputStreamId: typeof InputStream._id;
28
+ declare class OutputStream implements IOutputStream {
29
+ #private;
30
+ [symbolDispose]: () => void;
31
+ checkWrite(): any;
32
+ write(buf: any): any;
33
+ blockingWriteAndFlush(buf: any): any;
34
+ flush(): any;
35
+ blockingFlush(): any;
36
+ writeZeroes(len: any): any;
37
+ blockingWriteZeroesAndFlush(len: any): any;
38
+ splice(src: any, len: any): any;
39
+ blockingSplice(src: any, len: any): any;
40
+ subscribe(): Pollable;
41
+ static _id(outputStream: any): any;
42
+ /**
43
+ * @param {OutputStreamType} streamType
44
+ * @param {any} createPayload
45
+ */
46
+ static _create(streamType: any, id: any): OutputStream;
47
+ }
48
+ export declare const outputStreamCreate: typeof OutputStream._create;
49
+ export declare const outputStreamId: typeof OutputStream._id;
50
+ export declare const error: {
51
+ Error: {
52
+ new (payload: any): {
53
+ payload: any;
54
+ toDebugString(): string;
55
+ name: string;
56
+ message: string;
57
+ stack?: string;
58
+ cause?: unknown;
59
+ };
60
+ isError(error: unknown): error is Error;
61
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
62
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
63
+ stackTraceLimit: number;
64
+ };
65
+ };
66
+ export declare const streams: {
67
+ InputStream: typeof InputStream;
68
+ OutputStream: typeof OutputStream;
69
+ };
70
+ declare class Pollable {
71
+ #private;
72
+ [symbolDispose]: () => void;
73
+ ready(): any;
74
+ block(): void;
75
+ static _create(id: any, parent: any): Pollable;
76
+ }
77
+ export declare const pollableCreate: typeof Pollable._create;
78
+ export declare const poll: {
79
+ Pollable: typeof Pollable;
80
+ poll(list: any): any;
81
+ };
82
+ export declare function createPoll(call: any, id: any, initPayload: any): Pollable;
83
+ export declare function createPollLower(call: any, id: any, table: any): (initPayload: any) => number;
84
+ export {};
@@ -0,0 +1,381 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { createSyncFn } from "../synckit/index.js";
3
+ import { CALL_MASK, CALL_TYPE_MASK, FILE, HTTP_SERVER_INCOMING_HANDLER, HTTP, INPUT_STREAM_BLOCKING_READ, INPUT_STREAM_BLOCKING_SKIP, INPUT_STREAM_DISPOSE, INPUT_STREAM_READ, INPUT_STREAM_SKIP, INPUT_STREAM_SUBSCRIBE, OUTPUT_STREAM_BLOCKING_FLUSH, OUTPUT_STREAM_BLOCKING_SPLICE, OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH, OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH, OUTPUT_STREAM_CHECK_WRITE, OUTPUT_STREAM_DISPOSE, OUTPUT_STREAM_FLUSH, OUTPUT_STREAM_SPLICE, OUTPUT_STREAM_SUBSCRIBE, OUTPUT_STREAM_WRITE_ZEROES, OUTPUT_STREAM_WRITE, POLL_POLL_LIST, POLL_POLLABLE_BLOCK, POLL_POLLABLE_DISPOSE, POLL_POLLABLE_READY, SOCKET_TCP, STDERR, STDIN, STDOUT, reverseMap, } from "./calls.js";
4
+ import nodeProcess, { exit, stderr, stdout, env } from "node:process";
5
+ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
6
+ const _rawDebug = "_rawDebug" in nodeProcess
7
+ ? nodeProcess._rawDebug
8
+ : console.error.bind(console);
9
+ const workerPath = fileURLToPath(new URL("./worker-thread.js", import.meta.url));
10
+ const httpIncomingHandlers = new Map();
11
+ export function registerIncomingHttpHandler(id, handler) {
12
+ httpIncomingHandlers.set(id, handler);
13
+ }
14
+ const instanceId = Math.round(Math.random() * 1000).toString();
15
+ const DEBUG_DEFAULT = false;
16
+ const DEBUG = env.PREVIEW2_SHIM_DEBUG === "0"
17
+ ? false
18
+ : env.PREVIEW2_SHIM_DEBUG === "1"
19
+ ? true
20
+ : DEBUG_DEFAULT;
21
+ /**
22
+ * @type {(call: number, id: number | null, payload: any) -> any}
23
+ */
24
+ export let ioCall = createSyncFn(workerPath, DEBUG, (type, id, payload) => {
25
+ // 'callbacks' from the worker
26
+ // ONLY happens for an http server incoming handler, and NOTHING else (not even sockets, since accept is sync!)
27
+ if (type !== HTTP_SERVER_INCOMING_HANDLER) {
28
+ throw new Error("Internal error: only incoming handler callback is permitted");
29
+ }
30
+ const handler = httpIncomingHandlers.get(id);
31
+ if (!handler) {
32
+ throw new Error(`Internal error: no incoming handler registered for server ${id}`);
33
+ }
34
+ handler(payload);
35
+ });
36
+ if (DEBUG) {
37
+ const _ioCall = ioCall;
38
+ ioCall = function ioCall(num, id, payload) {
39
+ if (typeof id !== "number" && id !== null) {
40
+ throw new Error("id must be a number or null");
41
+ }
42
+ let ret;
43
+ try {
44
+ _rawDebug(instanceId, reverseMap[num & CALL_MASK], reverseMap[num & CALL_TYPE_MASK], id, payload);
45
+ ret = _ioCall(num, id, payload);
46
+ return ret;
47
+ }
48
+ catch (e) {
49
+ ret = e;
50
+ throw ret;
51
+ }
52
+ finally {
53
+ _rawDebug(instanceId, "->", ret);
54
+ }
55
+ };
56
+ }
57
+ const finalizationRegistry = new FinalizationRegistry((dispose) => void dispose());
58
+ const dummySymbol = Symbol();
59
+ export function registerDispose(resource, parentResource, id, disposeFn) {
60
+ // While strictly speaking all components should handle their disposal,
61
+ // this acts as a last-resort to catch all missed drops through the JS GC.
62
+ // Mainly for two cases - (1) components which are long lived, that get shut
63
+ // down and (2) users that interface with low-level WASI APIs directly in JS
64
+ // for various reasons may end up leaning on JS GC inadvertantly.
65
+ function finalizer() {
66
+ // This has no functional purpose other than to pin a strong reference
67
+ // from the child resource's finalizer to the parent resource, to ensure
68
+ // that we can never finalize a parent resource before a child resource.
69
+ // This makes the generational JS GC become piecewise over child resource
70
+ // graphs (generational at each resource hierarchy level at least).
71
+ if (parentResource?.[dummySymbol]) {
72
+ return;
73
+ }
74
+ disposeFn(id);
75
+ }
76
+ finalizationRegistry.register(resource, finalizer, finalizer);
77
+ return finalizer;
78
+ }
79
+ export function earlyDispose(finalizer) {
80
+ finalizationRegistry.unregister(finalizer);
81
+ finalizer();
82
+ }
83
+ const _Error = Error;
84
+ const IoError = class Error extends _Error {
85
+ payload;
86
+ constructor(payload) {
87
+ super(payload);
88
+ this.payload = payload;
89
+ }
90
+ toDebugString() {
91
+ return this.message;
92
+ }
93
+ };
94
+ function streamIoErrorCall(call, id, payload) {
95
+ try {
96
+ return ioCall(call, id, payload);
97
+ }
98
+ catch (e) {
99
+ if (e.tag === "closed") {
100
+ throw e;
101
+ }
102
+ if (e.tag === "last-operation-failed") {
103
+ e.val = new IoError(Object.assign(new Error(e.val.message), e.val));
104
+ throw e;
105
+ }
106
+ // any invalid error is a trap
107
+ console.trace(e);
108
+ exit(1);
109
+ }
110
+ }
111
+ class InputStream {
112
+ #id;
113
+ #streamType;
114
+ #finalizer;
115
+ read(len) {
116
+ return streamIoErrorCall(INPUT_STREAM_READ | this.#streamType, this.#id, len);
117
+ }
118
+ blockingRead(len) {
119
+ return streamIoErrorCall(INPUT_STREAM_BLOCKING_READ | this.#streamType, this.#id, len);
120
+ }
121
+ skip(len) {
122
+ return streamIoErrorCall(INPUT_STREAM_SKIP | this.#streamType, this.#id, len);
123
+ }
124
+ blockingSkip(len) {
125
+ return streamIoErrorCall(INPUT_STREAM_BLOCKING_SKIP | this.#streamType, this.#id, len);
126
+ }
127
+ subscribe() {
128
+ return pollableCreate(ioCall(INPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id), this);
129
+ }
130
+ static _id(stream) {
131
+ return stream.#id;
132
+ }
133
+ /**
134
+ * @param {FILE | SOCKET_TCP | STDIN | HTTP} streamType
135
+ */
136
+ static _create(streamType, id) {
137
+ const stream = new InputStream();
138
+ stream.#id = id;
139
+ stream.#streamType = streamType;
140
+ let disposeFn;
141
+ switch (streamType) {
142
+ case FILE:
143
+ disposeFn = fileInputStreamDispose;
144
+ break;
145
+ case SOCKET_TCP:
146
+ disposeFn = socketTcpInputStreamDispose;
147
+ break;
148
+ case STDIN:
149
+ disposeFn = stdinInputStreamDispose;
150
+ break;
151
+ case HTTP:
152
+ disposeFn = httpInputStreamDispose;
153
+ break;
154
+ default:
155
+ throw new Error("wasi-io trap: Dispose function not created for stream type " +
156
+ reverseMap[streamType]);
157
+ }
158
+ stream.#finalizer = registerDispose(stream, null, id, disposeFn);
159
+ return stream;
160
+ }
161
+ [symbolDispose]() {
162
+ if (this.#finalizer) {
163
+ earlyDispose(this.#finalizer);
164
+ this.#finalizer = null;
165
+ }
166
+ }
167
+ }
168
+ function fileInputStreamDispose(id) {
169
+ ioCall(INPUT_STREAM_DISPOSE | FILE, id, null);
170
+ }
171
+ function socketTcpInputStreamDispose(id) {
172
+ ioCall(INPUT_STREAM_DISPOSE | SOCKET_TCP, id, null);
173
+ }
174
+ function stdinInputStreamDispose(id) {
175
+ ioCall(INPUT_STREAM_DISPOSE | STDIN, id, null);
176
+ }
177
+ function httpInputStreamDispose(id) {
178
+ ioCall(INPUT_STREAM_DISPOSE | HTTP, id, null);
179
+ }
180
+ export const inputStreamCreate = InputStream._create;
181
+ // @ts-expect-error - Deleting static method
182
+ delete InputStream._create;
183
+ export const inputStreamId = InputStream._id;
184
+ // @ts-expect-error - Deleting static method
185
+ delete InputStream._id;
186
+ class OutputStream {
187
+ #id;
188
+ #streamType;
189
+ #finalizer;
190
+ checkWrite() {
191
+ return streamIoErrorCall(OUTPUT_STREAM_CHECK_WRITE | this.#streamType, this.#id, null);
192
+ }
193
+ write(buf) {
194
+ if (this.#streamType <= STDERR) {
195
+ return this.blockingWriteAndFlush(buf);
196
+ }
197
+ return streamIoErrorCall(OUTPUT_STREAM_WRITE | this.#streamType, this.#id, buf);
198
+ }
199
+ blockingWriteAndFlush(buf) {
200
+ if (this.#streamType <= STDERR) {
201
+ const stream = this.#streamType === STDERR ? stderr : stdout;
202
+ return void stream.write(buf);
203
+ }
204
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH | this.#streamType, this.#id, buf);
205
+ }
206
+ flush() {
207
+ return streamIoErrorCall(OUTPUT_STREAM_FLUSH | this.#streamType, this.#id, undefined);
208
+ }
209
+ blockingFlush() {
210
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_FLUSH | this.#streamType, this.#id, undefined);
211
+ }
212
+ writeZeroes(len) {
213
+ return streamIoErrorCall(OUTPUT_STREAM_WRITE_ZEROES | this.#streamType, this.#id, len);
214
+ }
215
+ blockingWriteZeroesAndFlush(len) {
216
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH | this.#streamType, this.#id, len);
217
+ }
218
+ splice(src, len) {
219
+ return streamIoErrorCall(OUTPUT_STREAM_SPLICE | this.#streamType, this.#id, {
220
+ src: src.#id,
221
+ len,
222
+ });
223
+ }
224
+ blockingSplice(src, len) {
225
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_SPLICE | this.#streamType, this.#id, {
226
+ src: inputStreamId(src),
227
+ len,
228
+ });
229
+ }
230
+ subscribe() {
231
+ return pollableCreate(ioCall(OUTPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id), undefined);
232
+ }
233
+ static _id(outputStream) {
234
+ return outputStream.#id;
235
+ }
236
+ /**
237
+ * @param {OutputStreamType} streamType
238
+ * @param {any} createPayload
239
+ */
240
+ static _create(streamType, id) {
241
+ const stream = new OutputStream();
242
+ stream.#id = id;
243
+ stream.#streamType = streamType;
244
+ let disposeFn;
245
+ switch (streamType) {
246
+ case STDOUT:
247
+ disposeFn = stdoutOutputStreamDispose;
248
+ break;
249
+ case STDERR:
250
+ disposeFn = stderrOutputStreamDispose;
251
+ break;
252
+ case SOCKET_TCP:
253
+ disposeFn = socketTcpOutputStreamDispose;
254
+ break;
255
+ case FILE:
256
+ disposeFn = fileOutputStreamDispose;
257
+ break;
258
+ case HTTP:
259
+ return stream;
260
+ default:
261
+ throw new Error("wasi-io trap: Dispose function not created for stream type " +
262
+ reverseMap[streamType]);
263
+ }
264
+ stream.#finalizer = registerDispose(stream, null, id, disposeFn);
265
+ return stream;
266
+ }
267
+ [symbolDispose]() {
268
+ if (this.#finalizer) {
269
+ earlyDispose(this.#finalizer);
270
+ this.#finalizer = null;
271
+ }
272
+ }
273
+ }
274
+ function stdoutOutputStreamDispose(id) {
275
+ ioCall(OUTPUT_STREAM_DISPOSE | STDOUT, id);
276
+ }
277
+ function stderrOutputStreamDispose(id) {
278
+ ioCall(OUTPUT_STREAM_DISPOSE | STDERR, id);
279
+ }
280
+ function socketTcpOutputStreamDispose(id) {
281
+ ioCall(OUTPUT_STREAM_DISPOSE | SOCKET_TCP, id);
282
+ }
283
+ function fileOutputStreamDispose(id) {
284
+ ioCall(OUTPUT_STREAM_DISPOSE | FILE, id);
285
+ }
286
+ export const outputStreamCreate = OutputStream._create;
287
+ // @ts-expect-error - Deleting static method
288
+ delete OutputStream._create;
289
+ export const outputStreamId = OutputStream._id;
290
+ // @ts-expect-error - Deleting static method
291
+ delete OutputStream._id;
292
+ export const error = { Error: IoError };
293
+ export const streams = { InputStream, OutputStream };
294
+ function pollableDispose(id) {
295
+ ioCall(POLL_POLLABLE_DISPOSE, id);
296
+ }
297
+ const rep = Symbol.for("cabiRep");
298
+ class Pollable {
299
+ #finalizer;
300
+ ready() {
301
+ return ioCall(POLL_POLLABLE_READY, this[rep]);
302
+ }
303
+ block() {
304
+ ioCall(POLL_POLLABLE_BLOCK, this[rep]);
305
+ }
306
+ static _create(id, parent) {
307
+ const pollable = new Pollable();
308
+ pollable[rep] = id;
309
+ pollable.#finalizer = registerDispose(pollable, parent, id, pollableDispose);
310
+ return pollable;
311
+ }
312
+ [symbolDispose]() {
313
+ if (this.#finalizer && this[rep]) {
314
+ earlyDispose(this.#finalizer);
315
+ this.#finalizer = null;
316
+ }
317
+ }
318
+ }
319
+ const cabiLowerSymbol = Symbol.for("cabiLower");
320
+ const T_FLAG = 1 << 30;
321
+ Pollable.prototype.ready[cabiLowerSymbol] = function ({ resourceTables: [table] }) {
322
+ return function pollableReady(handle) {
323
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
324
+ const ready = ioCall(POLL_POLLABLE_READY, rep);
325
+ return ready ? 1 : 0;
326
+ };
327
+ };
328
+ Pollable.prototype.block[cabiLowerSymbol] = function ({ resourceTables: [table] }) {
329
+ return function pollableBlock(handle) {
330
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
331
+ ioCall(POLL_POLLABLE_BLOCK, rep);
332
+ };
333
+ };
334
+ Pollable[Symbol.for("cabiDispose")] = function pollableDispose(rep) {
335
+ ioCall(POLL_POLLABLE_DISPOSE, rep);
336
+ };
337
+ export const pollableCreate = Pollable._create;
338
+ // @ts-expect-error - Deleting static method
339
+ delete Pollable._create;
340
+ export const poll = {
341
+ Pollable,
342
+ poll(list) {
343
+ return ioCall(POLL_POLL_LIST, null, list.map((pollable) => pollable[rep]));
344
+ },
345
+ };
346
+ poll.poll[cabiLowerSymbol] = function ({ memory, realloc, resourceTables: [table] }) {
347
+ return function pollPollList(listPtr, len, retptr) {
348
+ const handleList = new Uint32Array(memory.buffer, listPtr, len);
349
+ const repList = Array(len);
350
+ for (let i = 0; i < len; i++) {
351
+ const handle = handleList[i];
352
+ repList[i] = table[(handle << 1) + 1] & ~T_FLAG;
353
+ }
354
+ const result = ioCall(POLL_POLL_LIST, null, repList);
355
+ const ptr = realloc(0, 0, 4, result.byteLength);
356
+ const out = new Uint32Array(memory.buffer, ptr, result.length);
357
+ out.set(result);
358
+ const ret = new Uint32Array(memory.buffer, retptr, 2);
359
+ ret[0] = ptr;
360
+ ret[1] = result.length;
361
+ return retptr;
362
+ };
363
+ };
364
+ export function createPoll(call, id, initPayload) {
365
+ return pollableCreate(ioCall(call, id, initPayload), undefined);
366
+ }
367
+ export function createPollLower(call, id, table) {
368
+ return function (initPayload) {
369
+ const rep = ioCall(call, id, initPayload);
370
+ const free = table[0] & ~T_FLAG;
371
+ if (free === 0) {
372
+ table.push(0);
373
+ table.push(rep | T_FLAG);
374
+ return (table.length >> 1) - 1;
375
+ }
376
+ table[0] = table[free << 1];
377
+ table[free << 1] = 0;
378
+ table[(free << 1) + 1] = rep | T_FLAG;
379
+ return free;
380
+ };
381
+ }
@@ -0,0 +1,34 @@
1
+ import { PollState } from "./worker-thread.js";
2
+ import { Socket as TcpSocket } from "node:net";
3
+ import { IpSocketAddress } from "../../types/interfaces/wasi-sockets-network.js";
4
+ interface PendingAccept {
5
+ tcpSocket: TcpSocket | null;
6
+ err: Error | null;
7
+ pollState: PollState | null;
8
+ }
9
+ interface TcpSocketRecord {
10
+ state: number;
11
+ future: number | null;
12
+ tcpSocket: TcpSocket | null;
13
+ listenBacklogSize: number;
14
+ handle: TCP;
15
+ pendingAccepts: PendingAccept[];
16
+ pollState: PollState | null;
17
+ }
18
+ export declare const tcpSockets: Map<number, TcpSocketRecord>;
19
+ export declare function createTcpSocket(): number;
20
+ export declare function socketTcpFinish(id: number, fromState: any, toState: any): any;
21
+ export declare function socketTcpBindStart(id: number, localAddress: any, family: any): void;
22
+ export declare function socketTcpConnectStart(id: number, remoteAddress: IpSocketAddress, family: any): void;
23
+ export declare function socketTcpListenStart(id: number): void;
24
+ export declare function socketTcpAccept(id: number): number[];
25
+ export declare function socketTcpSetListenBacklogSize(id: number, backlogSize: any): void;
26
+ export declare function socketTcpGetLocalAddress(id: number): IpSocketAddress;
27
+ export declare function socketTcpGetRemoteAddress(id: number): IpSocketAddress;
28
+ export declare function socketTcpShutdown(id: number, _shutdownType: any): void;
29
+ export declare function socketTcpSetKeepAlive(id: number, { keepAlive, keepAliveIdleTime }: {
30
+ keepAlive: any;
31
+ keepAliveIdleTime: any;
32
+ }): void;
33
+ export declare function socketTcpDispose(id: number): void;
34
+ export {};