Skip to content

Commit

Permalink
feat(NODE-1837): support passing compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 6, 2022
1 parent 5577fc8 commit 2c1a917
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

/* auto-generated by NAPI-RS */

export function compress(data: Buffer): Promise<Buffer>
export function compress(data: Buffer, level?: number | undefined | null): Promise<Buffer>
export function decompress(data: Buffer): Promise<Buffer>
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use napi::{
};
use zstd::stream::{encode_all, decode_all};

const DEFAULT_LEVEL: i32 = 3;

struct Encoder {
data: Ref<JsBufferValue>
data: Ref<JsBufferValue>,
level: i32
}

#[napi]
Expand All @@ -18,7 +21,7 @@ impl Task for Encoder {

fn compute(&mut self) -> Result<Self::Output> {
let data: &[u8] = self.data.as_ref();
encode_all(data, 3).map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
encode_all(data, self.level).map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
}

fn resolve(&mut self, env: Env, output: Self::Output) -> Result<JsBuffer> {
Expand Down Expand Up @@ -56,9 +59,10 @@ impl Task for Decoder {
}

#[napi]
fn compress(data: JsBuffer) -> Result<AsyncTask<Encoder>> {
fn compress(data: JsBuffer, level: Option<i32>) -> Result<AsyncTask<Encoder>> {
let encoder = Encoder {
data: data.into_ref()?,
level: level.unwrap_or(DEFAULT_LEVEL)
};
Ok(AsyncTask::new(encoder))
}
Expand Down
33 changes: 30 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@ describe('zstd', () => {
describe('#compress', () => {
const buffer = Buffer.from('test');

it('returns a compressed buffer', async () => {
const result = await compress(buffer);
expect(await decompress(result)).to.deep.equal(buffer);
context('when not providing a compression level', () => {
it('returns a compressed buffer', async () => {
const result = await compress(buffer);
expect(await decompress(result)).to.deep.equal(buffer);
});
});

context('when providing a compression level', () => {
context('when the level is valid', () => {
it('returns a compressed buffer', async () => {
const result = await compress(buffer, 1);
expect(await decompress(result)).to.deep.equal(buffer);
});
});

context('when the level is invalid', () => {
context('when the level is too high', () => {
it('returns a compressed buffer', async () => {
const result = await compress(buffer, 100);
expect(await decompress(result)).to.deep.equal(buffer);
});
});

context('when the level is too low', () => {
it('returns a compressed buffer', async () => {
const result = await compress(buffer, -100);
expect(await decompress(result)).to.deep.equal(buffer);
});
});
});
});
});
});

0 comments on commit 2c1a917

Please sign in to comment.