@bytecodealliance/preview2-shim 0.15.1 → 0.16.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 (31) hide show
  1. package/lib/io/calls.js +0 -1
  2. package/lib/io/worker-http.js +12 -9
  3. package/lib/io/worker-io.js +75 -16
  4. package/lib/io/worker-socket-tcp.js +1 -0
  5. package/lib/io/worker-thread.js +4 -4
  6. package/lib/nodejs/cli.js +1 -1
  7. package/lib/nodejs/clocks.js +40 -9
  8. package/lib/nodejs/filesystem.js +10 -3
  9. package/lib/nodejs/http.js +25 -21
  10. package/lib/nodejs/random.js +26 -13
  11. package/lib/synckit/index.js +1 -1
  12. package/package.json +1 -1
  13. package/types/interfaces/wasi-cli-stderr.d.ts +1 -1
  14. package/types/interfaces/wasi-cli-stdin.d.ts +1 -1
  15. package/types/interfaces/wasi-cli-stdout.d.ts +1 -1
  16. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
  17. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
  18. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
  19. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +1 -1
  20. package/types/interfaces/wasi-filesystem-preopens.d.ts +1 -1
  21. package/types/interfaces/wasi-filesystem-types.d.ts +8 -8
  22. package/types/interfaces/wasi-http-incoming-handler.d.ts +2 -2
  23. package/types/interfaces/wasi-http-outgoing-handler.d.ts +4 -4
  24. package/types/interfaces/wasi-http-types.d.ts +50 -50
  25. package/types/interfaces/wasi-io-streams.d.ts +2 -2
  26. package/types/interfaces/wasi-sockets-instance-network.d.ts +1 -1
  27. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +4 -4
  28. package/types/interfaces/wasi-sockets-tcp-create-socket.d.ts +4 -4
  29. package/types/interfaces/wasi-sockets-tcp.d.ts +8 -8
  30. package/types/interfaces/wasi-sockets-udp-create-socket.d.ts +4 -4
  31. package/types/interfaces/wasi-sockets-udp.d.ts +5 -5
package/lib/io/calls.js CHANGED
@@ -65,7 +65,6 @@ export const HTTP_SERVER_CLEAR_OUTGOING_RESPONSE = ++call_id << CALL_SHIFT;
65
65
  export const HTTP_OUTGOING_BODY_DISPOSE = ++call_id << CALL_SHIFT;
66
66
 
67
67
  // Clocks
68
- export const CLOCKS_NOW = ++call_id << CALL_SHIFT;
69
68
  export const CLOCKS_DURATION_SUBSCRIBE = ++call_id << CALL_SHIFT;
70
69
  export const CLOCKS_INSTANT_SUBSCRIBE = ++call_id << CALL_SHIFT;
71
70
 
@@ -1,7 +1,4 @@
1
- import {
2
- createReadableStream,
3
- getStreamOrThrow,
4
- } from "./worker-thread.js";
1
+ import { createReadableStream, getStreamOrThrow } from "./worker-thread.js";
5
2
  import {
6
3
  createServer,
7
4
  request as httpRequest,
@@ -117,7 +114,7 @@ export async function createHttpRequest(
117
114
  host: authority.split(":")[0],
118
115
  port: authority.split(":")[1],
119
116
  path: pathWithQuery,
120
- timeout: connectTimeout && Number(connectTimeout),
117
+ timeout: connectTimeout && Number(connectTimeout / 1_000_000n),
121
118
  });
122
119
  break;
123
120
  case "https:":
@@ -127,7 +124,7 @@ export async function createHttpRequest(
127
124
  host: authority.split(":")[0],
128
125
  port: authority.split(":")[1],
129
126
  path: pathWithQuery,
130
- timeout: connectTimeout && Number(connectTimeout),
127
+ timeout: connectTimeout && Number(connectTimeout / 1_000_000n),
131
128
  });
132
129
  break;
133
130
  default:
@@ -143,20 +140,26 @@ export async function createHttpRequest(
143
140
  req.end();
144
141
  }
145
142
  const res = await new Promise((resolve, reject) => {
143
+ req.once('timeout', () => {
144
+ reject({
145
+ tag: "connection-timeout"
146
+ });
147
+ req.destroy();
148
+ });
146
149
  req.once("response", resolve);
147
150
  req.once("close", () => reject);
148
151
  req.once("error", reject);
149
152
  });
150
- if (firstByteTimeout) res.setTimeout(Number(firstByteTimeout));
153
+ if (firstByteTimeout) res.setTimeout(Number(firstByteTimeout / 1_000_000n));
151
154
  if (betweenBytesTimeout)
152
155
  res.once("readable", () => {
153
- res.setTimeout(Number(betweenBytesTimeout));
156
+ res.setTimeout(Number(betweenBytesTimeout / 1_000_000n));
154
157
  });
155
158
  const bodyStreamId = createReadableStream(res);
156
159
  return {
157
160
  status: res.statusCode,
158
161
  headers: Array.from(Object.entries(res.headers)),
159
- bodyStreamId
162
+ bodyStreamId,
160
163
  };
161
164
  } catch (e) {
162
165
  if (e?.tag) throw e;
@@ -33,7 +33,9 @@ import {
33
33
  STDOUT,
34
34
  reverseMap,
35
35
  } from "./calls.js";
36
- import { _rawDebug, exit, stderr, stdout, env } from "node:process";
36
+ import nodeProcess, { exit, stderr, stdout, env } from "node:process";
37
+
38
+ const _rawDebug = nodeProcess._rawDebug || console.error.bind(console);
37
39
 
38
40
  const workerPath = fileURLToPath(
39
41
  new URL("./worker-thread.js", import.meta.url)
@@ -409,24 +411,19 @@ function pollableDispose(id) {
409
411
  ioCall(POLL_POLLABLE_DISPOSE, id);
410
412
  }
411
413
 
414
+ const rep = Symbol.for("cabiRep");
415
+
412
416
  class Pollable {
413
- #id;
414
417
  #finalizer;
415
418
  ready() {
416
- if (this.#id === 0) return true;
417
- return ioCall(POLL_POLLABLE_READY, this.#id);
419
+ return ioCall(POLL_POLLABLE_READY, this[rep]);
418
420
  }
419
421
  block() {
420
- if (this.#id !== 0) {
421
- ioCall(POLL_POLLABLE_BLOCK, this.#id);
422
- }
423
- }
424
- static _getId(pollable) {
425
- return pollable.#id;
422
+ ioCall(POLL_POLLABLE_BLOCK, this[rep]);
426
423
  }
427
424
  static _create(id, parent) {
428
425
  const pollable = new Pollable();
429
- pollable.#id = id;
426
+ pollable[rep] = id;
430
427
  pollable.#finalizer = registerDispose(
431
428
  pollable,
432
429
  parent,
@@ -436,26 +433,88 @@ class Pollable {
436
433
  return pollable;
437
434
  }
438
435
  [symbolDispose]() {
439
- if (this.#finalizer) {
436
+ if (this.#finalizer && this[rep]) {
440
437
  earlyDispose(this.#finalizer);
441
438
  this.#finalizer = null;
442
439
  }
443
440
  }
444
441
  }
445
442
 
443
+ const cabiLowerSymbol = Symbol.for("cabiLower");
444
+ const T_FLAG = 1 << 30;
445
+
446
+ Pollable.prototype.ready[cabiLowerSymbol] = function ({
447
+ resourceTables: [table],
448
+ }) {
449
+ return function pollableReady(handle) {
450
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
451
+ const ready = ioCall(POLL_POLLABLE_READY, rep);
452
+ return ready ? 1 : 0;
453
+ };
454
+ };
455
+
456
+ Pollable.prototype.block[cabiLowerSymbol] = function ({
457
+ resourceTables: [table],
458
+ }) {
459
+ return function pollableBlock(handle) {
460
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
461
+ ioCall(POLL_POLLABLE_BLOCK, rep);
462
+ };
463
+ };
464
+
465
+ Pollable[Symbol.for("cabiDispose")] = function pollableDispose(rep) {
466
+ ioCall(POLL_POLLABLE_DISPOSE, rep);
467
+ };
468
+
446
469
  export const pollableCreate = Pollable._create;
447
470
  delete Pollable._create;
448
471
 
449
- const pollableGetId = Pollable._getId;
450
- delete Pollable._getId;
451
-
452
472
  export const poll = {
453
473
  Pollable,
454
474
  poll(list) {
455
- return ioCall(POLL_POLL_LIST, null, list.map(pollableGetId));
475
+ return ioCall(
476
+ POLL_POLL_LIST,
477
+ null,
478
+ list.map((pollable) => pollable[rep])
479
+ );
456
480
  },
457
481
  };
458
482
 
483
+ poll.poll[cabiLowerSymbol] = function ({ memory, realloc, resourceTables: [table] }) {
484
+ return function pollPollList (listPtr, len, retptr) {
485
+ const handleList = new Uint32Array(memory.buffer, listPtr, len);
486
+ const repList = Array(len);
487
+ for (let i = 0; i < len; i++) {
488
+ const handle = handleList[i];
489
+ repList[i] = table[(handle << 1) + 1] & ~T_FLAG;
490
+ }
491
+ const result = ioCall(POLL_POLL_LIST, null, repList);
492
+ const ptr = realloc(0, 0, 4, result.byteLength);
493
+ const out = new Uint32Array(memory.buffer, ptr, result.length);
494
+ out.set(result);
495
+ const ret = new Uint32Array(memory.buffer, retptr, 2);
496
+ ret[0] = ptr;
497
+ ret[1] = result.length;
498
+ return retptr;
499
+ };
500
+ };
501
+
459
502
  export function createPoll(call, id, initPayload) {
460
503
  return pollableCreate(ioCall(call, id, initPayload));
461
504
  }
505
+
506
+ export function createPollLower(call, id, table) {
507
+ return function (initPayload) {
508
+ const rep = ioCall(call, id, initPayload);
509
+ const free = table[0] & ~T_FLAG;
510
+ if (free === 0) {
511
+ table.push(0);
512
+ table.push(rep | T_FLAG);
513
+ return (table.length >> 1) - 1;
514
+ }
515
+ table[0] = table[free << 1];
516
+ table[free << 1] = 0;
517
+ table[(free << 1) + 1] = rep | T_FLAG;
518
+ return free;
519
+ };
520
+ }
@@ -8,6 +8,7 @@ import {
8
8
  pollStateReady,
9
9
  verifyPollsDroppedForDrop,
10
10
  } from "./worker-thread.js";
11
+ import process from "node:process";
11
12
  const { TCP, constants: TCPConstants } = process.binding("tcp_wrap");
12
13
  import {
13
14
  convertSocketError,
@@ -18,7 +18,6 @@ import {
18
18
  CALL_TYPE_MASK,
19
19
  CLOCKS_DURATION_SUBSCRIBE,
20
20
  CLOCKS_INSTANT_SUBSCRIBE,
21
- CLOCKS_NOW,
22
21
  FILE,
23
22
  FUTURE_DISPOSE,
24
23
  FUTURE_SUBSCRIBE,
@@ -147,6 +146,7 @@ import {
147
146
  socketUdpStream,
148
147
  udpSockets,
149
148
  } from "./worker-socket-udp.js";
149
+ import process from "node:process";
150
150
 
151
151
  function log(msg) {
152
152
  if (debug) process._rawDebug(msg);
@@ -536,8 +536,6 @@ function handle(call, id, payload) {
536
536
  }
537
537
 
538
538
  // Clocks
539
- case CLOCKS_NOW:
540
- return hrtime.bigint();
541
539
  case CLOCKS_DURATION_SUBSCRIBE:
542
540
  payload = hrtime.bigint() + payload;
543
541
  // fallthrough
@@ -775,7 +773,9 @@ function handle(call, id, payload) {
775
773
  pollStateCheck(pollState);
776
774
  if (pollState.ready) doneList.push(idx);
777
775
  }
778
- if (doneList.length > 0) return new Uint32Array(doneList);
776
+ if (doneList.length > 0) {
777
+ return new Uint32Array(doneList);
778
+ }
779
779
  let readyPromiseResolve;
780
780
  const readyPromise = new Promise(
781
781
  (resolve) => void (readyPromiseResolve = resolve)
package/lib/nodejs/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- import { argv, env, cwd } from "node:process";
1
+ import process, { argv, env, cwd } from "node:process";
2
2
  import {
3
3
  ioCall,
4
4
  streams,
@@ -1,16 +1,20 @@
1
- import { ioCall, createPoll } from "../io/worker-io.js";
1
+ import { createPoll } from "../io/worker-io.js";
2
2
  import {
3
- CLOCKS_NOW,
4
3
  CLOCKS_INSTANT_SUBSCRIBE,
5
4
  CLOCKS_DURATION_SUBSCRIBE,
6
5
  } from "../io/calls.js";
6
+ import { hrtime } from "node:process";
7
+
8
+ const symbolCabiLower = Symbol.for("cabiLower");
9
+
10
+ function resolution() {
11
+ return 1n;
12
+ }
7
13
 
8
14
  export const monotonicClock = {
9
- resolution() {
10
- return 1n;
11
- },
15
+ resolution,
12
16
  now() {
13
- return ioCall(CLOCKS_NOW, null, null);
17
+ return hrtime.bigint();
14
18
  },
15
19
  subscribeInstant(instant) {
16
20
  return createPoll(CLOCKS_INSTANT_SUBSCRIBE, null, instant);
@@ -22,12 +26,39 @@ export const monotonicClock = {
22
26
  };
23
27
 
24
28
  export const wallClock = {
29
+ resolution() {
30
+ return { seconds: 0n, nanoseconds: 1e6 };
31
+ },
25
32
  now() {
26
33
  const seconds = BigInt(Math.floor(Date.now() / 1e3));
27
34
  const nanoseconds = (Date.now() % 1e3) * 1e6;
28
35
  return { seconds, nanoseconds };
29
36
  },
30
- resolution() {
31
- return { seconds: 0n, nanoseconds: 1e6 };
32
- },
37
+ };
38
+
39
+ monotonicClock.resolution[symbolCabiLower] = () => resolution;
40
+ monotonicClock.now[symbolCabiLower] = () => hrtime.bigint;
41
+ wallClock.resolution[symbolCabiLower] = ({ memory }) => {
42
+ let buf32 = new Int32Array(memory.buffer);
43
+ return function now(retptr) {
44
+ if (memory.buffer !== buf32.buffer) buf32 = new Int32Array(memory.buffer);
45
+ if (retptr % 4) throw new Error('wasi-io trap: retptr not aligned');
46
+ buf32[(retptr >> 2) + 0] = 0;
47
+ buf32[(retptr >> 2) + 4] = 0;
48
+ buf32[(retptr >> 2) + 8] = 1_000_000;
49
+ };
50
+ };
51
+
52
+ wallClock.now[symbolCabiLower] = ({ memory }) => {
53
+ let buf32 = new Int32Array(memory.buffer);
54
+ let buf64 = new BigInt64Array(memory.buffer);
55
+ return function now(retptr) {
56
+ if (memory.buffer !== buf32.buffer) {
57
+ buf32 = new Int32Array(memory.buffer);
58
+ buf64 = new BigInt64Array(memory.buffer);
59
+ }
60
+ if (retptr % 4) throw new Error('wasi-io trap: retptr not aligned');
61
+ buf64[(retptr >> 2) + 0] = BigInt(Math.floor(Date.now() / 1e3));
62
+ buf32[(retptr >> 2) + 8] = (Date.now() % 1e3) * 1e6;
63
+ };
33
64
  };
@@ -7,7 +7,7 @@ import {
7
7
  } from "../io/worker-io.js";
8
8
  import { INPUT_STREAM_CREATE, OUTPUT_STREAM_CREATE } from "../io/calls.js";
9
9
  import { FILE } from "../io/calls.js";
10
- import {
10
+ import nodeFs, {
11
11
  closeSync,
12
12
  constants,
13
13
  fdatasyncSync,
@@ -17,7 +17,6 @@ import {
17
17
  futimesSync,
18
18
  linkSync,
19
19
  lstatSync,
20
- lutimesSync,
21
20
  mkdirSync,
22
21
  opendirSync,
23
22
  openSync,
@@ -33,6 +32,8 @@ import {
33
32
  } from "node:fs";
34
33
  import { platform } from "node:process";
35
34
 
35
+ const lutimesSync = nodeFs.lutimesSync;
36
+
36
37
  const symbolDispose = Symbol.dispose || Symbol.for("dispose");
37
38
 
38
39
  const isWindows = platform === "win32";
@@ -151,6 +152,8 @@ class Descriptor {
151
152
  try {
152
153
  ftruncateSync(this.#fd, Number(size));
153
154
  } catch (e) {
155
+ if (isWindows && e.code === 'EPERM')
156
+ throw 'access';
154
157
  throw convertFsError(e);
155
158
  }
156
159
  }
@@ -297,6 +300,9 @@ class Descriptor {
297
300
  dataModificationTimestamp.tag === "no-change" &&
298
301
  stats.dataModificationTimestamp
299
302
  );
303
+ if (!pathFlags.symlinkFollow && !lutimesSync){
304
+ throw new Error("Changing the timestamps of symlinks isn't supported");
305
+ }
300
306
  try {
301
307
  (pathFlags.symlinkFollow ? utimesSync : lutimesSync)(
302
308
  fullPath,
@@ -321,13 +327,14 @@ class Descriptor {
321
327
  }
322
328
 
323
329
  openAt(pathFlags, path, openFlags, descriptorFlags) {
330
+ if (preopenEntries.length === 0)
331
+ throw "access";
324
332
  const fullPath = this.#getFullPath(path, pathFlags.symlinkFollow);
325
333
  let fsOpenFlags = 0x0;
326
334
  if (openFlags.create) fsOpenFlags |= constants.O_CREAT;
327
335
  if (openFlags.directory) fsOpenFlags |= constants.O_DIRECTORY;
328
336
  if (openFlags.exclusive) fsOpenFlags |= constants.O_EXCL;
329
337
  if (openFlags.truncate) fsOpenFlags |= constants.O_TRUNC;
330
-
331
338
  if (descriptorFlags.read && descriptorFlags.write)
332
339
  fsOpenFlags |= constants.O_RDWR;
333
340
  else if (descriptorFlags.write) fsOpenFlags |= constants.O_WRONLY;
@@ -21,11 +21,13 @@ import {
21
21
  registerDispose,
22
22
  registerIncomingHttpHandler,
23
23
  } from "../io/worker-io.js";
24
- import { validateHeaderName, validateHeaderValue } from "node:http";
25
24
  import { HTTP } from "../io/calls.js";
26
25
 
26
+ import * as http from "node:http";
27
+ const { validateHeaderName = () => {}, validateHeaderValue = () => {} } = http;
28
+
27
29
  const symbolDispose = Symbol.dispose || Symbol.for("dispose");
28
- export const _forbiddenHeaders = new Set(["connection", "keep-alive"]);
30
+ export const _forbiddenHeaders = new Set(["connection", "keep-alive", "host"]);
29
31
 
30
32
  class IncomingBody {
31
33
  #finished = false;
@@ -173,27 +175,29 @@ class ResponseOutparam {
173
175
  const responseOutparamCreate = ResponseOutparam._create;
174
176
  delete ResponseOutparam._create;
175
177
 
178
+ const defaultHttpTimeout = 600_000_000_000n;
179
+
176
180
  class RequestOptions {
177
- #connectTimeoutMs;
178
- #firstByteTimeoutMs;
179
- #betweenBytesTimeoutMs;
180
- connectTimeoutMs() {
181
- return this.#connectTimeoutMs;
181
+ #connectTimeout = defaultHttpTimeout;
182
+ #firstByteTimeout = defaultHttpTimeout;
183
+ #betweenBytesTimeout = defaultHttpTimeout;
184
+ connectTimeout() {
185
+ return this.#connectTimeout;
182
186
  }
183
- setConnectTimeoutMs(duration) {
184
- this.#connectTimeoutMs = duration;
187
+ setConnectTimeout(duration) {
188
+ this.#connectTimeout = duration;
185
189
  }
186
- firstByteTimeoutMs() {
187
- return this.#firstByteTimeoutMs;
190
+ firstByteTimeout() {
191
+ return this.#firstByteTimeout;
188
192
  }
189
- setFirstByteTimeoutMs(duration) {
190
- this.#firstByteTimeoutMs = duration;
193
+ setFirstByteTimeout(duration) {
194
+ this.#firstByteTimeout = duration;
191
195
  }
192
- betweenBytesTimeoutMs() {
193
- return this.#betweenBytesTimeoutMs;
196
+ betweenBytesTimeout() {
197
+ return this.#betweenBytesTimeout;
194
198
  }
195
- setBetweenBytesTimeoutMs(duration) {
196
- this.#betweenBytesTimeoutMs = duration;
199
+ setBetweenBytesTimeout(duration) {
200
+ this.#betweenBytesTimeout = duration;
197
201
  }
198
202
  }
199
203
 
@@ -270,9 +274,9 @@ class OutgoingRequest {
270
274
  }
271
275
  [symbolDispose]() {}
272
276
  static _handle(request, options) {
273
- const connectTimeout = options?.connectTimeoutMs();
274
- const betweenBytesTimeout = options?.betweenBytesTimeoutMs();
275
- const firstByteTimeout = options?.firstByteTimeoutMs();
277
+ const connectTimeout = options?.connectTimeout();
278
+ const betweenBytesTimeout = options?.betweenBytesTimeout();
279
+ const firstByteTimeout = options?.firstByteTimeout();
276
280
  const scheme = schemeString(request.#scheme);
277
281
  // note: host header is automatically added by Node.js
278
282
  const headers = [];
@@ -289,7 +293,7 @@ class OutgoingRequest {
289
293
  outgoingBodyOutputStreamId(request.#body),
290
294
  connectTimeout,
291
295
  betweenBytesTimeout,
292
- firstByteTimeout
296
+ firstByteTimeout,
293
297
  );
294
298
  }
295
299
  }
@@ -1,32 +1,45 @@
1
- import { randomBytes } from "node:crypto";
1
+ import { randomBytes, randomFillSync } from "node:crypto";
2
2
 
3
3
  export const insecure = {
4
- getInsecureRandomBytes (len) {
5
- return randomBytes(Number(len));
6
- },
7
- getInsecureRandomU64 () {
4
+ getInsecureRandomBytes: getRandomBytes,
5
+ getInsecureRandomU64() {
8
6
  return new BigUint64Array(randomBytes(8).buffer)[0];
9
- }
7
+ },
10
8
  };
11
9
 
12
10
  let insecureSeedValue1, insecureSeedValue2;
13
11
 
14
12
  export const insecureSeed = {
15
- insecureSeed () {
13
+ insecureSeed() {
16
14
  if (insecureSeedValue1 === undefined) {
17
15
  insecureSeedValue1 = random.getRandomU64();
18
16
  insecureSeedValue2 = random.getRandomU64();
19
17
  }
20
18
  return [insecureSeedValue1, insecureSeedValue2];
21
- }
19
+ },
22
20
  };
23
21
 
24
22
  export const random = {
25
- getRandomBytes(len) {
26
- return randomBytes(Number(len));
27
- },
23
+ getRandomBytes,
28
24
 
29
- getRandomU64 () {
25
+ getRandomU64() {
30
26
  return new BigUint64Array(randomBytes(8).buffer)[0];
31
- }
27
+ },
28
+ };
29
+
30
+ function getRandomBytes(len) {
31
+ return randomBytes(Number(len));
32
+ }
33
+
34
+ getRandomBytes[Symbol.for("cabiLower")] = ({ memory, realloc }) => {
35
+ let buf32 = new Uint32Array(memory.buffer);
36
+ return function randomBytes(len, retptr) {
37
+ len = Number(len);
38
+ const ptr = realloc(0, 0, 1, len);
39
+ randomFillSync(memory.buffer, ptr, len);
40
+ if (memory.buffer !== buf32.buffer) buf32 = new Uint32Array(memory.buffer);
41
+ if (retptr % 4) throw new Error('wasi-io trap: retptr not aligned');
42
+ buf32[retptr >> 2] = ptr;
43
+ buf32[(retptr >> 2) + 1] = len;
44
+ };
32
45
  };
@@ -87,7 +87,7 @@ export function createSyncFn(workerPath, debug, callbackHandler) {
87
87
  }
88
88
  return result;
89
89
  };
90
- worker.unref();
90
+ if (worker.unref) worker.unref();
91
91
  return syncFn;
92
92
  }
93
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.15.1",
3
+ "version": "0.16.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",
@@ -1,5 +1,5 @@
1
1
  export namespace WasiCliStderr {
2
2
  export function getStderr(): OutputStream;
3
3
  }
4
- import type { OutputStream } from '../interfaces/wasi-io-streams.js';
4
+ import type { OutputStream } from './wasi-io-streams.js';
5
5
  export { OutputStream };
@@ -1,5 +1,5 @@
1
1
  export namespace WasiCliStdin {
2
2
  export function getStdin(): InputStream;
3
3
  }
4
- import type { InputStream } from '../interfaces/wasi-io-streams.js';
4
+ import type { InputStream } from './wasi-io-streams.js';
5
5
  export { InputStream };
@@ -1,5 +1,5 @@
1
1
  export namespace WasiCliStdout {
2
2
  export function getStdout(): OutputStream;
3
3
  }
4
- import type { OutputStream } from '../interfaces/wasi-io-streams.js';
4
+ import type { OutputStream } from './wasi-io-streams.js';
5
5
  export { OutputStream };
@@ -5,5 +5,5 @@ export namespace WasiCliTerminalStderr {
5
5
  */
6
6
  export function getTerminalStderr(): TerminalOutput | undefined;
7
7
  }
8
- import type { TerminalOutput } from '../interfaces/wasi-cli-terminal-output.js';
8
+ import type { TerminalOutput } from './wasi-cli-terminal-output.js';
9
9
  export { TerminalOutput };
@@ -5,5 +5,5 @@ export namespace WasiCliTerminalStdin {
5
5
  */
6
6
  export function getTerminalStdin(): TerminalInput | undefined;
7
7
  }
8
- import type { TerminalInput } from '../interfaces/wasi-cli-terminal-input.js';
8
+ import type { TerminalInput } from './wasi-cli-terminal-input.js';
9
9
  export { TerminalInput };
@@ -5,5 +5,5 @@ export namespace WasiCliTerminalStdout {
5
5
  */
6
6
  export function getTerminalStdout(): TerminalOutput | undefined;
7
7
  }
8
- import type { TerminalOutput } from '../interfaces/wasi-cli-terminal-output.js';
8
+ import type { TerminalOutput } from './wasi-cli-terminal-output.js';
9
9
  export { TerminalOutput };
@@ -23,7 +23,7 @@ export namespace WasiClocksMonotonicClock {
23
23
  */
24
24
  export function subscribeDuration(when: Duration): Pollable;
25
25
  }
26
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
26
+ import type { Pollable } from './wasi-io-poll.js';
27
27
  export { Pollable };
28
28
  /**
29
29
  * An instant in time, in nanoseconds. An instant is relative to an
@@ -4,5 +4,5 @@ export namespace WasiFilesystemPreopens {
4
4
  */
5
5
  export function getDirectories(): [Descriptor, string][];
6
6
  }
7
- import type { Descriptor } from '../interfaces/wasi-filesystem-types.js';
7
+ import type { Descriptor } from './wasi-filesystem-types.js';
8
8
  export { Descriptor };
@@ -260,13 +260,13 @@ export namespace WasiFilesystemTypes {
260
260
  */
261
261
  export function filesystemErrorCode(err: Error): ErrorCode | undefined;
262
262
  }
263
- import type { InputStream } from '../interfaces/wasi-io-streams.js';
263
+ import type { InputStream } from './wasi-io-streams.js';
264
264
  export { InputStream };
265
- import type { OutputStream } from '../interfaces/wasi-io-streams.js';
265
+ import type { OutputStream } from './wasi-io-streams.js';
266
266
  export { OutputStream };
267
- import type { Error } from '../interfaces/wasi-io-streams.js';
267
+ import type { Error } from './wasi-io-streams.js';
268
268
  export { Error };
269
- import type { Datetime } from '../interfaces/wasi-clocks-wall-clock.js';
269
+ import type { Datetime } from './wasi-clocks-wall-clock.js';
270
270
  export { Datetime };
271
271
  /**
272
272
  * File size or length of a region within a file.
@@ -639,6 +639,10 @@ export interface MetadataHashValue {
639
639
  upper: bigint,
640
640
  }
641
641
 
642
+ export class DirectoryEntryStream {
643
+ readDirectoryEntry(): DirectoryEntry | undefined;
644
+ }
645
+
642
646
  export class Descriptor {
643
647
  readViaStream(offset: Filesize): InputStream;
644
648
  writeViaStream(offset: Filesize): OutputStream;
@@ -668,7 +672,3 @@ export class Descriptor {
668
672
  metadataHash(): MetadataHashValue;
669
673
  metadataHashAt(pathFlags: PathFlags, path: string): MetadataHashValue;
670
674
  }
671
-
672
- export class DirectoryEntryStream {
673
- readDirectoryEntry(): DirectoryEntry | undefined;
674
- }
@@ -13,7 +13,7 @@ export namespace WasiHttpIncomingHandler {
13
13
  */
14
14
  export function handle(request: IncomingRequest, responseOut: ResponseOutparam): void;
15
15
  }
16
- import type { IncomingRequest } from '../interfaces/wasi-http-types.js';
16
+ import type { IncomingRequest } from './wasi-http-types.js';
17
17
  export { IncomingRequest };
18
- import type { ResponseOutparam } from '../interfaces/wasi-http-types.js';
18
+ import type { ResponseOutparam } from './wasi-http-types.js';
19
19
  export { ResponseOutparam };
@@ -13,11 +13,11 @@ export namespace WasiHttpOutgoingHandler {
13
13
  */
14
14
  export function handle(request: OutgoingRequest, options: RequestOptions | undefined): FutureIncomingResponse;
15
15
  }
16
- import type { OutgoingRequest } from '../interfaces/wasi-http-types.js';
16
+ import type { OutgoingRequest } from './wasi-http-types.js';
17
17
  export { OutgoingRequest };
18
- import type { RequestOptions } from '../interfaces/wasi-http-types.js';
18
+ import type { RequestOptions } from './wasi-http-types.js';
19
19
  export { RequestOptions };
20
- import type { FutureIncomingResponse } from '../interfaces/wasi-http-types.js';
20
+ import type { FutureIncomingResponse } from './wasi-http-types.js';
21
21
  export { FutureIncomingResponse };
22
- import type { ErrorCode } from '../interfaces/wasi-http-types.js';
22
+ import type { ErrorCode } from './wasi-http-types.js';
23
23
  export { ErrorCode };
@@ -354,15 +354,15 @@ export namespace WasiHttpTypes {
354
354
  * `output-stream` child.
355
355
  */
356
356
  }
357
- import type { Duration } from '../interfaces/wasi-clocks-monotonic-clock.js';
357
+ import type { Duration } from './wasi-clocks-monotonic-clock.js';
358
358
  export { Duration };
359
- import type { InputStream } from '../interfaces/wasi-io-streams.js';
359
+ import type { InputStream } from './wasi-io-streams.js';
360
360
  export { InputStream };
361
- import type { OutputStream } from '../interfaces/wasi-io-streams.js';
361
+ import type { OutputStream } from './wasi-io-streams.js';
362
362
  export { OutputStream };
363
- import type { Error as IoError } from '../interfaces/wasi-io-error.js';
363
+ import type { Error as IoError } from './wasi-io-error.js';
364
364
  export { IoError };
365
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
365
+ import type { Pollable } from './wasi-io-poll.js';
366
366
  export { Pollable };
367
367
  /**
368
368
  * This type corresponds to HTTP standard Methods.
@@ -629,10 +629,6 @@ export type Trailers = Fields;
629
629
  export type StatusCode = number;
630
630
  export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };
631
631
 
632
- export class ResponseOutparam {
633
- static set(param: ResponseOutparam, response: Result<OutgoingResponse, ErrorCode>): void;
634
- }
635
-
636
632
  export class OutgoingResponse {
637
633
  constructor(headers: Headers)
638
634
  statusCode(): StatusCode;
@@ -646,42 +642,22 @@ export class OutgoingBody {
646
642
  static finish(this_: OutgoingBody, trailers: Trailers | undefined): void;
647
643
  }
648
644
 
649
- export class Fields {
650
- constructor()
651
- static fromList(entries: [FieldKey, FieldValue][]): Fields;
652
- get(name: FieldKey): FieldValue[];
653
- has(name: FieldKey): boolean;
654
- set(name: FieldKey, value: FieldValue[]): void;
655
- 'delete'(name: FieldKey): void;
656
- append(name: FieldKey, value: FieldValue): void;
657
- entries(): [FieldKey, FieldValue][];
658
- clone(): Fields;
659
- }
660
-
661
- export class IncomingBody {
662
- stream(): InputStream;
663
- static finish(this_: IncomingBody): FutureTrailers;
664
- }
665
-
666
- export class FutureIncomingResponse {
667
- subscribe(): Pollable;
668
- get(): Result<Result<IncomingResponse, ErrorCode>, void> | undefined;
669
- }
670
-
671
- export class FutureTrailers {
672
- subscribe(): Pollable;
673
- get(): Result<Result<Trailers | undefined, ErrorCode>, void> | undefined;
674
- }
675
-
676
- export class IncomingRequest {
677
- method(): Method;
678
- pathWithQuery(): string | undefined;
679
- scheme(): Scheme | undefined;
680
- authority(): string | undefined;
645
+ export class IncomingResponse {
646
+ status(): StatusCode;
681
647
  headers(): Headers;
682
648
  consume(): IncomingBody;
683
649
  }
684
650
 
651
+ export class RequestOptions {
652
+ constructor()
653
+ connectTimeout(): Duration | undefined;
654
+ setConnectTimeout(duration: Duration | undefined): void;
655
+ firstByteTimeout(): Duration | undefined;
656
+ setFirstByteTimeout(duration: Duration | undefined): void;
657
+ betweenBytesTimeout(): Duration | undefined;
658
+ setBetweenBytesTimeout(duration: Duration | undefined): void;
659
+ }
660
+
685
661
  export class OutgoingRequest {
686
662
  constructor(headers: Headers)
687
663
  body(): OutgoingBody;
@@ -696,18 +672,42 @@ export class OutgoingRequest {
696
672
  headers(): Headers;
697
673
  }
698
674
 
699
- export class IncomingResponse {
700
- status(): StatusCode;
675
+ export class IncomingRequest {
676
+ method(): Method;
677
+ pathWithQuery(): string | undefined;
678
+ scheme(): Scheme | undefined;
679
+ authority(): string | undefined;
701
680
  headers(): Headers;
702
681
  consume(): IncomingBody;
703
682
  }
704
683
 
705
- export class RequestOptions {
684
+ export class Fields {
706
685
  constructor()
707
- connectTimeout(): Duration | undefined;
708
- setConnectTimeout(duration: Duration | undefined): void;
709
- firstByteTimeout(): Duration | undefined;
710
- setFirstByteTimeout(duration: Duration | undefined): void;
711
- betweenBytesTimeout(): Duration | undefined;
712
- setBetweenBytesTimeout(duration: Duration | undefined): void;
686
+ static fromList(entries: [FieldKey, FieldValue][]): Fields;
687
+ get(name: FieldKey): FieldValue[];
688
+ has(name: FieldKey): boolean;
689
+ set(name: FieldKey, value: FieldValue[]): void;
690
+ 'delete'(name: FieldKey): void;
691
+ append(name: FieldKey, value: FieldValue): void;
692
+ entries(): [FieldKey, FieldValue][];
693
+ clone(): Fields;
694
+ }
695
+
696
+ export class FutureTrailers {
697
+ subscribe(): Pollable;
698
+ get(): Result<Result<Trailers | undefined, ErrorCode>, void> | undefined;
699
+ }
700
+
701
+ export class FutureIncomingResponse {
702
+ subscribe(): Pollable;
703
+ get(): Result<Result<IncomingResponse, ErrorCode>, void> | undefined;
704
+ }
705
+
706
+ export class IncomingBody {
707
+ stream(): InputStream;
708
+ static finish(this_: IncomingBody): FutureTrailers;
709
+ }
710
+
711
+ export class ResponseOutparam {
712
+ static set(param: ResponseOutparam, response: Result<OutgoingResponse, ErrorCode>): void;
713
713
  }
@@ -188,9 +188,9 @@ export namespace WasiIoStreams {
188
188
  * is ready for reading, before performing the `splice`.
189
189
  */
190
190
  }
191
- import type { Error } from '../interfaces/wasi-io-error.js';
191
+ import type { Error } from './wasi-io-error.js';
192
192
  export { Error };
193
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
193
+ import type { Pollable } from './wasi-io-poll.js';
194
194
  export { Pollable };
195
195
  /**
196
196
  * An error for input-stream and output-stream operations.
@@ -4,5 +4,5 @@ export namespace WasiSocketsInstanceNetwork {
4
4
  */
5
5
  export function instanceNetwork(): Network;
6
6
  }
7
- import type { Network } from '../interfaces/wasi-sockets-network.js';
7
+ import type { Network } from './wasi-sockets-network.js';
8
8
  export { Network };
@@ -45,13 +45,13 @@ export namespace WasiSocketsIpNameLookup {
45
45
  * It's planned to be removed when `future` is natively supported in Preview3.
46
46
  */
47
47
  }
48
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
48
+ import type { Pollable } from './wasi-io-poll.js';
49
49
  export { Pollable };
50
- import type { Network } from '../interfaces/wasi-sockets-network.js';
50
+ import type { Network } from './wasi-sockets-network.js';
51
51
  export { Network };
52
- import type { ErrorCode } from '../interfaces/wasi-sockets-network.js';
52
+ import type { ErrorCode } from './wasi-sockets-network.js';
53
53
  export { ErrorCode };
54
- import type { IpAddress } from '../interfaces/wasi-sockets-network.js';
54
+ import type { IpAddress } from './wasi-sockets-network.js';
55
55
  export { IpAddress };
56
56
 
57
57
  export class ResolveAddressStream {
@@ -23,11 +23,11 @@ export namespace WasiSocketsTcpCreateSocket {
23
23
  */
24
24
  export function createTcpSocket(addressFamily: IpAddressFamily): TcpSocket;
25
25
  }
26
- import type { Network } from '../interfaces/wasi-sockets-network.js';
26
+ import type { Network } from './wasi-sockets-network.js';
27
27
  export { Network };
28
- import type { ErrorCode } from '../interfaces/wasi-sockets-network.js';
28
+ import type { ErrorCode } from './wasi-sockets-network.js';
29
29
  export { ErrorCode };
30
- import type { IpAddressFamily } from '../interfaces/wasi-sockets-network.js';
30
+ import type { IpAddressFamily } from './wasi-sockets-network.js';
31
31
  export { IpAddressFamily };
32
- import type { TcpSocket } from '../interfaces/wasi-sockets-tcp.js';
32
+ import type { TcpSocket } from './wasi-sockets-tcp.js';
33
33
  export { TcpSocket };
@@ -302,21 +302,21 @@ export namespace WasiSocketsTcp {
302
302
  * - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
303
303
  */
304
304
  }
305
- import type { InputStream } from '../interfaces/wasi-io-streams.js';
305
+ import type { InputStream } from './wasi-io-streams.js';
306
306
  export { InputStream };
307
- import type { OutputStream } from '../interfaces/wasi-io-streams.js';
307
+ import type { OutputStream } from './wasi-io-streams.js';
308
308
  export { OutputStream };
309
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
309
+ import type { Pollable } from './wasi-io-poll.js';
310
310
  export { Pollable };
311
- import type { Duration } from '../interfaces/wasi-clocks-monotonic-clock.js';
311
+ import type { Duration } from './wasi-clocks-monotonic-clock.js';
312
312
  export { Duration };
313
- import type { Network } from '../interfaces/wasi-sockets-network.js';
313
+ import type { Network } from './wasi-sockets-network.js';
314
314
  export { Network };
315
- import type { ErrorCode } from '../interfaces/wasi-sockets-network.js';
315
+ import type { ErrorCode } from './wasi-sockets-network.js';
316
316
  export { ErrorCode };
317
- import type { IpSocketAddress } from '../interfaces/wasi-sockets-network.js';
317
+ import type { IpSocketAddress } from './wasi-sockets-network.js';
318
318
  export { IpSocketAddress };
319
- import type { IpAddressFamily } from '../interfaces/wasi-sockets-network.js';
319
+ import type { IpAddressFamily } from './wasi-sockets-network.js';
320
320
  export { IpAddressFamily };
321
321
  /**
322
322
  * # Variants
@@ -23,11 +23,11 @@ export namespace WasiSocketsUdpCreateSocket {
23
23
  */
24
24
  export function createUdpSocket(addressFamily: IpAddressFamily): UdpSocket;
25
25
  }
26
- import type { Network } from '../interfaces/wasi-sockets-network.js';
26
+ import type { Network } from './wasi-sockets-network.js';
27
27
  export { Network };
28
- import type { ErrorCode } from '../interfaces/wasi-sockets-network.js';
28
+ import type { ErrorCode } from './wasi-sockets-network.js';
29
29
  export { ErrorCode };
30
- import type { IpAddressFamily } from '../interfaces/wasi-sockets-network.js';
30
+ import type { IpAddressFamily } from './wasi-sockets-network.js';
31
31
  export { IpAddressFamily };
32
- import type { UdpSocket } from '../interfaces/wasi-sockets-udp.js';
32
+ import type { UdpSocket } from './wasi-sockets-udp.js';
33
33
  export { UdpSocket };
@@ -220,15 +220,15 @@ export namespace WasiSocketsUdp {
220
220
  * It's planned to be removed when `future` is natively supported in Preview3.
221
221
  */
222
222
  }
223
- import type { Pollable } from '../interfaces/wasi-io-poll.js';
223
+ import type { Pollable } from './wasi-io-poll.js';
224
224
  export { Pollable };
225
- import type { Network } from '../interfaces/wasi-sockets-network.js';
225
+ import type { Network } from './wasi-sockets-network.js';
226
226
  export { Network };
227
- import type { ErrorCode } from '../interfaces/wasi-sockets-network.js';
227
+ import type { ErrorCode } from './wasi-sockets-network.js';
228
228
  export { ErrorCode };
229
- import type { IpSocketAddress } from '../interfaces/wasi-sockets-network.js';
229
+ import type { IpSocketAddress } from './wasi-sockets-network.js';
230
230
  export { IpSocketAddress };
231
- import type { IpAddressFamily } from '../interfaces/wasi-sockets-network.js';
231
+ import type { IpAddressFamily } from './wasi-sockets-network.js';
232
232
  export { IpAddressFamily };
233
233
  /**
234
234
  * A received datagram.