Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide the right URL to workers #234

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ examples/*.toml
.DS_Store
.wws
**/zig-cache
**/zig-out
**/zig-out
node_modules
24 changes: 18 additions & 6 deletions crates/worker/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::errors::{self, Result, WorkerError};

use actix_web::{
http::{header::HeaderMap, StatusCode},
http::{header::HeaderMap, StatusCode, Uri},
HttpRequest,
};
use base64::{engine::general_purpose, Engine as _};
Expand All @@ -16,7 +16,7 @@ use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
pub struct WasmInput<'a> {
/// Request full URL
url: &'a str,
url: String,
/// Request method
method: &'a str,
/// Request headers
Expand All @@ -43,10 +43,7 @@ impl<'a> WasmInput<'a> {
params.insert(k.to_string(), v.to_string());
}

let url = match request.uri().path_and_query() {
Some(path) => path.as_str(),
None => request.uri().path(),
};
let url = Self::build_url(request);

Self {
url,
Expand All @@ -58,6 +55,21 @@ impl<'a> WasmInput<'a> {
}
}

/// Prepare the URL from the given actix HTTP request. It will try to
/// load the full URL including the authority and the schema. This is
/// required by different frameworks.
fn build_url(request: &HttpRequest) -> String {
match Uri::builder()
.scheme(request.connection_info().scheme())
.authority(request.connection_info().host())
.path_and_query(request.uri().to_string())
.build()
{
Ok(uri) => uri.to_string(),
Err(_) => request.uri().to_string(),
}
}

/// Create HashMap from a HeadersMap
fn build_headers_hash(headers: &HeaderMap) -> HashMap<String, String> {
let mut parsed_headers = HashMap::new();
Expand Down
3 changes: 3 additions & 0 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ impl Worker {
Ok(())
}

/// Run the worker that will process the given URL. This method sets the
/// module context including all the required features like WASI and WASI-NN.
/// Then, it loads the module, run it and process the output.
pub async fn run(
&self,
request: &HttpRequest,
Expand Down
6 changes: 5 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COMPONENTS = components/rust-basic components/rust-kv

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

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

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

js-hono:
cd $@/src && \
npm install && npm run build

$(COMPONENTS):
mkdir -p $@
make $(@:components/%=%)
Expand Down
21 changes: 21 additions & 0 deletions examples/js-hono/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# JavaScript Hono example

Run a JavaScript application that uses the [HonoJS framework](https://hono.dev/) in Wasm Workers Server.

## Prerequisites

* Wasm Workers Server (wws):

```shell-session
curl -fsSL https://workers.wasmlabs.dev/install | bash
```

## Run

```shell-session
wws https://github.com/vmware-labs/wasm-workers-server.git -i --git-folder "examples/js-hono/dist"
```

## Resources

* [JavaScript documentation](https://workers.wasmlabs.dev/docs/languages/javascript)