@bytecodealliance/preview2-shim 0.18.1 → 0.19.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 (81) hide show
  1. package/dist/browser/cli.d.ts +16 -0
  2. package/dist/browser/cli.js +109 -0
  3. package/dist/browser/clocks.d.ts +3 -0
  4. package/dist/browser/clocks.js +36 -0
  5. package/dist/browser/config.d.ts +2 -0
  6. package/{lib → dist}/browser/config.js +2 -4
  7. package/dist/browser/environment.d.ts +5 -0
  8. package/dist/browser/environment.js +24 -0
  9. package/dist/browser/filesystem.d.ts +132 -0
  10. package/dist/browser/filesystem.js +464 -0
  11. package/dist/browser/http.d.ts +4 -0
  12. package/dist/browser/http.js +643 -0
  13. package/dist/browser/index.d.ts +7 -0
  14. package/dist/browser/index.js +7 -0
  15. package/dist/browser/io.d.ts +58 -0
  16. package/dist/browser/io.js +205 -0
  17. package/dist/browser/random.d.ts +4 -0
  18. package/dist/browser/random.js +49 -0
  19. package/dist/browser/sockets.d.ts +8 -0
  20. package/dist/browser/sockets.js +66 -0
  21. package/dist/common/instantiation.d.ts +97 -0
  22. package/dist/common/instantiation.js +227 -0
  23. package/dist/io/calls.d.ts +91 -0
  24. package/{lib → dist}/io/calls.js +4 -19
  25. package/dist/io/worker-http.d.ts +17 -0
  26. package/dist/io/worker-http.js +192 -0
  27. package/dist/io/worker-io.d.ts +84 -0
  28. package/dist/io/worker-io.js +375 -0
  29. package/dist/io/worker-socket-tcp.d.ts +34 -0
  30. package/dist/io/worker-socket-tcp.js +251 -0
  31. package/dist/io/worker-socket-udp.d.ts +54 -0
  32. package/dist/io/worker-socket-udp.js +492 -0
  33. package/dist/io/worker-sockets.d.ts +32 -0
  34. package/dist/io/worker-sockets.js +265 -0
  35. package/dist/io/worker-thread.d.ts +73 -0
  36. package/dist/io/worker-thread.js +791 -0
  37. package/dist/nodejs/cli.d.ts +30 -0
  38. package/dist/nodejs/cli.js +86 -0
  39. package/dist/nodejs/clocks.d.ts +3 -0
  40. package/dist/nodejs/clocks.js +61 -0
  41. package/dist/nodejs/filesystem.d.ts +113 -0
  42. package/dist/nodejs/filesystem.js +758 -0
  43. package/dist/nodejs/http.d.ts +82 -0
  44. package/dist/nodejs/http.js +674 -0
  45. package/dist/nodejs/index.d.ts +7 -0
  46. package/dist/nodejs/index.js +7 -0
  47. package/dist/nodejs/io.d.ts +1 -0
  48. package/dist/nodejs/io.js +1 -0
  49. package/dist/nodejs/random.d.ts +4 -0
  50. package/dist/nodejs/random.js +42 -0
  51. package/dist/nodejs/sockets.d.ts +21 -0
  52. package/dist/nodejs/sockets.js +482 -0
  53. package/dist/synckit/index.d.ts +2 -0
  54. package/dist/synckit/index.js +117 -0
  55. package/package.json +26 -13
  56. package/types/instantiation.d.ts +7 -7
  57. package/lib/browser/cli.js +0 -135
  58. package/lib/browser/clocks.js +0 -39
  59. package/lib/browser/environment.js +0 -29
  60. package/lib/browser/filesystem.js +0 -503
  61. package/lib/browser/http.js +0 -707
  62. package/lib/browser/index.js +0 -9
  63. package/lib/browser/io.js +0 -262
  64. package/lib/browser/random.js +0 -56
  65. package/lib/browser/sockets.js +0 -88
  66. package/lib/common/instantiation.js +0 -253
  67. package/lib/io/worker-http.js +0 -206
  68. package/lib/io/worker-io.js +0 -468
  69. package/lib/io/worker-socket-tcp.js +0 -331
  70. package/lib/io/worker-socket-udp.js +0 -587
  71. package/lib/io/worker-sockets.js +0 -364
  72. package/lib/io/worker-thread.js +0 -1051
  73. package/lib/nodejs/cli.js +0 -109
  74. package/lib/nodejs/clocks.js +0 -67
  75. package/lib/nodejs/filesystem.js +0 -829
  76. package/lib/nodejs/http.js +0 -764
  77. package/lib/nodejs/index.js +0 -9
  78. package/lib/nodejs/io.js +0 -1
  79. package/lib/nodejs/random.js +0 -49
  80. package/lib/nodejs/sockets.js +0 -565
  81. package/lib/synckit/index.js +0 -127
@@ -0,0 +1,16 @@
1
+ import type { exit as ExitNamespace, stderr as StderrNamespace, stdin as StdinNamespace, stdout as StdoutNamespace, terminalInput as TerminalInputNamespace, terminalOutput as TerminalOutputNamespace, terminalStderr as TerminalStderrNamespace, terminalStdin as TerminalStdinNamespace, terminalStdout as TerminalStdoutNamespace } from "../../types/cli.js";
2
+ import { type InputStreamHandler, type OutputStreamHandler } from "./io.js";
3
+ export { _setEnv, _setArgs, environment } from "./environment.js";
4
+ export { _setCwd } from "./config.js";
5
+ export declare const exit: typeof ExitNamespace;
6
+ export declare function _setStdin(handler: InputStreamHandler): void;
7
+ export declare function _setStderr(handler: OutputStreamHandler): void;
8
+ export declare function _setStdout(handler: OutputStreamHandler): void;
9
+ export declare const stdin: typeof StdinNamespace;
10
+ export declare const stdout: typeof StdoutNamespace;
11
+ export declare const stderr: typeof StderrNamespace;
12
+ export declare const terminalInput: typeof TerminalInputNamespace;
13
+ export declare const terminalOutput: typeof TerminalOutputNamespace;
14
+ export declare const terminalStderr: typeof TerminalStderrNamespace;
15
+ export declare const terminalStdin: typeof TerminalStdinNamespace;
16
+ export declare const terminalStdout: typeof TerminalStdoutNamespace;
@@ -0,0 +1,109 @@
1
+ import { inputStreamCreate, outputStreamCreate, pollableCreate, } from "./io.js";
2
+ export { _setEnv, _setArgs, environment } from "./environment.js";
3
+ export { _setCwd } from "./config.js";
4
+ const symbolDispose = Symbol.dispose ?? Symbol.for("dispose");
5
+ class ComponentExit extends Error {
6
+ exitError = true;
7
+ code;
8
+ constructor(code) {
9
+ super(`Component exited ${code === 0 ? "successfully" : "with error"}`);
10
+ this.code = code;
11
+ }
12
+ }
13
+ export const exit = {
14
+ exit(status) {
15
+ throw new ComponentExit(status.tag === "err" ? 1 : 0);
16
+ },
17
+ // @ts-expect-error - Available only wasi-cli v0.2.12
18
+ exitWithCode(code) {
19
+ throw new ComponentExit(code);
20
+ },
21
+ };
22
+ export function _setStdin(handler) {
23
+ stdinStream.handler = handler;
24
+ }
25
+ export function _setStderr(handler) {
26
+ stderrStream.handler = handler;
27
+ }
28
+ export function _setStdout(handler) {
29
+ stdoutStream.handler = handler;
30
+ }
31
+ const stdinStream = inputStreamCreate({
32
+ blockingRead(_len) {
33
+ // TODO
34
+ return new Uint8Array(0);
35
+ },
36
+ subscribe() {
37
+ // TODO
38
+ return pollableCreate();
39
+ },
40
+ [symbolDispose]() {
41
+ // TODO
42
+ },
43
+ });
44
+ const textDecoder = new TextDecoder();
45
+ const stdoutStream = outputStreamCreate({
46
+ write(contents) {
47
+ if (contents.at(-1) == 10) {
48
+ // console.log already appends a new line
49
+ contents = contents.subarray(0, -1);
50
+ }
51
+ console.log(textDecoder.decode(contents));
52
+ },
53
+ blockingFlush() { },
54
+ [symbolDispose]() { },
55
+ });
56
+ const stderrStream = outputStreamCreate({
57
+ write(contents) {
58
+ if (contents.at(-1) == 10) {
59
+ // console.error already appends a new line
60
+ contents = contents.subarray(0, -1);
61
+ }
62
+ console.error(textDecoder.decode(contents));
63
+ },
64
+ blockingFlush() { },
65
+ [symbolDispose]() { },
66
+ });
67
+ export const stdin = {
68
+ getStdin() {
69
+ return stdinStream;
70
+ },
71
+ };
72
+ export const stdout = {
73
+ getStdout() {
74
+ return stdoutStream;
75
+ },
76
+ };
77
+ export const stderr = {
78
+ getStderr() {
79
+ return stderrStream;
80
+ },
81
+ };
82
+ class TerminalInput {
83
+ }
84
+ class TerminalOutput {
85
+ }
86
+ const terminalStdoutInstance = new TerminalOutput();
87
+ const terminalStderrInstance = new TerminalOutput();
88
+ const terminalStdinInstance = new TerminalInput();
89
+ export const terminalInput = {
90
+ TerminalInput,
91
+ };
92
+ export const terminalOutput = {
93
+ TerminalOutput,
94
+ };
95
+ export const terminalStderr = {
96
+ getTerminalStderr() {
97
+ return terminalStderrInstance;
98
+ },
99
+ };
100
+ export const terminalStdin = {
101
+ getTerminalStdin() {
102
+ return terminalStdinInstance;
103
+ },
104
+ };
105
+ export const terminalStdout = {
106
+ getTerminalStdout() {
107
+ return terminalStdoutInstance;
108
+ },
109
+ };
@@ -0,0 +1,3 @@
1
+ import type { monotonicClock as MonotonicClockNamespace, wallClock as WallClockNamespace } from "../../types/clocks.js";
2
+ export declare const monotonicClock: typeof MonotonicClockNamespace;
3
+ export declare const wallClock: typeof WallClockNamespace;
@@ -0,0 +1,36 @@
1
+ import { pollableCreate } from "./io.js";
2
+ export const monotonicClock = {
3
+ resolution() {
4
+ // usually we dont get sub-millisecond accuracy in the browser
5
+ // Note: is there a better way to determine this?
6
+ return BigInt(1e6);
7
+ },
8
+ now() {
9
+ // performance.now() is in milliseconds, but we want nanoseconds
10
+ return BigInt(Math.floor(performance.now() * 1e6));
11
+ },
12
+ subscribeInstant(instant) {
13
+ instant = BigInt(instant);
14
+ const now = monotonicClock.now();
15
+ if (instant <= now) {
16
+ return pollableCreate(new Promise((resolve) => setTimeout(resolve, 0)));
17
+ }
18
+ return monotonicClock.subscribeDuration(instant - now);
19
+ },
20
+ subscribeDuration(duration) {
21
+ duration = BigInt(duration);
22
+ const ms = duration <= 0n ? 0 : Number(duration / 1000000n);
23
+ return pollableCreate(new Promise((resolve) => setTimeout(resolve, ms)));
24
+ },
25
+ };
26
+ export const wallClock = {
27
+ now() {
28
+ let now = Date.now(); // in milliseconds
29
+ const seconds = BigInt(Math.floor(now / 1e3));
30
+ const nanoseconds = (now % 1e3) * 1e6;
31
+ return { seconds, nanoseconds };
32
+ },
33
+ resolution() {
34
+ return { seconds: 0n, nanoseconds: 1e6 };
35
+ },
36
+ };
@@ -0,0 +1,2 @@
1
+ export declare function _setCwd(cwd: string): void;
2
+ export declare function _getCwd(): string;
@@ -1,9 +1,7 @@
1
1
  let _cwd = "/";
2
-
3
2
  export function _setCwd(cwd) {
4
- _cwd = cwd;
3
+ _cwd = cwd;
5
4
  }
6
-
7
5
  export function _getCwd() {
8
- return _cwd;
6
+ return _cwd;
9
7
  }
@@ -0,0 +1,5 @@
1
+ import type { environment as EnvironmentNamespace } from "../../types/cli.js";
2
+ export declare function _setEnv(envObj: Record<string, string>): void;
3
+ export declare function _setArgs(args: string[]): void;
4
+ export declare function _setCwd(cwd: string): void;
5
+ export declare const environment: typeof EnvironmentNamespace;
@@ -0,0 +1,24 @@
1
+ import { _setCwd as fsSetCwd } from "./config.js";
2
+ let _env = [];
3
+ let _args = [];
4
+ let _cwd = "/";
5
+ export function _setEnv(envObj) {
6
+ _env = Object.entries(envObj);
7
+ }
8
+ export function _setArgs(args) {
9
+ _args = args;
10
+ }
11
+ export function _setCwd(cwd) {
12
+ fsSetCwd((_cwd = cwd));
13
+ }
14
+ export const environment = {
15
+ getEnvironment() {
16
+ return _env;
17
+ },
18
+ getArguments() {
19
+ return _args;
20
+ },
21
+ initialCwd() {
22
+ return _cwd;
23
+ },
24
+ };
@@ -0,0 +1,132 @@
1
+ import { types as TypesNamespace, preopens as PreopensNamespace } from "../../types/filesystem.js";
2
+ import { InputStream as IInputStream, OutputStream as IOutputStream } from "../../types/interfaces/wasi-io-streams.js";
3
+ export { _setCwd } from "./config.js";
4
+ type Filesize = TypesNamespace.Filesize;
5
+ type OpenFlags = TypesNamespace.OpenFlags;
6
+ type PathFlags = TypesNamespace.PathFlags;
7
+ export interface FileDataEntry {
8
+ dir?: Record<string, FileDataEntry>;
9
+ source?: Uint8Array | string;
10
+ }
11
+ /**
12
+ * Root file data structure representing a filesystem tree.
13
+ * Each entry is either a directory (has `dir` property) or a file (has `source` property).
14
+ * @example
15
+ * // A simple filesystem with one directory containing one file:
16
+ * const fileData = {
17
+ * dir: {
18
+ * 'myfile.txt': { source: new Uint8Array([72, 101, 108, 108, 111]) }
19
+ * }
20
+ * };
21
+ */
22
+ export type FileData = FileDataEntry;
23
+ export declare function _setFileData(fileData: FileData): void;
24
+ export declare function _getFileData(): string;
25
+ declare class DirectoryEntryStream implements TypesNamespace.DirectoryEntryStream {
26
+ idx: number;
27
+ entries: [string, FileDataEntry][];
28
+ static _create(entries: [string, FileDataEntry][]): DirectoryEntryStream;
29
+ readDirectoryEntry(): TypesNamespace.DirectoryEntry | undefined;
30
+ }
31
+ declare class Descriptor implements TypesNamespace.Descriptor {
32
+ #private;
33
+ _getEntry(descriptor: Descriptor): FileDataEntry;
34
+ static _create(entry: FileDataEntry | any, isStream?: boolean): Descriptor;
35
+ readViaStream(_offset: bigint): IInputStream;
36
+ writeViaStream(_offset: bigint): IOutputStream;
37
+ appendViaStream(): IOutputStream;
38
+ advise(offset: Filesize, length: Filesize, advice: TypesNamespace.Advice): void;
39
+ syncData(): void;
40
+ getFlags(): TypesNamespace.DescriptorFlags;
41
+ getType(): "directory" | "regular-file" | "unknown" | "fifo";
42
+ setSize(size: bigint): void;
43
+ setTimes(dataAccessTimestamp: any, dataModificationTimestamp: any): void;
44
+ read(length: bigint, offset: bigint): [Uint8Array<ArrayBufferLike>, boolean];
45
+ write(buffer: Uint8Array, offset: Filesize): bigint;
46
+ readDirectory(): DirectoryEntryStream;
47
+ sync(): void;
48
+ createDirectoryAt(path: string): void;
49
+ stat(): {
50
+ type: "directory" | "regular-file" | "unknown";
51
+ linkCount: bigint;
52
+ size: bigint;
53
+ dataAccessTimestamp: {
54
+ seconds: bigint;
55
+ nanoseconds: number;
56
+ };
57
+ dataModificationTimestamp: {
58
+ seconds: bigint;
59
+ nanoseconds: number;
60
+ };
61
+ statusChangeTimestamp: {
62
+ seconds: bigint;
63
+ nanoseconds: number;
64
+ };
65
+ };
66
+ statAt(_pathFlags: PathFlags, path: string): {
67
+ type: "directory" | "regular-file" | "unknown";
68
+ linkCount: bigint;
69
+ size: bigint;
70
+ dataAccessTimestamp: {
71
+ seconds: bigint;
72
+ nanoseconds: number;
73
+ };
74
+ dataModificationTimestamp: {
75
+ seconds: bigint;
76
+ nanoseconds: number;
77
+ };
78
+ statusChangeTimestamp: {
79
+ seconds: bigint;
80
+ nanoseconds: number;
81
+ };
82
+ };
83
+ setTimesAt(): void;
84
+ linkAt(): void;
85
+ openAt(_pathFlags: PathFlags, path: string, openFlags: OpenFlags, _flags: TypesNamespace.DescriptorFlags): Descriptor;
86
+ readlinkAt(_path: string): string;
87
+ removeDirectoryAt(): void;
88
+ renameAt(): void;
89
+ symlinkAt(): void;
90
+ unlinkFileAt(): void;
91
+ isSameObject(other: TypesNamespace.Descriptor): boolean;
92
+ metadataHash(): {
93
+ upper: bigint;
94
+ lower: bigint;
95
+ };
96
+ metadataHashAt(_pathFlags: any, _path: string): {
97
+ upper: bigint;
98
+ lower: bigint;
99
+ };
100
+ }
101
+ export declare const preopens: typeof PreopensNamespace;
102
+ /**
103
+ * Replace all preopens with the given set.
104
+ * @param preopensConfig - Map of virtual paths to file data entries
105
+ */
106
+ export declare function _setPreopens(preopensConfig: Record<string, FileData>): void;
107
+ /**
108
+ * Add a single preopen mapping.
109
+ * @param virtualPath - The virtual path visible to the guest
110
+ * @param fileData - The file data object representing the directory
111
+ */
112
+ export declare function _addPreopen(virtualPath: string, fileData: FileData): void;
113
+ /**
114
+ * Clear all preopens, giving the guest no filesystem access.
115
+ *
116
+ * This functionality exists mostly to maintain backwards compatibility. Prefer setting preopens
117
+ * via `WASIShim` rather than making top level changes to preopens using these functions.
118
+ */
119
+ export declare function _clearPreopens(): void;
120
+ /**
121
+ * Get current preopens configuration.
122
+ * @returns Array of [descriptor, virtualPath] pairs
123
+ */
124
+ export declare function _getPreopens(): [Descriptor, string][];
125
+ /**
126
+ * Create a preopen descriptor for a host path.
127
+ * This is used internally to create isolated preopen instances.
128
+ * @param hostPreopen - The host filesystem path
129
+ * @returns A preopen descriptor
130
+ */
131
+ export declare function _createPreopenDescriptor(hostPreopen: string): Descriptor;
132
+ export declare const types: typeof TypesNamespace;