@bytecodealliance/preview2-shim 0.0.13 → 0.0.15

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 (44) hide show
  1. package/README.md +2 -2
  2. package/lib/browser/cli.js +21 -6
  3. package/lib/browser/filesystem.js +204 -20
  4. package/lib/browser/http.js +4 -7
  5. package/lib/browser/io.js +89 -19
  6. package/lib/browser/logging.js +2 -2
  7. package/lib/browser/poll.js +48 -4
  8. package/lib/http/error.js +1 -1
  9. package/lib/http/make-request.js +4 -3
  10. package/lib/http/wasi-http.js +88 -53
  11. package/lib/nodejs/cli.js +17 -7
  12. package/lib/nodejs/http.js +4 -7
  13. package/lib/nodejs/io.js +16 -5
  14. package/lib/nodejs/logging.js +2 -2
  15. package/package.json +4 -1
  16. package/types/interfaces/wasi-cli-environment.d.ts +22 -0
  17. package/types/interfaces/wasi-cli-exit.d.ts +7 -0
  18. package/types/interfaces/wasi-cli-run.d.ts +6 -0
  19. package/types/interfaces/wasi-cli-stderr.d.ts +5 -0
  20. package/types/interfaces/wasi-cli-stdin.d.ts +5 -0
  21. package/types/interfaces/wasi-cli-stdout.d.ts +5 -0
  22. package/types/interfaces/wasi-cli-terminal-input.d.ts +13 -0
  23. package/types/interfaces/wasi-cli-terminal-output.d.ts +13 -0
  24. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +9 -0
  25. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +9 -0
  26. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +9 -0
  27. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +24 -0
  28. package/types/interfaces/wasi-clocks-timezone.d.ts +71 -0
  29. package/types/interfaces/wasi-clocks-wall-clock.d.ts +31 -0
  30. package/types/interfaces/wasi-filesystem-preopens.d.ts +8 -0
  31. package/types/interfaces/wasi-filesystem-types.d.ts +843 -0
  32. package/types/interfaces/wasi-io-streams.d.ts +274 -0
  33. package/types/interfaces/wasi-poll-poll.d.ts +39 -0
  34. package/types/interfaces/wasi-random-insecure-seed.d.ts +22 -0
  35. package/types/interfaces/wasi-random-insecure.d.ts +20 -0
  36. package/types/interfaces/wasi-random-random.d.ts +22 -0
  37. package/types/interfaces/wasi-sockets-instance-network.d.ts +8 -0
  38. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +76 -0
  39. package/types/interfaces/wasi-sockets-network.d.ts +180 -0
  40. package/types/interfaces/wasi-sockets-tcp-create-socket.d.ts +33 -0
  41. package/types/interfaces/wasi-sockets-tcp.d.ts +298 -0
  42. package/types/interfaces/wasi-sockets-udp-create-socket.d.ts +33 -0
  43. package/types/interfaces/wasi-sockets-udp.d.ts +228 -0
  44. package/types/wasi-cli-command.d.ts +29 -0
@@ -0,0 +1,298 @@
1
+ export namespace WasiSocketsTcp {
2
+ /**
3
+ * Bind the socket to a specific network on the provided IP address and port.
4
+ *
5
+ * If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the implementation to decide which
6
+ * network interface(s) to bind to.
7
+ * If the TCP/UDP port is zero, the socket will be bound to a random free port.
8
+ *
9
+ * When a socket is not explicitly bound, the first invocation to a listen or connect operation will
10
+ * implicitly bind the socket.
11
+ *
12
+ * Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
13
+ *
14
+ * # Typical `start` errors
15
+ * - `address-family-mismatch`: The `local-address` has the wrong address family. (EINVAL)
16
+ * - `already-bound`: The socket is already bound. (EINVAL)
17
+ * - `concurrency-conflict`: Another `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
18
+ *
19
+ * # Typical `finish` errors
20
+ * - `ephemeral-ports-exhausted`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
21
+ * - `address-in-use`: Address is already in use. (EADDRINUSE)
22
+ * - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
23
+ * - `not-in-progress`: A `bind` operation is not in progress.
24
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
25
+ *
26
+ * # References
27
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
28
+ * - <https://man7.org/linux/man-pages/man2/bind.2.html>
29
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
30
+ * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
31
+ */
32
+ export function startBind(this_: TcpSocket, network: Network, localAddress: IpSocketAddress): void;
33
+ export function finishBind(this_: TcpSocket): void;
34
+ /**
35
+ * Connect to a remote endpoint.
36
+ *
37
+ * On success:
38
+ * - the socket is transitioned into the Connection state
39
+ * - a pair of streams is returned that can be used to read & write to the connection
40
+ *
41
+ * # Typical `start` errors
42
+ * - `address-family-mismatch`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
43
+ * - `invalid-remote-address`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
44
+ * - `invalid-remote-address`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
45
+ * - `already-attached`: The socket is already attached to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
46
+ * - `already-connected`: The socket is already in the Connection state. (EISCONN)
47
+ * - `already-listening`: The socket is already in the Listener state. (EOPNOTSUPP, EINVAL on Windows)
48
+ * - `concurrency-conflict`: Another `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
49
+ *
50
+ * # Typical `finish` errors
51
+ * - `timeout`: Connection timed out. (ETIMEDOUT)
52
+ * - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED)
53
+ * - `connection-reset`: The connection was reset. (ECONNRESET)
54
+ * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN)
55
+ * - `ephemeral-ports-exhausted`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
56
+ * - `not-in-progress`: A `connect` operation is not in progress.
57
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
58
+ *
59
+ * # References
60
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
61
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
62
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
63
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
64
+ */
65
+ export function startConnect(this_: TcpSocket, network: Network, remoteAddress: IpSocketAddress): void;
66
+ /**
67
+ * Note: the returned `input-stream` and `output-stream` are child
68
+ * resources of the `tcp-socket`. Implementations may trap if the
69
+ * `tcp-socket` is dropped before both of these streams are dropped.
70
+ */
71
+ export function finishConnect(this_: TcpSocket): [InputStream, OutputStream];
72
+ /**
73
+ * Start listening for new connections.
74
+ *
75
+ * Transitions the socket into the Listener state.
76
+ *
77
+ * Unlike POSIX:
78
+ * - this function is async. This enables interactive WASI hosts to inject permission prompts.
79
+ * - the socket must already be explicitly bound.
80
+ *
81
+ * # Typical `start` errors
82
+ * - `not-bound`: The socket is not bound to any local address. (EDESTADDRREQ)
83
+ * - `already-connected`: The socket is already in the Connection state. (EISCONN, EINVAL on BSD)
84
+ * - `already-listening`: The socket is already in the Listener state.
85
+ * - `concurrency-conflict`: Another `bind`, `connect` or `listen` operation is already in progress. (EINVAL on BSD)
86
+ *
87
+ * # Typical `finish` errors
88
+ * - `ephemeral-ports-exhausted`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
89
+ * - `not-in-progress`: A `listen` operation is not in progress.
90
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
91
+ *
92
+ * # References
93
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
94
+ * - <https://man7.org/linux/man-pages/man2/listen.2.html>
95
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
96
+ * - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
97
+ */
98
+ export function startListen(this_: TcpSocket): void;
99
+ export function finishListen(this_: TcpSocket): void;
100
+ /**
101
+ * Accept a new client socket.
102
+ *
103
+ * The returned socket is bound and in the Connection state.
104
+ *
105
+ * On success, this function returns the newly accepted client socket along with
106
+ * a pair of streams that can be used to read & write to the connection.
107
+ *
108
+ * Note: the returned `input-stream` and `output-stream` are child
109
+ * resources of the returned `tcp-socket`. Implementations may trap if the
110
+ * `tcp-socket` is dropped before its child streams are dropped.
111
+ *
112
+ * # Typical errors
113
+ * - `not-listening`: Socket is not in the Listener state. (EINVAL)
114
+ * - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
115
+ *
116
+ * Host implementations must skip over transient errors returned by the native accept syscall.
117
+ *
118
+ * # References
119
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
120
+ * - <https://man7.org/linux/man-pages/man2/accept.2.html>
121
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
122
+ * - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
123
+ */
124
+ export function accept(this_: TcpSocket): [TcpSocket, InputStream, OutputStream];
125
+ /**
126
+ * Get the bound local address.
127
+ *
128
+ * # Typical errors
129
+ * - `not-bound`: The socket is not bound to any local address.
130
+ *
131
+ * # References
132
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
133
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
134
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
135
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
136
+ */
137
+ export function localAddress(this_: TcpSocket): IpSocketAddress;
138
+ /**
139
+ * Get the bound remote address.
140
+ *
141
+ * # Typical errors
142
+ * - `not-connected`: The socket is not connected to a remote address. (ENOTCONN)
143
+ *
144
+ * # References
145
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
146
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
147
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
148
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
149
+ */
150
+ export function remoteAddress(this_: TcpSocket): IpSocketAddress;
151
+ /**
152
+ * Whether this is a IPv4 or IPv6 socket.
153
+ *
154
+ * Equivalent to the SO_DOMAIN socket option.
155
+ */
156
+ export function addressFamily(this_: TcpSocket): IpAddressFamily;
157
+ /**
158
+ * Whether IPv4 compatibility (dual-stack) mode is disabled or not.
159
+ *
160
+ * Equivalent to the IPV6_V6ONLY socket option.
161
+ *
162
+ * # Typical errors
163
+ * - `ipv6-only-operation`: (get/set) `this` socket is an IPv4 socket.
164
+ * - `already-bound`: (set) The socket is already bound.
165
+ * - `not-supported`: (set) Host does not support dual-stack sockets. (Implementations are not required to.)
166
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
167
+ */
168
+ export function ipv6Only(this_: TcpSocket): boolean;
169
+ export function setIpv6Only(this_: TcpSocket, value: boolean): void;
170
+ /**
171
+ * Hints the desired listen queue size. Implementations are free to ignore this.
172
+ *
173
+ * # Typical errors
174
+ * - `already-connected`: (set) The socket is already in the Connection state.
175
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
176
+ */
177
+ export function setListenBacklogSize(this_: TcpSocket, value: bigint): void;
178
+ /**
179
+ * Equivalent to the SO_KEEPALIVE socket option.
180
+ *
181
+ * # Typical errors
182
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
183
+ */
184
+ export function keepAlive(this_: TcpSocket): boolean;
185
+ export function setKeepAlive(this_: TcpSocket, value: boolean): void;
186
+ /**
187
+ * Equivalent to the TCP_NODELAY socket option.
188
+ *
189
+ * # Typical errors
190
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
191
+ */
192
+ export function noDelay(this_: TcpSocket): boolean;
193
+ export function setNoDelay(this_: TcpSocket, value: boolean): void;
194
+ /**
195
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
196
+ *
197
+ * # Typical errors
198
+ * - `already-connected`: (set) The socket is already in the Connection state.
199
+ * - `already-listening`: (set) The socket is already in the Listener state.
200
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
201
+ */
202
+ export function unicastHopLimit(this_: TcpSocket): number;
203
+ export function setUnicastHopLimit(this_: TcpSocket, value: number): void;
204
+ /**
205
+ * The kernel buffer space reserved for sends/receives on this socket.
206
+ *
207
+ * Note #1: an implementation may choose to cap or round the buffer size when setting the value.
208
+ * In other words, after setting a value, reading the same setting back may return a different value.
209
+ *
210
+ * Note #2: there is not necessarily a direct relationship between the kernel buffer size and the bytes of
211
+ * actual data to be sent/received by the application, because the kernel might also use the buffer space
212
+ * for internal metadata structures.
213
+ *
214
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
215
+ *
216
+ * # Typical errors
217
+ * - `already-connected`: (set) The socket is already in the Connection state.
218
+ * - `already-listening`: (set) The socket is already in the Listener state.
219
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
220
+ */
221
+ export function receiveBufferSize(this_: TcpSocket): bigint;
222
+ export function setReceiveBufferSize(this_: TcpSocket, value: bigint): void;
223
+ export function sendBufferSize(this_: TcpSocket): bigint;
224
+ export function setSendBufferSize(this_: TcpSocket, value: bigint): void;
225
+ /**
226
+ * Create a `pollable` which will resolve once the socket is ready for I/O.
227
+ *
228
+ * The created `pollable` is a child resource of the `tcp-socket`.
229
+ * Implementations may trap if the `tcp-socket` is dropped before all
230
+ * derived `pollable`s created with this function are dropped.
231
+ *
232
+ * Note: this function is here for WASI Preview2 only.
233
+ * It's planned to be removed when `future` is natively supported in Preview3.
234
+ */
235
+ export function subscribe(this_: TcpSocket): Pollable;
236
+ /**
237
+ * Initiate a graceful shutdown.
238
+ *
239
+ * - receive: the socket is not expecting to receive any more data from the peer. All subsequent read
240
+ * operations on the `input-stream` associated with this socket will return an End Of Stream indication.
241
+ * Any data still in the receive queue at time of calling `shutdown` will be discarded.
242
+ * - send: the socket is not expecting to send any more data to the peer. All subsequent write
243
+ * operations on the `output-stream` associated with this socket will return an error.
244
+ * - both: same effect as receive & send combined.
245
+ *
246
+ * The shutdown function does not close (drop) the socket.
247
+ *
248
+ * # Typical errors
249
+ * - `not-connected`: The socket is not in the Connection state. (ENOTCONN)
250
+ *
251
+ * # References
252
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
253
+ * - <https://man7.org/linux/man-pages/man2/shutdown.2.html>
254
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown>
255
+ * - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
256
+ */
257
+ export function shutdown(this_: TcpSocket, shutdownType: ShutdownType): void;
258
+ /**
259
+ * Dispose of the specified `tcp-socket`, after which it may no longer be used.
260
+ *
261
+ * Similar to the POSIX `close` function.
262
+ *
263
+ * Note: this function is scheduled to be removed when Resources are natively supported in Wit.
264
+ */
265
+ export function dropTcpSocket(this_: TcpSocket): void;
266
+ }
267
+ import type { InputStream } from '../interfaces/wasi-io-streams';
268
+ export { InputStream };
269
+ import type { OutputStream } from '../interfaces/wasi-io-streams';
270
+ export { OutputStream };
271
+ import type { Pollable } from '../interfaces/wasi-poll-poll';
272
+ export { Pollable };
273
+ import type { Network } from '../interfaces/wasi-sockets-network';
274
+ export { Network };
275
+ import type { ErrorCode } from '../interfaces/wasi-sockets-network';
276
+ export { ErrorCode };
277
+ import type { IpSocketAddress } from '../interfaces/wasi-sockets-network';
278
+ export { IpSocketAddress };
279
+ import type { IpAddressFamily } from '../interfaces/wasi-sockets-network';
280
+ export { IpAddressFamily };
281
+ /**
282
+ * A TCP socket handle.
283
+ */
284
+ export type TcpSocket = number;
285
+ /**
286
+ * # Variants
287
+ *
288
+ * ## `"receive"`
289
+ *
290
+ * Similar to `SHUT_RD` in POSIX.
291
+ * ## `"send"`
292
+ *
293
+ * Similar to `SHUT_WR` in POSIX.
294
+ * ## `"both"`
295
+ *
296
+ * Similar to `SHUT_RDWR` in POSIX.
297
+ */
298
+ export type ShutdownType = 'receive' | 'send' | 'both';
@@ -0,0 +1,33 @@
1
+ export namespace WasiSocketsUdpCreateSocket {
2
+ /**
3
+ * Create a new UDP socket.
4
+ *
5
+ * Similar to `socket(AF_INET or AF_INET6, SOCK_DGRAM, IPPROTO_UDP)` in POSIX.
6
+ *
7
+ * This function does not require a network capability handle. This is considered to be safe because
8
+ * at time of creation, the socket is not bound to any `network` yet. Up to the moment `bind`/`connect` is called,
9
+ * the socket is effectively an in-memory configuration object, unable to communicate with the outside world.
10
+ *
11
+ * All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous operations.
12
+ *
13
+ * # Typical errors
14
+ * - `not-supported`: The host does not support UDP sockets. (EOPNOTSUPP)
15
+ * - `address-family-not-supported`: The specified `address-family` is not supported. (EAFNOSUPPORT)
16
+ * - `new-socket-limit`: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
17
+ *
18
+ * # References:
19
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>
20
+ * - <https://man7.org/linux/man-pages/man2/socket.2.html>
21
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketw>
22
+ * - <https://man.freebsd.org/cgi/man.cgi?query=socket&sektion=2>
23
+ */
24
+ export function createUdpSocket(addressFamily: IpAddressFamily): UdpSocket;
25
+ }
26
+ import type { Network } from '../interfaces/wasi-sockets-network';
27
+ export { Network };
28
+ import type { ErrorCode } from '../interfaces/wasi-sockets-network';
29
+ export { ErrorCode };
30
+ import type { IpAddressFamily } from '../interfaces/wasi-sockets-network';
31
+ export { IpAddressFamily };
32
+ import type { UdpSocket } from '../interfaces/wasi-sockets-udp';
33
+ export { UdpSocket };
@@ -0,0 +1,228 @@
1
+ export namespace WasiSocketsUdp {
2
+ /**
3
+ * Bind the socket to a specific network on the provided IP address and port.
4
+ *
5
+ * If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the implementation to decide which
6
+ * network interface(s) to bind to.
7
+ * If the TCP/UDP port is zero, the socket will be bound to a random free port.
8
+ *
9
+ * When a socket is not explicitly bound, the first invocation to connect will implicitly bind the socket.
10
+ *
11
+ * Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
12
+ *
13
+ * # Typical `start` errors
14
+ * - `address-family-mismatch`: The `local-address` has the wrong address family. (EINVAL)
15
+ * - `already-bound`: The socket is already bound. (EINVAL)
16
+ * - `concurrency-conflict`: Another `bind` or `connect` operation is already in progress. (EALREADY)
17
+ *
18
+ * # Typical `finish` errors
19
+ * - `ephemeral-ports-exhausted`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
20
+ * - `address-in-use`: Address is already in use. (EADDRINUSE)
21
+ * - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
22
+ * - `not-in-progress`: A `bind` operation is not in progress.
23
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
24
+ *
25
+ * # References
26
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
27
+ * - <https://man7.org/linux/man-pages/man2/bind.2.html>
28
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
29
+ * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
30
+ */
31
+ export function startBind(this_: UdpSocket, network: Network, localAddress: IpSocketAddress): void;
32
+ export function finishBind(this_: UdpSocket): void;
33
+ /**
34
+ * Set the destination address.
35
+ *
36
+ * The local-address is updated based on the best network path to `remote-address`.
37
+ *
38
+ * When a destination address is set:
39
+ * - all receive operations will only return datagrams sent from the provided `remote-address`.
40
+ * - the `send` function can only be used to send to this destination.
41
+ *
42
+ * Note that this function does not generate any network traffic and the peer is not aware of this "connection".
43
+ *
44
+ * Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
45
+ *
46
+ * # Typical `start` errors
47
+ * - `address-family-mismatch`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
48
+ * - `invalid-remote-address`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
49
+ * - `invalid-remote-address`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
50
+ * - `already-attached`: The socket is already bound to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
51
+ * - `concurrency-conflict`: Another `bind` or `connect` operation is already in progress. (EALREADY)
52
+ *
53
+ * # Typical `finish` errors
54
+ * - `ephemeral-ports-exhausted`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
55
+ * - `not-in-progress`: A `connect` operation is not in progress.
56
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
57
+ *
58
+ * # References
59
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
60
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
61
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
62
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
63
+ */
64
+ export function startConnect(this_: UdpSocket, network: Network, remoteAddress: IpSocketAddress): void;
65
+ export function finishConnect(this_: UdpSocket): void;
66
+ /**
67
+ * Receive messages on the socket.
68
+ *
69
+ * This function attempts to receive up to `max-results` datagrams on the socket without blocking.
70
+ * The returned list may contain fewer elements than requested, but never more.
71
+ * If `max-results` is 0, this function returns successfully with an empty list.
72
+ *
73
+ * # Typical errors
74
+ * - `not-bound`: The socket is not bound to any local address. (EINVAL)
75
+ * - `remote-unreachable`: The remote address is not reachable. (ECONNREFUSED, ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN)
76
+ * - `would-block`: There is no pending data available to be read at the moment. (EWOULDBLOCK, EAGAIN)
77
+ *
78
+ * # References
79
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html>
80
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html>
81
+ * - <https://man7.org/linux/man-pages/man2/recv.2.html>
82
+ * - <https://man7.org/linux/man-pages/man2/recvmmsg.2.html>
83
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv>
84
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom>
85
+ * - <https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms741687(v=vs.85)>
86
+ * - <https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2>
87
+ */
88
+ export function receive(this_: UdpSocket, maxResults: bigint): Datagram[];
89
+ /**
90
+ * Send messages on the socket.
91
+ *
92
+ * This function attempts to send all provided `datagrams` on the socket without blocking and
93
+ * returns how many messages were actually sent (or queued for sending).
94
+ *
95
+ * This function semantically behaves the same as iterating the `datagrams` list and sequentially
96
+ * sending each individual datagram until either the end of the list has been reached or the first error occurred.
97
+ * If at least one datagram has been sent successfully, this function never returns an error.
98
+ *
99
+ * If the input list is empty, the function returns `ok(0)`.
100
+ *
101
+ * The remote address option is required. To send a message to the "connected" peer,
102
+ * call `remote-address` to get their address.
103
+ *
104
+ * # Typical errors
105
+ * - `address-family-mismatch`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
106
+ * - `invalid-remote-address`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
107
+ * - `invalid-remote-address`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
108
+ * - `already-connected`: The socket is in "connected" mode and the `datagram.remote-address` does not match the address passed to `connect`. (EISCONN)
109
+ * - `not-bound`: The socket is not bound to any local address. Unlike POSIX, this function does not perform an implicit bind.
110
+ * - `remote-unreachable`: The remote address is not reachable. (ECONNREFUSED, ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN)
111
+ * - `datagram-too-large`: The datagram is too large. (EMSGSIZE)
112
+ * - `would-block`: The send buffer is currently full. (EWOULDBLOCK, EAGAIN)
113
+ *
114
+ * # References
115
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html>
116
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html>
117
+ * - <https://man7.org/linux/man-pages/man2/send.2.html>
118
+ * - <https://man7.org/linux/man-pages/man2/sendmmsg.2.html>
119
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send>
120
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-sendto>
121
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasendmsg>
122
+ * - <https://man.freebsd.org/cgi/man.cgi?query=send&sektion=2>
123
+ */
124
+ export function send(this_: UdpSocket, datagrams: Datagram[]): bigint;
125
+ /**
126
+ * Get the current bound address.
127
+ *
128
+ * # Typical errors
129
+ * - `not-bound`: The socket is not bound to any local address.
130
+ *
131
+ * # References
132
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
133
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
134
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
135
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
136
+ */
137
+ export function localAddress(this_: UdpSocket): IpSocketAddress;
138
+ /**
139
+ * Get the address set with `connect`.
140
+ *
141
+ * # Typical errors
142
+ * - `not-connected`: The socket is not connected to a remote address. (ENOTCONN)
143
+ *
144
+ * # References
145
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
146
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
147
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
148
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
149
+ */
150
+ export function remoteAddress(this_: UdpSocket): IpSocketAddress;
151
+ /**
152
+ * Whether this is a IPv4 or IPv6 socket.
153
+ *
154
+ * Equivalent to the SO_DOMAIN socket option.
155
+ */
156
+ export function addressFamily(this_: UdpSocket): IpAddressFamily;
157
+ /**
158
+ * Whether IPv4 compatibility (dual-stack) mode is disabled or not.
159
+ *
160
+ * Equivalent to the IPV6_V6ONLY socket option.
161
+ *
162
+ * # Typical errors
163
+ * - `ipv6-only-operation`: (get/set) `this` socket is an IPv4 socket.
164
+ * - `already-bound`: (set) The socket is already bound.
165
+ * - `not-supported`: (set) Host does not support dual-stack sockets. (Implementations are not required to.)
166
+ * - `concurrency-conflict`: (set) Another `bind` or `connect` operation is already in progress. (EALREADY)
167
+ */
168
+ export function ipv6Only(this_: UdpSocket): boolean;
169
+ export function setIpv6Only(this_: UdpSocket, value: boolean): void;
170
+ /**
171
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
172
+ *
173
+ * # Typical errors
174
+ * - `concurrency-conflict`: (set) Another `bind` or `connect` operation is already in progress. (EALREADY)
175
+ */
176
+ export function unicastHopLimit(this_: UdpSocket): number;
177
+ export function setUnicastHopLimit(this_: UdpSocket, value: number): void;
178
+ /**
179
+ * The kernel buffer space reserved for sends/receives on this socket.
180
+ *
181
+ * Note #1: an implementation may choose to cap or round the buffer size when setting the value.
182
+ * In other words, after setting a value, reading the same setting back may return a different value.
183
+ *
184
+ * Note #2: there is not necessarily a direct relationship between the kernel buffer size and the bytes of
185
+ * actual data to be sent/received by the application, because the kernel might also use the buffer space
186
+ * for internal metadata structures.
187
+ *
188
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
189
+ *
190
+ * # Typical errors
191
+ * - `concurrency-conflict`: (set) Another `bind` or `connect` operation is already in progress. (EALREADY)
192
+ */
193
+ export function receiveBufferSize(this_: UdpSocket): bigint;
194
+ export function setReceiveBufferSize(this_: UdpSocket, value: bigint): void;
195
+ export function sendBufferSize(this_: UdpSocket): bigint;
196
+ export function setSendBufferSize(this_: UdpSocket, value: bigint): void;
197
+ /**
198
+ * Create a `pollable` which will resolve once the socket is ready for I/O.
199
+ *
200
+ * Note: this function is here for WASI Preview2 only.
201
+ * It's planned to be removed when `future` is natively supported in Preview3.
202
+ */
203
+ export function subscribe(this_: UdpSocket): Pollable;
204
+ /**
205
+ * Dispose of the specified `udp-socket`, after which it may no longer be used.
206
+ *
207
+ * Note: this function is scheduled to be removed when Resources are natively supported in Wit.
208
+ */
209
+ export function dropUdpSocket(this_: UdpSocket): void;
210
+ }
211
+ import type { Pollable } from '../interfaces/wasi-poll-poll';
212
+ export { Pollable };
213
+ import type { Network } from '../interfaces/wasi-sockets-network';
214
+ export { Network };
215
+ import type { ErrorCode } from '../interfaces/wasi-sockets-network';
216
+ export { ErrorCode };
217
+ import type { IpSocketAddress } from '../interfaces/wasi-sockets-network';
218
+ export { IpSocketAddress };
219
+ import type { IpAddressFamily } from '../interfaces/wasi-sockets-network';
220
+ export { IpAddressFamily };
221
+ /**
222
+ * A UDP socket handle.
223
+ */
224
+ export type UdpSocket = number;
225
+ export interface Datagram {
226
+ data: Uint8Array,
227
+ remoteAddress: IpSocketAddress,
228
+ }
@@ -0,0 +1,29 @@
1
+ import { WasiCliEnvironment } from './interfaces/wasi-cli-environment';
2
+ import { WasiCliExit } from './interfaces/wasi-cli-exit';
3
+ import { WasiCliStderr } from './interfaces/wasi-cli-stderr';
4
+ import { WasiCliStdin } from './interfaces/wasi-cli-stdin';
5
+ import { WasiCliStdout } from './interfaces/wasi-cli-stdout';
6
+ import { WasiCliTerminalInput } from './interfaces/wasi-cli-terminal-input';
7
+ import { WasiCliTerminalOutput } from './interfaces/wasi-cli-terminal-output';
8
+ import { WasiCliTerminalStderr } from './interfaces/wasi-cli-terminal-stderr';
9
+ import { WasiCliTerminalStdin } from './interfaces/wasi-cli-terminal-stdin';
10
+ import { WasiCliTerminalStdout } from './interfaces/wasi-cli-terminal-stdout';
11
+ import { WasiClocksMonotonicClock } from './interfaces/wasi-clocks-monotonic-clock';
12
+ import { WasiClocksTimezone } from './interfaces/wasi-clocks-timezone';
13
+ import { WasiClocksWallClock } from './interfaces/wasi-clocks-wall-clock';
14
+ import { WasiFilesystemPreopens } from './interfaces/wasi-filesystem-preopens';
15
+ import { WasiFilesystemTypes } from './interfaces/wasi-filesystem-types';
16
+ import { WasiIoStreams } from './interfaces/wasi-io-streams';
17
+ import { WasiPollPoll } from './interfaces/wasi-poll-poll';
18
+ import { WasiRandomInsecure } from './interfaces/wasi-random-insecure';
19
+ import { WasiRandomInsecureSeed } from './interfaces/wasi-random-insecure-seed';
20
+ import { WasiRandomRandom } from './interfaces/wasi-random-random';
21
+ import { WasiSocketsInstanceNetwork } from './interfaces/wasi-sockets-instance-network';
22
+ import { WasiSocketsIpNameLookup } from './interfaces/wasi-sockets-ip-name-lookup';
23
+ import { WasiSocketsNetwork } from './interfaces/wasi-sockets-network';
24
+ import { WasiSocketsTcp } from './interfaces/wasi-sockets-tcp';
25
+ import { WasiSocketsTcpCreateSocket } from './interfaces/wasi-sockets-tcp-create-socket';
26
+ import { WasiSocketsUdp } from './interfaces/wasi-sockets-udp';
27
+ import { WasiSocketsUdpCreateSocket } from './interfaces/wasi-sockets-udp-create-socket';
28
+ import { WasiCliRun } from './interfaces/wasi-cli-run';
29
+ export const run: typeof WasiCliRun;