@bytecodealliance/preview2-shim 0.16.1 → 0.16.2

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.
@@ -1,306 +1,5 @@
1
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
- * Bind can be attempted multiple times on the same socket, even with
10
- * different arguments on each iteration. But never concurrently and
11
- * only as long as the previous bind failed. Once a bind succeeds, the
12
- * binding can't be changed anymore.
13
- *
14
- * # Typical errors
15
- * - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
16
- * - `invalid-argument`: `local-address` is not a unicast address. (EINVAL)
17
- * - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. (EINVAL)
18
- * - `invalid-state`: The socket is already bound. (EINVAL)
19
- * - `address-in-use`: 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
- * # Implementors note
26
- * When binding to a non-zero port, this bind operation shouldn't be affected by the TIME_WAIT
27
- * state of a recently closed socket on the same local address. In practice this means that the SO_REUSEADDR
28
- * socket option should be set implicitly on all platforms, except on Windows where this is the default behavior
29
- * and SO_REUSEADDR performs something different entirely.
30
- *
31
- * Unlike in POSIX, in WASI the bind operation is async. This enables
32
- * interactive WASI hosts to inject permission prompts. Runtimes that
33
- * don't want to make use of this ability can simply call the native
34
- * `bind` as part of either `start-bind` or `finish-bind`.
35
- *
36
- * # References
37
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
38
- * - <https://man7.org/linux/man-pages/man2/bind.2.html>
39
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
40
- * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
41
- */
42
2
  export { TcpSocket };
43
- /**
44
- * Connect to a remote endpoint.
45
- *
46
- * On success:
47
- * - the socket is transitioned into the `connection` state.
48
- * - a pair of streams is returned that can be used to read & write to the connection
49
- *
50
- * After a failed connection attempt, the socket will be in the `closed`
51
- * state and the only valid action left is to `drop` the socket. A single
52
- * socket can not be used to connect more than once.
53
- *
54
- * # Typical errors
55
- * - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
56
- * - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)
57
- * - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)
58
- * - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
59
- * - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
60
- * - `invalid-argument`: The socket is already attached to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
61
- * - `invalid-state`: The socket is already in the `connected` state. (EISCONN)
62
- * - `invalid-state`: The socket is already in the `listening` state. (EOPNOTSUPP, EINVAL on Windows)
63
- * - `timeout`: Connection timed out. (ETIMEDOUT)
64
- * - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED)
65
- * - `connection-reset`: The connection was reset. (ECONNRESET)
66
- * - `connection-aborted`: The connection was aborted. (ECONNABORTED)
67
- * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
68
- * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
69
- * - `not-in-progress`: A connect operation is not in progress.
70
- * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
71
- *
72
- * # Implementors note
73
- * The POSIX equivalent of `start-connect` is the regular `connect` syscall.
74
- * Because all WASI sockets are non-blocking this is expected to return
75
- * EINPROGRESS, which should be translated to `ok()` in WASI.
76
- *
77
- * The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT`
78
- * with a timeout of 0 on the socket descriptor. Followed by a check for
79
- * the `SO_ERROR` socket option, in case the poll signaled readiness.
80
- *
81
- * # References
82
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
83
- * - <https://man7.org/linux/man-pages/man2/connect.2.html>
84
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
85
- * - <https://man.freebsd.org/cgi/man.cgi?connect>
86
- */
87
- /**
88
- * Start listening for new connections.
89
- *
90
- * Transitions the socket into the `listening` state.
91
- *
92
- * Unlike POSIX, the socket must already be explicitly bound.
93
- *
94
- * # Typical errors
95
- * - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ)
96
- * - `invalid-state`: The socket is already in the `connected` state. (EISCONN, EINVAL on BSD)
97
- * - `invalid-state`: The socket is already in the `listening` state.
98
- * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
99
- * - `not-in-progress`: A listen operation is not in progress.
100
- * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
101
- *
102
- * # Implementors note
103
- * Unlike in POSIX, in WASI the listen operation is async. This enables
104
- * interactive WASI hosts to inject permission prompts. Runtimes that
105
- * don't want to make use of this ability can simply call the native
106
- * `listen` as part of either `start-listen` or `finish-listen`.
107
- *
108
- * # References
109
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
110
- * - <https://man7.org/linux/man-pages/man2/listen.2.html>
111
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
112
- * - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
113
- */
114
- /**
115
- * Accept a new client socket.
116
- *
117
- * The returned socket is bound and in the `connected` state. The following properties are inherited from the listener socket:
118
- * - `address-family`
119
- * - `keep-alive-enabled`
120
- * - `keep-alive-idle-time`
121
- * - `keep-alive-interval`
122
- * - `keep-alive-count`
123
- * - `hop-limit`
124
- * - `receive-buffer-size`
125
- * - `send-buffer-size`
126
- *
127
- * On success, this function returns the newly accepted client socket along with
128
- * a pair of streams that can be used to read & write to the connection.
129
- *
130
- * # Typical errors
131
- * - `invalid-state`: Socket is not in the `listening` state. (EINVAL)
132
- * - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
133
- * - `connection-aborted`: An incoming connection was pending, but was terminated by the client before this listener could accept it. (ECONNABORTED)
134
- * - `new-socket-limit`: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
135
- *
136
- * # References
137
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
138
- * - <https://man7.org/linux/man-pages/man2/accept.2.html>
139
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
140
- * - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
141
- */
142
- /**
143
- * Get the bound local address.
144
- *
145
- * POSIX mentions:
146
- * > If the socket has not been bound to a local name, the value
147
- * > stored in the object pointed to by `address` is unspecified.
148
- *
149
- * WASI is stricter and requires `local-address` to return `invalid-state` when the socket hasn't been bound yet.
150
- *
151
- * # Typical errors
152
- * - `invalid-state`: The socket is not bound to any local address.
153
- *
154
- * # References
155
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
156
- * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
157
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
158
- * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
159
- */
160
- /**
161
- * Get the remote address.
162
- *
163
- * # Typical errors
164
- * - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN)
165
- *
166
- * # References
167
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
168
- * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
169
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
170
- * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
171
- */
172
- /**
173
- * Whether the socket is in the `listening` state.
174
- *
175
- * Equivalent to the SO_ACCEPTCONN socket option.
176
- */
177
- /**
178
- * Whether this is a IPv4 or IPv6 socket.
179
- *
180
- * Equivalent to the SO_DOMAIN socket option.
181
- */
182
- /**
183
- * Hints the desired listen queue size. Implementations are free to ignore this.
184
- *
185
- * If the provided value is 0, an `invalid-argument` error is returned.
186
- * Any other value will never cause an error, but it might be silently clamped and/or rounded.
187
- *
188
- * # Typical errors
189
- * - `not-supported`: (set) The platform does not support changing the backlog size after the initial listen.
190
- * - `invalid-argument`: (set) The provided value was 0.
191
- * - `invalid-state`: (set) The socket is in the `connect-in-progress` or `connected` state.
192
- */
193
- /**
194
- * Enables or disables keepalive.
195
- *
196
- * The keepalive behavior can be adjusted using:
197
- * - `keep-alive-idle-time`
198
- * - `keep-alive-interval`
199
- * - `keep-alive-count`
200
- * These properties can be configured while `keep-alive-enabled` is false, but only come into effect when `keep-alive-enabled` is true.
201
- *
202
- * Equivalent to the SO_KEEPALIVE socket option.
203
- */
204
- /**
205
- * Amount of time the connection has to be idle before TCP starts sending keepalive packets.
206
- *
207
- * If the provided value is 0, an `invalid-argument` error is returned.
208
- * Any other value will never cause an error, but it might be silently clamped and/or rounded.
209
- * I.e. after setting a value, reading the same setting back may return a different value.
210
- *
211
- * Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS)
212
- *
213
- * # Typical errors
214
- * - `invalid-argument`: (set) The provided value was 0.
215
- */
216
- /**
217
- * The time between keepalive packets.
218
- *
219
- * If the provided value is 0, an `invalid-argument` error is returned.
220
- * Any other value will never cause an error, but it might be silently clamped and/or rounded.
221
- * I.e. after setting a value, reading the same setting back may return a different value.
222
- *
223
- * Equivalent to the TCP_KEEPINTVL socket option.
224
- *
225
- * # Typical errors
226
- * - `invalid-argument`: (set) The provided value was 0.
227
- */
228
- /**
229
- * The maximum amount of keepalive packets TCP should send before aborting the connection.
230
- *
231
- * If the provided value is 0, an `invalid-argument` error is returned.
232
- * Any other value will never cause an error, but it might be silently clamped and/or rounded.
233
- * I.e. after setting a value, reading the same setting back may return a different value.
234
- *
235
- * Equivalent to the TCP_KEEPCNT socket option.
236
- *
237
- * # Typical errors
238
- * - `invalid-argument`: (set) The provided value was 0.
239
- */
240
- /**
241
- * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
242
- *
243
- * If the provided value is 0, an `invalid-argument` error is returned.
244
- *
245
- * # Typical errors
246
- * - `invalid-argument`: (set) The TTL value must be 1 or higher.
247
- */
248
- /**
249
- * The kernel buffer space reserved for sends/receives on this socket.
250
- *
251
- * If the provided value is 0, an `invalid-argument` error is returned.
252
- * Any other value will never cause an error, but it might be silently clamped and/or rounded.
253
- * I.e. after setting a value, reading the same setting back may return a different value.
254
- *
255
- * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
256
- *
257
- * # Typical errors
258
- * - `invalid-argument`: (set) The provided value was 0.
259
- */
260
- /**
261
- * Create a `pollable` which can be used to poll for, or block on,
262
- * completion of any of the asynchronous operations of this socket.
263
- *
264
- * When `finish-bind`, `finish-listen`, `finish-connect` or `accept`
265
- * return `error(would-block)`, this pollable can be used to wait for
266
- * their success or failure, after which the method can be retried.
267
- *
268
- * The pollable is not limited to the async operation that happens to be
269
- * in progress at the time of calling `subscribe` (if any). Theoretically,
270
- * `subscribe` only has to be called once per socket and can then be
271
- * (re)used for the remainder of the socket's lifetime.
272
- *
273
- * See <https://github.com/WebAssembly/wasi-sockets/TcpSocketOperationalSemantics.md#Pollable-readiness>
274
- * for a more information.
275
- *
276
- * Note: this function is here for WASI Preview2 only.
277
- * It's planned to be removed when `future` is natively supported in Preview3.
278
- */
279
- /**
280
- * Initiate a graceful shutdown.
281
- *
282
- * - `receive`: The socket is not expecting to receive any data from
283
- * the peer. The `input-stream` associated with this socket will be
284
- * closed. Any data still in the receive queue at time of calling
285
- * this method will be discarded.
286
- * - `send`: The socket has no more data to send to the peer. The `output-stream`
287
- * associated with this socket will be closed and a FIN packet will be sent.
288
- * - `both`: Same effect as `receive` & `send` combined.
289
- *
290
- * This function is idempotent. Shutting a down a direction more than once
291
- * has no effect and returns `ok`.
292
- *
293
- * The shutdown function does not close (drop) the socket.
294
- *
295
- * # Typical errors
296
- * - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
297
- *
298
- * # References
299
- * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
300
- * - <https://man7.org/linux/man-pages/man2/shutdown.2.html>
301
- * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown>
302
- * - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
303
- */
304
3
  }
305
4
  import type { InputStream } from './wasi-io-streams.js';
306
5
  export { InputStream };
@@ -334,32 +33,333 @@ export { IpAddressFamily };
334
33
  export type ShutdownType = 'receive' | 'send' | 'both';
335
34
 
336
35
  export class TcpSocket {
36
+ /**
37
+ * Bind the socket to a specific network on the provided IP address and port.
38
+ *
39
+ * If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the implementation to decide which
40
+ * network interface(s) to bind to.
41
+ * If the TCP/UDP port is zero, the socket will be bound to a random free port.
42
+ *
43
+ * Bind can be attempted multiple times on the same socket, even with
44
+ * different arguments on each iteration. But never concurrently and
45
+ * only as long as the previous bind failed. Once a bind succeeds, the
46
+ * binding can't be changed anymore.
47
+ *
48
+ * # Typical errors
49
+ * - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
50
+ * - `invalid-argument`: `local-address` is not a unicast address. (EINVAL)
51
+ * - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. (EINVAL)
52
+ * - `invalid-state`: The socket is already bound. (EINVAL)
53
+ * - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
54
+ * - `address-in-use`: Address is already in use. (EADDRINUSE)
55
+ * - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
56
+ * - `not-in-progress`: A `bind` operation is not in progress.
57
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
58
+ *
59
+ * # Implementors note
60
+ * When binding to a non-zero port, this bind operation shouldn't be affected by the TIME_WAIT
61
+ * state of a recently closed socket on the same local address. In practice this means that the SO_REUSEADDR
62
+ * socket option should be set implicitly on all platforms, except on Windows where this is the default behavior
63
+ * and SO_REUSEADDR performs something different entirely.
64
+ *
65
+ * Unlike in POSIX, in WASI the bind operation is async. This enables
66
+ * interactive WASI hosts to inject permission prompts. Runtimes that
67
+ * don't want to make use of this ability can simply call the native
68
+ * `bind` as part of either `start-bind` or `finish-bind`.
69
+ *
70
+ * # References
71
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
72
+ * - <https://man7.org/linux/man-pages/man2/bind.2.html>
73
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
74
+ * - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
75
+ */
337
76
  startBind(network: Network, localAddress: IpSocketAddress): void;
338
77
  finishBind(): void;
78
+ /**
79
+ * Connect to a remote endpoint.
80
+ *
81
+ * On success:
82
+ * - the socket is transitioned into the `connection` state.
83
+ * - a pair of streams is returned that can be used to read & write to the connection
84
+ *
85
+ * After a failed connection attempt, the socket will be in the `closed`
86
+ * state and the only valid action left is to `drop` the socket. A single
87
+ * socket can not be used to connect more than once.
88
+ *
89
+ * # Typical errors
90
+ * - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
91
+ * - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)
92
+ * - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)
93
+ * - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
94
+ * - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
95
+ * - `invalid-argument`: The socket is already attached to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
96
+ * - `invalid-state`: The socket is already in the `connected` state. (EISCONN)
97
+ * - `invalid-state`: The socket is already in the `listening` state. (EOPNOTSUPP, EINVAL on Windows)
98
+ * - `timeout`: Connection timed out. (ETIMEDOUT)
99
+ * - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED)
100
+ * - `connection-reset`: The connection was reset. (ECONNRESET)
101
+ * - `connection-aborted`: The connection was aborted. (ECONNABORTED)
102
+ * - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
103
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
104
+ * - `not-in-progress`: A connect operation is not in progress.
105
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
106
+ *
107
+ * # Implementors note
108
+ * The POSIX equivalent of `start-connect` is the regular `connect` syscall.
109
+ * Because all WASI sockets are non-blocking this is expected to return
110
+ * EINPROGRESS, which should be translated to `ok()` in WASI.
111
+ *
112
+ * The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT`
113
+ * with a timeout of 0 on the socket descriptor. Followed by a check for
114
+ * the `SO_ERROR` socket option, in case the poll signaled readiness.
115
+ *
116
+ * # References
117
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
118
+ * - <https://man7.org/linux/man-pages/man2/connect.2.html>
119
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
120
+ * - <https://man.freebsd.org/cgi/man.cgi?connect>
121
+ */
339
122
  startConnect(network: Network, remoteAddress: IpSocketAddress): void;
340
123
  finishConnect(): [InputStream, OutputStream];
124
+ /**
125
+ * Start listening for new connections.
126
+ *
127
+ * Transitions the socket into the `listening` state.
128
+ *
129
+ * Unlike POSIX, the socket must already be explicitly bound.
130
+ *
131
+ * # Typical errors
132
+ * - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ)
133
+ * - `invalid-state`: The socket is already in the `connected` state. (EISCONN, EINVAL on BSD)
134
+ * - `invalid-state`: The socket is already in the `listening` state.
135
+ * - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
136
+ * - `not-in-progress`: A listen operation is not in progress.
137
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
138
+ *
139
+ * # Implementors note
140
+ * Unlike in POSIX, in WASI the listen operation is async. This enables
141
+ * interactive WASI hosts to inject permission prompts. Runtimes that
142
+ * don't want to make use of this ability can simply call the native
143
+ * `listen` as part of either `start-listen` or `finish-listen`.
144
+ *
145
+ * # References
146
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
147
+ * - <https://man7.org/linux/man-pages/man2/listen.2.html>
148
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
149
+ * - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
150
+ */
341
151
  startListen(): void;
342
152
  finishListen(): void;
153
+ /**
154
+ * Accept a new client socket.
155
+ *
156
+ * The returned socket is bound and in the `connected` state. The following properties are inherited from the listener socket:
157
+ * - `address-family`
158
+ * - `keep-alive-enabled`
159
+ * - `keep-alive-idle-time`
160
+ * - `keep-alive-interval`
161
+ * - `keep-alive-count`
162
+ * - `hop-limit`
163
+ * - `receive-buffer-size`
164
+ * - `send-buffer-size`
165
+ *
166
+ * On success, this function returns the newly accepted client socket along with
167
+ * a pair of streams that can be used to read & write to the connection.
168
+ *
169
+ * # Typical errors
170
+ * - `invalid-state`: Socket is not in the `listening` state. (EINVAL)
171
+ * - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
172
+ * - `connection-aborted`: An incoming connection was pending, but was terminated by the client before this listener could accept it. (ECONNABORTED)
173
+ * - `new-socket-limit`: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
174
+ *
175
+ * # References
176
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
177
+ * - <https://man7.org/linux/man-pages/man2/accept.2.html>
178
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
179
+ * - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
180
+ */
343
181
  accept(): [TcpSocket, InputStream, OutputStream];
182
+ /**
183
+ * Get the bound local address.
184
+ *
185
+ * POSIX mentions:
186
+ * > If the socket has not been bound to a local name, the value
187
+ * > stored in the object pointed to by `address` is unspecified.
188
+ *
189
+ * WASI is stricter and requires `local-address` to return `invalid-state` when the socket hasn't been bound yet.
190
+ *
191
+ * # Typical errors
192
+ * - `invalid-state`: The socket is not bound to any local address.
193
+ *
194
+ * # References
195
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
196
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
197
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
198
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
199
+ */
344
200
  localAddress(): IpSocketAddress;
201
+ /**
202
+ * Get the remote address.
203
+ *
204
+ * # Typical errors
205
+ * - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN)
206
+ *
207
+ * # References
208
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
209
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
210
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
211
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
212
+ */
345
213
  remoteAddress(): IpSocketAddress;
214
+ /**
215
+ * Whether the socket is in the `listening` state.
216
+ *
217
+ * Equivalent to the SO_ACCEPTCONN socket option.
218
+ */
346
219
  isListening(): boolean;
220
+ /**
221
+ * Whether this is a IPv4 or IPv6 socket.
222
+ *
223
+ * Equivalent to the SO_DOMAIN socket option.
224
+ */
347
225
  addressFamily(): IpAddressFamily;
226
+ /**
227
+ * Hints the desired listen queue size. Implementations are free to ignore this.
228
+ *
229
+ * If the provided value is 0, an `invalid-argument` error is returned.
230
+ * Any other value will never cause an error, but it might be silently clamped and/or rounded.
231
+ *
232
+ * # Typical errors
233
+ * - `not-supported`: (set) The platform does not support changing the backlog size after the initial listen.
234
+ * - `invalid-argument`: (set) The provided value was 0.
235
+ * - `invalid-state`: (set) The socket is in the `connect-in-progress` or `connected` state.
236
+ */
348
237
  setListenBacklogSize(value: bigint): void;
238
+ /**
239
+ * Enables or disables keepalive.
240
+ *
241
+ * The keepalive behavior can be adjusted using:
242
+ * - `keep-alive-idle-time`
243
+ * - `keep-alive-interval`
244
+ * - `keep-alive-count`
245
+ * These properties can be configured while `keep-alive-enabled` is false, but only come into effect when `keep-alive-enabled` is true.
246
+ *
247
+ * Equivalent to the SO_KEEPALIVE socket option.
248
+ */
349
249
  keepAliveEnabled(): boolean;
350
250
  setKeepAliveEnabled(value: boolean): void;
251
+ /**
252
+ * Amount of time the connection has to be idle before TCP starts sending keepalive packets.
253
+ *
254
+ * If the provided value is 0, an `invalid-argument` error is returned.
255
+ * Any other value will never cause an error, but it might be silently clamped and/or rounded.
256
+ * I.e. after setting a value, reading the same setting back may return a different value.
257
+ *
258
+ * Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS)
259
+ *
260
+ * # Typical errors
261
+ * - `invalid-argument`: (set) The provided value was 0.
262
+ */
351
263
  keepAliveIdleTime(): Duration;
352
264
  setKeepAliveIdleTime(value: Duration): void;
265
+ /**
266
+ * The time between keepalive packets.
267
+ *
268
+ * If the provided value is 0, an `invalid-argument` error is returned.
269
+ * Any other value will never cause an error, but it might be silently clamped and/or rounded.
270
+ * I.e. after setting a value, reading the same setting back may return a different value.
271
+ *
272
+ * Equivalent to the TCP_KEEPINTVL socket option.
273
+ *
274
+ * # Typical errors
275
+ * - `invalid-argument`: (set) The provided value was 0.
276
+ */
353
277
  keepAliveInterval(): Duration;
354
278
  setKeepAliveInterval(value: Duration): void;
279
+ /**
280
+ * The maximum amount of keepalive packets TCP should send before aborting the connection.
281
+ *
282
+ * If the provided value is 0, an `invalid-argument` error is returned.
283
+ * Any other value will never cause an error, but it might be silently clamped and/or rounded.
284
+ * I.e. after setting a value, reading the same setting back may return a different value.
285
+ *
286
+ * Equivalent to the TCP_KEEPCNT socket option.
287
+ *
288
+ * # Typical errors
289
+ * - `invalid-argument`: (set) The provided value was 0.
290
+ */
355
291
  keepAliveCount(): number;
356
292
  setKeepAliveCount(value: number): void;
293
+ /**
294
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
295
+ *
296
+ * If the provided value is 0, an `invalid-argument` error is returned.
297
+ *
298
+ * # Typical errors
299
+ * - `invalid-argument`: (set) The TTL value must be 1 or higher.
300
+ */
357
301
  hopLimit(): number;
358
302
  setHopLimit(value: number): void;
303
+ /**
304
+ * The kernel buffer space reserved for sends/receives on this socket.
305
+ *
306
+ * If the provided value is 0, an `invalid-argument` error is returned.
307
+ * Any other value will never cause an error, but it might be silently clamped and/or rounded.
308
+ * I.e. after setting a value, reading the same setting back may return a different value.
309
+ *
310
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
311
+ *
312
+ * # Typical errors
313
+ * - `invalid-argument`: (set) The provided value was 0.
314
+ */
359
315
  receiveBufferSize(): bigint;
360
316
  setReceiveBufferSize(value: bigint): void;
361
317
  sendBufferSize(): bigint;
362
318
  setSendBufferSize(value: bigint): void;
319
+ /**
320
+ * Create a `pollable` which can be used to poll for, or block on,
321
+ * completion of any of the asynchronous operations of this socket.
322
+ *
323
+ * When `finish-bind`, `finish-listen`, `finish-connect` or `accept`
324
+ * return `error(would-block)`, this pollable can be used to wait for
325
+ * their success or failure, after which the method can be retried.
326
+ *
327
+ * The pollable is not limited to the async operation that happens to be
328
+ * in progress at the time of calling `subscribe` (if any). Theoretically,
329
+ * `subscribe` only has to be called once per socket and can then be
330
+ * (re)used for the remainder of the socket's lifetime.
331
+ *
332
+ * See <https://github.com/WebAssembly/wasi-sockets/TcpSocketOperationalSemantics.md#Pollable-readiness>
333
+ * for a more information.
334
+ *
335
+ * Note: this function is here for WASI Preview2 only.
336
+ * It's planned to be removed when `future` is natively supported in Preview3.
337
+ */
363
338
  subscribe(): Pollable;
339
+ /**
340
+ * Initiate a graceful shutdown.
341
+ *
342
+ * - `receive`: The socket is not expecting to receive any data from
343
+ * the peer. The `input-stream` associated with this socket will be
344
+ * closed. Any data still in the receive queue at time of calling
345
+ * this method will be discarded.
346
+ * - `send`: The socket has no more data to send to the peer. The `output-stream`
347
+ * associated with this socket will be closed and a FIN packet will be sent.
348
+ * - `both`: Same effect as `receive` & `send` combined.
349
+ *
350
+ * This function is idempotent. Shutting a down a direction more than once
351
+ * has no effect and returns `ok`.
352
+ *
353
+ * The shutdown function does not close (drop) the socket.
354
+ *
355
+ * # Typical errors
356
+ * - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
357
+ *
358
+ * # References
359
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
360
+ * - <https://man7.org/linux/man-pages/man2/shutdown.2.html>
361
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown>
362
+ * - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
363
+ */
364
364
  shutdown(shutdownType: ShutdownType): void;
365
365
  }