@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
@@ -1,331 +0,0 @@
1
- import {
2
- createFuture,
3
- createReadableStream,
4
- createReadableStreamPollState,
5
- createWritableStream,
6
- futureDispose,
7
- futureTakeValue,
8
- pollStateReady,
9
- verifyPollsDroppedForDrop,
10
- } from "./worker-thread.js";
11
- import process from "node:process";
12
- const { TCP, constants: TCPConstants } = process.binding("tcp_wrap");
13
- import {
14
- convertSocketError,
15
- convertSocketErrorCode,
16
- ipSocketAddress,
17
- isIPv4MappedAddress,
18
- isMulticastIpAddress,
19
- isUnicastIpAddress,
20
- isWildcardAddress,
21
- noLookup,
22
- serializeIpAddress,
23
- SOCKET_STATE_BIND,
24
- SOCKET_STATE_BOUND,
25
- SOCKET_STATE_CLOSED,
26
- SOCKET_STATE_CONNECT,
27
- SOCKET_STATE_CONNECTION,
28
- SOCKET_STATE_INIT,
29
- SOCKET_STATE_LISTEN,
30
- SOCKET_STATE_LISTENER,
31
- } from "./worker-sockets.js";
32
- import { Socket, Server } from "node:net";
33
-
34
- const win = process.platform === "win32";
35
-
36
- /**
37
- * @typedef {import("../../types/interfaces/wasi-sockets-network.js").IpSocketAddress} IpSocketAddress
38
- * @typedef {import("../../../types/interfaces/wasi-sockets-tcp.js").IpAddressFamily} IpAddressFamily
39
- * @typedef {import("node:net").Socket} TcpSocket
40
- *
41
- * @typedef {{
42
- * tcpSocket: number,
43
- * err: Error | null,
44
- * pollState: PollState,
45
- * }} PendingAccept
46
- *
47
- * @typedef {{
48
- * state: number,
49
- * future: number | null,
50
- * tcpSocket: TcpSocket | null,
51
- * listenBacklogSize: number,
52
- * handle: TCP,
53
- * pendingAccepts: PendingAccept[],
54
- * pollState: PollState,
55
- * }} TcpSocketRecord
56
- */
57
-
58
- /**
59
- * @type {Map<number, TcpSocketRecord>}
60
- */
61
- export const tcpSockets = new Map();
62
-
63
- let tcpSocketCnt = 0;
64
-
65
- /**
66
- * @param {IpAddressFamily} addressFamily
67
- */
68
- export function createTcpSocket() {
69
- const handle = new TCP(TCPConstants.SOCKET);
70
- tcpSockets.set(++tcpSocketCnt, {
71
- state: SOCKET_STATE_INIT,
72
- future: null,
73
- tcpSocket: null,
74
- listenBacklogSize: 128,
75
- handle,
76
- pendingAccepts: [],
77
- pollState: {
78
- ready: true,
79
- listener: null,
80
- polls: [],
81
- parentStream: null,
82
- },
83
- });
84
- return tcpSocketCnt;
85
- }
86
-
87
- export function socketTcpFinish(id, fromState, toState) {
88
- const socket = tcpSockets.get(id);
89
- if (socket.state !== fromState) {
90
- throw "not-in-progress";
91
- }
92
- if (!socket.pollState.ready) {
93
- throw "would-block";
94
- }
95
- const { tag, val } = futureTakeValue(socket.future).val;
96
- futureDispose(socket.future, false);
97
- socket.future = null;
98
- if (tag === "err") {
99
- socket.state = SOCKET_STATE_CLOSED;
100
- throw val;
101
- } else {
102
- socket.state = toState;
103
- // for the listener, we must immediately transition back to unresolved
104
- if (toState === SOCKET_STATE_LISTENER) {
105
- socket.pollState.ready = false;
106
- }
107
- return val;
108
- }
109
- }
110
-
111
- export function socketTcpBindStart(id, localAddress, family) {
112
- const socket = tcpSockets.get(id);
113
- if (socket.state !== SOCKET_STATE_INIT) {
114
- throw "invalid-state";
115
- }
116
- if (
117
- family !== localAddress.tag ||
118
- !isUnicastIpAddress(localAddress) ||
119
- isIPv4MappedAddress(localAddress)
120
- ) {
121
- throw "invalid-argument";
122
- }
123
- socket.state = SOCKET_STATE_BIND;
124
- const { handle } = socket;
125
- socket.future = createFuture(
126
- (async () => {
127
- const address = serializeIpAddress(localAddress);
128
- const port = localAddress.val.port;
129
- const code =
130
- localAddress.tag === "ipv6"
131
- ? handle.bind6(address, port, TCPConstants.UV_TCP_IPV6ONLY)
132
- : handle.bind(address, port);
133
- if (code !== 0) {
134
- throw convertSocketErrorCode(-code);
135
- }
136
- // This is a Node.js / libuv quirk to force the bind error to be thrown
137
- // (specifically address-in-use).
138
- {
139
- const out = {};
140
- const code = handle.getsockname(out);
141
- if (code !== 0) {
142
- throw convertSocketErrorCode(-code);
143
- }
144
- }
145
- })(),
146
- socket.pollState,
147
- );
148
- }
149
-
150
- export function socketTcpConnectStart(id, remoteAddress, family) {
151
- const socket = tcpSockets.get(id);
152
- if (socket.state !== SOCKET_STATE_INIT && socket.state !== SOCKET_STATE_BOUND) {
153
- throw "invalid-state";
154
- }
155
- if (
156
- isWildcardAddress(remoteAddress) ||
157
- family !== remoteAddress.tag ||
158
- !isUnicastIpAddress(remoteAddress) ||
159
- isMulticastIpAddress(remoteAddress) ||
160
- remoteAddress.val.port === 0 ||
161
- isIPv4MappedAddress(remoteAddress)
162
- ) {
163
- throw "invalid-argument";
164
- }
165
- socket.state = SOCKET_STATE_CONNECT;
166
- socket.future = createFuture(
167
- new Promise((resolve, reject) => {
168
- const tcpSocket = (socket.tcpSocket = new Socket({
169
- handle: socket.handle,
170
- pauseOnCreate: true,
171
- allowHalfOpen: true,
172
- }));
173
- function handleErr(err) {
174
- tcpSocket.off("connect", handleConnect);
175
- reject(convertSocketError(err));
176
- }
177
- function handleConnect() {
178
- tcpSocket.off("error", handleErr);
179
- resolve([createReadableStream(tcpSocket), createWritableStream(tcpSocket)]);
180
- }
181
- tcpSocket.once("connect", handleConnect);
182
- tcpSocket.once("error", handleErr);
183
- tcpSocket.connect({
184
- port: remoteAddress.val.port,
185
- host: serializeIpAddress(remoteAddress),
186
- lookup: noLookup,
187
- });
188
- }),
189
- socket.pollState,
190
- );
191
- }
192
-
193
- export function socketTcpListenStart(id) {
194
- const socket = tcpSockets.get(id);
195
- if (socket.state !== SOCKET_STATE_BOUND) {
196
- throw "invalid-state";
197
- }
198
- const { handle } = socket;
199
- socket.state = SOCKET_STATE_LISTEN;
200
- socket.future = createFuture(
201
- new Promise((resolve, reject) => {
202
- const server = new Server({
203
- pauseOnConnect: true,
204
- allowHalfOpen: true,
205
- });
206
- function handleErr(err) {
207
- server.off("listening", handleListen);
208
- reject(convertSocketError(err));
209
- }
210
- function handleListen() {
211
- server.off("error", handleErr);
212
- server.on("connection", (tcpSocket) => {
213
- pollStateReady(socket.pollState);
214
- const pollState = createReadableStreamPollState(tcpSocket);
215
- socket.pendingAccepts.push({
216
- tcpSocket,
217
- err: null,
218
- pollState,
219
- });
220
- });
221
- server.on("error", (err) => {
222
- pollStateReady(socket.pollState);
223
- socket.pendingAccepts.push({
224
- tcpSocket: null,
225
- err,
226
- pollState: null,
227
- });
228
- });
229
- resolve();
230
- }
231
- server.once("listening", handleListen);
232
- server.once("error", handleErr);
233
- server.listen(handle, socket.listenBacklogSize);
234
- }),
235
- socket.pollState,
236
- );
237
- }
238
-
239
- export function socketTcpAccept(id) {
240
- const socket = tcpSockets.get(id);
241
- if (socket.state !== SOCKET_STATE_LISTENER) {
242
- throw "invalid-state";
243
- }
244
- if (socket.pendingAccepts.length === 0) {
245
- throw "would-block";
246
- }
247
- const accept = socket.pendingAccepts.shift();
248
- if (accept.err) {
249
- socket.state = SOCKET_STATE_CLOSED;
250
- throw convertSocketError(accept.err);
251
- }
252
- if (socket.pendingAccepts.length === 0) {
253
- socket.pollState.ready = false;
254
- }
255
- tcpSockets.set(++tcpSocketCnt, {
256
- state: SOCKET_STATE_CONNECTION,
257
- future: null,
258
- tcpSocket: accept.tcpSocket,
259
- listenBacklogSize: 128,
260
- handle: accept.tcpSocket._handle,
261
- pendingAccepts: [],
262
- pollState: accept.pollState,
263
- });
264
- return [
265
- tcpSocketCnt,
266
- createReadableStream(accept.tcpSocket, accept.pollState),
267
- createWritableStream(accept.tcpSocket),
268
- ];
269
- }
270
-
271
- export function socketTcpSetListenBacklogSize(id, backlogSize) {
272
- const socket = tcpSockets.get(id);
273
- if (socket.state === SOCKET_STATE_LISTEN || socket.state === SOCKET_STATE_LISTENER) {
274
- throw "not-supported";
275
- }
276
- if (
277
- socket.state !== SOCKET_STATE_INIT &&
278
- socket.state !== SOCKET_STATE_BIND &&
279
- socket.state !== SOCKET_STATE_BOUND
280
- ) {
281
- throw "invalid-state";
282
- }
283
- socket.listenBacklogSize = Number(backlogSize);
284
- }
285
-
286
- export function socketTcpGetLocalAddress(id) {
287
- const { handle } = tcpSockets.get(id);
288
- const out = {};
289
- const code = handle.getsockname(out);
290
- if (code !== 0) {
291
- throw convertSocketErrorCode(-code);
292
- }
293
- return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
294
- }
295
-
296
- export function socketTcpGetRemoteAddress(id) {
297
- const { handle } = tcpSockets.get(id);
298
- const out = {};
299
- const code = handle.getpeername(out);
300
- if (code !== 0) {
301
- throw convertSocketErrorCode(-code);
302
- }
303
- return ipSocketAddress(out.family.toLowerCase(), out.address, out.port);
304
- }
305
-
306
- export function socketTcpShutdown(id, _shutdownType) {
307
- const socket = tcpSockets.get(id);
308
- if (socket.state !== SOCKET_STATE_CONNECTION) {
309
- throw "invalid-state";
310
- }
311
- if (win && socket.tcpSocket.destroySoon) {
312
- socket.tcpSocket.destroySoon();
313
- } else {
314
- socket.tcpSocket.destroy();
315
- }
316
- }
317
-
318
- export function socketTcpSetKeepAlive(id, { keepAlive, keepAliveIdleTime }) {
319
- const { handle } = tcpSockets.get(id);
320
- const code = handle.setKeepAlive(keepAlive, Number(keepAliveIdleTime / 1_000_000_000n));
321
- if (code !== 0) {
322
- throw convertSocketErrorCode(-code);
323
- }
324
- }
325
-
326
- export function socketTcpDispose(id) {
327
- const socket = tcpSockets.get(id);
328
- verifyPollsDroppedForDrop(socket.pollState, "tcp socket");
329
- socket.handle.close();
330
- tcpSockets.delete(id);
331
- }