From 55d40a6c2a7b054df9bae45d231a2b8ef73d1eed Mon Sep 17 00:00:00 2001 From: Maxim Schuwalow Date: Wed, 29 Jan 2025 23:54:54 +0100 Subject: [PATCH] javascript example working --- examples/js/js-default-wasi-http/.gitignore | 3 ++ .../js/js-default-wasi-http/metadata.json | 9 ++++ examples/js/js-default-wasi-http/package.json | 15 ++++++ .../js/js-default-wasi-http/rollup.config.mjs | 11 +++++ examples/js/js-default-wasi-http/src/main.js | 47 +++++++++++++++++++ examples/js/js-default-wasi-http/wit/main.wit | 8 ++++ 6 files changed, 93 insertions(+) create mode 100644 examples/js/js-default-wasi-http/.gitignore create mode 100644 examples/js/js-default-wasi-http/metadata.json create mode 100644 examples/js/js-default-wasi-http/package.json create mode 100644 examples/js/js-default-wasi-http/rollup.config.mjs create mode 100644 examples/js/js-default-wasi-http/src/main.js create mode 100644 examples/js/js-default-wasi-http/wit/main.wit diff --git a/examples/js/js-default-wasi-http/.gitignore b/examples/js/js-default-wasi-http/.gitignore new file mode 100644 index 0000000..4bded1c --- /dev/null +++ b/examples/js/js-default-wasi-http/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +out/ +src/generated/ diff --git a/examples/js/js-default-wasi-http/metadata.json b/examples/js/js-default-wasi-http/metadata.json new file mode 100644 index 0000000..8469293 --- /dev/null +++ b/examples/js/js-default-wasi-http/metadata.json @@ -0,0 +1,9 @@ +{ + "description": "A simple stateful Golem worker implemented in JavaScript with no dependencies on external services", + "requiresAdapter": false, + "requiresWASI": true, + "exclude": [ + "node_modules", + "out" + ] +} diff --git a/examples/js/js-default-wasi-http/package.json b/examples/js/js-default-wasi-http/package.json new file mode 100644 index 0000000..d0315f2 --- /dev/null +++ b/examples/js/js-default-wasi-http/package.json @@ -0,0 +1,15 @@ +{ + "scripts": { + "build": "rollup --config", + "componentize": "npm run build && jco componentize -w wit -o out/component_name.wasm out/main.js", + "clean": "rm -rf out src/generated", + "serve": "jco serve out/component_name.wasm" + }, + "devDependencies": { + "@golemcloud/componentize-js": "0.10.5-golem.3", + "@golemcloud/golem-ts": "1.1.1", + "@golemcloud/jco": "1.4.4-golem.1", + "@rollup/plugin-node-resolve": "^15.2.3", + "rollup": "^4.18.0" + } +} diff --git a/examples/js/js-default-wasi-http/rollup.config.mjs b/examples/js/js-default-wasi-http/rollup.config.mjs new file mode 100644 index 0000000..7b7efc1 --- /dev/null +++ b/examples/js/js-default-wasi-http/rollup.config.mjs @@ -0,0 +1,11 @@ +import resolve from "@rollup/plugin-node-resolve"; + +export default { + input: "src/main.js", + output: { + file: "out/main.js", + format: "esm", + }, + external: ["wasi:http/types@0.2.0"], + plugins: [resolve()], +}; diff --git a/examples/js/js-default-wasi-http/src/main.js b/examples/js/js-default-wasi-http/src/main.js new file mode 100644 index 0000000..4cecb96 --- /dev/null +++ b/examples/js/js-default-wasi-http/src/main.js @@ -0,0 +1,47 @@ +import { + ResponseOutparam, + OutgoingBody, + OutgoingResponse, + Fields, + } from 'wasi:http/types@0.2.0'; + +// For a detailed guide see https://github.com/bytecodealliance/jco/tree/main/examples/components/http-hello-world + +/** + * This export represents the `wasi:http/incoming-handler` interface, + * which describes implementing a HTTP handler in WebAssembly using WASI types. + */ +export const incomingHandler = { + /** + * This Javascript will be turned into a WebAssembly component by `jco` and turned into a + * WebAssembly binary with a single export (this `handler` function). + * + * The exported `handle` method is part of the `wasi:http/incoming-handler` interface, + * which defines how to hadle incoming web requests, turning this component into one that can + * serve web requests. + */ + handle: function (incomingRequest, responseOutparam) { + // Start building an outgoing response + const outgoingResponse = new OutgoingResponse(new Fields()); + + // Access the outgoing response body + let outgoingBody = outgoingResponse.body(); + { + // Create a stream for the response body + let outputStream = outgoingBody.write(); + // Write hello world to the response stream + outputStream.blockingWriteAndFlush( + new Uint8Array(new TextEncoder().encode('Hello from Javascript!\n')) + ); + // @ts-ignore: This is required in order to dispose the stream before we return + outputStream[Symbol.dispose](); + } + + // Set the status code for the response + outgoingResponse.setStatusCode(200); + // Finish the response body + OutgoingBody.finish(outgoingBody, undefined); + // Set the created response to an "OK" Result value + ResponseOutparam.set(outgoingResponse, { tag: 'ok', val: outgoingResponse }); + }, +} diff --git a/examples/js/js-default-wasi-http/wit/main.wit b/examples/js/js-default-wasi-http/wit/main.wit new file mode 100644 index 0000000..f3c4c9c --- /dev/null +++ b/examples/js/js-default-wasi-http/wit/main.wit @@ -0,0 +1,8 @@ +package pack:name; + +// See https://component-model.bytecodealliance.org/design/wit.html for more details about the WIT syntax +// See https://github.com/WebAssembly/wasi-http for an introduction to wasi-http + +world component-name { + export wasi:http/incoming-handler@0.2.0; +}