Skip to content
This repository has been archived by the owner on Feb 26, 2025. It is now read-only.

Commit

Permalink
javascript example working
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Jan 29, 2025
1 parent f0ca64b commit 55d40a6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/js/js-default-wasi-http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
out/
src/generated/
9 changes: 9 additions & 0 deletions examples/js/js-default-wasi-http/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
15 changes: 15 additions & 0 deletions examples/js/js-default-wasi-http/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
11 changes: 11 additions & 0 deletions examples/js/js-default-wasi-http/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -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/[email protected]"],
plugins: [resolve()],
};
47 changes: 47 additions & 0 deletions examples/js/js-default-wasi-http/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
ResponseOutparam,
OutgoingBody,
OutgoingResponse,
Fields,
} from 'wasi:http/[email protected]';

// 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<T> value
ResponseOutparam.set(outgoingResponse, { tag: 'ok', val: outgoingResponse });
},
}
8 changes: 8 additions & 0 deletions examples/js/js-default-wasi-http/wit/main.wit
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 55d40a6

Please sign in to comment.