@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,274 @@
1
+ export namespace WasiIoStreams {
2
+ /**
3
+ * Perform a non-blocking read from the stream.
4
+ *
5
+ * This function returns a list of bytes containing the data that was
6
+ * read, along with a `stream-status` which, indicates whether further
7
+ * reads are expected to produce data. The returned list will contain up to
8
+ * `len` bytes; it may return fewer than requested, but not more. An
9
+ * empty list and `stream-status:open` indicates no more data is
10
+ * available at this time, and that the pollable given by
11
+ * `subscribe-to-input-stream` will be ready when more data is available.
12
+ *
13
+ * Once a stream has reached the end, subsequent calls to `read` or
14
+ * `skip` will always report `stream-status:ended` rather than producing more
15
+ * data.
16
+ *
17
+ * When the caller gives a `len` of 0, it represents a request to read 0
18
+ * bytes. This read should always succeed and return an empty list and
19
+ * the current `stream-status`.
20
+ *
21
+ * The `len` parameter is a `u64`, which could represent a list of u8 which
22
+ * is not possible to allocate in wasm32, or not desirable to allocate as
23
+ * as a return value by the callee. The callee may return a list of bytes
24
+ * less than `len` in size while more bytes are available for reading.
25
+ */
26
+ export function read(this_: InputStream, len: bigint): [Uint8Array, StreamStatus];
27
+ /**
28
+ * Read bytes from a stream, after blocking until at least one byte can
29
+ * be read. Except for blocking, identical to `read`.
30
+ */
31
+ export function blockingRead(this_: InputStream, len: bigint): [Uint8Array, StreamStatus];
32
+ /**
33
+ * Skip bytes from a stream.
34
+ *
35
+ * This is similar to the `read` function, but avoids copying the
36
+ * bytes into the instance.
37
+ *
38
+ * Once a stream has reached the end, subsequent calls to read or
39
+ * `skip` will always report end-of-stream rather than producing more
40
+ * data.
41
+ *
42
+ * This function returns the number of bytes skipped, along with a
43
+ * `stream-status` indicating whether the end of the stream was
44
+ * reached. The returned value will be at most `len`; it may be less.
45
+ */
46
+ export function skip(this_: InputStream, len: bigint): [bigint, StreamStatus];
47
+ /**
48
+ * Skip bytes from a stream, after blocking until at least one byte
49
+ * can be skipped. Except for blocking behavior, identical to `skip`.
50
+ */
51
+ export function blockingSkip(this_: InputStream, len: bigint): [bigint, StreamStatus];
52
+ /**
53
+ * Create a `pollable` which will resolve once either the specified stream
54
+ * has bytes available to read or the other end of the stream has been
55
+ * closed.
56
+ * The created `pollable` is a child resource of the `input-stream`.
57
+ * Implementations may trap if the `input-stream` is dropped before
58
+ * all derived `pollable`s created with this function are dropped.
59
+ */
60
+ export function subscribeToInputStream(this_: InputStream): Pollable;
61
+ /**
62
+ * Dispose of the specified `input-stream`, after which it may no longer
63
+ * be used.
64
+ * Implementations may trap if this `input-stream` is dropped while child
65
+ * `pollable` resources are still alive.
66
+ * After this `input-stream` is dropped, implementations may report any
67
+ * corresponding `output-stream` has `stream-state.closed`.
68
+ */
69
+ export function dropInputStream(this_: InputStream): void;
70
+ /**
71
+ * Check readiness for writing. This function never blocks.
72
+ *
73
+ * Returns the number of bytes permitted for the next call to `write`,
74
+ * or an error. Calling `write` with more bytes than this function has
75
+ * permitted will trap.
76
+ *
77
+ * When this function returns 0 bytes, the `subscribe-to-output-stream`
78
+ * pollable will become ready when this function will report at least
79
+ * 1 byte, or an error.
80
+ */
81
+ export function checkWrite(this_: OutputStream): bigint;
82
+ /**
83
+ * Perform a write. This function never blocks.
84
+ *
85
+ * Precondition: check-write gave permit of Ok(n) and contents has a
86
+ * length of less than or equal to n. Otherwise, this function will trap.
87
+ *
88
+ * returns Err(closed) without writing if the stream has closed since
89
+ * the last call to check-write provided a permit.
90
+ */
91
+ export function write(this_: OutputStream, contents: Uint8Array): void;
92
+ /**
93
+ * Perform a write of up to 4096 bytes, and then flush the stream. Block
94
+ * until all of these operations are complete, or an error occurs.
95
+ *
96
+ * This is a convenience wrapper around the use of `check-write`,
97
+ * `subscribe-to-output-stream`, `write`, and `flush`, and is implemented
98
+ * with the following pseudo-code:
99
+ *
100
+ * ```text
101
+ * let pollable = subscribe-to-output-stream(this);
102
+ * while !contents.is_empty() {
103
+ * // Wait for the stream to become writable
104
+ * poll-oneoff(pollable);
105
+ * let Ok(n) = check-write(this); // eliding error handling
106
+ * let len = min(n, contents.len());
107
+ * let (chunk, rest) = contents.split_at(len);
108
+ * write(this, chunk); // eliding error handling
109
+ * contents = rest;
110
+ * }
111
+ * flush(this);
112
+ * // Wait for completion of `flush`
113
+ * poll-oneoff(pollable);
114
+ * // Check for any errors that arose during `flush`
115
+ * let _ = check-write(this); // eliding error handling
116
+ * ```
117
+ */
118
+ export function blockingWriteAndFlush(this_: OutputStream, contents: Uint8Array): void;
119
+ /**
120
+ * Request to flush buffered output. This function never blocks.
121
+ *
122
+ * This tells the output-stream that the caller intends any buffered
123
+ * output to be flushed. the output which is expected to be flushed
124
+ * is all that has been passed to `write` prior to this call.
125
+ *
126
+ * Upon calling this function, the `output-stream` will not accept any
127
+ * writes (`check-write` will return `ok(0)`) until the flush has
128
+ * completed. The `subscribe-to-output-stream` pollable will become ready
129
+ * when the flush has completed and the stream can accept more writes.
130
+ */
131
+ export function flush(this_: OutputStream): void;
132
+ /**
133
+ * Request to flush buffered output, and block until flush completes
134
+ * and stream is ready for writing again.
135
+ */
136
+ export function blockingFlush(this_: OutputStream): void;
137
+ /**
138
+ * Create a `pollable` which will resolve once the output-stream
139
+ * is ready for more writing, or an error has occured. When this
140
+ * pollable is ready, `check-write` will return `ok(n)` with n>0, or an
141
+ * error.
142
+ *
143
+ * If the stream is closed, this pollable is always ready immediately.
144
+ *
145
+ * The created `pollable` is a child resource of the `output-stream`.
146
+ * Implementations may trap if the `output-stream` is dropped before
147
+ * all derived `pollable`s created with this function are dropped.
148
+ */
149
+ export function subscribeToOutputStream(this_: OutputStream): Pollable;
150
+ /**
151
+ * Write zeroes to a stream.
152
+ *
153
+ * this should be used precisely like `write` with the exact same
154
+ * preconditions (must use check-write first), but instead of
155
+ * passing a list of bytes, you simply pass the number of zero-bytes
156
+ * that should be written.
157
+ */
158
+ export function writeZeroes(this_: OutputStream, len: bigint): void;
159
+ /**
160
+ * Read from one stream and write to another.
161
+ *
162
+ * This function returns the number of bytes transferred; it may be less
163
+ * than `len`.
164
+ *
165
+ * Unlike other I/O functions, this function blocks until all the data
166
+ * read from the input stream has been written to the output stream.
167
+ */
168
+ export function splice(this_: OutputStream, src: InputStream, len: bigint): [bigint, StreamStatus];
169
+ /**
170
+ * Read from one stream and write to another, with blocking.
171
+ *
172
+ * This is similar to `splice`, except that it blocks until at least
173
+ * one byte can be read.
174
+ */
175
+ export function blockingSplice(this_: OutputStream, src: InputStream, len: bigint): [bigint, StreamStatus];
176
+ /**
177
+ * Forward the entire contents of an input stream to an output stream.
178
+ *
179
+ * This function repeatedly reads from the input stream and writes
180
+ * the data to the output stream, until the end of the input stream
181
+ * is reached, or an error is encountered.
182
+ *
183
+ * Unlike other I/O functions, this function blocks until the end
184
+ * of the input stream is seen and all the data has been written to
185
+ * the output stream.
186
+ *
187
+ * This function returns the number of bytes transferred, and the status of
188
+ * the output stream.
189
+ */
190
+ export function forward(this_: OutputStream, src: InputStream): [bigint, StreamStatus];
191
+ /**
192
+ * Dispose of the specified `output-stream`, after which it may no longer
193
+ * be used.
194
+ * Implementations may trap if this `output-stream` is dropped while
195
+ * child `pollable` resources are still alive.
196
+ * After this `output-stream` is dropped, implementations may report any
197
+ * corresponding `input-stream` has `stream-state.closed`.
198
+ */
199
+ export function dropOutputStream(this_: OutputStream): void;
200
+ }
201
+ import type { Pollable } from '../interfaces/wasi-poll-poll';
202
+ export { Pollable };
203
+ /**
204
+ * Streams provide a sequence of data and then end; once they end, they
205
+ * no longer provide any further data.
206
+ *
207
+ * For example, a stream reading from a file ends when the stream reaches
208
+ * the end of the file. For another example, a stream reading from a
209
+ * socket ends when the socket is closed.
210
+ * # Variants
211
+ *
212
+ * ## `"open"`
213
+ *
214
+ * The stream is open and may produce further data.
215
+ * ## `"ended"`
216
+ *
217
+ * When reading, this indicates that the stream will not produce
218
+ * further data.
219
+ * When writing, this indicates that the stream will no longer be read.
220
+ * Further writes are still permitted.
221
+ */
222
+ export type StreamStatus = 'open' | 'ended';
223
+ /**
224
+ * An input bytestream. In the future, this will be replaced by handle
225
+ * types.
226
+ *
227
+ * `input-stream`s are *non-blocking* to the extent practical on underlying
228
+ * platforms. I/O operations always return promptly; if fewer bytes are
229
+ * promptly available than requested, they return the number of bytes promptly
230
+ * available, which could even be zero. To wait for data to be available,
231
+ * use the `subscribe-to-input-stream` function to obtain a `pollable` which
232
+ * can be polled for using `wasi:poll/poll.poll_oneoff`.
233
+ *
234
+ * And at present, it is a `u32` instead of being an actual handle, until
235
+ * the wit-bindgen implementation of handles and resources is ready.
236
+ *
237
+ * This [represents a resource](https://github.com/WebAssembly/WASI/blob/main/docs/WitInWasi.md#Resources).
238
+ */
239
+ export type InputStream = number;
240
+ /**
241
+ * An output bytestream. In the future, this will be replaced by handle
242
+ * types.
243
+ *
244
+ * `output-stream`s are *non-blocking* to the extent practical on
245
+ * underlying platforms. Except where specified otherwise, I/O operations also
246
+ * always return promptly, after the number of bytes that can be written
247
+ * promptly, which could even be zero. To wait for the stream to be ready to
248
+ * accept data, the `subscribe-to-output-stream` function to obtain a
249
+ * `pollable` which can be polled for using `wasi:poll`.
250
+ *
251
+ * And at present, it is a `u32` instead of being an actual handle, until
252
+ * the wit-bindgen implementation of handles and resources is ready.
253
+ *
254
+ * This [represents a resource](https://github.com/WebAssembly/WASI/blob/main/docs/WitInWasi.md#Resources).
255
+ */
256
+ export type OutputStream = number;
257
+ /**
258
+ * An error for output-stream operations.
259
+ *
260
+ * Contrary to input-streams, a closed output-stream is reported using
261
+ * an error.
262
+ * # Variants
263
+ *
264
+ * ## `"last-operation-failed"`
265
+ *
266
+ * The last operation (a write or flush) failed before completion.
267
+ * ## `"closed"`
268
+ *
269
+ * The stream is closed: no more input will be accepted by the
270
+ * stream. A closed output-stream will return this error on all
271
+ * future operations.
272
+ */
273
+ export type WriteError = 'last-operation-failed' | 'closed';
274
+
@@ -0,0 +1,39 @@
1
+ export namespace WasiPollPoll {
2
+ /**
3
+ * Dispose of the specified `pollable`, after which it may no longer
4
+ * be used.
5
+ */
6
+ export function dropPollable(this_: Pollable): void;
7
+ /**
8
+ * Poll for completion on a set of pollables.
9
+ *
10
+ * The "oneoff" in the name refers to the fact that this function must do a
11
+ * linear scan through the entire list of subscriptions, which may be
12
+ * inefficient if the number is large and the same subscriptions are used
13
+ * many times. In the future, this is expected to be obsoleted by the
14
+ * component model async proposal, which will include a scalable waiting
15
+ * facility.
16
+ *
17
+ * The result list<bool> is the same length as the argument
18
+ * list<pollable>, and indicates the readiness of each corresponding
19
+ * element in that / list, with true indicating ready.
20
+ */
21
+ export function pollOneoff(in_: Uint32Array): boolean[];
22
+ }
23
+ /**
24
+ * A "pollable" handle.
25
+ *
26
+ * This is conceptually represents a `stream<_, _>`, or in other words,
27
+ * a stream that one can wait on, repeatedly, but which does not itself
28
+ * produce any data. It's temporary scaffolding until component-model's
29
+ * async features are ready.
30
+ *
31
+ * And at present, it is a `u32` instead of being an actual handle, until
32
+ * the wit-bindgen implementation of handles and resources is ready.
33
+ *
34
+ * `pollable` lifetimes are not automatically managed. Users must ensure
35
+ * that they do not outlive the resource they reference.
36
+ *
37
+ * This [represents a resource](https://github.com/WebAssembly/WASI/blob/main/docs/WitInWasi.md#Resources).
38
+ */
39
+ export type Pollable = number;
@@ -0,0 +1,22 @@
1
+ export namespace WasiRandomInsecureSeed {
2
+ /**
3
+ * Return a 128-bit value that may contain a pseudo-random value.
4
+ *
5
+ * The returned value is not required to be computed from a CSPRNG, and may
6
+ * even be entirely deterministic. Host implementations are encouraged to
7
+ * provide pseudo-random values to any program exposed to
8
+ * attacker-controlled content, to enable DoS protection built into many
9
+ * languages' hash-map implementations.
10
+ *
11
+ * This function is intended to only be called once, by a source language
12
+ * to initialize Denial Of Service (DoS) protection in its hash-map
13
+ * implementation.
14
+ *
15
+ * # Expected future evolution
16
+ *
17
+ * This will likely be changed to a value import, to prevent it from being
18
+ * called multiple times and potentially used for purposes other than DoS
19
+ * protection.
20
+ */
21
+ export function insecureSeed(): [bigint, bigint];
22
+ }
@@ -0,0 +1,20 @@
1
+ export namespace WasiRandomInsecure {
2
+ /**
3
+ * Return `len` insecure pseudo-random bytes.
4
+ *
5
+ * This function is not cryptographically secure. Do not use it for
6
+ * anything related to security.
7
+ *
8
+ * There are no requirements on the values of the returned bytes, however
9
+ * implementations are encouraged to return evenly distributed values with
10
+ * a long period.
11
+ */
12
+ export function getInsecureRandomBytes(len: bigint): Uint8Array;
13
+ /**
14
+ * Return an insecure pseudo-random `u64` value.
15
+ *
16
+ * This function returns the same type of pseudo-random data as
17
+ * `get-insecure-random-bytes`, represented as a `u64`.
18
+ */
19
+ export function getInsecureRandomU64(): bigint;
20
+ }
@@ -0,0 +1,22 @@
1
+ export namespace WasiRandomRandom {
2
+ /**
3
+ * Return `len` cryptographically-secure pseudo-random bytes.
4
+ *
5
+ * This function must produce data from an adequately seeded
6
+ * cryptographically-secure pseudo-random number generator (CSPRNG), so it
7
+ * must not block, from the perspective of the calling program, and the
8
+ * returned data is always unpredictable.
9
+ *
10
+ * This function must always return fresh pseudo-random data. Deterministic
11
+ * environments must omit this function, rather than implementing it with
12
+ * deterministic data.
13
+ */
14
+ export function getRandomBytes(len: bigint): Uint8Array;
15
+ /**
16
+ * Return a cryptographically-secure pseudo-random `u64` value.
17
+ *
18
+ * This function returns the same type of pseudo-random data as
19
+ * `get-random-bytes`, represented as a `u64`.
20
+ */
21
+ export function getRandomU64(): bigint;
22
+ }
@@ -0,0 +1,8 @@
1
+ export namespace WasiSocketsInstanceNetwork {
2
+ /**
3
+ * Get a handle to the default network.
4
+ */
5
+ export function instanceNetwork(): Network;
6
+ }
7
+ import type { Network } from '../interfaces/wasi-sockets-network';
8
+ export { Network };
@@ -0,0 +1,76 @@
1
+ export namespace WasiSocketsIpNameLookup {
2
+ /**
3
+ * Resolve an internet host name to a list of IP addresses.
4
+ *
5
+ * See the wasi-socket proposal README.md for a comparison with getaddrinfo.
6
+ *
7
+ * # Parameters
8
+ * - `name`: The name to look up. IP addresses are not allowed. Unicode domain names are automatically converted
9
+ * to ASCII using IDNA encoding.
10
+ * - `address-family`: If provided, limit the results to addresses of this specific address family.
11
+ * - `include-unavailable`: When set to true, this function will also return addresses of which the runtime
12
+ * thinks (or knows) can't be connected to at the moment. For example, this will return IPv6 addresses on
13
+ * systems without an active IPv6 interface. Notes:
14
+ * - Even when no public IPv6 interfaces are present or active, names like "localhost" can still resolve to an IPv6 address.
15
+ * - Whatever is "available" or "unavailable" is volatile and can change everytime a network cable is unplugged.
16
+ *
17
+ * This function never blocks. It either immediately fails or immediately returns successfully with a `resolve-address-stream`
18
+ * that can be used to (asynchronously) fetch the results.
19
+ *
20
+ * At the moment, the stream never completes successfully with 0 items. Ie. the first call
21
+ * to `resolve-next-address` never returns `ok(none)`. This may change in the future.
22
+ *
23
+ * # Typical errors
24
+ * - `invalid-name`: `name` is a syntactically invalid domain name.
25
+ * - `invalid-name`: `name` is an IP address.
26
+ * - `address-family-not-supported`: The specified `address-family` is not supported. (EAI_FAMILY)
27
+ *
28
+ * # References:
29
+ * - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html>
30
+ * - <https://man7.org/linux/man-pages/man3/getaddrinfo.3.html>
31
+ * - <https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo>
32
+ * - <https://man.freebsd.org/cgi/man.cgi?query=getaddrinfo&sektion=3>
33
+ */
34
+ export function resolveAddresses(network: Network, name: string, addressFamily: IpAddressFamily | undefined, includeUnavailable: boolean): ResolveAddressStream;
35
+ /**
36
+ * Returns the next address from the resolver.
37
+ *
38
+ * This function should be called multiple times. On each call, it will
39
+ * return the next address in connection order preference. If all
40
+ * addresses have been exhausted, this function returns `none`.
41
+ * After which, you should release the stream with `drop-resolve-address-stream`.
42
+ *
43
+ * This function never returns IPv4-mapped IPv6 addresses.
44
+ *
45
+ * # Typical errors
46
+ * - `name-unresolvable`: Name does not exist or has no suitable associated IP addresses. (EAI_NONAME, EAI_NODATA, EAI_ADDRFAMILY)
47
+ * - `temporary-resolver-failure`: A temporary failure in name resolution occurred. (EAI_AGAIN)
48
+ * - `permanent-resolver-failure`: A permanent failure in name resolution occurred. (EAI_FAIL)
49
+ * - `would-block`: A result is not available yet. (EWOULDBLOCK, EAGAIN)
50
+ */
51
+ export function resolveNextAddress(this_: ResolveAddressStream): IpAddress | undefined;
52
+ /**
53
+ * Dispose of the specified `resolve-address-stream`, after which it may no longer be used.
54
+ *
55
+ * Note: this function is scheduled to be removed when Resources are natively supported in Wit.
56
+ */
57
+ export function dropResolveAddressStream(this_: ResolveAddressStream): void;
58
+ /**
59
+ * Create a `pollable` which will resolve once the stream is ready for I/O.
60
+ *
61
+ * Note: this function is here for WASI Preview2 only.
62
+ * It's planned to be removed when `future` is natively supported in Preview3.
63
+ */
64
+ export function subscribe(this_: ResolveAddressStream): Pollable;
65
+ }
66
+ import type { Pollable } from '../interfaces/wasi-poll-poll';
67
+ export { Pollable };
68
+ import type { Network } from '../interfaces/wasi-sockets-network';
69
+ export { Network };
70
+ import type { ErrorCode } from '../interfaces/wasi-sockets-network';
71
+ export { ErrorCode };
72
+ import type { IpAddress } from '../interfaces/wasi-sockets-network';
73
+ export { IpAddress };
74
+ import type { IpAddressFamily } from '../interfaces/wasi-sockets-network';
75
+ export { IpAddressFamily };
76
+ export type ResolveAddressStream = number;
@@ -0,0 +1,180 @@
1
+ export namespace WasiSocketsNetwork {
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
+ /**
18
+ * Error codes.
19
+ *
20
+ * In theory, every API can return any error code.
21
+ * In practice, API's typically only return the errors documented per API
22
+ * combined with a couple of errors that are always possible:
23
+ * - `unknown`
24
+ * - `access-denied`
25
+ * - `not-supported`
26
+ * - `out-of-memory`
27
+ *
28
+ * See each individual API for what the POSIX equivalents are. They sometimes differ per API.
29
+ * # Variants
30
+ *
31
+ * ## `"unknown"`
32
+ *
33
+ * Unknown error
34
+ * ## `"access-denied"`
35
+ *
36
+ * Access denied.
37
+ *
38
+ * POSIX equivalent: EACCES, EPERM
39
+ * ## `"not-supported"`
40
+ *
41
+ * The operation is not supported.
42
+ *
43
+ * POSIX equivalent: EOPNOTSUPP
44
+ * ## `"out-of-memory"`
45
+ *
46
+ * Not enough memory to complete the operation.
47
+ *
48
+ * POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY
49
+ * ## `"timeout"`
50
+ *
51
+ * The operation timed out before it could finish completely.
52
+ * ## `"concurrency-conflict"`
53
+ *
54
+ * This operation is incompatible with another asynchronous operation that is already in progress.
55
+ * ## `"not-in-progress"`
56
+ *
57
+ * Trying to finish an asynchronous operation that:
58
+ * - has not been started yet, or:
59
+ * - was already finished by a previous `finish-*` call.
60
+ *
61
+ * Note: this is scheduled to be removed when `future`s are natively supported.
62
+ * ## `"would-block"`
63
+ *
64
+ * The operation has been aborted because it could not be completed immediately.
65
+ *
66
+ * Note: this is scheduled to be removed when `future`s are natively supported.
67
+ * ## `"address-family-not-supported"`
68
+ *
69
+ * The specified address-family is not supported.
70
+ * ## `"address-family-mismatch"`
71
+ *
72
+ * An IPv4 address was passed to an IPv6 resource, or vice versa.
73
+ * ## `"invalid-remote-address"`
74
+ *
75
+ * 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.
76
+ * ## `"ipv4-only-operation"`
77
+ *
78
+ * The operation is only supported on IPv4 resources.
79
+ * ## `"ipv6-only-operation"`
80
+ *
81
+ * The operation is only supported on IPv6 resources.
82
+ * ## `"new-socket-limit"`
83
+ *
84
+ * A new socket resource could not be created because of a system limit.
85
+ * ## `"already-attached"`
86
+ *
87
+ * The socket is already attached to another network.
88
+ * ## `"already-bound"`
89
+ *
90
+ * The socket is already bound.
91
+ * ## `"already-connected"`
92
+ *
93
+ * The socket is already in the Connection state.
94
+ * ## `"not-bound"`
95
+ *
96
+ * The socket is not bound to any local address.
97
+ * ## `"not-connected"`
98
+ *
99
+ * The socket is not in the Connection state.
100
+ * ## `"address-not-bindable"`
101
+ *
102
+ * A bind operation failed because the provided address is not an address that the `network` can bind to.
103
+ * ## `"address-in-use"`
104
+ *
105
+ * A bind operation failed because the provided address is already in use.
106
+ * ## `"ephemeral-ports-exhausted"`
107
+ *
108
+ * A bind operation failed because there are no ephemeral ports available.
109
+ * ## `"remote-unreachable"`
110
+ *
111
+ * The remote address is not reachable
112
+ * ## `"already-listening"`
113
+ *
114
+ * The socket is already in the Listener state.
115
+ * ## `"not-listening"`
116
+ *
117
+ * The socket is already in the Listener state.
118
+ * ## `"connection-refused"`
119
+ *
120
+ * The connection was forcefully rejected
121
+ * ## `"connection-reset"`
122
+ *
123
+ * The connection was reset.
124
+ * ## `"datagram-too-large"`
125
+ *
126
+ * ## `"invalid-name"`
127
+ *
128
+ * The provided name is a syntactically invalid domain name.
129
+ * ## `"name-unresolvable"`
130
+ *
131
+ * Name does not exist or has no suitable associated IP addresses.
132
+ * ## `"temporary-resolver-failure"`
133
+ *
134
+ * A temporary failure in name resolution occurred.
135
+ * ## `"permanent-resolver-failure"`
136
+ *
137
+ * A permanent failure in name resolution occurred.
138
+ */
139
+ 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';
140
+ /**
141
+ * # Variants
142
+ *
143
+ * ## `"ipv4"`
144
+ *
145
+ * Similar to `AF_INET` in POSIX.
146
+ * ## `"ipv6"`
147
+ *
148
+ * Similar to `AF_INET6` in POSIX.
149
+ */
150
+ export type IpAddressFamily = 'ipv4' | 'ipv6';
151
+ export type Ipv4Address = [number, number, number, number];
152
+ export type Ipv6Address = [number, number, number, number, number, number, number, number];
153
+ export type IpAddress = IpAddressIpv4 | IpAddressIpv6;
154
+ export interface IpAddressIpv4 {
155
+ tag: 'ipv4',
156
+ val: Ipv4Address,
157
+ }
158
+ export interface IpAddressIpv6 {
159
+ tag: 'ipv6',
160
+ val: Ipv6Address,
161
+ }
162
+ export interface Ipv4SocketAddress {
163
+ port: number,
164
+ address: Ipv4Address,
165
+ }
166
+ export interface Ipv6SocketAddress {
167
+ port: number,
168
+ flowInfo: number,
169
+ address: Ipv6Address,
170
+ scopeId: number,
171
+ }
172
+ export type IpSocketAddress = IpSocketAddressIpv4 | IpSocketAddressIpv6;
173
+ export interface IpSocketAddressIpv4 {
174
+ tag: 'ipv4',
175
+ val: Ipv4SocketAddress,
176
+ }
177
+ export interface IpSocketAddressIpv6 {
178
+ tag: 'ipv6',
179
+ val: Ipv6SocketAddress,
180
+ }
@@ -0,0 +1,33 @@
1
+ export namespace WasiSocketsTcpCreateSocket {
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 '../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 { TcpSocket } from '../interfaces/wasi-sockets-tcp';
33
+ export { TcpSocket };