@bytecodealliance/preview3-shim 0.1.2 → 0.2.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 (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 +332 -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 +109 -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 -98
  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
@@ -1,23 +1,23 @@
1
- // world wasi:cli/command@0.3.0-rc-2026-03-15
2
- export type * as WasiCliEnvironment030Rc20260315 from './interfaces/wasi-cli-environment.js'; // import wasi:cli/environment@0.3.0-rc-2026-03-15
3
- export type * as WasiCliExit030Rc20260315 from './interfaces/wasi-cli-exit.js'; // import wasi:cli/exit@0.3.0-rc-2026-03-15
4
- export type * as WasiCliStderr030Rc20260315 from './interfaces/wasi-cli-stderr.js'; // import wasi:cli/stderr@0.3.0-rc-2026-03-15
5
- export type * as WasiCliStdin030Rc20260315 from './interfaces/wasi-cli-stdin.js'; // import wasi:cli/stdin@0.3.0-rc-2026-03-15
6
- export type * as WasiCliStdout030Rc20260315 from './interfaces/wasi-cli-stdout.js'; // import wasi:cli/stdout@0.3.0-rc-2026-03-15
7
- export type * as WasiCliTerminalInput030Rc20260315 from './interfaces/wasi-cli-terminal-input.js'; // import wasi:cli/terminal-input@0.3.0-rc-2026-03-15
8
- export type * as WasiCliTerminalOutput030Rc20260315 from './interfaces/wasi-cli-terminal-output.js'; // import wasi:cli/terminal-output@0.3.0-rc-2026-03-15
9
- export type * as WasiCliTerminalStderr030Rc20260315 from './interfaces/wasi-cli-terminal-stderr.js'; // import wasi:cli/terminal-stderr@0.3.0-rc-2026-03-15
10
- export type * as WasiCliTerminalStdin030Rc20260315 from './interfaces/wasi-cli-terminal-stdin.js'; // import wasi:cli/terminal-stdin@0.3.0-rc-2026-03-15
11
- export type * as WasiCliTerminalStdout030Rc20260315 from './interfaces/wasi-cli-terminal-stdout.js'; // import wasi:cli/terminal-stdout@0.3.0-rc-2026-03-15
12
- export type * as WasiCliTypes030Rc20260315 from './interfaces/wasi-cli-types.js'; // import wasi:cli/types@0.3.0-rc-2026-03-15
13
- export type * as WasiClocksMonotonicClock030Rc20260315 from './interfaces/wasi-clocks-monotonic-clock.js'; // import wasi:clocks/monotonic-clock@0.3.0-rc-2026-03-15
14
- export type * as WasiClocksSystemClock030Rc20260315 from './interfaces/wasi-clocks-system-clock.js'; // import wasi:clocks/system-clock@0.3.0-rc-2026-03-15
15
- export type * as WasiClocksTypes030Rc20260315 from './interfaces/wasi-clocks-types.js'; // import wasi:clocks/types@0.3.0-rc-2026-03-15
16
- export type * as WasiFilesystemPreopens030Rc20260315 from './interfaces/wasi-filesystem-preopens.js'; // import wasi:filesystem/preopens@0.3.0-rc-2026-03-15
17
- export type * as WasiFilesystemTypes030Rc20260315 from './interfaces/wasi-filesystem-types.js'; // import wasi:filesystem/types@0.3.0-rc-2026-03-15
18
- export type * as WasiRandomInsecureSeed030Rc20260315 from './interfaces/wasi-random-insecure-seed.js'; // import wasi:random/insecure-seed@0.3.0-rc-2026-03-15
19
- export type * as WasiRandomInsecure030Rc20260315 from './interfaces/wasi-random-insecure.js'; // import wasi:random/insecure@0.3.0-rc-2026-03-15
20
- export type * as WasiRandomRandom030Rc20260315 from './interfaces/wasi-random-random.js'; // import wasi:random/random@0.3.0-rc-2026-03-15
21
- export type * as WasiSocketsIpNameLookup030Rc20260315 from './interfaces/wasi-sockets-ip-name-lookup.js'; // import wasi:sockets/ip-name-lookup@0.3.0-rc-2026-03-15
22
- export type * as WasiSocketsTypes030Rc20260315 from './interfaces/wasi-sockets-types.js'; // import wasi:sockets/types@0.3.0-rc-2026-03-15
23
- export * as run from './interfaces/wasi-cli-run.js'; // export wasi:cli/run@0.3.0-rc-2026-03-15
1
+ // world wasi:cli/command@0.3.0
2
+ export type * as WasiCliEnvironment030 from './interfaces/wasi-cli-environment.js'; // import wasi:cli/environment@0.3.0
3
+ export type * as WasiCliExit030 from './interfaces/wasi-cli-exit.js'; // import wasi:cli/exit@0.3.0
4
+ export type * as WasiCliStderr030 from './interfaces/wasi-cli-stderr.js'; // import wasi:cli/stderr@0.3.0
5
+ export type * as WasiCliStdin030 from './interfaces/wasi-cli-stdin.js'; // import wasi:cli/stdin@0.3.0
6
+ export type * as WasiCliStdout030 from './interfaces/wasi-cli-stdout.js'; // import wasi:cli/stdout@0.3.0
7
+ export type * as WasiCliTerminalInput030 from './interfaces/wasi-cli-terminal-input.js'; // import wasi:cli/terminal-input@0.3.0
8
+ export type * as WasiCliTerminalOutput030 from './interfaces/wasi-cli-terminal-output.js'; // import wasi:cli/terminal-output@0.3.0
9
+ export type * as WasiCliTerminalStderr030 from './interfaces/wasi-cli-terminal-stderr.js'; // import wasi:cli/terminal-stderr@0.3.0
10
+ export type * as WasiCliTerminalStdin030 from './interfaces/wasi-cli-terminal-stdin.js'; // import wasi:cli/terminal-stdin@0.3.0
11
+ export type * as WasiCliTerminalStdout030 from './interfaces/wasi-cli-terminal-stdout.js'; // import wasi:cli/terminal-stdout@0.3.0
12
+ export type * as WasiCliTypes030 from './interfaces/wasi-cli-types.js'; // import wasi:cli/types@0.3.0
13
+ export type * as WasiClocksMonotonicClock030 from './interfaces/wasi-clocks-monotonic-clock.js'; // import wasi:clocks/monotonic-clock@0.3.0
14
+ export type * as WasiClocksSystemClock030 from './interfaces/wasi-clocks-system-clock.js'; // import wasi:clocks/system-clock@0.3.0
15
+ export type * as WasiClocksTypes030 from './interfaces/wasi-clocks-types.js'; // import wasi:clocks/types@0.3.0
16
+ export type * as WasiFilesystemPreopens030 from './interfaces/wasi-filesystem-preopens.js'; // import wasi:filesystem/preopens@0.3.0
17
+ export type * as WasiFilesystemTypes030 from './interfaces/wasi-filesystem-types.js'; // import wasi:filesystem/types@0.3.0
18
+ export type * as WasiRandomInsecureSeed030 from './interfaces/wasi-random-insecure-seed.js'; // import wasi:random/insecure-seed@0.3.0
19
+ export type * as WasiRandomInsecure030 from './interfaces/wasi-random-insecure.js'; // import wasi:random/insecure@0.3.0
20
+ export type * as WasiRandomRandom030 from './interfaces/wasi-random-random.js'; // import wasi:random/random@0.3.0
21
+ export type * as WasiSocketsIpNameLookup030 from './interfaces/wasi-sockets-ip-name-lookup.js'; // import wasi:sockets/ip-name-lookup@0.3.0
22
+ export type * as WasiSocketsTypes030 from './interfaces/wasi-sockets-types.js'; // import wasi:sockets/types@0.3.0
23
+ export * as run from './interfaces/wasi-cli-run.js'; // export wasi:cli/run@0.3.0
@@ -1,14 +1,14 @@
1
- // world wasi:http/service@0.3.0-rc-2026-03-15
2
- export type * as WasiCliStderr030Rc20260315 from './interfaces/wasi-cli-stderr.js'; // import wasi:cli/stderr@0.3.0-rc-2026-03-15
3
- export type * as WasiCliStdin030Rc20260315 from './interfaces/wasi-cli-stdin.js'; // import wasi:cli/stdin@0.3.0-rc-2026-03-15
4
- export type * as WasiCliStdout030Rc20260315 from './interfaces/wasi-cli-stdout.js'; // import wasi:cli/stdout@0.3.0-rc-2026-03-15
5
- export type * as WasiCliTypes030Rc20260315 from './interfaces/wasi-cli-types.js'; // import wasi:cli/types@0.3.0-rc-2026-03-15
6
- export type * as WasiClocksMonotonicClock030Rc20260315 from './interfaces/wasi-clocks-monotonic-clock.js'; // import wasi:clocks/monotonic-clock@0.3.0-rc-2026-03-15
7
- export type * as WasiClocksSystemClock030Rc20260315 from './interfaces/wasi-clocks-system-clock.js'; // import wasi:clocks/system-clock@0.3.0-rc-2026-03-15
8
- export type * as WasiClocksTypes030Rc20260315 from './interfaces/wasi-clocks-types.js'; // import wasi:clocks/types@0.3.0-rc-2026-03-15
9
- export type * as WasiHttpClient030Rc20260315 from './interfaces/wasi-http-client.js'; // import wasi:http/client@0.3.0-rc-2026-03-15
10
- export type * as WasiHttpTypes030Rc20260315 from './interfaces/wasi-http-types.js'; // import wasi:http/types@0.3.0-rc-2026-03-15
11
- export type * as WasiRandomInsecureSeed030Rc20260315 from './interfaces/wasi-random-insecure-seed.js'; // import wasi:random/insecure-seed@0.3.0-rc-2026-03-15
12
- export type * as WasiRandomInsecure030Rc20260315 from './interfaces/wasi-random-insecure.js'; // import wasi:random/insecure@0.3.0-rc-2026-03-15
13
- export type * as WasiRandomRandom030Rc20260315 from './interfaces/wasi-random-random.js'; // import wasi:random/random@0.3.0-rc-2026-03-15
14
- export * as handler from './interfaces/wasi-http-handler.js'; // export wasi:http/handler@0.3.0-rc-2026-03-15
1
+ // world wasi:http/service@0.3.0
2
+ export type * as WasiCliStderr030 from './interfaces/wasi-cli-stderr.js'; // import wasi:cli/stderr@0.3.0
3
+ export type * as WasiCliStdin030 from './interfaces/wasi-cli-stdin.js'; // import wasi:cli/stdin@0.3.0
4
+ export type * as WasiCliStdout030 from './interfaces/wasi-cli-stdout.js'; // import wasi:cli/stdout@0.3.0
5
+ export type * as WasiCliTypes030 from './interfaces/wasi-cli-types.js'; // import wasi:cli/types@0.3.0
6
+ export type * as WasiClocksMonotonicClock030 from './interfaces/wasi-clocks-monotonic-clock.js'; // import wasi:clocks/monotonic-clock@0.3.0
7
+ export type * as WasiClocksSystemClock030 from './interfaces/wasi-clocks-system-clock.js'; // import wasi:clocks/system-clock@0.3.0
8
+ export type * as WasiClocksTypes030 from './interfaces/wasi-clocks-types.js'; // import wasi:clocks/types@0.3.0
9
+ export type * as WasiHttpClient030 from './interfaces/wasi-http-client.js'; // import wasi:http/client@0.3.0
10
+ export type * as WasiHttpTypes030 from './interfaces/wasi-http-types.js'; // import wasi:http/types@0.3.0
11
+ export type * as WasiRandomInsecureSeed030 from './interfaces/wasi-random-insecure-seed.js'; // import wasi:random/insecure-seed@0.3.0
12
+ export type * as WasiRandomInsecure030 from './interfaces/wasi-random-insecure.js'; // import wasi:random/insecure@0.3.0
13
+ export type * as WasiRandomRandom030 from './interfaces/wasi-random-random.js'; // import wasi:random/random@0.3.0
14
+ export * as handler from './interfaces/wasi-http-handler.js'; // export wasi:http/handler@0.3.0
package/lib/nodejs/cli.js DELETED
@@ -1,127 +0,0 @@
1
- import process from "node:process";
2
- import { Readable } from "node:stream";
3
-
4
- import { ResourceWorker } from "./workers/resource-worker.js";
5
- import { StreamReader, readableByteStreamFromReader } from "./stream.js";
6
- import { future } from "./future.js";
7
-
8
- import { environment as environmentV2 } from "@bytecodealliance/preview2-shim/cli";
9
-
10
- export {
11
- _appendEnv,
12
- _setEnv,
13
- _setArgs,
14
- _setCwd,
15
- _setTerminalStdin,
16
- _setTerminalStdout,
17
- _setTerminalStderr,
18
- exit,
19
- terminalInput,
20
- terminalOutput,
21
- terminalStdin,
22
- terminalStdout,
23
- terminalStderr,
24
- } from "@bytecodealliance/preview2-shim/cli";
25
-
26
- // `wasi:cli/environment` renamed `initial-cwd` to `get-initial-cwd` between
27
- // p2 and p3. Adapt the p2-shim shape to the p3 WIT member name while
28
- // re-exporting the unchanged members.
29
- export const environment = {
30
- getEnvironment: environmentV2.getEnvironment,
31
- getArguments: environmentV2.getArguments,
32
- getInitialCwd: environmentV2.initialCwd,
33
- };
34
-
35
- let WORKER = null;
36
- function worker() {
37
- return (WORKER ??= new ResourceWorker(new URL("./workers/cli-worker.js", import.meta.url)));
38
- }
39
-
40
- /**
41
- * Map a Node.js error to a WIT error-code enum value.
42
- * WIT: enum error-code { io, illegal-byte-sequence, pipe }
43
- * @param {Error} err
44
- * @returns {string}
45
- */
46
- function errorCode(err) {
47
- if (err?.code === "EPIPE" || err?.code === "ERR_STREAM_PREMATURE_CLOSE") {
48
- return "pipe";
49
- }
50
- if (err?.code === "ERR_ENCODING_INVALID_ENCODED_DATA") {
51
- return "illegal-byte-sequence";
52
- }
53
- return "io";
54
- }
55
-
56
- export const stdin = {
57
- /**
58
- * Return a stream for reading from stdin, and a future to signal read results.
59
- *
60
- * WIT:
61
- * ```
62
- * read-via-stream: func() -> tuple<stream<u8>, future<result<_, error-code>>>;
63
- * ```
64
- *
65
- * @returns {[StreamReader, FutureReader]} A tuple of [stream, future].
66
- */
67
- readViaStream() {
68
- const readable = Readable.toWeb(process.stdin);
69
- const { tx: futureTx, rx: futureRx } = future();
70
-
71
- async function* stdinChunks() {
72
- try {
73
- for await (const chunk of readable.values({ preventCancel: true })) {
74
- yield chunk;
75
- }
76
- await futureTx.write({ tag: "ok", val: undefined });
77
- } catch (err) {
78
- await futureTx.write({ tag: "err", val: errorCode(err) });
79
- throw err;
80
- }
81
- }
82
-
83
- return [new StreamReader(stdinChunks()), futureRx];
84
- },
85
- };
86
-
87
- export const stdout = {
88
- /**
89
- * Write the given stream to stdout.
90
- *
91
- * WIT:
92
- * ```
93
- * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
94
- * ```
95
- * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
96
- */
97
- async writeViaStream(streamReader) {
98
- const readableStream = readableByteStreamFromReader(streamReader, { name: "stdout stream" });
99
- try {
100
- await worker().run({ op: "stdout", stream: readableStream }, [readableStream]);
101
- return { tag: "ok", val: undefined };
102
- } catch (err) {
103
- return { tag: "err", val: errorCode(err) };
104
- }
105
- },
106
- };
107
-
108
- export const stderr = {
109
- /**
110
- * Write the given stream to stderr.
111
- *
112
- * WIT:
113
- * ```
114
- * write-via-stream: func(data: stream<u8>) -> future<result<_, error-code>>;
115
- * ```
116
- * @returns {Promise<{tag: string, val?: string}>} Result of the write operation.
117
- */
118
- async writeViaStream(streamReader) {
119
- const readableStream = readableByteStreamFromReader(streamReader, { name: "stderr stream" });
120
- try {
121
- await worker().run({ op: "stderr", stream: readableStream }, [readableStream]);
122
- return { tag: "ok", val: undefined };
123
- } catch (err) {
124
- return { tag: "err", val: errorCode(err) };
125
- }
126
- },
127
- };
@@ -1,98 +0,0 @@
1
- import {
2
- wallClock,
3
- monotonicClock as monotonicClockV2,
4
- } from "@bytecodealliance/preview2-shim/clocks";
5
-
6
- // Re-export wall-clock as system-clock per latest WASI 0.3.0-draft.
7
- export const systemClock = {
8
- now: wallClock.now,
9
-
10
- /**
11
- * Query the resolution of the clock.
12
- *
13
- * WIT:
14
- * ```
15
- * get-resolution: func() -> duration;
16
- * ```
17
- *
18
- * @returns {bigint} The smallest distinguishable duration in nanoseconds.
19
- */
20
- getResolution() {
21
- const dt = wallClock.resolution();
22
- return BigInt(dt.seconds) * 1_000_000_000n + BigInt(dt.nanoseconds);
23
- },
24
- };
25
-
26
- // eslint-disable-next-line no-unused-vars
27
- const { subscribeInstant, subscribeDuration, ...baseMonotonicClock } = monotonicClockV2;
28
-
29
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
30
- const nsToTimeoutMs = (ns) => (ns + 999_999n) / 1_000_000n;
31
-
32
- export const monotonicClock = {
33
- ...baseMonotonicClock,
34
-
35
- /**
36
- * Query the resolution of the clock.
37
- *
38
- * WIT:
39
- * ```
40
- * get-resolution: func() -> duration;
41
- * ```
42
- *
43
- * @returns {bigint} Duration of a clock tick in nanoseconds.
44
- */
45
- getResolution() {
46
- return baseMonotonicClock.resolution();
47
- },
48
-
49
- /**
50
- * Waits until a specific mark (in nanoseconds) is reached
51
- *
52
- * WIT:
53
- * ```
54
- * wait-until: async func(when: mark);
55
- * ```
56
- * @async
57
- * @param {bigint} targetNs - The target mark in nanoseconds to wait until
58
- * @returns {Promise<void>} A promise that resolves when the target mark is reached
59
- * @throws {TypeError} If targetNs is not a bigint
60
- */
61
- async waitUntil(targetNs) {
62
- while (true) {
63
- const nowNs = monotonicClock.now();
64
- const diffNs = targetNs - nowNs;
65
-
66
- if (diffNs <= 0n) {
67
- return;
68
- }
69
-
70
- const ms = nsToTimeoutMs(diffNs);
71
- if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
72
- throw new TypeError(`Cannot wait for ${targetNs} ns, exceeds maximum safe integer`);
73
- }
74
-
75
- await sleep(Number(ms));
76
- }
77
- },
78
-
79
- /**
80
- * Waits for a specified duration in nanoseconds
81
- *
82
- * WIT:
83
- * ```
84
- * wait-for: async func(how-long: duration);
85
- * ```
86
- * @async
87
- * @param {bigint} durationNs - The duration to wait in nanoseconds
88
- * @returns {Promise<void>} A promise that resolves after the specified duration
89
- * @throws {TypeError} If durationNs is not a bigint
90
- */
91
- async waitFor(durationNs) {
92
- if (durationNs <= 0n) {
93
- return;
94
- }
95
-
96
- await monotonicClock.waitUntil(monotonicClock.now() + durationNs);
97
- },
98
- };