Skip to content

Commit

Permalink
chore: upgrade napi to 2.0.0-alpha.2
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Nov 15, 2021
1 parent 8c017c1 commit e54968e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
napi = { version = "2.0.0-alpha.1", features = ["serde-json", "async"] }
napi-derive = { version = "2.0.0-alpha.1" }
napi = { version = "2.0.0-alpha.2", features = ["serde-json", "async"] }
napi-derive = { version = "2.0.0-alpha.3" }
serde = { version = "1", features = ["derive"] }
brotli = { version = "3.3", features = ["simd"], git = "https://github.com/billyb2/rust-brotli.git", branch = "master" }

# [target.'cfg(all(target_arch = "x86_64", not(target_env = "musl")))'.dependencies]
# mimalloc-rust = "0.1"
brotli = { version = "3.3", features = ["simd"], git = "https://github.com/dropbox/rust-brotli", branch = "master" }

[build-dependencies]
napi-build = "1"
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export function compress(buffer: Buffer): Promise<Buffer>
export function compress(data: Buffer): Buffer
export function decompress(buffer: Buffer): Promise<Buffer>
32 changes: 17 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
],
"files": [
"index.d.ts",
"index.js"
"index.js",
"js-binding.js",
"js-binding.d.ts"
],
"napi": {
"name": "brotlijs",
Expand Down Expand Up @@ -42,8 +44,8 @@
"scripts": {
"artifacts": "napi artifacts",
"bench": "node -r @swc-node/register benchmark/bench.ts",
"build": "napi build --platform --release --dts index.d.ts",
"build:debug": "napi build --platform",
"build": "napi build --platform --release --js js-binding.js --dts js-binding.d.ts",
"build:debug": "napi build --platform --js js-binding.js --dts js-binding.d.ts",
"format": "run-p format:md format:json format:yaml format:source format:rs",
"format:md": "prettier --parser markdown --write './**/*.md'",
"format:json": "prettier --parser json --write './**/*.json'",
Expand All @@ -52,37 +54,37 @@
"format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'",
"lint": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}'",
"lint:fix": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}' --fix",
"prepublishOnly": "napi prepublish -t npm",
"prepublishOnly": "napi prepublish -t npm && esbuild js-binding.js --minify --allow-overwrite --outfile=js-binding.js",
"test": "ava",
"version": "napi version"
},
"devDependencies": {
"@napi-rs/cli": "2.0.0-alpha.3",
"@swc-node/register": "^1.3.5",
"@napi-rs/cli": "2.0.0-alpha.4",
"@swc-node/register": "^1.4.0",
"@types/benchmark": "^2.1.1",
"@types/node": "^14.17.22",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
"ava": "^3.15.0",
"benchmark": "^2.1.4",
"benny": "^3.7.0",
"benny": "^3.7.1",
"brotli-wasm": "^1.1.0",
"chalk": "^4.1.2",
"eslint": "^7.32.0",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-sonarjs": "^0.10.0",
"husky": "^7.0.2",
"husky": "^7.0.4",
"iltorb": "^2.4.5",
"lint-staged": "^11.2.0",
"lint-staged": "^11.2.6",
"npm-run-all": "^4.1.5",
"prettier": "^2.4.1",
"probe-image-size": "^7.2.1",
"typescript": "^4.4.3"
"typescript": "^4.4.3",
"esbuild": "^0.13.13"
},
"dependencies": {
"@node-rs/helper": "^1.2.1"
},
"lint-staged": {
"*.@(js|ts|tsx)": [
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2021-11-09
53 changes: 47 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,64 @@ use brotli;
use napi::bindgen_prelude::*;
use napi_derive::napi;

// #[global_allocator]
// static ALLOC: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc;

#[napi]
pub async fn compress(buffer: Buffer) -> Buffer {
#[inline]
fn brotli_compress(data: Buffer) -> Buffer {
let mut output = Vec::<u8>::new();
let mut params = brotli::enc::BrotliEncoderParams::default();
params.quality = 11 as i32;

match brotli::BrotliCompress(&mut buffer.as_ref(), &mut output, &params) {
match brotli::BrotliCompress(&mut data.as_ref(), &mut output, &params) {
Ok(_) => (),
Err(_) => todo!(),
}

Buffer::from(output)
}

#[napi]
pub fn compress(data: Buffer) -> Result<Buffer> {
let buffer = brotli_compress(data);

Ok(buffer.into())
}

pub struct AsyncRenderer {
data: Buffer,
}

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

fn compute(&mut self) -> Result<Self::Output> {
brotli_compress(self.data)
}

fn resolve(&mut self, _env: napi::Env, result: Self::Output) -> Result<Self::JsValue> {
Ok(result.into())
}
}

#[napi]
pub fn compress_async(
data: Buffer,
signal: Option<AbortSignal>,
) -> AsyncTask<AsyncRenderer> {

match signal {
Some(s) => AsyncTask::with_signal(
AsyncRenderer {
data,
},
s,
),
None => AsyncTask::new(AsyncRenderer {
data,
}),
}
}

#[napi]
pub async fn decompress(buffer: Buffer) -> Buffer {
let mut output = Vec::<u8>::new();
Expand Down

0 comments on commit e54968e

Please sign in to comment.