Skip to content

Commit 17e0f8a

Browse files
authored
Merge pull request #26 from wacker-dev/wasip2
Support the wasm32-wasip2 target
2 parents d8485ef + 0b4fa41 commit 17e0f8a

File tree

7 files changed

+158
-89
lines changed

7 files changed

+158
-89
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ on:
66
- 'release-**'
77
pull_request:
88
jobs:
9-
ci:
10-
name: Lint and test
9+
lint:
1110
runs-on: ubuntu-latest
1211
timeout-minutes: 30
1312
steps:
1413
- uses: actions/checkout@v4
1514
- name: Install Rust
16-
uses: dtolnay/rust-toolchain@1.79.0
15+
uses: dtolnay/rust-toolchain@1.82.0
1716
with:
18-
targets: wasm32-wasip1
17+
targets: wasm32-wasip2
1918
components: clippy, rustfmt
2019
- name: Re-vendor WIT
2120
run: |
@@ -25,5 +24,24 @@ jobs:
2524
run: cargo fmt --all -- --check
2625
- name: cargo clippy
2726
run: cargo clippy --all-targets --all-features -- -D warnings
27+
test:
28+
needs: lint
29+
runs-on: ubuntu-latest
30+
timeout-minutes: 30
31+
strategy:
32+
matrix:
33+
include:
34+
- rust: "1.81"
35+
targets: "wasm32-wasip1"
36+
- rust: "1.82"
37+
targets: "wasm32-wasip2"
38+
name: Test on target ${{ matrix.targets }}
39+
steps:
40+
- uses: actions/checkout@v4
41+
- name: Install Rust
42+
uses: dtolnay/rust-toolchain@master
43+
with:
44+
toolchain: ${{ matrix.rust }}
45+
targets: ${{ matrix.targets }}
2846
- name: cargo test
2947
run: cargo test

test-programs/artifacts/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ publish = false
77
[build-dependencies]
88
anyhow.workspace = true
99
cargo_metadata = "0.18.1"
10-
wit-component = "0.208.1"
10+
wit-component = "0.219.1"
1111
heck = "0.5.0"
12-
wasi-preview1-component-adapter-provider = "23.0.1"
12+
wasi-preview1-component-adapter-provider = "25.0.1"
13+
version_check = "0.9.5"

test-programs/artifacts/build.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ fn main() -> Result<()> {
1010
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
1111

1212
println!("cargo::rerun-if-changed=../src");
13+
println!("cargo::rerun-if-changed=../../waki");
14+
15+
let wasip2 = version_check::is_min_version("1.82.0").unwrap_or(false);
16+
let wasi_target = if wasip2 {
17+
"wasm32-wasip2"
18+
} else {
19+
"wasm32-wasip1"
20+
};
1321

1422
let status = Command::new("cargo")
1523
.arg("build")
1624
.arg("--package=test-programs")
17-
.arg("--target=wasm32-wasip1")
25+
.arg(format!("--target={wasi_target}"))
1826
.env("CARGO_TARGET_DIR", &out_dir)
1927
.env("CARGO_PROFILE_DEV_DEBUG", "1")
2028
.status()?;
@@ -37,21 +45,26 @@ fn main() -> Result<()> {
3745
for target in targets {
3846
let camel = target.to_shouty_snake_case();
3947
let wasm = out_dir
40-
.join("wasm32-wasip1")
48+
.join(wasi_target)
4149
.join("debug")
4250
.join(format!("{target}.wasm"));
4351

44-
let adapter = match target.as_str() {
45-
s if s.starts_with("client_") => {
46-
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER
47-
}
48-
s if s.starts_with("server_") => {
49-
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER
50-
}
51-
other => panic!("unknown type {other}"),
52+
let path = if wasip2 {
53+
wasm
54+
} else {
55+
compile_component(
56+
&wasm,
57+
match target.as_str() {
58+
s if s.starts_with("client_") => {
59+
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER
60+
}
61+
s if s.starts_with("server_") => {
62+
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER
63+
}
64+
other => panic!("unknown type {other}"),
65+
},
66+
)?
5267
};
53-
54-
let path = compile_component(&wasm, adapter)?;
5568
generated_code += &format!("pub const {camel}_COMPONENT: &str = {path:?};\n");
5669
}
5770

waki/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ waki-macros.workspace = true
1919
anyhow.workspace = true
2020
serde.workspace = true
2121
wit-bindgen = "0.34.0"
22-
url = "2.5.2"
22+
form_urlencoded = "1.2.1"
2323
http = "1.1.0"
24-
serde_urlencoded = "0.7.1"
2524
serde_json = { version = "1.0.128", optional = true }
2625
mime = { version = "0.3.17", optional = true }
2726
mime_guess = { version = "2.0.5", optional = true }

waki/src/common/request_and_response.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
};
88
use anyhow::{anyhow, Error, Result};
99
use serde::Serialize;
10+
use std::borrow::Borrow;
1011
use std::collections::HashMap;
1112

1213
macro_rules! impl_common_get_methods {
@@ -68,7 +69,7 @@ macro_rules! impl_common_get_methods {
6869

6970
/// Parse the body as form data.
7071
pub fn form(self) -> Result<HashMap<String, String>> {
71-
Ok(serde_urlencoded::from_bytes(self.body()?.as_ref())?)
72+
Ok(form_urlencoded::parse(self.body()?.as_ref()).into_owned().collect())
7273
}
7374

7475
/// Parse the body as multipart/form-data.
@@ -223,23 +224,24 @@ macro_rules! impl_common_set_methods {
223224
/// # use waki::ResponseBuilder;
224225
/// # fn run() {
225226
/// # let r = ResponseBuilder::new();
226-
/// r.form(&[("a", "b"), ("c", "d")]);
227+
/// r.form([("a", "b"), ("c", "d")]);
227228
/// # }
228229
/// ```
229-
pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> Self {
230-
let mut err = None;
230+
pub fn form<K, V, I>(mut self, form: I) -> Self
231+
where
232+
K: AsRef<str>,
233+
V: AsRef<str>,
234+
I: IntoIterator,
235+
I::Item: Borrow<(K, V)>,
236+
{
231237
if let Ok(ref mut inner) = self.inner {
232238
inner.headers.insert(
233239
CONTENT_TYPE,
234240
"application/x-www-form-urlencoded".parse().unwrap(),
235241
);
236-
match serde_urlencoded::to_string(form) {
237-
Ok(data) => inner.body = Body::Bytes(data.into()),
238-
Err(e) => err = Some(e.into()),
239-
}
240-
}
241-
if let Some(e) = err {
242-
self.inner = Err(e);
242+
let mut serializer = form_urlencoded::Serializer::new(String::new());
243+
serializer.extend_pairs(form);
244+
inner.body = Body::Bytes(serializer.finish().into())
243245
}
244246
self
245247
}

waki/src/common/scheme.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::bindings::wasi::http::types::Scheme;
2-
use std::fmt::{Display, Formatter, Result};
32

43
impl From<&str> for Scheme {
54
fn from(s: &str) -> Self {
@@ -11,12 +10,14 @@ impl From<&str> for Scheme {
1110
}
1211
}
1312

14-
impl Display for Scheme {
15-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
16-
f.write_str(match self {
17-
Scheme::Http => "http",
18-
Scheme::Https => "https",
19-
Scheme::Other(s) => s,
20-
})
13+
impl TryInto<http::uri::Scheme> for Scheme {
14+
type Error = http::uri::InvalidUri;
15+
16+
fn try_into(self) -> Result<http::uri::Scheme, Self::Error> {
17+
match self {
18+
Scheme::Http => Ok(http::uri::Scheme::HTTP),
19+
Scheme::Https => Ok(http::uri::Scheme::HTTPS),
20+
Scheme::Other(s) => s.as_str().try_into(),
21+
}
2122
}
2223
}

0 commit comments

Comments
 (0)