Skip to content

Commit a7bf26b

Browse files
authored
feat: provide the right URL to workers (#234)
* fix: provides the right URL to workers * feat: add the HonoJS example to the project
1 parent 2b72b05 commit a7bf26b

File tree

9 files changed

+1956
-8
lines changed

9 files changed

+1956
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ examples/*.toml
55
.DS_Store
66
.wws
77
**/zig-cache
8-
**/zig-out
8+
**/zig-out
9+
node_modules

crates/worker/src/io.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::errors::{self, Result, WorkerError};
55

66
use actix_web::{
7-
http::{header::HeaderMap, StatusCode},
7+
http::{header::HeaderMap, StatusCode, Uri},
88
HttpRequest,
99
};
1010
use base64::{engine::general_purpose, Engine as _};
@@ -16,7 +16,7 @@ use std::collections::HashMap;
1616
#[derive(Serialize, Deserialize)]
1717
pub struct WasmInput<'a> {
1818
/// Request full URL
19-
url: &'a str,
19+
url: String,
2020
/// Request method
2121
method: &'a str,
2222
/// Request headers
@@ -43,10 +43,7 @@ impl<'a> WasmInput<'a> {
4343
params.insert(k.to_string(), v.to_string());
4444
}
4545

46-
let url = match request.uri().path_and_query() {
47-
Some(path) => path.as_str(),
48-
None => request.uri().path(),
49-
};
46+
let url = Self::build_url(request);
5047

5148
Self {
5249
url,
@@ -58,6 +55,21 @@ impl<'a> WasmInput<'a> {
5855
}
5956
}
6057

58+
/// Prepare the URL from the given actix HTTP request. It will try to
59+
/// load the full URL including the authority and the schema. This is
60+
/// required by different frameworks.
61+
fn build_url(request: &HttpRequest) -> String {
62+
match Uri::builder()
63+
.scheme(request.connection_info().scheme())
64+
.authority(request.connection_info().host())
65+
.path_and_query(request.uri().to_string())
66+
.build()
67+
{
68+
Ok(uri) => uri.to_string(),
69+
Err(_) => request.uri().to_string(),
70+
}
71+
}
72+
6173
/// Create HashMap from a HeadersMap
6274
fn build_headers_hash(headers: &HeaderMap) -> HashMap<String, String> {
6375
let mut parsed_headers = HashMap::new();

crates/worker/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ impl Worker {
236236
Ok(())
237237
}
238238

239+
/// Run the worker that will process the given URL. This method sets the
240+
/// module context including all the required features like WASI and WASI-NN.
241+
/// Then, it loads the module, run it and process the output.
239242
pub async fn run(
240243
&self,
241244
request: &HttpRequest,

examples/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ COMPONENTS = components/rust-basic components/rust-kv
33

44
all: $(SUBDIRS) rust-params $(COMPONENTS) components/rust-params
55

6-
.PHONY: $(SUBDIRS) rust-params $(COMPONENTS) components/rust-params
6+
.PHONY: $(SUBDIRS) rust-params $(COMPONENTS) components/rust-params js-hono
77

88
$(SUBDIRS):
99
cd $@ && \
@@ -15,6 +15,10 @@ rust-params:
1515
cargo build --target wasm32-wasi --release && \
1616
cp target/wasm32-wasi/release/$@.wasm "./[id].wasm"
1717

18+
js-hono:
19+
cd $@/src && \
20+
npm install && npm run build
21+
1822
$(COMPONENTS):
1923
mkdir -p $@
2024
make $(@:components/%=%)

examples/js-hono/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# JavaScript Hono example
2+
3+
Run a JavaScript application that uses the [HonoJS framework](https://hono.dev/) in Wasm Workers Server.
4+
5+
## Prerequisites
6+
7+
* Wasm Workers Server (wws):
8+
9+
```shell-session
10+
curl -fsSL https://workers.wasmlabs.dev/install | bash
11+
```
12+
13+
## Run
14+
15+
```shell-session
16+
wws https://github.com/vmware-labs/wasm-workers-server.git -i --git-folder "examples/js-hono/dist"
17+
```
18+
19+
## Resources
20+
21+
* [JavaScript documentation](https://workers.wasmlabs.dev/docs/languages/javascript)

0 commit comments

Comments
 (0)