Skip to content

Commit 228b198

Browse files
committed
feat: provide total wasm size to the initializer
The initializer tries to get the total WASM size from the response. This can be done using the `Content-Length` header, but that approach has some downsides. First of all, it's not required and my be missing. Second, it will report the "on the wire" size of the response, the stream which already content encoded. If that includes compression, then there is no way to evaluate the total (uncompressed) size, or the currently read (compressed) bytes. So there is no way to sum up the bytes read (decoded, uncompressed) towards the total (compressed). On the other side, during the bundling process, we do know the total WASM size, and we also create hashes for it, so that we ensure its integrity. So we can also use that size as a total.
1 parent 10fd356 commit 228b198

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/pipelines/rust/initializer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
async function __trunkInitializer(source, initializer) {
1+
async function __trunkInitializer(init, source, sourceSize, initializer) {
22
if (initializer === undefined) {
33
return await init(source);
44
}
55

6-
return await __trunkInitWithProgress(source, initializer);
6+
return await __trunkInitWithProgress(init, source, sourceSize, initializer);
77
}
88

9-
async function __trunkInitWithProgress(source, initializer) {
9+
async function __trunkInitWithProgress(init, source, sourceSize, initializer) {
1010

1111
const {
1212
onStart, onProgress, onComplete, onSuccess, onFailure
@@ -21,7 +21,7 @@ async function __trunkInitWithProgress(source, initializer) {
2121
const status = response.status;
2222
const statusText = response.statusText;
2323

24-
const total = +response.headers.get("Content-Length");
24+
const total = sourceSize;
2525
let current = 0;
2626

2727
const stream = new ReadableStream({

src/pipelines/rust/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl RustApp {
574574

575575
tracing::debug!("copying {wasm_path} to {}", wasm_path_dist.display());
576576

577-
fs::copy(wasm_path, wasm_path_dist)
577+
fs::copy(wasm_path, &wasm_path_dist)
578578
.await
579579
.context("error copying wasm file to stage dir")?;
580580

@@ -659,6 +659,10 @@ impl RustApp {
659659
}
660660
}
661661

662+
// wasm size
663+
664+
let wasm_size = fs::metadata(&wasm_path_dist).await?.len();
665+
662666
// initializer
663667

664668
let initializer = match &self.initializer {
@@ -691,6 +695,7 @@ impl RustApp {
691695
cfg: self.cfg.clone(),
692696
js_output: hashed_js_name,
693697
wasm_output: hashed_wasm_name,
698+
wasm_size,
694699
ts_output,
695700
loader_shim_output: hashed_loader_name,
696701
r#type: self.app_type,

src/pipelines/rust/output.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub struct RustAppOutput {
1717
pub js_output: String,
1818
/// The filename of the generated WASM file written to the dist dir.
1919
pub wasm_output: String,
20+
/// The size of the WASM file
21+
pub wasm_size: u64,
2022
/// The filename of the generated .ts file written to the dist dir.
2123
pub ts_output: Option<String>,
2224
/// The filename of the generated loader shim script for web workers written to the dist dir.
@@ -140,11 +142,12 @@ init('{base}{wasm}');{bind}
140142
import init{import} from '{base}{js}';
141143
import initializer from '{base}{initializer}';
142144
143-
await __trunkInitializer('{base}{wasm}', initializer());
145+
await __trunkInitializer(init, '{base}{wasm}', {size}, initializer());
144146
145147
{bind}
146148
</script>"#,
147149
init = include_str!("initializer.js"),
150+
size = self.wasm_size,
148151
),
149152
}
150153
}

0 commit comments

Comments
 (0)