From 26cd2a68da6b6d751836dd07ef46cf2592a2cd8f Mon Sep 17 00:00:00 2001 From: naskya Date: Sun, 14 Jan 2024 12:19:32 +0900 Subject: [PATCH] refactor: use cfg_attr to enable napi features conditionally --- neko/index.js | 4 ++-- packages/backend/native-utils/src/util/acct.rs | 8 +++----- packages/backend/native-utils/src/util/random.rs | 7 +------ packages/backend/src/misc/secure-rndstr.ts | 4 ++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/neko/index.js b/neko/index.js index d527c2c8..d50aa638 100644 --- a/neko/index.js +++ b/neko/index.js @@ -268,7 +268,7 @@ if (!nativeBinding) { const { stringToAcct, acctToString, - nativeRandomStr, + genString, IdConvertType, convertId, nativeGetTimestamp, @@ -278,7 +278,7 @@ const { module.exports.stringToAcct = stringToAcct; module.exports.acctToString = acctToString; -module.exports.nativeRandomStr = nativeRandomStr; +module.exports.genString = genString; module.exports.IdConvertType = IdConvertType; module.exports.convertId = convertId; module.exports.nativeGetTimestamp = nativeGetTimestamp; diff --git a/packages/backend/native-utils/src/util/acct.rs b/packages/backend/native-utils/src/util/acct.rs index 65cd7ddc..5910afa8 100644 --- a/packages/backend/native-utils/src/util/acct.rs +++ b/packages/backend/native-utils/src/util/acct.rs @@ -1,13 +1,11 @@ -use napi_derive::napi; - #[derive(Clone, Eq, PartialEq, Debug)] -#[napi(object)] +#[cfg_attr(feature = "napi", napi_derive::napi(object))] pub struct Acct { pub username: String, pub host: Option, } -#[napi] +#[cfg_attr(feature = "napi", napi_derive::napi)] pub fn string_to_acct(acct: String) -> Acct { let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') { stripped @@ -27,7 +25,7 @@ pub fn string_to_acct(acct: String) -> Acct { } } -#[napi] +#[cfg_attr(feature = "napi", napi_derive::napi)] pub fn acct_to_string(acct: Acct) -> String { match acct.host { Some(host) => format!("{}@{}", acct.username, host), diff --git a/packages/backend/native-utils/src/util/random.rs b/packages/backend/native-utils/src/util/random.rs index ffcbca98..5be649c4 100644 --- a/packages/backend/native-utils/src/util/random.rs +++ b/packages/backend/native-utils/src/util/random.rs @@ -1,6 +1,7 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng}; /// Generate random string based on [thread_rng] and [Alphanumeric]. +#[cfg_attr(feature = "napi", napi_derive::napi)] pub fn gen_string(length: u16) -> String { thread_rng() .sample_iter(Alphanumeric) @@ -9,12 +10,6 @@ pub fn gen_string(length: u16) -> String { .collect() } -#[cfg(feature = "napi")] -#[napi_derive::napi] -pub fn native_random_str(length: u16) -> String { - gen_string(length) -} - #[cfg(test)] mod unit_test { use pretty_assertions::{assert_eq, assert_ne}; diff --git a/packages/backend/src/misc/secure-rndstr.ts b/packages/backend/src/misc/secure-rndstr.ts index 3d69a4d4..966e8756 100644 --- a/packages/backend/src/misc/secure-rndstr.ts +++ b/packages/backend/src/misc/secure-rndstr.ts @@ -1,5 +1,5 @@ -import { nativeRandomStr } from "native-utils/built/index.js"; +import { genString } from "native-utils/built/index.js"; export function secureRndstr(length = 32, _ = true): string { - return nativeRandomStr(length); + return genString(length); }