@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.
- package/dist/nodejs/cli.d.ts +51 -0
- package/dist/nodejs/cli.js +106 -0
- package/dist/nodejs/clocks.d.ts +55 -0
- package/dist/nodejs/clocks.js +83 -0
- package/dist/nodejs/filesystem/descriptor.d.ts +443 -0
- package/dist/nodejs/filesystem/descriptor.js +1010 -0
- package/dist/nodejs/filesystem/error.d.ts +61 -0
- package/dist/nodejs/filesystem/error.js +86 -0
- package/dist/nodejs/filesystem/utils.d.ts +8 -0
- package/dist/nodejs/filesystem/utils.js +25 -0
- package/dist/nodejs/filesystem.js +2 -0
- package/dist/nodejs/finalization.d.ts +2 -0
- package/{lib → dist}/nodejs/finalization.js +15 -21
- package/dist/nodejs/future.d.ts +98 -0
- package/dist/nodejs/future.js +176 -0
- package/dist/nodejs/http/client.d.ts +22 -0
- package/dist/nodejs/http/client.js +138 -0
- package/dist/nodejs/http/error.d.ts +18 -0
- package/dist/nodejs/http/error.js +62 -0
- package/dist/nodejs/http/fields.d.ts +144 -0
- package/dist/nodejs/http/fields.js +327 -0
- package/dist/nodejs/http/request.d.ts +256 -0
- package/dist/nodejs/http/request.js +606 -0
- package/dist/nodejs/http/response.d.ts +82 -0
- package/dist/nodejs/http/response.js +202 -0
- package/dist/nodejs/http/server.d.ts +13 -0
- package/dist/nodejs/http/server.js +127 -0
- package/dist/nodejs/http.d.ts +19 -0
- package/{lib → dist}/nodejs/http.js +13 -18
- package/dist/nodejs/index.js +8 -0
- package/dist/nodejs/random.d.ts +5 -0
- package/{lib → dist}/nodejs/random.js +2 -8
- package/dist/nodejs/sockets/address.d.ts +78 -0
- package/dist/nodejs/sockets/address.js +120 -0
- package/dist/nodejs/sockets/error.d.ts +83 -0
- package/dist/nodejs/sockets/error.js +120 -0
- package/dist/nodejs/sockets/ip-name-lookup.d.ts +18 -0
- package/dist/nodejs/sockets/ip-name-lookup.js +84 -0
- package/dist/nodejs/sockets/tcp.d.ts +333 -0
- package/dist/nodejs/sockets/tcp.js +723 -0
- package/dist/nodejs/sockets/udp.d.ts +224 -0
- package/dist/nodejs/sockets/udp.js +475 -0
- package/dist/nodejs/sockets.d.ts +11 -0
- package/{lib → dist}/nodejs/sockets.js +2 -5
- package/dist/nodejs/stream.d.ts +161 -0
- package/dist/nodejs/stream.js +388 -0
- package/dist/nodejs/workers/cli-worker.d.ts +1 -0
- package/dist/nodejs/workers/cli-worker.js +41 -0
- package/dist/nodejs/workers/filesystem-worker.d.ts +1 -0
- package/dist/nodejs/workers/filesystem-worker.js +102 -0
- package/dist/nodejs/workers/http-worker.d.ts +1 -0
- package/dist/nodejs/workers/http-worker.js +355 -0
- package/dist/nodejs/workers/resource-worker.d.ts +13 -0
- package/dist/nodejs/workers/resource-worker.js +152 -0
- package/dist/nodejs/workers/tcp-worker.d.ts +2 -0
- package/dist/nodejs/workers/tcp-worker.js +318 -0
- package/dist/nodejs/workers/udp-worker.d.ts +1 -0
- package/dist/nodejs/workers/udp-worker.js +156 -0
- package/package.json +20 -12
- package/types/interfaces/wasi-cli-environment.d.ts +3 -3
- package/types/interfaces/wasi-cli-exit.d.ts +12 -1
- package/types/interfaces/wasi-cli-run.d.ts +1 -1
- package/types/interfaces/wasi-cli-stderr.d.ts +3 -3
- package/types/interfaces/wasi-cli-stdin.d.ts +5 -5
- package/types/interfaces/wasi-cli-stdout.d.ts +3 -3
- package/types/interfaces/wasi-cli-terminal-input.d.ts +1 -1
- package/types/interfaces/wasi-cli-terminal-output.d.ts +1 -1
- package/types/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
- package/types/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
- package/types/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
- package/types/interfaces/wasi-cli-types.d.ts +5 -5
- package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +3 -3
- package/types/interfaces/wasi-clocks-system-clock.d.ts +6 -6
- package/types/interfaces/wasi-clocks-types.d.ts +1 -1
- package/types/interfaces/wasi-filesystem-preopens.d.ts +1 -1
- package/types/interfaces/wasi-filesystem-types.d.ts +74 -74
- package/types/interfaces/wasi-http-client.d.ts +1 -1
- package/types/interfaces/wasi-http-handler.d.ts +1 -1
- package/types/interfaces/wasi-http-types.d.ts +34 -34
- package/types/interfaces/wasi-random-insecure-seed.d.ts +5 -5
- package/types/interfaces/wasi-random-insecure.d.ts +5 -5
- package/types/interfaces/wasi-random-random.d.ts +5 -5
- package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +11 -11
- package/types/interfaces/wasi-sockets-types.d.ts +121 -121
- package/types/wasi-cli-command.d.ts +23 -23
- package/types/wasi-http-service.d.ts +14 -14
- package/lib/nodejs/cli.js +0 -127
- package/lib/nodejs/clocks.js +0 -100
- package/lib/nodejs/filesystem/descriptor.js +0 -1093
- package/lib/nodejs/filesystem/error.js +0 -92
- package/lib/nodejs/filesystem/utils.js +0 -19
- package/lib/nodejs/future.js +0 -190
- package/lib/nodejs/http/client.js +0 -161
- package/lib/nodejs/http/error.js +0 -71
- package/lib/nodejs/http/fields.js +0 -373
- package/lib/nodejs/http/request.js +0 -691
- package/lib/nodejs/http/response.js +0 -227
- package/lib/nodejs/http/server.js +0 -157
- package/lib/nodejs/sockets/address.js +0 -146
- package/lib/nodejs/sockets/error.js +0 -123
- package/lib/nodejs/sockets/ip-name-lookup.js +0 -93
- package/lib/nodejs/sockets/tcp.js +0 -801
- package/lib/nodejs/sockets/udp.js +0 -516
- package/lib/nodejs/stream.js +0 -437
- package/lib/nodejs/workers/cli-worker.js +0 -49
- package/lib/nodejs/workers/filesystem-worker.js +0 -119
- package/lib/nodejs/workers/http-worker.js +0 -417
- package/lib/nodejs/workers/resource-worker.js +0 -195
- package/lib/nodejs/workers/tcp-worker.js +0 -370
- package/lib/nodejs/workers/udp-worker.js +0 -194
- /package/{lib/nodejs/filesystem.js → dist/nodejs/filesystem.d.ts} +0 -0
- /package/{lib/nodejs/index.js → dist/nodejs/index.d.ts} +0 -0
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
// world wasi:cli/command@0.3.0
|
|
2
|
-
export type * as
|
|
3
|
-
export type * as
|
|
4
|
-
export type * as
|
|
5
|
-
export type * as
|
|
6
|
-
export type * as
|
|
7
|
-
export type * as
|
|
8
|
-
export type * as
|
|
9
|
-
export type * as
|
|
10
|
-
export type * as
|
|
11
|
-
export type * as
|
|
12
|
-
export type * as
|
|
13
|
-
export type * as
|
|
14
|
-
export type * as
|
|
15
|
-
export type * as
|
|
16
|
-
export type * as
|
|
17
|
-
export type * as
|
|
18
|
-
export type * as
|
|
19
|
-
export type * as
|
|
20
|
-
export type * as
|
|
21
|
-
export type * as
|
|
22
|
-
export type * as
|
|
23
|
-
export * as run from './interfaces/wasi-cli-run.js'; // export wasi:cli/run@0.3.0
|
|
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
|
|
2
|
-
export type * as
|
|
3
|
-
export type * as
|
|
4
|
-
export type * as
|
|
5
|
-
export type * as
|
|
6
|
-
export type * as
|
|
7
|
-
export type * as
|
|
8
|
-
export type * as
|
|
9
|
-
export type * as
|
|
10
|
-
export type * as
|
|
11
|
-
export type * as
|
|
12
|
-
export type * as
|
|
13
|
-
export type * as
|
|
14
|
-
export * as handler from './interfaces/wasi-http-handler.js'; // export wasi:http/handler@0.3.0
|
|
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
|
-
};
|
package/lib/nodejs/clocks.js
DELETED
|
@@ -1,100 +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
|
-
|
|
31
|
-
export const monotonicClock = {
|
|
32
|
-
...baseMonotonicClock,
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Query the resolution of the clock.
|
|
36
|
-
*
|
|
37
|
-
* WIT:
|
|
38
|
-
* ```
|
|
39
|
-
* get-resolution: func() -> duration;
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* @returns {bigint} Duration of a clock tick in nanoseconds.
|
|
43
|
-
*/
|
|
44
|
-
getResolution() {
|
|
45
|
-
return baseMonotonicClock.resolution();
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Waits until a specific mark (in nanoseconds) is reached
|
|
50
|
-
*
|
|
51
|
-
* WIT:
|
|
52
|
-
* ```
|
|
53
|
-
* wait-until: async func(when: mark);
|
|
54
|
-
* ```
|
|
55
|
-
* @async
|
|
56
|
-
* @param {bigint} targetNs - The target mark in nanoseconds to wait until
|
|
57
|
-
* @returns {Promise<void>} A promise that resolves when the target mark is reached
|
|
58
|
-
* @throws {TypeError} If targetNs is not a bigint
|
|
59
|
-
*/
|
|
60
|
-
async waitUntil(targetNs) {
|
|
61
|
-
const nowNs = monotonicClock.now();
|
|
62
|
-
const diffNs = targetNs - nowNs;
|
|
63
|
-
|
|
64
|
-
if (diffNs <= 0n) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const ms = diffNs / 1_000_000n;
|
|
69
|
-
if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
70
|
-
throw new TypeError(`Cannot wait for ${targetNs} ns, exceeds maximum safe integer`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
await sleep(Number(ms));
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Waits for a specified duration in nanoseconds
|
|
78
|
-
*
|
|
79
|
-
* WIT:
|
|
80
|
-
* ```
|
|
81
|
-
* wait-for: async func(how-long: duration);
|
|
82
|
-
* ```
|
|
83
|
-
* @async
|
|
84
|
-
* @param {bigint} durationNs - The duration to wait in nanoseconds
|
|
85
|
-
* @returns {Promise<void>} A promise that resolves after the specified duration
|
|
86
|
-
* @throws {TypeError} If durationNs is not a bigint
|
|
87
|
-
*/
|
|
88
|
-
async waitFor(durationNs) {
|
|
89
|
-
if (durationNs <= 0n) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const ms = durationNs / 1_000_000n;
|
|
94
|
-
if (ms > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
95
|
-
throw new TypeError(`Cannot wait for ${durationNs} ns, exceeds maximum safe integer`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await sleep(Number(ms));
|
|
99
|
-
},
|
|
100
|
-
};
|