Skip to content

Commit 5c2c322

Browse files
authored
Merge pull request #527 from napi-rs/xxh3
Xxh3
2 parents ed702de + ae614c6 commit 5c2c322

File tree

7 files changed

+290
-25
lines changed

7 files changed

+290
-25
lines changed

.cargo/config.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[target.x86_64-pc-windows-msvc]
2-
rustflags = ["-C", "target-cpu=skylake"]
3-
41
[target.aarch64-unknown-linux-gnu]
52
linker = "aarch64-linux-gnu-gcc"
63

README.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# node-rs
22

3+
![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg)
4+
35
When `Node.js` meet `Rust` = 🚀
46

57
# napi-rs
@@ -8,26 +10,27 @@ Make rust crates binding to Node.js use [napi-rs](https://github.com/napi-rs/nap
810

911
# Support matrix
1012

11-
| | node12 | node14 | node16 |
12-
| ---------------- | ------ | ------ | ------ |
13-
| Windows x64 ||||
14-
| Windows x32 ||||
15-
| Windows arm64 ||||
16-
| macOS x64 ||||
17-
| macOS arm64 ||||
18-
| Linux x64 gnu ||||
19-
| Linux x64 musl ||||
20-
| Linux arm gnu ||||
21-
| Linux arm64 gnu ||||
22-
| Linux arm64 musl ||||
23-
| Android arm64 ||||
24-
| FreeBSD x64 ||||
13+
| | node12 | node14 | node16 |
14+
| --------------------- | ------ | ------ | ------ |
15+
| Windows x64 ||||
16+
| Windows x32 ||||
17+
| Windows arm64 ||||
18+
| macOS x64 ||||
19+
| macOS arm64 (m chips) ||||
20+
| Linux x64 gnu ||||
21+
| Linux x64 musl ||||
22+
| Linux arm gnu ||||
23+
| Linux arm64 gnu ||||
24+
| Linux arm64 musl ||||
25+
| Android arm64 ||||
26+
| FreeBSD x64 ||||
2527

2628
# Packages
2729

28-
| Package | Status | Downloads | Description |
29-
| -------------------------------------------- | -------------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------ |
30-
| [`@node-rs/crc32`](./packages/crc32) | ![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg) | ![](https://img.shields.io/npm/dm/@node-rs/crc32.svg?sanitize=true) | Fastest `CRC32` implementation using `SIMD` |
31-
| [`@node-rs/jieba`](./packages/jieba) | ![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg) | ![](https://img.shields.io/npm/dm/@node-rs/jieba.svg?sanitize=true) | [`jieba-rs`](https://github.com/messense/jieba-rs) binding |
32-
| [`@node-rs/bcrypt`](./packages/bcrypt) | ![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg) | ![](https://img.shields.io/npm/dm/@node-rs/bcrypt.svg?sanitize=true) | Fastest bcrypt implementation |
33-
| [`@node-rs/deno-lint`](./packages/deno-lint) | ![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg) | ![](https://img.shields.io/npm/dm/@node-rs/deno-lint.svg?sanitize=true) | [deno_lint](https://github.com/denoland/deno_lint) Node.js binding |
30+
| Package | Version | Downloads | Description |
31+
| -------------------------------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------- |
32+
| [`@node-rs/crc32`](./packages/crc32) | ![](https://img.shields.io/npm/v/@node-rs/crc32.svg) | ![](https://img.shields.io/npm/dm/@node-rs/crc32.svg?sanitize=true) | Fastest `CRC32` implementation using `SIMD` |
33+
| [`@node-rs/jieba`](./packages/jieba) | ![](https://img.shields.io/npm/v/@node-rs/jieba.svg) | ![](https://img.shields.io/npm/dm/@node-rs/jieba.svg?sanitize=true) | [`jieba-rs`](https://github.com/messense/jieba-rs) binding |
34+
| [`@node-rs/bcrypt`](./packages/bcrypt) | ![](https://img.shields.io/npm/v/@node-rs/bcrypt.svg) | ![](https://img.shields.io/npm/dm/@node-rs/bcrypt.svg?sanitize=true) | Fastest bcrypt implementation |
35+
| [`@node-rs/deno-lint`](./packages/deno-lint) | ![](https://img.shields.io/npm/v/@node-rs/deno-lint.svg) | ![](https://img.shields.io/npm/dm/@node-rs/deno-lint.svg?sanitize=true) | [deno_lint](https://github.com/denoland/deno_lint) Node.js binding |
36+
| [`@node-rs/xxhash`](./packages/xxhash) | ![](https://img.shields.io/npm/v/@node-rs/xxhash.svg) | ![](https://img.shields.io/npm/dm/@node-rs/xxhash.svg?sanitize=true) | [`xxhash-rust`](https://github.com/DoumanAsh/xxhash-rust) Node.js binding |

packages/xxhash/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
> 🚀 Help me to become a full-time open-source developer by [sponsoring me on Github](https://github.com/sponsors/Brooooooklyn)
88
9-
Fastest `xxhash` implementation in Node.js.
9+
[`xxhash-rust`](https://github.com/DoumanAsh/xxhash-rust) binding for Node.js.
1010

1111
## Install this package
1212

@@ -52,12 +52,31 @@ export class Xxh32 {
5252
constructor(seed?: number)
5353
update(input: BufferLike): this
5454
digest(): number
55+
reset(): void
5556
}
5657

5758
export class Xxh64 {
5859
constructor(seed?: BigInt)
5960
update(input: BufferLike): this
6061
digest(): BigInt
62+
reset(): void
63+
}
64+
65+
export class Xxh3 {
66+
static withSeed(seed?: BigInt): Xxh3
67+
static withSecret(secret: BufferLike): Xxh3
68+
private constructor() {}
69+
update(input: BufferLike): this
70+
digest(): BigInt
71+
reset(): void
72+
}
73+
74+
export const xxh3: {
75+
xxh64: (input: BufferLike, seed?: BigInt) => BigInt
76+
xxh64WithSecret: (input: BufferLike, secret: BufferLike) => BigInt
77+
xxh128: (input: BufferLike, seed?: BigInt) => BigInt
78+
xxh128WithSecret: (input: BufferLike, secret: BufferLike) => BigInt
79+
Xxh3: typeof Xxh3
6180
}
6281
```
6382
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import test from 'ava'
2+
3+
import { xxh3 } from '../index'
4+
5+
const SEC_WITH_192_LENGTH = Buffer.from(
6+
'515293b422141cabb24c131a914d54d767738ce3f46141d91dfdfffa8b2e7ada507318f242dd112f28f213cfc1c4aba1e641e8a7f103746cc542d66668607e2ea4fce4f08640780d0bcd9f171a31f8932ae617033afd5e100c3fb6f0b5b9be611419d79c2bf2358ba1c8562ae24dd1aa2619ab30dcfaa9b8f3363b2a350e750a6aae7e307d16b1d3250f7ed6315ec127fac8643dfcb733ffe622bbc97a3097c6eabd24dee519bc7817e0e8195a426b07ad7452f6ee72465e065afe56e498a450',
7+
'hex',
8+
)
9+
10+
test('xxh64 string', (t) => {
11+
t.is(xxh3.xxh64('hello world'), BigInt('15296390279056496779'))
12+
})
13+
14+
test('xxh64 Buffer', (t) => {
15+
t.is(xxh3.xxh64(Buffer.from('hello world')), BigInt('15296390279056496779'))
16+
})
17+
18+
test('xxh64 with seed', (t) => {
19+
t.is(xxh3.xxh64(Buffer.from('hello world'), BigInt(128)), BigInt('18072542215751182891'))
20+
})
21+
22+
test('xxh64 with secret', (t) => {
23+
t.is(xxh3.xxh64WithSecret('hello world', SEC_WITH_192_LENGTH), BigInt('8365614992180151249'))
24+
})
25+
26+
test('xxh128 string', (t) => {
27+
t.is(xxh3.xxh128('hello world'), BigInt('297150157938599054391163723952090887879'))
28+
})
29+
30+
test('xxh128 buffer', (t) => {
31+
t.is(xxh3.xxh128(Buffer.from('hello world')), BigInt('297150157938599054391163723952090887879'))
32+
})
33+
34+
test('xxh128 with seed', (t) => {
35+
t.is(xxh3.xxh128(Buffer.from('hello world'), BigInt(128)), BigInt('248039115514001876413444952452915338056'))
36+
})
37+
38+
test('xxh128 with secret', (t) => {
39+
t.is(xxh3.xxh128WithSecret('hello world', SEC_WITH_192_LENGTH), BigInt('169165111715981571090973585540606896681'))
40+
})
41+
42+
test('Xxh3 withSeed', (t) => {
43+
const instance = xxh3.Xxh3.withSeed()
44+
t.true(instance instanceof xxh3.Xxh3)
45+
t.is(instance.update('hello world').digest(), BigInt('15296390279056496779'))
46+
t.is(instance.update(Buffer.from('hello world')).digest(), BigInt('16495854690286049632'))
47+
instance.reset()
48+
t.is(instance.update('hello world').digest(), BigInt('15296390279056496779'))
49+
})
50+
51+
test('Xxh3 withSecret', (t) => {
52+
const instance = xxh3.Xxh3.withSecret(SEC_WITH_192_LENGTH)
53+
t.true(instance instanceof xxh3.Xxh3)
54+
t.is(instance.update('hello world').digest(), BigInt('8365614992180151249'))
55+
t.is(instance.update(Buffer.from('hello world')).digest(), BigInt('14168446104542996972'))
56+
instance.reset()
57+
t.is(instance.update('hello world').digest(), BigInt('8365614992180151249'))
58+
})

packages/xxhash/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,29 @@ export class Xxh32 {
1414
constructor(seed?: number)
1515
update(input: BufferLike): this
1616
digest(): number
17+
reset(): void
1718
}
1819

1920
export class Xxh64 {
2021
constructor(seed?: BigInt)
2122
update(input: BufferLike): this
2223
digest(): BigInt
24+
reset(): void
25+
}
26+
27+
export class Xxh3 {
28+
static withSeed(seed?: BigInt): Xxh3
29+
static withSecret(secret: BufferLike): Xxh3
30+
private constructor() {}
31+
update(input: BufferLike): this
32+
digest(): BigInt
33+
reset(): void
34+
}
35+
36+
export const xxh3: {
37+
xxh64: (input: BufferLike, seed?: BigInt) => BigInt
38+
xxh64WithSecret: (input: BufferLike, secret: BufferLike) => BigInt
39+
xxh128: (input: BufferLike, seed?: BigInt) => BigInt
40+
xxh128WithSecret: (input: BufferLike, secret: BufferLike) => BigInt
41+
Xxh3: typeof Xxh3
2342
}

packages/xxhash/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,30 @@ const {
55
xxh64: _xxh64,
66
Xxh32: _Xxh32,
77
Xxh64: _Xxh64,
8+
xxh3,
89
} = loadBinding(__dirname, 'xxhash', '@node-rs/xxhash')
910

11+
class Xxh3 {
12+
update(data) {
13+
return xxh3.update.call(this, Buffer.from(data))
14+
}
15+
}
16+
17+
Xxh3.withSecret = function withSecret(secret) {
18+
const instance = new Xxh3()
19+
xxh3.createXxh3WithSecret(instance, Buffer.from(secret))
20+
return instance
21+
}
22+
23+
Xxh3.withSeed = function withSeed(seed = BigInt(0)) {
24+
const instance = new Xxh3()
25+
xxh3.createXxh3WithSeed(instance, seed)
26+
return instance
27+
}
28+
29+
Xxh3.prototype.digest = xxh3.digest
30+
Xxh3.prototype.reset = xxh3.reset
31+
1032
module.exports = {
1133
xxh32: function xxh32(input, seed) {
1234
return _xxh32(Buffer.from(input), seed == null ? 0 : seed)
@@ -24,4 +46,19 @@ module.exports = {
2446
return super.update(Buffer.from(input))
2547
}
2648
},
49+
xxh3: {
50+
xxh64: function xxh64(input, seed) {
51+
return xxh3.xxh64(Buffer.from(input), seed == null ? BigInt(0) : seed)
52+
},
53+
xxh64WithSecret(input, secret) {
54+
return xxh3.xxh64WithSecret(Buffer.from(input), Buffer.from(secret))
55+
},
56+
xxh128: function xxh128(input, seed) {
57+
return xxh3.xxh128(Buffer.from(input), seed == null ? BigInt(0) : seed)
58+
},
59+
xxh128WithSecret(input, secret) {
60+
return xxh3.xxh128WithSecret(Buffer.from(input), Buffer.from(secret))
61+
},
62+
Xxh3,
63+
},
2764
}

0 commit comments

Comments
 (0)