Skip to content

Commit 2883992

Browse files
committed
feat(*): upgrade to NAPI-RS 3.0 alpha
1 parent f27d119 commit 2883992

26 files changed

+143
-198
lines changed

.taplo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ exclude = ["node_modules/**/*.toml"]
33
# https://taplo.tamasfe.dev/configuration/formatter-options.html
44
[formatting]
55
align_entries = true
6+
column_width = 180
67
indent_tables = true
78
reorder_keys = true

Cargo.toml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
[workspace]
2-
members = [
3-
"./crates/alloc",
4-
"./packages/argon2",
5-
"./packages/bcrypt",
6-
"./packages/crc32",
7-
"./packages/jieba",
8-
"./packages/jsonwebtoken",
9-
"./packages/xxhash",
10-
]
2+
members = ["./crates/alloc", "./packages/argon2", "./packages/bcrypt", "./packages/crc32", "./packages/jieba", "./packages/jsonwebtoken", "./packages/xxhash"]
113
resolver = "2"
124

5+
[workspace.dependencies]
6+
argon2 = { version = "0.5", features = ["rand"] }
7+
base64 = { version = "0.22" }
8+
bcrypt = "0.15"
9+
blowfish = { version = "0.9", features = ["bcrypt"] }
10+
crc32c = { version = "0.6" }
11+
crc32fast = { version = "1.4", features = ["nightly"] }
12+
getrandom = "0.2"
13+
global_alloc = { path = "./crates/alloc" }
14+
indexmap = { version = "2", features = ["serde"] }
15+
jieba-rs = { version = "0.6", features = ["default-dict", "tfidf", "textrank"] }
16+
jsonwebtoken = { version = "9" }
17+
napi = { version = "3.0.0-alpha.13", default-features = false, features = ["napi3"] }
18+
napi-build = "2"
19+
napi-derive = { version = "3.0.0-alpha.13", default-features = false, features = ["type-def"] }
20+
once_cell = "1"
21+
quickcheck = "1.0"
22+
rand_core = { version = "0.6", features = ["getrandom"] }
23+
serde = "1.0"
24+
serde_json = "1.0"
25+
xxhash-rust = { version = "0.8", features = ["xxh32", "xxh64", "xxh3"] }
1326
[profile.release]
1427
codegen-units = 1
1528
lto = true

packages/argon2/Cargo.toml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ version = "0.0.0"
77
crate-type = ["cdylib"]
88

99
[dependencies]
10-
argon2 = { version = "0.5", features = ["rand"] }
11-
global_alloc = { path = "../../crates/alloc" }
12-
napi = { version = "2", default-features = false, features = ["napi3"] }
13-
napi-derive = { version = "2", default-features = false, features = [
14-
"type-def",
15-
] }
16-
rand_core = { version = "0.6", features = ["getrandom"] }
10+
argon2 = { workspace = true }
11+
global_alloc = { workspace = true }
12+
napi = { workspace = true, features = ["napi3"] }
13+
napi-derive = { workspace = true, default-features = false, features = ["type-def"] }
14+
rand_core = { workspace = true }
1715

1816
[build-dependencies]
19-
napi-build = "2"
17+
napi-build = { workspace = true }

packages/argon2/index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
3-
export const enum Algorithm {
3+
export declare const enum Algorithm {
44
/**
55
* Optimizes against GPU cracking attacks but vulnerable to side-channels.
66
* Accesses the memory array in a password dependent order, reducing the possibility of time–memory tradeoff (TMTO) attacks.
@@ -19,13 +19,13 @@ export const enum Algorithm {
1919
Argon2id = 2
2020
}
2121

22-
export declare function hash(password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<string>
22+
export declare function hash(password: string | Uint8Array, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<string>
2323

24-
export declare function hashRaw(password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<Buffer>
24+
export declare function hashRaw(password: string | Uint8Array, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<Buffer>
2525

26-
export declare function hashRawSync(password: string | Buffer, options?: Options | undefined | null): Buffer
26+
export declare function hashRawSync(password: string | Uint8Array, options?: Options | undefined | null): Buffer
2727

28-
export declare function hashSync(password: string | Buffer, options?: Options | undefined | null): string
28+
export declare function hashSync(password: string | Uint8Array, options?: Options | undefined | null): string
2929

3030
export interface Options {
3131
/**
@@ -60,15 +60,15 @@ export interface Options {
6060
parallelism?: number
6161
algorithm?: Algorithm
6262
version?: Version
63-
secret?: Buffer
64-
salt?: Buffer
63+
secret?: Uint8Array
64+
salt?: Uint8Array
6565
}
6666

67-
export declare function verify(hashed: string | Buffer, password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<boolean>
67+
export declare function verify(hashed: string | Uint8Array, password: string | Uint8Array, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise<boolean>
6868

69-
export declare function verifySync(hashed: string | Buffer, password: string | Buffer, options?: Options | undefined | null): boolean
69+
export declare function verifySync(hashed: string | Uint8Array, password: string | Uint8Array, options?: Options | undefined | null): boolean
7070

71-
export const enum Version {
71+
export declare const enum Version {
7272
/** Version 16 (0x10 in hex) */
7373
V0x10 = 0,
7474
/**

packages/argon2/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,15 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
336336
nativeBinding = require('./argon2.wasi.cjs')
337337
} catch (err) {
338338
if (process.env.NAPI_RS_FORCE_WASI) {
339-
console.error(err)
339+
loadErrors.push(err)
340340
}
341341
}
342342
if (!nativeBinding) {
343343
try {
344344
nativeBinding = require('@node-rs/argon2-wasm32-wasi')
345345
} catch (err) {
346346
if (process.env.NAPI_RS_FORCE_WASI) {
347-
console.error(err)
347+
loadErrors.push(err)
348348
}
349349
}
350350
}

packages/argon2/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use argon2::{
1313
use rand_core::OsRng;
1414

1515
#[napi]
16+
#[derive(Clone, Copy)]
1617
pub enum Algorithm {
1718
/// Optimizes against GPU cracking attacks but vulnerable to side-channels.
1819
/// Accesses the memory array in a password dependent order, reducing the possibility of time–memory tradeoff (TMTO) attacks.
@@ -40,6 +41,7 @@ impl Algorithm {
4041
}
4142

4243
#[napi]
44+
#[derive(Clone, Copy)]
4345
pub enum Version {
4446
/// Version 16 (0x10 in hex)
4547
V0x10,
@@ -89,8 +91,8 @@ pub struct Options {
8991
pub parallelism: Option<u32>,
9092
pub algorithm: Option<Algorithm>,
9193
pub version: Option<Version>,
92-
pub secret: Option<Buffer>,
93-
pub salt: Option<Buffer>,
94+
pub secret: Option<Uint8Array>,
95+
pub salt: Option<Uint8Array>,
9496
}
9597

9698
impl Options {
@@ -140,7 +142,7 @@ impl Task for HashTask {
140142

141143
#[napi]
142144
pub fn hash(
143-
password: Either<String, Buffer>,
145+
password: Either<String, &[u8]>,
144146
options: Option<Options>,
145147
abort_signal: Option<AbortSignal>,
146148
) -> AsyncTask<HashTask> {
@@ -159,7 +161,7 @@ pub fn hash(
159161
#[napi]
160162
pub fn hash_sync(
161163
env: Env,
162-
password: Either<String, Buffer>,
164+
password: Either<String, &[u8]>,
163165
options: Option<Options>,
164166
) -> Result<String> {
165167
let mut hash_task = HashTask {
@@ -216,7 +218,7 @@ impl Task for RawHashTask {
216218

217219
#[napi]
218220
pub fn hash_raw(
219-
password: Either<String, Buffer>,
221+
password: Either<String, &[u8]>,
220222
options: Option<Options>,
221223
abort_signal: Option<AbortSignal>,
222224
) -> AsyncTask<RawHashTask> {
@@ -235,7 +237,7 @@ pub fn hash_raw(
235237
#[napi]
236238
pub fn hash_raw_sync(
237239
env: Env,
238-
password: Either<String, Buffer>,
240+
password: Either<String, &[u8]>,
239241
options: Option<Options>,
240242
) -> Result<Buffer> {
241243
let mut hash_task = RawHashTask {
@@ -279,8 +281,8 @@ impl Task for VerifyTask {
279281

280282
#[napi]
281283
pub fn verify(
282-
hashed: Either<String, Buffer>,
283-
password: Either<String, Buffer>,
284+
hashed: Either<String, &[u8]>,
285+
password: Either<String, &[u8]>,
284286
options: Option<Options>,
285287
abort_signal: Option<AbortSignal>,
286288
) -> Result<AsyncTask<VerifyTask>> {
@@ -305,8 +307,8 @@ pub fn verify(
305307
#[napi]
306308
pub fn verify_sync(
307309
env: Env,
308-
hashed: Either<String, Buffer>,
309-
password: Either<String, Buffer>,
310+
hashed: Either<String, &[u8]>,
311+
password: Either<String, &[u8]>,
310312
options: Option<Options>,
311313
) -> Result<bool> {
312314
let mut verify_task = VerifyTask {

packages/bcrypt/Cargo.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ version = "0.1.0"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
base64 = { version = "0.22" }
12-
bcrypt = "0.15"
13-
blowfish = { version = "0.9", features = ["bcrypt"] }
14-
getrandom = "0.2"
15-
global_alloc = { path = "../../crates/alloc" }
16-
napi = { version = "2", default-features = false, features = ["napi3"] }
17-
napi-derive = { version = "2" }
11+
base64 = { workspace = true }
12+
bcrypt = { workspace = true }
13+
blowfish = { workspace = true }
14+
getrandom = { workspace = true }
15+
global_alloc = { workspace = true }
16+
napi = { workspace = true, default-features = false, features = ["napi3"] }
17+
napi-derive = { workspace = true }
1818

1919
[dev-dependencies]
20-
quickcheck = "1.0"
20+
quickcheck = { workspace = true }
2121

2222
[build-dependencies]
23-
napi-build = "2"
23+
napi-build = { workspace = true }

packages/bcrypt/binding.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export declare function genSalt(round: number, version?: '2a' | '2x' | '2y' | '2
66

77
export declare function genSaltSync(round: number, version?: '2a' | '2x' | '2y' | '2b'): string
88

9-
export declare function hash(input: string | Buffer, cost?: number | undefined | null, salt?: string | Buffer | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>
9+
export declare function hash(input: Uint8Array | string, cost?: number | undefined | null, salt?: string | Buffer | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>
1010

1111
export declare function hashSync(input: string | Buffer, cost?: number | undefined | null, salt?: string | Buffer | undefined | null): string
1212

13-
export declare function verify(password: string | Buffer, hash: string | Buffer, signal?: AbortSignal | undefined | null): Promise<boolean>
13+
export declare function verify(password: Uint8Array | string, hash: Uint8Array | string, signal?: AbortSignal | undefined | null): Promise<boolean>
1414

1515
export declare function verifySync(input: string | Buffer, hash: string | Buffer): boolean
1616

packages/bcrypt/binding.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,15 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
336336
nativeBinding = require('./bcrypt.wasi.cjs')
337337
} catch (err) {
338338
if (process.env.NAPI_RS_FORCE_WASI) {
339-
console.error(err)
339+
loadErrors.push(err)
340340
}
341341
}
342342
if (!nativeBinding) {
343343
try {
344344
nativeBinding = require('@node-rs/bcrypt-wasm32-wasi')
345345
} catch (err) {
346346
if (process.env.NAPI_RS_FORCE_WASI) {
347-
console.error(err)
347+
loadErrors.push(err)
348348
}
349349
}
350350
}

packages/bcrypt/src/hash_task.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
11
use napi::{
2-
bindgen_prelude::Either, Env, Error, JsBuffer, JsBufferValue, Ref, Result, Status, Task,
2+
bindgen_prelude::{Either, Uint8Array},
3+
Env, Error, Result, Status, Task,
34
};
45
use napi_derive::napi;
56

6-
pub enum AsyncHashInput {
7-
String(String),
8-
Buffer(Ref<JsBufferValue>),
9-
}
10-
11-
impl AsyncHashInput {
12-
#[inline]
13-
pub fn from_either(input: Either<String, JsBuffer>) -> Result<Self> {
14-
match input {
15-
Either::A(s) => Ok(Self::String(s)),
16-
Either::B(b) => Ok(Self::Buffer(b.into_ref()?)),
17-
}
18-
}
19-
}
20-
21-
impl AsRef<[u8]> for AsyncHashInput {
22-
#[inline]
23-
fn as_ref(&self) -> &[u8] {
24-
match self {
25-
Self::String(s) => s.as_bytes(),
26-
Self::Buffer(b) => b.as_ref(),
27-
}
28-
}
29-
}
30-
317
pub struct HashTask {
32-
buf: AsyncHashInput,
8+
buf: Either<Uint8Array, String>,
339
cost: u32,
3410
salt: [u8; 16],
3511
}
3612

3713
impl HashTask {
3814
#[inline]
39-
pub fn new(buf: AsyncHashInput, cost: u32, salt: [u8; 16]) -> HashTask {
15+
pub fn new(buf: Either<Uint8Array, String>, cost: u32, salt: [u8; 16]) -> HashTask {
4016
HashTask { buf, cost, salt }
4117
}
4218

@@ -54,20 +30,10 @@ impl Task for HashTask {
5430
type JsValue = String;
5531

5632
fn compute(&mut self) -> Result<Self::Output> {
57-
match &self.buf {
58-
AsyncHashInput::String(s) => Self::hash(s.as_bytes(), self.salt, self.cost),
59-
AsyncHashInput::Buffer(buf) => Self::hash(buf.as_ref(), self.salt, self.cost),
60-
}
33+
Self::hash(self.buf.as_ref(), self.salt, self.cost)
6134
}
6235

6336
fn resolve(&mut self, _env: Env, output: Self::Output) -> Result<Self::JsValue> {
6437
Ok(output)
6538
}
66-
67-
fn finally(&mut self, env: Env) -> Result<()> {
68-
if let AsyncHashInput::Buffer(buf) = &mut self.buf {
69-
buf.unref(env)?;
70-
}
71-
Ok(())
72-
}
7339
}

packages/bcrypt/src/lib.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ use std::cmp;
88

99
use bcrypt::Version;
1010
use napi::bindgen_prelude::*;
11-
use napi::{Error, JsBuffer, Result, Status};
1211
use napi_derive::*;
1312

14-
use crate::hash_task::AsyncHashInput;
1513
use crate::hash_task::HashTask;
1614
use crate::salt_task::{format_salt, gen_salt};
1715
use crate::verify_task::VerifyTask;
@@ -74,7 +72,7 @@ pub fn hash_sync(
7472

7573
#[napi]
7674
pub fn hash(
77-
input: Either<String, JsBuffer>,
75+
input: Either<Uint8Array, String>,
7876
cost: Option<u32>,
7977
salt: Option<Either<String, Buffer>>,
8078
signal: Option<AbortSignal>,
@@ -89,11 +87,7 @@ pub fn hash(
8987
} else {
9088
gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?
9189
};
92-
let task = HashTask::new(
93-
AsyncHashInput::from_either(input)?,
94-
cost.unwrap_or(DEFAULT_COST),
95-
salt,
96-
);
90+
let task = HashTask::new(input, cost.unwrap_or(DEFAULT_COST), salt);
9791
Ok(AsyncTask::with_optional_signal(task, signal))
9892
}
9993

@@ -114,14 +108,11 @@ fn either_string_buffer_as_bytes(input: &Either<String, Buffer>) -> &[u8] {
114108

115109
#[napi]
116110
pub fn verify(
117-
password: Either<String, JsBuffer>,
118-
hash: Either<String, JsBuffer>,
111+
password: Either<Uint8Array, String>,
112+
hash: Either<Uint8Array, String>,
119113
signal: Option<AbortSignal>,
120114
) -> Result<AsyncTask<VerifyTask>> {
121-
let task = VerifyTask::new(
122-
AsyncHashInput::from_either(password)?,
123-
AsyncHashInput::from_either(hash)?,
124-
);
115+
let task = VerifyTask::new(password, hash);
125116
Ok(AsyncTask::with_optional_signal(task, signal))
126117
}
127118

0 commit comments

Comments
 (0)