Skip to content

Commit

Permalink
feat(*): upgrade to NAPI-RS 3.0 alpha (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Oct 3, 2024
1 parent f27d119 commit f66abff
Show file tree
Hide file tree
Showing 33 changed files with 731 additions and 794 deletions.
21 changes: 0 additions & 21 deletions .denolint.json

This file was deleted.

1 change: 1 addition & 0 deletions .taplo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ exclude = ["node_modules/**/*.toml"]
# https://taplo.tamasfe.dev/configuration/formatter-options.html
[formatting]
align_entries = true
column_width = 180
indent_tables = true
reorder_keys = true
32 changes: 23 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
[workspace]
members = [
"./crates/alloc",
"./packages/argon2",
"./packages/bcrypt",
"./packages/crc32",
"./packages/jieba",
"./packages/jsonwebtoken",
"./packages/xxhash",
]
members = ["./crates/alloc", "./packages/argon2", "./packages/bcrypt", "./packages/crc32", "./packages/jieba", "./packages/jsonwebtoken", "./packages/xxhash"]
resolver = "2"

[workspace.dependencies]
argon2 = { version = "0.5", features = ["rand"] }
base64 = { version = "0.22" }
bcrypt = "0.15"
blowfish = { version = "0.9", features = ["bcrypt"] }
crc32c = { version = "0.6" }
crc32fast = { version = "1.4", features = ["nightly"] }
getrandom = "0.2"
global_alloc = { path = "./crates/alloc" }
indexmap = { version = "2", features = ["serde"] }
jieba-rs = { version = "0.6", features = ["default-dict", "tfidf", "textrank"] }
jsonwebtoken = { version = "9" }
mimalloc = "0.1"
napi = { version = "3.0.0-alpha.13", default-features = false, features = ["napi3"] }
napi-build = "2"
napi-derive = { version = "3.0.0-alpha.13", default-features = false, features = ["type-def"] }
once_cell = "1"
quickcheck = "1.0"
rand_core = { version = "0.6", features = ["getrandom"] }
serde = "1.0"
serde_json = "1.0"
xxhash-rust = { version = "0.8", features = ["xxh32", "xxh64", "xxh3"] }
[profile.release]
codegen-units = 1
lto = true
Expand Down
4 changes: 2 additions & 2 deletions crates/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[target.'cfg(all(not(target_os = "linux"), not(target_family = "wasm")))'.dependencies]
mimalloc = { version = "0.1" }
mimalloc = { workspace = true }

[target.'cfg(target_os = "linux")'.dependencies]
mimalloc = { version = "0.1", features = ["local_dynamic_tls"] }
mimalloc = { workspace = true, features = ["local_dynamic_tls"] }
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
"postinstall": "husky install"
},
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.54",
"@napi-rs/wasm-runtime": "^0.2.4",
"@swc-node/core": "^1.13.1",
"@swc-node/register": "^1.10.0",
"@swc/core": "^1.6.7",
"@napi-rs/cli": "^3.0.0-alpha.63",
"@napi-rs/wasm-runtime": "^0.2.5",
"@swc-node/core": "^1.13.3",
"@swc-node/register": "^1.10.9",
"@swc/core": "^1.7.26",
"@taplo/cli": "^0.7.0",
"@tybys/wasm-util": "^0.9.0",
"@types/node": "^20.14.9",
"@types/node": "^22.7.4",
"ava": "^6.1.3",
"benchmark": "^2.1.4",
"codecov": "^3.8.3",
"cross-env": "^7.0.3",
"husky": "^9.0.11",
"lerna": "^8.1.5",
"lint-staged": "^15.2.7",
"npm-run-all2": "^6.2.1",
"oxlint": "^0.9.0",
"prettier": "^3.3.2",
"husky": "^9.1.6",
"lerna": "^8.1.8",
"lint-staged": "^15.2.10",
"npm-run-all2": "^6.2.3",
"oxlint": "^0.9.9",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"tslib": "^2.6.3",
"typescript": "^5.5.3"
"tslib": "^2.7.0",
"typescript": "^5.6.2"
},
"lint-staged": {
"*.@(js|ts|tsx)": [
Expand Down
14 changes: 6 additions & 8 deletions packages/argon2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ version = "0.0.0"
crate-type = ["cdylib"]

[dependencies]
argon2 = { version = "0.5", features = ["rand"] }
global_alloc = { path = "../../crates/alloc" }
napi = { version = "2", default-features = false, features = ["napi3"] }
napi-derive = { version = "2", default-features = false, features = [
"type-def",
] }
rand_core = { version = "0.6", features = ["getrandom"] }
argon2 = { workspace = true }
global_alloc = { workspace = true }
napi = { workspace = true, features = ["napi3"] }
napi-derive = { workspace = true, default-features = false, features = ["type-def"] }
rand_core = { workspace = true }

[build-dependencies]
napi-build = "2"
napi-build = { workspace = true }
21 changes: 10 additions & 11 deletions packages/argon2/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export const enum Algorithm {
export declare const enum Algorithm {
/**
* Optimizes against GPU cracking attacks but vulnerable to side-channels.
* Accesses the memory array in a password dependent order, reducing the possibility of time–memory tradeoff (TMTO) attacks.
Expand All @@ -19,13 +19,13 @@ export const enum Algorithm {
Argon2id = 2
}

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

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

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

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

export interface Options {
/**
Expand Down Expand Up @@ -60,15 +60,15 @@ export interface Options {
parallelism?: number
algorithm?: Algorithm
version?: Version
secret?: Buffer
salt?: Buffer
secret?: Uint8Array
salt?: Uint8Array
}

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

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

export const enum Version {
export declare const enum Version {
/** Version 16 (0x10 in hex) */
V0x10 = 0,
/**
Expand All @@ -77,4 +77,3 @@ export const enum Version {
*/
V0x13 = 1
}

4 changes: 2 additions & 2 deletions packages/argon2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
nativeBinding = require('./argon2.wasi.cjs')
} catch (err) {
if (process.env.NAPI_RS_FORCE_WASI) {
console.error(err)
loadErrors.push(err)
}
}
if (!nativeBinding) {
try {
nativeBinding = require('@node-rs/argon2-wasm32-wasi')
} catch (err) {
if (process.env.NAPI_RS_FORCE_WASI) {
console.error(err)
loadErrors.push(err)
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions packages/argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use argon2::{
use rand_core::OsRng;

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

#[napi]
#[derive(Clone, Copy)]
pub enum Version {
/// Version 16 (0x10 in hex)
V0x10,
Expand All @@ -59,7 +61,7 @@ impl Version {
}
}

#[napi(object)]
#[napi(object, object_to_js = false)]
#[derive(Default)]
pub struct Options {
/// The amount of memory to be used by the hash function, in kilobytes. Each thread will have a memory pool of this size. Note that large values for highly concurrent usage will cause starvation and thrashing if your system memory gets full.
Expand Down Expand Up @@ -89,8 +91,8 @@ pub struct Options {
pub parallelism: Option<u32>,
pub algorithm: Option<Algorithm>,
pub version: Option<Version>,
pub secret: Option<Buffer>,
pub salt: Option<Buffer>,
pub secret: Option<Uint8Array>,
pub salt: Option<Uint8Array>,
}

impl Options {
Expand Down Expand Up @@ -140,7 +142,7 @@ impl Task for HashTask {

#[napi]
pub fn hash(
password: Either<String, Buffer>,
password: Either<String, &[u8]>,
options: Option<Options>,
abort_signal: Option<AbortSignal>,
) -> AsyncTask<HashTask> {
Expand All @@ -159,7 +161,7 @@ pub fn hash(
#[napi]
pub fn hash_sync(
env: Env,
password: Either<String, Buffer>,
password: Either<String, &[u8]>,
options: Option<Options>,
) -> Result<String> {
let mut hash_task = HashTask {
Expand Down Expand Up @@ -216,7 +218,7 @@ impl Task for RawHashTask {

#[napi]
pub fn hash_raw(
password: Either<String, Buffer>,
password: Either<String, &[u8]>,
options: Option<Options>,
abort_signal: Option<AbortSignal>,
) -> AsyncTask<RawHashTask> {
Expand All @@ -235,7 +237,7 @@ pub fn hash_raw(
#[napi]
pub fn hash_raw_sync(
env: Env,
password: Either<String, Buffer>,
password: Either<String, &[u8]>,
options: Option<Options>,
) -> Result<Buffer> {
let mut hash_task = RawHashTask {
Expand Down Expand Up @@ -279,8 +281,8 @@ impl Task for VerifyTask {

#[napi]
pub fn verify(
hashed: Either<String, Buffer>,
password: Either<String, Buffer>,
hashed: Either<String, &[u8]>,
password: Either<String, &[u8]>,
options: Option<Options>,
abort_signal: Option<AbortSignal>,
) -> Result<AsyncTask<VerifyTask>> {
Expand All @@ -305,8 +307,8 @@ pub fn verify(
#[napi]
pub fn verify_sync(
env: Env,
hashed: Either<String, Buffer>,
password: Either<String, Buffer>,
hashed: Either<String, &[u8]>,
password: Either<String, &[u8]>,
options: Option<Options>,
) -> Result<bool> {
let mut verify_task = VerifyTask {
Expand Down
18 changes: 9 additions & 9 deletions packages/bcrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ version = "0.1.0"
crate-type = ["cdylib"]

[dependencies]
base64 = { version = "0.22" }
bcrypt = "0.15"
blowfish = { version = "0.9", features = ["bcrypt"] }
getrandom = "0.2"
global_alloc = { path = "../../crates/alloc" }
napi = { version = "2", default-features = false, features = ["napi3"] }
napi-derive = { version = "2" }
base64 = { workspace = true }
bcrypt = { workspace = true }
blowfish = { workspace = true }
getrandom = { workspace = true }
global_alloc = { workspace = true }
napi = { workspace = true, default-features = false, features = ["napi3"] }
napi-derive = { workspace = true }

[dev-dependencies]
quickcheck = "1.0"
quickcheck = { workspace = true }

[build-dependencies]
napi-build = "2"
napi-build = { workspace = true }
5 changes: 2 additions & 3 deletions packages/bcrypt/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ export declare function genSalt(round: number, version?: '2a' | '2x' | '2y' | '2

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

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

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

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

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

4 changes: 2 additions & 2 deletions packages/bcrypt/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
nativeBinding = require('./bcrypt.wasi.cjs')
} catch (err) {
if (process.env.NAPI_RS_FORCE_WASI) {
console.error(err)
loadErrors.push(err)
}
}
if (!nativeBinding) {
try {
nativeBinding = require('@node-rs/bcrypt-wasm32-wasi')
} catch (err) {
if (process.env.NAPI_RS_FORCE_WASI) {
console.error(err)
loadErrors.push(err)
}
}
}
Expand Down
Loading

0 comments on commit f66abff

Please sign in to comment.