Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: missing asBuffer option in uncompress/uncompressSync
  • Loading branch information
Brooooooklyn committed Aug 11, 2021
1 parent c20f65d commit ac573f8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
14 changes: 12 additions & 2 deletions __test__/index.spec.ts
Expand Up @@ -12,12 +12,22 @@ test('compress should be async version of compressSync', async (t) => {
t.deepEqual(await compress(fixture), compressSync(fixture))
})

test('should be able to decompress sync', (t) => {
test('should be able to uncompress sync', (t) => {
const fixture = 'hello world 😂 🎧 🚀'
t.deepEqual(uncompressSync(compressSync(fixture)), Buffer.from(fixture))
})

test('should be able to decompress', async (t) => {
test('should be able to uncompress sync into string', (t) => {
const fixture = 'hello world 😂 🎧 🚀'
t.deepEqual(uncompressSync(compressSync(fixture), { asBuffer: false }), fixture)
})

test('should be able to uncompress', async (t) => {
const fixture = 'hello world 😂 🎧 🚀'
t.deepEqual(await uncompress(await compress(fixture)), Buffer.from(fixture))
})

test('should be able to uncompress into string', async (t) => {
const fixture = 'hello world 😂 🎧 🚀'
t.deepEqual(await uncompress(await compress(fixture), { asBuffer: false }), fixture)
})
6 changes: 6 additions & 0 deletions index.d.ts
@@ -1,4 +1,10 @@
export function compressSync(input: Buffer | string | ArrayBuffer | Uint8Array): Buffer
export function compress(input: Buffer | string | ArrayBuffer | Uint8Array): Promise<Buffer>
export function uncompressSync(compressed: Buffer): Buffer
export function uncompressSync(compressed: Buffer, opt: { asBuffer: true }): Buffer
export function uncompressSync(compressed: Buffer, opt: { asBuffer: false }): string
export function uncompressSync(compressed: Buffer, opt?: { asBuffer: boolean }): string | Buffer
export function uncompress(compressed: Buffer): Promise<Buffer>
export function uncompress(compressed: Buffer, opt: { asBuffer: true }): Promise<Buffer>
export function uncompress(compressed: Buffer, opt: { asBuffer: false }): Promise<string>
export function uncompress(compressed: Buffer, opt?: { asBuffer: boolean }): Promise<string | Buffer>
12 changes: 8 additions & 4 deletions index.js
Expand Up @@ -3,8 +3,8 @@ const { loadBinding } = require('@node-rs/helper')
const {
compressSync: _compressSync,
compress: _compress,
uncompress,
uncompressSync,
uncompress: _uncompress,
uncompressSync: _uncompressSync,
} = loadBinding(__dirname, 'snappy', '@napi-rs/snappy')

module.exports = {
Expand All @@ -14,6 +14,10 @@ module.exports = {
compress: function compress(input) {
return _compress(Buffer.from(input))
},
uncompress,
uncompressSync,
uncompress: function uncompress(input, opt = { asBuffer: true }) {
return _uncompress(input, Boolean(opt.asBuffer))
},
uncompressSync: function uncompressSync(input, opt = { asBuffer: true }) {
return _uncompressSync(input, Boolean(opt.asBuffer))
},
}
44 changes: 36 additions & 8 deletions src/lib.rs
Expand Up @@ -3,7 +3,12 @@
#[macro_use]
extern crate napi_derive;

use napi::{CallContext, Env, Error, JsBuffer, JsBufferValue, JsObject, Ref, Result, Status, Task};
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(
Expand Down Expand Up @@ -48,11 +53,12 @@ impl Task for Enc {
struct Dec {
inner: Decoder,
data: Ref<JsBufferValue>,
as_buffer: bool,
}

impl Task for Dec {
type Output = Vec<u8>;
type JsValue = JsBuffer;
type JsValue = JsUnknown;

fn compute(&mut self) -> Result<Self::Output> {
let data_ref: &[u8] = &self.data;
Expand All @@ -63,7 +69,15 @@ impl Task for Dec {
}

fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
env.create_buffer_with_data(output).map(|b| b.into_raw())
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())
}
}
}

Expand All @@ -89,24 +103,38 @@ fn compress(ctx: CallContext) -> Result<JsObject> {
ctx.env.spawn(encoder).map(|v| v.promise_object())
}

#[js_function(1)]
fn uncompress_sync(ctx: CallContext) -> Result<JsBuffer> {
#[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| ctx.env.create_buffer_with_data(d))
.map(|b| b.into_raw())
.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(1)]
#[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())
}

0 comments on commit ac573f8

Please sign in to comment.