@bytecodealliance/preview2-shim 0.0.21 → 0.14.1

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 +4 -14
  2. package/lib/browser/cli.js +2 -4
  3. package/lib/browser/clocks.js +15 -27
  4. package/lib/browser/filesystem.js +2 -30
  5. package/lib/browser/http.js +1 -3
  6. package/lib/browser/io.js +4 -2
  7. package/lib/common/assert.js +7 -0
  8. package/lib/io/calls.js +64 -0
  9. package/lib/io/worker-http.js +95 -0
  10. package/lib/io/worker-io.js +322 -0
  11. package/lib/io/worker-thread.js +569 -0
  12. package/lib/nodejs/cli.js +45 -59
  13. package/lib/nodejs/clocks.js +13 -27
  14. package/lib/nodejs/filesystem.js +539 -459
  15. package/lib/nodejs/http.js +440 -173
  16. package/lib/nodejs/index.js +4 -1
  17. package/lib/nodejs/io.js +1 -0
  18. package/lib/nodejs/sockets/socket-common.js +116 -0
  19. package/lib/nodejs/sockets/socketopts-bindings.js +94 -0
  20. package/lib/nodejs/sockets/tcp-socket-impl.js +794 -0
  21. package/lib/nodejs/sockets/udp-socket-impl.js +628 -0
  22. package/lib/nodejs/sockets/wasi-sockets.js +320 -0
  23. package/lib/nodejs/sockets.js +11 -200
  24. package/lib/synckit/index.js +4 -2
  25. package/package.json +1 -5
  26. package/types/interfaces/wasi-cli-terminal-input.d.ts +4 -0
  27. package/types/interfaces/wasi-cli-terminal-output.d.ts +4 -0
  28. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +19 -6
  29. package/types/interfaces/wasi-filesystem-types.d.ts +1 -178
  30. package/types/interfaces/wasi-http-outgoing-handler.d.ts +2 -2
  31. package/types/interfaces/wasi-http-types.d.ts +412 -82
  32. package/types/interfaces/wasi-io-error.d.ts +16 -0
  33. package/types/interfaces/wasi-io-poll.d.ts +19 -8
  34. package/types/interfaces/wasi-io-streams.d.ts +26 -46
  35. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +9 -21
  36. package/types/interfaces/wasi-sockets-network.d.ts +4 -0
  37. package/types/interfaces/wasi-sockets-tcp.d.ts +75 -18
  38. package/types/interfaces/wasi-sockets-udp-create-socket.d.ts +1 -1
  39. package/types/interfaces/wasi-sockets-udp.d.ts +282 -193
  40. package/types/wasi-cli-command.d.ts +28 -28
  41. package/types/wasi-http-proxy.d.ts +12 -12
  42. package/lib/common/io.js +0 -183
  43. package/lib/common/make-request.js +0 -30
  44. package/types/interfaces/wasi-clocks-timezone.d.ts +0 -56
@@ -0,0 +1,320 @@
1
+ /**
2
+ * @typedef {import("../../../types/interfaces/wasi-sockets-network").Network} Network
3
+ * @typedef {import("../../../types/interfaces/wasi-sockets-network").ErrorCode} ErrorCode
4
+ * @typedef {import("../../../types/interfaces/wasi-sockets-network").IpAddressFamily} IpAddressFamily
5
+ * @typedef {import("../../../types/interfaces/wasi-sockets-network").IpAddress} IpAddress
6
+ * @typedef {import("../../../types/interfaces/wasi-sockets-tcp").TcpSocket} TcpSocket
7
+ * @typedef {import("../../../types/interfaces/wasi-sockets-udp").UdpSocket} UdpSocket
8
+ */
9
+
10
+ import { isIP } from "net";
11
+ import { assert } from "../../common/assert.js";
12
+ import {
13
+ SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST,
14
+ SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST,
15
+ SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST,
16
+ } from "../../io/calls.js";
17
+ import { ioCall, pollableCreate } from "../../io/worker-io.js";
18
+ import { deserializeIpAddress } from "./socket-common.js";
19
+ import { TcpSocketImpl } from "./tcp-socket-impl.js";
20
+ import { IncomingDatagramStream, OutgoingDatagramStream, UdpSocketImpl } from "./udp-socket-impl.js";
21
+
22
+ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
23
+
24
+ /** @type {ErrorCode} */
25
+ export const errorCode = {
26
+ // ### GENERAL ERRORS ###
27
+
28
+ /// Unknown error
29
+ unknown: "unknown",
30
+
31
+ /// Access denied.
32
+ ///
33
+ /// POSIX equivalent: EACCES, EPERM
34
+ accessDenied: "access-denied",
35
+
36
+ /// The operation is not supported.
37
+ ///
38
+ /// POSIX equivalent: EOPNOTSUPP
39
+ notSupported: "not-supported",
40
+
41
+ /// One of the arguments is invalid.
42
+ ///
43
+ /// POSIX equivalent: EINVAL
44
+ invalidArgument: "invalid-argument",
45
+
46
+ /// Not enough memory to complete the operation.
47
+ ///
48
+ /// POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY
49
+ outOfMemory: "out-of-memory",
50
+
51
+ /// The operation timed out before it could finish completely.
52
+ timeout: "timeout",
53
+
54
+ /// This operation is incompatible with another asynchronous operation that is already in progress.
55
+ ///
56
+ /// POSIX equivalent: EALREADY
57
+ concurrencyConflict: "concurrency-conflict",
58
+
59
+ /// Trying to finish an asynchronous operation that:
60
+ /// - has not been started yet, or:
61
+ /// - was already finished by a previous `finish-*` call.
62
+ ///
63
+ /// Note: this is scheduled to be removed when `future`s are natively supported.
64
+ notInProgress: "not-in-progress",
65
+
66
+ /// The operation has been aborted because it could not be completed immediately.
67
+ ///
68
+ /// Note: this is scheduled to be removed when `future`s are natively supported.
69
+ wouldBlock: "would-block",
70
+
71
+ // ### TCP & UDP SOCKET ERRORS ###
72
+
73
+ /// The operation is not valid in the socket's current state.
74
+ invalidState: "invalid-state",
75
+
76
+ /// A new socket resource could not be created because of a system limit.
77
+ newSocketLimit: "new-socket-limit",
78
+
79
+ /// A bind operation failed because the provided address is not an address that the `network` can bind to.
80
+ addressNotBindable: "address-not-bindable",
81
+
82
+ /// A bind operation failed because the provided address is already in use or because there are no ephemeral ports available.
83
+ addressInUse: "address-in-use",
84
+
85
+ /// The remote address is not reachable
86
+ remoteUnreachable: "remote-unreachable",
87
+
88
+ // ### TCP SOCKET ERRORS ###
89
+
90
+ /// The connection was forcefully rejected
91
+ connectionRefused: "connection-refused",
92
+
93
+ /// The connection was reset.
94
+ connectionReset: "connection-reset",
95
+
96
+ /// A connection was aborted.
97
+ connectionAborted: "connection-aborted",
98
+
99
+ // ### UDP SOCKET ERRORS ###
100
+ datagramTooLarge: "datagram-too-large",
101
+
102
+ // ### NAME LOOKUP ERRORS ###
103
+
104
+ /// Name does not exist or has no suitable associated IP addresses.
105
+ nameUnresolvable: "name-unresolvable",
106
+
107
+ /// A temporary failure in name resolution occurred.
108
+ temporaryResolverFailure: "temporary-resolver-failure",
109
+
110
+ /// A permanent failure in name resolution occurred.
111
+ permanentResolverFailure: "permanent-resolver-failure",
112
+ };
113
+
114
+ /** @type {IpAddressFamily[]} */
115
+ const supportedAddressFamilies = ["ipv4", "ipv6"];
116
+
117
+ export const IpAddressFamily = {
118
+ ipv4: "ipv4",
119
+ ipv6: "ipv6",
120
+ };
121
+
122
+ export class WasiSockets {
123
+ networkCnt = 1;
124
+ socketCnt = 1;
125
+
126
+ // TODO: figure out what the max number of sockets should be
127
+ maxSockets = 100;
128
+
129
+ /** @type {Network} */ networkInstance = null;
130
+ /** @type {Map<number,Network>} */ networks = new Map();
131
+ /** @type {Map<number,TcpSocket} */ tcpSockets = new Map();
132
+ /** @type {Map<number,UdpSocket} */ udpSockets = new Map();
133
+
134
+ constructor() {
135
+ const net = this;
136
+
137
+ class Network {
138
+ constructor() {
139
+ this.id = net.networkCnt++;
140
+ net.networks.set(this.id, this);
141
+ }
142
+ }
143
+
144
+ class UdpSocket extends UdpSocketImpl {
145
+ /**
146
+ * @param {IpAddressFamily} addressFamily
147
+ * */
148
+ constructor(addressFamily) {
149
+ super(addressFamily, net.socketCnt++);
150
+ net.udpSockets.set(this.id, this);
151
+ }
152
+ }
153
+
154
+ this.udp = {
155
+ UdpSocket,
156
+ OutgoingDatagramStream,
157
+ IncomingDatagramStream,
158
+ };
159
+
160
+ class TcpSocket extends TcpSocketImpl {
161
+ /**
162
+ * @param {IpAddressFamily} addressFamily
163
+ * */
164
+ constructor(addressFamily) {
165
+ super(addressFamily, TcpSocket, net.socketCnt++);
166
+ net.tcpSockets.set(this.id, this);
167
+ }
168
+ }
169
+
170
+ this.tcp = {
171
+ TcpSocket,
172
+ };
173
+
174
+ this.instanceNetwork = {
175
+ /**
176
+ * @returns {Network}
177
+ */
178
+ instanceNetwork() {
179
+ // TODO: should networkInstance be a singleton?
180
+ if (!net.networkInstance) {
181
+ net.networkInstance = new Network();
182
+ }
183
+ return net.networkInstance;
184
+ },
185
+ };
186
+
187
+ this.network = {
188
+ errorCode,
189
+ IpAddressFamily,
190
+ Network,
191
+ };
192
+
193
+ this.udpCreateSocket = {
194
+ createUdpSocket(addressFamily) {
195
+ assert(
196
+ supportedAddressFamilies.includes(addressFamily) === false,
197
+ errorCode.notSupported,
198
+ "The specified `address-family` is not supported."
199
+ );
200
+
201
+ assert(
202
+ net.socketCnt + 1 > net.maxSockets,
203
+ errorCode.newSocketLimit,
204
+ "The new socket resource could not be created because of a system limit"
205
+ );
206
+
207
+ try {
208
+ return new UdpSocket(addressFamily);
209
+ } catch (err) {
210
+ assert(true, errorCode.notSupported, err);
211
+ }
212
+ },
213
+ };
214
+
215
+ this.tcpCreateSocket = {
216
+ /**
217
+ * @param {IpAddressFamily} addressFamily
218
+ * @returns {TcpSocket}
219
+ * @throws {not-supported} The specified `address-family` is not supported. (EAFNOSUPPORT)
220
+ * @throws {new-socket-limit} The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
221
+ */
222
+ createTcpSocket(addressFamily) {
223
+ assert(
224
+ supportedAddressFamilies.includes(addressFamily) === false,
225
+ errorCode.notSupported,
226
+ "The specified `address-family` is not supported."
227
+ );
228
+
229
+ assert(
230
+ net.socketCnt + 1 > net.maxSockets,
231
+ errorCode.newSocketLimit,
232
+ "The new socket resource could not be created because of a system limit"
233
+ );
234
+
235
+ // try {
236
+ return new TcpSocket(addressFamily);
237
+ // } catch (err) {
238
+ // // assert(true, errorCode.unknown, err);
239
+ // throw err;
240
+ // }
241
+ },
242
+ };
243
+
244
+ class ResolveAddressStream {
245
+ #pollId;
246
+ #data;
247
+ #curItem = 0;
248
+ #error;
249
+ resolveNextAddress() {
250
+ if (this.#error) throw this.#error;
251
+ if (!this.#data) {
252
+ const { value: addresses, error } = ioCall(SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST, this.#pollId);
253
+ if (error) throw (this.#error = convertResolveAddressError(error));
254
+ this.#data = addresses.map((address) => {
255
+ const family = `ipv${isIP(address)}`;
256
+ return {
257
+ tag: family,
258
+ val: deserializeIpAddress(address),
259
+ };
260
+ });
261
+ }
262
+ if (this.#curItem < this.#data.length) return this.#data[this.#curItem++];
263
+ return undefined;
264
+ }
265
+ subscribe() {
266
+ if (this.#data) return pollableCreate(0);
267
+ return pollableCreate(this.#pollId);
268
+ }
269
+ [symbolDispose]() {
270
+ if (!this.#data) ioCall(SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST);
271
+ }
272
+ static _create(hostname) {
273
+ const res = new ResolveAddressStream();
274
+ if (hostname === "0.0.0.0") {
275
+ res.#pollId = 0;
276
+ res.#data = { tag: "ipv4", val: [0, 0, 0, 0] };
277
+ return res;
278
+ } else if (hostname === "::") {
279
+ res.#pollId = 0;
280
+ res.#data = { tag: "ipv6", val: [0, 0, 0, 0, 0, 0, 0, 0] };
281
+ return res;
282
+ } else if (hostname === "::1") {
283
+ res.#pollId = 0;
284
+ res.#data = { tag: "ipv6", val: [0, 0, 0, 0, 0, 0, 0, 1] };
285
+ return res;
286
+ }
287
+ res.#pollId = ioCall(SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST, null, {
288
+ hostname,
289
+ });
290
+ return res;
291
+ }
292
+ }
293
+
294
+ const resolveAddressStreamCreate = ResolveAddressStream._create;
295
+ delete ResolveAddressStream._create;
296
+
297
+ this.ipNameLookup = {
298
+ ResolveAddressStream,
299
+
300
+ /**
301
+ *
302
+ * @param {Network} network
303
+ * @param {string} name
304
+ * @returns {ResolveAddressStream}
305
+ * @throws {invalid-argument} `name` is a syntactically invalid domain name or IP address.
306
+ */
307
+ resolveAddresses(network, name) {
308
+ // TODO: bind to network
309
+ return resolveAddressStreamCreate(name);
310
+ },
311
+ };
312
+ }
313
+ }
314
+
315
+ function convertResolveAddressError(err) {
316
+ switch (err.code) {
317
+ default:
318
+ return "unknown";
319
+ }
320
+ }
@@ -1,200 +1,11 @@
1
- export const instanceNetwork = {
2
- instanceNetwork () {
3
- console.log(`[sockets] instance network`);
4
- }
5
- };
6
-
7
- export const ipNameLookup = {
8
- dropResolveAddressStream () {
9
-
10
- },
11
- subscribe () {
12
-
13
- },
14
- resolveAddresses () {
15
-
16
- },
17
- resolveNextAddress () {
18
-
19
- },
20
- nonBlocking () {
21
-
22
- },
23
- setNonBlocking () {
24
-
25
- },
26
- };
27
-
28
- export const network = {
29
- dropNetwork () {
30
-
31
- }
32
- };
33
-
34
- export const tcpCreateSocket = {
35
- createTcpSocket () {
36
-
37
- }
38
- };
39
-
40
- export const tcp = {
41
- subscribe () {
42
-
43
- },
44
- dropTcpSocket() {
45
-
46
- },
47
- bind() {
48
-
49
- },
50
- connect() {
51
-
52
- },
53
- listen() {
54
-
55
- },
56
- accept() {
57
-
58
- },
59
- localAddress() {
60
-
61
- },
62
- remoteAddress() {
63
-
64
- },
65
- addressFamily() {
66
-
67
- },
68
- ipv6Only() {
69
-
70
- },
71
- setIpv6Only() {
72
-
73
- },
74
- setListenBacklogSize() {
75
-
76
- },
77
- keepAlive() {
78
-
79
- },
80
- setKeepAlive() {
81
-
82
- },
83
- noDelay() {
84
-
85
- },
86
- setNoDelay() {
87
-
88
- },
89
- unicastHopLimit() {
90
-
91
- },
92
- setUnicastHopLimit() {
93
-
94
- },
95
- receiveBufferSize() {
96
-
97
- },
98
- setReceiveBufferSize() {
99
-
100
- },
101
- sendBufferSize() {
102
-
103
- },
104
- setSendBufferSize() {
105
-
106
- },
107
- nonBlocking() {
108
-
109
- },
110
- setNonBlocking() {
111
-
112
- },
113
- shutdown() {
114
-
115
- }
116
- };
117
-
118
- export const udp = {
119
- subscribe () {
120
-
121
- },
122
-
123
- dropUdpSocket () {
124
-
125
- },
126
-
127
- bind () {
128
-
129
- },
130
-
131
- connect () {
132
-
133
- },
134
-
135
- receive () {
136
-
137
- },
138
-
139
- send () {
140
-
141
- },
142
-
143
- localAddress () {
144
-
145
- },
146
-
147
- remoteAddress () {
148
-
149
- },
150
-
151
- addressFamily () {
152
-
153
- },
154
-
155
- ipv6Only () {
156
-
157
- },
158
-
159
- setIpv6Only () {
160
-
161
- },
162
-
163
- unicastHopLimit () {
164
-
165
- },
166
-
167
- setUnicastHopLimit () {
168
-
169
- },
170
-
171
- receiveBufferSize () {
172
-
173
- },
174
-
175
- setReceiveBufferSize () {
176
-
177
- },
178
-
179
- sendBufferSize () {
180
-
181
- },
182
-
183
- setSendBufferSize () {
184
-
185
- },
186
-
187
- nonBlocking () {
188
-
189
- },
190
-
191
- setNonBlocking () {
192
-
193
- }
194
- };
195
-
196
- export const udpCreateSocket = {
197
- createTcpSocket () {
198
-
199
- }
200
- };
1
+ import { WasiSockets } from "./sockets/wasi-sockets.js";
2
+
3
+ export const {
4
+ ipNameLookup,
5
+ instanceNetwork,
6
+ network,
7
+ tcpCreateSocket,
8
+ udpCreateSocket,
9
+ tcp,
10
+ udp,
11
+ } = new WasiSockets();
@@ -75,7 +75,7 @@ function startWorkerThread(
75
75
  const worker = new Worker(workerPath, {
76
76
  workerData: { workerPort },
77
77
  transferList: [workerPort],
78
- execArgv: execArgv,
78
+ execArgv: execArgv
79
79
  });
80
80
  let nextID = 0;
81
81
  const syncFn = (...args) => {
@@ -98,7 +98,9 @@ function startWorkerThread(
98
98
  throw new Error(`Internal error: Expected id ${id} but got id ${id2}`);
99
99
  }
100
100
  if (error) {
101
- throw Object.assign(error, properties);
101
+ if (error instanceof Error)
102
+ throw Object.assign(error, properties);
103
+ throw error;
102
104
  }
103
105
  return result;
104
106
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.21",
3
+ "version": "0.14.1",
4
4
  "description": "WASI Preview2 shim for JS environments",
5
5
  "author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
6
6
  "type": "module",
@@ -11,10 +11,6 @@
11
11
  "node": "./lib/nodejs/index.js",
12
12
  "default": "./lib/browser/index.js"
13
13
  },
14
- "./io": {
15
- "types": "./types/io.d.ts",
16
- "default": "./lib/common/io.js"
17
- },
18
14
  "./*": {
19
15
  "types": "./types/*.d.ts",
20
16
  "node": "./lib/nodejs/*.js",
@@ -1,2 +1,6 @@
1
1
  export namespace WasiCliTerminalInput {
2
+ export { TerminalInput };
3
+ }
4
+
5
+ export class TerminalInput {
2
6
  }
@@ -1,2 +1,6 @@
1
1
  export namespace WasiCliTerminalOutput {
2
+ export { TerminalOutput };
3
+ }
4
+
5
+ export class TerminalOutput {
2
6
  }
@@ -7,18 +7,31 @@ export namespace WasiClocksMonotonicClock {
7
7
  */
8
8
  export function now(): Instant;
9
9
  /**
10
- * Query the resolution of the clock.
10
+ * Query the resolution of the clock. Returns the duration of time
11
+ * corresponding to a clock tick.
11
12
  */
12
- export function resolution(): Instant;
13
+ export function resolution(): Duration;
13
14
  /**
14
- * Create a `pollable` which will resolve once the specified time has been
15
- * reached.
15
+ * Create a `pollable` which will resolve once the specified instant
16
+ * occured.
16
17
  */
17
- export function subscribe(when: Instant, absolute: boolean): Pollable;
18
+ export function subscribeInstant(when: Instant): Pollable;
19
+ /**
20
+ * Create a `pollable` which will resolve once the given duration has
21
+ * elapsed, starting at the time at which this function was called.
22
+ * occured.
23
+ */
24
+ export function subscribeDuration(when: Duration): Pollable;
18
25
  }
19
26
  import type { Pollable } from '../interfaces/wasi-io-poll.js';
20
27
  export { Pollable };
21
28
  /**
22
- * A timestamp in nanoseconds.
29
+ * An instant in time, in nanoseconds. An instant is relative to an
30
+ * unspecified initial value, and can only be compared to instances from
31
+ * the same monotonic-clock.
23
32
  */
24
33
  export type Instant = bigint;
34
+ /**
35
+ * A duration of time, in nanoseconds.
36
+ */
37
+ export type Duration = bigint;