Skip to content

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

fix(fig): open legacy raw fig-kiwi files

PAVEL TKACHEV992e09a5cd39
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 889a04c14..af6bdcafa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -67,6 +67,7 @@
### Fixed
+- Open legacy raw `.fig` files that store the Kiwi document and thumbnail without a ZIP wrapper. (#582)
- Preserve a frame's auto-layout HUG sizing mode when converting it into a component with `create_component`, so later padding changes still resize the component as expected.
- Run `openpencil import` on Node so the npm-installed CLI no longer fails with `Bun is not defined`. (#575)
- Isolate browser-development MCP servers behind worktree-aware Portless WebSocket routes and per-runtime socket/discovery paths, preventing concurrent worktrees from competing for port 7600 or the global MCP socket.
diff --git a/packages/fig/src/archive.ts b/packages/fig/src/archive.ts
index f24adcb65..6d5b228b1 100644
--- a/packages/fig/src/archive.ts
+++ b/packages/fig/src/archive.ts
@@ -2,9 +2,11 @@ import { unzipSync, zipSync, type Unzipped, type Zippable } from 'fflate'
import type { FigPageManifestEntry } from '@open-pencil/kiwi/fig'
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
-import { buildFigKiwi } from '@open-pencil/kiwi/fig/container'
+import { buildFigKiwi, parseFigKiwiChunks } from '@open-pencil/kiwi/fig/container'
import { decodeFigKiwiCanvas } from '@open-pencil/kiwi/fig/parse'
+import { hasPNGSignature } from './thumbnail'
+
export interface FigImageEntry {
name: string
data: Uint8Array
@@ -51,12 +53,27 @@ function isCanonicalCanvasEntry(name: string): boolean {
return name === 'canvas.fig' || name === 'canvas'
}
-/** Parse a complete zipped `.fig` file into its Figma protocol payload and binary resources. */
+function parseRawFigKiwi(
+ bytes: Uint8Array,
+ onPages?: (pages: FigPageManifestEntry[]) => void
+): FigParseResult | null {
+ const chunks = parseFigKiwiChunks(bytes)
+ if (!chunks) return null
+
+ const decoded = decodeFigKiwiCanvas(bytes, onPages)
+ const thumbnailPNG = chunks.slice(2).find(hasPNGSignature) ?? null
+ return { ...decoded, images: [], thumbnailPNG, metaJSON: null }
+}
+
+/** Parse a complete zipped or legacy raw `.fig` file into its protocol payload and resources. */
export function parseFigBuffer(
buffer: ArrayBuffer,
onPages?: (pages: FigPageManifestEntry[]) => void
): FigParseResult {
const bytes = new Uint8Array(buffer)
+ const raw = parseRawFigKiwi(bytes, onPages)
+ if (raw) return raw
+
const canvasArchive = unzipSync(bytes, { filter: ({ name }) => isCanonicalCanvasEntry(name) })
let canvasData = findCanvasData(canvasArchive)
let archive: Unzipped
diff --git a/packages/fig/src/thumbnail.ts b/packages/fig/src/thumbnail.ts
index bb764c5cd..2fb1802c8 100644
--- a/packages/fig/src/thumbnail.ts
+++ b/packages/fig/src/thumbnail.ts
@@ -45,7 +45,7 @@ function boundedLimit(value: number | undefined, fallback: number): number {
return Number.isFinite(value) && value && value > 0 ? value : fallback
}
-function hasPNGSignature(bytes: Uint8Array): boolean {
+export function hasPNGSignature(bytes: Uint8Array): boolean {
return PNG_SIGNATURE.every((byte, index) => bytes[index] === byte)
}
diff --git a/packages/fig/tests/index.test.ts b/packages/fig/tests/index.test.ts
index 2a118f1f4..af6387545 100644
--- a/packages/fig/tests/index.test.ts
+++ b/packages/fig/tests/index.test.ts
@@ -18,6 +18,16 @@ import {
writeFigContainer
} from '../src/index'
+const PNG_SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+
+function appendChunk(container: Uint8Array, chunk: Uint8Array): Uint8Array {
+ const bytes = new Uint8Array(container.byteLength + 4 + chunk.byteLength)
+ bytes.set(container)
+ new DataView(bytes.buffer).setUint32(container.byteLength, chunk.byteLength, true)
+ bytes.set(chunk, container.byteLength + 4)
+ return bytes
+}
+
describe('@open-pencil/fig package API', () => {
beforeAll(async () => {
await initCodec()
@@ -68,6 +78,34 @@ describe('@open-pencil/fig package API', () => {
expect(parsed.metaJSON).toBe(metaJSON)
})
+ it('parses legacy raw fig-kiwi files and preserves their thumbnail chunk', () => {
+ const container = writeFigContainer(
+ {
+ schemaDeflated: deflateSync(getSchemaBytes()),
+ dataRaw: encodeMessage(
+ createNodeChangesMessage(0, 0, [
+ {
+ guid: { sessionID: 0, localID: 0 },
+ type: 'DOCUMENT',
+ phase: 'CREATED',
+ name: 'Document'
+ }
+ ])
+ )
+ },
+ { version: 1 }
+ )
+ const thumbnailPNG = new Uint8Array([...PNG_SIGNATURE, 1, 2, 3])
+ const bytes = appendChunk(container, thumbnailPNG)
+ const parsed = parseFigBuffer(bytes.buffer as ArrayBuffer)
+
+ expect(parsed.nodeChanges).toHaveLength(1)
+ expect(parsed.figKiwiVersion).toBe(1)
+ expect(parsed.images).toEqual([])
+ expect(parsed.thumbnailPNG).toEqual(thumbnailPNG)
+ expect(parsed.metaJSON).toBeNull()
+ })
+
it('rejects invalid fig-kiwi containers', () => {
expect(() => readFigContainer(new Uint8Array([1, 2, 3]))).toThrow('Invalid fig-kiwi')
})