diff --git a/.cargo/config.toml b/.cargo/config.toml index 1261fea8..e4ab5467 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,9 @@ +[target.x86_64-unknown-linux-gnu] +rustflags = ["-C", "target-feature=+sse4.2"] +[target.x86_64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+sse4.2"] +[target.x86_64-apple-darwin] +rustflags = ["-C", "target-feature=+sse4.2"] [target.aarch64-unknown-linux-musl] linker = "aarch64-linux-musl-gcc" rustflags = ["-C", "target-feature=-crt-static"] diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ad4a3fa5..cf21dd63 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,6 +40,7 @@ jobs: docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian build: >- set -e && + rustup target add x86_64-unknown-linux-gnu && yarn lerna exec "yarn build --target x86_64-unknown-linux-gnu" --concurrency 1 --stream --no-prefix && strip packages/*/*.node - host: ubuntu-latest @@ -59,8 +60,9 @@ jobs: - host: ubuntu-latest target: aarch64-unknown-linux-gnu docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64 - build: | - yarn lerna exec "yarn build --target aarch64-unknown-linux-gnu" --concurrency 1 --stream --no-prefix + build: >- + rustup target add aarch64-unknown-linux-gnu && + yarn lerna exec "yarn build --target aarch64-unknown-linux-gnu" --concurrency 1 --stream --no-prefix && llvm-strip packages/*/*.node - host: ubuntu-latest target: 'armv7-unknown-linux-gnueabihf' @@ -109,7 +111,7 @@ jobs: uses: dtolnay/rust-toolchain@stable if: ${{ !matrix.settings.docker }} with: - toolchain: stable + toolchain: nightly-2023-01-11 targets: ${{ matrix.settings.target }} - name: Cache cargo registry diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 3ae30245..031134fb 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -23,7 +23,7 @@ jobs: - name: Install uses: dtolnay/rust-toolchain@stable with: - toolchain: stable + toolchain: nightly-2023-01-11 components: rustfmt, clippy - name: 'Install dependencies' diff --git a/Cargo.toml b/Cargo.toml index 27bb60c6..a08a0573 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ members = [ ] [profile.release] -codegen-units = 4 +codegen-units = 1 lto = true overflow-checks = false strip = 'symbols' diff --git a/packages/argon2/Cargo.toml b/packages/argon2/Cargo.toml index 03d6cc76..21769086 100644 --- a/packages/argon2/Cargo.toml +++ b/packages/argon2/Cargo.toml @@ -13,7 +13,7 @@ global_alloc = { path = "../../crates/alloc" } napi-derive = { version = "2", default-features = false, features = [ "type-def", ] } -rand = { version = "0.8", features = ["getrandom"] } +rand = { version = "0.8", features = ["nightly", "simd_support", "getrandom"] } [build-dependencies] napi-build = "2" diff --git a/packages/argon2/src/lib.rs b/packages/argon2/src/lib.rs index d2d76892..66d8442c 100644 --- a/packages/argon2/src/lib.rs +++ b/packages/argon2/src/lib.rs @@ -124,9 +124,9 @@ impl Task for HashTask { let salt = SaltString::generate(&mut OsRng); let hasher = self.options.to_argon(); hasher - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))? + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))? .hash_password(self.password.as_slice(), &salt) - .map_err(|err| Error::new(Status::GenericFailure, format!("{}", err))) + .map_err(|err| Error::new(Status::GenericFailure, format!("{err}"))) .map(|h| h.to_string()) } @@ -185,7 +185,7 @@ impl Task for RawHashTask { let hasher = self .options .to_argon() - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?; + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?; let output_len = hasher .params() .output_len() @@ -194,7 +194,7 @@ impl Task for RawHashTask { hasher .hash_password_into(self.password.as_slice(), salt.as_bytes(), &mut output) - .map_err(|err| Error::new(Status::GenericFailure, format!("{}", err))) + .map_err(|err| Error::new(Status::GenericFailure, format!("{err}"))) .map(|_| output) } @@ -251,11 +251,11 @@ impl Task for VerifyTask { fn compute(&mut self) -> Result { let parsed_hash = argon2::PasswordHash::new(self.hashed.as_str()) - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?; + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?; let argon2 = self.options.to_argon(); Ok( argon2 - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))? + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))? .verify_password(self.password.as_bytes(), &parsed_hash) .is_ok(), ) @@ -278,12 +278,12 @@ pub fn verify( password: match password { Either::A(s) => s, Either::B(b) => String::from_utf8(b.to_vec()) - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?, + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?, }, hashed: match hashed { Either::A(s) => s, Either::B(b) => String::from_utf8(b.to_vec()) - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?, + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?, }, options: options.unwrap_or_default(), }, @@ -302,12 +302,12 @@ pub fn verify_sync( password: match password { Either::A(s) => s, Either::B(b) => String::from_utf8(b.to_vec()) - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?, + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?, }, hashed: match hashed { Either::A(s) => s, Either::B(b) => String::from_utf8(b.to_vec()) - .map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))?, + .map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?, }, options: options.unwrap_or_default(), }; diff --git a/packages/bcrypt/src/hash_task.rs b/packages/bcrypt/src/hash_task.rs index f6c68e3b..7607c772 100644 --- a/packages/bcrypt/src/hash_task.rs +++ b/packages/bcrypt/src/hash_task.rs @@ -44,7 +44,7 @@ impl HashTask { pub fn hash(buf: &[u8], salt: [u8; 16], cost: u32) -> Result { bcrypt::hash_with_salt(buf, cost, salt) .map(|hash_part| hash_part.to_string()) - .map_err(|err| Error::new(Status::GenericFailure, format!("{}", err))) + .map_err(|err| Error::new(Status::GenericFailure, format!("{err}"))) } } diff --git a/packages/bcrypt/src/lib.rs b/packages/bcrypt/src/lib.rs index 4b1d9dde..aaa336a2 100644 --- a/packages/bcrypt/src/lib.rs +++ b/packages/bcrypt/src/lib.rs @@ -26,7 +26,7 @@ pub fn gen_salt_sync(round: u32, version: String) -> Result { let salt = gen_salt().map_err(|err| { Error::new( Status::GenericFailure, - format!("Generate salt failed {}", err), + format!("Generate salt failed {err}"), ) })?; Ok(format_salt( @@ -60,7 +60,7 @@ pub fn hash_sync( s.copy_from_slice(salt.as_ref()); s } else { - gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))? + gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))? }; match input { Either::A(s) => HashTask::hash(s.as_bytes(), salt, cost.unwrap_or(DEFAULT_COST)), @@ -80,7 +80,7 @@ pub fn hash( s.copy_from_slice(salt.as_ref()); s } else { - gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{}", err)))? + gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))? }; let task = HashTask::new( AsyncHashInput::from_either(input)?, @@ -127,7 +127,7 @@ fn version_from_str(version: &str) -> Result { "2x" => Ok(Version::TwoY), _ => Err(Error::new( Status::InvalidArg, - format!("{} is not a valid version", version), + format!("{version} is not a valid version"), )), } } diff --git a/packages/bcrypt/src/salt_task.rs b/packages/bcrypt/src/salt_task.rs index d0ba49d4..af8b34e2 100644 --- a/packages/bcrypt/src/salt_task.rs +++ b/packages/bcrypt/src/salt_task.rs @@ -24,7 +24,7 @@ pub(crate) fn format_salt(rounds: u32, version: &Version, salt: &[u8; 16]) -> St base64::engine::fast_portable::PAD, ), ); - format!("${}${:0>2}${}", version, rounds, base64_string) + format!("${version}${rounds:0>2}${base64_string}") } pub struct SaltTask { @@ -41,7 +41,7 @@ impl Task for SaltTask { let random = gen_salt().map_err(|err| { Error::new( Status::GenericFailure, - format!("Generate salt failed {}", err), + format!("Generate salt failed {err}"), ) })?; Ok(format_salt(self.round, &self.version, &random)) diff --git a/packages/crc32/Cargo.toml b/packages/crc32/Cargo.toml index 75f77073..35b76135 100644 --- a/packages/crc32/Cargo.toml +++ b/packages/crc32/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["cdylib"] [dependencies] crc32c = { version = "0.6" } -crc32fast = { version = "1.3" } +crc32fast = { version = "1.3", features = ["nightly"] } global_alloc = { path = "../../crates/alloc" } napi = { version = "2", default-features = false, features = ["napi3"] } napi-derive = { version = "2", default-features = false } diff --git a/packages/crc32/build.rs b/packages/crc32/build.rs index cbae1638..1f866b6a 100644 --- a/packages/crc32/build.rs +++ b/packages/crc32/build.rs @@ -1,79 +1,5 @@ extern crate napi_build; -use std::env; -use std::fs::File; -use std::io::{self, Write}; -use std::path::{Path, PathBuf}; - -type Result = std::result::Result>; - -const CASTAGNOLI_POLY: u32 = 0x82f63b78; - -fn main() -> Result<()> { +fn main() { napi_build::setup(); - - let out_dir = match env::var_os("OUT_DIR") { - None => return Err(From::from("OUT_DIR environment variable not defined")), - Some(out_dir) => PathBuf::from(out_dir), - }; - write_crc_tables(&out_dir)?; - - Ok(()) -} - -fn write_crc_tables(out_dir: &Path) -> Result<()> { - let out_path = out_dir.join("crc32_table.rs"); - let mut out = io::BufWriter::new(File::create(out_path)?); - - let table = make_table(CASTAGNOLI_POLY); - let table16 = make_table16(CASTAGNOLI_POLY); - - writeln!(out, "pub const TABLE: [u32; 256] = [")?; - for &x in table.iter() { - writeln!(out, " {},", x)?; - } - writeln!(out, "];\n")?; - - writeln!(out, "pub const TABLE16: [[u32; 256]; 16] = [")?; - for table in table16.iter() { - writeln!(out, " [")?; - for &x in table.iter() { - writeln!(out, " {},", x)?; - } - writeln!(out, " ],")?; - } - writeln!(out, "];")?; - - out.flush()?; - - Ok(()) -} - -fn make_table16(poly: u32) -> [[u32; 256]; 16] { - let mut tab = [[0; 256]; 16]; - tab[0] = make_table(poly); - for i in 0..256 { - let mut crc = tab[0][i]; - for j in 1..16 { - crc = (crc >> 8) ^ tab[0][crc as u8 as usize]; - tab[j][i] = crc; - } - } - tab -} - -fn make_table(poly: u32) -> [u32; 256] { - let mut tab = [0; 256]; - for i in 0u32..256u32 { - let mut crc = i; - for _ in 0..8 { - if crc & 1 == 1 { - crc = (crc >> 1) ^ poly; - } else { - crc >>= 1; - } - } - tab[i as usize] = crc; - } - tab } diff --git a/packages/crc32/src/lib.rs b/packages/crc32/src/lib.rs index bf8efa92..c3c11599 100644 --- a/packages/crc32/src/lib.rs +++ b/packages/crc32/src/lib.rs @@ -5,26 +5,28 @@ extern crate global_alloc; use crc32c::crc32c_append; use crc32fast::Hasher; -use napi::bindgen_prelude::{Buffer, Either}; +use napi::{bindgen_prelude::Either, JsBuffer, Result}; use napi_derive::*; #[napi(js_name = "crc32c")] -pub fn crc32c(input: Either, initial_state: Option) -> u32 { - crc32c_append( - initial_state.unwrap_or(0), - match &input { - Either::A(s) => s.as_bytes(), - Either::B(b) => b.as_ref(), - }, - ) +pub fn crc32c(input: Either, initial_state: Option) -> Result { + Ok(match input { + Either::A(s) => crc32c_append(initial_state.unwrap_or(0), s.as_bytes()), + Either::B(b) => crc32c_append(initial_state.unwrap_or(0), &b.into_value()?), + }) } #[napi] -pub fn crc32(input: Either, initial_state: Option) -> u32 { +pub fn crc32(input: Either, initial_state: Option) -> Result { let mut hasher = Hasher::new_with_initial(initial_state.unwrap_or(0)); - hasher.update(match &input { - Either::A(s) => s.as_bytes(), - Either::B(b) => b.as_ref(), - }); - hasher.finalize() + match input { + Either::A(s) => { + hasher.update(s.as_bytes()); + } + Either::B(b) => { + let b = b.into_value()?; + hasher.update(&b); + } + }; + Ok(hasher.finalize()) } diff --git a/packages/deno-lint/index.d.ts b/packages/deno-lint/index.d.ts index 416d1fff..7e1eed8b 100644 --- a/packages/deno-lint/index.d.ts +++ b/packages/deno-lint/index.d.ts @@ -6,6 +6,6 @@ export function lint( fileName: string, sourceCode: string | Buffer, - allRules?: boolean | string | undefined | null + allRules?: boolean | string | undefined | null, ): Array export function denolint(dirname: string, configPath: string): boolean diff --git a/packages/deno-lint/src/lib.rs b/packages/deno-lint/src/lib.rs index 7df9a310..701af911 100644 --- a/packages/deno-lint/src/lib.rs +++ b/packages/deno-lint/src/lib.rs @@ -65,7 +65,7 @@ fn lint( Either::B(b) => str::from_utf8(b.as_ref()).map_err(|e| { Error::new( Status::StringExpected, - format!("Input source is not valid utf8 string {}", e), + format!("Input source is not valid utf8 string {e}"), ) })?, }; @@ -75,7 +75,7 @@ fn lint( .map_err(|e| { Error::new( Status::GenericFailure, - format!("Lint failed: {}, at: {}", e, file_name), + format!("Lint failed: {e}, at: {file_name}"), ) })?; @@ -89,7 +89,7 @@ fn denolint(__dirname: String, config_path: String) -> Result { let cwd = env::current_dir().map_err(|e| { Error::new( Status::GenericFailure, - format!("Get current_dir failed {}", e), + format!("Get current_dir failed {e}"), ) })?; let config_existed = fs::metadata(&config_path) @@ -115,17 +115,17 @@ fn denolint(__dirname: String, config_path: String) -> Result { type_builder .add("typescript", "*.ts") - .map_err(|e| Error::from_reason(format!("{}", e)))?; + .map_err(|e| Error::from_reason(format!("{e}")))?; type_builder .add("typescript", "*.tsx") - .map_err(|e| Error::from_reason(format!("{}", e)))?; + .map_err(|e| Error::from_reason(format!("{e}")))?; let types = type_builder .add_defaults() .select("typescript") .select("js") .build() - .map_err(|e| Error::from_reason(format!("{}", e)))?; + .map_err(|e| Error::from_reason(format!("{e}")))?; let ignore_file_path = match fs::File::open(&denolint_ignore_file) { Ok(_) => denolint_ignore_file.as_path().to_str().ok_or_else(|| { @@ -156,18 +156,18 @@ fn denolint(__dirname: String, config_path: String) -> Result { r.push_str(&f); overrides .add(&r) - .unwrap_or_else(|_| panic!("Adding excluded file {:?} failed", f)); + .unwrap_or_else(|_| panic!("Adding excluded file {f:?} failed")); } let o = overrides .build() - .unwrap_or_else(|_| panic!("Applying files.exclude from {:?} failed", config_path)); + .unwrap_or_else(|_| panic!("Applying files.exclude from {config_path:?} failed")); dir_walker.overrides(o); } for entry in dir_walker.build().filter_map(|v| v.ok()) { let p = entry.path(); if p.is_file() { let file_content = fs::read_to_string(p) - .map_err(|e| Error::from_reason(format!("Read file {:?} failed: {}", p, e)))?; + .map_err(|e| Error::from_reason(format!("Read file {p:?} failed: {e}")))?; let linter = LinterBuilder::default() .rules(rules.clone()) diff --git a/packages/jieba/src/lib.rs b/packages/jieba/src/lib.rs index 2ad97ce6..42971e2b 100644 --- a/packages/jieba/src/lib.rs +++ b/packages/jieba/src/lib.rs @@ -194,7 +194,7 @@ pub fn load_tfidf_dict(dict: Buffer) -> Result<()> { tfidf .load_dict(&mut readable_dict) .map(|_| tfidf) - .map_err(|e| Error::new(Status::GenericFailure, format!("{}", e))) + .map_err(|e| Error::new(Status::GenericFailure, format!("{e}"))) })?; Ok(()) } diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 00000000..78c5fdbb --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly-2023-01-11