117b5be67900df7c282bcd8eb144906083476aee1bfafa4f412f2b65b2b6d9e1d4b9ea4cf5b4c8362fbf1fd0ab0569ccf10896e7f4d90a928c588de613a71b 7.7 KB

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