Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use BufferSlice instead of JsBuffer #210

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare function compress(input: string | Buffer, options?: EncOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<Buffer>
export declare function compress(input: string | Uint8Array, options?: EncOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<Buffer>

export declare function compressSync(input: string | Buffer, options?: EncOptions | undefined | null): Buffer
export declare function compressSync(input: string | Uint8Array, options?: EncOptions | undefined | null): Buffer

export interface DecOptions {
asBuffer?: boolean
Expand Down Expand Up @@ -31,7 +31,7 @@ export interface EncOptions {
copyOutputData?: boolean
}

export declare function uncompress(input: string | Buffer, options?: DecOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<string | Buffer>
export declare function uncompress(input: string | Uint8Array, options?: DecOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<string | Buffer>

export declare function uncompressSync(input: string | Buffer, options?: DecOptions | undefined | null): string | Buffer
export declare function uncompressSync(input: string | Uint8Array, options?: DecOptions | undefined | null): string | Buffer

158 changes: 68 additions & 90 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
#[macro_use]
extern crate napi_derive;

use napi::{bindgen_prelude::*, JsBuffer, JsBufferValue, Ref};
use napi::bindgen_prelude::*;
use snap::raw::{Decoder, Encoder};

#[cfg(not(all(target_os = "linux", target_arch = "arm")))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

pub enum Data {
Buffer(Ref<JsBufferValue>),
String(String),
}

#[napi(object)]
pub struct DecOptions {
pub as_buffer: Option<bool>,
Expand All @@ -34,34 +29,23 @@ pub struct EncOptions {
pub copy_output_data: Option<bool>,
}

impl TryFrom<Either<String, JsBuffer>> for Data {
type Error = Error;

fn try_from(value: Either<String, JsBuffer>) -> Result<Self> {
match value {
Either::A(s) => Ok(Data::String(s)),
Either::B(b) => Ok(Data::Buffer(b.into_ref()?)),
}
}
}

pub struct Enc {
inner: Encoder,
data: Data,
data: Either<String, Uint8Array>,
options: Option<EncOptions>,
}

#[napi]
impl Task for Enc {
type Output = Vec<u8>;
type JsValue = JsBuffer;
type JsValue = Buffer;

fn compute(&mut self) -> Result<Self::Output> {
self
.inner
.compress_vec(match self.data {
Data::Buffer(ref b) => b.as_ref(),
Data::String(ref s) => s.as_bytes(),
Either::A(ref b) => b.as_bytes(),
Either::B(ref s) => s.as_ref(),
})
.map_err(|e| Error::new(Status::GenericFailure, format!("{e}")))
}
Expand All @@ -73,38 +57,31 @@ impl Task for Enc {
.and_then(|o| o.copy_output_data)
.unwrap_or(false)
{
env.create_buffer_copy(output)
BufferSlice::copy_from(&env, output)
} else {
env.create_buffer_with_data(output)
}
.map(|b| b.into_raw())
}

fn finally(&mut self, env: Env) -> Result<()> {
if let Data::Buffer(b) = &mut self.data {
b.unref(env)?;
BufferSlice::from_data(&env, output)
}
Ok(())
.and_then(|s| s.into_buffer(&env))
}
}

pub struct Dec {
inner: Decoder,
data: Data,
data: Either<String, Uint8Array>,
options: Option<DecOptions>,
}

#[napi]
impl Task for Dec {
type Output = Vec<u8>;
type JsValue = Either<String, JsBuffer>;
type JsValue = Either<String, Buffer>;

fn compute(&mut self) -> Result<Self::Output> {
self
.inner
.decompress_vec(match self.data {
Data::Buffer(ref b) => b.as_ref(),
Data::String(ref s) => s.as_bytes(),
Either::A(ref s) => s.as_bytes(),
Either::B(ref b) => b.as_ref(),
})
.map_err(|e| Error::new(Status::GenericFailure, format!("{e}")))
}
Expand All @@ -113,64 +90,58 @@ impl Task for Dec {
let opt_ref = self.options.as_ref();
if opt_ref.and_then(|o| o.as_buffer).unwrap_or(true) {
if opt_ref.and_then(|o| o.copy_output_data).unwrap_or(false) {
Ok(Either::B(
env.create_buffer_copy(output).map(|o| o.into_raw())?,
))
BufferSlice::copy_from(&env, output)
.and_then(|slice| slice.into_buffer(&env))
.map(Either::B)
} else {
Ok(Either::B(
env.create_buffer_with_data(output).map(|o| o.into_raw())?,
))
BufferSlice::from_data(&env, output)
.and_then(|slice| slice.into_buffer(&env))
.map(Either::B)
}
} else {
Ok(Either::A(String::from_utf8(output).map_err(|e| {
Error::new(Status::GenericFailure, format!("{e}"))
})?))
}
}

fn finally(&mut self, env: Env) -> Result<()> {
if let Data::Buffer(b) = &mut self.data {
b.unref(env)?;
}
Ok(())
}
}

#[napi]
pub fn compress_sync(
env: Env,
input: Either<String, JsBuffer>,
input: Either<String, &[u8]>,
options: Option<EncOptions>,
) -> Result<JsBuffer> {
let enc = Encoder::new();
let mut encoder = Enc {
inner: enc,
data: Data::try_from(input)?,
options,
};
match encoder.compute() {
Ok(output) => {
let ret = encoder.resolve(env, output);
encoder.finally(env)?;
ret
}
Err(err) => {
encoder.finally(env)?;
Err(err)
}
}
) -> Result<BufferSlice> {
let mut enc = Encoder::new();
enc
.compress_vec(match input {
Either::A(ref s) => s.as_bytes(),
Either::B(b) => b,
})
.map_err(|err| Error::new(Status::GenericFailure, format!("{err}")))
.and_then(|output| {
if options
.as_ref()
.and_then(|o| o.copy_output_data)
.unwrap_or(false)
{
BufferSlice::copy_from(&env, output)
} else {
BufferSlice::from_data(&env, output)
}
})
}

#[napi]
pub fn compress(
input: Either<String, JsBuffer>,
input: Either<String, Uint8Array>,
options: Option<EncOptions>,
signal: Option<AbortSignal>,
) -> Result<AsyncTask<Enc>> {
let enc = Encoder::new();
let encoder = Enc {
inner: enc,
data: Data::try_from(input)?,
data: input,
options,
};
match signal {
Expand All @@ -182,38 +153,45 @@ pub fn compress(
#[napi]
pub fn uncompress_sync(
env: Env,
input: Either<String, JsBuffer>,
input: Either<String, &[u8]>,
options: Option<DecOptions>,
) -> Result<Either<String, JsBuffer>> {
let dec = Decoder::new();
let mut decoder = Dec {
inner: dec,
data: Data::try_from(input)?,
options,
};
match decoder.compute() {
Ok(output) => {
let ret = decoder.resolve(env, output);
decoder.finally(env)?;
ret
}
Err(err) => {
decoder.finally(env)?;
Err(err)
}
}
) -> Result<Either<String, BufferSlice>> {
let mut dec = Decoder::new();
dec
.decompress_vec(match input {
Either::A(ref s) => s.as_bytes(),
Either::B(b) => b,
})
.map_err(|err| Error::new(Status::GenericFailure, format!("{err}")))
.and_then(|output| {
if options.as_ref().and_then(|o| o.as_buffer).unwrap_or(true) {
if options
.as_ref()
.and_then(|o| o.copy_output_data)
.unwrap_or(false)
{
BufferSlice::copy_from(&env, output).map(Either::B)
} else {
BufferSlice::from_data(&env, output).map(Either::B)
}
} else {
Ok(Either::A(String::from_utf8(output).map_err(|e| {
Error::new(Status::GenericFailure, format!("{e}"))
})?))
}
})
}

#[napi]
pub fn uncompress(
input: Either<String, JsBuffer>,
input: Either<String, Uint8Array>,
options: Option<DecOptions>,
signal: Option<AbortSignal>,
) -> Result<AsyncTask<Dec>> {
let dec = Decoder::new();
let decoder = Dec {
inner: dec,
data: Data::try_from(input)?,
data: input,
options,
};
Ok(AsyncTask::with_optional_signal(decoder, signal))
Expand Down