@bytecodealliance/preview2-shim 0.0.7 → 0.0.9

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 (87) hide show
  1. package/README.md +4 -3
  2. package/lib/browser/cli-base.js +57 -0
  3. package/lib/browser/clocks.js +50 -0
  4. package/lib/browser/filesystem.js +134 -141
  5. package/lib/browser/http.js +120 -0
  6. package/lib/browser/index.js +14 -20
  7. package/lib/browser/io.js +62 -20
  8. package/lib/browser/logging.js +8 -4
  9. package/lib/browser/poll.js +10 -6
  10. package/lib/browser/random.js +32 -25
  11. package/lib/browser/sockets.js +203 -0
  12. package/lib/nodejs/cli-base.js +108 -0
  13. package/lib/nodejs/clocks.js +50 -0
  14. package/lib/nodejs/filesystem.js +313 -150
  15. package/lib/nodejs/http.js +120 -0
  16. package/lib/nodejs/index.js +14 -20
  17. package/lib/nodejs/io.js +138 -19
  18. package/lib/nodejs/logging.js +8 -4
  19. package/lib/nodejs/poll.js +10 -6
  20. package/lib/nodejs/random.js +18 -13
  21. package/lib/nodejs/sockets.js +203 -0
  22. package/package.json +1 -1
  23. package/types/exports/{HTTP.d.ts → http-incoming-handler.d.ts} +1 -1
  24. package/types/imports/cli-base-environment.d.ts +17 -0
  25. package/types/imports/{exit.d.ts → cli-base-exit.d.ts} +4 -1
  26. package/types/imports/{preopens.d.ts → cli-base-preopens.d.ts} +6 -9
  27. package/types/imports/cli-base-stderr.d.ts +5 -0
  28. package/types/imports/cli-base-stdin.d.ts +5 -0
  29. package/types/imports/cli-base-stdout.d.ts +5 -0
  30. package/types/imports/clocks-monotonic-clock.d.ts +24 -0
  31. package/types/imports/clocks-timezone.d.ts +71 -0
  32. package/types/imports/clocks-wall-clock.d.ts +31 -0
  33. package/types/imports/filesystem-filesystem.d.ts +857 -0
  34. package/types/imports/{default-outgoing-HTTP.d.ts → http-outgoing-handler.d.ts} +1 -1
  35. package/types/imports/{types.d.ts → http-types.d.ts} +28 -28
  36. package/types/imports/io-streams.d.ts +180 -0
  37. package/types/imports/logging-handler.d.ts +40 -0
  38. package/types/imports/poll-poll.d.ts +41 -0
  39. package/types/imports/random-insecure-seed.d.ts +22 -0
  40. package/types/imports/random-insecure.d.ts +20 -0
  41. package/types/imports/random-random.d.ts +22 -0
  42. package/types/imports/{instance-network.d.ts → sockets-instance-network.d.ts} +4 -1
  43. package/types/imports/sockets-ip-name-lookup.d.ts +76 -0
  44. package/types/imports/sockets-network.d.ts +212 -0
  45. package/types/imports/sockets-tcp-create-socket.d.ts +33 -0
  46. package/types/imports/sockets-tcp.d.ts +285 -0
  47. package/types/imports/sockets-udp-create-socket.d.ts +33 -0
  48. package/types/imports/sockets-udp.d.ts +219 -0
  49. package/types/wasi-command.d.ts +23 -0
  50. package/types/wasi-proxy.d.ts +11 -44
  51. package/types/wasi-reactor.d.ts +23 -70
  52. package/lib/browser/default-outgoing-HTTP.js +0 -3
  53. package/lib/browser/environment.js +0 -3
  54. package/lib/browser/exit.js +0 -21
  55. package/lib/browser/monotonic-clock.js +0 -35
  56. package/lib/browser/preopens.js +0 -11
  57. package/lib/browser/stderr.js +0 -4
  58. package/lib/browser/streams.js +0 -60
  59. package/lib/browser/timezone.js +0 -12
  60. package/lib/browser/types.js +0 -99
  61. package/lib/browser/wall-clock.js +0 -12
  62. package/lib/nodejs/default-outgoing-HTTP.js +0 -3
  63. package/lib/nodejs/environment.js +0 -7
  64. package/lib/nodejs/exit.js +0 -4
  65. package/lib/nodejs/monotonic-clock.js +0 -14
  66. package/lib/nodejs/preopens.js +0 -11
  67. package/lib/nodejs/stderr.js +0 -4
  68. package/lib/nodejs/streams.js +0 -63
  69. package/lib/nodejs/timezone.js +0 -12
  70. package/lib/nodejs/types.js +0 -99
  71. package/lib/nodejs/wall-clock.js +0 -12
  72. package/types/imports/console.d.ts +0 -17
  73. package/types/imports/environment.d.ts +0 -4
  74. package/types/imports/filesystem.d.ts +0 -212
  75. package/types/imports/ip-name-lookup.d.ts +0 -19
  76. package/types/imports/monotonic-clock.d.ts +0 -8
  77. package/types/imports/network.d.ts +0 -50
  78. package/types/imports/poll.d.ts +0 -5
  79. package/types/imports/random.d.ts +0 -5
  80. package/types/imports/streams.d.ts +0 -23
  81. package/types/imports/tcp-create-socket.d.ts +0 -9
  82. package/types/imports/tcp.d.ts +0 -52
  83. package/types/imports/timezone.d.ts +0 -13
  84. package/types/imports/udp-create-socket.d.ts +0 -9
  85. package/types/imports/udp.d.ts +0 -36
  86. package/types/imports/wall-clock.d.ts +0 -8
  87. package/types/index.d.ts +0 -8
@@ -0,0 +1,212 @@
1
+ export namespace SocketsNetwork {
2
+ /**
3
+ * Dispose of the specified `network`, after which it may no longer be used.
4
+ *
5
+ * Note: this function is scheduled to be removed when Resources are natively supported in Wit.
6
+ */
7
+ export function dropNetwork(this: Network): void;
8
+ }
9
+ /**
10
+ * An opaque resource that represents access to (a subset of) the network.
11
+ * This enables context-based security for networking.
12
+ * There is no need for this to map 1:1 to a physical network interface.
13
+ *
14
+ * FYI, In the future this will be replaced by handle types.
15
+ */
16
+ export type Network = number;
17
+ export type Ipv6Address = [number, number, number, number, number, number, number, number];
18
+ export interface Ipv6SocketAddress {
19
+ port: number,
20
+ flowInfo: number,
21
+ address: Ipv6Address,
22
+ scopeId: number,
23
+ }
24
+ export type Ipv4Address = [number, number, number, number];
25
+ export interface Ipv4SocketAddress {
26
+ port: number,
27
+ address: Ipv4Address,
28
+ }
29
+ export type IpSocketAddress = IpSocketAddressIpv4 | IpSocketAddressIpv6;
30
+ export interface IpSocketAddressIpv4 {
31
+ tag: 'ipv4',
32
+ val: Ipv4SocketAddress,
33
+ }
34
+ export interface IpSocketAddressIpv6 {
35
+ tag: 'ipv6',
36
+ val: Ipv6SocketAddress,
37
+ }
38
+ /**
39
+ * # Variants
40
+ *
41
+ * ## `"ipv4"`
42
+ *
43
+ * Similar to `AF_INET` in POSIX.
44
+ *
45
+ * ## `"ipv6"`
46
+ *
47
+ * Similar to `AF_INET6` in POSIX.
48
+ */
49
+ export type IpAddressFamily = 'ipv4' | 'ipv6';
50
+ export type IpAddress = IpAddressIpv4 | IpAddressIpv6;
51
+ export interface IpAddressIpv4 {
52
+ tag: 'ipv4',
53
+ val: Ipv4Address,
54
+ }
55
+ export interface IpAddressIpv6 {
56
+ tag: 'ipv6',
57
+ val: Ipv6Address,
58
+ }
59
+ /**
60
+ * Error codes.
61
+ *
62
+ * In theory, every API can return any error code.
63
+ * In practice, API's typically only return the errors documented per API
64
+ * combined with a couple of errors that are always possible:
65
+ * - `unknown`
66
+ * - `access-denied`
67
+ * - `not-supported`
68
+ * - `out-of-memory`
69
+ *
70
+ * See each individual API for what the POSIX equivalents are. They sometimes differ per API.
71
+ *
72
+ * # Variants
73
+ *
74
+ * ## `"unknown"`
75
+ *
76
+ * Unknown error
77
+ *
78
+ * ## `"access-denied"`
79
+ *
80
+ * Access denied.
81
+ *
82
+ * POSIX equivalent: EACCES, EPERM
83
+ *
84
+ * ## `"not-supported"`
85
+ *
86
+ * The operation is not supported.
87
+ *
88
+ * POSIX equivalent: EOPNOTSUPP
89
+ *
90
+ * ## `"out-of-memory"`
91
+ *
92
+ * Not enough memory to complete the operation.
93
+ *
94
+ * POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY
95
+ *
96
+ * ## `"timeout"`
97
+ *
98
+ * The operation timed out before it could finish completely.
99
+ *
100
+ * ## `"concurrency-conflict"`
101
+ *
102
+ * This operation is incompatible with another asynchronous operation that is already in progress.
103
+ *
104
+ * ## `"not-in-progress"`
105
+ *
106
+ * Trying to finish an asynchronous operation that:
107
+ * - has not been started yet, or:
108
+ * - was already finished by a previous `finish-*` call.
109
+ *
110
+ * Note: this is scheduled to be removed when `future`s are natively supported.
111
+ *
112
+ * ## `"would-block"`
113
+ *
114
+ * The operation has been aborted because it could not be completed immediately.
115
+ *
116
+ * Note: this is scheduled to be removed when `future`s are natively supported.
117
+ *
118
+ * ## `"address-family-not-supported"`
119
+ *
120
+ * The specified address-family is not supported.
121
+ *
122
+ * ## `"address-family-mismatch"`
123
+ *
124
+ * An IPv4 address was passed to an IPv6 resource, or vice versa.
125
+ *
126
+ * ## `"invalid-remote-address"`
127
+ *
128
+ * The socket address is not a valid remote address. E.g. the IP address is set to INADDR_ANY, or the port is set to 0.
129
+ *
130
+ * ## `"ipv4-only-operation"`
131
+ *
132
+ * The operation is only supported on IPv4 resources.
133
+ *
134
+ * ## `"ipv6-only-operation"`
135
+ *
136
+ * The operation is only supported on IPv6 resources.
137
+ *
138
+ * ## `"new-socket-limit"`
139
+ *
140
+ * A new socket resource could not be created because of a system limit.
141
+ *
142
+ * ## `"already-attached"`
143
+ *
144
+ * The socket is already attached to another network.
145
+ *
146
+ * ## `"already-bound"`
147
+ *
148
+ * The socket is already bound.
149
+ *
150
+ * ## `"already-connected"`
151
+ *
152
+ * The socket is already in the Connection state.
153
+ *
154
+ * ## `"not-bound"`
155
+ *
156
+ * The socket is not bound to any local address.
157
+ *
158
+ * ## `"not-connected"`
159
+ *
160
+ * The socket is not in the Connection state.
161
+ *
162
+ * ## `"address-not-bindable"`
163
+ *
164
+ * A bind operation failed because the provided address is not an address that the `network` can bind to.
165
+ *
166
+ * ## `"address-in-use"`
167
+ *
168
+ * A bind operation failed because the provided address is already in use.
169
+ *
170
+ * ## `"ephemeral-ports-exhausted"`
171
+ *
172
+ * A bind operation failed because there are no ephemeral ports available.
173
+ *
174
+ * ## `"remote-unreachable"`
175
+ *
176
+ * The remote address is not reachable
177
+ *
178
+ * ## `"already-listening"`
179
+ *
180
+ * The socket is already in the Listener state.
181
+ *
182
+ * ## `"not-listening"`
183
+ *
184
+ * The socket is already in the Listener state.
185
+ *
186
+ * ## `"connection-refused"`
187
+ *
188
+ * The connection was forcefully rejected
189
+ *
190
+ * ## `"connection-reset"`
191
+ *
192
+ * The connection was reset.
193
+ *
194
+ * ## `"datagram-too-large"`
195
+ *
196
+ * ## `"invalid-name"`
197
+ *
198
+ * The provided name is a syntactically invalid domain name.
199
+ *
200
+ * ## `"name-unresolvable"`
201
+ *
202
+ * Name does not exist or has no suitable associated IP addresses.
203
+ *
204
+ * ## `"temporary-resolver-failure"`
205
+ *
206
+ * A temporary failure in name resolution occurred.
207
+ *
208
+ * ## `"permanent-resolver-failure"`
209
+ *
210
+ * A permanent failure in name resolution occurred.
211
+ */
212
+ export type ErrorCode = 'unknown' | 'access-denied' | 'not-supported' | 'out-of-memory' | 'timeout' | 'concurrency-conflict' | 'not-in-progress' | 'would-block' | 'address-family-not-supported' | 'address-family-mismatch' | 'invalid-remote-address' | 'ipv4-only-operation' | 'ipv6-only-operation' | 'new-socket-limit' | 'already-attached' | 'already-bound' | 'already-connected' | 'not-bound' | 'not-connected' | 'address-not-bindable' | 'address-in-use' | 'ephemeral-ports-exhausted' | 'remote-unreachable' | 'already-listening' | 'not-listening' | 'connection-refused' | 'connection-reset' | 'datagram-too-large' | 'invalid-name' | 'name-unresolvable' | 'temporary-resolver-failure' | 'permanent-resolver-failure';
@@ -0,0 +1,33 @@
1
+ export namespace SocketsTcpCreateSocket {
2
+ /**
3
+ * Create a new TCP socket.
4
+ *
5
+ * Similar to `socket(AF_INET or AF_INET6, SOCK_STREAM, IPPROTO_TCP)` 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`/`listen`/`connect`
9
+ * is called, 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 TCP 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 createTcpSocket(addressFamily: IpAddressFamily): TcpSocket;
25
+ }
26
+ import type { Network } from '../imports/network';
27
+ export { Network };
28
+ import type { ErrorCode } from '../imports/network';
29
+ export { ErrorCode };
30
+ import type { IpAddressFamily } from '../imports/network';
31
+ export { IpAddressFamily };
32
+ import type { TcpSocket } from '../imports/tcp';
33
+ export { TcpSocket };
@@ -0,0 +1,285 @@
1
+ export namespace SocketsTcp {
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
+ export function finishConnect(this: TcpSocket): [InputStream, OutputStream];
67
+ /**
68
+ * Start listening for new connections.
69
+ *
70
+ * Transitions the socket into the Listener state.
71
+ *
72
+ * Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
73
+ *
74
+ * # Typical `start` errors
75
+ * - `already-attached`: The socket is already attached to a different network. The `network` passed to `listen` must be identical to the one passed to `bind`.
76
+ * - `already-connected`: The socket is already in the Connection state. (EISCONN, EINVAL on BSD)
77
+ * - `already-listening`: The socket is already in the Listener state.
78
+ * - `concurrency-conflict`: Another `bind`, `connect` or `listen` operation is already in progress. (EINVAL on BSD)
79
+ *
80
+ * # Typical `finish` errors
81
+ * - `ephemeral-ports-exhausted`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
82
+ * - `not-in-progress`: A `listen` operation is not in progress.
83
+ * - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
84
+ *
85
+ * # References
86
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
87
+ * - <https://man7.org/linux/man-pages/man2/listen.2.html>
88
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
89
+ * - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
90
+ */
91
+ export function startListen(this: TcpSocket, network: Network): void;
92
+ export function finishListen(this: TcpSocket): void;
93
+ /**
94
+ * Accept a new client socket.
95
+ *
96
+ * The returned socket is bound and in the Connection state.
97
+ *
98
+ * On success, this function returns the newly accepted client socket along with
99
+ * a pair of streams that can be used to read & write to the connection.
100
+ *
101
+ * # Typical errors
102
+ * - `not-listening`: Socket is not in the Listener state. (EINVAL)
103
+ * - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
104
+ *
105
+ * Host implementations must skip over transient errors returned by the native accept syscall.
106
+ *
107
+ * # References
108
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
109
+ * - <https://man7.org/linux/man-pages/man2/accept.2.html>
110
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
111
+ * - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
112
+ */
113
+ export function accept(this: TcpSocket): [TcpSocket, InputStream, OutputStream];
114
+ /**
115
+ * Get the bound local address.
116
+ *
117
+ * # Typical errors
118
+ * - `not-bound`: The socket is not bound to any local address.
119
+ *
120
+ * # References
121
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
122
+ * - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
123
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
124
+ * - <https://man.freebsd.org/cgi/man.cgi?getsockname>
125
+ */
126
+ export function localAddress(this: TcpSocket): IpSocketAddress;
127
+ /**
128
+ * Get the bound remote address.
129
+ *
130
+ * # Typical errors
131
+ * - `not-connected`: The socket is not connected to a remote address. (ENOTCONN)
132
+ *
133
+ * # References
134
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
135
+ * - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
136
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
137
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
138
+ */
139
+ export function remoteAddress(this: TcpSocket): IpSocketAddress;
140
+ /**
141
+ * Whether this is a IPv4 or IPv6 socket.
142
+ *
143
+ * Equivalent to the SO_DOMAIN socket option.
144
+ */
145
+ export function addressFamily(this: TcpSocket): IpAddressFamily;
146
+ /**
147
+ * Whether IPv4 compatibility (dual-stack) mode is disabled or not.
148
+ *
149
+ * Equivalent to the IPV6_V6ONLY socket option.
150
+ *
151
+ * # Typical errors
152
+ * - `ipv6-only-operation`: (get/set) `this` socket is an IPv4 socket.
153
+ * - `already-bound`: (set) The socket is already bound.
154
+ * - `not-supported`: (set) Host does not support dual-stack sockets. (Implementations are not required to.)
155
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
156
+ */
157
+ export function ipv6Only(this: TcpSocket): boolean;
158
+ export function setIpv6Only(this: TcpSocket, value: boolean): void;
159
+ /**
160
+ * Hints the desired listen queue size. Implementations are free to ignore this.
161
+ *
162
+ * # Typical errors
163
+ * - `already-connected`: (set) The socket is already in the Connection state.
164
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
165
+ */
166
+ export function setListenBacklogSize(this: TcpSocket, value: bigint): void;
167
+ /**
168
+ * Equivalent to the SO_KEEPALIVE socket option.
169
+ *
170
+ * # Typical errors
171
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
172
+ */
173
+ export function keepAlive(this: TcpSocket): boolean;
174
+ export function setKeepAlive(this: TcpSocket, value: boolean): void;
175
+ /**
176
+ * Equivalent to the TCP_NODELAY socket option.
177
+ *
178
+ * # Typical errors
179
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
180
+ */
181
+ export function noDelay(this: TcpSocket): boolean;
182
+ export function setNoDelay(this: TcpSocket, value: boolean): void;
183
+ /**
184
+ * Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
185
+ *
186
+ * # Typical errors
187
+ * - `already-connected`: (set) The socket is already in the Connection state.
188
+ * - `already-listening`: (set) The socket is already in the Listener state.
189
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
190
+ */
191
+ export function unicastHopLimit(this: TcpSocket): number;
192
+ export function setUnicastHopLimit(this: TcpSocket, value: number): void;
193
+ /**
194
+ * The kernel buffer space reserved for sends/receives on this socket.
195
+ *
196
+ * Note #1: an implementation may choose to cap or round the buffer size when setting the value.
197
+ * In other words, after setting a value, reading the same setting back may return a different value.
198
+ *
199
+ * Note #2: there is not necessarily a direct relationship between the kernel buffer size and the bytes of
200
+ * actual data to be sent/received by the application, because the kernel might also use the buffer space
201
+ * for internal metadata structures.
202
+ *
203
+ * Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
204
+ *
205
+ * # Typical errors
206
+ * - `already-connected`: (set) The socket is already in the Connection state.
207
+ * - `already-listening`: (set) The socket is already in the Listener state.
208
+ * - `concurrency-conflict`: (set) A `bind`, `connect` or `listen` operation is already in progress. (EALREADY)
209
+ */
210
+ export function receiveBufferSize(this: TcpSocket): bigint;
211
+ export function setReceiveBufferSize(this: TcpSocket, value: bigint): void;
212
+ export function sendBufferSize(this: TcpSocket): bigint;
213
+ export function setSendBufferSize(this: TcpSocket, value: bigint): void;
214
+ /**
215
+ * Create a `pollable` which will resolve once the socket is ready for I/O.
216
+ *
217
+ * Note: this function is here for WASI Preview2 only.
218
+ * It's planned to be removed when `future` is natively supported in Preview3.
219
+ */
220
+ export function subscribe(this: TcpSocket): Pollable;
221
+ /**
222
+ * Initiate a graceful shutdown.
223
+ *
224
+ * - receive: the socket is not expecting to receive any more data from the peer. All subsequent read
225
+ * operations on the `input-stream` associated with this socket will return an End Of Stream indication.
226
+ * Any data still in the receive queue at time of calling `shutdown` will be discarded.
227
+ * - send: the socket is not expecting to send any more data to the peer. All subsequent write
228
+ * operations on the `output-stream` associated with this socket will return an error.
229
+ * - both: same effect as receive & send combined.
230
+ *
231
+ * The shutdown function does not close (drop) the socket.
232
+ *
233
+ * # Typical errors
234
+ * - `not-connected`: The socket is not in the Connection state. (ENOTCONN)
235
+ *
236
+ * # References
237
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
238
+ * - <https://man7.org/linux/man-pages/man2/shutdown.2.html>
239
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown>
240
+ * - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
241
+ */
242
+ export function shutdown(this: TcpSocket, shutdownType: ShutdownType): void;
243
+ /**
244
+ * Dispose of the specified `tcp-socket`, after which it may no longer be used.
245
+ *
246
+ * Similar to the POSIX `close` function.
247
+ *
248
+ * Note: this function is scheduled to be removed when Resources are natively supported in Wit.
249
+ */
250
+ export function dropTcpSocket(this: TcpSocket): void;
251
+ }
252
+ import type { InputStream } from '../imports/streams';
253
+ export { InputStream };
254
+ import type { OutputStream } from '../imports/streams';
255
+ export { OutputStream };
256
+ import type { Pollable } from '../imports/poll';
257
+ export { Pollable };
258
+ import type { Network } from '../imports/network';
259
+ export { Network };
260
+ import type { ErrorCode } from '../imports/network';
261
+ export { ErrorCode };
262
+ import type { IpSocketAddress } from '../imports/network';
263
+ export { IpSocketAddress };
264
+ import type { IpAddressFamily } from '../imports/network';
265
+ export { IpAddressFamily };
266
+ /**
267
+ * A TCP socket handle.
268
+ */
269
+ export type TcpSocket = number;
270
+ /**
271
+ * # Variants
272
+ *
273
+ * ## `"receive"`
274
+ *
275
+ * Similar to `SHUT_RD` in POSIX.
276
+ *
277
+ * ## `"send"`
278
+ *
279
+ * Similar to `SHUT_WR` in POSIX.
280
+ *
281
+ * ## `"both"`
282
+ *
283
+ * Similar to `SHUT_RDWR` in POSIX.
284
+ */
285
+ export type ShutdownType = 'receive' | 'send' | 'both';
@@ -0,0 +1,33 @@
1
+ export namespace SocketsUdpCreateSocket {
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 '../imports/network';
27
+ export { Network };
28
+ import type { ErrorCode } from '../imports/network';
29
+ export { ErrorCode };
30
+ import type { IpAddressFamily } from '../imports/network';
31
+ export { IpAddressFamily };
32
+ import type { UdpSocket } from '../imports/udp';
33
+ export { UdpSocket };