Skip to content

Latest commit

 

History

History
89 lines (66 loc) · 3.89 KB

File metadata and controls

89 lines (66 loc) · 3.89 KB

Sample: wasi:http in JavaScript

Open in GitHub Codespaces

An example project showing how to build a spec-compliant wasi:http/proxy server for WASI 0.2 written in JavaScript.

Routes

The following HTTP routes are available from the component:

/               # Hello world
/sleep          # Sleep for {ms} milliseconds
/echo           # Echo the HTTP body
/echo-headers   # Echo the HTTP headers
/upload         # Echo uploaded blob

Testing routes:

# Hello world
$ curl localhost:8080
# Sleep for {ms} milliseconds
$ curl localhost:8080/sleep/2000
# Echo the HTTP body
$ curl -d "Test echo body" localhost:8080/echo
# Echo the HTTP headers
$ curl -H "X-Test-Header: 123" localhost:8080/echo-headers
# Echo uploaded blob
$ echo "Hello World!" > test_file.txt
$ curl -H "Content-Type: text/plain" --data-binary @test_file.txt http://localhost:8080/upload

Quick Start

The project uses Wasmtime as its runtime. However, if needed, it can easily be adjusted to use jco instead. For wasmtime installation, simply run:

$ curl https://wasmtime.dev/install.sh -sSf | bash

The quickest way to start is by using just.

$ just serve # to build and serve the wasm component on `localhost:8080`
$ curl localhost:8080 # to send requests to component.

Alternatively, run:

$ npm install
$ npm run build
$ wasmtime serve -S common dist/sample-wasi-http-js.wasm

Debugging in VSCode

You can debug by installing the StarlingMonkey debugger extension for Visual Studio Code. Componentize-js is the compile tool that bundles the JavaScript application into a single .js file according to the rollup.config.js file, creates a WebAssembly module with the SpiderMonkey JavaScript engine, and then creates a component with both modules inside. For details, see the build and build:debug scripts in the package.json file in the root folder.

NOTE: At this time, the debug extension requires both a StarlingMonkey component with debugging enabled, using componentize-js and the --enable-script-debugging runtime argument flag. In addition, to attach the debugger to the SpiderMonkey debugging API inside the StarlingMonkey WebAssembly component, the following additional interfaces need to be built into the debug version of the component:

import wasi:cli/[email protected];
import wasi:sockets/[email protected];
import wasi:sockets/[email protected];
import wasi:sockets/[email protected];
import wasi:sockets/[email protected];

At the moment, the extension does not add these interfaces for you; to add these interfaces, a replica of the WIT directory with a modified server.wit file is included in the wit-debug directory. When you start the StarlingMonkey debugger by selecting the Run menu and then Start Debugging (or press F5), the tasks.json file executes npm run build:debug, which compiles the modified WIT file to build the debug version of the component automatically.

Following a successful build, the StarlingMonkey debugger will launch wasmtime to serve the debug version of the component. Set a breakpoint on the JavaScript in the bundled version of your code, in dist/bundle.js, as that is the file that StarlingMonkey will be executing inside the component.

See Also

License

Apache-2.0 with LLVM Exception