@pairdraw/kiwi 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +28 -0
- package/README.md +112 -2
- package/dist/chunks/fig/codec.js +1371 -0
- package/dist/chunks/fig/codec.js.map +1 -0
- package/dist/fig/codec.d.ts +477 -0
- package/dist/fig/codec.js +2 -0
- package/dist/fig/container.d.ts +48 -0
- package/dist/fig/container.js +2 -0
- package/dist/fig/guid.d.ts +13 -0
- package/dist/fig/guid.js +31 -0
- package/dist/fig/guid.js.map +1 -0
- package/dist/fig/parse.d.ts +33 -0
- package/dist/fig/parse.js +83 -0
- package/dist/fig/parse.js.map +1 -0
- package/dist/fig.d.ts +185 -0
- package/dist/fig.js +117 -0
- package/dist/fig.js.map +1 -0
- package/dist/index.d.ts +182 -0
- package/dist/page-manifest.d.ts +16 -0
- package/dist/schema-runtime.d.ts +2 -0
- package/dist/schema-runtime.js +2 -0
- package/dist/types.d.ts +26 -0
- package/package.json +64 -4
- package/src/fig/codec.ts +536 -0
- package/src/fig/container.ts +180 -0
- package/src/fig/guid.ts +24 -0
- package/src/fig/index.ts +5 -0
- package/src/fig/page-manifest.ts +149 -0
- package/src/fig/parse.ts +136 -0
- package/src/fig/protocol.ts +237 -0
- package/src/fig/schema/fig.kiwi +5628 -0
- package/src/fig/schema.ts +7 -0
- package/src/fig/types.ts +25 -0
- package/src/fig/variable-bindings.ts +11 -0
- package/src/global.d.ts +4 -0
- package/src/schema-runtime/bb.ts +284 -0
- package/src/schema-runtime/binary.ts +102 -0
- package/src/schema-runtime/index.ts +25 -0
- package/src/schema-runtime/interpreter.ts +465 -0
- package/src/schema-runtime/parser.ts +287 -0
- package/src/schema-runtime/schema.ts +24 -0
- package/src/schema-runtime/util.ts +10 -0
- package/src/schema-runtime/validate.ts +88 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.js","names":["compileSchema"],"sources":["../../src/fig/parse.ts"],"sourcesContent":["import { inflateSync } from 'fflate'\n\nimport {\n decodeBinarySchema,\n compileSchema,\n ByteBuffer,\n type HeaderFields,\n type RuntimeMessage\n} from '../schema-runtime'\nimport type { NodeChange } from './codec'\nimport { decompressFigKiwiData, figFileKind, type FigFileKind } from './container'\nimport { extractFigPageManifest, type FigPageManifestEntry } from './page-manifest'\n\n/**\n * Deduplicates pluginData/pluginRelaunchData entries on raw NodeChange objects.\n * Some .fig files have millions of identical entries where only a small\n * fraction are unique by full triple.\n * Full-triple key (id+key+value) preserves multi-entry subsystems like OkHCL.\n */\nexport function deduplicateNodeChangePluginData(nodeChanges: NodeChange[]): void {\n for (const nc of nodeChanges) {\n if (nc.pluginData && nc.pluginData.length > 1) {\n const map = new Map<string, (typeof nc.pluginData)[number]>()\n for (const entry of nc.pluginData) {\n map.set(`${entry.pluginID}\\0${entry.key}\\0${entry.value}`, entry)\n }\n if (map.size < nc.pluginData.length) {\n nc.pluginData = [...map.values()]\n }\n }\n if (nc.pluginRelaunchData && nc.pluginRelaunchData.length > 1) {\n const map = new Map<string, (typeof nc.pluginRelaunchData)[number]>()\n for (const entry of nc.pluginRelaunchData) {\n map.set(`${entry.pluginID}\\0${entry.command}\\0${entry.message}\\0${entry.isDeleted}`, entry)\n }\n if (map.size < nc.pluginRelaunchData.length) {\n nc.pluginRelaunchData = [...map.values()]\n }\n }\n }\n}\n\ninterface FigKiwiPayload {\n schemaDeflated: Uint8Array\n dataRaw: Uint8Array\n version: number\n kind: FigFileKind\n}\n\nexport function parseFigKiwiContainer(data: Uint8Array): FigKiwiPayload | null {\n const kind = figFileKind(data)\n if (!kind) return null\n\n const view = new DataView(data.buffer, data.byteOffset, data.byteLength)\n const version = view.getUint32(8, true)\n let offset = 12\n\n const chunks: Uint8Array[] = []\n while (offset < data.length) {\n if (offset + 4 > data.length) break\n const len = view.getUint32(offset, true)\n offset += 4\n if (offset + len > data.length) {\n throw new Error(\n `Corrupted .fig file: chunk at offset ${offset - 4} declares length ${len} but only ${data.length - offset} bytes remain`\n )\n }\n chunks.push(data.slice(offset, offset + len))\n offset += len\n }\n if (chunks.length < 2) return null\n\n return { schemaDeflated: chunks[0], dataRaw: decompressFigKiwiData(chunks[1]), version, kind }\n}\n\nfunction isRuntimeMessage(value: unknown): value is RuntimeMessage {\n return (\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Uint8Array)\n )\n}\n\nexport interface FigKiwiDecodeResult {\n nodeChanges: NodeChange[]\n blobs: Uint8Array[]\n figKiwiVersion: number\n /** Deflated kiwi schema bytes from the original file (for roundtrip fidelity). */\n figSchemaDeflated: Uint8Array\n /** A design file or a FigJam board, by the canvas's header. */\n kind: FigFileKind\n}\n\n/** Decode one raw `fig-kiwi` canvas payload. Outer `.fig` archive handling lives in `@pairdraw/fig`. */\nexport function decodeFigKiwiCanvas(\n data: Uint8Array,\n onPages?: (pages: FigPageManifestEntry[]) => void,\n headers?: HeaderFields\n): FigKiwiDecodeResult {\n const payload = parseFigKiwiContainer(data)\n if (!payload) throw new Error('Invalid fig-kiwi container')\n\n const schemaBytes = inflateSync(payload.schemaDeflated)\n const compiled = compileSchema(decodeBinarySchema(new ByteBuffer(schemaBytes)), headers)\n if (onPages) {\n try {\n const pages = extractFigPageManifest(compiled.schema, payload.dataRaw)\n if (pages.length > 0) onPages(pages)\n } catch (error) {\n console.warn('Failed to scan FIG page manifest; continuing with full decode:', error)\n }\n }\n if (!compiled.decodeMessage) throw new Error('The fig-kiwi schema has no Message')\n const message = compiled.decodeMessage(payload.dataRaw)\n\n const records = message.nodeChanges\n if (!Array.isArray(records) || records.length === 0) {\n throw new Error('No nodes found in .fig file')\n }\n const nodeChanges = records as NodeChange[]\n deduplicateNodeChangePluginData(nodeChanges)\n\n const blobs: Uint8Array[] = (Array.isArray(message.blobs) ? message.blobs : []).map((blob) => {\n const bytes = isRuntimeMessage(blob) ? blob.bytes : undefined\n return bytes instanceof Uint8Array ? bytes : new Uint8Array(Object.values(bytes ?? {}))\n })\n\n return {\n nodeChanges,\n blobs,\n figKiwiVersion: payload.version,\n figSchemaDeflated: payload.schemaDeflated,\n kind: payload.kind\n }\n}\n"],"mappings":"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"}
|
package/dist/fig.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Color, GUID, Matrix, Vector } from "./types.js";
|
|
2
|
+
import { Schema } from "./index.js";
|
|
3
|
+
import { FigPageManifestEntry, extractFigPageManifest } from "./page-manifest.js";
|
|
4
|
+
//#region src/fig/schema.d.ts
|
|
5
|
+
declare const schema: Schema;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/fig/protocol.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Figma Multiplayer Protocol
|
|
10
|
+
*
|
|
11
|
+
* This module handles the low-level WebSocket communication with Figma's
|
|
12
|
+
* multiplayer server. The protocol uses:
|
|
13
|
+
*
|
|
14
|
+
* - Kiwi binary serialization (schema-based, like Protocol Buffers)
|
|
15
|
+
* - Zstd compression for all messages
|
|
16
|
+
* - Session-based authentication via cookies
|
|
17
|
+
*
|
|
18
|
+
* Message types (from Figma's schema):
|
|
19
|
+
* 0 = JOIN_START - Server sends session info
|
|
20
|
+
* 1 = NODE_CHANGES - Create/update/delete nodes
|
|
21
|
+
* 2 = USER_CHANGES - User presence updates
|
|
22
|
+
* 3 = JOIN_END - Initial sync complete
|
|
23
|
+
* 4 = SIGNAL - Various metadata (reconnect info, etc.)
|
|
24
|
+
* 5 = STYLE - Style updates
|
|
25
|
+
* ...and more
|
|
26
|
+
*
|
|
27
|
+
* Wire format:
|
|
28
|
+
* All messages are Zstd-compressed Kiwi-encoded binary data.
|
|
29
|
+
* Zstd magic bytes: 0x28 0xB5 0x2F 0xFD
|
|
30
|
+
*/
|
|
31
|
+
declare const MESSAGE_TYPES: {
|
|
32
|
+
readonly JOIN_START: 0;
|
|
33
|
+
readonly NODE_CHANGES: 1;
|
|
34
|
+
readonly USER_CHANGES: 2;
|
|
35
|
+
readonly JOIN_END: 3;
|
|
36
|
+
readonly SIGNAL: 4;
|
|
37
|
+
readonly STYLE: 5;
|
|
38
|
+
readonly STYLE_SET: 6;
|
|
39
|
+
readonly JOIN_START_SKIP_RELOAD: 7;
|
|
40
|
+
readonly NOTIFY_SHOULD_UPGRADE: 8;
|
|
41
|
+
readonly UPGRADE_DONE: 9;
|
|
42
|
+
readonly UPGRADE_REFRESH: 10;
|
|
43
|
+
readonly SCENE_GRAPH_QUERY: 11;
|
|
44
|
+
readonly SCENE_GRAPH_REPLY: 12;
|
|
45
|
+
readonly DIFF: 13;
|
|
46
|
+
readonly CLIENT_BROADCAST: 14;
|
|
47
|
+
};
|
|
48
|
+
declare const NODE_TYPES: {
|
|
49
|
+
readonly NONE: 0;
|
|
50
|
+
readonly DOCUMENT: 1;
|
|
51
|
+
readonly CANVAS: 2;
|
|
52
|
+
readonly GROUP: 3;
|
|
53
|
+
readonly FRAME: 4;
|
|
54
|
+
readonly BOOLEAN_OPERATION: 5;
|
|
55
|
+
readonly VECTOR: 6;
|
|
56
|
+
readonly STAR: 7;
|
|
57
|
+
readonly LINE: 8;
|
|
58
|
+
readonly ELLIPSE: 9;
|
|
59
|
+
readonly RECTANGLE: 10;
|
|
60
|
+
readonly REGULAR_POLYGON: 11;
|
|
61
|
+
readonly ROUNDED_RECTANGLE: 12;
|
|
62
|
+
readonly TEXT: 13;
|
|
63
|
+
readonly SLICE: 14;
|
|
64
|
+
readonly SYMBOL: 15;
|
|
65
|
+
readonly INSTANCE: 16;
|
|
66
|
+
readonly STICKY: 17;
|
|
67
|
+
readonly SHAPE_WITH_TEXT: 18;
|
|
68
|
+
readonly CONNECTOR: 19;
|
|
69
|
+
readonly CODE_BLOCK: 20;
|
|
70
|
+
readonly WIDGET: 21;
|
|
71
|
+
readonly STAMP: 22;
|
|
72
|
+
readonly MEDIA: 23;
|
|
73
|
+
readonly HIGHLIGHT: 24;
|
|
74
|
+
readonly SECTION: 25;
|
|
75
|
+
readonly SECTION_OVERLAY: 26;
|
|
76
|
+
readonly WASHI_TAPE: 27;
|
|
77
|
+
readonly VARIABLE: 28;
|
|
78
|
+
};
|
|
79
|
+
declare const NODE_PHASES: {
|
|
80
|
+
readonly CREATED: 0;
|
|
81
|
+
readonly REMOVED: 1;
|
|
82
|
+
};
|
|
83
|
+
declare const BLEND_MODES: {
|
|
84
|
+
readonly PASS_THROUGH: 0;
|
|
85
|
+
readonly NORMAL: 1;
|
|
86
|
+
readonly DARKEN: 2;
|
|
87
|
+
readonly MULTIPLY: 3;
|
|
88
|
+
readonly LINEAR_BURN: 4;
|
|
89
|
+
readonly COLOR_BURN: 5;
|
|
90
|
+
readonly LIGHTEN: 6;
|
|
91
|
+
readonly SCREEN: 7;
|
|
92
|
+
readonly LINEAR_DODGE: 8;
|
|
93
|
+
readonly COLOR_DODGE: 9;
|
|
94
|
+
readonly OVERLAY: 10;
|
|
95
|
+
readonly SOFT_LIGHT: 11;
|
|
96
|
+
readonly HARD_LIGHT: 12;
|
|
97
|
+
readonly DIFFERENCE: 13;
|
|
98
|
+
readonly EXCLUSION: 14;
|
|
99
|
+
readonly HUE: 15;
|
|
100
|
+
readonly SATURATION: 16;
|
|
101
|
+
readonly COLOR: 17;
|
|
102
|
+
readonly LUMINOSITY: 18;
|
|
103
|
+
};
|
|
104
|
+
declare const PAINT_TYPES: {
|
|
105
|
+
readonly SOLID: 0;
|
|
106
|
+
readonly GRADIENT_LINEAR: 1;
|
|
107
|
+
readonly GRADIENT_RADIAL: 2;
|
|
108
|
+
readonly GRADIENT_ANGULAR: 3;
|
|
109
|
+
readonly GRADIENT_DIAMOND: 4;
|
|
110
|
+
readonly IMAGE: 5;
|
|
111
|
+
readonly EMOJI: 6;
|
|
112
|
+
readonly VIDEO: 7;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Zstd magic bytes
|
|
116
|
+
*/
|
|
117
|
+
declare const ZSTD_MAGIC: Uint8Array<ArrayBuffer>;
|
|
118
|
+
/**
|
|
119
|
+
* Kiwi uses field numbers to identify message fields.
|
|
120
|
+
* Field 1 with value = message type indicates the message kind.
|
|
121
|
+
*/
|
|
122
|
+
declare const KIWI: {
|
|
123
|
+
/** First byte of valid Kiwi messages (field number 1) */
|
|
124
|
+
readonly MESSAGE_MARKER: 1;
|
|
125
|
+
/** Field number for sessionID in JOIN_START message */
|
|
126
|
+
readonly SESSION_ID_FIELD: 2;
|
|
127
|
+
/** Varint continuation bit (MSB set = more bytes follow) */
|
|
128
|
+
readonly VARINT_CONTINUE_BIT: 128;
|
|
129
|
+
/** Varint value mask (lower 7 bits contain data) */
|
|
130
|
+
readonly VARINT_VALUE_MASK: 127;
|
|
131
|
+
/** Bits per varint byte */
|
|
132
|
+
readonly VARINT_BITS_PER_BYTE: 7;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Valid session ID range (based on observed Figma behavior)
|
|
136
|
+
*/
|
|
137
|
+
declare const SESSION_ID: {
|
|
138
|
+
readonly MIN: 10000;
|
|
139
|
+
readonly MAX: 1000000;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Parse a varint from a Uint8Array at given position
|
|
143
|
+
* Returns [value, newPosition]
|
|
144
|
+
*/
|
|
145
|
+
declare function parseVarint(data: Uint8Array, pos: number): [number, number];
|
|
146
|
+
/**
|
|
147
|
+
* Check if data is a valid Kiwi message
|
|
148
|
+
*/
|
|
149
|
+
declare function isKiwiMessage(data: Uint8Array): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Get message type from Kiwi message
|
|
152
|
+
*/
|
|
153
|
+
declare function getKiwiMessageType(data: Uint8Array): number | null;
|
|
154
|
+
/**
|
|
155
|
+
* fig-wire header magic (first 8 bytes of some messages)
|
|
156
|
+
*/
|
|
157
|
+
declare const FIG_WIRE_MAGIC = "fig-wire";
|
|
158
|
+
/**
|
|
159
|
+
* Check if data is Zstd-compressed
|
|
160
|
+
*/
|
|
161
|
+
declare function isZstdCompressed(data: Uint8Array): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Check if data has fig-wire header
|
|
164
|
+
*/
|
|
165
|
+
declare function hasFigWireHeader(data: Uint8Array): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Skip fig-wire header and find zstd data
|
|
168
|
+
* Header format: "fig-wire" (8 bytes) + version (4 bytes LE) + zstd data
|
|
169
|
+
*/
|
|
170
|
+
declare function skipFigWireHeader(data: Uint8Array): Uint8Array;
|
|
171
|
+
/**
|
|
172
|
+
* Current multiplayer protocol version
|
|
173
|
+
*/
|
|
174
|
+
declare const PROTOCOL_VERSION = 151;
|
|
175
|
+
/**
|
|
176
|
+
* Build WebSocket URL for Figma multiplayer
|
|
177
|
+
*/
|
|
178
|
+
declare function buildMultiplayerURL(fileKey: string, trackingId?: string): string;
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/fig/variable-bindings.d.ts
|
|
181
|
+
/** Figma's plugin-facing variable id, as `VariableID:<session>:<local>`. */
|
|
182
|
+
declare function parseVariableId(variableId: string): GUID | null;
|
|
183
|
+
//#endregion
|
|
184
|
+
export { BLEND_MODES, Color, FIG_WIRE_MAGIC, FigPageManifestEntry, GUID, KIWI, MESSAGE_TYPES, Matrix, NODE_PHASES, NODE_TYPES, PAINT_TYPES, PROTOCOL_VERSION, SESSION_ID, Vector, ZSTD_MAGIC, buildMultiplayerURL, extractFigPageManifest, schema as figmaSchema, getKiwiMessageType, hasFigWireHeader, isKiwiMessage, isZstdCompressed, parseVariableId, parseVarint, skipFigWireHeader };
|
|
185
|
+
//# sourceMappingURL=fig.d.ts.map
|
package/dist/fig.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { BLEND_MODES, ByteBuffer, FIG_WIRE_MAGIC, KIWI, MESSAGE_TYPES, NODE_PHASES, NODE_TYPES, PAINT_TYPES, PROTOCOL_VERSION, SESSION_ID, ZSTD_MAGIC, buildMultiplayerURL, getKiwiMessageType, hasFigWireHeader, isKiwiMessage, isZstdCompressed, parseVarint, schema, skipFigWireHeader } from "./chunks/fig/codec.js";
|
|
2
|
+
//#region src/fig/page-manifest.ts
|
|
3
|
+
function enumName(bb, definition) {
|
|
4
|
+
const value = bb.readVarUint();
|
|
5
|
+
return definition?.kind === "ENUM" ? definition.names[value] ?? null : null;
|
|
6
|
+
}
|
|
7
|
+
function scanNodeChange(bb, schema, nodeChange) {
|
|
8
|
+
let sourceSessionId = null;
|
|
9
|
+
let sourceLocalId = null;
|
|
10
|
+
let parentSessionId = null;
|
|
11
|
+
let parentLocalId = null;
|
|
12
|
+
let positionOffset = null;
|
|
13
|
+
let phase = null;
|
|
14
|
+
let type = null;
|
|
15
|
+
let name = null;
|
|
16
|
+
let internalOnly = false;
|
|
17
|
+
for (let fieldId = bb.readVarUint(); fieldId !== 0; fieldId = bb.readVarUint()) {
|
|
18
|
+
const field = nodeChange.byId[fieldId];
|
|
19
|
+
if (!field) throw new Error(`Invalid field ${fieldId} in Kiwi NodeChange`);
|
|
20
|
+
switch (field.name) {
|
|
21
|
+
case "guid":
|
|
22
|
+
sourceSessionId = bb.readVarUint();
|
|
23
|
+
sourceLocalId = bb.readVarUint();
|
|
24
|
+
break;
|
|
25
|
+
case "parentIndex":
|
|
26
|
+
parentSessionId = bb.readVarUint();
|
|
27
|
+
parentLocalId = bb.readVarUint();
|
|
28
|
+
positionOffset = bb.offset;
|
|
29
|
+
bb.skipString();
|
|
30
|
+
break;
|
|
31
|
+
case "phase":
|
|
32
|
+
phase = enumName(bb, field.definition);
|
|
33
|
+
break;
|
|
34
|
+
case "type":
|
|
35
|
+
type = enumName(bb, field.definition);
|
|
36
|
+
break;
|
|
37
|
+
case "name":
|
|
38
|
+
if (type === "DOCUMENT" || type === "CANVAS") name = bb.readString();
|
|
39
|
+
else bb.skipString();
|
|
40
|
+
break;
|
|
41
|
+
case "internalOnly":
|
|
42
|
+
internalOnly = !!bb.readByte();
|
|
43
|
+
break;
|
|
44
|
+
default: schema.skip(field, bb);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (type !== "DOCUMENT" && type !== "CANVAS" || sourceSessionId === null || sourceLocalId === null) return null;
|
|
48
|
+
let position = null;
|
|
49
|
+
if (positionOffset !== null) {
|
|
50
|
+
const endOffset = bb.offset;
|
|
51
|
+
bb.offset = positionOffset;
|
|
52
|
+
position = bb.readString();
|
|
53
|
+
bb.offset = endOffset;
|
|
54
|
+
}
|
|
55
|
+
const parentId = parentSessionId === null || parentLocalId === null ? null : `${parentSessionId}:${parentLocalId}`;
|
|
56
|
+
return {
|
|
57
|
+
sourceId: `${sourceSessionId}:${sourceLocalId}`,
|
|
58
|
+
parentId,
|
|
59
|
+
position,
|
|
60
|
+
phase,
|
|
61
|
+
type,
|
|
62
|
+
name: name ?? "Page",
|
|
63
|
+
internalOnly
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Scans encoded Kiwi bytes without materializing NodeChange objects. Kiwi messages are not
|
|
68
|
+
* length-prefixed, so every value is visited, but only document/page fields are allocated.
|
|
69
|
+
*/
|
|
70
|
+
function extractFigPageManifest(schema, data) {
|
|
71
|
+
const message = schema.definition("Message");
|
|
72
|
+
const nodeChange = schema.definition("NodeChange");
|
|
73
|
+
if (message.kind !== "MESSAGE" || nodeChange.kind !== "MESSAGE") return [];
|
|
74
|
+
const bb = new ByteBuffer(data);
|
|
75
|
+
const candidates = [];
|
|
76
|
+
for (let fieldId = bb.readVarUint(); fieldId !== 0; fieldId = bb.readVarUint()) {
|
|
77
|
+
const field = message.byId[fieldId];
|
|
78
|
+
if (!field) throw new Error(`Invalid field ${fieldId} in Kiwi Message`);
|
|
79
|
+
if (field.name !== "nodeChanges" || !field.isArray) {
|
|
80
|
+
schema.skip(field, bb);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
for (let length = bb.readVarUint(); length > 0; length--) {
|
|
84
|
+
const candidate = scanNodeChange(bb, schema, nodeChange);
|
|
85
|
+
if (candidate) candidates.push(candidate);
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
const documentId = candidates.find((candidate) => candidate.type === "DOCUMENT" && candidate.phase !== "REMOVED")?.sourceId;
|
|
90
|
+
return candidates.filter((candidate) => candidate.type === "CANVAS" && candidate.phase !== "REMOVED" && (!documentId || candidate.parentId === documentId)).sort((a, b) => {
|
|
91
|
+
const aPosition = a.position ?? "";
|
|
92
|
+
const bPosition = b.position ?? "";
|
|
93
|
+
if (aPosition < bPosition) return -1;
|
|
94
|
+
if (aPosition > bPosition) return 1;
|
|
95
|
+
return 0;
|
|
96
|
+
}).map(({ sourceId, name, position, internalOnly }) => ({
|
|
97
|
+
sourceId,
|
|
98
|
+
name,
|
|
99
|
+
position,
|
|
100
|
+
internalOnly
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/fig/variable-bindings.ts
|
|
105
|
+
/** Figma's plugin-facing variable id, as `VariableID:<session>:<local>`. */
|
|
106
|
+
function parseVariableId(variableId) {
|
|
107
|
+
const match = variableId.match(/VariableID:(\d+):(\d+)/);
|
|
108
|
+
if (!match) return null;
|
|
109
|
+
return {
|
|
110
|
+
sessionID: Number.parseInt(match.at(1) ?? "0", 10),
|
|
111
|
+
localID: Number.parseInt(match.at(2) ?? "0", 10)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { BLEND_MODES, FIG_WIRE_MAGIC, KIWI, MESSAGE_TYPES, NODE_PHASES, NODE_TYPES, PAINT_TYPES, PROTOCOL_VERSION, SESSION_ID, ZSTD_MAGIC, buildMultiplayerURL, extractFigPageManifest, schema as figmaSchema, getKiwiMessageType, hasFigWireHeader, isKiwiMessage, isZstdCompressed, parseVariableId, parseVarint, skipFigWireHeader };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=fig.js.map
|
package/dist/fig.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fig.js","names":[],"sources":["../src/fig/page-manifest.ts","../src/fig/variable-bindings.ts"],"sourcesContent":["import { ByteBuffer } from '../schema-runtime/bb'\nimport type { CompiledDefinition, CompiledSchema } from '../schema-runtime/interpreter'\n\nexport interface FigPageManifestEntry {\n sourceId: string\n name: string\n position: string | null\n internalOnly: boolean\n}\n\ninterface PageCandidate extends FigPageManifestEntry {\n parentId: string | null\n phase: string | null\n type: string | null\n}\n\nfunction enumName(bb: ByteBuffer, definition: CompiledDefinition | null): string | null {\n const value = bb.readVarUint()\n return definition?.kind === 'ENUM' ? (definition.names[value] ?? null) : null\n}\n\nfunction scanNodeChange(\n bb: ByteBuffer,\n schema: CompiledSchema,\n nodeChange: CompiledDefinition\n): PageCandidate | null {\n let sourceSessionId: number | null = null\n let sourceLocalId: number | null = null\n let parentSessionId: number | null = null\n let parentLocalId: number | null = null\n let positionOffset: number | null = null\n let phase: string | null = null\n let type: string | null = null\n let name: string | null = null\n let internalOnly = false\n\n for (let fieldId = bb.readVarUint(); fieldId !== 0; fieldId = bb.readVarUint()) {\n const field = nodeChange.byId[fieldId]\n if (!field) throw new Error(`Invalid field ${fieldId} in Kiwi NodeChange`)\n switch (field.name) {\n case 'guid':\n sourceSessionId = bb.readVarUint()\n sourceLocalId = bb.readVarUint()\n break\n case 'parentIndex':\n parentSessionId = bb.readVarUint()\n parentLocalId = bb.readVarUint()\n positionOffset = bb.offset\n bb.skipString()\n break\n case 'phase':\n phase = enumName(bb, field.definition)\n break\n case 'type':\n type = enumName(bb, field.definition)\n break\n case 'name':\n if (type === 'DOCUMENT' || type === 'CANVAS') name = bb.readString()\n else bb.skipString()\n break\n case 'internalOnly':\n internalOnly = !!bb.readByte()\n break\n default:\n schema.skip(field, bb)\n }\n }\n\n if (\n (type !== 'DOCUMENT' && type !== 'CANVAS') ||\n sourceSessionId === null ||\n sourceLocalId === null\n ) {\n return null\n }\n let position: string | null = null\n if (positionOffset !== null) {\n const endOffset = bb.offset\n bb.offset = positionOffset\n position = bb.readString()\n bb.offset = endOffset\n }\n const parentId =\n parentSessionId === null || parentLocalId === null\n ? null\n : `${parentSessionId}:${parentLocalId}`\n return {\n sourceId: `${sourceSessionId}:${sourceLocalId}`,\n parentId,\n position,\n phase,\n type,\n name: name ?? 'Page',\n internalOnly\n }\n}\n\n/**\n * Scans encoded Kiwi bytes without materializing NodeChange objects. Kiwi messages are not\n * length-prefixed, so every value is visited, but only document/page fields are allocated.\n */\nexport function extractFigPageManifest(\n schema: CompiledSchema,\n data: Uint8Array\n): FigPageManifestEntry[] {\n const message = schema.definition('Message')\n const nodeChange = schema.definition('NodeChange')\n if (message.kind !== 'MESSAGE' || nodeChange.kind !== 'MESSAGE') return []\n\n const bb = new ByteBuffer(data)\n const candidates: PageCandidate[] = []\n for (let fieldId = bb.readVarUint(); fieldId !== 0; fieldId = bb.readVarUint()) {\n const field = message.byId[fieldId]\n if (!field) throw new Error(`Invalid field ${fieldId} in Kiwi Message`)\n if (field.name !== 'nodeChanges' || !field.isArray) {\n schema.skip(field, bb)\n continue\n }\n for (let length = bb.readVarUint(); length > 0; length--) {\n const candidate = scanNodeChange(bb, schema, nodeChange)\n if (candidate) candidates.push(candidate)\n }\n break\n }\n\n const documentId = candidates.find(\n (candidate) => candidate.type === 'DOCUMENT' && candidate.phase !== 'REMOVED'\n )?.sourceId\n return candidates\n .filter(\n (candidate) =>\n candidate.type === 'CANVAS' &&\n candidate.phase !== 'REMOVED' &&\n (!documentId || candidate.parentId === documentId)\n )\n .sort((a, b) => {\n const aPosition = a.position ?? ''\n const bPosition = b.position ?? ''\n if (aPosition < bPosition) return -1\n if (aPosition > bPosition) return 1\n return 0\n })\n .map(({ sourceId, name, position, internalOnly }) => ({\n sourceId,\n name,\n position,\n internalOnly\n }))\n}\n","import type { GUID } from './types'\n\n/** Figma's plugin-facing variable id, as `VariableID:<session>:<local>`. */\nexport function parseVariableId(variableId: string): GUID | null {\n const match = variableId.match(/VariableID:(\\d+):(\\d+)/)\n if (!match) return null\n return {\n sessionID: Number.parseInt(match.at(1) ?? '0', 10),\n localID: Number.parseInt(match.at(2) ?? '0', 10)\n }\n}\n"],"mappings":"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"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
//#region src/schema-runtime/schema.d.ts
|
|
2
|
+
interface Schema {
|
|
3
|
+
package: string | null;
|
|
4
|
+
definitions: Definition[];
|
|
5
|
+
}
|
|
6
|
+
type DefinitionKind = 'ENUM' | 'STRUCT' | 'MESSAGE';
|
|
7
|
+
interface Definition {
|
|
8
|
+
name: string;
|
|
9
|
+
line: number;
|
|
10
|
+
column: number;
|
|
11
|
+
kind: DefinitionKind;
|
|
12
|
+
fields: Field[];
|
|
13
|
+
}
|
|
14
|
+
interface Field {
|
|
15
|
+
name: string;
|
|
16
|
+
line: number;
|
|
17
|
+
column: number;
|
|
18
|
+
type: string | null;
|
|
19
|
+
isArray: boolean;
|
|
20
|
+
isDeprecated: boolean;
|
|
21
|
+
value: number;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/schema-runtime/bb.d.ts
|
|
25
|
+
declare class ByteBuffer {
|
|
26
|
+
private _data;
|
|
27
|
+
private _index;
|
|
28
|
+
length: number;
|
|
29
|
+
constructor(data?: Uint8Array);
|
|
30
|
+
get offset(): number;
|
|
31
|
+
set offset(value: number);
|
|
32
|
+
/** The bytes this buffer reads, as a view rather than a copy. */
|
|
33
|
+
get bytes(): Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* Returns a view into the internal buffer, not a copy.
|
|
36
|
+
*
|
|
37
|
+
* Consumers transferring this Uint8Array across thread boundaries (e.g. via
|
|
38
|
+
* postMessage with transferables) MUST copy first: `new Uint8Array(buffer)`.
|
|
39
|
+
* Otherwise, if multiple Uint8Arrays share the same underlying ArrayBuffer and
|
|
40
|
+
* one is transferred, all views into that buffer become detached.
|
|
41
|
+
*/
|
|
42
|
+
toUint8Array(): Uint8Array;
|
|
43
|
+
readByte(): number;
|
|
44
|
+
readByteArray(): Uint8Array;
|
|
45
|
+
skipByteArray(): void;
|
|
46
|
+
readVarFloat(): number;
|
|
47
|
+
readVarUint(): number;
|
|
48
|
+
readVarInt(): number;
|
|
49
|
+
readVarUint64(): bigint;
|
|
50
|
+
readVarInt64(): bigint;
|
|
51
|
+
readString(): string;
|
|
52
|
+
skipString(): void;
|
|
53
|
+
private findStringTerminator;
|
|
54
|
+
private _growBy;
|
|
55
|
+
writeByte(value: number): void;
|
|
56
|
+
writeByteArray(value: Uint8Array): void;
|
|
57
|
+
writeVarFloat(value: number): void;
|
|
58
|
+
writeVarUint(value: number): void;
|
|
59
|
+
writeVarInt(value: number): void;
|
|
60
|
+
writeVarUint64(value: bigint | string): void;
|
|
61
|
+
writeVarInt64(value: bigint | string): void;
|
|
62
|
+
writeString(value: string): void;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/schema-runtime/interpreter.d.ts
|
|
66
|
+
type RuntimeValue = boolean | number | string | bigint | Uint8Array | RuntimeMessage | RuntimeValue[];
|
|
67
|
+
type RuntimeMessage = {
|
|
68
|
+
[name: string]: RuntimeValue;
|
|
69
|
+
};
|
|
70
|
+
type RuntimeEnum = {
|
|
71
|
+
[name: string]: string | number;
|
|
72
|
+
};
|
|
73
|
+
type DecodeFunction = (bb: ByteBuffer | Uint8Array) => RuntimeMessage;
|
|
74
|
+
type EncodeFunction = (message: RuntimeMessage, bb?: ByteBuffer) => Uint8Array | undefined;
|
|
75
|
+
type RuntimeEntry = typeof ByteBuffer | CompiledSchema | RuntimeEnum | RuntimeValue | DecodeFunction | EncodeFunction | undefined;
|
|
76
|
+
type RuntimeCodec = {
|
|
77
|
+
ByteBuffer: typeof ByteBuffer;
|
|
78
|
+
/** The tables the codec reads with, for scanners that pick single fields out of a message. */
|
|
79
|
+
schema: CompiledSchema;
|
|
80
|
+
encodeMessage?: EncodeFunction;
|
|
81
|
+
decodeMessage?: DecodeFunction;
|
|
82
|
+
encodePaint?: EncodeFunction;
|
|
83
|
+
encodeNodeChange?: EncodeFunction;
|
|
84
|
+
[name: string]: RuntimeEntry;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Message types decoded as headers: only the named fields are read, and the rest stay in the
|
|
88
|
+
* message bytes until `decodeWhole` reads the message in full. A large message of many such
|
|
89
|
+
* messages then holds a little of each rather than every value of every one.
|
|
90
|
+
*/
|
|
91
|
+
type HeaderFields = Readonly<Record<string, readonly string[]>>;
|
|
92
|
+
/** What a field holds: a native type, or an enum, struct, or message of the schema. */
|
|
93
|
+
declare const TYPE: {
|
|
94
|
+
readonly BOOL: 0;
|
|
95
|
+
readonly BYTE: 1;
|
|
96
|
+
readonly INT: 2;
|
|
97
|
+
readonly UINT: 3;
|
|
98
|
+
readonly FLOAT: 4;
|
|
99
|
+
readonly STRING: 5;
|
|
100
|
+
readonly INT64: 6;
|
|
101
|
+
readonly UINT64: 7;
|
|
102
|
+
readonly ENUM: 8;
|
|
103
|
+
readonly STRUCT: 9;
|
|
104
|
+
readonly MESSAGE: 10;
|
|
105
|
+
};
|
|
106
|
+
type TypeCode = (typeof TYPE)[keyof typeof TYPE];
|
|
107
|
+
/** A field resolved once: its type as a number, and the definition behind it. */
|
|
108
|
+
interface CompiledField {
|
|
109
|
+
readonly name: string;
|
|
110
|
+
readonly id: number;
|
|
111
|
+
readonly isArray: boolean;
|
|
112
|
+
readonly isDeprecated: boolean;
|
|
113
|
+
readonly type: TypeCode;
|
|
114
|
+
/** The definition an enum, struct, or message field holds; null for a native type. */
|
|
115
|
+
readonly definition: CompiledDefinition | null;
|
|
116
|
+
/** Whether a header of the owning message keeps this field. */
|
|
117
|
+
readonly kept: boolean;
|
|
118
|
+
}
|
|
119
|
+
interface CompiledDefinition {
|
|
120
|
+
readonly name: string;
|
|
121
|
+
readonly kind: DefinitionKind;
|
|
122
|
+
readonly fields: readonly CompiledField[];
|
|
123
|
+
/** A message's fields by ID, with a hole at every ID the definition does not use. */
|
|
124
|
+
readonly byId: ReadonlyArray<CompiledField | undefined>;
|
|
125
|
+
/** An enum's member names by value. */
|
|
126
|
+
readonly names: ReadonlyArray<string | undefined>;
|
|
127
|
+
/** An enum's values by member name. */
|
|
128
|
+
readonly values: ReadonlyMap<string, number>;
|
|
129
|
+
/** Whether messages of this definition decode as headers. */
|
|
130
|
+
readonly header: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* A schema's definitions resolved into tables, and the one reader, skipper, and writer that
|
|
134
|
+
* every codec and scanner shares; nothing is looked up by name while a message is read.
|
|
135
|
+
*/
|
|
136
|
+
interface CompiledSchema {
|
|
137
|
+
readonly definitions: readonly CompiledDefinition[];
|
|
138
|
+
/** A definition by name; throws for one the schema lacks. */
|
|
139
|
+
definition(name: string): CompiledDefinition;
|
|
140
|
+
/** One field's value, its array included; its shape follows the schema at run time. */
|
|
141
|
+
read(field: CompiledField, bb: ByteBuffer): unknown;
|
|
142
|
+
/** Past one field's value, its array included. */
|
|
143
|
+
skip(field: CompiledField, bb: ByteBuffer): void;
|
|
144
|
+
/** One field's value, its array included; a message field writes its own end marker. */
|
|
145
|
+
write(field: CompiledField, value: RuntimeValue, bb: ByteBuffer): void;
|
|
146
|
+
/** A struct or message read in full, or as a header where the definition is one. */
|
|
147
|
+
decode(definition: CompiledDefinition, bb: ByteBuffer): RuntimeMessage;
|
|
148
|
+
/** A struct or message written in full. */
|
|
149
|
+
encode(definition: CompiledDefinition, message: RuntimeMessage, bb: ByteBuffer): void;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* A header read again in full from the bytes it came from, as a new message the caller owns,
|
|
153
|
+
* or undefined for a message that was decoded whole.
|
|
154
|
+
*/
|
|
155
|
+
declare function decodeWhole(message: object): RuntimeMessage | undefined;
|
|
156
|
+
declare function compileSchemaTables(schema: Schema, headers?: HeaderFields): CompiledSchema;
|
|
157
|
+
/**
|
|
158
|
+
* Interprets a schema through its compiled tables instead of generating and `eval`-ing a
|
|
159
|
+
* per-schema decoder/encoder (as `compileSchemaJS` + `new Function` do). A `new Function(...)`-
|
|
160
|
+
* built codec is indistinguishable from `eval` to a CSP: embedders that run under `script-src`
|
|
161
|
+
* without `unsafe-eval` (e.g. a sandboxed plugin host) cannot call `compileSchema` at all
|
|
162
|
+
* otherwise. The tables produce byte-identical output to the generated code: same field order,
|
|
163
|
+
* same tag/no-tag rules for MESSAGE vs STRUCT, same deprecated-field skip behavior.
|
|
164
|
+
*/
|
|
165
|
+
declare function compileSchemaRuntime(schema: Schema, headers?: HeaderFields): RuntimeCodec;
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/schema-runtime/binary.d.ts
|
|
168
|
+
declare function decodeBinarySchema(buffer: Uint8Array | ByteBuffer): Schema;
|
|
169
|
+
declare function encodeBinarySchema(schema: Schema): Uint8Array;
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/schema-runtime/parser.d.ts
|
|
172
|
+
declare function parseSchema(text: string): Schema;
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/schema-runtime/validate.d.ts
|
|
175
|
+
declare function findDefinition(schema: Schema, name: string): Definition | null;
|
|
176
|
+
declare function findField(schema: Schema, definitionName: string, fieldName: string): Field | null;
|
|
177
|
+
declare function expectFieldNumber(schema: Schema, definitionName: string, fieldName: string, expectedValue: number): void;
|
|
178
|
+
declare function expectEnumValue(schema: Schema, enumName: string, memberName: string, expectedValue: number): void;
|
|
179
|
+
declare function validateSchema(schema: Schema): void;
|
|
180
|
+
//#endregion
|
|
181
|
+
export { ByteBuffer, CompiledDefinition, CompiledField, CompiledSchema, Definition, Field, HeaderFields, RuntimeCodec, RuntimeMessage, RuntimeValue, Schema, TYPE, TypeCode, compileSchemaRuntime, compileSchemaTables, decodeBinarySchema, decodeWhole, encodeBinarySchema, expectEnumValue, expectFieldNumber, findDefinition, findField, parseSchema, validateSchema };
|
|
182
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CompiledSchema } from "./index.js";
|
|
2
|
+
//#region src/fig/page-manifest.d.ts
|
|
3
|
+
interface FigPageManifestEntry {
|
|
4
|
+
sourceId: string;
|
|
5
|
+
name: string;
|
|
6
|
+
position: string | null;
|
|
7
|
+
internalOnly: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Scans encoded Kiwi bytes without materializing NodeChange objects. Kiwi messages are not
|
|
11
|
+
* length-prefixed, so every value is visited, but only document/page fields are allocated.
|
|
12
|
+
*/
|
|
13
|
+
declare function extractFigPageManifest(schema: CompiledSchema, data: Uint8Array): FigPageManifestEntry[];
|
|
14
|
+
//#endregion
|
|
15
|
+
export { FigPageManifestEntry, extractFigPageManifest };
|
|
16
|
+
//# sourceMappingURL=page-manifest.d.ts.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { ByteBuffer, CompiledDefinition, CompiledField, CompiledSchema, Definition, Field, HeaderFields, RuntimeCodec, RuntimeMessage, RuntimeValue, Schema, TYPE, TypeCode, compileSchemaRuntime, compileSchemaTables, decodeBinarySchema, decodeWhole, encodeBinarySchema, expectEnumValue, expectFieldNumber, findDefinition, findField, parseSchema, validateSchema } from "./index.js";
|
|
2
|
+
export { ByteBuffer, type CompiledDefinition, type CompiledField, type CompiledSchema, type Definition, type Field, type HeaderFields, type RuntimeCodec, type RuntimeMessage, type RuntimeValue, type Schema, TYPE, type TypeCode, compileSchemaRuntime as compileSchema, compileSchemaTables, decodeBinarySchema, decodeWhole, encodeBinarySchema, expectEnumValue, expectFieldNumber, findDefinition, findField, parseSchema, validateSchema };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { ByteBuffer, TYPE, compileSchemaRuntime, compileSchemaTables, decodeBinarySchema, decodeWhole, encodeBinarySchema, expectEnumValue, expectFieldNumber, findDefinition, findField, parseSchema, validateSchema } from "./chunks/fig/codec.js";
|
|
2
|
+
export { ByteBuffer, TYPE, compileSchemaRuntime as compileSchema, compileSchemaTables, decodeBinarySchema, decodeWhole, encodeBinarySchema, expectEnumValue, expectFieldNumber, findDefinition, findField, parseSchema, validateSchema };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/fig/types.d.ts
|
|
2
|
+
interface GUID {
|
|
3
|
+
sessionID: number;
|
|
4
|
+
localID: number;
|
|
5
|
+
}
|
|
6
|
+
interface Color {
|
|
7
|
+
r: number;
|
|
8
|
+
g: number;
|
|
9
|
+
b: number;
|
|
10
|
+
a: number;
|
|
11
|
+
}
|
|
12
|
+
interface Vector {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
}
|
|
16
|
+
interface Matrix {
|
|
17
|
+
m00: number;
|
|
18
|
+
m01: number;
|
|
19
|
+
m02: number;
|
|
20
|
+
m10: number;
|
|
21
|
+
m11: number;
|
|
22
|
+
m12: number;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { Color, GUID, Matrix, Vector };
|
|
26
|
+
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/kiwi",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.16.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src",
|
|
8
|
+
"README.md",
|
|
9
|
+
"NOTICE"
|
|
10
|
+
],
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@hpcc-js/wasm-zstd": "^1.16.2",
|
|
13
|
+
"fflate": "^0.8.3"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
"./schema-runtime": {
|
|
17
|
+
"types": "./dist/schema-runtime.d.ts",
|
|
18
|
+
"import": "./dist/schema-runtime.js",
|
|
19
|
+
"default": "./dist/schema-runtime.js"
|
|
20
|
+
},
|
|
21
|
+
"./fig": {
|
|
22
|
+
"types": "./dist/fig.d.ts",
|
|
23
|
+
"import": "./dist/fig.js",
|
|
24
|
+
"default": "./dist/fig.js"
|
|
25
|
+
},
|
|
26
|
+
"./fig/codec": {
|
|
27
|
+
"types": "./dist/fig/codec.d.ts",
|
|
28
|
+
"import": "./dist/fig/codec.js",
|
|
29
|
+
"default": "./dist/fig/codec.js"
|
|
30
|
+
},
|
|
31
|
+
"./fig/container": {
|
|
32
|
+
"types": "./dist/fig/container.d.ts",
|
|
33
|
+
"import": "./dist/fig/container.js",
|
|
34
|
+
"default": "./dist/fig/container.js"
|
|
35
|
+
},
|
|
36
|
+
"./fig/guid": {
|
|
37
|
+
"types": "./dist/fig/guid.d.ts",
|
|
38
|
+
"import": "./dist/fig/guid.js",
|
|
39
|
+
"default": "./dist/fig/guid.js"
|
|
40
|
+
},
|
|
41
|
+
"./fig/parse": {
|
|
42
|
+
"types": "./dist/fig/parse.d.ts",
|
|
43
|
+
"import": "./dist/fig/parse.js",
|
|
44
|
+
"default": "./dist/fig/parse.js"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"description": "Kiwi schema, runtime, codecs, and containers for Figma-format files, without SceneGraph knowledge.",
|
|
48
|
+
"keywords": [
|
|
49
|
+
"pairdraw",
|
|
50
|
+
"kiwi",
|
|
51
|
+
"figma",
|
|
52
|
+
"binary-format"
|
|
53
|
+
],
|
|
54
|
+
"author": "Badryansah Bangsawan",
|
|
55
|
+
"homepage": "https://github.com/BadryansahBangsawan/pairdraw/tree/main/packages/kiwi#readme",
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/BadryansahBangsawan/pairdraw/issues"
|
|
58
|
+
},
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "git+https://github.com/BadryansahBangsawan/pairdraw.git",
|
|
62
|
+
"directory": "packages/kiwi"
|
|
63
|
+
},
|
|
64
|
+
"type": "module",
|
|
65
|
+
"sideEffects": false
|
|
66
|
+
}
|