@bytecodealliance/preview3-shim 0.1.0-rc.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 (67) hide show
  1. package/LICENSE +220 -0
  2. package/README.md +14 -0
  3. package/lib/nodejs/cli.js +127 -0
  4. package/lib/nodejs/clocks.js +100 -0
  5. package/lib/nodejs/filesystem/descriptor.js +997 -0
  6. package/lib/nodejs/filesystem/error.js +92 -0
  7. package/lib/nodejs/filesystem/utils.js +19 -0
  8. package/lib/nodejs/filesystem.js +2 -0
  9. package/lib/nodejs/finalization.js +33 -0
  10. package/lib/nodejs/future.js +187 -0
  11. package/lib/nodejs/http/client.js +146 -0
  12. package/lib/nodejs/http/error.js +71 -0
  13. package/lib/nodejs/http/fields.js +359 -0
  14. package/lib/nodejs/http/request.js +589 -0
  15. package/lib/nodejs/http/response.js +212 -0
  16. package/lib/nodejs/http/server.js +157 -0
  17. package/lib/nodejs/http.js +34 -0
  18. package/lib/nodejs/index.js +8 -0
  19. package/lib/nodejs/random.js +14 -0
  20. package/lib/nodejs/sockets/address.js +146 -0
  21. package/lib/nodejs/sockets/error.js +123 -0
  22. package/lib/nodejs/sockets/ip-name-lookup.js +93 -0
  23. package/lib/nodejs/sockets/tcp.js +776 -0
  24. package/lib/nodejs/sockets/udp.js +516 -0
  25. package/lib/nodejs/sockets.js +14 -0
  26. package/lib/nodejs/stream.js +423 -0
  27. package/lib/nodejs/workers/cli-worker.js +49 -0
  28. package/lib/nodejs/workers/filesystem-worker.js +119 -0
  29. package/lib/nodejs/workers/http-worker.js +387 -0
  30. package/lib/nodejs/workers/resource-worker.js +151 -0
  31. package/lib/nodejs/workers/tcp-worker.js +283 -0
  32. package/lib/nodejs/workers/udp-worker.js +194 -0
  33. package/package.json +57 -0
  34. package/types/cli.d.ts +12 -0
  35. package/types/clocks.d.ts +2 -0
  36. package/types/filesystem.d.ts +2 -0
  37. package/types/http.d.ts +4 -0
  38. package/types/index.d.ts +6 -0
  39. package/types/interfaces/wasi-cli-environment.d.ts +21 -0
  40. package/types/interfaces/wasi-cli-exit.d.ts +6 -0
  41. package/types/interfaces/wasi-cli-run.d.ts +5 -0
  42. package/types/interfaces/wasi-cli-stderr.d.ts +14 -0
  43. package/types/interfaces/wasi-cli-stdin.d.ts +19 -0
  44. package/types/interfaces/wasi-cli-stdout.d.ts +14 -0
  45. package/types/interfaces/wasi-cli-terminal-input.d.ts +8 -0
  46. package/types/interfaces/wasi-cli-terminal-output.d.ts +8 -0
  47. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +7 -0
  48. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +7 -0
  49. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +7 -0
  50. package/types/interfaces/wasi-cli-types.d.ts +15 -0
  51. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +33 -0
  52. package/types/interfaces/wasi-clocks-system-clock.d.ts +38 -0
  53. package/types/interfaces/wasi-clocks-types.d.ts +5 -0
  54. package/types/interfaces/wasi-filesystem-preopens.d.ts +6 -0
  55. package/types/interfaces/wasi-filesystem-types.d.ts +780 -0
  56. package/types/interfaces/wasi-http-client.d.ts +9 -0
  57. package/types/interfaces/wasi-http-handler.d.ts +9 -0
  58. package/types/interfaces/wasi-http-types.d.ts +619 -0
  59. package/types/interfaces/wasi-random-insecure-seed.d.ts +21 -0
  60. package/types/interfaces/wasi-random-insecure.d.ts +26 -0
  61. package/types/interfaces/wasi-random-random.d.ts +31 -0
  62. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +77 -0
  63. package/types/interfaces/wasi-sockets-types.d.ts +830 -0
  64. package/types/random.d.ts +3 -0
  65. package/types/sockets.d.ts +2 -0
  66. package/types/wasi-cli-command.d.ts +23 -0
  67. package/types/wasi-http-service.d.ts +14 -0
@@ -0,0 +1,830 @@
1
+ /** @module Interface wasi:sockets/types@0.3.0-rc-2026-03-15 **/
2
+ export type Duration = import('./wasi-clocks-types.js').Duration;
3
+ /**
4
+ * Error codes.
5
+ *
6
+ * In theory, every API can return any error code.
7
+ * In practice, API's typically only return the errors documented per API
8
+ * combined with a couple of errors that are always possible:
9
+ * - `other`
10
+ * - `access-denied`
11
+ * - `not-supported`
12
+ * - `out-of-memory`
13
+ *
14
+ * See each individual API for what the POSIX equivalents are. They sometimes differ per API.
15
+ */
16
+ export type ErrorCode = ErrorCodeAccessDenied | ErrorCodeNotSupported | ErrorCodeInvalidArgument | ErrorCodeOutOfMemory | ErrorCodeTimeout | ErrorCodeInvalidState | ErrorCodeAddressNotBindable | ErrorCodeAddressInUse | ErrorCodeRemoteUnreachable | ErrorCodeConnectionRefused | ErrorCodeConnectionBroken | ErrorCodeConnectionReset | ErrorCodeConnectionAborted | ErrorCodeDatagramTooLarge | ErrorCodeOther;
17
+ /**
18
+ * Access denied.
19
+ *
20
+ * POSIX equivalent: EACCES, EPERM
21
+ */
22
+ export interface ErrorCodeAccessDenied {
23
+ tag: 'access-denied',
24
+ }
25
+ /**
26
+ * The operation is not supported.
27
+ *
28
+ * POSIX equivalent: EOPNOTSUPP, ENOPROTOOPT, EPFNOSUPPORT, EPROTONOSUPPORT, ESOCKTNOSUPPORT
29
+ */
30
+ export interface ErrorCodeNotSupported {
31
+ tag: 'not-supported',
32
+ }
33
+ /**
34
+ * One of the arguments is invalid.
35
+ *
36
+ * POSIX equivalent: EINVAL, EDESTADDRREQ, EAFNOSUPPORT
37
+ */
38
+ export interface ErrorCodeInvalidArgument {
39
+ tag: 'invalid-argument',
40
+ }
41
+ /**
42
+ * Not enough memory to complete the operation.
43
+ *
44
+ * POSIX equivalent: ENOMEM, ENOBUFS
45
+ */
46
+ export interface ErrorCodeOutOfMemory {
47
+ tag: 'out-of-memory',
48
+ }
49
+ /**
50
+ * The operation timed out before it could finish completely.
51
+ *
52
+ * POSIX equivalent: ETIMEDOUT
53
+ */
54
+ export interface ErrorCodeTimeout {
55
+ tag: 'timeout',
56
+ }
57
+ /**
58
+ * The operation is not valid in the socket's current state.
59
+ */
60
+ export interface ErrorCodeInvalidState {
61
+ tag: 'invalid-state',
62
+ }
63
+ /**
64
+ * The local address is not available.
65
+ *
66
+ * POSIX equivalent: EADDRNOTAVAIL
67
+ */
68
+ export interface ErrorCodeAddressNotBindable {
69
+ tag: 'address-not-bindable',
70
+ }
71
+ /**
72
+ * A bind operation failed because the provided address is already in
73
+ * use or because there are no ephemeral ports available.
74
+ *
75
+ * POSIX equivalent: EADDRINUSE
76
+ */
77
+ export interface ErrorCodeAddressInUse {
78
+ tag: 'address-in-use',
79
+ }
80
+ /**
81
+ * The remote address is not reachable.
82
+ *
83
+ * POSIX equivalent: EHOSTUNREACH, EHOSTDOWN, ENETDOWN, ENETUNREACH, ENONET
84
+ */
85
+ export interface ErrorCodeRemoteUnreachable {
86
+ tag: 'remote-unreachable',
87
+ }
88
+ /**
89
+ * The connection was forcefully rejected.
90
+ *
91
+ * POSIX equivalent: ECONNREFUSED
92
+ */
93
+ export interface ErrorCodeConnectionRefused {
94
+ tag: 'connection-refused',
95
+ }
96
+ /**
97
+ * A write failed because the connection was broken.
98
+ *
99
+ * POSIX equivalent: EPIPE
100
+ */
101
+ export interface ErrorCodeConnectionBroken {
102
+ tag: 'connection-broken',
103
+ }
104
+ /**
105
+ * The connection was reset.
106
+ *
107
+ * POSIX equivalent: ECONNRESET
108
+ */
109
+ export interface ErrorCodeConnectionReset {
110
+ tag: 'connection-reset',
111
+ }
112
+ /**
113
+ * The connection was aborted.
114
+ *
115
+ * POSIX equivalent: ECONNABORTED
116
+ */
117
+ export interface ErrorCodeConnectionAborted {
118
+ tag: 'connection-aborted',
119
+ }
120
+ /**
121
+ * The size of a datagram sent to a UDP socket exceeded the maximum
122
+ * supported size.
123
+ *
124
+ * POSIX equivalent: EMSGSIZE
125
+ */
126
+ export interface ErrorCodeDatagramTooLarge {
127
+ tag: 'datagram-too-large',
128
+ }
129
+ /**
130
+ * A catch-all for errors not captured by the existing variants.
131
+ * Implementations can use this to extend the error type without
132
+ * breaking existing code.
133
+ */
134
+ export interface ErrorCodeOther {
135
+ tag: 'other',
136
+ val: string | undefined,
137
+ }
138
+ /**
139
+ * # Variants
140
+ *
141
+ * ## `"ipv4"`
142
+ *
143
+ * Similar to `AF_INET` in POSIX.
144
+ * ## `"ipv6"`
145
+ *
146
+ * Similar to `AF_INET6` in POSIX.
147
+ */
148
+ export type IpAddressFamily = 'ipv4' | 'ipv6';
149
+ export type Ipv4Address = [number, number, number, number];
150
+ export type Ipv6Address = [number, number, number, number, number, number, number, number];
151
+ export type IpAddress = IpAddressIpv4 | IpAddressIpv6;
152
+ export interface IpAddressIpv4 {
153
+ tag: 'ipv4',
154
+ val: Ipv4Address,
155
+ }
156
+ export interface IpAddressIpv6 {
157
+ tag: 'ipv6',
158
+ val: Ipv6Address,
159
+ }
160
+ export interface Ipv4SocketAddress {
161
+ /**
162
+ * sin_port
163
+ */
164
+ port: number,
165
+ /**
166
+ * sin_addr
167
+ */
168
+ address: Ipv4Address,
169
+ }
170
+ export interface Ipv6SocketAddress {
171
+ /**
172
+ * sin6_port
173
+ */
174
+ port: number,
175
+ /**
176
+ * sin6_flowinfo
177
+ */
178
+ flowInfo: number,
179
+ /**
180
+ * sin6_addr
181
+ */
182
+ address: Ipv6Address,
183
+ /**
184
+ * sin6_scope_id
185
+ */
186
+ scopeId: number,
187
+ }
188
+ export type IpSocketAddress = IpSocketAddressIpv4 | IpSocketAddressIpv6;
189
+ export interface IpSocketAddressIpv4 {
190
+ tag: 'ipv4',
191
+ val: Ipv4SocketAddress,
192
+ }
193
+ export interface IpSocketAddressIpv6 {
194
+ tag: 'ipv6',
195
+ val: Ipv6SocketAddress,
196
+ }
197
+ export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };
198
+
199
+ export class TcpSocket {
200
+ /**
201
+ * This type does not have a public constructor.
202
+ */
203
+ private constructor();
204
+ /**
205
+ * Create a new TCP socket.
206
+ *
207
+ * Similar to `socket(AF_INET or AF_INET6, SOCK_STREAM, IPPROTO_TCP)`
208
+ * in POSIX. On IPv6 sockets, IPV6_V6ONLY is enabled by default and
209
+ * can't be configured otherwise.
210
+ *
211
+ * Unlike POSIX, WASI sockets have no notion of a socket-level
212
+ * `O_NONBLOCK` flag. Instead they fully rely on the Component Model's
213
+ * async support.
214
+ *
215
+ * # Typical errors
216
+ * - `not-supported`: The `address-family` is not supported. (EAFNOSUPPORT)
217
+ *
218
+ * # References
219
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>
220
+ * - <https://man7.org/linux/man-pages/man2/socket.2.html>
221
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketw>
222
+ * - <https://man.freebsd.org/cgi/man.cgi?query=socket&sektion=2>
223
+ */
224
+ static create(addressFamily: IpAddressFamily): TcpSocket;
225
+ /**
226
+ * Bind the socket to the provided IP address and port.
227
+ *
228
+ * If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is
229
+ * left to the implementation to decide which network interface(s) to
230
+ * bind to. If the TCP/UDP port is zero, the socket will be bound to a
231
+ * random free port.
232
+ *
233
+ * Bind can be attempted multiple times on the same socket, even with
234
+ * different arguments on each iteration. But never concurrently and
235
+ * only as long as the previous bind failed. Once a bind succeeds, the
236
+ * binding can't be changed anymore.
237
+ *
238
+ * # Typical errors
239
+ * - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
240
+ * - `invalid-argument`: `local-address` is not a unicast address. (EINVAL)
241
+ * - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. (EINVAL)
242
+ * - `invalid-state`: The socket is already bound. (EINVAL)
243
+ * - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
244
+ * - `address-in-use`: Address is already in use. (EADDRINUSE)
245
+ * - `address-not-bindable`: `local-address` is not an address that can be bound to. (EADDRNOTAVAIL)
246
+ *
247
+ * # Implementors note
248
+ * The bind operation shouldn't be affected by the TIME_WAIT state of a
249
+ * recently closed socket on the same local address. In practice this
250
+ * means that the SO_REUSEADDR socket option should be set implicitly
251
+ * on all platforms, except on Windows where this is the default
252
+ * behavior and SO_REUSEADDR performs something different.
253
+ *
254
+ * # References
255
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
256
+ * - <https://man7.org/linux/man-pages/man2/bind.2.html>
257
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
258
+ * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
259
+ */
260
+ bind(localAddress: IpSocketAddress): void;
261
+ /**
262
+ * Connect to a remote endpoint.
263
+ *
264
+ * On success, the socket is transitioned into the `connected` state
265
+ * and the `remote-address` of the socket is updated.
266
+ * The `local-address` may be updated as well, based on the best network
267
+ * path to `remote-address`. If the socket was not already explicitly
268
+ * bound, this function will implicitly bind the socket to a random
269
+ * free port.
270
+ *
271
+ * After a failed connection attempt, the socket will be in the `closed`
272
+ * state and the only valid action left is to `drop` the socket. A single
273
+ * socket can not be used to connect more than once.
274
+ *
275
+ * # Typical errors
276
+ * - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
277
+ * - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)
278
+ * - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)
279
+ * - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
280
+ * - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
281
+ * - `invalid-state`: The socket is already in the `connecting` state. (EALREADY)
282
+ * - `invalid-state`: The socket is already in the `connected` state. (EISCONN)
283
+ * - `invalid-state`: The socket is already in the `listening` state. (EOPNOTSUPP, EINVAL on Windows)
284
+ * - `timeout`: Connection timed out. (ETIMEDOUT)
285
+ * - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED)
286
+ * - `connection-reset`: The connection was reset. (ECONNRESET)
287
+ * - `connection-aborted`: The connection was aborted. (ECONNABORTED)
288
+ * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
289
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
290
+ *
291
+ * # References
292
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
293
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
294
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
295
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
296
+ */
297
+ connect(remoteAddress: IpSocketAddress): Promise<void>;
298
+ /**
299
+ * Start listening and return a stream of new inbound connections.
300
+ *
301
+ * Transitions the socket into the `listening` state. This can be called
302
+ * at most once per socket.
303
+ *
304
+ * If the socket is not already explicitly bound, this function will
305
+ * implicitly bind the socket to a random free port.
306
+ *
307
+ * Normally, the returned sockets are bound, in the `connected` state
308
+ * and immediately ready for I/O. Though, depending on exact timing and
309
+ * circumstances, a newly accepted connection may already be `closed`
310
+ * by the time the server attempts to perform its first I/O on it. This
311
+ * is true regardless of whether the WASI implementation uses
312
+ * "synthesized" sockets or not (see Implementors Notes below).
313
+ *
314
+ * The following properties are inherited from the listener socket:
315
+ * - `address-family`
316
+ * - `keep-alive-enabled`
317
+ * - `keep-alive-idle-time`
318
+ * - `keep-alive-interval`
319
+ * - `keep-alive-count`
320
+ * - `hop-limit`
321
+ * - `receive-buffer-size`
322
+ * - `send-buffer-size`
323
+ *
324
+ * # Typical errors
325
+ * - `invalid-state`: The socket is already in the `connected` state. (EISCONN, EINVAL on BSD)
326
+ * - `invalid-state`: The socket is already in the `listening` state.
327
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
328
+ *
329
+ * # Implementors note
330
+ * This method returns a single perpetual stream that should only close
331
+ * on fatal errors (if any). Yet, the POSIX' `accept` function may also
332
+ * return transient errors (e.g. ECONNABORTED). The exact details differ
333
+ * per operation system. For example, the Linux manual mentions:
334
+ *
335
+ * > Linux accept() passes already-pending network errors on the new
336
+ * > socket as an error code from accept(). This behavior differs from
337
+ * > other BSD socket implementations. For reliable operation the
338
+ * > application should detect the network errors defined for the
339
+ * > protocol after accept() and treat them like EAGAIN by retrying.
340
+ * > In the case of TCP/IP, these are ENETDOWN, EPROTO, ENOPROTOOPT,
341
+ * > EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
342
+ * Source: https://man7.org/linux/man-pages/man2/accept.2.html
343
+ *
344
+ * WASI implementations have two options to handle this:
345
+ * - Optionally log it and then skip over non-fatal errors returned by
346
+ * `accept`. Guest code never gets to see these failures. Or:
347
+ * - Synthesize a `tcp-socket` resource that exposes the error when
348
+ * attempting to send or receive on it. Guest code then sees these
349
+ * failures as regular I/O errors.
350
+ *
351
+ * In either case, the stream returned by this `listen` method remains
352
+ * operational.
353
+ *
354
+ * WASI requires `listen` to perform an implicit bind if the socket
355
+ * has not already been bound. Not all platforms (notably Windows)
356
+ * exhibit this behavior out of the box. On platforms that require it,
357
+ * the WASI implementation can emulate this behavior by performing
358
+ * the bind itself if the guest hasn't already done so.
359
+ *
360
+ * # References
361
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
362
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
363
+ * - <https://man7.org/linux/man-pages/man2/listen.2.html>
364
+ * - <https://man7.org/linux/man-pages/man2/accept.2.html>
365
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
366
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
367
+ * - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
368
+ * - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
369
+ */
370
+ listen(): ReadableStream<TcpSocket>;
371
+ /**
372
+ * Transmit data to peer.
373
+ *
374
+ * The caller should close the stream when it has no more data to send
375
+ * to the peer. Under normal circumstances this will cause a FIN packet
376
+ * to be sent out. Closing the stream is equivalent to calling
377
+ * `shutdown(SHUT_WR)` in POSIX.
378
+ *
379
+ * This function may be called at most once and returns once the full
380
+ * contents of the stream are transmitted or an error is encountered.
381
+ *
382
+ * # Typical errors
383
+ * - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
384
+ * - `invalid-state`: `send` has already been called on this socket.
385
+ * - `connection-broken`: The connection is not writable anymore. (EPIPE, ECONNABORTED on Windows)
386
+ * - `connection-reset`: The connection was reset. (ECONNRESET)
387
+ * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
388
+ *
389
+ * # References
390
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html>
391
+ * - <https://man7.org/linux/man-pages/man2/send.2.html>
392
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send>
393
+ * - <https://man.freebsd.org/cgi/man.cgi?query=send&sektion=2>
394
+ */
395
+ send(data: ReadableStream<number>): Promise<Result<void, ErrorCode>>;
396
+ /**
397
+ * Read data from peer.
398
+ *
399
+ * Returns a `stream` of data sent by the peer. The implementation
400
+ * drops the stream once no more data is available. At that point, the
401
+ * returned `future` resolves to:
402
+ * - `ok` after a graceful shutdown from the peer (i.e. a FIN packet), or
403
+ * - `err` if the socket was closed abnormally.
404
+ *
405
+ * `receive` may be called only once per socket. Subsequent calls return
406
+ * a closed stream and a future resolved to `err(invalid-state)`.
407
+ *
408
+ * If the caller is not expecting to receive any more data from the peer,
409
+ * they should drop the stream. Any data still in the receive queue
410
+ * will be discarded. This is equivalent to calling `shutdown(SHUT_RD)`
411
+ * in POSIX.
412
+ *
413
+ * # Typical errors
414
+ * - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
415
+ * - `invalid-state`: `receive` has already been called on this socket.
416
+ * - `connection-reset`: The connection was reset. (ECONNRESET)
417
+ * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
418
+ *
419
+ * # References
420
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html>
421
+ * - <https://man7.org/linux/man-pages/man2/recv.2.html>
422
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv>
423
+ * - <https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2>
424
+ */
425
+ receive(): [ReadableStream<number>, Promise<Result<void, ErrorCode>>];
426
+ /**
427
+ * Get the bound local address.
428
+ *
429
+ * POSIX mentions:
430
+ * > If the socket has not been bound to a local name, the value
431
+ * > stored in the object pointed to by `address` is unspecified.
432
+ *
433
+ * WASI is stricter and requires `get-local-address` to return
434
+ * `invalid-state` when the socket hasn't been bound yet.
435
+ *
436
+ * # Typical errors
437
+ * - `invalid-state`: The socket is not bound to any local address.
438
+ *
439
+ * # References
440
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
441
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
442
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
443
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
444
+ */
445
+ getLocalAddress(): IpSocketAddress;
446
+ /**
447
+ * Get the remote address.
448
+ *
449
+ * # Typical errors
450
+ * - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN)
451
+ *
452
+ * # References
453
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
454
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
455
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
456
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
457
+ */
458
+ getRemoteAddress(): IpSocketAddress;
459
+ /**
460
+ * Whether the socket is in the `listening` state.
461
+ *
462
+ * Equivalent to the SO_ACCEPTCONN socket option.
463
+ */
464
+ getIsListening(): boolean;
465
+ /**
466
+ * Whether this is a IPv4 or IPv6 socket.
467
+ *
468
+ * This is the value passed to the constructor.
469
+ *
470
+ * Equivalent to the SO_DOMAIN socket option.
471
+ */
472
+ getAddressFamily(): IpAddressFamily;
473
+ /**
474
+ * Hints the desired listen queue size. Implementations are free to
475
+ * ignore this.
476
+ *
477
+ * If the provided value is 0, an `invalid-argument` error is returned.
478
+ * Any other value will never cause an error, but it might be silently
479
+ * clamped and/or rounded.
480
+ *
481
+ * # Typical errors
482
+ * - `not-supported`: (set) The platform does not support changing the backlog size after the initial listen.
483
+ * - `invalid-argument`: (set) The provided value was 0.
484
+ * - `invalid-state`: (set) The socket is in the `connecting` or `connected` state.
485
+ */
486
+ setListenBacklogSize(value: bigint): void;
487
+ /**
488
+ * Enables or disables keepalive.
489
+ *
490
+ * The keepalive behavior can be adjusted using:
491
+ * - `keep-alive-idle-time`
492
+ * - `keep-alive-interval`
493
+ * - `keep-alive-count`
494
+ * These properties can be configured while `keep-alive-enabled` is
495
+ * false, but only come into effect when `keep-alive-enabled` is true.
496
+ *
497
+ * Equivalent to the SO_KEEPALIVE socket option.
498
+ */
499
+ getKeepAliveEnabled(): boolean;
500
+ setKeepAliveEnabled(value: boolean): void;
501
+ /**
502
+ * Amount of time the connection has to be idle before TCP starts
503
+ * sending keepalive packets.
504
+ *
505
+ * If the provided value is 0, an `invalid-argument` error is returned.
506
+ * All other values are accepted without error, but may be
507
+ * clamped or rounded. As a result, the value read back from
508
+ * this setting may differ from the value that was set.
509
+ *
510
+ * Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS)
511
+ *
512
+ * # Typical errors
513
+ * - `invalid-argument`: (set) The provided value was 0.
514
+ */
515
+ getKeepAliveIdleTime(): Duration;
516
+ setKeepAliveIdleTime(value: Duration): void;
517
+ /**
518
+ * The time between keepalive packets.
519
+ *
520
+ * If the provided value is 0, an `invalid-argument` error is returned.
521
+ * All other values are accepted without error, but may be
522
+ * clamped or rounded. As a result, the value read back from
523
+ * this setting may differ from the value that was set.
524
+ *
525
+ * Equivalent to the TCP_KEEPINTVL socket option.
526
+ *
527
+ * # Typical errors
528
+ * - `invalid-argument`: (set) The provided value was 0.
529
+ */
530
+ getKeepAliveInterval(): Duration;
531
+ setKeepAliveInterval(value: Duration): void;
532
+ /**
533
+ * The maximum amount of keepalive packets TCP should send before
534
+ * aborting the connection.
535
+ *
536
+ * If the provided value is 0, an `invalid-argument` error is returned.
537
+ * All other values are accepted without error, but may be
538
+ * clamped or rounded. As a result, the value read back from
539
+ * this setting may differ from the value that was set.
540
+ *
541
+ * Equivalent to the TCP_KEEPCNT socket option.
542
+ *
543
+ * # Typical errors
544
+ * - `invalid-argument`: (set) The provided value was 0.
545
+ */
546
+ getKeepAliveCount(): number;
547
+ setKeepAliveCount(value: number): void;
548
+ /**
549
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
550
+ *
551
+ * If the provided value is 0, an `invalid-argument` error is returned.
552
+ *
553
+ * # Typical errors
554
+ * - `invalid-argument`: (set) The TTL value must be 1 or higher.
555
+ */
556
+ getHopLimit(): number;
557
+ setHopLimit(value: number): void;
558
+ /**
559
+ * Kernel buffer space reserved for sending/receiving on this socket.
560
+ * Implementations usually treat this as a cap the buffer can grow to,
561
+ * rather than allocating the full amount immediately.
562
+ *
563
+ * If the provided value is 0, an `invalid-argument` error is returned.
564
+ * All other values are accepted without error, but may be
565
+ * clamped or rounded. As a result, the value read back from
566
+ * this setting may differ from the value that was set.
567
+ *
568
+ * This is only a performance hint. The implementation may ignore it or
569
+ * tweak it based on real traffic patterns.
570
+ * Linux and macOS appear to behave differently depending on whether a
571
+ * buffer size was explicitly set. When set, they tend to honor it; when
572
+ * not set, they dynamically adjust the buffer size as the connection
573
+ * progresses. This is especially noticeable when comparing the values
574
+ * from before and after connection establishment.
575
+ *
576
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
577
+ *
578
+ * # Typical errors
579
+ * - `invalid-argument`: (set) The provided value was 0.
580
+ */
581
+ getReceiveBufferSize(): bigint;
582
+ setReceiveBufferSize(value: bigint): void;
583
+ getSendBufferSize(): bigint;
584
+ setSendBufferSize(value: bigint): void;
585
+ }
586
+
587
+ export class UdpSocket {
588
+ /**
589
+ * This type does not have a public constructor.
590
+ */
591
+ private constructor();
592
+ /**
593
+ * Create a new UDP socket.
594
+ *
595
+ * Similar to `socket(AF_INET or AF_INET6, SOCK_DGRAM, IPPROTO_UDP)`
596
+ * in POSIX. On IPv6 sockets, IPV6_V6ONLY is enabled by default and
597
+ * can't be configured otherwise.
598
+ *
599
+ * Unlike POSIX, WASI sockets have no notion of a socket-level
600
+ * `O_NONBLOCK` flag. Instead they fully rely on the Component Model's
601
+ * async support.
602
+ *
603
+ * # References:
604
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>
605
+ * - <https://man7.org/linux/man-pages/man2/socket.2.html>
606
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketw>
607
+ * - <https://man.freebsd.org/cgi/man.cgi?query=socket&sektion=2>
608
+ */
609
+ static create(addressFamily: IpAddressFamily): UdpSocket;
610
+ /**
611
+ * Bind the socket to the provided IP address and port.
612
+ *
613
+ * If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is
614
+ * left to the implementation to decide which network interface(s) to
615
+ * bind to. If the port is zero, the socket will be bound to a random
616
+ * free port.
617
+ *
618
+ * # Typical errors
619
+ * - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
620
+ * - `invalid-state`: The socket is already bound. (EINVAL)
621
+ * - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
622
+ * - `address-in-use`: Address is already in use. (EADDRINUSE)
623
+ * - `address-not-bindable`: `local-address` is not an address that can be bound to. (EADDRNOTAVAIL)
624
+ *
625
+ * # References
626
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
627
+ * - <https://man7.org/linux/man-pages/man2/bind.2.html>
628
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
629
+ * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
630
+ */
631
+ bind(localAddress: IpSocketAddress): void;
632
+ /**
633
+ * Associate this socket with a specific peer address.
634
+ *
635
+ * On success, the `remote-address` of the socket is updated.
636
+ * The `local-address` may be updated as well, based on the best network
637
+ * path to `remote-address`. If the socket was not already explicitly
638
+ * bound, this function will implicitly bind the socket to a random
639
+ * free port.
640
+ *
641
+ * When a UDP socket is "connected", the `send` and `receive` methods
642
+ * are limited to communicating with that peer only:
643
+ * - `send` can only be used to send to this destination.
644
+ * - `receive` will only return datagrams sent from the provided `remote-address`.
645
+ *
646
+ * The name "connect" was kept to align with the existing POSIX
647
+ * terminology. Other than that, this function only changes the local
648
+ * socket configuration and does not generate any network traffic.
649
+ * The peer is not aware of this "connection".
650
+ *
651
+ * This method may be called multiple times on the same socket to change
652
+ * its association, but only the most recent one will be effective.
653
+ *
654
+ * # Typical errors
655
+ * - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
656
+ * - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
657
+ * - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
658
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
659
+ *
660
+ * # Implementors note
661
+ * If the socket is already connected, some platforms (e.g. Linux)
662
+ * require a disconnect before connecting to a different peer address.
663
+ *
664
+ * # References
665
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
666
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
667
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
668
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
669
+ */
670
+ connect(remoteAddress: IpSocketAddress): void;
671
+ /**
672
+ * Dissociate this socket from its peer address.
673
+ *
674
+ * After calling this method, `send` & `receive` are free to communicate
675
+ * with any remote address again.
676
+ *
677
+ * The POSIX equivalent of this is calling `connect` with an `AF_UNSPEC` address.
678
+ *
679
+ * # Typical errors
680
+ * - `invalid-state`: The socket is not connected.
681
+ *
682
+ * # References
683
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
684
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
685
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
686
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
687
+ */
688
+ disconnect(): void;
689
+ /**
690
+ * Send a message on the socket to a particular peer.
691
+ *
692
+ * If the socket is connected, the peer address may be left empty. In
693
+ * that case this is equivalent to `send` in POSIX. Otherwise it is
694
+ * equivalent to `sendto`.
695
+ *
696
+ * Additionally, if the socket is connected, a `remote-address` argument
697
+ * _may_ be provided but then it must be identical to the address
698
+ * passed to `connect`.
699
+ *
700
+ * If the socket has not been explicitly bound, it will be
701
+ * implicitly bound to a random free port.
702
+ *
703
+ * Implementations may trap if the `data` length exceeds 64 KiB.
704
+ *
705
+ * # Typical errors
706
+ * - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
707
+ * - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
708
+ * - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
709
+ * - `invalid-argument`: The socket is in "connected" mode and `remote-address` is `some` value that does not match the address passed to `connect`. (EISCONN)
710
+ * - `invalid-argument`: The socket is not "connected" and no value for `remote-address` was provided. (EDESTADDRREQ)
711
+ * - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
712
+ * - `connection-refused`: The connection was refused. (ECONNREFUSED)
713
+ * - `datagram-too-large`: The datagram is too large. (EMSGSIZE)
714
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
715
+ *
716
+ * # Implementors note
717
+ * WASI requires `send` to perform an implicit bind if the socket
718
+ * has not been bound. Not all platforms (notably Windows) exhibit
719
+ * this behavior natively. On such platforms, the WASI implementation
720
+ * should emulate it by performing the bind if the guest has not
721
+ * already done so.
722
+ *
723
+ * # References
724
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html>
725
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html>
726
+ * - <https://man7.org/linux/man-pages/man2/send.2.html>
727
+ * - <https://man7.org/linux/man-pages/man2/sendmmsg.2.html>
728
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send>
729
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-sendto>
730
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasendmsg>
731
+ * - <https://man.freebsd.org/cgi/man.cgi?query=send&sektion=2>
732
+ */
733
+ send(data: Uint8Array, remoteAddress: IpSocketAddress | undefined): Promise<void>;
734
+ /**
735
+ * Receive a message on the socket.
736
+ *
737
+ * On success, the return value contains a tuple of the received data
738
+ * and the address of the sender. Theoretical maximum length of the
739
+ * data is 64 KiB. Though in practice, it will typically be less than
740
+ * 1500 bytes.
741
+ *
742
+ * If the socket is connected, the sender address is guaranteed to
743
+ * match the remote address passed to `connect`.
744
+ *
745
+ * # Typical errors
746
+ * - `invalid-state`: The socket has not been bound yet.
747
+ * - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
748
+ * - `connection-refused`: The connection was refused. (ECONNREFUSED)
749
+ *
750
+ * # References
751
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html>
752
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html>
753
+ * - <https://man7.org/linux/man-pages/man2/recv.2.html>
754
+ * - <https://man7.org/linux/man-pages/man2/recvmmsg.2.html>
755
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom>
756
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nc-mswsock-lpfn_wsarecvmsg>
757
+ * - <https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2>
758
+ */
759
+ receive(): Promise<[Uint8Array, IpSocketAddress]>;
760
+ /**
761
+ * Get the current bound address.
762
+ *
763
+ * POSIX mentions:
764
+ * > If the socket has not been bound to a local name, the value
765
+ * > stored in the object pointed to by `address` is unspecified.
766
+ *
767
+ * WASI is stricter and requires `get-local-address` to return
768
+ * `invalid-state` when the socket hasn't been bound yet.
769
+ *
770
+ * # Typical errors
771
+ * - `invalid-state`: The socket is not bound to any local address.
772
+ *
773
+ * # References
774
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
775
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
776
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
777
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
778
+ */
779
+ getLocalAddress(): IpSocketAddress;
780
+ /**
781
+ * Get the address the socket is currently "connected" to.
782
+ *
783
+ * # Typical errors
784
+ * - `invalid-state`: The socket is not "connected" to a specific remote address. (ENOTCONN)
785
+ *
786
+ * # References
787
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
788
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
789
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
790
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
791
+ */
792
+ getRemoteAddress(): IpSocketAddress;
793
+ /**
794
+ * Whether this is a IPv4 or IPv6 socket.
795
+ *
796
+ * This is the value passed to the constructor.
797
+ *
798
+ * Equivalent to the SO_DOMAIN socket option.
799
+ */
800
+ getAddressFamily(): IpAddressFamily;
801
+ /**
802
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
803
+ *
804
+ * If the provided value is 0, an `invalid-argument` error is returned.
805
+ *
806
+ * # Typical errors
807
+ * - `invalid-argument`: (set) The TTL value must be 1 or higher.
808
+ */
809
+ getUnicastHopLimit(): number;
810
+ setUnicastHopLimit(value: number): void;
811
+ /**
812
+ * Kernel buffer space reserved for sending/receiving on this socket.
813
+ * Implementations usually treat this as a cap the buffer can grow to,
814
+ * rather than allocating the full amount immediately.
815
+ *
816
+ * If the provided value is 0, an `invalid-argument` error is returned.
817
+ * All other values are accepted without error, but may be
818
+ * clamped or rounded. As a result, the value read back from
819
+ * this setting may differ from the value that was set.
820
+ *
821
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
822
+ *
823
+ * # Typical errors
824
+ * - `invalid-argument`: (set) The provided value was 0.
825
+ */
826
+ getReceiveBufferSize(): bigint;
827
+ setReceiveBufferSize(value: bigint): void;
828
+ getSendBufferSize(): bigint;
829
+ setSendBufferSize(value: bigint): void;
830
+ }