This repository has been archived by the owner on Feb 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0ca64b
commit 55d40a6
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
out/ | ||
src/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |