UNPKG

7.42 kBTypeScriptView Raw
1/**
2 * The WASI API provides an implementation of the [WebAssembly System Interface](https://wasi.dev/)specification. WASI gives sandboxed WebAssembly applications access to the
3 * underlying operating system via a collection of POSIX-like functions.
4 *
5 * ```js
6 * import fs from 'fs';
7 * import { WASI } from 'wasi';
8 *
9 * const wasi = new WASI({
10 * args: process.argv,
11 * env: process.env,
12 * preopens: {
13 * '/sandbox': '/some/real/path/that/wasm/can/access'
14 * }
15 * });
16 * const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
17 *
18 * const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
19 * const instance = await WebAssembly.instantiate(wasm, importObject);
20 *
21 * wasi.start(instance);
22 * ```
23 *
24 * ```js
25 * 'use strict';
26 * const fs = require('fs');
27 * const { WASI } = require('wasi');
28 * const wasi = new WASI({
29 * args: process.argv,
30 * env: process.env,
31 * preopens: {
32 * '/sandbox': '/some/real/path/that/wasm/can/access'
33 * }
34 * });
35 * const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
36 *
37 * (async () => {
38 * const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
39 * const instance = await WebAssembly.instantiate(wasm, importObject);
40 *
41 * wasi.start(instance);
42 * })();
43 * ```
44 *
45 * To run the above example, create a new WebAssembly text format file named`demo.wat`:
46 *
47 * ```text
48 * (module
49 * ;; Import the required fd_write WASI function which will write the given io vectors to stdout
50 * ;; The function signature for fd_write is:
51 * ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
52 * (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
53 *
54 * (memory 1)
55 * (export "memory" (memory 0))
56 *
57 * ;; Write 'hello world\n' to memory at an offset of 8 bytes
58 * ;; Note the trailing newline which is required for the text to appear
59 * (data (i32.const 8) "hello world\n")
60 *
61 * (func $main (export "_start")
62 * ;; Creating a new io vector within linear memory
63 * (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
64 * (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
65 *
66 * (call $fd_write
67 * (i32.const 1) ;; file_descriptor - 1 for stdout
68 * (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
69 * (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
70 * (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
71 * )
72 * drop ;; Discard the number of bytes written from the top of the stack
73 * )
74 * )
75 * ```
76 *
77 * Use [wabt](https://github.com/WebAssembly/wabt) to compile `.wat` to `.wasm`
78 *
79 * ```console
80 * $ wat2wasm demo.wat
81 * ```
82 *
83 * The `--experimental-wasi-unstable-preview1` CLI argument is needed for this
84 * example to run.
85 * @experimental
86 * @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/wasi.js)
87 */
88declare module 'wasi' {
89 interface WASIOptions {
90 /**
91 * An array of strings that the WebAssembly application will
92 * see as command line arguments. The first argument is the virtual path to the
93 * WASI command itself.
94 */
95 args?: string[] | undefined;
96 /**
97 * An object similar to `process.env` that the WebAssembly
98 * application will see as its environment.
99 */
100 env?: object | undefined;
101 /**
102 * This object represents the WebAssembly application's
103 * sandbox directory structure. The string keys of `preopens` are treated as
104 * directories within the sandbox. The corresponding values in `preopens` are
105 * the real paths to those directories on the host machine.
106 */
107 preopens?: NodeJS.Dict<string> | undefined;
108 /**
109 * By default, WASI applications terminate the Node.js
110 * process via the `__wasi_proc_exit()` function. Setting this option to `true`
111 * causes `wasi.start()` to return the exit code rather than terminate the
112 * process.
113 * @default false
114 */
115 returnOnExit?: boolean | undefined;
116 /**
117 * The file descriptor used as standard input in the WebAssembly application.
118 * @default 0
119 */
120 stdin?: number | undefined;
121 /**
122 * The file descriptor used as standard output in the WebAssembly application.
123 * @default 1
124 */
125 stdout?: number | undefined;
126 /**
127 * The file descriptor used as standard error in the WebAssembly application.
128 * @default 2
129 */
130 stderr?: number | undefined;
131 }
132 /**
133 * The `WASI` class provides the WASI system call API and additional convenience
134 * methods for working with WASI-based applications. Each `WASI` instance
135 * represents a distinct sandbox environment. For security purposes, each `WASI`instance must have its command-line arguments, environment variables, and
136 * sandbox directory structure configured explicitly.
137 * @since v13.3.0, v12.16.0
138 */
139 class WASI {
140 constructor(options?: WASIOptions);
141 /**
142 * Attempt to begin execution of `instance` as a WASI command by invoking its`_start()` export. If `instance` does not contain a `_start()` export, or if`instance` contains an `_initialize()`
143 * export, then an exception is thrown.
144 *
145 * `start()` requires that `instance` exports a [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory) named`memory`. If
146 * `instance` does not have a `memory` export an exception is thrown.
147 *
148 * If `start()` is called more than once, an exception is thrown.
149 * @since v13.3.0, v12.16.0
150 */
151 start(instance: object): void; // TODO: avoid DOM dependency until WASM moved to own lib.
152 /**
153 * Attempt to initialize `instance` as a WASI reactor by invoking its`_initialize()` export, if it is present. If `instance` contains a `_start()`export, then an exception is thrown.
154 *
155 * `initialize()` requires that `instance` exports a [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory) named`memory`.
156 * If `instance` does not have a `memory` export an exception is thrown.
157 *
158 * If `initialize()` is called more than once, an exception is thrown.
159 * @since v14.6.0, v12.19.0
160 */
161 initialize(instance: object): void; // TODO: avoid DOM dependency until WASM moved to own lib.
162 /**
163 * `wasiImport` is an object that implements the WASI system call API. This object
164 * should be passed as the `wasi_snapshot_preview1` import during the instantiation
165 * of a [`WebAssembly.Instance`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance).
166 * @since v13.3.0, v12.16.0
167 */
168 readonly wasiImport: NodeJS.Dict<any>; // TODO: Narrow to DOM types
169 }
170}
171declare module 'node:wasi' {
172 export * from 'wasi';
173}
174
\No newline at end of file