Skip to content

Commit 8de3071

Browse files
authored
Merge pull request #34 from nyurik/brotli-upd
Upgrade to Brotli v5
2 parents 26c1335 + 4044291 commit 8de3071

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ opt-level = 's'
1919

2020
[dependencies]
2121
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
22-
brotli = "3.3"
22+
brotli = "5.0"
2323
console_error_panic_hook = { version = "0.1", optional = true }
2424
serde = { version = "1.0", features = ["derive"] }
2525
serde_json = { version = "1.0" }

src/stream.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::set_panic_hook;
22
use brotli::enc::encode::{
3-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
4-
BrotliEncoderIsFinished, BrotliEncoderOperation, BrotliEncoderParameter,
5-
BrotliEncoderSetParameter, BrotliEncoderStateStruct,
3+
BrotliEncoderDestroyInstance, BrotliEncoderOperation, BrotliEncoderParameter,
4+
BrotliEncoderStateStruct,
65
};
76
use brotli::enc::StandardAlloc; // Re-exported from alloc_stdlib::StandardAlloc
87
use brotli::{self, BrotliDecompressStream, BrotliResult, BrotliState};
@@ -58,15 +57,11 @@ impl CompressStream {
5857
pub fn new(quality: Option<u32>) -> CompressStream {
5958
set_panic_hook();
6059
let alloc = StandardAlloc::default();
61-
let mut state = BrotliEncoderCreateInstance(alloc);
60+
let mut state = BrotliEncoderStateStruct::new(alloc);
6261
match quality {
6362
None => (),
6463
Some(quality) => {
65-
BrotliEncoderSetParameter(
66-
&mut state,
67-
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
68-
quality,
69-
);
64+
state.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, quality);
7065
}
7166
}
7267
Self {
@@ -97,8 +92,7 @@ impl CompressStream {
9792
// `BrotliEncoderCompressStream` does not return a `BrotliResult` but returns a boolean,
9893
// which is different from `BrotliDecompressStream`.
9994
// But the requirement for input/output buf is common so we reused `BrotliStreamResult` to report it.
100-
let ret = BrotliEncoderCompressStream(
101-
&mut self.state,
95+
if self.state.compress_stream(
10296
op,
10397
&mut available_in,
10498
&input,
@@ -108,8 +102,7 @@ impl CompressStream {
108102
&mut output_offset,
109103
&mut Some(self.total_out),
110104
&mut nop_callback,
111-
);
112-
if ret != 0 {
105+
) {
113106
if available_in == 0 {
114107
output.truncate(output_offset);
115108
Ok(BrotliStreamResult {
@@ -136,9 +129,8 @@ impl CompressStream {
136129
let op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH;
137130
let input = Vec::new().into_boxed_slice();
138131
let mut available_in = 0;
139-
while BrotliEncoderIsFinished(&mut self.state) == 0 && available_out > 0 {
140-
let ret = BrotliEncoderCompressStream(
141-
&mut self.state,
132+
while !self.state.is_finished() && available_out > 0 {
133+
if !self.state.compress_stream(
142134
op,
143135
&mut available_in,
144136
&input,
@@ -148,8 +140,7 @@ impl CompressStream {
148140
&mut output_offset,
149141
&mut Some(self.total_out),
150142
&mut nop_callback,
151-
);
152-
if ret == 0 {
143+
) {
153144
return Err(JsError::new(
154145
"Brotli streaming compress failed: When finishing",
155146
));

0 commit comments

Comments
 (0)