Skip to content

AI-native design editor. Open-source Figma alternative.

Zstd compression via Tauri Rust command, deflate fallback for browser

Danila Poyarkov495e419e79af
diff --git a/bun.lock b/bun.lock
index 31b35b506..8218e2019 100644
--- a/bun.lock
+++ b/bun.lock
@@ -19,7 +19,6 @@
"tailwindcss": "^4.2.1",
"vue": "^3.5.29",
"yoga-layout": "^3.2.1",
- "zstd-wasm": "^0.0.9",
},
"devDependencies": {
"@iconify-json/lucide": "^1.2.94",
@@ -521,8 +520,6 @@
"yoga-layout": ["[email protected]", "", {}, "sha512-0LPOt3AxKqMdFBZA3HBAt/t/8vIKq7VaQYbuA8WxCgung+p9TVyKRYdpvCb80HcdTN2NkbIKbhNwKUfm3tQywQ=="],
- "zstd-wasm": ["[email protected]", "", {}, "sha512-V96PWYhAKv4LfdhH92ntYUlTY1cPtvuxGBoZNDd20UyC9ALwlM9Ae1SKik+KI7NwU9xd62MTXVZAP6fq8SfKVg=="],
-
"@tailwindcss/oxide-wasm32-wasi/@emnapi/core": ["@emnapi/[email protected]", "", { "dependencies": { "@emnapi/wasi-threads": "1.1.0", "tslib": "^2.4.0" }, "bundled": true }, "sha512-AvT9QFpxK0Zd8J0jopedNm+w/2fIzvtPKPjqyw9jwvBaReTTqPBk9Hixaz7KbjimP+QNz605/XnjFcDAL2pqBg=="],
"@tailwindcss/oxide-wasm32-wasi/@emnapi/runtime": ["@emnapi/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" }, "bundled": true }, "sha512-mehfKSMWjjNol8659Z8KxEMrdSJDDot5SXMq00dM8BN4o+CLNXQ0xH2V7EchNHV4RmbZLmmPdEaXZc5H2FXmDg=="],
diff --git a/desktop/Cargo.lock b/desktop/Cargo.lock
index e3271210d..3c860aedf 100644
--- a/desktop/Cargo.lock
+++ b/desktop/Cargo.lock
@@ -393,6 +393,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2"
dependencies = [
"find-msvc-tools",
+ "jobserver",
+ "libc",
"shlex",
]
@@ -1770,6 +1772,16 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
+[[package]]
+name = "jobserver"
+version = "0.1.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33"
+dependencies = [
+ "getrandom 0.3.4",
+ "libc",
+]
+
[[package]]
name = "js-sys"
version = "0.3.90"
@@ -2334,6 +2346,7 @@ dependencies = [
"tauri-plugin-dialog",
"tauri-plugin-fs",
"tauri-plugin-opener",
+ "zstd",
]
[[package]]
@@ -5333,6 +5346,34 @@ version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
+[[package]]
+name = "zstd"
+version = "0.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
+dependencies = [
+ "zstd-safe",
+]
+
+[[package]]
+name = "zstd-safe"
+version = "7.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
+dependencies = [
+ "zstd-sys",
+]
+
+[[package]]
+name = "zstd-sys"
+version = "2.0.16+zstd.1.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
+
[[package]]
name = "zvariant"
version = "5.10.0"
diff --git a/desktop/Cargo.toml b/desktop/Cargo.toml
index b0a21d2cb..a7a7abf79 100644
--- a/desktop/Cargo.toml
+++ b/desktop/Cargo.toml
@@ -24,4 +24,5 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
tauri-plugin-dialog = "2.6.0"
tauri-plugin-fs = "2.4.5"
+zstd = "0.13"
diff --git a/desktop/src/lib.rs b/desktop/src/lib.rs
index 066dc360a..bab9adec2 100644
--- a/desktop/src/lib.rs
+++ b/desktop/src/lib.rs
@@ -3,9 +3,15 @@ use tauri::{
Emitter, Manager,
};
+#[tauri::command]
+fn zstd_compress(data: Vec<u8>) -> Result<Vec<u8>, String> {
+ zstd::encode_all(data.as_slice(), 3).map_err(|e| e.to_string())
+}
+
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
+ .invoke_handler(tauri::generate_handler![zstd_compress])
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
diff --git a/package.json b/package.json
index ee39871cf..e63f9c49e 100644
--- a/package.json
+++ b/package.json
@@ -32,8 +32,7 @@
"reka-ui": "^2.8.2",
"tailwindcss": "^4.2.1",
"vue": "^3.5.29",
- "yoga-layout": "^3.2.1",
- "zstd-wasm": "^0.0.9"
+ "yoga-layout": "^3.2.1"
},
"devDependencies": {
"@iconify-json/lucide": "^1.2.94",
diff --git a/src/engine/fig-export.ts b/src/engine/fig-export.ts
index bedcd89f3..4cc4fac29 100644
--- a/src/engine/fig-export.ts
+++ b/src/engine/fig-export.ts
@@ -1,7 +1,8 @@
-import { zipSync } from 'fflate'
+import { zipSync, deflateSync } from 'fflate'
import { decodeBinarySchema, compileSchema, ByteBuffer } from '../kiwi/kiwi-schema'
import { inflateSync } from 'fflate'
import { encodeVectorNetworkBlob } from './vector'
+import { IS_TAURI } from '../constants'
import type { SceneGraph, SceneNode, Color } from './scene-graph'
@@ -208,10 +209,16 @@ function sceneNodeToKiwi(
return result
}
+async function compressData(data: Uint8Array): Promise<Uint8Array> {
+ if (IS_TAURI) {
+ const { invoke } = await import('@tauri-apps/api/core')
+ return new Uint8Array(await invoke<number[]>('zstd_compress', { data: Array.from(data) }))
+ }
+ return deflateSync(data)
+}
+
async function buildFigKiwi(schemaDeflated: Uint8Array, dataRaw: Uint8Array): Promise<Uint8Array> {
- const { ZstdCodec } = await import('zstd-wasm')
- const zstd = await ZstdCodec.run()
- const dataCompressed = zstd.compress(dataRaw)
+ const dataCompressed = await compressData(dataRaw)
const FIG_KIWI_VERSION = 106
const total = 8 + 4 + 4 + schemaDeflated.length + 4 + dataCompressed.length