@bytecodealliance/preview2-shim 0.17.0 → 0.17.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.
@@ -29,16 +29,19 @@ export const environment = {
29
29
  };
30
30
 
31
31
  class ComponentExit extends Error {
32
- constructor(ok) {
33
- super(`Component exited ${ok ? 'successfully' : 'with error'}`);
32
+ constructor(code) {
33
+ super(`Component exited ${code === 0 ? 'successfully' : 'with error'}`);
34
34
  this.exitError = true;
35
- this.ok = ok;
35
+ this.code = code;
36
36
  }
37
37
  }
38
38
 
39
39
  export const exit = {
40
40
  exit (status) {
41
- throw new ComponentExit(status.tag === 'err' ? true : false);
41
+ throw new ComponentExit(status.tag === 'err' ? 1 : 0);
42
+ },
43
+ exitWithCode (code) {
44
+ throw new ComponentExit(code);
42
45
  }
43
46
  };
44
47
 
@@ -31,7 +31,7 @@ import {
31
31
  } from "./worker-sockets.js";
32
32
  import { Socket, Server } from "node:net";
33
33
 
34
- const winOrMac = process.platform === 'win32' || process.platform === 'darwin';
34
+ const win = process.platform === 'win32';
35
35
 
36
36
  /**
37
37
  * @typedef {import("../../types/interfaces/wasi-sockets-network.js").IpSocketAddress} IpSocketAddress
@@ -268,7 +268,7 @@ export function socketTcpGetRemoteAddress(id) {
268
268
  export function socketTcpShutdown(id, _shutdownType) {
269
269
  const socket = tcpSockets.get(id);
270
270
  if (socket.state !== SOCKET_STATE_CONNECTION) throw "invalid-state";
271
- if (winOrMac && socket.tcpSocket.destroySoon)
271
+ if (win && socket.tcpSocket.destroySoon)
272
272
  socket.tcpSocket.destroySoon();
273
273
  else
274
274
  socket.tcpSocket.destroy();
@@ -393,8 +393,9 @@ function handle(call, id, payload) {
393
393
  return;
394
394
  }
395
395
  case HTTP_OUTGOING_BODY_DISPOSE:
396
- if (!streams.delete(id))
397
- throw new Error("wasi-io trap: stream not found to dispose");
396
+ if (debug && !streams.has(id))
397
+ console.warn(`wasi-io: stream ${id} not found to dispose`);
398
+ streams.delete(id);
398
399
  return;
399
400
  case HTTP_SERVER_START:
400
401
  return startHttpServer(id, payload);
package/lib/nodejs/cli.js CHANGED
@@ -47,6 +47,9 @@ export const exit = {
47
47
  exit(status) {
48
48
  process.exit(status.tag === "err" ? 1 : 0);
49
49
  },
50
+ exitWithCode(code) {
51
+ process.exit(code);
52
+ }
50
53
  };
51
54
 
52
55
  // Stdin is created as a FILE descriptor
@@ -368,19 +368,16 @@ class Descriptor {
368
368
  }
369
369
  }
370
370
  try {
371
- const fd = openSync(fullPath, fsOpenFlags);
371
+ const fd = openSync(fullPath.endsWith('/') ? fullPath.slice(0, -1) : fullPath, fsOpenFlags);
372
372
  const descriptor = descriptorCreate(
373
373
  fd,
374
374
  descriptorFlags,
375
375
  fullPath,
376
376
  preopenEntries
377
377
  );
378
- if (fullPath.endsWith("/") && isWindows) {
379
- // check if its a directory
380
- if (descriptor.getType() !== "directory") {
381
- descriptor[symbolDispose]();
382
- throw "not-directory";
383
- }
378
+ if (fullPath.endsWith('/') && descriptor.getType() !== 'directory') {
379
+ descriptor[symbolDispose]();
380
+ throw "not-directory";
384
381
  }
385
382
  return descriptor;
386
383
  } catch (e) {
@@ -671,6 +668,7 @@ function convertFsError(e) {
671
668
  case "ENOSPC":
672
669
  return "insufficient-space";
673
670
  case "ENOTDIR":
671
+ case 'ERR_FS_EISDIR':
674
672
  return "not-directory";
675
673
  case "ENOTEMPTY":
676
674
  return "not-empty";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.17.0",
3
+ "version": "0.17.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",
@@ -569,12 +569,6 @@ export class Descriptor {
569
569
  /**
570
570
  * Open a file or directory.
571
571
  *
572
- * The returned descriptor is not guaranteed to be the lowest-numbered
573
- * descriptor not currently open/ it is randomized to prevent applications
574
- * from depending on making assumptions about indexes, since this is
575
- * error-prone in multi-threaded contexts. The returned descriptor is
576
- * guaranteed to be less than 2**31.
577
- *
578
572
  * If `flags` contains `descriptor-flags::mutate-directory`, and the base
579
573
  * descriptor doesn't have `descriptor-flags::mutate-directory` set,
580
574
  * `open-at` fails with `error-code::read-only`.
@@ -255,7 +255,7 @@ export interface ErrorCodeInternalError {
255
255
  */
256
256
  export type HeaderError = HeaderErrorInvalidSyntax | HeaderErrorForbidden | HeaderErrorImmutable;
257
257
  /**
258
- * This error indicates that a `field-key` or `field-value` was
258
+ * This error indicates that a `field-name` or `field-value` was
259
259
  * syntactically invalid when used with an operation that sets headers in a
260
260
  * `fields`.
261
261
  */
@@ -263,7 +263,7 @@ export interface HeaderErrorInvalidSyntax {
263
263
  tag: 'invalid-syntax',
264
264
  }
265
265
  /**
266
- * This error indicates that a forbidden `field-key` was used when trying
266
+ * This error indicates that a forbidden `field-name` was used when trying
267
267
  * to set a header in a `fields`.
268
268
  */
269
269
  export interface HeaderErrorForbidden {
@@ -278,8 +278,22 @@ export interface HeaderErrorImmutable {
278
278
  }
279
279
  /**
280
280
  * Field keys are always strings.
281
+ *
282
+ * Field keys should always be treated as case insensitive by the `fields`
283
+ * resource for the purposes of equality checking.
284
+ *
285
+ * # Deprecation
286
+ *
287
+ * This type has been deprecated in favor of the `field-name` type.
281
288
  */
282
289
  export type FieldKey = string;
290
+ /**
291
+ * Field names are always strings.
292
+ *
293
+ * Field names should always be treated as case insensitive by the `fields`
294
+ * resource for the purposes of equality checking.
295
+ */
296
+ export type FieldName = FieldKey;
283
297
  /**
284
298
  * Field values should always be ASCII strings. However, in
285
299
  * reality, HTTP implementations often have to interpret malformed values,
@@ -312,68 +326,71 @@ export class Fields {
312
326
  *
313
327
  * The resulting `fields` is mutable.
314
328
  *
315
- * The list represents each key-value pair in the Fields. Keys
329
+ * The list represents each name-value pair in the Fields. Names
316
330
  * which have multiple values are represented by multiple entries in this
317
- * list with the same key.
331
+ * list with the same name.
318
332
  *
319
- * The tuple is a pair of the field key, represented as a string, and
333
+ * The tuple is a pair of the field name, represented as a string, and
320
334
  * Value, represented as a list of bytes.
321
335
  *
322
- * An error result will be returned if any `field-key` or `field-value` is
336
+ * An error result will be returned if any `field-name` or `field-value` is
323
337
  * syntactically invalid, or if a field is forbidden.
324
338
  */
325
- static fromList(entries: Array<[FieldKey, FieldValue]>): Fields;
339
+ static fromList(entries: Array<[FieldName, FieldValue]>): Fields;
326
340
  /**
327
- * Get all of the values corresponding to a key. If the key is not present
341
+ * Get all of the values corresponding to a name. If the name is not present
328
342
  * in this `fields` or is syntactically invalid, an empty list is returned.
329
- * However, if the key is present but empty, this is represented by a list
343
+ * However, if the name is present but empty, this is represented by a list
330
344
  * with one or more empty field-values present.
331
345
  */
332
- get(name: FieldKey): Array<FieldValue>;
346
+ get(name: FieldName): Array<FieldValue>;
333
347
  /**
334
- * Returns `true` when the key is present in this `fields`. If the key is
348
+ * Returns `true` when the name is present in this `fields`. If the name is
335
349
  * syntactically invalid, `false` is returned.
336
350
  */
337
- has(name: FieldKey): boolean;
351
+ has(name: FieldName): boolean;
338
352
  /**
339
- * Set all of the values for a key. Clears any existing values for that
340
- * key, if they have been set.
353
+ * Set all of the values for a name. Clears any existing values for that
354
+ * name, if they have been set.
341
355
  *
342
356
  * Fails with `header-error.immutable` if the `fields` are immutable.
343
357
  *
344
- * Fails with `header-error.invalid-syntax` if the `field-key` or any of
358
+ * Fails with `header-error.invalid-syntax` if the `field-name` or any of
345
359
  * the `field-value`s are syntactically invalid.
346
360
  */
347
- set(name: FieldKey, value: Array<FieldValue>): void;
361
+ set(name: FieldName, value: Array<FieldValue>): void;
348
362
  /**
349
- * Delete all values for a key. Does nothing if no values for the key
363
+ * Delete all values for a name. Does nothing if no values for the name
350
364
  * exist.
351
365
  *
352
366
  * Fails with `header-error.immutable` if the `fields` are immutable.
353
367
  *
354
- * Fails with `header-error.invalid-syntax` if the `field-key` is
368
+ * Fails with `header-error.invalid-syntax` if the `field-name` is
355
369
  * syntactically invalid.
356
370
  */
357
- 'delete'(name: FieldKey): void;
371
+ 'delete'(name: FieldName): void;
358
372
  /**
359
- * Append a value for a key. Does not change or delete any existing
360
- * values for that key.
373
+ * Append a value for a name. Does not change or delete any existing
374
+ * values for that name.
361
375
  *
362
376
  * Fails with `header-error.immutable` if the `fields` are immutable.
363
377
  *
364
- * Fails with `header-error.invalid-syntax` if the `field-key` or
378
+ * Fails with `header-error.invalid-syntax` if the `field-name` or
365
379
  * `field-value` are syntactically invalid.
366
380
  */
367
- append(name: FieldKey, value: FieldValue): void;
381
+ append(name: FieldName, value: FieldValue): void;
368
382
  /**
369
- * Retrieve the full set of keys and values in the Fields. Like the
370
- * constructor, the list represents each key-value pair.
383
+ * Retrieve the full set of names and values in the Fields. Like the
384
+ * constructor, the list represents each name-value pair.
371
385
  *
372
- * The outer list represents each key-value pair in the Fields. Keys
386
+ * The outer list represents each name-value pair in the Fields. Names
373
387
  * which have multiple values are represented by multiple entries in this
374
- * list with the same key.
388
+ * list with the same name.
389
+ *
390
+ * The names and values are always returned in the original casing and in
391
+ * the order in which they will be serialized for transport.
375
392
  */
376
- entries(): Array<[FieldKey, FieldValue]>;
393
+ entries(): Array<[FieldName, FieldValue]>;
377
394
  /**
378
395
  * Make a deep copy of the Fields. Equivalent in behavior to calling the
379
396
  * `fields` constructor on the return value of `entries`. The resulting
@@ -14,6 +14,9 @@ export type StreamError = StreamErrorLastOperationFailed | StreamErrorClosed;
14
14
  * The last operation (a write or flush) failed before completion.
15
15
  *
16
16
  * More information is available in the `error` payload.
17
+ *
18
+ * After this, the stream will be closed. All future operations return
19
+ * `stream-error::closed`.
17
20
  */
18
21
  export interface StreamErrorLastOperationFailed {
19
22
  tag: 'last-operation-failed',
@@ -53,7 +53,7 @@ export class ResolveAddressStream {
53
53
  /**
54
54
  * Create a `pollable` which will resolve once the stream is ready for I/O.
55
55
  *
56
- * Note: this function is here for WASI Preview2 only.
56
+ * Note: this function is here for WASI 0.2 only.
57
57
  * It's planned to be removed when `future` is natively supported in Preview3.
58
58
  */
59
59
  subscribe(): Pollable;
@@ -332,7 +332,7 @@ export class TcpSocket {
332
332
  * See <https://github.com/WebAssembly/wasi-sockets/blob/main/TcpSocketOperationalSemantics.md#pollable-readiness>
333
333
  * for more information.
334
334
  *
335
- * Note: this function is here for WASI Preview2 only.
335
+ * Note: this function is here for WASI 0.2 only.
336
336
  * It's planned to be removed when `future` is natively supported in Preview3.
337
337
  */
338
338
  subscribe(): Pollable;
@@ -82,7 +82,7 @@ export class IncomingDatagramStream {
82
82
  /**
83
83
  * Create a `pollable` which will resolve once the stream is ready to receive again.
84
84
  *
85
- * Note: this function is here for WASI Preview2 only.
85
+ * Note: this function is here for WASI 0.2 only.
86
86
  * It's planned to be removed when `future` is natively supported in Preview3.
87
87
  */
88
88
  subscribe(): Pollable;
@@ -143,7 +143,7 @@ export class OutgoingDatagramStream {
143
143
  /**
144
144
  * Create a `pollable` which will resolve once the stream is ready to send again.
145
145
  *
146
- * Note: this function is here for WASI Preview2 only.
146
+ * Note: this function is here for WASI 0.2 only.
147
147
  * It's planned to be removed when `future` is natively supported in Preview3.
148
148
  */
149
149
  subscribe(): Pollable;
@@ -290,7 +290,7 @@ export class UdpSocket {
290
290
  /**
291
291
  * Create a `pollable` which will resolve once the socket is ready for I/O.
292
292
  *
293
- * Note: this function is here for WASI Preview2 only.
293
+ * Note: this function is here for WASI 0.2 only.
294
294
  * It's planned to be removed when `future` is natively supported in Preview3.
295
295
  */
296
296
  subscribe(): Pollable;