From d02aa8c7e00b992cb7553bc6a75f74ac560ffdcf Mon Sep 17 00:00:00 2001 From: Brooooooklyn Date: Tue, 3 Jun 2025 06:46:55 +0000 Subject: [PATCH] fix(native): opt out napi-derive noop feature (#12686) It would cause the napi-derive not work as expect in workspace level ## Summary by CodeRabbit - **Refactor** - Improved internal handling and type definitions for document parsing, resulting in clearer and more maintainable data structures. - **Chores** - Introduced a new feature flag for mobile native builds, enabling conditional compilation for enhanced flexibility across Android and iOS. - Updated build scripts to support the new feature flag for both Android and iOS platforms. - Updated iOS app dependencies to newer versions, including Apollo iOS, ChidoriMenu, and swift-collections, and removed SQLite.swift. - **Tests** - Enhanced Rust linting and testing workflows to run selectively across workspace packages with the new feature flag enabled. --- .github/workflows/build-test.yml | 5 ++- packages/backend/native/index.d.ts | 12 ++++- packages/backend/native/src/doc_loader.rs | 45 +++++++++++-------- .../apps/android/App/app/build.gradle | 3 ++ .../xcshareddata/swiftpm/Package.resolved | 21 +++------ .../xcshareddata/swiftpm/Package.resolved | 12 ++--- packages/frontend/apps/ios/App/Podfile.lock | 12 ++--- .../apps/ios/App/xc-universal-binary.sh | 8 ++-- packages/frontend/apps/ios/codegen.ts | 4 +- packages/frontend/mobile-native/Cargo.toml | 5 ++- 10 files changed, 72 insertions(+), 55 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 37d2744d1c..32b0c82250 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -151,7 +151,8 @@ jobs: - name: Clippy run: | rustup component add clippy - cargo clippy --all-targets --all-features -- -D warnings + cargo clippy --workspace --exclude affine_server_native --all-targets --all-features -- -D warnings + cargo clippy -p affine_server_native --all-targets --all-features -- -D warnings check-git-status: name: Check Git Status @@ -923,7 +924,7 @@ jobs: uses: taiki-e/install-action@nextest - name: Run tests - run: cargo nextest run --release --no-fail-fast + run: cargo nextest run --workspace --exclude affine_server_native --features use-as-lib --release --no-fail-fast copilot-api-test: name: Server Copilot Api Test diff --git a/packages/backend/native/index.d.ts b/packages/backend/native/index.d.ts index 8456170f3c..12bbd6e85a 100644 --- a/packages/backend/native/index.d.ts +++ b/packages/backend/native/index.d.ts @@ -8,6 +8,11 @@ export const AFFINE_PRO_LICENSE_AES_KEY: string | undefined | null export const AFFINE_PRO_PUBLIC_KEY: string | undefined | null +export interface Chunk { + index: number + content: string +} + export declare function fromModelName(modelName: string): Tokenizer | null export declare function getMime(input: Uint8Array): string @@ -22,6 +27,11 @@ export declare function mergeUpdatesInApplyWay(updates: Array): Buffer export declare function mintChallengeResponse(resource: string, bits?: number | undefined | null): Promise -export declare function parseDoc(filePath: string, doc: Buffer): Promise<{ name: string, chunks: Array<{index: number, content: string}> }> +export interface ParsedDoc { + name: string + chunks: Array +} + +export declare function parseDoc(filePath: string, doc: Buffer): Promise export declare function verifyChallengeResponse(response: string, bits: number, resource: string): Promise diff --git a/packages/backend/native/src/doc_loader.rs b/packages/backend/native/src/doc_loader.rs index 5d3aba9897..e254d7050b 100644 --- a/packages/backend/native/src/doc_loader.rs +++ b/packages/backend/native/src/doc_loader.rs @@ -1,10 +1,22 @@ use affine_common::doc_loader::Doc; use napi::{ anyhow::anyhow, - bindgen_prelude::{Array, AsyncTask, Buffer, Object}, + bindgen_prelude::{AsyncTask, Buffer}, Env, Result, Task, }; +#[napi(object)] +pub struct Chunk { + pub index: i64, + pub content: String, +} + +#[napi(object)] +pub struct ParsedDoc { + pub name: String, + pub chunks: Vec, +} + pub struct Document { inner: Doc, } @@ -14,21 +26,20 @@ impl Document { self.inner.name.clone() } - fn chunks(&self, env: &Env) -> Result { - let vec = self + fn chunks(&self) -> Vec { + self .inner .chunks .iter() .enumerate() .map(|(i, chunk)| { let content = crate::utils::clean_content(&chunk.content); - let mut obj = Object::new(env)?; - obj.set("index", i as i64)?; - obj.set("content", content)?; - Ok(obj) + Chunk { + index: i as i64, + content, + } }) - .collect::>>()?; - Array::from_vec(env, vec) + .collect::>() } } @@ -40,24 +51,22 @@ pub struct AsyncParseDocResponse { #[napi] impl Task for AsyncParseDocResponse { type Output = Document; - type JsValue = Object<'static>; + type JsValue = ParsedDoc; fn compute(&mut self) -> Result { let doc = Doc::new(&self.file_path, &self.doc).map_err(|e| anyhow!(e))?; Ok(Document { inner: doc }) } - fn resolve(&mut self, env: Env, doc: Document) -> Result { - let mut obj = Object::new(&env)?; - obj.set("name", doc.name())?; - obj.set("chunks", doc.chunks(&env)?)?; - Ok(obj) + fn resolve(&mut self, _: Env, doc: Document) -> Result { + Ok(ParsedDoc { + name: doc.name(), + chunks: doc.chunks(), + }) } } -#[napi( - ts_return_type = "Promise<{ name: string, chunks: Array<{index: number, content: string}> }>" -)] +#[napi] pub fn parse_doc(file_path: String, doc: Buffer) -> AsyncTask { AsyncTask::new(AsyncParseDocResponse { file_path, diff --git a/packages/frontend/apps/android/App/app/build.gradle b/packages/frontend/apps/android/App/app/build.gradle index fbedd6c449..de49bc8296 100644 --- a/packages/frontend/apps/android/App/app/build.gradle +++ b/packages/frontend/apps/android/App/app/build.gradle @@ -136,6 +136,9 @@ dependencies { cargo { module = "../../../../mobile-native" libname = "affine_mobile_native" + features { + defaultAnd("use-as-lib") + } targets = ["arm64"] pythonCommand = "python3" targetDirectory = "../../../../../../target" diff --git a/packages/frontend/apps/ios/App/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/packages/frontend/apps/ios/App/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 6d6a9ad2c0..63f227d74d 100644 --- a/packages/frontend/apps/ios/App/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/packages/frontend/apps/ios/App/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apollographql/apollo-ios", "state" : { - "revision" : "51e535dcf5439c01396d668a9598748ea86c7c1a", - "version" : "1.20.0" + "revision" : "39fea7617346c0731be25f61afd537e7032fb562", + "version" : "1.22.0" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Lakr233/ChidoriMenu", "state" : { - "revision" : "ccd37f01cd4dcf4fb0889f263573d90d5c16e59b", - "version" : "2.4.3" + "revision" : "3bb4323fe0f7f8f435d15656c3eeffcbb7c9c605", + "version" : "3.0.0" } }, { @@ -27,15 +27,6 @@ "version" : "0.16.0" } }, - { - "identity" : "sqlite.swift", - "kind" : "remoteSourceControl", - "location" : "https://github.com/stephencelis/SQLite.swift.git", - "state" : { - "revision" : "a95fc6df17d108bd99210db5e8a9bac90fe984b8", - "version" : "0.15.3" - } - }, { "identity" : "swift-cmark", "kind" : "remoteSourceControl", @@ -50,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "671108c96644956dddcd89dd59c203dcdb36cec7", - "version" : "1.1.4" + "revision" : "c1805596154bb3a265fd91b8ac0c4433b4348fb0", + "version" : "1.2.0" } }, { diff --git a/packages/frontend/apps/ios/App/App.xcworkspace/xcshareddata/swiftpm/Package.resolved b/packages/frontend/apps/ios/App/App.xcworkspace/xcshareddata/swiftpm/Package.resolved index cad0d2711a..a0763ea436 100644 --- a/packages/frontend/apps/ios/App/App.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/packages/frontend/apps/ios/App/App.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apollographql/apollo-ios.git", "state" : { - "revision" : "6ecc75281dab2fa231cb0d5fed3a3713826fecae", - "version" : "1.21.0" + "revision" : "39fea7617346c0731be25f61afd537e7032fb562", + "version" : "1.22.0" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Lakr233/ChidoriMenu", "state" : { - "revision" : "ccd37f01cd4dcf4fb0889f263573d90d5c16e59b", - "version" : "2.4.3" + "revision" : "3bb4323fe0f7f8f435d15656c3eeffcbb7c9c605", + "version" : "3.0.0" } }, { @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "671108c96644956dddcd89dd59c203dcdb36cec7", - "version" : "1.1.4" + "revision" : "c1805596154bb3a265fd91b8ac0c4433b4348fb0", + "version" : "1.2.0" } }, { diff --git a/packages/frontend/apps/ios/App/Podfile.lock b/packages/frontend/apps/ios/App/Podfile.lock index 87d06e4df2..ef43285496 100644 --- a/packages/frontend/apps/ios/App/Podfile.lock +++ b/packages/frontend/apps/ios/App/Podfile.lock @@ -45,13 +45,13 @@ EXTERNAL SOURCES: :path: "../../../../../node_modules/capacitor-plugin-app-tracking-transparency" SPEC CHECKSUMS: - Capacitor: 106e7a4205f4618d582b886a975657c61179138d - CapacitorApp: d63334c052278caf5d81585d80b21905c6f93f39 - CapacitorBrowser: 081852cf532acf77b9d2953f3a88fe5b9711fb06 + Capacitor: 03bc7cbdde6a629a8b910a9d7d78c3cc7ed09ea7 + CapacitorApp: febecbb9582cb353aed037e18ec765141f880fe9 + CapacitorBrowser: 6299776d496e968505464884d565992faa20444a CapacitorCordova: 5967b9ba03915ef1d585469d6e31f31dc49be96f - CapacitorHaptics: 70e47470fa1a6bd6338cd102552e3846b7f9a1b3 - CapacitorKeyboard: 969647d0ca2e5c737d7300088e2517aa832434e2 - CapacitorPluginAppTrackingTransparency: 2a2792623a5a72795f2e8f9ab3f1147573732fd8 + CapacitorHaptics: 1f1e17041f435d8ead9ff2a34edd592c6aa6a8d6 + CapacitorKeyboard: 09fd91dcde4f8a37313e7f11bde553ad1ed52036 + CapacitorPluginAppTrackingTransparency: 92ae9c1cfb5cf477753db9269689332a686f675a CryptoSwift: 967f37cea5a3294d9cce358f78861652155be483 PODFILE CHECKSUM: 2c1e4be82121f2d9724ecf7e31dd14e165aeb082 diff --git a/packages/frontend/apps/ios/App/xc-universal-binary.sh b/packages/frontend/apps/ios/App/xc-universal-binary.sh index 5bdb6e3fa7..85fb7e7257 100644 --- a/packages/frontend/apps/ios/App/xc-universal-binary.sh +++ b/packages/frontend/apps/ios/App/xc-universal-binary.sh @@ -47,20 +47,20 @@ for arch in $ARCHS; do # Intel iOS simulator export CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios" - $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target x86_64-apple-ios + $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target x86_64-apple-ios --features use-as-lib ;; arm64) if [ $IS_SIMULATOR -eq 0 ]; then # Hardware iOS targets - $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target aarch64-apple-ios + $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target aarch64-apple-ios --features use-as-lib cp $SRC_ROOT/../../../target/aarch64-apple-ios/${RELFLAG}/lib${FFI_TARGET}.a $SRCROOT/lib${FFI_TARGET}.a else # M1 iOS simulator - $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target aarch64-apple-ios-sim + $HOME/.cargo/bin/cargo rustc -p "${FFI_TARGET}" --lib --crate-type staticlib --$RELFLAG --target aarch64-apple-ios-sim --features use-as-lib cp $SRC_ROOT/../../../target/aarch64-apple-ios-sim/${RELFLAG}/lib${FFI_TARGET}.a $SRCROOT/lib${FFI_TARGET}.a fi esac done -$HOME/.cargo/bin/cargo run -p affine_mobile_native --bin uniffi-bindgen generate --library $SRCROOT/lib${FFI_TARGET}.a --language swift --out-dir $SRCROOT/../../ios/App/App/uniffi +$HOME/.cargo/bin/cargo run -p affine_mobile_native --features use-as-lib --bin uniffi-bindgen generate --library $SRCROOT/lib${FFI_TARGET}.a --language swift --out-dir $SRCROOT/../../ios/App/App/uniffi diff --git a/packages/frontend/apps/ios/codegen.ts b/packages/frontend/apps/ios/codegen.ts index b6a6735a69..6cadbb1698 100644 --- a/packages/frontend/apps/ios/codegen.ts +++ b/packages/frontend/apps/ios/codegen.ts @@ -17,14 +17,14 @@ execSync( console.log('[*] rust...'); execSync( - 'cargo build -p affine_mobile_native --lib --release --target aarch64-apple-ios', + 'cargo build -p affine_mobile_native --features use-as-lib --lib --release --target aarch64-apple-ios', { stdio: 'inherit', } ); execSync( - `cargo run -p affine_mobile_native --bin uniffi-bindgen generate \ + `cargo run -p affine_mobile_native --features use-as-lib --bin uniffi-bindgen generate \ --library ${ProjectRoot}/target/aarch64-apple-ios/release/libaffine_mobile_native.a \ --language swift --out-dir ${PackageRoot}/App/App/uniffi`, { stdio: 'inherit' } diff --git a/packages/frontend/mobile-native/Cargo.toml b/packages/frontend/mobile-native/Cargo.toml index a00c2ec69c..634195df86 100644 --- a/packages/frontend/mobile-native/Cargo.toml +++ b/packages/frontend/mobile-native/Cargo.toml @@ -11,9 +11,12 @@ crate-type = ["cdylib", "staticlib"] name = "uniffi-bindgen" path = "uniffi-bindgen.rs" +[features] +use-as-lib = ["affine_nbstore/use-as-lib"] + [dependencies] affine_common = { workspace = true } -affine_nbstore = { workspace = true, features = ["use-as-lib"] } +affine_nbstore = { workspace = true } anyhow = { workspace = true } base64-simd = { workspace = true } chrono = { workspace = true }