Skip to content

Commit

Permalink
fix(*): Node.js worker_threads safe by upgrade NAPI-RS version
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Mar 6, 2022
1 parent 3d329ba commit 0dd30ff
Show file tree
Hide file tree
Showing 31 changed files with 10,984 additions and 7,429 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ packages/jieba/index.js linguist-detectable=false
packages/jieba/index.d.ts linguist-detectable=false
packages/xxhash/index.js linguist-detectable=false
packages/xxhash/index.d.ts linguist-detectable=false
.yarn/releases/*.js linguist-detectable=false
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,12 @@ temp/

*.node
lib
artifacts
artifacts

.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
node_modules
lib
.yarn
785 changes: 785 additions & 0 deletions .yarn/releases/yarn-3.2.0.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarnPath: .yarn/releases/yarn-3.2.0.cjs
nodeLinker: node-modules
2 changes: 1 addition & 1 deletion ava.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const USE_TS_NODE = (function () {

module.exports = {
extensions: ['ts'],
workerThreads: false,
workerThreads: true,
require: [USE_TS_NODE ? 'ts-node/register/transpile-only' : '@swc-node/register'],
files: ['packages/**/*.spec.ts'],
timeout: '3m',
Expand Down
2 changes: 1 addition & 1 deletion crates/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ 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 = "linux", target_arch = "aarch64", target_env = "musl")))'.dependencies]
mimalloc-rust = {version = "0.1"}
mimalloc-rust = { version = "0.1" }
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "node-rs",
"packageManager": "yarn@3.2.0",
"version": "0.0.0",
"description": "Node & Rust bindings monorepo",
"author": "LongYinan <lynweklm@gmail.com>",
Expand All @@ -15,16 +16,18 @@
"build:ts": "tsc -b tsconfig.project.json -verbose",
"lint": "eslint . -c ./.eslintrc.yml \"packages/**/*.{ts,js}\"",
"test": "ava",
"format": "run-p format:prettier format:rs",
"format": "run-p format:prettier format:rs format:toml",
"format:prettier": "prettier --config ./package.json -w .",
"format:rs": "cargo fmt --all",
"format:toml": "taplo format",
"typecheck": "tsc -b tsconfig.project.json -verbose",
"postinstall": "husky install"
},
"devDependencies": {
"@napi-rs/cli": "^2.4.4",
"@swc-node/core": "^1.8.2",
"@swc-node/register": "^1.4.2",
"@taplo/cli": "^0.3.2",
"@types/node": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
Expand Down Expand Up @@ -52,6 +55,12 @@
],
"*.@(js|ts|tsx|yml|yaml|json|md)": [
"prettier --write"
],
"*.toml": [
"taplo format"
],
"*.rs": [
"cargo fmt --"
]
},
"prettier": {
Expand Down
10 changes: 6 additions & 4 deletions packages/argon2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ version = "0.0.0"
crate-type = ["cdylib"]

[dependencies]
argon2 = {version = "0.3", features = ["parallel"]}
napi = {version = "2", default-features = false, features = ["napi3"]}
napi-derive = {version = "2", default-features = false, features = ["type-def"]}
rand = {git = "https://github.com/rust-random/rand", version = "0.8", rev = "a407bdf", features = ["nightly", "simd_support"]}
argon2 = { version = "0.3", features = ["parallel"] }
napi = { version = "2", default-features = false, features = ["napi3"] }
napi-derive = { version = "2", default-features = false, features = [
"type-def",
] }
rand = { version = "0.8", features = ["nightly", "simd_support"] }

[build-dependencies]
napi-build = "1"
2 changes: 1 addition & 1 deletion packages/argon2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
"version": "napi version"
},
"devDependencies": {
"argon2": "^0.28.3"
"argon2": "^0.28.5"
}
}
16 changes: 7 additions & 9 deletions packages/bcrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ version = "0.1.0"
crate-type = ["cdylib"]

[dependencies]
bcrypt = "0.10"
blowfish = {version = "0.9", features = ["bcrypt"]}
byteorder = "1"
global_alloc = {path = "../../crates/alloc"}
napi = {version = "2", default-features = false, features = ["napi3"]}
napi-derive = {version = "2"}
phf = {version = "0.10", features = ["macros"]}
radix64 = "0.6"
rand = {git = "https://github.com/rust-random/rand", rev = "a407bdf"}
bcrypt = "0.12"
blowfish = { version = "0.9", features = ["bcrypt"] }
global_alloc = { path = "../../crates/alloc" }
napi = { version = "2", default-features = false, features = ["napi3"] }
napi-derive = { version = "2" }
getrandom = "0.2"
base64 = { version = "0.13", default-features = false }

[dev-dependencies]
quickcheck = "1.0"
Expand Down
98 changes: 0 additions & 98 deletions packages/bcrypt/src/b64.rs

This file was deleted.

11 changes: 7 additions & 4 deletions packages/bcrypt/src/bcrypt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use blowfish::Blowfish;
use byteorder::{ByteOrder, BE};

fn setup(cost: u32, salt: &[u8], key: &[u8]) -> Blowfish {
assert!(cost < 32);
Expand All @@ -21,17 +20,21 @@ pub fn bcrypt(cost: u32, salt: &[u8], password: &[u8], output: &mut [u8]) {

let state = setup(cost, salt, password);
// OrpheanBeholderScryDoubt
#[allow(clippy::unreadable_literal)]
let mut ctext = [
0x4f727068, 0x65616e42, 0x65686f6c, 0x64657253, 0x63727944, 0x6f756274,
];
for i in 0..3 {
let i: usize = i * 2;
for _ in 0..64 {
let (l, r) = state.bc_encrypt(ctext[i], ctext[i + 1]);
let [l, r] = state.bc_encrypt([ctext[i], ctext[i + 1]]);
ctext[i] = l;
ctext[i + 1] = r;
}
BE::write_u32(&mut output[i * 4..(i + 1) * 4], ctext[i]);
BE::write_u32(&mut output[(i + 1) * 4..(i + 2) * 4], ctext[i + 1]);

let buf = ctext[i].to_be_bytes();
output[i * 4..][..4].copy_from_slice(&buf);
let buf = ctext[i + 1].to_be_bytes();
output[(i + 1) * 4..][..4].copy_from_slice(&buf);
}
}
27 changes: 20 additions & 7 deletions packages/bcrypt/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::error;
use std::fmt;
use std::io;

/// Library generic result type.
Expand All @@ -10,11 +10,13 @@ pub type BcryptResult<T> = Result<T, BcryptError>;
/// passwords
pub enum BcryptError {
Io(io::Error),
InvalidVersion(String),
CostNotAllowed(u32),
InvalidCost(String),
InvalidPrefix(String),
InvalidHash(String),
Rand(rand::Error),
InvalidBase64(base64::DecodeError),
Rand(getrandom::Error),
InvalidVersion(String),
}

macro_rules! impl_from_error {
Expand All @@ -27,18 +29,27 @@ macro_rules! impl_from_error {
};
}

impl_from_error!(base64::DecodeError, BcryptError::InvalidBase64);
impl_from_error!(io::Error, BcryptError::Io);
impl_from_error!(rand::Error, BcryptError::Rand);
impl_from_error!(getrandom::Error, BcryptError::Rand);

impl fmt::Display for BcryptError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BcryptError::Io(ref err) => write!(f, "IO error: {}", err),
BcryptError::InvalidCost(ref cost) => write!(f, "Invalid Cost: {}", cost),
BcryptError::InvalidVersion(ref v) => write!(f, "Invalid version: {}", v),
BcryptError::CostNotAllowed(ref cost) => write!(
f,
"Cost needs to be between {} and {}, got {}",
crate::lib_bcrypt::MIN_COST,
crate::lib_bcrypt::MAX_COST,
cost
),
BcryptError::InvalidPrefix(ref prefix) => write!(f, "Invalid Prefix: {}", prefix),
BcryptError::InvalidHash(ref hash) => write!(f, "Invalid hash: {}", hash),
BcryptError::InvalidBase64(ref err) => write!(f, "Base64 error: {}", err),
BcryptError::Rand(ref err) => write!(f, "Rand error: {}", err),
BcryptError::InvalidVersion(ref err) => write!(f, "Invalid version: {}", err),
}
}
}
Expand All @@ -48,9 +59,11 @@ impl error::Error for BcryptError {
match *self {
BcryptError::Io(ref err) => Some(err),
BcryptError::InvalidCost(_)
| BcryptError::InvalidVersion(_)
| BcryptError::CostNotAllowed(_)
| BcryptError::InvalidPrefix(_)
| BcryptError::InvalidHash(_) => None,
| BcryptError::InvalidHash(_)
| BcryptError::InvalidVersion(_) => None,
BcryptError::InvalidBase64(ref err) => Some(err),
BcryptError::Rand(ref err) => Some(err),
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/bcrypt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::all)]
#![allow(dead_code)]

/// Explicit extern crate to use allocator.
extern crate global_alloc;
Expand All @@ -14,7 +15,6 @@ use crate::hash_task::HashTask;
use crate::lib_bcrypt::{format_salt, gen_salt, Version};
use crate::verify_task::VerifyTask;

mod b64;
mod errors;
mod hash_task;
mod lib_bcrypt;
Expand All @@ -26,7 +26,12 @@ pub const DEFAULT_COST: u32 = 12;

#[napi]
pub fn gen_salt_sync(round: u32, version: String) -> Result<String> {
let salt = gen_salt();
let salt = gen_salt().map_err(|err| {
Error::new(
Status::GenericFailure,
format!("Generate salt failed {}", err),
)
})?;
Ok(format_salt(
round,
Version::from_str(version.as_str()).map_err(|_| Error::from_status(Status::InvalidArg))?,
Expand Down
Loading

0 comments on commit 0dd30ff

Please sign in to comment.