Skip to content

Commit

Permalink
Merge pull request #58 from fermyon/fix-examples
Browse files Browse the repository at this point in the history
Fix examples
  • Loading branch information
rylev authored Sep 9, 2024
2 parents 882a18d + 2865580 commit 8383179
Show file tree
Hide file tree
Showing 63 changed files with 2,305 additions and 1,566 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ authors = ["Fermyon Engineering <[email protected]>"]
[lib]
crate-type = ["cdylib"]

[package.metadata.component]
package = "component:spin-fileserver-example"
adapter = "adapters/fd1e948d/wasi_snapshot_preview1.reactor.wasm"

[dependencies]
cargo-component-bindings = "0.4.0"
# Helpful crate to manage errors.
Expand Down
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
http.wasm
composed.wasm
16 changes: 3 additions & 13 deletions examples/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,18 @@ component for serving static files.
- [Spin v2.0+](https://developer.fermyon.com/spin/install)
- [Rust](https://rustup.rs/)
- [cargo-component](https://github.com/bytecodealliance/cargo-component)
- [wasm-tools](https://github.com/bytecodealliance/wasm-tools/)
- Note that you'll need [this fork](https://github.com/dicej/wasm-tools/tree/wasm-compose-resource-imports) until [this PR](https://github.com/bytecodealliance/wasm-tools/pull/1261) has been merged and released.
- [wac](https://github.com/bytecodealliance/wac)
- [NodeJS](https://nodejs.org/en/download)
- [componentize-js](https://github.com/dicej/componentize-js)
- [curl](https://curl.se/download.html) or a web browser for testing

Once you have Rust and NodeJS installed, the following should give you everything else:

*NOTE*: Until https://github.com/bytecodealliance/componentize-js/pull/69 has
been merged, you'll need to build and install `componentize-js` from source
using
https://github.com/dicej/componentize-js/tree/imported-resource-destructors
instead of the `npm install` command below. See the README.md in that
repository for instructions.

```shell
rustup target add wasm32-wasi
cargo install cargo-component
cargo install --locked --git https://github.com/dicej/wasm-tools \
--branch wasm-compose-resource-imports wasm-tools
# See NOTE above for installing `componentize-js`
# npm install @bytecodealliance/componentize-js
cargo install wac-cli
npm install -g @bytecodealliance/jco @bytecodealliance/componentize-js
```

## Building and Running
Expand Down
18 changes: 9 additions & 9 deletions examples/javascript/app.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { handle as spinFileserverHandle } from "wasi:http/[email protected]-rc-2023-10-18"
import { OutgoingResponse, ResponseOutparam, OutgoingBody, Fields } from "wasi:http/[email protected]-rc-2023-10-18"
import { handle as spinFileserverHandle } from "wasi:http/[email protected]"
import { OutgoingResponse, ResponseOutparam, OutgoingBody, Fields } from "wasi:http/[email protected]"

const encoder = new TextEncoder()
const disposeSymbol = Symbol.dispose || Symbol.for('dispose')
Expand All @@ -10,14 +10,13 @@ function handle(request, responseOut) {

if (method.tag === "get") {
if (path === "/hello") {
const response = new OutgoingResponse(
200,
new Fields([["content-type", encoder.encode("text/plain")]])
)

const fields = new Fields()
fields.append("content-type", encoder.encode("text/plain"))
const response = new OutgoingResponse(fields)

const responseBody = response.write()
ResponseOutparam.set(responseOut, { tag: "ok", val: response })

const responseStream = responseBody.write()
responseStream.blockingWriteAndFlush(encoder.encode("Hello, world!"))
responseStream[disposeSymbol]()
Expand All @@ -26,7 +25,8 @@ function handle(request, responseOut) {
spinFileserverHandle(request, responseOut)
}
} else {
const response = new OutgoingResponse(400, new Fields([]))
const response = new OutgoingResponse(new Fields([]))
response.setSatusCode(400)
ResponseOutparam.set(responseOut, { tag: "ok", val: response })
OutgoingBody.finish(response.write())
}
Expand Down
3 changes: 1 addition & 2 deletions examples/javascript/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { componentize } from "@bytecodealliance/componentize-js"
import { readFile, writeFile } from "node:fs/promises"

const { component } = await componentize(
await readFile("app.mjs"),
await readFile("app.mjs", "utf-8"),
{
witPath: "../wit",
worldName: "proxy",
preview2Adapter: "../../adapters/fd1e948d/wasi_snapshot_preview1.reactor.wasm",
enableStdout: true,
}
);
Expand Down
7 changes: 0 additions & 7 deletions examples/javascript/compose.yml

This file was deleted.

Loading

0 comments on commit 8383179

Please sign in to comment.