generated from napi-rs/package-template
/
lib.rs
140 lines (123 loc) · 3.57 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![deny(clippy::all)]
#[macro_use]
extern crate napi_derive;
use std::ffi::CString;
use napi::{
CallContext, Env, Error, JsBoolean, JsBuffer, JsBufferValue, JsObject, JsUnknown, 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>,
as_buffer: bool,
}
impl Task for Dec {
type Output = Vec<u8>;
type JsValue = JsUnknown;
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> {
if self.as_buffer {
env
.create_buffer_with_data(output)
.map(|b| b.into_raw().into_unknown())
} else {
let len = output.len();
let c_string = CString::new(output)?;
unsafe { env.create_string_from_c_char(c_string.as_ptr(), len) }.map(|v| v.into_unknown())
}
}
}
#[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(2)]
fn uncompress_sync(ctx: CallContext) -> Result<JsUnknown> {
let data = ctx.get::<JsBuffer>(0)?;
let as_buffer = ctx.get::<JsBoolean>(1)?.get_value()?;
let mut dec = Decoder::new();
dec
.decompress_vec(&data.into_value()?)
.map_err(|e| Error::new(napi::Status::GenericFailure, format!("{}", e)))
.and_then(|d| {
if as_buffer {
ctx
.env
.create_buffer_with_data(d)
.map(|b| b.into_raw().into_unknown())
} else {
let len = d.len();
let c_string = CString::new(d)?;
unsafe { ctx.env.create_string_from_c_char(c_string.as_ptr(), len) }
.map(|v| v.into_unknown())
}
})
}
#[js_function(2)]
fn uncompress(ctx: CallContext) -> Result<JsObject> {
let data = ctx.get::<JsBuffer>(0)?;
let as_buffer = ctx.get::<JsBoolean>(1)?.get_value()?;
let dec = Decoder::new();
let decoder = Dec {
inner: dec,
data: data.into_ref()?,
as_buffer,
};
ctx.env.spawn(decoder).map(|v| v.promise_object())
}