From 313c40bb625f9c9d84fdf406b35f4679aa447ee4 Mon Sep 17 00:00:00 2001 From: Angel M De Miguel Date: Wed, 5 Jul 2023 11:02:47 +0200 Subject: [PATCH 1/4] feat: add new fetch bindings to the rust kit --- Cargo.lock | 72 ++++++- Cargo.toml | 1 + examples/Makefile | 7 +- examples/rust-basic/Cargo.lock | 166 +++++++++++++++- examples/rust-fetch/Cargo.lock | 330 ++++++++++++++++++++++++++++++++ examples/rust-fetch/Cargo.toml | 10 + examples/rust-fetch/README.md | 35 ++++ examples/rust-fetch/src/main.rs | 68 +++++++ kits/rust/Cargo.toml | 4 +- kits/rust/src/bindings.rs | 64 +++++++ kits/rust/src/lib.rs | 4 + 11 files changed, 752 insertions(+), 9 deletions(-) create mode 100644 examples/rust-fetch/Cargo.lock create mode 100644 examples/rust-fetch/Cargo.toml create mode 100644 examples/rust-fetch/README.md create mode 100644 examples/rust-fetch/src/main.rs create mode 100644 kits/rust/src/bindings.rs diff --git a/Cargo.lock b/Cargo.lock index da43de96..3eab46b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3152,6 +3152,7 @@ dependencies = [ "http", "serde", "serde_json", + "wit-bindgen-rust", "worker", ] @@ -3808,7 +3809,16 @@ version = "0.2.0" source = "git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976#b89d5079ba5b07b319631a1b191d2139f126c976" dependencies = [ "anyhow", - "wit-parser 0.2.0", + "wit-parser 0.2.0 (git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976)", +] + +[[package]] +name = "wit-bindgen-gen-core" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "wit-parser 0.2.0 (git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba)", ] [[package]] @@ -3817,7 +3827,26 @@ version = "0.2.0" source = "git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976#b89d5079ba5b07b319631a1b191d2139f126c976" dependencies = [ "heck 0.3.3", - "wit-bindgen-gen-core", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976)", +] + +[[package]] +name = "wit-bindgen-gen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck 0.3.3", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba)", +] + +[[package]] +name = "wit-bindgen-gen-rust-wasm" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck 0.3.3", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba)", + "wit-bindgen-gen-rust 0.2.0 (git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba)", ] [[package]] @@ -3826,8 +3855,29 @@ version = "0.2.0" source = "git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976#b89d5079ba5b07b319631a1b191d2139f126c976" dependencies = [ "heck 0.3.3", - "wit-bindgen-gen-core", - "wit-bindgen-gen-rust", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976)", + "wit-bindgen-gen-rust 0.2.0 (git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976)", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "async-trait", + "bitflags 1.3.2", + "wit-bindgen-rust-impl", +] + +[[package]] +name = "wit-bindgen-rust-impl" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "proc-macro2", + "syn 1.0.109", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba)", + "wit-bindgen-gen-rust-wasm", ] [[package]] @@ -3849,7 +3899,7 @@ source = "git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07 dependencies = [ "proc-macro2", "syn 1.0.109", - "wit-bindgen-gen-core", + "wit-bindgen-gen-core 0.2.0 (git+https://github.com/fermyon/wit-bindgen-backport?rev=b89d5079ba5b07b319631a1b191d2139f126c976)", "wit-bindgen-gen-wasmtime", ] @@ -3865,6 +3915,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "wit-parser" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "id-arena", + "pulldown-cmark", + "unicode-normalization", + "unicode-xid", +] + [[package]] name = "wit-parser" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index d509d10b..d9eb84f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ members = [ exclude = [ "examples/pdf-create", "examples/rust-basic", + "examples/rust-fetch", "examples/rust-kv", "examples/rust-params" ] diff --git a/examples/Makefile b/examples/Makefile index 2d3d6bb5..0f040c55 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -5,6 +5,11 @@ rust-basic: cargo build --target wasm32-wasi --release && \ cp target/wasm32-wasi/release/rust-basic.wasm ./basic.wasm +rust-fetch: + cd rust-fetch && \ + cargo build --target wasm32-wasi --release && \ + cp target/wasm32-wasi/release/rust-fetch.wasm ./index.wasm + rust-kv: cd rust-kv && \ cargo build --target wasm32-wasi --release && \ @@ -20,4 +25,4 @@ rust-pdf-create: cargo build --target wasm32-wasi --release && \ mv target/wasm32-wasi/release/rust-pdf-create.wasm ./index.wasm -all: rust-basic rust-kv rust-params +all: rust-basic rust-fetch rust-kv rust-params diff --git a/examples/rust-basic/Cargo.lock b/examples/rust-basic/Cargo.lock index 088a27af..1a80e1b5 100644 --- a/examples/rust-basic/Cargo.lock +++ b/examples/rust-basic/Cargo.lock @@ -8,12 +8,29 @@ version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" +[[package]] +name = "async-trait" +version = "0.1.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b84f9ebcc6c1f5b8cb160f6990096a5c127f423fcb6e1ccc46c370cbdfb75dfc" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "base64" version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bytes" version = "1.2.1" @@ -26,6 +43,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "http" version = "0.2.8" @@ -37,12 +63,24 @@ dependencies = [ "itoa", ] +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + [[package]] name = "itoa" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "proc-macro2" version = "1.0.43" @@ -52,6 +90,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulldown-cmark" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + [[package]] name = "quote" version = "1.0.21" @@ -117,12 +166,63 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-ident" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -131,19 +231,81 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-workers-rs" -version = "1.0.1" +version = "1.3.0" dependencies = [ "anyhow", "base64", "http", "serde", "serde_json", + "wit-bindgen-rust", "worker", ] +[[package]] +name = "wit-bindgen-gen-core" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-gen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck", + "wit-bindgen-gen-core", +] + +[[package]] +name = "wit-bindgen-gen-rust-wasm" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "async-trait", + "bitflags", + "wit-bindgen-rust-impl", +] + +[[package]] +name = "wit-bindgen-rust-impl" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "proc-macro2", + "syn", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust-wasm", +] + +[[package]] +name = "wit-parser" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "id-arena", + "pulldown-cmark", + "unicode-normalization", + "unicode-xid", +] + [[package]] name = "worker" -version = "1.0.1" +version = "1.3.0" dependencies = [ "anyhow", "http", diff --git a/examples/rust-fetch/Cargo.lock b/examples/rust-fetch/Cargo.lock new file mode 100644 index 00000000..b20f14e2 --- /dev/null +++ b/examples/rust-fetch/Cargo.lock @@ -0,0 +1,330 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" + +[[package]] +name = "async-trait" +version = "0.1.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79fa67157abdfd688a259b6648808757db9347af834624f27ec646da976aee5d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.23", +] + +[[package]] +name = "base64" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "http" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + +[[package]] +name = "itoa" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a" + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "proc-macro2" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pulldown-cmark" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + +[[package]] +name = "quote" +version = "1.0.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rust-fetch" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde", + "serde_json", + "wasm-workers-rs", +] + +[[package]] +name = "ryu" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9" + +[[package]] +name = "serde" +version = "1.0.166" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d01b7404f9d441d3ad40e6a636a7782c377d2abdbe4fa2440e2edcc2f4f10db8" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.166" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd83d6dde2b6b2d466e14d9d1acce8816dedee94f735eac6395808b3483c6d6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.23", +] + +[[package]] +name = "serde_json" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f1e14e89be7aa4c4b78bdbdc9eb5bf8517829a600ae8eaa39a6e1d960b5185c" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59fb7d6d8281a51045d62b8eb3a7d1ce347b76f312af50cd3dc0af39c87c1737" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-ident" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-workers-rs" +version = "1.3.0" +dependencies = [ + "anyhow", + "base64", + "http", + "serde", + "serde_json", + "wit-bindgen-rust", + "worker", +] + +[[package]] +name = "wit-bindgen-gen-core" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-gen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck", + "wit-bindgen-gen-core", +] + +[[package]] +name = "wit-bindgen-gen-rust-wasm" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "heck", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "async-trait", + "bitflags", + "wit-bindgen-rust-impl", +] + +[[package]] +name = "wit-bindgen-rust-impl" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "proc-macro2", + "syn 1.0.109", + "wit-bindgen-gen-core", + "wit-bindgen-gen-rust-wasm", +] + +[[package]] +name = "wit-parser" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" +dependencies = [ + "anyhow", + "id-arena", + "pulldown-cmark", + "unicode-normalization", + "unicode-xid", +] + +[[package]] +name = "worker" +version = "1.3.0" +dependencies = [ + "anyhow", + "http", + "quote", + "serde", + "serde_json", + "syn 1.0.109", + "wasi", +] diff --git a/examples/rust-fetch/Cargo.toml b/examples/rust-fetch/Cargo.toml new file mode 100644 index 00000000..241431b8 --- /dev/null +++ b/examples/rust-fetch/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust-fetch" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.63" +wasm-workers-rs = { path = "../../kits/rust" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.85" diff --git a/examples/rust-fetch/README.md b/examples/rust-fetch/README.md new file mode 100644 index 00000000..4431416a --- /dev/null +++ b/examples/rust-fetch/README.md @@ -0,0 +1,35 @@ +# Rust fetch example + +Compile a Rust worker that can retrieve data from a remote endpoint. You can run it in Wasm Workers Server. + +## Prerequisites + +* Wasm Workers Server (wws): + + ```shell-session + curl -fsSL https://workers.wasmlabs.dev/install | bash + ``` + +* [Install Rust with rustup](https://www.rust-lang.org/tools/install) +* Install the `wasm32-wasi` target: + + ```shell-session + rustup target add wasm32-wasi + ``` + +## Build + +```shell-session +cargo build --target wasm32-wasi --release && \ + cp target/wasm32-wasi/release/rust-fetch.wasm ./index.wasm +``` + +## Run + +```shell-session +wws . +``` + +## Resources + +* [Rust documentation](https://workers.wasmlabs.dev/docs/languages/rust) diff --git a/examples/rust-fetch/src/main.rs b/examples/rust-fetch/src/main.rs new file mode 100644 index 00000000..892c49cb --- /dev/null +++ b/examples/rust-fetch/src/main.rs @@ -0,0 +1,68 @@ +use anyhow::Result; +use serde::{Deserialize, Serialize}; +use wasm_workers_rs::{ + bindings, + http::{self, Request, Response}, + worker, Content, +}; + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct Post { + id: i32, + title: String, + body: String, + user_id: i32, +} + +#[worker] +fn handler(_req: Request) -> Result> { + let external_request = Request::builder() + .uri("https://jsonplaceholder.typicode.com/posts/1") + .body(String::new()) + .unwrap(); + + // Get the request + let res = bindings::send_http_request(external_request).unwrap(); + + // Parse the response + let data = res.body(); + let data_str = String::from_utf8_lossy(&data); + + eprintln!("API response: {data_str}"); + + let post: Post = serde_json::from_slice(&data).unwrap(); + + // Applied changes here to use the Response method. This requires changes + // on signature and how it returns the data. + let response = format!( + " + + Wasm Workers Server + + + + + + +
+

{}

+

{}

+
+", + &post.title, &post.body + ); + + Ok(http::Response::builder() + .status(200) + .header("x-generated-by", "wasm-workers-server") + .body(response.into())?) +} diff --git a/kits/rust/Cargo.toml b/kits/rust/Cargo.toml index b6dae869..5a69536d 100644 --- a/kits/rust/Cargo.toml +++ b/kits/rust/Cargo.toml @@ -17,4 +17,6 @@ http = "0.2.8" worker = { path = "./worker" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" -base64 = "0.21.0" \ No newline at end of file +base64 = "0.21.0" +# Use an old version until we add support for components. +wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" } diff --git a/kits/rust/src/bindings.rs b/kits/rust/src/bindings.rs new file mode 100644 index 00000000..94675834 --- /dev/null +++ b/kits/rust/src/bindings.rs @@ -0,0 +1,64 @@ +use ::http::{Method, Request, Response}; + +// Load bindings from WIT file. +wit_bindgen_rust::import!({paths: ["../../wit/core/http.wit"]}); + +use self::http::{ + send_http_request as host_send_http_request, HttpMethod, HttpRequest, HttpResponse, +}; + +pub use self::http::HttpRequestError; + +impl From for Response> { + fn from(value: HttpResponse) -> Self { + let mut builder = Response::builder().status(value.status); + + for (key, value) in value.headers.iter() { + builder = builder.header(key, value); + } + + match value.body { + Some(data) => builder.body(data).unwrap(), + None => builder.body(Vec::new()).unwrap(), + } + } +} + +pub fn send_http_request(req: Request) -> Result>, HttpRequestError> { + let method = match *req.method() { + Method::GET => HttpMethod::Get, + Method::POST => HttpMethod::Get, + Method::PUT => HttpMethod::Get, + Method::PATCH => HttpMethod::Get, + Method::DELETE => HttpMethod::Get, + Method::OPTIONS => HttpMethod::Get, + Method::HEAD => HttpMethod::Get, + _ => HttpMethod::Get, + }; + + let mut parsed_headers: Vec<(String, String)> = Vec::new(); + + for (key, value) in req.headers().iter() { + if let Ok(value) = value.to_str() { + parsed_headers.push((key.to_string(), value.to_string())) + } + } + + let headers_slice: Vec<(&str, &str)> = parsed_headers + .iter() + .map(|(k, v)| (k.as_str(), v.as_str())) + .collect::>(); + + let uri = req.uri().to_string(); + let body = req.into_body(); + + let request = HttpRequest { + body: Some(body.as_bytes()), + headers: &headers_slice, + method, + params: &[], + uri: &uri, + }; + + host_send_http_request(request).map(|http_req| http_req.into()) +} diff --git a/kits/rust/src/lib.rs b/kits/rust/src/lib.rs index fe3620e2..0d28e718 100644 --- a/kits/rust/src/lib.rs +++ b/kits/rust/src/lib.rs @@ -3,6 +3,7 @@ mod cache; mod content; + pub use cache::Cache; pub use content::Content; pub mod io; @@ -10,3 +11,6 @@ pub mod io; pub use worker::worker; // Re-export http pub use http; + +// Bindings! +pub mod bindings; From ce1290f9b7e109e6c95dcd35f24476c1265736d5 Mon Sep 17 00:00:00 2001 From: Angel M De Miguel Date: Wed, 5 Jul 2023 14:04:33 +0200 Subject: [PATCH 2/4] docs: pin the Rust examples to the latest version branch --- docs/docs/languages/rust.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/languages/rust.md b/docs/docs/languages/rust.md index 283863d8..8fecabcb 100644 --- a/docs/docs/languages/rust.md +++ b/docs/docs/languages/rust.md @@ -29,7 +29,7 @@ In this example, the worker will get a request and print all the related informa [dependencies] anyhow = "1.0.63" - wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/" } + wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/", branch = "v1.3" } ``` 1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `worker` macro: @@ -136,7 +136,7 @@ To add a KV store to your worker, follow these steps: [dependencies] anyhow = "1.0.63" - wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/" } + wasm-workers-rs = { git = "https://github.com/vmware-labs/wasm-workers-server/", branch = "v1.3" } ``` 1. Add the `reply` function to the `src/main.rs` file. You will need to import the required resources from the `wasm-workers-rs` crate and use the `worker` macro. In this case, we will add a new attribute to the `worker` macro called `cache` and update the function signature: @@ -317,4 +317,4 @@ If you prefer, you can configure the environment variable value dynamically by f ## Other examples * [Basic](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/rust-basic) -* [Counter](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/rust-kv) \ No newline at end of file +* [Counter](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/rust-kv) From 0bc6094bbd37a6624f68fb1aed9a4c889a159be8 Mon Sep 17 00:00:00 2001 From: Angel M De Miguel Date: Wed, 5 Jul 2023 14:22:40 +0200 Subject: [PATCH 3/4] feat: allow passing a Vec as body when making requests from a worker --- kits/rust/src/bindings.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kits/rust/src/bindings.rs b/kits/rust/src/bindings.rs index 94675834..a01f97a1 100644 --- a/kits/rust/src/bindings.rs +++ b/kits/rust/src/bindings.rs @@ -24,7 +24,10 @@ impl From for Response> { } } -pub fn send_http_request(req: Request) -> Result>, HttpRequestError> { +pub fn send_http_request(req: Request) -> Result>, HttpRequestError> +where + T: Into>, +{ let method = match *req.method() { Method::GET => HttpMethod::Get, Method::POST => HttpMethod::Get, @@ -50,10 +53,10 @@ pub fn send_http_request(req: Request) -> Result>, Http .collect::>(); let uri = req.uri().to_string(); - let body = req.into_body(); + let body: Vec = req.into_body().into(); let request = HttpRequest { - body: Some(body.as_bytes()), + body: Some(body.as_slice()), headers: &headers_slice, method, params: &[], From 3baec2187d221ce6def5abe234fb94ec5c01e1ae Mon Sep 17 00:00:00 2001 From: Angel M De Miguel Date: Thu, 6 Jul 2023 09:06:18 +0200 Subject: [PATCH 4/4] docs: add a clarification about how rust-fetch example is retrieving the data --- examples/rust-fetch/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rust-fetch/README.md b/examples/rust-fetch/README.md index 4431416a..e9db6cf5 100644 --- a/examples/rust-fetch/README.md +++ b/examples/rust-fetch/README.md @@ -1,6 +1,6 @@ # Rust fetch example -Compile a Rust worker that can retrieve data from a remote endpoint. You can run it in Wasm Workers Server. +Compile a Rust worker that can retrieve data from a remote endpoint (via HTTP bindings). You can run it in Wasm Workers Server. ## Prerequisites