Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples #58

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <engineering@fermyon.com>"]
[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/incoming-handler@0.2.0-rc-2023-10-18"
import { OutgoingResponse, ResponseOutparam, OutgoingBody, Fields } from "wasi:http/types@0.2.0-rc-2023-10-18"
import { handle as spinFileserverHandle } from "wasi:http/incoming-handler@0.2.0"
import { OutgoingResponse, ResponseOutparam, OutgoingBody, Fields } from "wasi:http/types@0.2.0"

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
Loading