@bytecodealliance/preview2-shim 0.15.1 → 0.16.0

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.
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
 
@@ -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;
@@ -174,26 +176,38 @@ const responseOutparamCreate = ResponseOutparam._create;
174
176
  delete ResponseOutparam._create;
175
177
 
176
178
  class RequestOptions {
177
- #connectTimeoutMs;
178
- #firstByteTimeoutMs;
179
- #betweenBytesTimeoutMs;
179
+ #connectTimeout;
180
+ #firstByteTimeout;
181
+ #betweenBytesTimeout;
182
+ //The WASI Duration is nanoseconds, js timers are set with Ms.
183
+ //We store the data as nanos, but provide TimeoutMs methods for
184
+ //convenience in setting timers elsewhere
185
+ connectTimeout() {
186
+ return this.#connectTimeout;
187
+ }
180
188
  connectTimeoutMs() {
181
- return this.#connectTimeoutMs;
189
+ return this.#connectTimeout / 1_000_000;
182
190
  }
183
- setConnectTimeoutMs(duration) {
184
- this.#connectTimeoutMs = duration;
191
+ setConnectTimeout(duration) {
192
+ this.#connectTimeout = duration;
193
+ }
194
+ firstByteTimeout() {
195
+ return this.#firstByteTimeout;
185
196
  }
186
197
  firstByteTimeoutMs() {
187
- return this.#firstByteTimeoutMs;
198
+ return this.#firstByteTimeout / 1_000_000;
199
+ }
200
+ setFirstByteTimeout(duration) {
201
+ this.#firstByteTimeout = duration;
188
202
  }
189
- setFirstByteTimeoutMs(duration) {
190
- this.#firstByteTimeoutMs = duration;
203
+ betweenBytesTimeout() {
204
+ return this.#betweenBytesTimeout;
191
205
  }
192
206
  betweenBytesTimeoutMs() {
193
- return this.#betweenBytesTimeoutMs;
207
+ return this.#betweenBytesTimeout / 1_000_000;
194
208
  }
195
- setBetweenBytesTimeoutMs(duration) {
196
- this.#betweenBytesTimeoutMs = duration;
209
+ setBetweenBytesTimeout(duration) {
210
+ this.#betweenBytesTimeout = duration;
197
211
  }
198
212
  }
199
213
 
@@ -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
+ randomBytes[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.0",
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",