interop/build/web/
wasm.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use crate::TEST_SERVER_PORT;
18use crate::util::RunningProcess;
19use color_eyre::eyre::Result;
20use std::net::SocketAddr;
21use std::path::PathBuf;
22
23pub(crate) async fn build_wasm(wasm_deploy_path: PathBuf) -> Result<()> {
24    use tokio::process::Command;
25
26    let cwd = std::env::current_dir()?;
27
28    if cfg!(feature = "proteus") {
29        let spinner = RunningProcess::new("Building Cryptobox ESM bundle...", false);
30
31        Command::new("bun")
32            .args(["install"])
33            .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
34            .stdout(std::process::Stdio::null())
35            .stderr(std::process::Stdio::null())
36            .status()
37            .await?;
38
39        Command::new("bun")
40            .args(["run", "build"])
41            .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
42            .stdout(std::process::Stdio::null())
43            .stderr(std::process::Stdio::null())
44            .status()
45            .await?;
46
47        spinner.success("Cryptobox ESM bundle [OK]");
48    }
49
50    let spinner = RunningProcess::new("Building WASM bundle...", false);
51
52    Command::new("cargo")
53        .args(["make", "wasm"])
54        .current_dir(cwd.join("crypto-ffi"))
55        .stdout(std::process::Stdio::null())
56        .stderr(std::process::Stdio::null())
57        .status()
58        .await?;
59
60    Command::new("bun")
61        .args(["run", "build"])
62        .current_dir(cwd.join("crypto-ffi/bindings/js"))
63        .stdout(std::process::Stdio::null())
64        .stderr(std::process::Stdio::null())
65        .status()
66        .await?;
67
68    std::fs::copy(
69        cwd.join("crypto-ffi/bindings/js/test/index.html"),
70        wasm_deploy_path.join("index.html"),
71    )?;
72
73    std::fs::copy(
74        cwd.join("crypto-ffi/bindings/js/src/corecrypto.js"),
75        wasm_deploy_path.join("corecrypto.js"),
76    )?;
77
78    std::fs::copy(
79        cwd.join("crypto-ffi/bindings/js/src/corecrypto.d.ts"),
80        wasm_deploy_path.join("corecrypto.d.ts"),
81    )?;
82
83    std::fs::copy(
84        cwd.join("crypto-ffi/bindings/js/src/core-crypto-ffi_bg.wasm"),
85        wasm_deploy_path.join("core-crypto-ffi_bg.wasm"),
86    )?;
87
88    spinner.success("WASM bundle [OK]");
89    Ok(())
90}
91
92pub(crate) async fn spawn_http_server(wasm_deploy_path: PathBuf) -> Result<()> {
93    use warp::Filter as _;
94    let addr = SocketAddr::from(([0, 0, 0, 0], TEST_SERVER_PORT.parse()?));
95    let warp_filter_cc = warp::path("core-crypto").and(warp::fs::dir(wasm_deploy_path));
96    let warp_filter_cbox =
97        warp::path("cryptobox").and(warp::fs::dir("interop/src/build/web/cryptobox-esm/dist".to_string()));
98
99    warp::serve(warp_filter_cc.or(warp_filter_cbox).boxed())
100        .bind(addr)
101        .await;
102
103    Ok(())
104}