interop/build/web/
wasm.rs1use crate::util::RunningProcess;
2use color_eyre::eyre::Result;
3use std::net::SocketAddr;
4use std::path::PathBuf;
5
6pub(crate) async fn build_wasm(wasm_deploy_path: PathBuf) -> Result<()> {
7 use tokio::process::Command;
8
9 let cwd = std::env::current_dir()?;
10
11 if cfg!(feature = "proteus") {
12 let spinner = RunningProcess::new("Building Cryptobox ESM bundle...", false);
13
14 Command::new("bun")
15 .args(["install"])
16 .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
17 .stdout(std::process::Stdio::null())
18 .stderr(std::process::Stdio::null())
19 .status()
20 .await?;
21
22 Command::new("bun")
23 .args(["run", "build"])
24 .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
25 .stdout(std::process::Stdio::null())
26 .stderr(std::process::Stdio::null())
27 .status()
28 .await?;
29
30 spinner.success("Cryptobox ESM bundle [OK]");
31 }
32
33 let spinner = RunningProcess::new("Building WASM bundle...", false);
34
35 Command::new("cargo")
36 .args(["make", "wasm"])
37 .current_dir(cwd.join("crypto-ffi"))
38 .stdout(std::process::Stdio::null())
39 .stderr(std::process::Stdio::null())
40 .status()
41 .await?;
42
43 Command::new("bun")
44 .args(["run", "build"])
45 .current_dir(cwd.join("crypto-ffi/bindings/js"))
46 .stdout(std::process::Stdio::null())
47 .stderr(std::process::Stdio::null())
48 .status()
49 .await?;
50
51 std::fs::copy(
52 cwd.join("crypto-ffi/bindings/js/test/wdio/index.html"),
53 wasm_deploy_path.join("index.html"),
54 )?;
55
56 std::fs::copy(
57 cwd.join("crypto-ffi/bindings/js/src/corecrypto.js"),
58 wasm_deploy_path.join("corecrypto.js"),
59 )?;
60
61 std::fs::copy(
62 cwd.join("crypto-ffi/bindings/js/src/corecrypto.d.ts"),
63 wasm_deploy_path.join("corecrypto.d.ts"),
64 )?;
65
66 std::fs::copy(
67 cwd.join("crypto-ffi/bindings/js/src/core-crypto-ffi_bg.wasm"),
68 wasm_deploy_path.join("core-crypto-ffi_bg.wasm"),
69 )?;
70
71 spinner.success("WASM bundle [OK]");
72 Ok(())
73}
74
75pub(crate) fn bind_http_server(wasm_deploy_path: PathBuf) -> (SocketAddr, impl Future<Output = ()> + 'static) {
76 use warp::Filter as _;
77 let warp_filter_cc = warp::path("core-crypto").and(warp::fs::dir(wasm_deploy_path));
78 let warp_filter_cbox =
79 warp::path("cryptobox").and(warp::fs::dir("interop/src/build/web/cryptobox-esm/dist".to_string()));
80
81 warp::serve(warp_filter_cc.or(warp_filter_cbox).boxed()).bind_ephemeral(([0, 0, 0, 0], 0))
82}