Skip to content

Commit 006b373

Browse files
committed
feat: use base64 encoding to speed up loading
1 parent 8824341 commit 006b373

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

Diff for: crates/cli-support/src/js/file_util.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use base64::{prelude::BASE64_STANDARD_NO_PAD, Engine};
2+
3+
pub(crate) fn create_load_inline_bytes_snippet(bytes: &[u8], variable_name: String) -> String {
4+
format!(
5+
"
6+
let {variable_name};
7+
const base64 = \"{base64}\";
8+
if (typeof Buffer === 'undefined') {{
9+
{variable_name} = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
10+
}} else {{
11+
{variable_name} = Buffer.from(base64, 'base64');
12+
}}
13+
",
14+
variable_name = variable_name,
15+
base64 = BASE64_STANDARD_NO_PAD.encode(bytes),
16+
)
17+
}

Diff for: crates/cli-support/src/js/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::wit::{JsImport, JsImportName, NonstandardWitSection, WasmBindgenAux};
1010
use crate::{reset_indentation, Bindgen, EncodeInto, OutputMode, PLACEHOLDER_MODULE};
1111
use anyhow::{anyhow, bail, Context as _, Error};
1212
use binding::TsReference;
13+
use file_util::create_load_inline_bytes_snippet;
1314
use std::borrow::Cow;
1415
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
1516
use std::fmt;
@@ -19,6 +20,7 @@ use std::path::{Path, PathBuf};
1920
use walrus::{FunctionId, ImportId, MemoryId, Module, TableId, ValType};
2021

2122
mod binding;
23+
pub(crate) mod file_util;
2224

2325
pub struct Context<'a> {
2426
globals: String,
@@ -308,19 +310,14 @@ impl<'a> Context<'a> {
308310
fn generate_inline_wasm_loading(&mut self) -> String {
309311
let mut shim = String::new();
310312

311-
let buf = self.module.emit_wasm();
313+
let wasm = self.module.emit_wasm();
314+
315+
let serialized = create_load_inline_bytes_snippet(&wasm, "bytes".into());
312316

313-
let mut serialized = "const bytes = new Uint8Array([".to_string();
314-
let (last, bytes) = buf.split_last().unwrap();
315-
for byte in bytes {
316-
serialized.push_str(&format!("{},", byte));
317-
}
318-
serialized.push_str(&format!("{}", last));
319-
serialized.push_str("]);");
320317
shim.push_str(&serialized);
321318
shim.push_str(
322319
"
323-
const wasmModule = new WebAssembly.Module(bytes.buffer);
320+
const wasmModule = new WebAssembly.Module(bytes);
324321
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
325322
wasm = wasmInstance.exports;
326323
module.exports.__wasm = wasm;

Diff for: crates/cli-support/src/wasm2es6js.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use anyhow::{bail, Error};
2-
use base64::{prelude::BASE64_STANDARD, Engine as _};
32
use std::collections::HashSet;
43
use std::fmt::Write;
54
use walrus::Module;
65

6+
use crate::js::file_util::create_load_inline_bytes_snippet;
7+
78
pub struct Config {
89
base64: bool,
910
fetch_path: Option<String>,
@@ -232,18 +233,7 @@ impl Output {
232233
let wasm = self.module.emit_wasm();
233234
let (bytes, booted) = if self.base64 {
234235
(
235-
format!(
236-
"
237-
let bytes;
238-
const base64 = \"{base64}\";
239-
if (typeof Buffer === 'undefined') {{
240-
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
241-
}} else {{
242-
bytes = Buffer.from(base64, 'base64');
243-
}}
244-
",
245-
base64 = BASE64_STANDARD.encode(&wasm)
246-
),
236+
create_load_inline_bytes_snippet(&wasm, "bytes".into()),
247237
inst,
248238
)
249239
} else if let Some(ref path) = self.fetch_path {

0 commit comments

Comments
 (0)