@bytecodealliance/preview2-shim 0.20.0 → 0.20.2

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/README.md CHANGED
@@ -6,6 +6,10 @@ Node.js support is fully tested and conformant against the Wasmtime test suite.
6
6
 
7
7
  Browser support is considered experimental, and not currently suitable for production applications.
8
8
 
9
+ The Node.js implementation owns its worker artifact. Direct package use and supported downstream
10
+ bundlers should resolve it through the public shim imports; applications do not need to import or
11
+ copy files from `dist/io`.
12
+
9
13
  # Features
10
14
 
11
15
  ## WASI Shim object for easy instantiation
@@ -38,7 +38,7 @@ declare class Descriptor implements TypesNamespace.Descriptor {
38
38
  advise(offset: Filesize, length: Filesize, advice: TypesNamespace.Advice): void;
39
39
  syncData(): void;
40
40
  getFlags(): TypesNamespace.DescriptorFlags;
41
- getType(): "directory" | "regular-file" | "unknown" | "fifo";
41
+ getType(): "directory" | "fifo" | "regular-file" | "unknown";
42
42
  setSize(size: bigint): void;
43
43
  setTimes(dataAccessTimestamp: any, dataModificationTimestamp: any): void;
44
44
  read(length: bigint, offset: bigint): [Uint8Array<ArrayBufferLike>, boolean];
@@ -29,6 +29,17 @@ export declare const OUTPUT_STREAM_BLOCKING_SPLICE: number;
29
29
  export declare const OUTPUT_STREAM_SUBSCRIBE: number;
30
30
  export declare const OUTPUT_STREAM_DISPOSE: number;
31
31
  export declare const OUTPUT_STREAM_GET_TOTAL_BYTES: number;
32
+ export declare const FILESYSTEM_DESCRIPTOR_OPEN: number;
33
+ export declare const FILESYSTEM_DESCRIPTOR_CLOSE: number;
34
+ export declare const FILESYSTEM_DESCRIPTOR_SYNC_DATA: number;
35
+ export declare const FILESYSTEM_DESCRIPTOR_GET_TYPE: number;
36
+ export declare const FILESYSTEM_DESCRIPTOR_SET_SIZE: number;
37
+ export declare const FILESYSTEM_DESCRIPTOR_SET_TIMES: number;
38
+ export declare const FILESYSTEM_DESCRIPTOR_READ: number;
39
+ export declare const FILESYSTEM_DESCRIPTOR_WRITE: number;
40
+ export declare const FILESYSTEM_DESCRIPTOR_SYNC: number;
41
+ export declare const FILESYSTEM_DESCRIPTOR_STAT: number;
42
+ export declare const FILESYSTEM_DESCRIPTOR_METADATA_HASH: number;
32
43
  export declare const POLL_POLLABLE_READY: number;
33
44
  export declare const POLL_POLLABLE_BLOCK: number;
34
45
  export declare const POLL_POLLABLE_DISPOSE: number;
package/dist/io/calls.js CHANGED
@@ -35,6 +35,18 @@ export const OUTPUT_STREAM_BLOCKING_SPLICE = ++call_id << CALL_SHIFT;
35
35
  export const OUTPUT_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
36
36
  export const OUTPUT_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
37
37
  export const OUTPUT_STREAM_GET_TOTAL_BYTES = ++call_id << CALL_SHIFT;
38
+ // Filesystem descriptors
39
+ export const FILESYSTEM_DESCRIPTOR_OPEN = ++call_id << CALL_SHIFT;
40
+ export const FILESYSTEM_DESCRIPTOR_CLOSE = ++call_id << CALL_SHIFT;
41
+ export const FILESYSTEM_DESCRIPTOR_SYNC_DATA = ++call_id << CALL_SHIFT;
42
+ export const FILESYSTEM_DESCRIPTOR_GET_TYPE = ++call_id << CALL_SHIFT;
43
+ export const FILESYSTEM_DESCRIPTOR_SET_SIZE = ++call_id << CALL_SHIFT;
44
+ export const FILESYSTEM_DESCRIPTOR_SET_TIMES = ++call_id << CALL_SHIFT;
45
+ export const FILESYSTEM_DESCRIPTOR_READ = ++call_id << CALL_SHIFT;
46
+ export const FILESYSTEM_DESCRIPTOR_WRITE = ++call_id << CALL_SHIFT;
47
+ export const FILESYSTEM_DESCRIPTOR_SYNC = ++call_id << CALL_SHIFT;
48
+ export const FILESYSTEM_DESCRIPTOR_STAT = ++call_id << CALL_SHIFT;
49
+ export const FILESYSTEM_DESCRIPTOR_METADATA_HASH = ++call_id << CALL_SHIFT;
38
50
  // Io Poll
39
51
  export const POLL_POLLABLE_READY = ++call_id << CALL_SHIFT;
40
52
  export const POLL_POLLABLE_BLOCK = ++call_id << CALL_SHIFT;
@@ -0,0 +1,29 @@
1
+ export declare function openFileResource(path: string, flags: number): Promise<{
2
+ id: number;
3
+ type: any;
4
+ }>;
5
+ export declare function retainFileResource(id: number): number;
6
+ export declare function releaseFileResource(id: number): Promise<void>;
7
+ export declare function fileResourceFd(id: number): number;
8
+ export declare function syncFileResourceData(id: number): Promise<void>;
9
+ export declare function getFileResourceType(id: number): Promise<"block-device" | "character-device" | "directory" | "fifo" | "regular-file" | "socket" | "symbolic-link" | "unknown">;
10
+ export declare function setFileResourceSize(id: number, size: number): Promise<void>;
11
+ export declare function setFileResourceTimes(id: number, atime: number, mtime: number): Promise<void>;
12
+ export declare function readFileResource(id: number, length: number, offset: number): Promise<{
13
+ bytes: Uint8Array<ArrayBuffer>;
14
+ eof: boolean;
15
+ }>;
16
+ export declare function writeFileResource(id: number, buffer: Uint8Array, offset: number): Promise<number>;
17
+ export declare function syncFileResource(id: number): Promise<void>;
18
+ export declare function statFileResource(id: number): Promise<{
19
+ type: string;
20
+ nlink: bigint;
21
+ size: bigint;
22
+ atimeNs: bigint;
23
+ mtimeNs: bigint;
24
+ ctimeNs: bigint;
25
+ }>;
26
+ export declare function metadataHashFileResource(id: number): Promise<{
27
+ upper: bigint;
28
+ lower: bigint;
29
+ }>;
@@ -0,0 +1,116 @@
1
+ import fs from "node:fs";
2
+ import { promisify } from "node:util";
3
+ const closeAsync = promisify(fs.close);
4
+ const fdatasyncAsync = promisify(fs.fdatasync);
5
+ const fstatAsync = promisify(fs.fstat);
6
+ const fsyncAsync = promisify(fs.fsync);
7
+ const ftruncateAsync = promisify(fs.ftruncate);
8
+ const futimesAsync = promisify(fs.futimes);
9
+ const openAsync = promisify(fs.open);
10
+ const readAsync = promisify(fs.read);
11
+ const writeAsync = promisify(fs.write);
12
+ let nextFileResourceId = 0;
13
+ const fileResources = new Map();
14
+ function lookupType(obj) {
15
+ if (obj.isFile()) {
16
+ return "regular-file";
17
+ }
18
+ if (obj.isSocket()) {
19
+ return "socket";
20
+ }
21
+ if (obj.isSymbolicLink()) {
22
+ return "symbolic-link";
23
+ }
24
+ if (obj.isFIFO()) {
25
+ return "fifo";
26
+ }
27
+ if (obj.isDirectory()) {
28
+ return "directory";
29
+ }
30
+ if (obj.isCharacterDevice()) {
31
+ return "character-device";
32
+ }
33
+ if (obj.isBlockDevice()) {
34
+ return "block-device";
35
+ }
36
+ return "unknown";
37
+ }
38
+ function getFileResource(id) {
39
+ const resource = fileResources.get(id);
40
+ if (!resource) {
41
+ const error = new Error(`Invalid filesystem descriptor resource: ${id}`);
42
+ error.code = "EBADF";
43
+ throw error;
44
+ }
45
+ return resource;
46
+ }
47
+ export async function openFileResource(path, flags) {
48
+ const fd = await openAsync(path, flags);
49
+ let type;
50
+ try {
51
+ type = lookupType(await fstatAsync(fd, { bigint: true }));
52
+ }
53
+ catch (error) {
54
+ await closeAsync(fd);
55
+ throw error;
56
+ }
57
+ const id = ++nextFileResourceId;
58
+ fileResources.set(id, { fd, refs: 1 });
59
+ return { id, type };
60
+ }
61
+ export function retainFileResource(id) {
62
+ getFileResource(id).refs++;
63
+ return id;
64
+ }
65
+ export async function releaseFileResource(id) {
66
+ const resource = getFileResource(id);
67
+ if (--resource.refs === 0) {
68
+ fileResources.delete(id);
69
+ await closeAsync(resource.fd);
70
+ }
71
+ }
72
+ export function fileResourceFd(id) {
73
+ return getFileResource(id).fd;
74
+ }
75
+ export async function syncFileResourceData(id) {
76
+ await fdatasyncAsync(fileResourceFd(id));
77
+ }
78
+ export async function getFileResourceType(id) {
79
+ return lookupType(await fstatAsync(fileResourceFd(id), { bigint: true }));
80
+ }
81
+ export async function setFileResourceSize(id, size) {
82
+ await ftruncateAsync(fileResourceFd(id), size);
83
+ }
84
+ export async function setFileResourceTimes(id, atime, mtime) {
85
+ await futimesAsync(fileResourceFd(id), atime, mtime);
86
+ }
87
+ export async function readFileResource(id, length, offset) {
88
+ const buffer = new Uint8Array(length);
89
+ const result = await readAsync(fileResourceFd(id), buffer, 0, length, offset);
90
+ // Deno v1 returns the byte count directly, while Node returns an object
91
+ // containing the byte count and buffer through util.promisify's custom args.
92
+ const bytesRead = typeof result === "number" ? result : result.bytesRead;
93
+ return { bytes: buffer.subarray(0, bytesRead), eof: bytesRead === 0 };
94
+ }
95
+ export async function writeFileResource(id, buffer, offset) {
96
+ const result = await writeAsync(fileResourceFd(id), buffer, 0, buffer.byteLength, offset);
97
+ return typeof result === "number" ? result : result.bytesWritten;
98
+ }
99
+ export async function syncFileResource(id) {
100
+ await fsyncAsync(fileResourceFd(id));
101
+ }
102
+ export async function statFileResource(id) {
103
+ const stats = await fstatAsync(fileResourceFd(id), { bigint: true });
104
+ return {
105
+ type: lookupType(stats),
106
+ nlink: stats.nlink,
107
+ size: stats.size,
108
+ atimeNs: stats.atimeNs,
109
+ mtimeNs: stats.mtimeNs,
110
+ ctimeNs: stats.ctimeNs,
111
+ };
112
+ }
113
+ export async function metadataHashFileResource(id) {
114
+ const stats = await fstatAsync(fileResourceFd(id), { bigint: true });
115
+ return { upper: stats.mtimeNs, lower: stats.ino };
116
+ }
@@ -2,13 +2,13 @@ export declare function stopHttpServer(id: any): Promise<void>;
2
2
  export declare function clearOutgoingResponse(id: any): void;
3
3
  export declare function getHttpServerAddress(id: any): Promise<any>;
4
4
  export declare function setOutgoingResponse(id: any, { statusCode, headers, streamId }: {
5
- statusCode: any;
6
5
  headers: any;
6
+ statusCode: any;
7
7
  streamId: any;
8
8
  }): Promise<void>;
9
9
  export declare function startHttpServer(id: number, { port, host }: {
10
- port: any;
11
10
  host: any;
11
+ port: any;
12
12
  }): Promise<void>;
13
13
  export declare function createHttpRequest(method: any, scheme: any, authority: any, pathWithQuery: any, headers: any, bodyId: any, connectTimeout: any, betweenBytesTimeout: any, firstByteTimeout: any): Promise<{
14
14
  status: number | undefined;
@@ -1,4 +1,4 @@
1
- import { fileURLToPath } from "node:url";
1
+ import { Worker } from "node:worker_threads";
2
2
  import { createSyncFn } from "../synckit/index.js";
3
3
  import { CALL_MASK, CALL_TYPE_MASK, FILE, HTTP_SERVER_INCOMING_HANDLER, HTTP, INPUT_STREAM_BLOCKING_READ, INPUT_STREAM_BLOCKING_SKIP, INPUT_STREAM_DISPOSE, INPUT_STREAM_READ, INPUT_STREAM_SKIP, INPUT_STREAM_SUBSCRIBE, OUTPUT_STREAM_BLOCKING_FLUSH, OUTPUT_STREAM_BLOCKING_SPLICE, OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH, OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH, OUTPUT_STREAM_CHECK_WRITE, OUTPUT_STREAM_DISPOSE, OUTPUT_STREAM_FLUSH, OUTPUT_STREAM_SPLICE, OUTPUT_STREAM_SUBSCRIBE, OUTPUT_STREAM_WRITE_ZEROES, OUTPUT_STREAM_WRITE, POLL_POLL_LIST, POLL_POLLABLE_BLOCK, POLL_POLLABLE_DISPOSE, POLL_POLLABLE_READY, SOCKET_TCP, STDERR, STDIN, STDOUT, reverseMap, } from "./calls.js";
4
4
  import nodeProcess, { exit, stderr, stdout, env } from "node:process";
@@ -6,7 +6,6 @@ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
6
6
  const _rawDebug = "_rawDebug" in nodeProcess
7
7
  ? nodeProcess._rawDebug
8
8
  : console.error.bind(console);
9
- const workerPath = fileURLToPath(new URL("./worker-thread.js", import.meta.url));
10
9
  const httpIncomingHandlers = new Map();
11
10
  export function registerIncomingHttpHandler(id, handler) {
12
11
  httpIncomingHandlers.set(id, handler);
@@ -21,7 +20,7 @@ const DEBUG = env.PREVIEW2_SHIM_DEBUG === "0"
21
20
  /**
22
21
  * @type {(call: number, id: number | null, payload: any) -> any}
23
22
  */
24
- export let ioCall = createSyncFn(workerPath, DEBUG, (type, id, payload) => {
23
+ export let ioCall = createSyncFn((options) => new Worker(new URL("./worker-thread.bundle.js", import.meta.url), options), DEBUG, (type, id, payload) => {
25
24
  // 'callbacks' from the worker
26
25
  // ONLY happens for an http server incoming handler, and NOTHING else (not even sockets, since accept is sync!)
27
26
  if (type !== HTTP_SERVER_INCOMING_HANDLER) {
@@ -11,12 +11,12 @@ export declare function noLookup(ip: any, _opts: any, cb: any): void;
11
11
  export declare function socketResolveAddress(name: string): Promise<{
12
12
  tag: string;
13
13
  val: Ipv4Address | Ipv6Address;
14
- }[]> | Promise<{
14
+ }[][]> | Promise<{
15
15
  tag: string;
16
16
  val: Ipv4Address | Ipv6Address;
17
- }[][]>;
18
- export declare function convertSocketError(err: any): "unknown" | "would-block" | "connection-refused" | "invalid-argument" | "invalid-state" | "access-denied" | "not-supported" | "out-of-memory" | "concurrency-conflict" | "address-not-bindable" | "address-in-use" | "connection-reset" | "connection-aborted";
19
- export declare function convertSocketErrorCode(code: any): "unknown" | "would-block" | "connection-refused" | "invalid-argument" | "invalid-state" | "access-denied" | "not-supported" | "out-of-memory" | "concurrency-conflict" | "address-not-bindable" | "address-in-use" | "connection-reset" | "connection-aborted";
17
+ }[]>;
18
+ export declare function convertSocketError(err: any): "access-denied" | "address-in-use" | "address-not-bindable" | "concurrency-conflict" | "connection-aborted" | "connection-refused" | "connection-reset" | "invalid-argument" | "invalid-state" | "not-supported" | "out-of-memory" | "unknown" | "would-block";
19
+ export declare function convertSocketErrorCode(code: any): "access-denied" | "address-in-use" | "address-not-bindable" | "concurrency-conflict" | "connection-aborted" | "connection-refused" | "connection-reset" | "invalid-argument" | "invalid-state" | "not-supported" | "out-of-memory" | "unknown" | "would-block";
20
20
  export declare function tupleToIPv6(arr: Ipv6Address): string | null;
21
21
  export declare function tupleToIpv4(arr: Ipv4Address): string | null;
22
22
  export declare function isMulticastIpAddress(ipSocketAddress: IpSocketAddress): boolean;