@wasmer/wasm-terminal
v0.12.0
Published
A terminal-like component for the browser, that fetches and runs Wasm modules in the context of a shell. π
Downloads
53
Readme
@wasmer/wasm-terminal
A terminal-like component for the browser, that fetches and runs Wasm modules in the context of a shell. π
Documentation for Wasmer-JS Stack can be found on the Wasmer Docs.
Table of Contents
Features
This project is built using Xterm.js, and Comlink π
Runs WASI Wasm modules using @wasmer/wasi and @wasmer/wasmfs. π
Provides a terminal-like experience, with stuff like autocomplete, hotkeys, pipes, and more! π©βπ»
Allows passing in your own custom Wasm binaries, or callback commands to provide commands in the shell! βοΈ
Runs processes in seperate web workers using Comlink! π
Exports a command fetcher for fetching packages from WAPM! π¦
Installation
For installing @wasmer/wasm-terminal
, just run this command in your shell:
npm install --save @wasmer/wasm-terminal
Quick Start
First, We must also include the [xterm](https://github.com/xtermjs/xterm.js/).css
. For example:
<!-- This includes the external stylesheet. NOTE: The path should point to wherever you are hosting the wasm-terminal output. -->
<link
rel="stylesheet"
type="text/css"
href="./node_modules/@wasmer/wasm-terminal/lib/xterm/xterm.css"
/>
import WasmTerminal, { fetchCommandFromWAPM } from "@wasmer/wasm-terminal";
import { lowerI64Imports } from "@wasmer/wasm-transformer";
// Let's write handler for the fetchCommand property of the WasmTerminal Config.
const fetchCommandHandler = async ({args}) => {
let commandName = args[0];
// Let's return a "CallbackCommand" if our command matches a special name
if (commandName === "callback-command") {
const callbackCommand = async (options, wasmFs) => {
return `Callback Command Working! Options: ${options}, fs: ${wasmFs}`;
};
return callbackCommand;
}
// Let's fetch a wasm Binary from WAPM for the command name.
const wasmBinary = await fetchCommandFromWAPM({args});
// lower i64 imports from Wasi Modules, so that most Wasi modules
// Can run in a Javascript context.
return await lowerI64Imports(wasmBinary);
};
// Let's create our Wasm Terminal
const wasmTerminal = new WasmTerminal({
// Function that is run whenever a command is fetched
fetchCommand: fetchCommandHandler
});
// Let's print out our initial message
wasmTerminal.print("Hello World!");
// Let's bind our Wasm terminal to it's container
const containerElement = document.querySelector("#root");
wasmTerminal.open(containerElement);
wasmTerminal.fit();
wasmTerminal.focus();
// Later, when we are done with the terminal, let's destroy it
// wasmTerminal.destroy();
NOTE: Remember to include the CSS file mentioned at the beginning of the "Quick Start" section.
Reference API
The Reference API Documentation can be found on the @wasmer/wasm-terminal
Reference API Wasmer Docs.
Browser Compatibility
For more simple Wasm modules, E.g cowsay, the Wasm terminal will work on the latest version of all major browsers. However, more complex Wasm modules may only work on browsers that support SharedArrayBuffer. Which was previously implemented in all major browsers, but was removed due to the Meltdown and Spectre attacks. Though, some major browsers have already started to re-enable this feature. The following cases that may be problemsome are:
Wasm modules that infinitely loop like wasm-matrix. They will block the main thread and freeze the browser.
Wasm modules that take in input from
/dev/stdin
such as lolcat. They will not take input from the shell. but instead, usewindow.prompt
to get input. Sincewindow.prompt
can pause the execution of javascript on the main thread for synchronous reads.
Contributing
The project is split up around the relationship between a terminal, tty, and shell.
This project follows the all-contributors specification.
Contributions of any kind are welcome! π