@bytecodealliance/preview2-shim 0.17.1 → 0.17.3

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 (67) hide show
  1. package/lib/browser/cli.js +91 -94
  2. package/lib/browser/clocks.js +30 -29
  3. package/lib/browser/filesystem.js +298 -251
  4. package/lib/browser/http.js +129 -128
  5. package/lib/browser/index.js +8 -16
  6. package/lib/browser/io.js +143 -135
  7. package/lib/browser/random.js +44 -42
  8. package/lib/browser/sockets.js +68 -166
  9. package/lib/common/instantiation.js +127 -0
  10. package/lib/io/calls.js +7 -5
  11. package/lib/io/worker-http.js +175 -157
  12. package/lib/io/worker-io.js +402 -386
  13. package/lib/io/worker-socket-tcp.js +271 -219
  14. package/lib/io/worker-socket-udp.js +494 -429
  15. package/lib/io/worker-sockets.js +276 -262
  16. package/lib/io/worker-thread.js +946 -815
  17. package/lib/nodejs/cli.js +64 -63
  18. package/lib/nodejs/clocks.js +51 -45
  19. package/lib/nodejs/filesystem.js +788 -654
  20. package/lib/nodejs/http.js +693 -617
  21. package/lib/nodejs/index.js +8 -16
  22. package/lib/nodejs/random.js +32 -28
  23. package/lib/nodejs/sockets.js +538 -474
  24. package/lib/synckit/index.js +94 -85
  25. package/package.json +9 -5
  26. package/types/cli.d.ts +11 -23
  27. package/types/clocks.d.ts +2 -5
  28. package/types/filesystem.d.ts +2 -5
  29. package/types/http.d.ts +3 -7
  30. package/types/index.d.ts +6 -15
  31. package/types/instantiation.d.ts +112 -0
  32. package/types/interfaces/wasi-cli-environment.d.ts +21 -22
  33. package/types/interfaces/wasi-cli-exit.d.ts +5 -6
  34. package/types/interfaces/wasi-cli-run.d.ts +5 -6
  35. package/types/interfaces/wasi-cli-stderr.d.ts +3 -5
  36. package/types/interfaces/wasi-cli-stdin.d.ts +3 -5
  37. package/types/interfaces/wasi-cli-stdout.d.ts +3 -5
  38. package/types/interfaces/wasi-cli-terminal-input.d.ts +5 -3
  39. package/types/interfaces/wasi-cli-terminal-output.d.ts +5 -3
  40. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +7 -9
  41. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +7 -9
  42. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +7 -9
  43. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +24 -26
  44. package/types/interfaces/wasi-clocks-wall-clock.d.ts +23 -24
  45. package/types/interfaces/wasi-filesystem-preopens.d.ts +6 -8
  46. package/types/interfaces/wasi-filesystem-types.d.ts +34 -33
  47. package/types/interfaces/wasi-http-incoming-handler.d.ts +16 -19
  48. package/types/interfaces/wasi-http-outgoing-handler.d.ts +18 -23
  49. package/types/interfaces/wasi-http-types.d.ts +49 -38
  50. package/types/interfaces/wasi-io-error.d.ts +5 -3
  51. package/types/interfaces/wasi-io-poll.d.ts +27 -25
  52. package/types/interfaces/wasi-io-streams.d.ts +24 -21
  53. package/types/interfaces/wasi-random-insecure-seed.d.ts +21 -22
  54. package/types/interfaces/wasi-random-insecure.d.ts +19 -20
  55. package/types/interfaces/wasi-random-random.d.ts +23 -24
  56. package/types/interfaces/wasi-sockets-instance-network.d.ts +6 -8
  57. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +32 -34
  58. package/types/interfaces/wasi-sockets-network.d.ts +5 -3
  59. package/types/interfaces/wasi-sockets-tcp-create-socket.d.ts +28 -33
  60. package/types/interfaces/wasi-sockets-tcp.d.ts +17 -23
  61. package/types/interfaces/wasi-sockets-udp-create-socket.d.ts +28 -33
  62. package/types/interfaces/wasi-sockets-udp.d.ts +20 -17
  63. package/types/io.d.ts +3 -7
  64. package/types/random.d.ts +3 -7
  65. package/types/sockets.d.ts +7 -15
  66. package/types/wasi-cli-command.d.ts +29 -29
  67. package/types/wasi-http-proxy.d.ts +13 -13
@@ -23,105 +23,114 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
23
  SOFTWARE.
24
24
  */
25
25
 
26
- import path from "node:path";
26
+ import path from 'node:path';
27
27
  import {
28
- MessageChannel,
29
- Worker,
30
- receiveMessageOnPort,
31
- workerData,
32
- parentPort,
33
- } from "node:worker_threads";
28
+ MessageChannel,
29
+ Worker,
30
+ receiveMessageOnPort,
31
+ workerData,
32
+ parentPort,
33
+ } from 'node:worker_threads';
34
34
 
35
35
  const DEFAULT_WORKER_BUFFER_SIZE = 1024;
36
36
 
37
37
  function extractProperties(object) {
38
- if (object && typeof object === "object") {
39
- const properties = {};
40
- for (const key in object) {
41
- properties[key] = object[key];
38
+ if (object && typeof object === 'object') {
39
+ const properties = {};
40
+ for (const key in object) {
41
+ properties[key] = object[key];
42
+ }
43
+ return properties;
42
44
  }
43
- return properties;
44
- }
45
45
  }
46
46
 
47
47
  const CALL_TIMEOUT = undefined;
48
48
 
49
49
  export function createSyncFn(workerPath, debug, callbackHandler) {
50
- if (!path.isAbsolute(workerPath)) {
51
- throw new Error("`workerPath` must be absolute");
52
- }
53
- const { port1: mainPort, port2: workerPort } = new MessageChannel();
54
- const worker = new Worker(workerPath, {
55
- workerData: { workerPort, debug },
56
- transferList: [workerPort],
57
- execArgv: []
58
- });
59
- worker.on('message', ({ type, id, payload }) => {
60
- if (!type)
61
- throw new Error('Internal error: Expected a type of a worker callback');
62
- callbackHandler(type, id, payload);
63
- });
64
- let nextID = 0;
65
- const syncFn = (...args) => {
66
- const cid = nextID++;
67
- const sharedBuffer = new SharedArrayBuffer(DEFAULT_WORKER_BUFFER_SIZE);
68
- const sharedBufferView = new Int32Array(sharedBuffer);
69
- const msg = { sharedBuffer, cid, args };
70
- worker.postMessage(msg);
71
- const status = Atomics.wait(sharedBufferView, 0, 0, CALL_TIMEOUT);
72
- if (!["ok", "not-equal"].includes(status)) {
73
- throw new Error("Internal error: Atomics.wait() failed: " + status);
74
- }
75
- const {
76
- cid: cid2,
77
- result,
78
- error,
79
- properties,
80
- } = receiveMessageOnPort(mainPort).message;
81
- if (cid !== cid2) {
82
- throw new Error(`Internal error: Expected id ${cid} but got id ${cid2}`);
50
+ if (!path.isAbsolute(workerPath)) {
51
+ throw new Error('`workerPath` must be absolute');
83
52
  }
84
- if (error) {
85
- if (error instanceof Error) throw Object.assign(error, properties);
86
- throw error;
53
+ const { port1: mainPort, port2: workerPort } = new MessageChannel();
54
+ const worker = new Worker(workerPath, {
55
+ workerData: { workerPort, debug },
56
+ transferList: [workerPort],
57
+ execArgv: [],
58
+ });
59
+ worker.on('message', ({ type, id, payload }) => {
60
+ if (!type) {
61
+ throw new Error(
62
+ 'Internal error: Expected a type of a worker callback'
63
+ );
64
+ }
65
+ callbackHandler(type, id, payload);
66
+ });
67
+ let nextID = 0;
68
+ const syncFn = (...args) => {
69
+ const cid = nextID++;
70
+ const sharedBuffer = new SharedArrayBuffer(DEFAULT_WORKER_BUFFER_SIZE);
71
+ const sharedBufferView = new Int32Array(sharedBuffer);
72
+ const msg = { sharedBuffer, cid, args };
73
+ worker.postMessage(msg);
74
+ const status = Atomics.wait(sharedBufferView, 0, 0, CALL_TIMEOUT);
75
+ if (!['ok', 'not-equal'].includes(status)) {
76
+ throw new Error('Internal error: Atomics.wait() failed: ' + status);
77
+ }
78
+ const {
79
+ cid: cid2,
80
+ result,
81
+ error,
82
+ properties,
83
+ } = receiveMessageOnPort(mainPort).message;
84
+ if (cid !== cid2) {
85
+ throw new Error(
86
+ `Internal error: Expected id ${cid} but got id ${cid2}`
87
+ );
88
+ }
89
+ if (error) {
90
+ if (error instanceof Error) {
91
+ throw Object.assign(error, properties);
92
+ }
93
+ throw error;
94
+ }
95
+ return result;
96
+ };
97
+ if (worker.unref) {
98
+ worker.unref();
87
99
  }
88
- return result;
89
- };
90
- if (worker.unref) worker.unref();
91
- return syncFn;
100
+ return syncFn;
92
101
  }
93
102
 
94
103
  export function runAsWorker(fn) {
95
- if (!workerData) {
96
- return;
97
- }
98
- const { workerPort, debug } = workerData;
99
- try {
100
- parentPort.on("message", ({ sharedBuffer, cid, args }) => {
101
- (async () => {
102
- const sharedBufferView = new Int32Array(sharedBuffer);
103
- let msg;
104
- try {
105
- msg = { cid, result: await fn(...args) };
106
- } catch (error) {
107
- msg = { cid, error, properties: extractProperties(error) };
108
- }
109
- workerPort.postMessage(msg);
110
- Atomics.add(sharedBufferView, 0, 1);
111
- Atomics.notify(sharedBufferView, 0);
112
- })();
113
- });
114
- } catch (error) {
115
- parentPort.on("message", ({ sharedBuffer, cid }) => {
116
- const sharedBufferView = new Int32Array(sharedBuffer);
117
- workerPort.postMessage({
118
- cid,
119
- error,
120
- properties: extractProperties(error),
121
- });
122
- Atomics.add(sharedBufferView, 0, 1);
123
- Atomics.notify(sharedBufferView, 0);
124
- });
125
- }
126
- return debug;
104
+ if (!workerData) {
105
+ return;
106
+ }
107
+ const { workerPort, debug } = workerData;
108
+ try {
109
+ parentPort.on('message', ({ sharedBuffer, cid, args }) => {
110
+ (async () => {
111
+ const sharedBufferView = new Int32Array(sharedBuffer);
112
+ let msg;
113
+ try {
114
+ msg = { cid, result: await fn(...args) };
115
+ } catch (error) {
116
+ msg = { cid, error, properties: extractProperties(error) };
117
+ }
118
+ workerPort.postMessage(msg);
119
+ Atomics.add(sharedBufferView, 0, 1);
120
+ Atomics.notify(sharedBufferView, 0);
121
+ })();
122
+ });
123
+ } catch (error) {
124
+ parentPort.on('message', ({ sharedBuffer, cid }) => {
125
+ const sharedBufferView = new Int32Array(sharedBuffer);
126
+ workerPort.postMessage({
127
+ cid,
128
+ error,
129
+ properties: extractProperties(error),
130
+ });
131
+ Atomics.add(sharedBufferView, 0, 1);
132
+ Atomics.notify(sharedBufferView, 0);
133
+ });
134
+ }
135
+ return debug;
127
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "description": "WASI Preview2 shim for JS environments",
5
5
  "author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
6
6
  "type": "module",
@@ -15,18 +15,22 @@
15
15
  "types": "./types/*.d.ts",
16
16
  "node": "./lib/nodejs/*.js",
17
17
  "default": "./lib/browser/*.js"
18
+ },
19
+ "./instantiation": {
20
+ "types": "./types/instantiation.d.ts",
21
+ "node": "./lib/common/instantiation.js",
22
+ "default": "./lib/common/instantiation.js"
18
23
  }
19
24
  },
20
25
  "scripts": {
21
- "test": "node --expose-gc ../../node_modules/mocha/bin/mocha.js -u tdd test/test.js --timeout 30000"
26
+ "lint": "eslint -c ../../eslint.config.mjs ./lib/**/*.js",
27
+ "lint:fix": "npm run lint -- --fix",
28
+ "test": "vitest run -c test/vitest.ts"
22
29
  },
23
30
  "files": [
24
31
  "types",
25
32
  "lib"
26
33
  ],
27
- "devDependencies": {
28
- "mocha": "^10.2.0"
29
- },
30
34
  "repository": {
31
35
  "type": "git",
32
36
  "url": "git+https://github.com/bytecodealliance/jco.git"
package/types/cli.d.ts CHANGED
@@ -1,23 +1,11 @@
1
- import type { WasiCliEnvironment } from './interfaces/wasi-cli-environment.d.ts';
2
- import type { WasiCliExit } from './interfaces/wasi-cli-exit.d.ts';
3
- import type { WasiCliRun } from './interfaces/wasi-cli-run.d.ts';
4
- import type { WasiCliStderr } from './interfaces/wasi-cli-stderr.d.ts';
5
- import type { WasiCliStdin } from './interfaces/wasi-cli-stdin.d.ts';
6
- import type { WasiCliStdout } from './interfaces/wasi-cli-stdout.d.ts';
7
- import type { WasiCliTerminalInput } from './interfaces/wasi-cli-terminal-input.d.ts';
8
- import type { WasiCliTerminalOutput } from './interfaces/wasi-cli-terminal-output.d.ts';
9
- import type { WasiCliTerminalStderr } from './interfaces/wasi-cli-terminal-stderr.d.ts';
10
- import type { WasiCliTerminalStdin } from './interfaces/wasi-cli-terminal-stdin.d.ts';
11
- import type { WasiCliTerminalStdout } from './interfaces/wasi-cli-terminal-stdout.d.ts';
12
-
13
- export const environment: typeof WasiCliEnvironment;
14
- export const exit: typeof WasiCliExit;
15
- export const run: typeof WasiCliRun;
16
- export const stderr: typeof WasiCliStderr;
17
- export const stdin: typeof WasiCliStdin;
18
- export const stdout: typeof WasiCliStdout;
19
- export const terminalInput: typeof WasiCliTerminalInput;
20
- export const terminalOutput: typeof WasiCliTerminalOutput;
21
- export const terminalStderr: typeof WasiCliTerminalStderr;
22
- export const terminalStdin: typeof WasiCliTerminalStdin;
23
- export const terminalStdout: typeof WasiCliTerminalStdout;
1
+ export type * as environment from './interfaces/wasi-cli-environment.d.ts';
2
+ export type * as exit from './interfaces/wasi-cli-exit.d.ts';
3
+ export type * as run from './interfaces/wasi-cli-run.d.ts';
4
+ export type * as stderr from './interfaces/wasi-cli-stderr.d.ts';
5
+ export type * as stdin from './interfaces/wasi-cli-stdin.d.ts';
6
+ export type * as stdout from './interfaces/wasi-cli-stdout.d.ts';
7
+ export type * as terminalInput from './interfaces/wasi-cli-terminal-input.d.ts';
8
+ export type * as terminalOutput from './interfaces/wasi-cli-terminal-output.d.ts';
9
+ export type * as terminalStderr from './interfaces/wasi-cli-terminal-stderr.d.ts';
10
+ export type * as terminalStdin from './interfaces/wasi-cli-terminal-stdin.d.ts';
11
+ export type * as terminalStdout from './interfaces/wasi-cli-terminal-stdout.d.ts';
package/types/clocks.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- import type { WasiClocksMonotonicClock } from './interfaces/wasi-clocks-monotonic-clock.d.ts';
2
- import type { WasiClocksWallClock } from './interfaces/wasi-clocks-wall-clock.d.ts';
3
-
4
- export const wallClock: typeof WasiClocksWallClock;
5
- export const monotonicClock: typeof WasiClocksMonotonicClock;
1
+ export type * as monotonicClock from './interfaces/wasi-clocks-monotonic-clock.d.ts';
2
+ export type * as wallClock from './interfaces/wasi-clocks-wall-clock.d.ts';
@@ -1,5 +1,2 @@
1
- import type { WasiFilesystemPreopens } from './interfaces/wasi-filesystem-preopens.d.ts';
2
- import type { WasiFilesystemTypes } from './interfaces/wasi-filesystem-types.d.ts';
3
-
4
- export const preopens: typeof WasiFilesystemPreopens;
5
- export const types: typeof WasiFilesystemTypes;
1
+ export type * as preopens from './interfaces/wasi-filesystem-preopens.d.ts';
2
+ export type * as types from './interfaces/wasi-filesystem-types.d.ts';
package/types/http.d.ts CHANGED
@@ -1,7 +1,3 @@
1
- import type { WasiHttpIncomingHandler } from './interfaces/wasi-http-incoming-handler.d.ts';
2
- import type { WasiHttpOutgoingHandler } from './interfaces/wasi-http-outgoing-handler.d.ts';
3
- import type { WasiHttpTypes } from './interfaces/wasi-http-types.d.ts';
4
-
5
- export const incomingHandler: typeof WasiHttpIncomingHandler;
6
- export const outgoingHandler: typeof WasiHttpOutgoingHandler;
7
- export const types: typeof WasiHttpTypes;
1
+ export type * as incomingHandler from './interfaces/wasi-http-incoming-handler.d.ts';
2
+ export type * as outgoingHandler from './interfaces/wasi-http-outgoing-handler.d.ts';
3
+ export type * as types from './interfaces/wasi-http-types.d.ts';
package/types/index.d.ts CHANGED
@@ -1,15 +1,6 @@
1
- import type * as WasiCli from "./cli.d.ts";
2
- import type * as WasiClocks from './clocks.d.ts';
3
- import type * as WasiFilesystem from './filesystem.d.ts';
4
- import type * as WasiHttp from "./http.d.ts";
5
- import type * as WasiIo from "./io.d.ts";
6
- import type * as WasiRandom from "./random.d.ts";
7
- import type * as WasiSockets from "./sockets.d.ts";
8
-
9
- export const cli: typeof WasiCli;
10
- export const clocks: typeof WasiClocks;
11
- export const filesystem: typeof WasiFilesystem;
12
- export const http: typeof WasiHttp;
13
- export const io: typeof WasiIo;
14
- export const random: typeof WasiRandom;
15
- export const sockets: typeof WasiSockets;
1
+ export type * as cli from "./cli.d.ts";
2
+ export type * as clocks from './clocks.d.ts';
3
+ export type * as filesystem from './filesystem.d.ts';
4
+ export type * as http from "./http.d.ts";
5
+ export type * as random from "./random.d.ts";
6
+ export type * as sockets from "./sockets.d.ts";
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Type alias that represents the kind of imports that the average
3
+ * transpiled Jco component will require.
4
+ *
5
+ * For example, `wasi:http/types` and `wasi:http/outgoing-handler` are present due to
6
+ * the ability for a component to call `fetch()` at any point in time.
7
+ *
8
+ * While the feature can be disabled when building with `jco componentize` or `componentize-js`,
9
+ * by default it is enabled, and as such included here (practically, the implementations can be no-ops).
10
+ */
11
+ type WASIImportObject = {
12
+ 'wasi:cli/environment': typeof import('./interfaces/wasi-cli-environment.d.ts');
13
+ 'wasi:cli/exit': typeof import('./interfaces/wasi-cli-exit.d.ts');
14
+ 'wasi:cli/stderr': typeof import('./interfaces/wasi-cli-stderr.d.ts');
15
+ 'wasi:cli/stdin': typeof import('./interfaces/wasi-cli-stdin.d.ts');
16
+ 'wasi:cli/stdout': typeof import('./interfaces/wasi-cli-stdout.d.ts');
17
+ 'wasi:cli/terminal-input': typeof import('./interfaces/wasi-cli-terminal-input.d.ts');
18
+ 'wasi:cli/terminal-output': typeof import('./interfaces/wasi-cli-terminal-output.d.ts');
19
+ 'wasi:cli/terminal-stderr': typeof import('./interfaces/wasi-cli-terminal-stderr.d.ts');
20
+ 'wasi:cli/terminal-stdin': typeof import('./interfaces/wasi-cli-terminal-stdin.d.ts');
21
+ 'wasi:cli/terminal-stdout': typeof import('./interfaces/wasi-cli-terminal-stdout.d.ts');
22
+
23
+ 'wasi:sockets/instance-network': typeof import('./interfaces/wasi-sockets-instance-network.d.ts');
24
+ 'wasi:sockets/ip-name-lookup': typeof import('./interfaces/wasi-sockets-ip-name-lookup.d.ts');
25
+ 'wasi:sockets/network': typeof import('./interfaces/wasi-sockets-network.d.ts');
26
+ 'wasi:sockets/tcp': typeof import('./interfaces/wasi-sockets-tcp.d.ts');
27
+ 'wasi:sockets/tcp-create-socket': typeof import('./interfaces/wasi-sockets-tcp-create-socket.d.ts');
28
+ 'wasi:sockets/udp': typeof import('./interfaces/wasi-sockets-udp.d.ts');
29
+ 'wasi:sockets/udp-create-socket': typeof import('./interfaces/wasi-sockets-udp-create-socket.d.ts');
30
+
31
+ 'wasi:filesystem/preopens': typeof import('./interfaces/wasi-filesystem-preopens.d.ts');
32
+ 'wasi:filesystem/types': typeof import('./interfaces/wasi-filesystem-types.d.ts');
33
+
34
+ 'wasi:io/error': typeof import('./interfaces/wasi-io-error.d.ts');
35
+ 'wasi:io/poll': typeof import('./interfaces/wasi-io-poll.d.ts');
36
+ 'wasi:io/streams': typeof import('./interfaces/wasi-io-streams.d.ts');
37
+
38
+ 'wasi:random/random': typeof import('./interfaces/wasi-random-random.d.ts');
39
+ 'wasi:random/insecure': typeof import('./interfaces/wasi-random-insecure.d.ts');
40
+ 'wasi:random/insecure-seed': typeof import('./interfaces/wasi-random-insecure-seed.d.ts');
41
+
42
+ 'wasi:clocks/monotonic-clock': typeof import('./interfaces/wasi-clocks-monotonic-clock.d.ts');
43
+ 'wasi:clocks/timezone': typeof import('./interfaces/wasi-clocks-timezone.d.ts');
44
+ 'wasi:clocks/wall-clock': typeof import('./interfaces/wasi-clocks-wall-clock.d.ts');
45
+
46
+ 'wasi:http/types': typeof import('./interfaces/wasi-http-types.d.ts');
47
+ 'wasi:http/outgoing-handler': typeof import('./interfaces/wasi-http-outgoing-handler.d.ts');
48
+ };
49
+
50
+ /**
51
+ * (EXPERIMENTAL) A class that holds WASI shims and can be used to configure
52
+ * an instantiation of a WebAssembly component transpiled with jco
53
+ * (i.e. via `jco transpile`).
54
+ *
55
+ * Normally, transpiled components contain mapping for WASI interfaces
56
+ * and/or imports that import the relevant packages (ex. `@bytecodealliance/preview2-shim/clocks`)
57
+ * from the right sources.
58
+ *
59
+ * This function makes use of the `WASIShim` object to provide an object that can be easily
60
+ * fed to the `instantiate` function produced by a transpiled component:
61
+ *
62
+ * ```js
63
+ * import { WASIShim } from "@bytecodealliance/preview2-shim/instantiation"
64
+ * // ...
65
+ * import { instantiate } from "path/to/transpiled/component.js"
66
+ * // ...
67
+ * const component = await instantiate(null, new WASIShim().getImportObject())
68
+ * ```
69
+ *
70
+ * You can also replace imports that you'd like to override with custom implementations,
71
+ * by using the `WASIShim` object directly:
72
+ *
73
+ * ```js
74
+ * import { random } from "@bytecodealliance/preview2-shim"
75
+ * import { WASIShim } from "@bytecodealliance/preview2-shim/instantiation"
76
+ * // ...
77
+ * import { instantiate } from "path/to/transpiled/component.js"
78
+ * // ...
79
+ * const customWASIShim = new WASIShim({
80
+ * random: {
81
+ * // For these two interfaces we re-use the default provided shim
82
+ * random: random.random,
83
+ * insecure-seed: random.insecureSeed,
84
+ * // For insecure, we can supply our own custom implementation
85
+ * insecure: {
86
+ * ...
87
+ * }
88
+ * }
89
+ * });
90
+ *
91
+ * const component = await instantiate(null, customWASIShim.getImportObject())
92
+ * ```
93
+ *
94
+ * Note that this object is similar but not identical to the Node `WASI` object --
95
+ * it is solely concerned with shimming of preview2 when dealing with a WebAssembly
96
+ * component transpiled by Jco. While this object *does* work with Node (and the browser)
97
+ * semantics are not the same as Node's `WASI` object.
98
+ *
99
+ * @class WASIShim
100
+ */
101
+ export class WASIShim {
102
+ constructor(shims?: Partial<WASIImportObject>);
103
+
104
+ /**
105
+ * Generate an import object for the shim that can be used with
106
+ * functions like `instantiate` that are exposed from a transpiled
107
+ * WebAssembly component.
108
+ *
109
+ * @returns {object}
110
+ */
111
+ getImportObject(): WASIImportObject;
112
+ }
@@ -1,22 +1,21 @@
1
- export namespace WasiCliEnvironment {
2
- /**
3
- * Get the POSIX-style environment variables.
4
- *
5
- * Each environment variable is provided as a pair of string variable names
6
- * and string value.
7
- *
8
- * Morally, these are a value import, but until value imports are available
9
- * in the component model, this import function should return the same
10
- * values each time it is called.
11
- */
12
- export function getEnvironment(): Array<[string, string]>;
13
- /**
14
- * Get the POSIX-style arguments to the program.
15
- */
16
- export function getArguments(): Array<string>;
17
- /**
18
- * Return a path that programs should use as their initial current working
19
- * directory, interpreting `.` as shorthand for this.
20
- */
21
- export function initialCwd(): string | undefined;
22
- }
1
+ /** @module Interface wasi:cli/environment@0.2.3 **/
2
+ /**
3
+ * Get the POSIX-style environment variables.
4
+ *
5
+ * Each environment variable is provided as a pair of string variable names
6
+ * and string value.
7
+ *
8
+ * Morally, these are a value import, but until value imports are available
9
+ * in the component model, this import function should return the same
10
+ * values each time it is called.
11
+ */
12
+ export function getEnvironment(): Array<[string, string]>;
13
+ /**
14
+ * Get the POSIX-style arguments to the program.
15
+ */
16
+ export function getArguments(): Array<string>;
17
+ /**
18
+ * Return a path that programs should use as their initial current working
19
+ * directory, interpreting `.` as shorthand for this.
20
+ */
21
+ export function initialCwd(): string | undefined;
@@ -1,7 +1,6 @@
1
- export namespace WasiCliExit {
2
- /**
3
- * Exit the current instance and any linked instances.
4
- */
5
- export function exit(status: Result<void, void>): void;
6
- }
1
+ /** @module Interface wasi:cli/exit@0.2.3 **/
2
+ /**
3
+ * Exit the current instance and any linked instances.
4
+ */
5
+ export function exit(status: Result<void, void>): void;
7
6
  export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };
@@ -1,6 +1,5 @@
1
- export namespace WasiCliRun {
2
- /**
3
- * Run the program.
4
- */
5
- export function run(): void;
6
- }
1
+ /** @module Interface wasi:cli/run@0.2.3 **/
2
+ /**
3
+ * Run the program.
4
+ */
5
+ export function run(): void;
@@ -1,5 +1,3 @@
1
- export namespace WasiCliStderr {
2
- export function getStderr(): OutputStream;
3
- }
4
- import type { OutputStream } from './wasi-io-streams.js';
5
- export { OutputStream };
1
+ /** @module Interface wasi:cli/stderr@0.2.3 **/
2
+ export function getStderr(): OutputStream;
3
+ export type OutputStream = import('./wasi-io-streams.js').OutputStream;
@@ -1,5 +1,3 @@
1
- export namespace WasiCliStdin {
2
- export function getStdin(): InputStream;
3
- }
4
- import type { InputStream } from './wasi-io-streams.js';
5
- export { InputStream };
1
+ /** @module Interface wasi:cli/stdin@0.2.3 **/
2
+ export function getStdin(): InputStream;
3
+ export type InputStream = import('./wasi-io-streams.js').InputStream;
@@ -1,5 +1,3 @@
1
- export namespace WasiCliStdout {
2
- export function getStdout(): OutputStream;
3
- }
4
- import type { OutputStream } from './wasi-io-streams.js';
5
- export { OutputStream };
1
+ /** @module Interface wasi:cli/stdout@0.2.3 **/
2
+ export function getStdout(): OutputStream;
3
+ export type OutputStream = import('./wasi-io-streams.js').OutputStream;
@@ -1,6 +1,8 @@
1
- export namespace WasiCliTerminalInput {
2
- export { TerminalInput };
3
- }
1
+ /** @module Interface wasi:cli/terminal-input@0.2.3 **/
4
2
 
5
3
  export class TerminalInput {
4
+ /**
5
+ * This type does not have a public constructor.
6
+ */
7
+ private constructor();
6
8
  }
@@ -1,6 +1,8 @@
1
- export namespace WasiCliTerminalOutput {
2
- export { TerminalOutput };
3
- }
1
+ /** @module Interface wasi:cli/terminal-output@0.2.3 **/
4
2
 
5
3
  export class TerminalOutput {
4
+ /**
5
+ * This type does not have a public constructor.
6
+ */
7
+ private constructor();
6
8
  }
@@ -1,9 +1,7 @@
1
- export namespace WasiCliTerminalStderr {
2
- /**
3
- * If stderr is connected to a terminal, return a `terminal-output` handle
4
- * allowing further interaction with it.
5
- */
6
- export function getTerminalStderr(): TerminalOutput | undefined;
7
- }
8
- import type { TerminalOutput } from './wasi-cli-terminal-output.js';
9
- export { TerminalOutput };
1
+ /** @module Interface wasi:cli/terminal-stderr@0.2.3 **/
2
+ /**
3
+ * If stderr is connected to a terminal, return a `terminal-output` handle
4
+ * allowing further interaction with it.
5
+ */
6
+ export function getTerminalStderr(): TerminalOutput | undefined;
7
+ export type TerminalOutput = import('./wasi-cli-terminal-output.js').TerminalOutput;
@@ -1,9 +1,7 @@
1
- export namespace WasiCliTerminalStdin {
2
- /**
3
- * If stdin is connected to a terminal, return a `terminal-input` handle
4
- * allowing further interaction with it.
5
- */
6
- export function getTerminalStdin(): TerminalInput | undefined;
7
- }
8
- import type { TerminalInput } from './wasi-cli-terminal-input.js';
9
- export { TerminalInput };
1
+ /** @module Interface wasi:cli/terminal-stdin@0.2.3 **/
2
+ /**
3
+ * If stdin is connected to a terminal, return a `terminal-input` handle
4
+ * allowing further interaction with it.
5
+ */
6
+ export function getTerminalStdin(): TerminalInput | undefined;
7
+ export type TerminalInput = import('./wasi-cli-terminal-input.js').TerminalInput;
@@ -1,9 +1,7 @@
1
- export namespace WasiCliTerminalStdout {
2
- /**
3
- * If stdout is connected to a terminal, return a `terminal-output` handle
4
- * allowing further interaction with it.
5
- */
6
- export function getTerminalStdout(): TerminalOutput | undefined;
7
- }
8
- import type { TerminalOutput } from './wasi-cli-terminal-output.js';
9
- export { TerminalOutput };
1
+ /** @module Interface wasi:cli/terminal-stdout@0.2.3 **/
2
+ /**
3
+ * If stdout is connected to a terminal, return a `terminal-output` handle
4
+ * allowing further interaction with it.
5
+ */
6
+ export function getTerminalStdout(): TerminalOutput | undefined;
7
+ export type TerminalOutput = import('./wasi-cli-terminal-output.js').TerminalOutput;