-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlib.rs
70 lines (55 loc) · 1.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pub mod stream;
use brotli;
use wasm_bindgen::prelude::*;
use serde::{Serialize, Deserialize};
pub fn set_panic_hook() {
#[cfg(feature="console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
type Options = {
quality?: number
};
export function compress(buf: Uint8Array, options?: Options): Uint8Array;
"#;
#[derive(Serialize, Deserialize)]
pub struct Options {
#[serde(default = "default_quality")]
pub quality: i32
}
fn default_quality() -> i32 { 11 }
#[wasm_bindgen(js_name = compress, skip_typescript)]
pub fn compress(buf: Box<[u8]>, raw_options: &JsValue) -> Result<Box<[u8]>, JsValue> {
set_panic_hook();
let options: Options;
if raw_options.is_undefined() {
options = serde_json::from_str("{}").unwrap();
} else if raw_options.is_object() {
options = raw_options.into_serde().unwrap();
} else {
return Err(JsValue::from_str("Options is not an object"));
}
let mut out = Vec::<u8>::new();
let mut params = brotli::enc::BrotliEncoderParams::default();
params.quality = options.quality;
match brotli::BrotliCompress(&mut buf.as_ref(), &mut out, ¶ms) {
Ok(_) => (),
Err(e) => return Err(JsValue::from_str(&format!(
"Brotli compress failed: {:?}", e
))),
}
Ok(out.into_boxed_slice())
}
#[wasm_bindgen(js_name = decompress)]
pub fn decompress(buf: Box<[u8]>) -> Result<Box<[u8]>, JsValue> {
set_panic_hook();
let mut out = Vec::<u8>::new();
match brotli::BrotliDecompress(&mut buf.as_ref(), &mut out) {
Ok(_) => (),
Err(e) => return Err(JsValue::from_str(&format!(
"Brotli decompress failed: {:?}", e
))),
}
Ok(out.into_boxed_slice())
}