Skip to content

Commit

Permalink
feat: added exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Nov 26, 2021
1 parent b2260c7 commit 4fd604c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
9 changes: 4 additions & 5 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { compress, decompress, compress_sync, decompress_sync } from '../index.js'
import { compress, uncompress, compressSync, uncompressSync } from '../index.js'

const stringToCompress = 'adewqeqweqwewleekqwoekqwoekqwpoekqwpoekqwpoekqwpoekqwpoekqwpokeeqw'

Expand All @@ -14,15 +14,14 @@ test('compress decompress should work', async (t) => {
const before = Buffer.from(stringToCompress)
const compressed = await compress(before)
t.true(before.length > compressed.length)
const decompressed = await decompress(compressed)
const decompressed = await uncompress(compressed)
t.is(before.toString('utf8'), decompressed.toString('utf8'))
})


test('compress decompress sync should work', (t) => {
const before = Buffer.from(stringToCompress)
const compressed = compress_sync(before)
const compressed = compressSync(before)
t.true(before.length > compressed.length)
const decompressed = decompress_sync(compressed)
const decompressed = uncompressSync(compressed)
t.is(before.toString('utf8'), decompressed.toString('utf8'))
})
2 changes: 1 addition & 1 deletion benchmark/bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import b from 'benny'

import { sync } from '../index'
import { uncompress, compress, } from '../index'

function add(a: number) {
return a + 100
Expand Down
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const sync: (input: number) => number
// sleep [duration] ms, return Promise which resolved 2 * duration
export const sleep: (duration: number) => Promise<number>
export const compress: (data: Buffer | string) => Promise<Buffer>
export const uncompress: (data: Buffer) => Promise<Buffer>
export const compressSync: (data: Buffer) => Buffer
export const uncompressSync: (data: Buffer) => Buffer
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,25 @@ const { loadBinding } = require('@node-rs/helper')
* loadBinding helper will load `package-template.[PLATFORM].node` from `__dirname` first
* If failed to load addon, it will fallback to load from `@napi-rs/package-template-[PLATFORM]`
*/
module.exports = loadBinding(__dirname, 'package-template', '@napi-rs/package-template')
const {
compress: _compress,
uncompress: _uncompress,
compress_sync,
uncompress_sync,
} = loadBinding(__dirname, 'package-template', '@napi-rs/package-template')

module.exports.compress = function compress(data) {
return _compress(Buffer.isBuffer(data) ? data : Buffer.from(data))
}

module.exports.compressSync = function compressSync(data) {
return compress_sync(Buffer.isBuffer(data) ? data : Buffer.from(data))
}

module.exports.uncompress = function uncompress(data) {
return _uncompress(Buffer.isBuffer(data) ? data : Buffer.from(data))
}

module.exports.uncompressSync = function uncompressSync(data) {
return uncompress_sync(Buffer.isBuffer(data) ? data : Buffer.from(data))
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ impl Task for Dec {
#[module_exports]
fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("compress", compress_data)?;
exports.create_named_method("decompress", decompress_data)?;
exports.create_named_method("uncompress", uncompress_data)?;
exports.create_named_method("compress_sync", compress_data_sync)?;
exports.create_named_method("decompress_sync", decompress_data_sync)?;
exports.create_named_method("uncompress_sync", uncompress_data_sync)?;
Ok(())
}

Expand All @@ -86,7 +86,7 @@ fn compress_data(ctx: CallContext) -> Result<JsObject> {
}

#[js_function(1)]
fn decompress_data(ctx: CallContext) -> Result<JsObject> {
fn uncompress_data(ctx: CallContext) -> Result<JsObject> {
let data = ctx.get::<JsBuffer>(0)?;
let decoder = Dec {
data: data.into_ref()?,
Expand All @@ -95,7 +95,7 @@ fn decompress_data(ctx: CallContext) -> Result<JsObject> {
}

#[js_function(1)]
fn decompress_data_sync(ctx: CallContext) -> Result<JsUnknown> {
fn uncompress_data_sync(ctx: CallContext) -> Result<JsUnknown> {
let data = ctx.get::<JsBuffer>(0)?;
decompress_size_prepended(&data.into_value()?)
.map_err(|e| Error::new(napi::Status::GenericFailure, format!("{}", e)))
Expand Down

0 comments on commit 4fd604c

Please sign in to comment.