@bytecodealliance/preview3-shim 0.1.1 → 0.2.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.
Files changed (112) hide show
  1. package/dist/nodejs/cli.d.ts +51 -0
  2. package/dist/nodejs/cli.js +106 -0
  3. package/dist/nodejs/clocks.d.ts +55 -0
  4. package/dist/nodejs/clocks.js +83 -0
  5. package/dist/nodejs/filesystem/descriptor.d.ts +443 -0
  6. package/dist/nodejs/filesystem/descriptor.js +1010 -0
  7. package/dist/nodejs/filesystem/error.d.ts +61 -0
  8. package/dist/nodejs/filesystem/error.js +86 -0
  9. package/dist/nodejs/filesystem/utils.d.ts +8 -0
  10. package/dist/nodejs/filesystem/utils.js +25 -0
  11. package/dist/nodejs/filesystem.js +2 -0
  12. package/dist/nodejs/finalization.d.ts +2 -0
  13. package/{lib → dist}/nodejs/finalization.js +15 -21
  14. package/dist/nodejs/future.d.ts +98 -0
  15. package/dist/nodejs/future.js +176 -0
  16. package/dist/nodejs/http/client.d.ts +22 -0
  17. package/dist/nodejs/http/client.js +138 -0
  18. package/dist/nodejs/http/error.d.ts +18 -0
  19. package/dist/nodejs/http/error.js +62 -0
  20. package/dist/nodejs/http/fields.d.ts +144 -0
  21. package/dist/nodejs/http/fields.js +327 -0
  22. package/dist/nodejs/http/request.d.ts +256 -0
  23. package/dist/nodejs/http/request.js +606 -0
  24. package/dist/nodejs/http/response.d.ts +82 -0
  25. package/dist/nodejs/http/response.js +202 -0
  26. package/dist/nodejs/http/server.d.ts +13 -0
  27. package/dist/nodejs/http/server.js +127 -0
  28. package/dist/nodejs/http.d.ts +19 -0
  29. package/{lib → dist}/nodejs/http.js +13 -18
  30. package/dist/nodejs/index.js +8 -0
  31. package/dist/nodejs/random.d.ts +5 -0
  32. package/{lib → dist}/nodejs/random.js +2 -8
  33. package/dist/nodejs/sockets/address.d.ts +78 -0
  34. package/dist/nodejs/sockets/address.js +120 -0
  35. package/dist/nodejs/sockets/error.d.ts +83 -0
  36. package/dist/nodejs/sockets/error.js +120 -0
  37. package/dist/nodejs/sockets/ip-name-lookup.d.ts +18 -0
  38. package/dist/nodejs/sockets/ip-name-lookup.js +84 -0
  39. package/dist/nodejs/sockets/tcp.d.ts +333 -0
  40. package/dist/nodejs/sockets/tcp.js +723 -0
  41. package/dist/nodejs/sockets/udp.d.ts +224 -0
  42. package/dist/nodejs/sockets/udp.js +475 -0
  43. package/dist/nodejs/sockets.d.ts +11 -0
  44. package/{lib → dist}/nodejs/sockets.js +2 -5
  45. package/dist/nodejs/stream.d.ts +161 -0
  46. package/dist/nodejs/stream.js +388 -0
  47. package/dist/nodejs/workers/cli-worker.d.ts +1 -0
  48. package/dist/nodejs/workers/cli-worker.js +41 -0
  49. package/dist/nodejs/workers/filesystem-worker.d.ts +1 -0
  50. package/dist/nodejs/workers/filesystem-worker.js +102 -0
  51. package/dist/nodejs/workers/http-worker.d.ts +1 -0
  52. package/dist/nodejs/workers/http-worker.js +355 -0
  53. package/dist/nodejs/workers/resource-worker.d.ts +13 -0
  54. package/dist/nodejs/workers/resource-worker.js +152 -0
  55. package/dist/nodejs/workers/tcp-worker.d.ts +2 -0
  56. package/dist/nodejs/workers/tcp-worker.js +318 -0
  57. package/dist/nodejs/workers/udp-worker.d.ts +1 -0
  58. package/dist/nodejs/workers/udp-worker.js +156 -0
  59. package/package.json +20 -12
  60. package/types/interfaces/wasi-cli-environment.d.ts +3 -3
  61. package/types/interfaces/wasi-cli-exit.d.ts +12 -1
  62. package/types/interfaces/wasi-cli-run.d.ts +1 -1
  63. package/types/interfaces/wasi-cli-stderr.d.ts +3 -3
  64. package/types/interfaces/wasi-cli-stdin.d.ts +5 -5
  65. package/types/interfaces/wasi-cli-stdout.d.ts +3 -3
  66. package/types/interfaces/wasi-cli-terminal-input.d.ts +1 -1
  67. package/types/interfaces/wasi-cli-terminal-output.d.ts +1 -1
  68. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
  69. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
  70. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
  71. package/types/interfaces/wasi-cli-types.d.ts +5 -5
  72. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +3 -3
  73. package/types/interfaces/wasi-clocks-system-clock.d.ts +6 -6
  74. package/types/interfaces/wasi-clocks-types.d.ts +1 -1
  75. package/types/interfaces/wasi-filesystem-preopens.d.ts +1 -1
  76. package/types/interfaces/wasi-filesystem-types.d.ts +74 -74
  77. package/types/interfaces/wasi-http-client.d.ts +1 -1
  78. package/types/interfaces/wasi-http-handler.d.ts +1 -1
  79. package/types/interfaces/wasi-http-types.d.ts +34 -34
  80. package/types/interfaces/wasi-random-insecure-seed.d.ts +5 -5
  81. package/types/interfaces/wasi-random-insecure.d.ts +5 -5
  82. package/types/interfaces/wasi-random-random.d.ts +5 -5
  83. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +11 -11
  84. package/types/interfaces/wasi-sockets-types.d.ts +121 -121
  85. package/types/wasi-cli-command.d.ts +23 -23
  86. package/types/wasi-http-service.d.ts +14 -14
  87. package/lib/nodejs/cli.js +0 -127
  88. package/lib/nodejs/clocks.js +0 -100
  89. package/lib/nodejs/filesystem/descriptor.js +0 -1093
  90. package/lib/nodejs/filesystem/error.js +0 -92
  91. package/lib/nodejs/filesystem/utils.js +0 -19
  92. package/lib/nodejs/future.js +0 -190
  93. package/lib/nodejs/http/client.js +0 -161
  94. package/lib/nodejs/http/error.js +0 -71
  95. package/lib/nodejs/http/fields.js +0 -373
  96. package/lib/nodejs/http/request.js +0 -691
  97. package/lib/nodejs/http/response.js +0 -227
  98. package/lib/nodejs/http/server.js +0 -157
  99. package/lib/nodejs/sockets/address.js +0 -146
  100. package/lib/nodejs/sockets/error.js +0 -123
  101. package/lib/nodejs/sockets/ip-name-lookup.js +0 -93
  102. package/lib/nodejs/sockets/tcp.js +0 -801
  103. package/lib/nodejs/sockets/udp.js +0 -516
  104. package/lib/nodejs/stream.js +0 -437
  105. package/lib/nodejs/workers/cli-worker.js +0 -49
  106. package/lib/nodejs/workers/filesystem-worker.js +0 -119
  107. package/lib/nodejs/workers/http-worker.js +0 -417
  108. package/lib/nodejs/workers/resource-worker.js +0 -195
  109. package/lib/nodejs/workers/tcp-worker.js +0 -370
  110. package/lib/nodejs/workers/udp-worker.js +0 -194
  111. /package/{lib/nodejs/filesystem.js → dist/nodejs/filesystem.d.ts} +0 -0
  112. /package/{lib/nodejs/index.js → dist/nodejs/index.d.ts} +0 -0
@@ -0,0 +1,51 @@
1
+ import { StreamReader } from "./stream.js";
2
+ import * as cliV2 from "@bytecodealliance/preview2-shim/cli";
3
+ export declare const _appendEnv: (env: Record<string, string>) => void, _setEnv: typeof cliV2._setEnv, _setArgs: typeof cliV2._setArgs, _setCwd: typeof cliV2._setCwd, _setTerminalStdin: (terminalStdin: unknown) => void, _setTerminalStdout: (terminalStdout: unknown) => void, _setTerminalStderr: (terminalStderr: unknown) => void, exit: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-exit"), terminalInput: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-terminal-input"), terminalOutput: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-terminal-output"), terminalStdin: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-terminal-stdin"), terminalStdout: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-terminal-stdout"), terminalStderr: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-terminal-stderr");
4
+ export declare const environment: {
5
+ getEnvironment: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-environment").getEnvironment;
6
+ getArguments: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-environment").getArguments;
7
+ getInitialCwd: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-cli-environment").initialCwd;
8
+ };
9
+ export declare const stdin: {
10
+ /**
11
+ * Return a stream for reading from stdin, and a future to signal read results.
12
+ *
13
+ * WIT:
14
+ * ```
15
+ * read-via-stream: func() -> tuple<stream<u8>, future<result<_, error-code>>>;
16
+ * ```
17
+ *
18
+ * @returns {[StreamReader, FutureReader]} A tuple of [stream, future].
19
+ */
20
+ readViaStream(): (StreamReader | import("./future.js").FutureReader)[];
21
+ };
22
+ export declare const stdout: {
23
+ /**
24
+ * Write the given stream to stdout.
25
+ *
26
+ * WIT:
27
+ * ```
28
+ * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
29
+ * ```
30
+ * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
31
+ */
32
+ writeViaStream(streamReader: any): Promise<{
33
+ tag: string;
34
+ val: string;
35
+ }>;
36
+ };
37
+ export declare const stderr: {
38
+ /**
39
+ * Write the given stream to stderr.
40
+ *
41
+ * WIT:
42
+ * ```
43
+ * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
44
+ * ```
45
+ * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
46
+ */
47
+ writeViaStream(streamReader: any): Promise<{
48
+ tag: string;
49
+ val: string;
50
+ }>;
51
+ };
@@ -0,0 +1,106 @@
1
+ import process from "node:process";
2
+ import { Readable } from "node:stream";
3
+ import { ResourceWorker } from "./workers/resource-worker.js";
4
+ import { StreamReader, readableByteStreamFromReader } from "./stream.js";
5
+ import { future } from "./future.js";
6
+ import * as cliV2 from "@bytecodealliance/preview2-shim/cli";
7
+ const nodeCliV2 = cliV2;
8
+ export const { _appendEnv, _setEnv, _setArgs, _setCwd, _setTerminalStdin, _setTerminalStdout, _setTerminalStderr, exit, terminalInput, terminalOutput, terminalStdin, terminalStdout, terminalStderr, } = nodeCliV2;
9
+ // `wasi:cli/environment` renamed `initial-cwd` to `get-initial-cwd` between
10
+ // p2 and p3. Adapt the p2-shim shape to the p3 WIT member name while
11
+ // re-exporting the unchanged members.
12
+ export const environment = {
13
+ getEnvironment: cliV2.environment.getEnvironment,
14
+ getArguments: cliV2.environment.getArguments,
15
+ getInitialCwd: cliV2.environment.initialCwd,
16
+ };
17
+ let WORKER = null;
18
+ function worker() {
19
+ return (WORKER ??= new ResourceWorker(new URL("./workers/cli-worker.js", import.meta.url)));
20
+ }
21
+ /**
22
+ * Map a Node.js error to a WIT error-code enum value.
23
+ * WIT: enum error-code { io, illegal-byte-sequence, pipe }
24
+ * @param {Error} err
25
+ * @returns {string}
26
+ */
27
+ function errorCode(err) {
28
+ if (err?.code === "EPIPE" || err?.code === "ERR_STREAM_PREMATURE_CLOSE") {
29
+ return "pipe";
30
+ }
31
+ if (err?.code === "ERR_ENCODING_INVALID_ENCODED_DATA") {
32
+ return "illegal-byte-sequence";
33
+ }
34
+ return "io";
35
+ }
36
+ export const stdin = {
37
+ /**
38
+ * Return a stream for reading from stdin, and a future to signal read results.
39
+ *
40
+ * WIT:
41
+ * ```
42
+ * read-via-stream: func() -> tuple<stream<u8>, future<result<_, error-code>>>;
43
+ * ```
44
+ *
45
+ * @returns {[StreamReader, FutureReader]} A tuple of [stream, future].
46
+ */
47
+ readViaStream() {
48
+ const readable = Readable.toWeb(process.stdin);
49
+ const { tx: futureTx, rx: futureRx } = future();
50
+ async function* stdinChunks() {
51
+ try {
52
+ for await (const chunk of readable.values({ preventCancel: true })) {
53
+ yield chunk;
54
+ }
55
+ await futureTx.write({ tag: "ok", val: undefined });
56
+ }
57
+ catch (err) {
58
+ await futureTx.write({ tag: "err", val: errorCode(err) });
59
+ throw err;
60
+ }
61
+ }
62
+ return [new StreamReader(stdinChunks()), futureRx];
63
+ },
64
+ };
65
+ export const stdout = {
66
+ /**
67
+ * Write the given stream to stdout.
68
+ *
69
+ * WIT:
70
+ * ```
71
+ * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
72
+ * ```
73
+ * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
74
+ */
75
+ async writeViaStream(streamReader) {
76
+ const readableStream = readableByteStreamFromReader(streamReader, { name: "stdout stream" });
77
+ try {
78
+ await worker().run({ op: "stdout", stream: readableStream }, [readableStream]);
79
+ return { tag: "ok", val: undefined };
80
+ }
81
+ catch (err) {
82
+ return { tag: "err", val: errorCode(err) };
83
+ }
84
+ },
85
+ };
86
+ export const stderr = {
87
+ /**
88
+ * Write the given stream to stderr.
89
+ *
90
+ * WIT:
91
+ * ```
92
+ * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
93
+ * ```
94
+ * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
95
+ */
96
+ async writeViaStream(streamReader) {
97
+ const readableStream = readableByteStreamFromReader(streamReader, { name: "stderr stream" });
98
+ try {
99
+ await worker().run({ op: "stderr", stream: readableStream }, [readableStream]);
100
+ return { tag: "ok", val: undefined };
101
+ }
102
+ catch (err) {
103
+ return { tag: "err", val: errorCode(err) };
104
+ }
105
+ },
106
+ };
@@ -0,0 +1,55 @@
1
+ export declare const systemClock: {
2
+ now: typeof import("@bytecodealliance/preview2-shim/interfaces/wasi-clocks-wall-clock").now;
3
+ /**
4
+ * Query the resolution of the clock.
5
+ *
6
+ * WIT:
7
+ * ```
8
+ * get-resolution: func() -> duration;
9
+ * ```
10
+ *
11
+ * @returns {bigint} The smallest distinguishable duration in nanoseconds.
12
+ */
13
+ getResolution(): bigint;
14
+ };
15
+ export declare const monotonicClock: {
16
+ /**
17
+ * Query the resolution of the clock.
18
+ *
19
+ * WIT:
20
+ * ```
21
+ * get-resolution: func() -> duration;
22
+ * ```
23
+ *
24
+ * @returns {bigint} Duration of a clock tick in nanoseconds.
25
+ */
26
+ getResolution(): bigint;
27
+ /**
28
+ * Waits until a specific mark (in nanoseconds) is reached
29
+ *
30
+ * WIT:
31
+ * ```
32
+ * wait-until: async func(when: mark);
33
+ * ```
34
+ * @async
35
+ * @param {bigint} targetNs - The target mark in nanoseconds to wait until
36
+ * @returns {Promise<void>} A promise that resolves when the target mark is reached
37
+ * @throws {TypeError} If targetNs is not a bigint
38
+ */
39
+ waitUntil(targetNs: any): Promise<void>;
40
+ /**
41
+ * Waits for a specified duration in nanoseconds
42
+ *
43
+ * WIT:
44
+ * ```
45
+ * wait-for: async func(how-long: duration);
46
+ * ```
47
+ * @async
48
+ * @param {bigint} durationNs - The duration to wait in nanoseconds
49
+ * @returns {Promise<void>} A promise that resolves after the specified duration
50
+ * @throws {TypeError} If durationNs is not a bigint
51
+ */
52
+ waitFor(durationNs: any): Promise<void>;
53
+ now(): import("@bytecodealliance/preview2-shim/interfaces/wasi-clocks-monotonic-clock").Instant;
54
+ resolution(): import("@bytecodealliance/preview2-shim/interfaces/wasi-clocks-monotonic-clock").Duration;
55
+ };
@@ -0,0 +1,83 @@
1
+ import { wallClock, monotonicClock as monotonicClockV2, } from "@bytecodealliance/preview2-shim/clocks";
2
+ // Re-export wall-clock as system-clock per latest WASI 0.3.0-draft.
3
+ export const systemClock = {
4
+ now: wallClock.now,
5
+ /**
6
+ * Query the resolution of the clock.
7
+ *
8
+ * WIT:
9
+ * ```
10
+ * get-resolution: func() -> duration;
11
+ * ```
12
+ *
13
+ * @returns {bigint} The smallest distinguishable duration in nanoseconds.
14
+ */
15
+ getResolution() {
16
+ const dt = wallClock.resolution();
17
+ return BigInt(dt.seconds) * 1000000000n + BigInt(dt.nanoseconds);
18
+ },
19
+ };
20
+ // eslint-disable-next-line no-unused-vars
21
+ const { subscribeInstant, subscribeDuration, ...baseMonotonicClock } = monotonicClockV2;
22
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
23
+ const nsToTimeoutMs = (ns) => (ns + 999999n) / 1000000n;
24
+ export const monotonicClock = {
25
+ ...baseMonotonicClock,
26
+ /**
27
+ * Query the resolution of the clock.
28
+ *
29
+ * WIT:
30
+ * ```
31
+ * get-resolution: func() -> duration;
32
+ * ```
33
+ *
34
+ * @returns {bigint} Duration of a clock tick in nanoseconds.
35
+ */
36
+ getResolution() {
37
+ return baseMonotonicClock.resolution();
38
+ },
39
+ /**
40
+ * Waits until a specific mark (in nanoseconds) is reached
41
+ *
42
+ * WIT:
43
+ * ```
44
+ * wait-until: async func(when: mark);
45
+ * ```
46
+ * @async
47
+ * @param {bigint} targetNs - The target mark in nanoseconds to wait until
48
+ * @returns {Promise<void>} A promise that resolves when the target mark is reached
49
+ * @throws {TypeError} If targetNs is not a bigint
50
+ */
51
+ async waitUntil(targetNs) {
52
+ while (true) {
53
+ const nowNs = monotonicClock.now();
54
+ const diffNs = targetNs - nowNs;
55
+ if (diffNs <= 0n) {
56
+ return;
57
+ }
58
+ const ms = nsToTimeoutMs(diffNs);
59
+ if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
60
+ throw new TypeError(`Cannot wait for ${targetNs} ns, exceeds maximum safe integer`);
61
+ }
62
+ await sleep(Number(ms));
63
+ }
64
+ },
65
+ /**
66
+ * Waits for a specified duration in nanoseconds
67
+ *
68
+ * WIT:
69
+ * ```
70
+ * wait-for: async func(how-long: duration);
71
+ * ```
72
+ * @async
73
+ * @param {bigint} durationNs - The duration to wait in nanoseconds
74
+ * @returns {Promise<void>} A promise that resolves after the specified duration
75
+ * @throws {TypeError} If durationNs is not a bigint
76
+ */
77
+ async waitFor(durationNs) {
78
+ if (durationNs <= 0n) {
79
+ return;
80
+ }
81
+ await monotonicClock.waitUntil(monotonicClock.now() + durationNs);
82
+ },
83
+ };