Skip to content

Commit

Permalink
Merge pull request #32 from Brooooooklyn/upgrade-to-latest-napi
Browse files Browse the repository at this point in the history
feat: napi-rs 2.0
  • Loading branch information
Brooooooklyn authored Dec 20, 2021
2 parents 6f3fa47 + a37b15c commit 7e75cd7
Show file tree
Hide file tree
Showing 27 changed files with 523 additions and 434 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: [Brooooooklyn]
github: [Brooooooklyn]
28 changes: 21 additions & 7 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ jobs:
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 && strip blake.linux-x64-gnu.node
docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip *.node
- host: ubuntu-latest
target: x86_64-unknown-linux-musl
architecture: x64
docker: |
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 && strip blake.linux-x64-musl.node
build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder yarn build && strip *.node
- host: macos-latest
target: aarch64-apple-darwin
build: |
Expand All @@ -67,7 +67,7 @@ jobs:
sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y
build: |
yarn build --target=aarch64-unknown-linux-gnu
aarch64-linux-gnu-strip blake.linux-arm64-gnu.node
aarch64-linux-gnu-strip *.node
- host: ubuntu-latest
architecture: x64
target: armv7-unknown-linux-gnueabihf
Expand All @@ -76,15 +76,27 @@ jobs:
sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y
build: |
yarn build --target=armv7-unknown-linux-gnueabihf
arm-linux-gnueabihf-strip blake.linux-arm-gnueabihf.node
arm-linux-gnueabihf-strip *.node
- host: ubuntu-latest
architecture: x64
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 CC="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang"
export CXX="${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}"
yarn build --target aarch64-linux-android
${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip *.node
- host: ubuntu-latest
architecture: x64
target: armv7-linux-androideabi
build: |
export CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang"
export CC="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang"
export CXX="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang++"
export PATH="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}"
yarn build --target armv7-linux-androideabi
${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip *.node
- host: ubuntu-latest
architecture: x64
target: aarch64-unknown-linux-musl
Expand All @@ -93,7 +105,7 @@ jobs:
docker pull ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine
docker tag ghcr.io/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 sh -c "rustup toolchain install $(cat ./rust-toolchain) && rustup target add aarch64-unknown-linux-musl && yarn build --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip blake.linux-arm64-musl.node"
docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder sh -c "rustup toolchain install $(cat ./rust-toolchain) && rustup target add aarch64-unknown-linux-musl && yarn build --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node"
- host: windows-latest
architecture: x64
target: aarch64-pc-windows-msvc
Expand Down Expand Up @@ -153,6 +165,7 @@ jobs:
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
build-freebsd:
runs-on: macos-10.15
name: Build FreeBSD
Expand Down Expand Up @@ -201,6 +214,7 @@ jobs:
with:
name: bindings-freebsd
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
test-macOS-windows-binding:
name: Test bindings on ${{ matrix.settings.target }} - node@${{ matrix.node }}
needs:
Expand Down Expand Up @@ -277,7 +291,7 @@ jobs:
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/blake -w /blake node:${{ matrix.node }}-slim yarn test
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-slim yarn test
test-linux-x64-musl-binding:
name: Test bindings on x86_64-unknown-linux-musl - node@${{ matrix.node }}
needs:
Expand Down Expand Up @@ -314,7 +328,7 @@ jobs:
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/blake -w /blake node:${{ matrix.node }}-alpine yarn test
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-alpine yarn test
test-linux-aarch64-gnu-binding:
name: Test bindings on aarch64-unknown-linux-gnu - node@${{ matrix.node }}
needs:
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ blake2b_simd = "1.0"
blake2s_simd = "1.0"
blake3 = "1"
hex = "0.4"
napi = "2.0.0-beta.7"
napi-derive = "2.0.0-beta.5"
napi = "2.0.0"
napi-derive = "2.0.0"
ryu = "1"

[target.'cfg(all(not(all(target_os = "linux", target_arch = "aarch64", target_env = "musl")), not(all(target_os = "windows", target_arch = "aarch64"))))'.dependencies]
Expand Down
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Node.js binding for https://github.com/BLAKE3-team/BLAKE3. High performance, and
| Linux arm64 gnu ||||
| Linux arm64 musl ||||
| Android arm64 ||||
| Android armv7 ||||
| FreeBSD x64 ||||

## Blake2
Expand All @@ -30,24 +31,24 @@ Support `blake2b` `blake2bp` `blake2s` `blake2sp` algorithm.
### Unkeyed Hash

```js
import { Blake2BHasher } from "@napi-rs/blake-hash";
import { Blake2BHasher } from '@napi-rs/blake-hash'

const hasher = new Blake2BHasher();
hasher.update("content to be hash");
hasher.digest("hex"); // could also be `base64` or `url-safe-base64`
const hasher = new Blake2BHasher()
hasher.update('content to be hash')
hasher.digest('hex') // could also be `base64` or `url-safe-base64`
```

### Keyed Hash

```js
import { Blake2BHasher, Blake2BParam } from "@napi-rs/blake-hash";
import { Blake2BHasher, Blake2BParam } from '@napi-rs/blake-hash'

const hashParams = new Blake2BParam();
hashParams.personal("someone@email.com");
const hash = Blake2BHasher.withParams(hashParams);
const hashParams = new Blake2BParam()
hashParams.personal('someone@email.com')
const hash = Blake2BHasher.withParams(hashParams)

hash.update("your secret");
hash.digest("hex");
hash.update('your secret')
hash.digest('hex')
```

## Blake3
Expand All @@ -57,33 +58,33 @@ hash.digest("hex");
#### Default hash function

```js
import { blake3 } from "@napi-rs/blake-hash";
import { blake3 } from '@napi-rs/blake-hash'

blake3("hello"); //ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
blake3('hello') //ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
```

#### Hasher

```js
import { Blake3Hasher } from "@napi-rs/blake-hash";
import { Blake3Hasher } from '@napi-rs/blake-hash'

const hasher = new Blake3Hasher();
hasher.update("hello");
hasher.digest("hex"); // ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
const hasher = new Blake3Hasher()
hasher.update('hello')
hasher.digest('hex') // ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
```

### `KeyedHash`

> Full documentation: https://docs.rs/blake3/latest/blake3/fn.keyed_hash.html
```js
import { randomBytes } from "crypto";
import { randomBytes } from 'crypto'

import { Blake3Hasher } from "@napi-rs/blake-hash";
import { Blake3Hasher } from '@napi-rs/blake-hash'

const hasher = Blake3Hasher.newKeyed(randomBytes(32)); // The key must be 32 bytes
hasher.update("hello");
hasher.digest("hex"); // 9e8e05888735e59036c1ec66938f5bdb2b3933ce647918b739c23b699f1431a3
const hasher = Blake3Hasher.newKeyed(randomBytes(32)) // The key must be 32 bytes
hasher.update('hello')
hasher.digest('hex') // 9e8e05888735e59036c1ec66938f5bdb2b3933ce647918b739c23b699f1431a3
```

### `DeriveKey`
Expand All @@ -105,13 +106,13 @@ Full documentation: https://docs.rs/blake3/latest/blake3/fn.derive_key.html
> [argon2]: https://en.wikipedia.org/wiki/Argon2
```js
import { Blake3Hasher } from "@napi-rs/blake-hash";
import { Blake3Hasher } from '@napi-rs/blake-hash'

const context = "BLAKE3 2021-11-10 12:13:59 example context";
const context = 'BLAKE3 2021-11-10 12:13:59 example context'

const hasher = Blake3Hasher.newDeriveKey(context);
hasher.update("hello");
hasher.digest("hex"); // e186adf36b0c4e421b2baa881e158a4b3b074626882a6e1dfb231aebb7e149ee
const hasher = Blake3Hasher.newDeriveKey(context)
hasher.update('hello')
hasher.digest('hex') // e186adf36b0c4e421b2baa881e158a4b3b074626882a6e1dfb231aebb7e149ee
```

## Performance
Expand Down
8 changes: 4 additions & 4 deletions __test__/blake2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ test('blake2b', (t) => {
const hasher = new Blake2BHasher()
t.is(
hasher.update('hello').digest('hex'),
'e4cfa39a3d37be31c59609e807970799caa68a19bfaa15135f165085e01d41a65ba1e1b146aeb6bd0092b49eac214c103ccfa3a365954bbbe52f74a2b3620c94'
'e4cfa39a3d37be31c59609e807970799caa68a19bfaa15135f165085e01d41a65ba1e1b146aeb6bd0092b49eac214c103ccfa3a365954bbbe52f74a2b3620c94',
)
})

test('blake2bp', (t) => {
const hasher = new Blake2BpHasher()
t.is(
hasher.update('hello').digest('hex'),
'3d9b524855d3675f3ccbe8e189b3f00a2712ba7301f9b88a7e31aad4916777459953a70f9c98869bc39872591c30e6dfa5b5decbfcf977c909db9f9f7e4441d1'
'3d9b524855d3675f3ccbe8e189b3f00a2712ba7301f9b88a7e31aad4916777459953a70f9c98869bc39872591c30e6dfa5b5decbfcf977c909db9f9f7e4441d1',
)
})

test('blake2s', (t) => {
const hasher = new Blake2SHasher()
t.is(
hasher.update('hello').digest('hex'),
'19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25'
'19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25',
)
})

test('blake2sp', (t) => {
const hasher = new Blake2SpHasher()
t.is(
hasher.update('hello').digest('hex'),
'223dfe42565ddf97210b34a384860b603717d5c63c1872c9fc99f1b15de6631b'
'223dfe42565ddf97210b34a384860b603717d5c63c1872c9fc99f1b15de6631b',
)
})
4 changes: 2 additions & 2 deletions __test__/blake3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('blake3', (t) => {
const hasher = new Blake3Hasher()
t.is(
hasher.update('hello world').digest('hex'),
'd74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24'
'd74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24',
)
})

Expand All @@ -15,6 +15,6 @@ test('blake3 keyed', (t) => {
const hasher = Blake3Hasher.newKeyed(derivedKey)
t.is(
hasher.update('hello world').digest('hex'),
'e654a33cb9b9573b8cf9f4a3c5c8bc19dbfeb6362584ee55b1545e98492650f0'
'e654a33cb9b9573b8cf9f4a3c5c8bc19dbfeb6362584ee55b1545e98492650f0',
)
})
21 changes: 16 additions & 5 deletions bench-next.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { promises as fs } from 'fs'
import xxhash from '@node-rs/xxhash'
import b from 'benny'

import { Blake2BHasher, Blake2BpHasher, Blake2SpHasher, Blake2SHasher, Blake3Hasher, blake3UrlSafeBase64 } from './index.js'
import {
Blake2BHasher,
Blake2BpHasher,
Blake2SpHasher,
Blake2SHasher,
Blake3Hasher,
blake3UrlSafeBase64,
} from './index.js'

const FIXTURE = [3, 'http://abc.xyz/logo.webp', 256, 0.9, 'image/webp']
const BIG_IMAGE = await fs.readFile('./anime-girl.png')
Expand Down Expand Up @@ -56,10 +63,13 @@ function getURLSafeHashBlake3(items) {
}

function bigIntToBase64UrlSafe(bigInt) {
return Buffer.from(bigInt.toString(16), 'hex').toString('base64').replace(/\//g, '-')
return Buffer.from(bigInt.toString(16), 'hex')
.toString('base64')
.replace(/\//g, '-')
}

await b.suite('digest hash into url-safe-base64',
await b.suite(
'digest hash into url-safe-base64',
b.add('blake3', () => {
getURLSafeHashBlake3(FIXTURE)
}),
Expand All @@ -85,7 +95,8 @@ await b.suite('digest hash into url-safe-base64',
b.complete(),
)

await b.suite('digest big file',
await b.suite(
'digest big file',
b.add('blake3', () => {
const hash = new Blake3Hasher()
hash.update(BIG_IMAGE)
Expand Down Expand Up @@ -129,4 +140,4 @@ await b.suite('digest big file',
}),
b.cycle(),
b.complete(),
)
)
9 changes: 6 additions & 3 deletions bench.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Blake2BHasher, Blake2SHasher, Blake3Hasher } from './index.js'

const BIG_IMAGE = await fs.readFile('./anime-girl.png')

await b.suite('digest big file blake2b',
await b.suite(
'digest big file blake2b',
b.add('blake2b-napi', () => {
const hash = new Blake2BHasher()
hash.update(BIG_IMAGE)
Expand All @@ -23,7 +24,8 @@ await b.suite('digest big file blake2b',
b.complete(),
)

await b.suite('digest big file blake2s',
await b.suite(
'digest big file blake2s',
b.add('blake2s-napi', () => {
const hash = new Blake2SHasher()
hash.update(BIG_IMAGE)
Expand All @@ -38,7 +40,8 @@ await b.suite('digest big file blake2s',
b.complete(),
)

await b.suite('digest big file blake3',
await b.suite(
'digest big file blake3',
b.add('blake3-napi', () => {
const hash = new Blake3Hasher()
hash.update(BIG_IMAGE)
Expand Down
Loading

0 comments on commit 7e75cd7

Please sign in to comment.