diff --git a/backend/.dockerignore b/backend/.dockerignore index 985e2cd4..091f4766 100644 --- a/backend/.dockerignore +++ b/backend/.dockerignore @@ -2,5 +2,4 @@ target Dockerfile .dockerignore databases -sync_lib/pkg *.yml diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 200fcd82..dfe18212 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -430,28 +430,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "console" -version = "0.15.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "console_error_panic_hook" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" -dependencies = [ - "cfg-if", - "wasm-bindgen", -] - [[package]] name = "const-oid" version = "0.9.6" @@ -613,12 +591,6 @@ dependencies = [ "serde", ] -[[package]] -name = "encode_unicode" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" - [[package]] name = "encoding_rs" version = "0.8.35" @@ -1203,19 +1175,6 @@ dependencies = [ "serde", ] -[[package]] -name = "insta" -version = "1.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50259abbaa67d11d2bcafc7ba1d094ed7a0c70e3ce893f0d0997f73558cb3084" -dependencies = [ - "console", - "linked-hash-map", - "once_cell", - "pin-project", - "similar", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1270,12 +1229,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1341,16 +1294,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "minicov" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27fe9f1cc3c22e1687f9446c2083c4c5fc7f0bcf1c7a86bdbded14985895b4b" -dependencies = [ - "cc", - "walkdir", -] - [[package]] name = "miniz_oxide" version = "0.8.0" @@ -1516,26 +1459,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "pin-project" -version = "1.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.90", -] - [[package]] name = "pin-project-lite" version = "0.2.15" @@ -1811,15 +1734,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - [[package]] name = "sanitize-filename" version = "0.6.0" @@ -1829,12 +1743,6 @@ dependencies = [ "regex", ] -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "scopeguard" version = "1.2.0" @@ -1994,12 +1902,6 @@ dependencies = [ "rand_core 0.6.4", ] -[[package]] -name = "similar" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" - [[package]] name = "slab" version = "0.4.9" @@ -2293,18 +2195,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_lib" -version = "0.4.0" -dependencies = [ - "base64 0.22.1", - "console_error_panic_hook", - "insta", - "thiserror 2.0.12", - "wasm-bindgen", - "wasm-bindgen-test", -] - [[package]] name = "sync_server" version = "0.4.0" @@ -2313,6 +2203,7 @@ dependencies = [ "axum", "axum-extra", "axum_typed_multipart", + "base64 0.22.1", "bimap", "chrono", "clap", @@ -2328,7 +2219,6 @@ dependencies = [ "serde_with", "serde_yaml", "sqlx", - "sync_lib", "thiserror 2.0.12", "tokio", "tower-http", @@ -2794,16 +2684,6 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" -[[package]] -name = "walkdir" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" -dependencies = [ - "same-file", - "winapi-util", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -2850,19 +2730,6 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" -dependencies = [ - "cfg-if", - "js-sys", - "once_cell", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "wasm-bindgen-macro" version = "0.2.99" @@ -2892,41 +2759,6 @@ version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" -[[package]] -name = "wasm-bindgen-test" -version = "0.3.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d44563646eb934577f2772656c7ad5e9c90fac78aa8013d776fcdaf24625d" -dependencies = [ - "js-sys", - "minicov", - "scoped-tls", - "wasm-bindgen", - "wasm-bindgen-futures", - "wasm-bindgen-test-macro", -] - -[[package]] -name = "wasm-bindgen-test-macro" -version = "0.3.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54171416ce73aa0b9c377b51cc3cb542becee1cd678204812e8392e5b0e4a031" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.90", -] - -[[package]] -name = "web-sys" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "whoami" version = "1.5.2" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 738dbdd3..db849393 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -2,7 +2,6 @@ resolver = "2" members = [ "sync_server", - "sync_lib" ] [workspace.package] diff --git a/backend/sync_lib/Cargo.toml b/backend/sync_lib/Cargo.toml deleted file mode 100644 index 6147a357..00000000 --- a/backend/sync_lib/Cargo.toml +++ /dev/null @@ -1,31 +0,0 @@ -[package] -name = "sync_lib" -version.workspace = true -edition.workspace = true -authors.workspace = true -license.workspace = true -repository.workspace = true - -[lib] -crate-type = ["cdylib", "rlib"] - -[dependencies] -base64 = "0.22.1" -wasm-bindgen = "0.2.99" -thiserror = { workspace = true } - -# The `console_error_panic_hook` crate provides better debugging of panics by -# logging them with `console.error`. This is great for development, but requires -# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for -# code size when deploying. -console_error_panic_hook = { version = "0.1.7", optional = true } - -[dev-dependencies] -wasm-bindgen-test = "0.3.49" -insta = "1.42.2" - -[features] -default = ["console_error_panic_hook"] - -[lints] -workspace = true diff --git a/backend/sync_lib/pkg/package.json b/backend/sync_lib/pkg/package.json deleted file mode 100644 index 9bb9b50e..00000000 --- a/backend/sync_lib/pkg/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "sync_lib", - "type": "module", - "collaborators": [ - "Andras Schmelczer " - ], - "version": "0.4.0", - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/schmelczer/vault-link" - }, - "files": [ - "sync_lib_bg.wasm", - "sync_lib.js", - "sync_lib.d.ts" - ], - "main": "sync_lib.js", - "types": "sync_lib.d.ts", - "sideEffects": [ - "./snippets/*" - ] -} \ No newline at end of file diff --git a/backend/sync_lib/src/errors.rs b/backend/sync_lib/src/errors.rs deleted file mode 100644 index c09eafb1..00000000 --- a/backend/sync_lib/src/errors.rs +++ /dev/null @@ -1,29 +0,0 @@ -use base64::DecodeError; -use thiserror::Error; -use wasm_bindgen::JsValue; - -#[derive(Error, Debug)] -pub enum SyncLibError { - #[error("Base64 decoding error because of {}", .reason)] - Base64DecodingError { reason: String }, -} - -impl From for SyncLibError { - fn from(e: DecodeError) -> Self { - SyncLibError::Base64DecodingError { - reason: e.to_string(), - } - } -} - -impl From for SyncLibError { - fn from(e: std::string::FromUtf8Error) -> Self { - SyncLibError::Base64DecodingError { - reason: e.to_string(), - } - } -} - -impl From for JsValue { - fn from(val: SyncLibError) -> Self { JsValue::from_str(&val.to_string()) } -} diff --git a/backend/sync_lib/src/lib.rs b/backend/sync_lib/src/lib.rs deleted file mode 100644 index 437ce9bd..00000000 --- a/backend/sync_lib/src/lib.rs +++ /dev/null @@ -1,78 +0,0 @@ -//! This crate provides utilities for easily communicating between backend & -//! frontend and ensuring the same logic for encoding and decoding binary data, -//! and filtering files. -//! -//! The crate is designed to be used as a Rust library and as a -//! TypeScript/JavaScript package through WebAssembly (WASM). -//! -//! # Modules -//! -//! - `errors`: Contains error types used in this crate. - -use core::str; - -use base64::{Engine as _, engine::general_purpose::STANDARD}; -use errors::SyncLibError; -use wasm_bindgen::prelude::*; - -pub mod errors; - -/// Encode binary data for easy transport over HTTP. Inverse of -/// `base64_to_bytes`. -/// -/// # Arguments -/// -/// - `input`: The binary data to encode. -/// -/// # Returns -/// -/// The base64-encoded string. -/// -/// # Panics -/// -/// If the input is not valid UTF-8. -#[wasm_bindgen(js_name = bytesToBase64)] -#[must_use] -pub fn bytes_to_base64(input: &[u8]) -> String { - set_panic_hook(); - - STANDARD.encode(input) -} - -/// Inverse of `bytes_to_base64`. -/// Decode base64-encoded data into binary data. -/// -/// # Arguments -/// -/// - `input`: The base64-encoded string. -/// -/// # Returns -/// -/// The decoded binary data. -/// -/// # Errors -/// -/// If the input is not valid base64. -#[wasm_bindgen(js_name = base64ToBytes)] -pub fn base64_to_bytes(input: &str) -> Result, SyncLibError> { - set_panic_hook(); - - STANDARD.decode(input).map_err(SyncLibError::from) -} - -/// We don't want to support merging structured data like JSON, YAML, etc. -#[wasm_bindgen(js_name = isFileTypeMergable)] -#[must_use] -pub fn is_file_type_mergable(path_or_file_name: &str) -> bool { - set_panic_hook(); - - let file_extension = path_or_file_name.split('.').next_back().unwrap_or_default(); - - matches!(file_extension.to_lowercase().as_str(), "md" | "txt") -} - -fn set_panic_hook() { - // https://github.com/rustwasm/console_error_panic_hook#readme - #[cfg(feature = "console_error_panic_hook")] - console_error_panic_hook::set_once(); -} diff --git a/backend/sync_lib/tests/snapshots/web__base64_to_bytes_error.snap b/backend/sync_lib/tests/snapshots/web__base64_to_bytes_error.snap deleted file mode 100644 index fa178767..00000000 --- a/backend/sync_lib/tests/snapshots/web__base64_to_bytes_error.snap +++ /dev/null @@ -1,10 +0,0 @@ ---- -source: sync_lib/tests/web.rs -expression: base64_to_bytes(input) -snapshot_kind: text ---- -Err( - Base64DecodingError { - reason: "Invalid symbol 61, offset 0.", - }, -) diff --git a/backend/sync_lib/tests/web.rs b/backend/sync_lib/tests/web.rs deleted file mode 100644 index 42aa5309..00000000 --- a/backend/sync_lib/tests/web.rs +++ /dev/null @@ -1,36 +0,0 @@ -use insta::assert_debug_snapshot; -use sync_lib::*; -use wasm_bindgen_test::*; - -#[wasm_bindgen_test(unsupported = test)] -fn test_bytes_to_base64() { - let input = b"hello"; - let expected = "aGVsbG8="; - assert_eq!(bytes_to_base64(input), expected); -} - -#[wasm_bindgen_test(unsupported = test)] -fn test_base64_to_bytes() { - let input = "aGVsbG8="; - let expected = b"hello".to_vec(); - assert_eq!(base64_to_bytes(input).unwrap(), expected); -} - -#[test] // insta doesn't support wasm-bindgen-test -fn test_base64_to_bytes_error() { - let input = "==="; - assert_debug_snapshot!(base64_to_bytes(input)); -} - -#[wasm_bindgen_test(unsupported = test)] -fn test_is_file_type_mergable() { - assert!(is_file_type_mergable(".md")); - assert!(is_file_type_mergable("hi.md")); - assert!(is_file_type_mergable("my/path/to/my/document.md")); - assert!(is_file_type_mergable("hi.MD")); - assert!(is_file_type_mergable("my/path/to/my/DOCUMENT.MD")); - - assert!(!is_file_type_mergable(".json")); - assert!(!is_file_type_mergable("HELLO.JSON")); - assert!(!is_file_type_mergable("my/config.yml")); -} diff --git a/backend/sync_server/Cargo.toml b/backend/sync_server/Cargo.toml index e854ba5d..7849766d 100644 --- a/backend/sync_server/Cargo.toml +++ b/backend/sync_server/Cargo.toml @@ -7,8 +7,6 @@ license.workspace = true repository.workspace = true [dependencies] -sync_lib = { path = "../sync_lib" } - serde = { workspace = true } thiserror = { workspace = true } @@ -35,6 +33,7 @@ clap-verbosity-flag = "3.0.3" bimap = "0.6.3" ts-rs = { version = "10.1", features = ["uuid-impl", "chrono-impl"] } serde_with = "3.12.0" +base64 = "0.22.1" reconcile-text = "0.4.10" [lints] diff --git a/backend/sync_server/src/app_state/database/models.rs b/backend/sync_server/src/app_state/database/models.rs index e995611e..7796f627 100644 --- a/backend/sync_server/src/app_state/database/models.rs +++ b/backend/sync_server/src/app_state/database/models.rs @@ -1,6 +1,6 @@ +use base64::{Engine as _, engine::general_purpose::STANDARD}; use chrono::{DateTime, Utc}; use serde::Serialize; -use sync_lib::bytes_to_base64; use ts_rs::TS; pub type VaultId = String; @@ -80,7 +80,7 @@ impl From for DocumentVersion { document_id: value.document_id, relative_path: value.relative_path, updated_date: value.updated_date, - content_base64: bytes_to_base64(&value.content), + content_base64: STANDARD.encode(&value.content), is_deleted: value.is_deleted, user_id: value.user_id, device_id: value.device_id, diff --git a/backend/sync_server/src/server/update_document.rs b/backend/sync_server/src/server/update_document.rs index a0c443e2..5223efcf 100644 --- a/backend/sync_server/src/server/update_document.rs +++ b/backend/sync_server/src/server/update_document.rs @@ -8,7 +8,6 @@ use axum_typed_multipart::TypedMultipart; use log::info; use reconcile_text::{BuiltinTokenizer, is_binary, reconcile}; use serde::Deserialize; -use sync_lib::is_file_type_mergable; use super::{ device_id_header::DeviceIdHeader, requests::UpdateDocumentVersion, @@ -21,7 +20,10 @@ use crate::{ }, config::user_config::User, errors::{SyncServerError, not_found_error, server_error}, - utils::{dedup_paths::dedup_paths, normalize::normalize, sanitize_path::sanitize_path}, + utils::{ + dedup_paths::dedup_paths, is_filetype_mergable::is_file_type_mergable, + normalize::normalize, sanitize_path::sanitize_path, + }, }; #[derive(Deserialize)] diff --git a/backend/sync_server/src/utils.rs b/backend/sync_server/src/utils.rs index 870f4ae5..63876468 100644 --- a/backend/sync_server/src/utils.rs +++ b/backend/sync_server/src/utils.rs @@ -1,3 +1,4 @@ pub mod dedup_paths; +pub mod is_filetype_mergable; pub mod normalize; pub mod sanitize_path; diff --git a/backend/sync_server/src/utils/is_filetype_mergable.rs b/backend/sync_server/src/utils/is_filetype_mergable.rs new file mode 100644 index 00000000..fba4b323 --- /dev/null +++ b/backend/sync_server/src/utils/is_filetype_mergable.rs @@ -0,0 +1,23 @@ +pub fn is_file_type_mergable(path_or_file_name: &str) -> bool { + let file_extension = path_or_file_name.split('.').next_back().unwrap_or_default(); + + matches!(file_extension.to_lowercase().as_str(), "md" | "txt") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_is_file_type_mergable() { + assert!(is_file_type_mergable(".md")); + assert!(is_file_type_mergable("hi.md")); + assert!(is_file_type_mergable("my/path/to/my/document.md")); + assert!(is_file_type_mergable("hi.MD")); + assert!(is_file_type_mergable("my/path/to/my/DOCUMENT.MD")); + + assert!(!is_file_type_mergable(".json")); + assert!(!is_file_type_mergable("HELLO.JSON")); + assert!(!is_file_type_mergable("my/config.yml")); + } +} diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 9bf856eb..7c84b5a1 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -22,7 +22,7 @@ "../backend/sync_lib/pkg": { "name": "sync_lib", "version": "0.4.0", - "dev": true, + "extraneous": true, "license": "MIT" }, "node_modules/@ampproject/remapping": { @@ -6760,10 +6760,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/sync_lib": { - "resolved": "../backend/sync_lib/pkg", - "link": true - }, "node_modules/sync-client": { "resolved": "sync-client", "link": true @@ -7736,7 +7732,6 @@ "@types/node": "^22.15.30", "jest": "^29.7.0", "reconcile-text": "^0.4.10", - "sync_lib": "file:../../backend/sync_lib/pkg", "ts-jest": "^29.3.4", "ts-loader": "^9.5.2", "tslib": "2.8.1", diff --git a/frontend/sync-client/package.json b/frontend/sync-client/package.json index 6eafc0dc..08f6543c 100644 --- a/frontend/sync-client/package.json +++ b/frontend/sync-client/package.json @@ -23,7 +23,6 @@ "@types/node": "^22.15.30", "jest": "^29.7.0", "reconcile-text": "^0.4.10", - "sync_lib": "file:../../backend/sync_lib/pkg", "ts-jest": "^29.3.4", "ts-loader": "^9.5.2", "tslib": "2.8.1", diff --git a/frontend/sync-client/src/file-operations/file-operations.test.ts b/frontend/sync-client/src/file-operations/file-operations.test.ts index 1b339e38..d6644f72 100644 --- a/frontend/sync-client/src/file-operations/file-operations.test.ts +++ b/frontend/sync-client/src/file-operations/file-operations.test.ts @@ -7,7 +7,6 @@ import { FileOperations } from "./file-operations"; import { Logger } from "../tracing/logger"; import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly"; import type { FileSystemOperations } from "./filesystem-operations"; -import init from "sync_lib"; import fs from "fs"; import { TextWithCursors } from "reconcile-text"; @@ -73,13 +72,6 @@ class FakeFileSystemOperations implements FileSystemOperations { } describe("File operations", () => { - beforeEach(async () => { - const wasmBin = fs.readFileSync( - "../../backend/sync_lib/pkg/sync_lib_bg.wasm" - ); - await init({ module_or_path: wasmBin }); - }); - it("should deconflict renames", async () => { const fileSystemOperations = new FakeFileSystemOperations(); const fileOperations = new FileOperations( diff --git a/frontend/sync-client/src/file-operations/file-operations.ts b/frontend/sync-client/src/file-operations/file-operations.ts index 0bafc6cb..d891960c 100644 --- a/frontend/sync-client/src/file-operations/file-operations.ts +++ b/frontend/sync-client/src/file-operations/file-operations.ts @@ -1,7 +1,6 @@ import type { Logger } from "../tracing/logger"; import type { FileSystemOperations } from "./filesystem-operations"; import type { Database, RelativePath } from "../persistence/database"; -import { isFileTypeMergable } from "sync_lib"; import { SafeFileSystemOperations } from "./safe-filesystem-operations"; import type { TextWithCursors } from "reconcile-text"; import { isBinary, reconcile } from "reconcile-text"; diff --git a/frontend/sync-client/src/sync-client.ts b/frontend/sync-client/src/sync-client.ts index 6d51212e..d8e899ad 100644 --- a/frontend/sync-client/src/sync-client.ts +++ b/frontend/sync-client/src/sync-client.ts @@ -1,5 +1,3 @@ -import initWasm from "sync_lib"; -import wasmBin from "../../../backend/sync_lib/pkg/sync_lib_bg.wasm"; import type { PersistenceProvider } from "./persistence/persistence"; import type { HistoryEntry, HistoryStats } from "./tracing/sync-history"; import { SyncHistory } from "./tracing/sync-history"; diff --git a/frontend/sync-client/src/utils/deserialize.test.ts b/frontend/sync-client/src/utils/deserialize.test.ts deleted file mode 100644 index b053c2a3..00000000 --- a/frontend/sync-client/src/utils/deserialize.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import init, { base64ToBytes } from "sync_lib"; -import fs from "fs"; - -describe("deserialize", () => { - it("should serialize a Uint8Array to a base64 string", async () => { - const wasmBin = fs.readFileSync( - "../../backend/sync_lib/pkg/sync_lib_bg.wasm" - ); - await init({ module_or_path: wasmBin }); - - const base64 = "SGVsbG8="; - const jsResult = base64ToBytes(base64); - const expected = new Uint8Array([72, 101, 108, 108, 111]); - expect(jsResult).toEqual(expected); - const rustResult = base64ToBytes(base64); - expect(jsResult).toEqual(rustResult); - }); -}); diff --git a/frontend/sync-client/src/utils/is-file-type-mergable.test.ts b/frontend/sync-client/src/utils/is-file-type-mergable.test.ts new file mode 100644 index 00000000..5032a4e4 --- /dev/null +++ b/frontend/sync-client/src/utils/is-file-type-mergable.test.ts @@ -0,0 +1,28 @@ +import { isFileTypeMergable } from "./is-file-type-mergable"; + +describe("isFileTypeMergable", () => { + it("should return true for .md files", () => { + expect(isFileTypeMergable(".md")).toBe(true); + expect(isFileTypeMergable("hi.md")).toBe(true); + expect(isFileTypeMergable("my/path/to/my/document.md")).toBe(true); + }); + + it("should return true for .txt files", () => { + expect(isFileTypeMergable(".txt")).toBe(true); + expect(isFileTypeMergable("hi.txt")).toBe(true); + expect(isFileTypeMergable("my/path/to/my/document.txt")).toBe(true); + }); + + it("should be case insensitive", () => { + expect(isFileTypeMergable("hi.MD")).toBe(true); + expect(isFileTypeMergable("my/path/to/my/DOCUMENT.MD")).toBe(true); + expect(isFileTypeMergable("hi.TXT")).toBe(true); + expect(isFileTypeMergable("my/path/to/my/DOCUMENT.TXT")).toBe(true); + }); + + it("should return false for non-mergable file types", () => { + expect(isFileTypeMergable(".json")).toBe(false); + expect(isFileTypeMergable("HELLO.JSON")).toBe(false); + expect(isFileTypeMergable("my/config.yml")).toBe(false); + }); +}); \ No newline at end of file diff --git a/frontend/sync-client/src/utils/is-file-type-mergable.ts b/frontend/sync-client/src/utils/is-file-type-mergable.ts new file mode 100644 index 00000000..b3c06e6d --- /dev/null +++ b/frontend/sync-client/src/utils/is-file-type-mergable.ts @@ -0,0 +1,6 @@ +export function isFileTypeMergable(pathOrFileName: string): boolean { + const parts = pathOrFileName.split("."); + const fileExtension = parts.at(-1) || ""; + + return ["md", "txt"].includes(fileExtension.toLowerCase()); +} diff --git a/frontend/sync-client/src/utils/serialize.test.ts b/frontend/sync-client/src/utils/serialize.test.ts deleted file mode 100644 index d01fae18..00000000 --- a/frontend/sync-client/src/utils/serialize.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { serialize } from "./serialize"; -import init, { bytesToBase64 } from "sync_lib"; -import fs from "fs"; - -describe("serialize", () => { - it("should serialize a Uint8Array to a base64 string", async () => { - const wasmBin = fs.readFileSync( - "../../backend/sync_lib/pkg/sync_lib_bg.wasm" - ); - await init({ module_or_path: wasmBin }); - - const data = new Uint8Array([72, 101, 108, 108, 111]); - const jsResult = serialize(data); - const rustResult = bytesToBase64(data); - expect(rustResult).toBe("SGVsbG8="); - expect(jsResult).toBe(rustResult); - }); -}); diff --git a/frontend/sync-client/src/utils/serialize.ts b/frontend/sync-client/src/utils/serialize.ts deleted file mode 100644 index 79eedaab..00000000 --- a/frontend/sync-client/src/utils/serialize.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { bytesToBase64 } from "byte-base64"; - -export function serialize(data: Uint8Array): string { - return bytesToBase64(data); -}