generated from napi-rs/package-template
/
lib.rs
112 lines (96 loc) · 2.85 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![deny(clippy::all)]
#[macro_use]
extern crate napi_derive;
use napi::{CallContext, Env, Error, JsBuffer, JsBufferValue, JsObject, Ref, Result, Status, Task};
use snap::raw::{Decoder, Encoder};
#[cfg(all(
target_arch = "x86_64",
not(target_env = "musl"),
not(debug_assertions)
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[module_exports]
fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("compressSync", compress_sync)?;
exports.create_named_method("compress", compress)?;
exports.create_named_method("uncompressSync", uncompress_sync)?;
exports.create_named_method("uncompress", uncompress)?;
Ok(())
}
struct Enc {
inner: Encoder,
data: Ref<JsBufferValue>,
}
impl Task for Enc {
type Output = Vec<u8>;
type JsValue = JsBuffer;
fn compute(&mut self) -> Result<Self::Output> {
let data_ref: &[u8] = &self.data;
self
.inner
.compress_vec(data_ref)
.map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
}
fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
env.create_buffer_with_data(output).map(|b| b.into_raw())
}
}
struct Dec {
inner: Decoder,
data: Ref<JsBufferValue>,
}
impl Task for Dec {
type Output = Vec<u8>;
type JsValue = JsBuffer;
fn compute(&mut self) -> Result<Self::Output> {
let data_ref: &[u8] = &self.data;
self
.inner
.decompress_vec(data_ref)
.map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
}
fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
env.create_buffer_with_data(output).map(|b| b.into_raw())
}
}
#[js_function(1)]
fn compress_sync(ctx: CallContext) -> Result<JsBuffer> {
let data = ctx.get::<JsBuffer>(0)?;
let mut enc = Encoder::new();
enc
.compress_vec(&data.into_value()?)
.map_err(|e| Error::new(napi::Status::GenericFailure, format!("{}", e)))
.and_then(|d| ctx.env.create_buffer_with_data(d))
.map(|b| b.into_raw())
}
#[js_function(1)]
fn compress(ctx: CallContext) -> Result<JsObject> {
let data = ctx.get::<JsBuffer>(0)?;
let enc = Encoder::new();
let encoder = Enc {
inner: enc,
data: data.into_ref()?,
};
ctx.env.spawn(encoder).map(|v| v.promise_object())
}
#[js_function(1)]
fn uncompress_sync(ctx: CallContext) -> Result<JsBuffer> {
let data = ctx.get::<JsBuffer>(0)?;
let mut dec = Decoder::new();
dec
.decompress_vec(&data.into_value()?)
.map_err(|e| Error::new(napi::Status::GenericFailure, format!("{}", e)))
.and_then(|d| ctx.env.create_buffer_with_data(d))
.map(|b| b.into_raw())
}
#[js_function(1)]
fn uncompress(ctx: CallContext) -> Result<JsObject> {
let data = ctx.get::<JsBuffer>(0)?;
let dec = Decoder::new();
let decoder = Dec {
inner: dec,
data: data.into_ref()?,
};
ctx.env.spawn(decoder).map(|v| v.promise_object())
}