From 668e4fb4d27f587698e7c923ac652852344a529b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:08:50 +0800 Subject: [PATCH 1/8] feat: add base global_alloc crate --- crates/alloc/Cargo.toml | 9 +++++++++ crates/alloc/src/lib.rs | 6 ++++++ 2 files changed, 15 insertions(+) create mode 100644 crates/alloc/Cargo.toml create mode 100644 crates/alloc/src/lib.rs diff --git a/crates/alloc/Cargo.toml b/crates/alloc/Cargo.toml new file mode 100644 index 00000000..82aaad28 --- /dev/null +++ b/crates/alloc/Cargo.toml @@ -0,0 +1,9 @@ +[package] +edition = "2021" +name = "global_alloc" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[target.'cfg(not(all(target_os = "windows", target_arch = "aarch64")))'.dependencies] +mimalloc-rust = {version = "0.1"} diff --git a/crates/alloc/src/lib.rs b/crates/alloc/src/lib.rs new file mode 100644 index 00000000..07e8ef30 --- /dev/null +++ b/crates/alloc/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(all( + not(debug_assertions), + not(all(target_os = "windows", target_arch = "aarch64")) +))] +#[global_allocator] +static ALLOC: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; From bdc39279a9f7b27c378b1465e587540f6160d640 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:10:07 +0800 Subject: [PATCH 2/8] chore(bcrypt): migrate to mimalloc_rust --- packages/bcrypt/Cargo.toml | 6 ++---- packages/bcrypt/src/lib.rs | 14 +++----------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/packages/bcrypt/Cargo.toml b/packages/bcrypt/Cargo.toml index 8930ac88..5983fe39 100644 --- a/packages/bcrypt/Cargo.toml +++ b/packages/bcrypt/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "node-rs-bcrypt" version = "0.1.0" @@ -10,15 +10,13 @@ crate-type = ["cdylib"] [dependencies] blowfish = {version = "0.8", features = ["bcrypt"]} byteorder = "1" +global_alloc = {path = "../../crates/alloc"} napi = "1" napi-derive = "1" phf = {version = "0.10", features = ["macros"]} radix64 = "0.6" rand = "0.8" -[target.'cfg(all(target_arch = "x86_64", not(target_env = "musl")))'.dependencies] -mimalloc = {version = "0.1"} - [dev-dependencies] quickcheck = "1.0" diff --git a/packages/bcrypt/src/lib.rs b/packages/bcrypt/src/lib.rs index 6f96ecb4..81d8fe6a 100644 --- a/packages/bcrypt/src/lib.rs +++ b/packages/bcrypt/src/lib.rs @@ -1,13 +1,13 @@ #![deny(clippy::all)] #![allow(clippy::nonstandard_macro_braces)] -#[macro_use] -extern crate napi_derive; +/// Explicit extern crate to use allocator. +extern crate global_alloc; -use std::convert::TryInto; use std::str::FromStr; use napi::{CallContext, Error, JsBoolean, JsBuffer, JsNumber, JsObject, JsString, Result, Status}; +use napi_derive::*; use crate::hash_task::HashTask; use crate::lib_bcrypt::{format_salt, gen_salt, Version}; @@ -21,14 +21,6 @@ mod lib_bcrypt; mod salt_task; mod verify_task; -#[cfg(all( - target_arch = "x86_64", - not(target_env = "musl"), - not(debug_assertions) -))] -#[global_allocator] -static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; - #[module_exports] fn init(mut exports: JsObject) -> Result<()> { exports.create_named_method("genSaltSync", js_salt)?; From fa9c43721ea22113f48483519fdcef64efcd5c04 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:10:22 +0800 Subject: [PATCH 3/8] chore(crc32): migrate to mimalloc_rust --- packages/crc32/Cargo.toml | 6 ++---- packages/crc32/src/lib.rs | 22 +++++++--------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/crc32/Cargo.toml b/packages/crc32/Cargo.toml index f1fb6a06..78f97d49 100644 --- a/packages/crc32/Cargo.toml +++ b/packages/crc32/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "node-rs-crc32" version = "0.1.0" @@ -10,11 +10,9 @@ crate-type = ["cdylib"] [dependencies] crc32c = {version = "0.6"} crc32fast = {version = "1.2", features = ["nightly"]} +global_alloc = {path = "../../crates/alloc"} napi = "1" napi-derive = "1" -[target.'cfg(all(target_arch = "x86_64", not(target_env = "musl")))'.dependencies] -mimalloc = {version = "0.1"} - [build-dependencies] napi-build = "1" diff --git a/packages/crc32/src/lib.rs b/packages/crc32/src/lib.rs index 4182dcc4..25a88cbb 100644 --- a/packages/crc32/src/lib.rs +++ b/packages/crc32/src/lib.rs @@ -1,21 +1,13 @@ #![deny(clippy::all)] #![allow(clippy::nonstandard_macro_braces)] -#[macro_use] -extern crate napi_derive; +/// Explicit extern crate to use allocator. +extern crate global_alloc; use crc32c::crc32c_append; use crc32fast::Hasher; use napi::{CallContext, JsBuffer, JsNumber, JsObject, Result}; -use std::convert::TryInto; - -#[cfg(all( - target_arch = "x86_64", - not(target_env = "musl"), - not(debug_assertions) -))] -#[global_allocator] -static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; +use napi_derive::*; #[module_exports] fn init(mut exports: JsObject) -> Result<()> { @@ -27,16 +19,16 @@ fn init(mut exports: JsObject) -> Result<()> { #[js_function(2)] fn crc32c(ctx: CallContext) -> Result { let input_data = ctx.get::(0)?.into_value()?; - let init_state = ctx.get::(1)?; - let result = crc32c_append(init_state.try_into()?, &input_data); + let init_state = ctx.get::(1)?.get_uint32()?; + let result = crc32c_append(init_state, &input_data); ctx.env.create_uint32(result) } #[js_function(2)] fn crc32(ctx: CallContext) -> Result { let input_data = ctx.get::(0)?.into_value()?; - let init_state = ctx.get::(1)?; - let mut hasher = Hasher::new_with_initial(init_state.try_into()?); + let init_state = ctx.get::(1)?.get_uint32()?; + let mut hasher = Hasher::new_with_initial(init_state); hasher.update(&input_data); ctx.env.create_uint32(hasher.finalize()) } From 66014fa417ebff6b42546c29b3ccfa767c7f668b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:10:38 +0800 Subject: [PATCH 4/8] chore(deno-lint): migrate to mimalloc_rust --- packages/deno-lint/Cargo.toml | 8 +++----- packages/deno-lint/src/lib.rs | 13 +++---------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/deno-lint/Cargo.toml b/packages/deno-lint/Cargo.toml index b470d2e5..4b22c4e2 100644 --- a/packages/deno-lint/Cargo.toml +++ b/packages/deno-lint/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "deno-lint" version = "0.1.0" @@ -13,6 +13,7 @@ anyhow = "1" deno_ast = "0.4.1" deno_lint = "0.18.1" env_logger = "0.9" +global_alloc = {path = "../../crates/alloc"} globwalk = "0.8" ignore = "0.4" napi = {version = "1", features = ["serde-json"]} @@ -20,8 +21,5 @@ napi-derive = "1" serde = "1" serde_json = "1" -[target.'cfg(all(target_arch = "x86_64", not(target_env = "musl")))'.dependencies] -mimalloc = {version = "0.1"} - [build-dependencies] -napi-build = "1.1.0" +napi-build = "1" diff --git a/packages/deno-lint/src/lib.rs b/packages/deno-lint/src/lib.rs index a10287e9..494a5fd6 100644 --- a/packages/deno-lint/src/lib.rs +++ b/packages/deno-lint/src/lib.rs @@ -1,8 +1,8 @@ #![deny(clippy::all)] #![allow(clippy::nonstandard_macro_braces)] -#[macro_use] -extern crate napi_derive; +/// Explicit extern crate to use allocator. +extern crate global_alloc; use std::env; use std::fs; @@ -16,18 +16,11 @@ use deno_lint::rules::{get_all_rules, get_recommended_rules}; use ignore::types::TypesBuilder; use ignore::WalkBuilder; use napi::{CallContext, Error, JsBoolean, JsBuffer, JsObject, JsString, Result, Status}; +use napi_derive::*; mod config; mod diagnostics; -#[cfg(all( - target_arch = "x86_64", - not(target_env = "musl"), - not(debug_assertions) -))] -#[global_allocator] -static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; - #[module_exports] fn init(mut exports: JsObject) -> Result<()> { env_logger::init(); From e01d558a37a7691e3f359ccefc566ff031861621 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:10:56 +0800 Subject: [PATCH 5/8] chore(jieba): migrate to mimalloc_rust --- packages/jieba/Cargo.toml | 8 +++----- packages/jieba/src/lib.rs | 13 +++---------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/jieba/Cargo.toml b/packages/jieba/Cargo.toml index acefbced..b05182fe 100644 --- a/packages/jieba/Cargo.toml +++ b/packages/jieba/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "node-rs-jieba" version = "0.1.0" @@ -8,13 +8,11 @@ version = "0.1.0" crate-type = ["cdylib"] [dependencies] +global_alloc = {path = "../../crates/alloc"} jieba-rs = {version = "0.6", features = ["default-dict", "tfidf", "textrank"]} napi = "1" napi-derive = "1" once_cell = "1.8" -[target.'cfg(all(target_arch = "x86_64", not(target_env = "musl")))'.dependencies] -mimalloc = {version = "0.1"} - [build-dependencies] -napi-build = "1.1.0" +napi-build = "1" diff --git a/packages/jieba/src/lib.rs b/packages/jieba/src/lib.rs index d7e57bf1..e33a98eb 100644 --- a/packages/jieba/src/lib.rs +++ b/packages/jieba/src/lib.rs @@ -1,8 +1,8 @@ #![deny(clippy::all)] #![allow(clippy::nonstandard_macro_braces)] -#[macro_use] -extern crate napi_derive; +/// Explicit extern crate to use allocator. +extern crate global_alloc; use std::convert::TryInto; use std::str; @@ -12,16 +12,9 @@ use napi::{ CallContext, Env, Error, JsBoolean, JsBuffer, JsNumber, JsObject, JsString, JsUndefined, Result, Status, }; +use napi_derive::*; use once_cell::sync::OnceCell; -#[cfg(all( - target_arch = "x86_64", - not(target_env = "musl"), - not(debug_assertions) -))] -#[global_allocator] -static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; - static JIEBA: OnceCell = OnceCell::new(); static TFIDF_INSTANCE: OnceCell = OnceCell::new(); From 0f1320947ef6d44ed3a4ce57d03c959ea43b377c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:11:20 +0800 Subject: [PATCH 6/8] chore: upgrade rust-toolchain --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index f1fe3380..e7eda32f 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-09-08 \ No newline at end of file +nightly-2021-10-21 From ff17f0e017fb53d3219ab65e210c783148772382 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:11:57 +0800 Subject: [PATCH 7/8] ci: upgrade github actions --- .github/workflows/ci.yaml | 83 +++++++++++++++++-------------------- .github/workflows/lint.yaml | 2 +- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 10ac6962..7c71afac 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,7 +21,9 @@ jobs: settings: - host: macos-latest target: 'x86_64-apple-darwin' - build: yarn build + build: | + yarn build + strip -x packages/*/*.node - host: windows-latest build: yarn build target: 'x86_64-pc-windows-msvc' @@ -35,44 +37,49 @@ jobs: setup: | choco install nodejs-lts --x86 -y --force echo "C:\\Program Files (x86)\\nodejs" >> $GITHUB_PATH - - host: ubuntu-20.04 + - host: ubuntu-latest target: 'x86_64-unknown-linux-gnu' docker: | - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian docker tag $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-debian builder build: | docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build - - host: ubuntu-20.04 + - host: ubuntu-latest target: 'x86_64-unknown-linux-musl' docker: | - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL docker pull $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-alpine docker tag $DOCKER_REGISTRY_URL/napi-rs/napi-rs/nodejs-rust:lts-alpine builder build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build - host: macos-latest target: 'aarch64-apple-darwin' - build: npx lerna exec "yarn build --target aarch64-apple-darwin" --concurrency 1 --stream --no-prefix - - host: ubuntu-20.04 + build: | + sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*; + export CC=$(xcrun -f clang); + export CXX=$(xcrun -f clang++); + SYSROOT=$(xcrun --sdk macosx --show-sdk-path); + export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT"; + npx lerna exec "yarn build --target aarch64-apple-darwin" --concurrency 1 --stream --no-prefix + strip -x packages/*/*.node + - host: ubuntu-latest target: 'aarch64-unknown-linux-gnu' setup: | sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y build: npx lerna exec "yarn build --target aarch64-unknown-linux-gnu" --concurrency 1 --stream --no-prefix - - host: ubuntu-20.04 + - host: ubuntu-latest target: 'armv7-unknown-linux-gnueabihf' setup: | sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y build: npx lerna exec "yarn build --target armv7-unknown-linux-gnueabihf" --concurrency 1 --stream --no-prefix - - host: ubuntu-20.04 + - host: ubuntu-latest target: 'aarch64-linux-android' build: | export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang" + export PATH="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}" npx lerna exec "yarn build --target aarch64-linux-android" --concurrency 1 --stream --no-prefix - host: ubuntu-latest target: 'aarch64-unknown-linux-musl' downloadTarget: 'aarch64-unknown-linux-musl' docker: | - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL docker pull ghcr.io/brooooooklyn/canvas/musl-builder:lts docker tag ghcr.io/brooooooklyn/canvas/musl-builder:lts builder build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/node-rs -w /node-rs builder sh -c "rustup toolchain install $(cat ./rust-toolchain) && rustup target add aarch64-unknown-linux-musl && npx lerna exec \"yarn build --target aarch64-unknown-linux-musl\" --concurrency 1 --stream --no-prefix" @@ -105,19 +112,19 @@ jobs: command: generate-lockfile - name: Cache cargo registry - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: ~/.cargo/registry key: ${{ matrix.settings.target }}-node@14-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo index - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: ~/.cargo/git key: ${{ matrix.settings.target }}-node@14-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }} - name: Cache NPM dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: node_modules key: npm-cache-${{ matrix.settings.target }}-node@14-${{ hashFiles('yarn.lock') }} @@ -296,7 +303,7 @@ jobs: fail-fast: false matrix: node: ['12', '14', '16'] - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -338,7 +345,7 @@ jobs: fail-fast: false matrix: node: ['12', '14', '16'] - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset @@ -363,27 +370,21 @@ jobs: shell: bash - name: Setup and run tests - uses: docker://multiarch/ubuntu-core:arm64-focal + uses: addnab/docker-run-action@v3 with: - args: > - sh -c " - apt-get update && \ - apt-get install -y ca-certificates gnupg2 curl apt-transport-https && \ - curl -sL https://deb.nodesource.com/setup_${{ matrix.node }}.x | bash - && \ - apt-get install -y nodejs && \ - npm install -g yarn && \ - yarn install --ignore-scripts --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000 && \ - yarn build:ts && \ - yarn test && \ - ls -la - " + image: ghcr.io/napi-rs/napi-rs/nodejs:aarch64-${{ matrix.node }} + options: -v ${{ github.workspace }}:/skia -w /skia + run: | + pnpm install --ignore-scripts && \ + npm test && \ + ls -la test-linux-aarch64-musl-binding: name: Test bindings on aarch64-unknown-linux-musl - node@${{ matrix.node }} needs: - build - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset @@ -426,7 +427,7 @@ jobs: fail-fast: false matrix: node: ['12', '14', '16'] - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset @@ -451,20 +452,14 @@ jobs: shell: bash - name: Setup and run tests - uses: docker://multiarch/ubuntu-core:armhf-focal + uses: addnab/docker-run-action@v3 with: - args: > - sh -c " - apt-get update && \ - apt-get install -y ca-certificates gnupg2 curl apt-transport-https && \ - curl -sL https://deb.nodesource.com/setup_${{ matrix.node }}.x | bash - && \ - apt-get install -y nodejs && \ - npm install -g yarn && \ - yarn install --ignore-scripts --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000 && \ - yarn build:ts && \ - yarn test && \ - ls -la - " + image: ghcr.io/napi-rs/napi-rs/nodejs:armhf-${{ matrix.node }} + options: -v ${{ github.workspace }}:/skia -w /skia + run: | + pnpm install --ignore-scripts && \ + npm test && \ + ls -la publish: name: Publish @@ -486,7 +481,7 @@ jobs: check-latest: true - name: Cache NPM dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: node_modules key: npm-cache-ubuntu-latest-publish-${{ hashFiles('yarn.lock') }} diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 4325703f..3391c9d4 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -28,7 +28,7 @@ jobs: components: rustfmt, clippy - name: Cache NPM dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: node_modules key: npm-cache-lint-node@14-${{ hashFiles('yarn.lock') }} From 5ea38c3bfd9b178f1fa3bc0d2c0ff2f60eae807b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 21 Oct 2021 22:13:00 +0800 Subject: [PATCH 8/8] feat(xxhash): init xxhash32 and xxhash64 --- .cargo/config.toml | 3 + Cargo.toml | 4 + package.json | 1 + packages/xxhash/Cargo.toml | 17 +++ packages/xxhash/README.md | 95 +++++++++++++++ packages/xxhash/__test__/xxh32.spec.ts | 38 ++++++ packages/xxhash/__test__/xxh64.spec.ts | 38 ++++++ packages/xxhash/benchmark/xxhash.js | 70 ++++++++++++ packages/xxhash/build.rs | 5 + packages/xxhash/index.d.ts | 23 ++++ packages/xxhash/index.js | 27 +++++ packages/xxhash/npm/android-arm64/README.md | 3 + .../xxhash/npm/android-arm64/package.json | 27 +++++ packages/xxhash/npm/darwin-arm64/README.md | 3 + packages/xxhash/npm/darwin-arm64/package.json | 27 +++++ packages/xxhash/npm/darwin-x64/README.md | 3 + packages/xxhash/npm/darwin-x64/package.json | 27 +++++ packages/xxhash/npm/freebsd-x64/README.md | 3 + packages/xxhash/npm/freebsd-x64/package.json | 27 +++++ .../xxhash/npm/linux-arm-gnueabihf/README.md | 3 + .../npm/linux-arm-gnueabihf/package.json | 27 +++++ packages/xxhash/npm/linux-arm64-gnu/README.md | 3 + .../xxhash/npm/linux-arm64-gnu/package.json | 27 +++++ .../xxhash/npm/linux-arm64-musl/README.md | 3 + .../xxhash/npm/linux-arm64-musl/package.json | 27 +++++ packages/xxhash/npm/linux-x64-gnu/README.md | 3 + .../xxhash/npm/linux-x64-gnu/package.json | 27 +++++ packages/xxhash/npm/linux-x64-musl/README.md | 3 + .../xxhash/npm/linux-x64-musl/package.json | 27 +++++ .../xxhash/npm/win32-arm64-msvc/README.md | 3 + .../xxhash/npm/win32-arm64-msvc/package.json | 27 +++++ packages/xxhash/npm/win32-ia32-msvc/README.md | 3 + .../xxhash/npm/win32-ia32-msvc/package.json | 27 +++++ packages/xxhash/npm/win32-x64-msvc/README.md | 3 + .../xxhash/npm/win32-x64-msvc/package.json | 27 +++++ packages/xxhash/package.json | 62 ++++++++++ packages/xxhash/src/lib.rs | 108 ++++++++++++++++++ yarn.lock | 24 ++++ 38 files changed, 875 insertions(+) create mode 100644 packages/xxhash/Cargo.toml create mode 100644 packages/xxhash/README.md create mode 100644 packages/xxhash/__test__/xxh32.spec.ts create mode 100644 packages/xxhash/__test__/xxh64.spec.ts create mode 100644 packages/xxhash/benchmark/xxhash.js create mode 100644 packages/xxhash/build.rs create mode 100644 packages/xxhash/index.d.ts create mode 100644 packages/xxhash/index.js create mode 100644 packages/xxhash/npm/android-arm64/README.md create mode 100644 packages/xxhash/npm/android-arm64/package.json create mode 100644 packages/xxhash/npm/darwin-arm64/README.md create mode 100644 packages/xxhash/npm/darwin-arm64/package.json create mode 100644 packages/xxhash/npm/darwin-x64/README.md create mode 100644 packages/xxhash/npm/darwin-x64/package.json create mode 100644 packages/xxhash/npm/freebsd-x64/README.md create mode 100644 packages/xxhash/npm/freebsd-x64/package.json create mode 100644 packages/xxhash/npm/linux-arm-gnueabihf/README.md create mode 100644 packages/xxhash/npm/linux-arm-gnueabihf/package.json create mode 100644 packages/xxhash/npm/linux-arm64-gnu/README.md create mode 100644 packages/xxhash/npm/linux-arm64-gnu/package.json create mode 100644 packages/xxhash/npm/linux-arm64-musl/README.md create mode 100644 packages/xxhash/npm/linux-arm64-musl/package.json create mode 100644 packages/xxhash/npm/linux-x64-gnu/README.md create mode 100644 packages/xxhash/npm/linux-x64-gnu/package.json create mode 100644 packages/xxhash/npm/linux-x64-musl/README.md create mode 100644 packages/xxhash/npm/linux-x64-musl/package.json create mode 100644 packages/xxhash/npm/win32-arm64-msvc/README.md create mode 100644 packages/xxhash/npm/win32-arm64-msvc/package.json create mode 100644 packages/xxhash/npm/win32-ia32-msvc/README.md create mode 100644 packages/xxhash/npm/win32-ia32-msvc/package.json create mode 100644 packages/xxhash/npm/win32-x64-msvc/README.md create mode 100644 packages/xxhash/npm/win32-x64-msvc/package.json create mode 100644 packages/xxhash/package.json create mode 100644 packages/xxhash/src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index d5de7aa5..8b5b5992 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,6 @@ +[target.x86_64-pc-windows-msvc] +rustflags = ["-C", "target-cpu=skylake"] + [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" diff --git a/Cargo.toml b/Cargo.toml index 9884c8cf..d0eee53a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,12 +2,16 @@ cargo-features = ["strip"] [workspace] members = [ + "./crates/alloc", "./packages/bcrypt", "./packages/crc32", "./packages/deno-lint", "./packages/jieba", + "./packages/xxhash", ] [profile.release] +codegen-units = 1 lto = true +overflow-checks = false strip = 'symbols' diff --git a/package.json b/package.json index a475f805..7212f0c7 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "devDependencies": { "@napi-rs/cli": "^1.3.3", "@swc-node/register": "^1.3.6", + "@types/node": "^16.11.1", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", "ava": "^3.15.0", diff --git a/packages/xxhash/Cargo.toml b/packages/xxhash/Cargo.toml new file mode 100644 index 00000000..264913d4 --- /dev/null +++ b/packages/xxhash/Cargo.toml @@ -0,0 +1,17 @@ +[package] +authors = ["LongYinan "] +edition = "2021" +name = "xxhash" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +global_alloc = {path = "../../crates/alloc"} +napi = {version = "1", features = ["napi6"]} +napi-derive = "1" +xxhash-rust = {version = "0.8", features = ["xxh32", "const_xxh32", "xxh64", "const_xxh64", "xxh3", "const_xxh3"]} + +[build-dependencies] +napi-build = "1" diff --git a/packages/xxhash/README.md b/packages/xxhash/README.md new file mode 100644 index 00000000..8daa018e --- /dev/null +++ b/packages/xxhash/README.md @@ -0,0 +1,95 @@ +# `@node-rs/xxhash` + +![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg) +![](https://img.shields.io/npm/dm/@node-rs/xxhash.svg?sanitize=true) +[![Install size](https://packagephobia.com/badge?p=@node-rs/xxhash)](https://packagephobia.com/result?p=@node-rs/xxhash) + +> 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn) + +Fastest `xxhash` implementation in Node.js. + +## Install this package + +``` +yarn add @node-rs/xxhash +pnpm add @node-rs/xxhash +npm install @node-rs/xxhash +``` + +## Support matrix + +| | node12 | node14 | node16 | +| --------------------- | ------ | ------ | ------ | +| Windows x64 | ✓ | ✓ | ✓ | +| Windows x32 | ✓ | ✓ | ✓ | +| Windows arm64 | ✓ | ✓ | ✓ | +| macOS x64 | ✓ | ✓ | ✓ | +| macOS arm64 (m chips) | ✓ | ✓ | ✓ | +| Linux x64 gnu | ✓ | ✓ | ✓ | +| Linux x64 musl | ✓ | ✓ | ✓ | +| Linux arm gnu | ✓ | ✓ | ✓ | +| Linux arm64 gnu | ✓ | ✓ | ✓ | +| Linux arm64 musl | ✓ | ✓ | ✓ | +| Android arm64 | ✓ | ✓ | ✓ | +| FreeBSD x64 | ✓ | ✓ | ✓ | + +## API + +```ts +export type BufferLike = + | Buffer + | string + | Uint8Array + | ArrayBuffer + | SharedArrayBuffer + | ReadonlyArray + | number[] + +export function xxh32(input: BufferLike, seed?: number): number +export function xxh64(input: BufferLike, seed?: BigInt): BigInt + +export class Xxh32 { + constructor(seed?: number) + update(input: BufferLike): this + digest(): number +} + +export class Xxh64 { + constructor(seed?: BigInt) + update(input: BufferLike): this + digest(): BigInt +} +``` + +## Performance + +### Hardware + +``` +OS: Windows 10 x86_64 +Host: Micro-Star International Co., Ltd. MS-7C35 +Kernel: 10.0.19043 +Terminal: Windows Terminal +CPU: AMD Ryzen 9 5950X (32) @ 3.400GHz +Memory: 32688MiB +``` + +### Result + +``` +@node-rs/xxhash h32 x 4,663 ops/sec ±6.22% (81 runs sampled) +xxhashjs h32 x 1,880 ops/sec ±7.11% (75 runs sampled) +xxh32 bench suite: Fastest is @node-rs/xxhash h32 + +@node-rs/xxhash h32 x 13,452 ops/sec ±2.73% (80 runs sampled) +xxhashjs h32 x 2,496 ops/sec ±0.39% (97 runs sampled) +xxh32 multi steps bench suite: Fastest is @node-rs/xxhash h32 + +@node-rs/xxhash 64 x 15,806 ops/sec ±3.14% (79 runs sampled) +xxhashjs h64 x 69.11 ops/sec ±5.99% (60 runs sampled) +xxh64 bench suite: Fastest is @node-rs/xxhash 64 + +@node-rs/xxhash 64 x 13,841 ops/sec ±3.17% (82 runs sampled) +xxhashjs h64 x 79.71 ops/sec ±4.34% (70 runs sampled) +xxh64 multi steps bench suite: Fastest is @node-rs/xxhash 64 +``` diff --git a/packages/xxhash/__test__/xxh32.spec.ts b/packages/xxhash/__test__/xxh32.spec.ts new file mode 100644 index 00000000..c77724a2 --- /dev/null +++ b/packages/xxhash/__test__/xxh32.spec.ts @@ -0,0 +1,38 @@ +import test from 'ava' +import { h32 } from 'xxhashjs' + +import { xxh32, Xxh32 } from '../index' + +const FX = Buffer.from('@node-rs/xxhash vs xxhashjs') +const SEED = 0xabcdef01 + +test('xxh32 without seed', (t) => { + t.is(xxh32(FX), h32(FX, 0).toNumber()) +}) + +test('xxh32 with seed', (t) => { + t.is(xxh32(FX, SEED), h32(FX, SEED).toNumber()) +}) + +test('xxh32 string', (t) => { + t.is(xxh32(FX.toString('utf8')), h32(FX, 0).toNumber()) +}) + +test('xxh32 Uint8Array', (t) => { + t.is(xxh32(new Uint8Array(FX.buffer)), h32(FX.buffer, 0).toNumber()) +}) + +test('Xxh32 oneshot', (t) => { + t.is(new Xxh32().update(FX).digest(), h32(FX, 0).toNumber()) +}) + +test('Xxh32 multi steps', (t) => { + t.is( + new Xxh32().update(FX).update(FX).update(FX).digest(), + h32(0).update(FX).update(FX).update(FX).digest().toNumber(), + ) +}) + +test('Xxh32 string', (t) => { + t.is(new Xxh32().update(FX.toString('utf8')).digest(), h32(FX, 0).toNumber()) +}) diff --git a/packages/xxhash/__test__/xxh64.spec.ts b/packages/xxhash/__test__/xxh64.spec.ts new file mode 100644 index 00000000..79d48b25 --- /dev/null +++ b/packages/xxhash/__test__/xxh64.spec.ts @@ -0,0 +1,38 @@ +import test from 'ava' +import { h64 } from 'xxhashjs' + +import { xxh64, Xxh64 } from '../index' + +const FX = Buffer.from('@node-rs/xxhash vs xxhashjs') +const SEED = 0xabcdef01 + +test('xxh64 without seed', (t) => { + t.is(xxh64(FX).toString(16), h64(FX, 0).toString(16)) +}) + +test('xxh64 with seed', (t) => { + t.is(xxh64(FX, BigInt(SEED)).toString(16), h64(FX, SEED).toString(16)) +}) + +test('xxh64 string', (t) => { + t.is(xxh64(FX.toString('utf8')).toString(16), h64(FX, 0).toString(16)) +}) + +test('xxh64 Uint8Array', (t) => { + t.is(xxh64(new Uint8Array(FX.buffer)).toString(16), h64(FX.buffer, 0).toString(16)) +}) + +test('Xxh64 oneshot', (t) => { + t.is(new Xxh64().update(FX).digest().toString(16), h64(FX, 0).toString(16)) +}) + +test('Xxh64 multi steps', (t) => { + t.is( + new Xxh64().update(FX).update(FX).update(FX).digest().toString(16), + h64(0).update(FX).update(FX).update(FX).digest().toString(16), + ) +}) + +test('Xxh64 string', (t) => { + t.is(new Xxh64().update(FX.toString('utf8')).digest().toString(16), h64(FX, 0).toString(16)) +}) diff --git a/packages/xxhash/benchmark/xxhash.js b/packages/xxhash/benchmark/xxhash.js new file mode 100644 index 00000000..a3a7a17a --- /dev/null +++ b/packages/xxhash/benchmark/xxhash.js @@ -0,0 +1,70 @@ +const { readFileSync } = require('fs') +const { join } = require('path') + +const { Suite } = require('benchmark') +const chalk = require('chalk') +const { h32: h32js, h64: h64js } = require('xxhashjs') + +const { xxh32, xxh64, Xxh32, Xxh64 } = require('../index') + +const FX = readFileSync(join(__dirname, '..', '..', '..', 'yarn.lock')) + +new Suite('xxh32') + .add('@node-rs/xxhash h32', () => { + xxh32(FX) + }) + .add('xxhashjs h32', () => { + h32js(FX, 0).toNumber() + }) + .on('cycle', function (event) { + console.info(String(event.target)) + }) + .on('complete', function () { + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) + }) + .run() + +new Suite('xxh32 multi steps') + .add('@node-rs/xxhash h32', () => { + new Xxh32().update(FX).digest() + }) + .add('xxhashjs h32', () => { + h32js().update(FX).digest().toNumber() + }) + .on('cycle', function (event) { + console.info(String(event.target)) + }) + .on('complete', function () { + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) + }) + .run() + +new Suite('xxh64') + .add('@node-rs/xxhash 64', () => { + xxh64(FX).toString(16) + }) + .add('xxhashjs h64', () => { + h64js(FX, 0).toString(16) + }) + .on('cycle', function (event) { + console.info(String(event.target)) + }) + .on('complete', function () { + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) + }) + .run() + +new Suite('xxh64 multi steps') + .add('@node-rs/xxhash 64', () => { + new Xxh64().update(FX).digest().toString(16) + }) + .add('xxhashjs h64', () => { + h64js(0).update(FX).digest().toString(16) + }) + .on('cycle', function (event) { + console.info(String(event.target)) + }) + .on('complete', function () { + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) + }) + .run() diff --git a/packages/xxhash/build.rs b/packages/xxhash/build.rs new file mode 100644 index 00000000..1f866b6a --- /dev/null +++ b/packages/xxhash/build.rs @@ -0,0 +1,5 @@ +extern crate napi_build; + +fn main() { + napi_build::setup(); +} diff --git a/packages/xxhash/index.d.ts b/packages/xxhash/index.d.ts new file mode 100644 index 00000000..77b6947b --- /dev/null +++ b/packages/xxhash/index.d.ts @@ -0,0 +1,23 @@ +export type BufferLike = + | Buffer + | string + | Uint8Array + | ArrayBuffer + | SharedArrayBuffer + | ReadonlyArray + | number[] + +export function xxh32(input: BufferLike, seed?: number): number +export function xxh64(input: BufferLike, seed?: BigInt): BigInt + +export class Xxh32 { + constructor(seed?: number) + update(input: BufferLike): this + digest(): number +} + +export class Xxh64 { + constructor(seed?: BigInt) + update(input: BufferLike): this + digest(): BigInt +} diff --git a/packages/xxhash/index.js b/packages/xxhash/index.js new file mode 100644 index 00000000..2bfd9534 --- /dev/null +++ b/packages/xxhash/index.js @@ -0,0 +1,27 @@ +const { loadBinding } = require('@node-rs/helper') + +const { + xxh32: _xxh32, + xxh64: _xxh64, + Xxh32: _Xxh32, + Xxh64: _Xxh64, +} = loadBinding(__dirname, 'xxhash', '@node-rs/xxhash') + +module.exports = { + xxh32: function xxh32(input, seed) { + return _xxh32(Buffer.from(input), seed == null ? 0 : seed) + }, + xxh64: function xxh64(input, seed) { + return _xxh64(Buffer.from(input), seed == null ? BigInt(0) : seed) + }, + Xxh32: class Xxh32 extends _Xxh32 { + update(input) { + return super.update(Buffer.from(input)) + } + }, + Xxh64: class Xxh64 extends _Xxh64 { + update(input) { + return super.update(Buffer.from(input)) + } + }, +} diff --git a/packages/xxhash/npm/android-arm64/README.md b/packages/xxhash/npm/android-arm64/README.md new file mode 100644 index 00000000..8b0e7c68 --- /dev/null +++ b/packages/xxhash/npm/android-arm64/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-android-arm64` + +This is the **aarch64-linux-android** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/android-arm64/package.json b/packages/xxhash/npm/android-arm64/package.json new file mode 100644 index 00000000..63339062 --- /dev/null +++ b/packages/xxhash/npm/android-arm64/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-android-arm64", + "version": "0.0.0", + "os": ["android"], + "cpu": ["arm64"], + "main": "xxhash.android-arm64.node", + "files": ["xxhash.android-arm64.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/darwin-arm64/README.md b/packages/xxhash/npm/darwin-arm64/README.md new file mode 100644 index 00000000..81db4a1e --- /dev/null +++ b/packages/xxhash/npm/darwin-arm64/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-darwin-arm64` + +This is the **aarch64-apple-darwin** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/darwin-arm64/package.json b/packages/xxhash/npm/darwin-arm64/package.json new file mode 100644 index 00000000..9ba4bf03 --- /dev/null +++ b/packages/xxhash/npm/darwin-arm64/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-darwin-arm64", + "version": "0.0.0", + "os": ["darwin"], + "cpu": ["arm64"], + "main": "xxhash.darwin-arm64.node", + "files": ["xxhash.darwin-arm64.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/darwin-x64/README.md b/packages/xxhash/npm/darwin-x64/README.md new file mode 100644 index 00000000..f15f68c3 --- /dev/null +++ b/packages/xxhash/npm/darwin-x64/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-darwin-x64` + +This is the **x86_64-apple-darwin** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/darwin-x64/package.json b/packages/xxhash/npm/darwin-x64/package.json new file mode 100644 index 00000000..4fbd4e72 --- /dev/null +++ b/packages/xxhash/npm/darwin-x64/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-darwin-x64", + "version": "0.0.0", + "os": ["darwin"], + "cpu": ["x64"], + "main": "xxhash.darwin-x64.node", + "files": ["xxhash.darwin-x64.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/freebsd-x64/README.md b/packages/xxhash/npm/freebsd-x64/README.md new file mode 100644 index 00000000..dd0142e5 --- /dev/null +++ b/packages/xxhash/npm/freebsd-x64/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-freebsd-x64` + +This is the **x86_64-unknown-freebsd** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/freebsd-x64/package.json b/packages/xxhash/npm/freebsd-x64/package.json new file mode 100644 index 00000000..e0dc4c50 --- /dev/null +++ b/packages/xxhash/npm/freebsd-x64/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-freebsd-x64", + "version": "0.0.0", + "os": ["freebsd"], + "cpu": ["x64"], + "main": "xxhash.freebsd-x64.node", + "files": ["xxhash.freebsd-x64.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/linux-arm-gnueabihf/README.md b/packages/xxhash/npm/linux-arm-gnueabihf/README.md new file mode 100644 index 00000000..53d5ae66 --- /dev/null +++ b/packages/xxhash/npm/linux-arm-gnueabihf/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-linux-arm-gnueabihf` + +This is the **armv7-unknown-linux-gnueabihf** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/linux-arm-gnueabihf/package.json b/packages/xxhash/npm/linux-arm-gnueabihf/package.json new file mode 100644 index 00000000..200c58cf --- /dev/null +++ b/packages/xxhash/npm/linux-arm-gnueabihf/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-linux-arm-gnueabihf", + "version": "0.0.0", + "os": ["linux"], + "cpu": ["arm"], + "main": "xxhash.linux-arm-gnueabihf.node", + "files": ["xxhash.linux-arm-gnueabihf.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/linux-arm64-gnu/README.md b/packages/xxhash/npm/linux-arm64-gnu/README.md new file mode 100644 index 00000000..acf20077 --- /dev/null +++ b/packages/xxhash/npm/linux-arm64-gnu/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-linux-arm64-gnu` + +This is the **aarch64-unknown-linux-gnu** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/linux-arm64-gnu/package.json b/packages/xxhash/npm/linux-arm64-gnu/package.json new file mode 100644 index 00000000..e99c5cce --- /dev/null +++ b/packages/xxhash/npm/linux-arm64-gnu/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-linux-arm64-gnu", + "version": "0.0.0", + "os": ["linux"], + "cpu": ["arm64"], + "main": "xxhash.linux-arm64-gnu.node", + "files": ["xxhash.linux-arm64-gnu.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/linux-arm64-musl/README.md b/packages/xxhash/npm/linux-arm64-musl/README.md new file mode 100644 index 00000000..35f4510a --- /dev/null +++ b/packages/xxhash/npm/linux-arm64-musl/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-linux-arm64-musl` + +This is the **aarch64-unknown-linux-musl** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/linux-arm64-musl/package.json b/packages/xxhash/npm/linux-arm64-musl/package.json new file mode 100644 index 00000000..3c9891f4 --- /dev/null +++ b/packages/xxhash/npm/linux-arm64-musl/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-linux-arm64-musl", + "version": "0.0.0", + "os": ["linux"], + "cpu": ["arm64"], + "main": "xxhash.linux-arm64-musl.node", + "files": ["xxhash.linux-arm64-musl.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/linux-x64-gnu/README.md b/packages/xxhash/npm/linux-x64-gnu/README.md new file mode 100644 index 00000000..28159c62 --- /dev/null +++ b/packages/xxhash/npm/linux-x64-gnu/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-linux-x64-gnu` + +This is the **x86_64-unknown-linux-gnu** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/linux-x64-gnu/package.json b/packages/xxhash/npm/linux-x64-gnu/package.json new file mode 100644 index 00000000..cc0b1aa5 --- /dev/null +++ b/packages/xxhash/npm/linux-x64-gnu/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-linux-x64-gnu", + "version": "0.0.0", + "os": ["linux"], + "cpu": ["x64"], + "main": "xxhash.linux-x64-gnu.node", + "files": ["xxhash.linux-x64-gnu.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/linux-x64-musl/README.md b/packages/xxhash/npm/linux-x64-musl/README.md new file mode 100644 index 00000000..3e8f1daa --- /dev/null +++ b/packages/xxhash/npm/linux-x64-musl/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-linux-x64-musl` + +This is the **x86_64-unknown-linux-musl** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/linux-x64-musl/package.json b/packages/xxhash/npm/linux-x64-musl/package.json new file mode 100644 index 00000000..c950101f --- /dev/null +++ b/packages/xxhash/npm/linux-x64-musl/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-linux-x64-musl", + "version": "0.0.0", + "os": ["linux"], + "cpu": ["x64"], + "main": "xxhash.linux-x64-musl.node", + "files": ["xxhash.linux-x64-musl.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/win32-arm64-msvc/README.md b/packages/xxhash/npm/win32-arm64-msvc/README.md new file mode 100644 index 00000000..2c3fbb48 --- /dev/null +++ b/packages/xxhash/npm/win32-arm64-msvc/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-win32-arm64-msvc` + +This is the **aarch64-pc-windows-msvc** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/win32-arm64-msvc/package.json b/packages/xxhash/npm/win32-arm64-msvc/package.json new file mode 100644 index 00000000..e0f761f2 --- /dev/null +++ b/packages/xxhash/npm/win32-arm64-msvc/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-win32-arm64-msvc", + "version": "0.0.0", + "os": ["win32"], + "cpu": ["arm64"], + "main": "xxhash.win32-arm64-msvc.node", + "files": ["xxhash.win32-arm64-msvc.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/win32-ia32-msvc/README.md b/packages/xxhash/npm/win32-ia32-msvc/README.md new file mode 100644 index 00000000..9510c510 --- /dev/null +++ b/packages/xxhash/npm/win32-ia32-msvc/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-win32-ia32-msvc` + +This is the **i686-pc-windows-msvc** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/win32-ia32-msvc/package.json b/packages/xxhash/npm/win32-ia32-msvc/package.json new file mode 100644 index 00000000..ee9fa3d9 --- /dev/null +++ b/packages/xxhash/npm/win32-ia32-msvc/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-win32-ia32-msvc", + "version": "0.0.0", + "os": ["win32"], + "cpu": ["ia32"], + "main": "xxhash.win32-ia32-msvc.node", + "files": ["xxhash.win32-ia32-msvc.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/win32-x64-msvc/README.md b/packages/xxhash/npm/win32-x64-msvc/README.md new file mode 100644 index 00000000..df489697 --- /dev/null +++ b/packages/xxhash/npm/win32-x64-msvc/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-win32-x64-msvc` + +This is the **x86_64-pc-windows-msvc** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/win32-x64-msvc/package.json b/packages/xxhash/npm/win32-x64-msvc/package.json new file mode 100644 index 00000000..c96d4648 --- /dev/null +++ b/packages/xxhash/npm/win32-x64-msvc/package.json @@ -0,0 +1,27 @@ +{ + "name": "@node-rs/xxhash-win32-x64-msvc", + "version": "0.0.0", + "os": ["win32"], + "cpu": ["x64"], + "main": "xxhash.win32-x64-msvc.node", + "files": ["xxhash.win32-x64-msvc.node"], + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/package.json b/packages/xxhash/package.json new file mode 100644 index 00000000..b63f76c0 --- /dev/null +++ b/packages/xxhash/package.json @@ -0,0 +1,62 @@ +{ + "name": "@node-rs/xxhash", + "version": "0.0.0", + "description": "Fastest xxhash implementation in Node.js", + "keywords": ["hash", "xxhash", "xxhashjs", "Rust", "node-rs", "napi", "napi-rs", "N-API", "Node-API"], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "main": "index.js", + "typings": "index.d.ts", + "files": ["index.js", "index.d.ts"], + "napi": { + "name": "xxhash", + "triples": { + "defaults": true, + "additional": [ + "i686-pc-windows-msvc", + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-gnu", + "armv7-unknown-linux-gnueabihf", + "aarch64-apple-darwin", + "aarch64-linux-android", + "x86_64-unknown-freebsd", + "aarch64-unknown-linux-musl", + "aarch64-pc-windows-msvc" + ] + } + }, + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "scripts": { + "artifacts": "napi artifacts -d ../../artifacts", + "bench": "cross-env NODE_ENV=production node benchmark/xxhash.js", + "build": "napi build --platform --release", + "build:debug": "napi build --platform", + "prepublishOnly": "napi prepublish", + "version": "napi version" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@node-rs/helper": "^1.2.1" + }, + "devDependencies": { + "@types/xxhashjs": "^0.2.2", + "xxhashjs": "^0.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + } +} diff --git a/packages/xxhash/src/lib.rs b/packages/xxhash/src/lib.rs new file mode 100644 index 00000000..24c4dbca --- /dev/null +++ b/packages/xxhash/src/lib.rs @@ -0,0 +1,108 @@ +/// Explicit extern crate to use allocator. +extern crate global_alloc; + +use napi::*; +use napi_derive::*; +use xxhash_rust::{xxh32, xxh64}; + +#[module_exports] +fn init(mut exports: JsObject, env: Env) -> Result<()> { + exports.create_named_method("xxh32", xxh32)?; + exports.create_named_method("xxh64", xxh64)?; + + let xxh32_class = env.define_class( + "Xxh32", + xxh32_constructor, + &[ + Property::new(&env, "update")?.with_method(update_xxh32), + Property::new(&env, "digest")?.with_method(digest_xxh32), + ], + )?; + let xxh64_class = env.define_class( + "Xxh64", + xxh64_constructor, + &[ + Property::new(&env, "update")?.with_method(update_xxh64), + Property::new(&env, "digest")?.with_method(digest_xxh64), + ], + )?; + exports.set_named_property("Xxh32", xxh32_class)?; + exports.set_named_property("Xxh64", xxh64_class)?; + Ok(()) +} + +#[js_function(2)] +fn xxh32(ctx: CallContext) -> Result { + let input = ctx.get::(0)?.into_value()?; + let seed = ctx.get::(1)?.get_uint32()?; + ctx + .env + .create_uint32(xxhash_rust::xxh32::xxh32(input.as_ref(), seed)) +} + +#[js_function(1)] +fn xxh32_constructor(ctx: CallContext) -> Result { + let mut this = ctx.this_unchecked::(); + let seed = if ctx.length == 1 { + ctx.get::(0)?.get_uint32()? + } else { + 0 + }; + let native = xxh32::Xxh32::new(seed); + ctx.env.wrap(&mut this, native)?; + ctx.env.get_undefined() +} + +#[js_function(1)] +fn update_xxh32(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let native = ctx.env.unwrap::(&this)?; + let input = ctx.get::(0)?.into_value()?; + native.update(input.as_ref()); + Ok(this) +} + +#[js_function] +fn digest_xxh32(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let native = ctx.env.unwrap::(&this)?; + ctx.env.create_uint32(native.digest()) +} + +#[js_function(2)] +fn xxh64(ctx: CallContext) -> Result { + let input = ctx.get::(0)?.into_value()?; + let (seed, _) = ctx.get::(1)?.get_u64()?; + ctx + .env + .create_bigint_from_u64(xxhash_rust::xxh64::xxh64(input.as_ref(), seed)) +} + +#[js_function(1)] +fn xxh64_constructor(ctx: CallContext) -> Result { + let mut this = ctx.this_unchecked::(); + let seed = if ctx.length == 1 { + ctx.get::(0)?.get_u64()?.0 + } else { + 0 + }; + let native = xxh64::Xxh64::new(seed); + ctx.env.wrap(&mut this, native)?; + ctx.env.get_undefined() +} + +#[js_function(1)] +fn update_xxh64(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let native = ctx.env.unwrap::(&this)?; + let input = ctx.get::(0)?.into_value()?; + native.update(input.as_ref()); + Ok(this) +} + +#[js_function] +fn digest_xxh64(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let native = ctx.env.unwrap::(&this)?; + ctx.env.create_bigint_from_u64(native.digest()) +} diff --git a/yarn.lock b/yarn.lock index d252e238..21206b0b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1337,6 +1337,11 @@ resolved "https://registry.npmjs.org/@types/node/-/node-16.9.0.tgz#d9512fe037472dcb58931ce19f837348db828a62" integrity sha512-nmP+VR4oT0pJUPFbKE4SXj3Yb4Q/kz3M9dSAO1GGMebRKWHQxLfDNmU/yh3xxCJha3N60nQ/JwXWwOE/ZSEVag== +"@types/node@^16.11.1": + version "16.11.1" + resolved "https://registry.npmjs.org/@types/node/-/node-16.11.1.tgz#2e50a649a50fc403433a14f829eface1a3443e97" + integrity sha512-PYGcJHL9mwl1Ek3PLiYgyEKtwTMmkMw4vbiyz/ps3pfdRYLVv+SN7qHVAImrjdAXxgluDEw6Ph4lyv+m9UpRmA== + "@types/normalize-package-data@^2.4.0": version "2.4.1" resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301" @@ -1356,6 +1361,13 @@ tapable "^2.2.0" webpack "^5" +"@types/xxhashjs@^0.2.2": + version "0.2.2" + resolved "https://registry.npmjs.org/@types/xxhashjs/-/xxhashjs-0.2.2.tgz#f72d9398e94b3cb59f53b784186a70b472e61376" + integrity sha512-+hlk/W1kgnZn0vR22XNhxHk/qIRQYF54i0UTF2MwBAPd0e7xSy+jKOJwSwTdRQrNnOMRVv+vsh8ITV0uyhp2yg== + dependencies: + "@types/node" "*" + "@typescript-eslint/eslint-plugin@^4.33.0": version "4.33.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.33.0.tgz#c24dc7c8069c7706bc40d99f6fa87edcb2005276" @@ -2611,6 +2623,11 @@ crypto-random-string@^2.0.0: resolved "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz#ef2a7a966ec11083388369baa02ebead229b30d5" integrity sha512-v1plID3y9r/lPhviJ1wrXpLeyUIGAZ2SHNYTEapm7/8A9nLPoyvVp3RK/EPFqn5kEznyWgYZNsRtYYIWbuG8KA== +cuint@^0.2.2: + version "0.2.2" + resolved "https://registry.npmjs.org/cuint/-/cuint-0.2.2.tgz#408086d409550c2631155619e9fa7bcadc3b991b" + integrity sha1-QICG1AlVDCYxFVYZ6fp7ytw7mRs= + currently-unhandled@^0.4.1: version "0.4.1" resolved "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea" @@ -7415,6 +7432,13 @@ xtend@~4.0.1: resolved "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== +xxhashjs@^0.2.2: + version "0.2.2" + resolved "https://registry.npmjs.org/xxhashjs/-/xxhashjs-0.2.2.tgz#8a6251567621a1c46a5ae204da0249c7f8caa9d8" + integrity sha512-AkTuIuVTET12tpsVIQo+ZU6f/qDmKuRUcjaqR+OIvm+aCBsZ95i7UVY5WJ9TMsSaZ0DA2WxoZ4acu0sPH+OKAw== + dependencies: + cuint "^0.2.2" + y18n@^4.0.0: version "4.0.3" resolved "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz#b5f259c82cd6e336921efd7bfd8bf560de9eeedf"