interop/build/web/
wasm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Wire
// Copyright (C) 2022 Wire Swiss GmbH

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

use crate::util::RunningProcess;
use crate::TEST_SERVER_PORT;
use color_eyre::eyre::Result;
use std::net::SocketAddr;

async fn find_wasm_file(deploy_path: &std::path::Path) -> Result<std::path::PathBuf> {
    let wasm_base_path = deploy_path.to_path_buf();
    let wasm_path = if wasm_base_path.exists() {
        let mut wasm_dir = tokio::fs::read_dir(wasm_base_path.clone()).await?;
        let mut maybe_wasm_path = None;
        while let Some(entry) = wasm_dir.next_entry().await? {
            log::debug!("wasm dir entry: {entry:?}");
            if let Some(ext) = entry.path().extension() {
                if ext == std::ffi::OsStr::new("wasm") {
                    log::debug!("found");
                    maybe_wasm_path = Some(entry);
                }
            }
        }
        maybe_wasm_path
            .map(|entry| entry.path())
            .unwrap_or_else(|| wasm_base_path.join(".not-found"))
    } else {
        wasm_base_path.join(".not-found")
    };

    Ok(wasm_path)
}

pub(crate) async fn build_wasm() -> Result<()> {
    use sha2::{Digest, Sha256};
    use tokio::process::Command;

    let cwd = std::env::current_dir()?;

    if cfg!(feature = "proteus") {
        let spinner = RunningProcess::new("Building Cryptobox ESM bundle...", false);

        Command::new("bun")
            .args(["install"])
            .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .await?;

        Command::new("bun")
            .args(["run", "build"])
            .current_dir(cwd.join("interop/src/build/web/cryptobox-esm"))
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .await?;

        spinner.success("Cryptobox ESM bundle [OK]");
    }

    let mut spinner = RunningProcess::new("Building WASM bundle...", false);

    let wasm_deploy_path = cwd.join("platforms/web");

    let exe_path = std::env::current_exe()?;
    let exe_folder = exe_path.parent().unwrap();
    let wasm_cache_path = exe_folder.join(".wasm.cache");
    let js_cache_path = exe_folder.join(".js.cache");

    let wasm_path = find_wasm_file(&wasm_deploy_path).await?;
    let js_path = wasm_deploy_path.join("corecrypto.js");

    std::fs::copy(
        cwd.join("crypto-ffi/bindings/js/test/index.html"),
        wasm_deploy_path.join("index.html"),
    )?;

    if !wasm_path.exists() || !js_path.exists() {
        spinner.update("WASM: No JS/WASM files found, rebuilding; Please wait...");
    } else if wasm_cache_path.exists() && js_cache_path.exists() {
        let wasm_hash = hex::decode(tokio::fs::read_to_string(wasm_cache_path.clone()).await?)?;
        let js_hash = hex::decode(tokio::fs::read_to_string(js_cache_path.clone()).await?)?;

        let mut hasher = Sha256::new();
        hasher.update(tokio::fs::read(js_path.clone()).await?);
        let js_current_hash = hasher.finalize();

        let mut hasher = Sha256::new();
        hasher.update(tokio::fs::read(wasm_path.clone()).await?);
        let wasm_current_hash = hasher.finalize();

        if js_current_hash[..] == js_hash && wasm_current_hash[..] == wasm_hash {
            spinner.success("WASM: builds are identical - skipping build");
            return Ok(());
        }

        spinner.update("WASM: Hashes differ, needs rebuild! Please wait...");
    } else {
        spinner.update("WASM: No cache file found, rebuilding to get a cache! Please wait...");
    }

    let mut cargo_args = vec!["make", "wasm"];
    let mut bun_env = vec![];

    if cfg!(feature = "proteus") {
        spinner.update(
            "`proteus` feature enabled. Building `core-crypto` with proteus support & enabling bun env BUILD_PROTEUS=1; Also building ESM bundle for Cryptobox",
        );
        cargo_args.push("--features");
        cargo_args.push("proteus");
        bun_env.push(("BUILD_PROTEUS", "1"));
    }

    Command::new("cargo")
        .args(&cargo_args)
        .current_dir(cwd.join("crypto-ffi"))
        .stdout(std::process::Stdio::null())
        .status()
        .await?;

    Command::new("bun")
        .args(["run", "wdio"])
        .envs(bun_env.clone())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .await?;

    spinner.update("WASM: Computing new file hashes...");

    let mut hasher = Sha256::new();
    hasher.update(tokio::fs::read(js_path).await?);
    let js_current_hash = hex::encode(hasher.finalize());

    let wasm_path = find_wasm_file(&wasm_deploy_path).await?;
    log::debug!("Found wasm file at {wasm_path:?}");

    let mut hasher = Sha256::new();
    hasher.update(tokio::fs::read(wasm_path).await?);
    let wasm_current_hash = hex::encode(hasher.finalize());

    log::debug!("Cache updated; CoreCrypto.wasm[{wasm_current_hash}] | corecrypto.js[{js_current_hash}]");
    log::debug!("JS Cache Path: {js_cache_path:?} | WASM Cache Path: {wasm_cache_path:?}");

    tokio::fs::write(js_cache_path, js_current_hash).await?;
    tokio::fs::write(wasm_cache_path, wasm_current_hash).await?;

    spinner.success("WASM bundle [OK]");

    Ok(())
}

pub(crate) async fn spawn_http_server() -> Result<()> {
    use warp::Filter as _;
    let addr = SocketAddr::from(([0, 0, 0, 0], TEST_SERVER_PORT.parse()?));
    let warp_filter_cc = warp::path("core-crypto").and(warp::fs::dir("platforms/web".to_string()));
    let warp_filter_cbox =
        warp::path("cryptobox").and(warp::fs::dir("interop/src/build/web/cryptobox-esm/dist".to_string()));

    warp::serve(warp_filter_cc.or(warp_filter_cbox).boxed())
        .bind(addr)
        .await;

    Ok(())
}