Skip to content

Commit

Permalink
Merge pull request #34 from nyurik/brotli-upd
Browse files Browse the repository at this point in the history
Upgrade to Brotli v5
  • Loading branch information
pimterry authored Apr 18, 2024
2 parents 26c1335 + 4044291 commit 8de3071
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ opt-level = 's'

[dependencies]
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
brotli = "3.3"
brotli = "5.0"
console_error_panic_hook = { version = "0.1", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
27 changes: 9 additions & 18 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::set_panic_hook;
use brotli::enc::encode::{
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
BrotliEncoderIsFinished, BrotliEncoderOperation, BrotliEncoderParameter,
BrotliEncoderSetParameter, BrotliEncoderStateStruct,
BrotliEncoderDestroyInstance, BrotliEncoderOperation, BrotliEncoderParameter,
BrotliEncoderStateStruct,
};
use brotli::enc::StandardAlloc; // Re-exported from alloc_stdlib::StandardAlloc
use brotli::{self, BrotliDecompressStream, BrotliResult, BrotliState};
Expand Down Expand Up @@ -58,15 +57,11 @@ impl CompressStream {
pub fn new(quality: Option<u32>) -> CompressStream {
set_panic_hook();
let alloc = StandardAlloc::default();
let mut state = BrotliEncoderCreateInstance(alloc);
let mut state = BrotliEncoderStateStruct::new(alloc);
match quality {
None => (),
Some(quality) => {
BrotliEncoderSetParameter(
&mut state,
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
quality,
);
state.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, quality);
}
}
Self {
Expand Down Expand Up @@ -97,8 +92,7 @@ impl CompressStream {
// `BrotliEncoderCompressStream` does not return a `BrotliResult` but returns a boolean,
// which is different from `BrotliDecompressStream`.
// But the requirement for input/output buf is common so we reused `BrotliStreamResult` to report it.
let ret = BrotliEncoderCompressStream(
&mut self.state,
if self.state.compress_stream(
op,
&mut available_in,
&input,
Expand All @@ -108,8 +102,7 @@ impl CompressStream {
&mut output_offset,
&mut Some(self.total_out),
&mut nop_callback,
);
if ret != 0 {
) {
if available_in == 0 {
output.truncate(output_offset);
Ok(BrotliStreamResult {
Expand All @@ -136,9 +129,8 @@ impl CompressStream {
let op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH;
let input = Vec::new().into_boxed_slice();
let mut available_in = 0;
while BrotliEncoderIsFinished(&mut self.state) == 0 && available_out > 0 {
let ret = BrotliEncoderCompressStream(
&mut self.state,
while !self.state.is_finished() && available_out > 0 {
if !self.state.compress_stream(
op,
&mut available_in,
&input,
Expand All @@ -148,8 +140,7 @@ impl CompressStream {
&mut output_offset,
&mut Some(self.total_out),
&mut nop_callback,
);
if ret == 0 {
) {
return Err(JsError::new(
"Brotli streaming compress failed: When finishing",
));
Expand Down

0 comments on commit 8de3071

Please sign in to comment.