Skip to content

Commit c44f9e6

Browse files
committed
Use ruzstd to decompress in Wasm
Allow decompressing bytecodes when running in `wasm32-unknown-unknown`.
1 parent 7d88afb commit c44f9e6

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ quote = "1.0"
108108
rand = { version = "0.8.5", default-features = false }
109109
rand_chacha = { version = "0.3.1", default-features = false }
110110
rand_distr = { version = "0.4.3", default-features = false }
111+
ruzstd = "0.7.1"
111112
k8s-openapi = { version = "0.21.1", features = ["v1_28"] }
112113
pathdiff = "0.2.1"
113114
kube = "0.88.1"

examples/Cargo.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

linera-base/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ tracing-subscriber = { workspace = true, features = ["json", "fmt", "ansi"] }
5656
wasm-bindgen-futures = { workspace = true, optional = true }
5757
web-time = { workspace = true, optional = true }
5858

59+
[target.'cfg(target_arch = "wasm32")'.dependencies]
60+
ruzstd.workspace = true
61+
5962
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
6063
chrono.workspace = true
6164
rand = { workspace = true, features = ["getrandom", "std", "std_rng"] }

linera-base/src/data_types.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,14 @@ impl fmt::Debug for Bytecode {
843843
#[derive(Error, Debug)]
844844
pub enum DecompressionError {
845845
/// Compressed bytecode is invalid, and could not be decompressed.
846+
#[cfg(not(target_arch = "wasm32"))]
846847
#[error("Bytecode could not be decompressed")]
847848
InvalidCompressedBytecode(#[source] io::Error),
849+
850+
/// Compressed bytecode is invalid, and could not be decompressed.
851+
#[cfg(target_arch = "wasm32")]
852+
#[error("Bytecode could not be decompressed")]
853+
InvalidCompressedBytecode(#[from] ruzstd::frame_decoder::FrameDecoderError),
848854
}
849855

850856
/// A compressed WebAssembly module's bytecode.
@@ -884,7 +890,29 @@ impl TryFrom<&CompressedBytecode> for Bytecode {
884890
}
885891
}
886892

887-
#[cfg(not(target_arch = "wasm32"))]
893+
#[cfg(target_arch = "wasm32")]
894+
impl TryFrom<&CompressedBytecode> for Bytecode {
895+
type Error = DecompressionError;
896+
897+
fn try_from(compressed_bytecode: &CompressedBytecode) -> Result<Self, Self::Error> {
898+
use ruzstd::{io::Read, streaming_decoder::StreamingDecoder};
899+
900+
let compressed_bytes = &*compressed_bytecode.compressed_bytes;
901+
let mut bytes = Vec::new();
902+
let mut decoder = StreamingDecoder::new(compressed_bytes)?;
903+
904+
// Decode multiple frames, if present
905+
// (https://github.com/KillingSpark/zstd-rs/issues/57)
906+
while !decoder.get_ref().is_empty() {
907+
decoder
908+
.read_to_end(&mut bytes)
909+
.expect("Reading from a slice in memory should not result in IO errors");
910+
}
911+
912+
Ok(Bytecode { bytes })
913+
}
914+
}
915+
888916
impl TryFrom<CompressedBytecode> for Bytecode {
889917
type Error = DecompressionError;
890918

0 commit comments

Comments
 (0)