Skip to content

Commit

Permalink
feat: allow running JavaScript workers with async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelmmiguel committed May 29, 2023
1 parent a948002 commit ec2fc6f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
52 changes: 52 additions & 0 deletions examples/js-async/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Using an async function to reply!
async function handle(request) {
if (request.method != "GET") {
// Don't allow other methods.
// Here you can see how to return a custom status
return new Response("Method not allowed", {
status: 405
});
}

// Body response
const body = `<!DOCTYPE html>
<head>
<title>Wasm Workers Server</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<style>
body { max-width: 1000px; }
main { margin: 5rem 0; }
h1, p { text-align: center; }
h1 { margin-bottom: 2rem; }
pre { font-size: .9rem; }
pre > code { padding: 2rem; }
p { margin-top: 2rem; }
</style>
</head>
<body>
<main>
<h1>Hello from Wasm Workers Server 👋</h1>
<pre><code>Replying to ${request.url}
Method: ${request.method}
User Agent: ${request.headers.get("user-agent")}
Payload: ${request.body || "-"}</code></pre>
<p>
This page was generated by a JavaScript (async worker) file running in WebAssembly.
</p>
</main>
</body>`;

// Build a new response
let response = new Response(body);

// Add a new header
response.headers.set("x-generated-by", "wasm-workers-server");

return response;
}

addEventListener('fetch', event => {
event.respondWith(handle(event.request));
});
26 changes: 19 additions & 7 deletions kits/javascript/src/glue.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,25 @@ const requestToHandler = input => {

handlerFunction(event);

return {
data: event.response.body,
headers: event.response.headers.headers,
status: event.response.status,
kv: Cache.state
};
// Always convert event.response to a Promise
Promise.resolve(
event.response
).then(res => {
// Set the result in the global value
result = {
data: res.body,
headers: res.headers.headers,
status: res.status,
kv: Cache.state
};
})
.catch((err) => {
throw new Error(`Error getting the response in the worker: ${err}`);
});
};

// This is the entrypoint for the project.
entrypoint = requestToHandler;
entrypoint = requestToHandler;

// Set the result
result = {};
13 changes: 11 additions & 2 deletions kits/javascript/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ fn main() {
let input_value = json::transcode_input(context, input_bytes).unwrap();

// Run the handler to get the output
let output_value = match entrypoint.call(&global, &[input_value]) {
Ok(result) => result,
match entrypoint.call(&global, &[input_value]) {
Ok(_) => {}
Err(err) => panic!("{}", err.to_string()),
};

if context.is_pending() {
if let Err(err) = context.execute_pending() {
panic!("{}", err.to_string());
}
}

let global = context.global_object().unwrap();
let output_value = global.get_property("result").unwrap();

let output = json::transcode_output(output_value).unwrap();

stdout()
Expand Down
Binary file modified kits/javascript/wasm-workers-quick-js-engine.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ mod test {
"http://localhost:8080",
"This page was generated by a JavaScript file",
),
(
"js-async",
global_timeout.unwrap_or(5),
"http://localhost:8080",
"This page was generated by a JavaScript (async worker) file",
),
(
"js-json",
global_timeout.unwrap_or(5),
Expand Down

0 comments on commit ec2fc6f

Please sign in to comment.