From ca59a1065fd547ea4f851d4397a3d58e6b017137 Mon Sep 17 00:00:00 2001 From: openhands Date: Sat, 29 Aug 2026 19:27:27 +0200 Subject: [PATCH] perf: use lzma-wasm for SWF decompression and drop vite Replace the pure-JS lzma decoder with lzma-wasm (Rust/WASM, base64-inlined, zero-alloc decompress), giving an order-of-magnitude speedup on furni imports. Remove the obsolete lzma type shim and the redundant top-level vite dev dependency, which nothing imports directly. --- next.config.ts | 2 +- package.json | 3 +-- pnpm-lock.yaml | 17 +++++++---------- src/lib/services/swf/swf-parser.test.ts | 7 +++++-- src/lib/services/swf/swf-parser.ts | 7 +++++-- src/types/lzma.d.ts | 14 -------------- 6 files changed, 19 insertions(+), 31 deletions(-) delete mode 100644 src/types/lzma.d.ts diff --git a/next.config.ts b/next.config.ts index 1d3953c0..618d221b 100644 --- a/next.config.ts +++ b/next.config.ts @@ -32,7 +32,7 @@ const nextConfig: NextConfig = { reactStrictMode: true, compress: true, productionBrowserSourceMaps: false, - serverExternalPackages: ["lzma", "sharp", "pino", "pino-pretty"], + serverExternalPackages: ["lzma-wasm", "sharp", "pino", "pino-pretty"], async redirects() { return [ diff --git a/package.json b/package.json index a3af572b..eb68cf61 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "jszip": "3.10.1", "lenis": "1.3.26", "lucide-react": "1.33.0", - "lzma": "^2.3.2", + "lzma-wasm": "1.0.7", "motion": "13.1.1", "music-metadata": "11.15.0", "mysql2": "3.23.4", @@ -85,7 +85,6 @@ "tailwindcss": "4.3.3", "tsx": "4.23.12", "typescript": "7.0.2", - "vite": "8.2.2", "vitest": "4.1.11" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 34fdd9f7..f69623d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -79,9 +79,9 @@ importers: lucide-react: specifier: 1.33.0 version: 1.33.0(react@19.2.8) - lzma: - specifier: ^2.3.2 - version: 2.3.2 + lzma-wasm: + specifier: 1.0.7 + version: 1.0.7 motion: specifier: 13.1.1 version: 13.1.1(react-dom@19.2.8(react@19.2.8))(react@19.2.8) @@ -185,9 +185,6 @@ importers: typescript: specifier: 7.0.2 version: 7.0.2 - vite: - specifier: 8.2.2 - version: 8.2.2(@types/node@26.2.0)(esbuild@0.25.12)(jiti@2.7.0)(tsx@4.23.12)(yaml@2.9.0) vitest: specifier: 4.1.11 version: 4.1.11(@types/node@26.2.0)(@vitest/coverage-v8@4.1.11)(vite@8.2.2(@types/node@26.2.0)(esbuild@0.25.12)(jiti@2.7.0)(tsx@4.23.12)(yaml@2.9.0)) @@ -2450,9 +2447,9 @@ packages: peerDependencies: react: ^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0 - lzma@2.3.2: - resolution: {integrity: sha512-DcfiawQ1avYbW+hsILhF38IKAlnguc/fjHrychs9hdxe4qLykvhT5VTGNs5YRWgaNePh7NTxGD4uv4gKsRomCQ==} - hasBin: true + lzma-wasm@1.0.7: + resolution: {integrity: sha512-/fm+kRC7MqF9aCAWU5GwmgWllzM7ehC46aB5Va7V1PAnUl0qSU8ZA2XWk8yOgZbkgL6MWLDyYIVJR+UQziwvUA==} + engines: {node: '>=16.0.0'} magic-string@0.30.21: resolution: {integrity: sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==} @@ -4593,7 +4590,7 @@ snapshots: dependencies: react: 19.2.8 - lzma@2.3.2: {} + lzma-wasm@1.0.7: {} magic-string@0.30.21: dependencies: diff --git a/src/lib/services/swf/swf-parser.test.ts b/src/lib/services/swf/swf-parser.test.ts index 25ef890f..ef6d2630 100644 --- a/src/lib/services/swf/swf-parser.test.ts +++ b/src/lib/services/swf/swf-parser.test.ts @@ -1,5 +1,5 @@ import { deflateSync } from "node:zlib"; -import LZMA from "lzma"; +import { initWasmSync, compress as lzmaCompress } from "lzma-wasm"; import { describe, expect, it } from "vitest"; import { decompressSwf, @@ -33,7 +33,10 @@ function cwsFixture(payload: Buffer, version = 6): Buffer { } function zwsFixture(payload: Buffer, version = 6): Buffer { - const rawLzma = LZMA.compress(payload, 9); + initWasmSync(); + const rawLzma = Buffer.from( + lzmaCompress(payload, { format: "lzma", level: 9 }), + ); const props = Buffer.from(rawLzma.slice(0, 5)); const compressedPayload = Buffer.from(rawLzma.slice(13)); const compressedSize = 5 + compressedPayload.length; diff --git a/src/lib/services/swf/swf-parser.ts b/src/lib/services/swf/swf-parser.ts index 8a640b7e..8ca3a02e 100644 --- a/src/lib/services/swf/swf-parser.ts +++ b/src/lib/services/swf/swf-parser.ts @@ -3,8 +3,9 @@ */ import { inflateSync } from "node:zlib"; +import { initWasmSync, decompress as lzmaDecompress } from "lzma-wasm"; -const LZMA = require("lzma/src/lzma_worker").LZMA_WORKER; +initWasmSync(); // ── SWF Tag Types ────────────────────────────────────────────────── export const TAG_END = 0; @@ -62,7 +63,9 @@ export function decompressSwf(buffer: Buffer): Buffer { lzmaHeader.writeUInt32LE(0, 9); const lzmaStream = Buffer.concat([lzmaHeader, compressedPayload]); - const decompressed = Buffer.from(LZMA.decompress(lzmaStream) as number[]); + const decompressed = Buffer.from( + lzmaDecompress(lzmaStream, { expectedSize: uncompressedSize }), + ); const header = Buffer.alloc(8); buffer.copy(header, 0, 0, 8); diff --git a/src/types/lzma.d.ts b/src/types/lzma.d.ts deleted file mode 100644 index 502b42f8..00000000 --- a/src/types/lzma.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -declare module "lzma" { - export const LZMA: any; - export function decompress( - byte_array: any, - on_finish?: (result: any, error: any) => void, - on_progress?: (percent: number) => void, - ): any; - export function compress( - string: string | Uint8Array, - mode: number, - on_finish?: (result: any, error: any) => void, - on_progress?: (percent: number) => void, - ): any; -}