Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: add an example for using wasm modules in Edge Functions (#32810)

Lakshan Pereracdb95a60366d
diff --git a/examples/edge-functions/supabase/config.toml b/examples/edge-functions/supabase/config.toml
index 99281efbabb..dbf1d6d2d1b 100644
--- a/examples/edge-functions/supabase/config.toml
+++ b/examples/edge-functions/supabase/config.toml
@@ -73,3 +73,6 @@ objects_path = "./buckets/images"
[functions.kysely-postgres]
import_map = "./functions/import_map.json"
+
+[functions.wasm-modules]
+static_files = [ "./functions/wasm-modules/add-wasm/pkg/add_wasm_bg.wasm"]
diff --git a/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.lock b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.lock
new file mode 100644
index 00000000000..b624e908a6b
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.lock
@@ -0,0 +1,133 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "add-wasm"
+version = "0.1.0"
+dependencies = [
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "bumpalo"
+version = "3.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "log"
+version = "0.4.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
+
+[[package]]
+name = "once_cell"
+version = "1.20.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.38"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rustversion"
+version = "1.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
+
+[[package]]
+name = "syn"
+version = "2.0.96"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.100"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+ "rustversion",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.100"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
+dependencies = [
+ "bumpalo",
+ "log",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.100"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.100"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.100"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
+dependencies = [
+ "unicode-ident",
+]
diff --git a/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.toml b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.toml
new file mode 100644
index 00000000000..ab11b6eb564
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "add-wasm"
+version = "0.1.0"
+description = "A simple wasm module that adds two numbers"
+license = "MIT/Apache-2.0"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+wasm-bindgen = "0.2"
diff --git a/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/src/lib.rs b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/src/lib.rs
new file mode 100644
index 00000000000..57fd95b9f99
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/wasm-modules/add-wasm/src/lib.rs
@@ -0,0 +1,6 @@
+use wasm_bindgen::prelude::*;
+
+#[wasm_bindgen]
+pub fn add(a: u32, b: u32) -> u32 {
+ a + b + 100
+}
diff --git a/examples/edge-functions/supabase/functions/wasm-modules/index.ts b/examples/edge-functions/supabase/functions/wasm-modules/index.ts
new file mode 100644
index 00000000000..66d5085cf24
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/wasm-modules/index.ts
@@ -0,0 +1,16 @@
+const wasmBuffer = await Deno.readFile(
+ new URL("./add-wasm/pkg/add_wasm_bg.wasm", import.meta.url),
+);
+
+const { instance } = await WebAssembly.instantiate(
+ wasmBuffer,
+);
+const { add } = instance.exports;
+
+Deno.serve(async (req) => {
+ const { a, b } = await req.json();
+ return new Response(
+ JSON.stringify({ result: add(a, b) }),
+ { headers: { "Content-Type": "application/json" } },
+ );
+});