Skip to content

Commit 9c6a1af

Browse files
committed
add simple test cases for polyfill that check if it can successfully load and run the wasm plugin. create wasm-serve package to serve wasm files locally
1 parent 171e1f5 commit 9c6a1af

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"core",
88
"iota",
99
"test-utils",
10+
"wasm-serve",
1011
]
1112

1213
[workspace.package]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// node --test
2+
3+
import assert from "node:assert";
4+
import { test } from "node:test";
5+
6+
import { WasmPlugin } from "./jsoncodegen-wasm32-wasip1.ts";
7+
8+
// cargo run --bin wasm-serve -- target/wasm32-wasip1/wasm/jsoncodegen*.wasm --port 7357
9+
const wasmServer = "http://localhost:7357/";
10+
11+
// list of wasm files that are served by the wasm-server is listed at "/"
12+
const response = await fetch(wasmServer);
13+
const filenames = await response.json();
14+
15+
for (const filename of filenames) {
16+
const url = new URL(filename, wasmServer);
17+
18+
test(url.href, async () => {
19+
const plugin = await WasmPlugin.load(url);
20+
const output = plugin.run("{}");
21+
assert.ok(output, "Plugin should return a string");
22+
});
23+
}

polyfill/jsoncodegen-wasm32-wasip1.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class WasmPlugin {
3030
* * @param url - The URL of the .wasm file.
3131
* @returns A promise that resolves to a generic WasmPlugin.
3232
*/
33-
static async load(url: string): Promise<WasmPlugin> {
33+
static async load(url: string | URL): Promise<WasmPlugin> {
3434
const response = fetch(url);
3535

3636
try {
@@ -121,8 +121,10 @@ export class PluginManager {
121121
* Not exported to the consumer.
122122
*/
123123
class WasiExit extends Error {
124-
constructor(public code: number) {
124+
public code: number;
125+
constructor(code: number) {
125126
super(`WASI Exit: ${code}`);
127+
this.code = code;
126128
}
127129
}
128130

wasm-serve/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "wasm-serve"
3+
version.workspace = true
4+
edition.workspace = true
5+
6+
[dependencies]
7+
axum = "0.8"
8+
clap = { version = "4", features = ["derive"] }
9+
tokio = { version = "1", features = ["rt-multi-thread"] }
10+
tower-http = { version = "0.6", features = ["fs", "trace"] }
11+
tracing = "0.1"
12+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

wasm-serve/src/main.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use axum::{
2+
Json, Router,
3+
routing::{get, get_service},
4+
};
5+
use clap::Parser;
6+
use std::{net::SocketAddr, path::PathBuf};
7+
use tower_http::services::ServeFile;
8+
use tracing_subscriber::EnvFilter;
9+
10+
/// A simple server to serve WASM files
11+
#[derive(Parser, Debug)]
12+
struct Args {
13+
/// List of .wasm files to serve
14+
#[arg(required = true, num_args = 1..)]
15+
files: Vec<PathBuf>,
16+
17+
#[arg(short, long, default_value_t = 0)]
18+
port: u16,
19+
}
20+
21+
#[tokio::main]
22+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
tracing_subscriber::fmt()
24+
.with_env_filter(EnvFilter::from_default_env(/* RUST_LOG env var sets logging level */))
25+
.init();
26+
27+
let args = Args::parse();
28+
let mut app = Router::new();
29+
30+
let mut route_paths = vec![];
31+
32+
for path in args.files {
33+
if path.is_file()
34+
&& let Some(unicode_path) = path.to_str()
35+
{
36+
let route_path = format!("/{}", unicode_path);
37+
let service = get_service(ServeFile::new(&path));
38+
app = app.route(&route_path, service);
39+
40+
tracing::info!("serving file {:?} at route {}", path, route_path);
41+
route_paths.push(route_path);
42+
}
43+
}
44+
45+
app = app.route("/", get(Json(route_paths)));
46+
47+
let addr = SocketAddr::from(([0, 0, 0, 0], args.port));
48+
let listener = tokio::net::TcpListener::bind(addr).await?;
49+
let local_addr = listener.local_addr()?;
50+
51+
tracing::info!("listening on {}", local_addr);
52+
axum::serve(listener, app).await?;
53+
54+
Ok(())
55+
}

0 commit comments

Comments
 (0)