这是indexloc提供的服务,不要输入任何密码
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Caching
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Format
run: cargo fmt
- name: Clippy
run: cargo clippy
- name: Test
run: cargo test
build:
Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse {

for route in routes.routes.iter() {
if route.path == req.path() {
let body_str = String::from_utf8(body.to_vec()).unwrap_or(String::from(""));
let body_str = String::from_utf8(body.to_vec()).unwrap_or_else(|_| String::from(""));

// Init KV
let kv_namespace = match &route.config {
Expand All @@ -68,10 +68,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse {
let connector = data_connectors.read().unwrap();
let kv_store = connector.kv.find_store(namespace);

match kv_store {
Some(store) => Some(store.clone()),
None => None,
}
kv_store.map(|store| store.clone())
}
None => None,
};
Expand All @@ -95,7 +92,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse {
for (key, val) in handler_result.headers.iter() {
// Note that QuickJS is replacing the "-" character
// with "_" on property keys. Here, we rollback it
builder.insert_header((key.replace("_", "-").as_str(), val.as_str()));
builder.insert_header((key.replace('_', "-").as_str(), val.as_str()));
}

// Write to the state if required
Expand Down
12 changes: 6 additions & 6 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ impl Route {
Self {
path: Self::retrieve_route(base_path, &filepath),
handler: filepath,
runner: runner,
config: config,
runner,
config,
}
}

// Process the given path to return the proper route for the API.
// It will transform paths like test/index.wasm into /test.
fn retrieve_route(base_path: &Path, path: &PathBuf) -> String {
fn retrieve_route(base_path: &Path, path: &Path) -> String {
// TODO: Improve this entire method
// @ref #13
if let Some(api_path) = path.to_str() {
let parsed_path: String = api_path
.to_string()
.replace(".wasm", "")
.replace(".js", "")
.replace(base_path.to_str().unwrap_or_else(|| "./"), "");
.replace(base_path.to_str().unwrap_or("./"), "");
let mut normalized = String::from("/") + &parsed_path.replace("index", "");

// Remove trailing / to avoid 404 errors
Expand All @@ -83,7 +83,7 @@ impl Route {
normalized
} else {
// TODO: Manage better unexpected characters in paths
String::from(path.to_str().unwrap_or_else(|| "/unknown"))
String::from(path.to_str().unwrap_or("/unknown"))
}
}
}
Expand All @@ -106,7 +106,7 @@ pub fn initialize_routes(base_path: &Path) -> Vec<Route> {
for entry in glob_items {
match entry {
Ok(filepath) => {
routes.push(Route::new(&base_path, filepath));
routes.push(Route::new(base_path, filepath));
}
Err(e) => println!("Could not read the file {:?}", e),
}
Expand Down
15 changes: 7 additions & 8 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use wasi_common::{pipe::ReadPipe, pipe::WritePipe};
use wasmtime::*;
use wasmtime_wasi::sync::WasiCtxBuilder;
Expand Down Expand Up @@ -39,8 +39,8 @@ impl WasmInput {
url: request.uri().to_string(),
method: String::from(request.method().as_str()),
headers: build_headers_hash(request.headers()),
body: body,
kv: kv.unwrap_or(HashMap::new()),
body,
kv: kv.unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -113,8 +113,7 @@ impl Runner {
RunnerHandlerType::JavaScript,
module,
fs::read_to_string(path)
.expect(&format!("Error reading {}", path.display()))
.to_string(),
.unwrap_or_else(|_| panic!("Error reading {}", path.display())),
)
} else {
let module = Module::from_file(&engine, path)?;
Expand All @@ -130,7 +129,7 @@ impl Runner {
})
}

fn is_js_file(path: &PathBuf) -> bool {
fn is_js_file(path: &Path) -> bool {
match path.extension() {
Some(os_str) => os_str == "js",
None => false,
Expand Down Expand Up @@ -162,9 +161,9 @@ impl Runner {

// WASI context
let wasi = WasiCtxBuilder::new()
.stdin(Box::new(stdin.clone()))
.stdin(Box::new(stdin))
.stdout(Box::new(stdout.clone()))
.stderr(Box::new(stderr.clone()))
.stderr(Box::new(stderr))
.inherit_args()?
.build();
let mut store = Store::new(&self.engine, wasi);
Expand Down