Skip to content

Commit c2e51c9

Browse files
committed
fix(wasm): serialize doc_bytes as Uint8Array instead of Array<number>
serde_wasm_bindgen serializes Vec<u8> via serialize_seq, producing a plain JS Array of numbers. When passed to new Blob([bytes]), the array is stringified rather than treated as binary, producing corrupt PDFs. Use serde_bytes to route through serialize_bytes for redact/sanitize responses, and construct Uint8Array directly via js_sys for split.
1 parent 1859b6f commit c2e51c9

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

crates/paperjam-wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ crate-type = ["cdylib"]
1212
paperjam-core = { path = "../paperjam-core", default-features = false, features = ["signatures", "validation"] }
1313
wasm-bindgen = "0.2"
1414
serde = { version = "1.0", features = ["derive"] }
15+
serde_bytes = "0.11"
1516
serde-wasm-bindgen = "0.6"
1617
js-sys = "0.3"
1718
getrandom_v02 = { package = "getrandom", version = "0.2", features = ["js"] }

crates/paperjam-wasm/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,16 @@ impl WasmDocument {
354354
let ranges: Vec<(u32, u32)> = serde_wasm_bindgen::from_value(ranges)
355355
.map_err(|e| JsValue::from_str(&e.to_string()))?;
356356
let docs = paperjam_core::manipulation::split(&self.inner, &ranges).map_err(to_js_err)?;
357-
let mut byte_arrays: Vec<Vec<u8>> = Vec::new();
357+
let result = js_sys::Array::new();
358358
for doc in docs {
359359
let mut inner = doc.into_inner();
360360
let mut buf = Vec::new();
361361
inner
362362
.save_to(&mut buf)
363363
.map_err(|e| JsValue::from_str(&e.to_string()))?;
364-
byte_arrays.push(buf);
364+
result.push(&js_sys::Uint8Array::from(buf.as_slice()));
365365
}
366-
serde_wasm_bindgen::to_value(&byte_arrays).map_err(|e| JsValue::from_str(&e.to_string()))
366+
Ok(result.into())
367367
}
368368

369369
/// Sanitize the document by removing potentially dangerous content.
@@ -396,6 +396,7 @@ impl WasmDocument {
396396
};
397397
#[derive(Serialize)]
398398
struct SanitizeResponse {
399+
#[serde(with = "serde_bytes")]
399400
doc_bytes: Vec<u8>,
400401
result: SanitizeResultJs,
401402
}
@@ -432,6 +433,7 @@ impl WasmDocument {
432433
};
433434
#[derive(Serialize)]
434435
struct RedactResponse {
436+
#[serde(with = "serde_bytes")]
435437
doc_bytes: Vec<u8>,
436438
result: RedactResultJs,
437439
}

0 commit comments

Comments
 (0)