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: refactor runner code into multiple runtimes to simplify the addition of new ones #62

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target
!tests/**/*.wasm
examples/*.toml
.DS_Store
.wws
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"
wasmtime = "4.0.0"
wasmtime-wasi = "4.0.0"
anyhow = "1.0.66"
blake3 = "1.3.3"
wasi-common = "4.0.0"
actix-web = "4"
actix-files = "0.6.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/js-basic/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const reply = (request) => {
<h1>Hello from Wasm Workers Server 👋</h1>
<pre><code>Replying to ${request.url}
Method: ${request.method}
User Agent: ${request.headers.get("userAgent")}
User Agent: ${request.headers.get("user-agent")}
Payload: ${request.body || "-"}</code></pre>
<p>
This page was generated by a JavaScript file running in WebAssembly.
Expand Down
29 changes: 11 additions & 18 deletions kits/javascript/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,37 @@

use quickjs_wasm_rs::{json, Context};
use std::{
env,
io::{self, stdin, stdout, Read, Write},
env, fs,
io::{stdin, stdout, Read, Write},
};

// JS polyfill
static POLYFILL: &str = include_str!("./glue.js");

// Separator between source code and request data
static SEPARATOR: &str = "[[[input]]]";

fn main() {
let mut context = Context::default();
context
.register_globals(io::stderr(), io::stderr())
.unwrap();
let context = Context::default();

let source = fs::read_to_string("/src/index.js");
let mut contents = String::new();
let mut source = String::new();
contents.push_str(&POLYFILL);
let mut request = String::new();
contents.push_str(POLYFILL);

stdin().read_to_string(&mut source).unwrap();
let chunks: Vec<&str> = source.split(SEPARATOR).collect();
stdin().read_to_string(&mut request).unwrap();

// Inject global variables
for (key, val) in env::vars() {
let escaped_val = val.replace('"', "\\\"");
contents.push_str(&format!("const {} = \"{}\";", key, escaped_val));
}

contents.push_str(&chunks.first().unwrap());
contents.push_str(&source.unwrap());

let _ = context.eval_global("handler.js", &contents).unwrap();
let global = context.global_object().unwrap();
let entrypoint = global.get_property("entrypoint").unwrap();

let input_bytes = &chunks.last().unwrap().as_bytes();
let input_value = json::transcode_input(&context, &input_bytes).unwrap();
let input_bytes = request.as_bytes();
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]) {
Expand All @@ -50,7 +44,6 @@ fn main() {
let output = json::transcode_output(output_value).unwrap();

stdout()
.write(&output)
.write_all(&output)
.expect("Error when returning the response");
stdout().flush().expect("Error when returning the response");
}
Binary file modified kits/javascript/wasm-workers-quick-js-engine.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate lazy_static;
mod config;
mod data;
mod router;
mod runner;
mod workers;

use actix_files::{Files, NamedFile};
use actix_web::dev::{fn_service, ServiceRequest, ServiceResponse};
Expand All @@ -20,10 +20,10 @@ use actix_web::{
use clap::Parser;
use data::kv::KV;
use router::Routes;
use runner::WasmOutput;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use std::{collections::HashMap, sync::RwLock};
use workers::wasm_io::WasmOutput;

// Provide a static root_path so it can be used in the default_worker to manage
// static assets.
Expand Down Expand Up @@ -159,8 +159,8 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse {
};

let handler_result = route
.runner
.run(&req, body_str, store, vars)
.worker
.run(&req, &body_str, store, vars)
.unwrap_or_else(|_| WasmOutput::failed());

let mut builder = HttpResponse::build(
Expand Down Expand Up @@ -246,7 +246,7 @@ async fn main() -> std::io::Result<()> {

// Append routes to the current service
for route in routes.routes.iter() {
app = app.service(web::resource(&route.actix_path()).to(wasm_handler));
app = app.service(web::resource(route.actix_path()).to(wasm_handler));

// Configure KV
if let Some(namespace) = route.config.as_ref().and_then(|c| c.data_kv_namespace()) {
Expand All @@ -261,7 +261,7 @@ async fn main() -> std::io::Result<()> {
}

app = app.service(
Files::new(&static_prefix, &args.path.join("public"))
Files::new(&static_prefix, args.path.join("public"))
.index_file("index.html")
// This handler check if there's an HTML file in the public folder that
// can reply to the given request. For example, if someone request /about,
Expand Down
12 changes: 6 additions & 6 deletions src/router/route.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::{config::Config, runner::Runner};
use crate::{config::Config, workers::Worker};
use lazy_static::lazy_static;
use regex::Regex;
use std::{
Expand Down Expand Up @@ -37,14 +37,13 @@ pub enum RouteAffinity {
/// api/index.wasm => /api
/// api/v2/ping.wasm => /api/v2/ping
/// ```
#[derive(Clone)]
pub struct Route {
/// The wasm module that will manage the route
pub handler: PathBuf,
/// The URL path
pub path: String,
/// The preconfigured runner
pub runner: Runner,
/// The associated worker
pub worker: Worker,
/// The associated configuration if available
pub config: Option<Config>,
}
Expand All @@ -55,7 +54,8 @@ impl Route {
///
/// This method also initializes the Runner and loads the Config if available.
pub fn new(base_path: &Path, filepath: PathBuf, prefix: &str) -> Self {
let runner = Runner::new(&filepath).unwrap();
let worker = Worker::new(&filepath).unwrap();

// Load configuration
let mut config_path = filepath.clone();
config_path.set_extension("toml");
Expand All @@ -73,7 +73,7 @@ impl Route {
Self {
path: Self::retrieve_route(base_path, &filepath, prefix),
handler: filepath,
runner,
worker,
config,
}
}
Expand Down
Loading