@moondraft/import 0.0.0-stage → 0.2.0
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/CHANGELOG.md +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +45 -0
- package/dist/adapter/variables.js +106 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5665 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +96 -0
- package/dist/taco/model/component-evaluation.js +555 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +79 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2369 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +55 -4
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Payload } from '@moondraft/editor/core';
|
|
2
|
+
/**
|
|
3
|
+
* Builds editable layers from the agent build tool's layer syntax on an isolated core.
|
|
4
|
+
* @param text - Source text accepted by the build tool.
|
|
5
|
+
* @returns The built layers as a paste payload.
|
|
6
|
+
* @throws An error if the build is refused or produces no layers.
|
|
7
|
+
*/
|
|
8
|
+
export declare function payloadFromSyntax(text: string): Payload;
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createAgentTools } from '@moondraft/editor/agent';
|
|
2
|
+
import { clipboard, createCore } from '@moondraft/editor/core';
|
|
3
|
+
/**
|
|
4
|
+
* Builds editable layers from the agent build tool's layer syntax on an isolated core.
|
|
5
|
+
* @param text - Source text accepted by the build tool.
|
|
6
|
+
* @returns The built layers as a paste payload.
|
|
7
|
+
* @throws An error if the build is refused or produces no layers.
|
|
8
|
+
*/
|
|
9
|
+
export function payloadFromSyntax(text) {
|
|
10
|
+
const core = createCore();
|
|
11
|
+
const build = createAgentTools(core).find((tool) => tool.name === 'build');
|
|
12
|
+
const result = build.run({ do: text });
|
|
13
|
+
if (!result.ok)
|
|
14
|
+
throw new Error(result.text);
|
|
15
|
+
const payload = clipboard.copy(core, core.doc.children(''));
|
|
16
|
+
if (!payload)
|
|
17
|
+
throw new Error('build produced no layers');
|
|
18
|
+
return payload;
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type KiwiContainerHeader, type KiwiContainerOptions } from "./container/kiwiContainer.ts";
|
|
2
|
+
import type { KiwiObject, KiwiSchema } from "./kiwi/types.ts";
|
|
3
|
+
/** The JSON Figma puts in the `figmeta` marker. */
|
|
4
|
+
export interface FigmaClipboardMeta {
|
|
5
|
+
readonly fileKey: string;
|
|
6
|
+
readonly pasteID: number;
|
|
7
|
+
/** `"scene"` for copied layers. */
|
|
8
|
+
readonly dataType: string;
|
|
9
|
+
}
|
|
10
|
+
export interface FigmaClipboard {
|
|
11
|
+
readonly meta: FigmaClipboardMeta;
|
|
12
|
+
readonly header: KiwiContainerHeader;
|
|
13
|
+
readonly schema: KiwiSchema;
|
|
14
|
+
readonly message: KiwiObject;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads the `text/html` Figma writes when layers are copied. The HTML holds
|
|
18
|
+
* two comments, `<!--(figmeta)BASE64(/figmeta)-->` with JSON about the copy
|
|
19
|
+
* and `<!--(figma)BASE64(/figma)-->` with a Kiwi container. Some browsers
|
|
20
|
+
* deliver the comment brackets escaped as `<!--` and `-->`; both
|
|
21
|
+
* spellings are accepted. The HTML is searched as text, so this runs
|
|
22
|
+
* without a DOM. Chunks are limited to `FIGMA_READ_LIMITS.maxPasteChunkBytes`
|
|
23
|
+
* unless `options.maxDecompressedChunkBytes` says otherwise.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readFigmaClipboardHtml(html: string, options?: KiwiContainerOptions): FigmaClipboard;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { FigmaReadError } from "./container/error.js";
|
|
2
|
+
import { FIGMA_READ_LIMITS } from "./container/limits.js";
|
|
3
|
+
import { readKiwiContainer } from "./container/kiwiContainer.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reads the `text/html` Figma writes when layers are copied. The HTML holds
|
|
6
|
+
* two comments, `<!--(figmeta)BASE64(/figmeta)-->` with JSON about the copy
|
|
7
|
+
* and `<!--(figma)BASE64(/figma)-->` with a Kiwi container. Some browsers
|
|
8
|
+
* deliver the comment brackets escaped as `<!--` and `-->`; both
|
|
9
|
+
* spellings are accepted. The HTML is searched as text, so this runs
|
|
10
|
+
* without a DOM. Chunks are limited to `FIGMA_READ_LIMITS.maxPasteChunkBytes`
|
|
11
|
+
* unless `options.maxDecompressedChunkBytes` says otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export function readFigmaClipboardHtml(html, options = {}) {
|
|
14
|
+
if (typeof html !== "string") {
|
|
15
|
+
throw new TypeError("readFigmaClipboardHtml expects a string");
|
|
16
|
+
}
|
|
17
|
+
const meta = readMeta(decodeBase64(findMarker(html, "figmeta"), "figmeta"));
|
|
18
|
+
const { header, schema, message } = readKiwiContainer(decodeBase64(findMarker(html, "figma"), "figma"), {
|
|
19
|
+
maxDecompressedChunkBytes: options.maxDecompressedChunkBytes ?? FIGMA_READ_LIMITS.maxPasteChunkBytes,
|
|
20
|
+
});
|
|
21
|
+
return { meta, header, schema, message };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Finds the one `(name)…(/name)` marker inside a comment. The search moves
|
|
25
|
+
* forward only: each `indexOf` starts where the last one stopped, so the
|
|
26
|
+
* time is linear in the HTML size even for many unclosed openers.
|
|
27
|
+
*/
|
|
28
|
+
function findMarker(html, name) {
|
|
29
|
+
const opener = `(${name})`;
|
|
30
|
+
const closer = `(/${name})`;
|
|
31
|
+
const payloads = [];
|
|
32
|
+
let at = 0;
|
|
33
|
+
for (;;) {
|
|
34
|
+
const open = html.indexOf(opener, at);
|
|
35
|
+
if (open < 0)
|
|
36
|
+
break;
|
|
37
|
+
at = open + opener.length;
|
|
38
|
+
if (!html.startsWith("<!--", open - 4) && !html.startsWith("<!--", open - 7))
|
|
39
|
+
continue;
|
|
40
|
+
let close = html.indexOf(closer, at);
|
|
41
|
+
while (close >= 0 && !html.startsWith("-->", close + closer.length) && !html.startsWith("-->", close + closer.length)) {
|
|
42
|
+
close = html.indexOf(closer, close + closer.length);
|
|
43
|
+
}
|
|
44
|
+
// No closed comment after this opener means none after any later opener either.
|
|
45
|
+
if (close < 0)
|
|
46
|
+
break;
|
|
47
|
+
payloads.push(html.slice(at, close));
|
|
48
|
+
at = close + closer.length;
|
|
49
|
+
}
|
|
50
|
+
if (payloads.length === 0) {
|
|
51
|
+
throw new FigmaReadError("missing-marker", `the HTML has no (${name}) marker`);
|
|
52
|
+
}
|
|
53
|
+
if (payloads.length > 1) {
|
|
54
|
+
throw new FigmaReadError("duplicate-marker", `the HTML has ${payloads.length} (${name}) markers, expected one`);
|
|
55
|
+
}
|
|
56
|
+
return payloads[0];
|
|
57
|
+
}
|
|
58
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
59
|
+
function decodeBase64(text, name) {
|
|
60
|
+
const compact = text.replace(/[\t\n\f\r ]+/g, "");
|
|
61
|
+
if (compact.length === 0 || !BASE64.test(compact)) {
|
|
62
|
+
throw new FigmaReadError("bad-base64", `the (${name}) marker does not hold valid base64`);
|
|
63
|
+
}
|
|
64
|
+
const binary = atob(compact);
|
|
65
|
+
const bytes = new Uint8Array(binary.length);
|
|
66
|
+
for (let index = 0; index < binary.length; index += 1)
|
|
67
|
+
bytes[index] = binary.charCodeAt(index);
|
|
68
|
+
return bytes;
|
|
69
|
+
}
|
|
70
|
+
function readMeta(bytes) {
|
|
71
|
+
let value;
|
|
72
|
+
try {
|
|
73
|
+
value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
77
|
+
throw new FigmaReadError("bad-meta", `the (figmeta) marker is not UTF-8 JSON: ${reason}`);
|
|
78
|
+
}
|
|
79
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
80
|
+
throw new FigmaReadError("bad-meta", "the (figmeta) JSON is not an object");
|
|
81
|
+
}
|
|
82
|
+
const { fileKey, pasteID, dataType } = value;
|
|
83
|
+
if (typeof fileKey !== "string" || typeof pasteID !== "number" || typeof dataType !== "string") {
|
|
84
|
+
throw new FigmaReadError("bad-meta", "the (figmeta) JSON needs a string fileKey, a number pasteID and a string dataType");
|
|
85
|
+
}
|
|
86
|
+
return { fileKey, pasteID, dataType };
|
|
87
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How much data a Figma clipboard holds, read before any of it is decoded,
|
|
3
|
+
* so a paste can plan its progress bar up front.
|
|
4
|
+
*/
|
|
5
|
+
export interface FigmaClipboardSize {
|
|
6
|
+
/** Bytes of the Kiwi container (the decoded `(figma)` marker). */
|
|
7
|
+
readonly containerBytes: number;
|
|
8
|
+
/** Bytes of the message chunk as stored, still compressed. */
|
|
9
|
+
readonly messageCompressedBytes: number;
|
|
10
|
+
/** How the message chunk is compressed. */
|
|
11
|
+
readonly messageCompression: "zstd" | "deflate";
|
|
12
|
+
/**
|
|
13
|
+
* Bytes of the message once decompressed, from the zstd frame header's
|
|
14
|
+
* content size; null when the frame does not say (Figma's clipboard always
|
|
15
|
+
* does) or the chunk is deflate, which never says.
|
|
16
|
+
*/
|
|
17
|
+
readonly messageBytes: number | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Reads how large the clipboard's Figma data is without decoding it: it
|
|
21
|
+
* finds the `(figma)` marker and decodes only the few base64 characters
|
|
22
|
+
* that hold the container header, the two chunk lengths and the message's
|
|
23
|
+
* zstd frame header (base64 maps every 4 characters to 3 bytes, so any byte
|
|
24
|
+
* can be decoded on its own). The cost does not grow with the clipboard's
|
|
25
|
+
* size beyond the marker search.
|
|
26
|
+
*
|
|
27
|
+
* Returns null when the HTML has no readable marker or the container is cut
|
|
28
|
+
* short; the full read (`readFigmaClipboardHtml`) then says why. This is
|
|
29
|
+
* only an estimate for planning: it checks no more than it needs.
|
|
30
|
+
*/
|
|
31
|
+
export declare function peekFigmaClipboardSize(html: string): FigmaClipboardSize | null;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { startsWithZstdMagic } from "./container/compression.js";
|
|
2
|
+
/** The container's 8-byte magic and u32 version, then the schema chunk's u32 length. */
|
|
3
|
+
const HEADER_BYTES = 12;
|
|
4
|
+
/** The most bytes a zstd frame header takes: magic, descriptor, window, dictionary id, content size. */
|
|
5
|
+
const ZSTD_FRAME_HEADER_BYTES = 4 + 1 + 1 + 4 + 8;
|
|
6
|
+
/**
|
|
7
|
+
* Reads how large the clipboard's Figma data is without decoding it: it
|
|
8
|
+
* finds the `(figma)` marker and decodes only the few base64 characters
|
|
9
|
+
* that hold the container header, the two chunk lengths and the message's
|
|
10
|
+
* zstd frame header (base64 maps every 4 characters to 3 bytes, so any byte
|
|
11
|
+
* can be decoded on its own). The cost does not grow with the clipboard's
|
|
12
|
+
* size beyond the marker search.
|
|
13
|
+
*
|
|
14
|
+
* Returns null when the HTML has no readable marker or the container is cut
|
|
15
|
+
* short; the full read (`readFigmaClipboardHtml`) then says why. This is
|
|
16
|
+
* only an estimate for planning: it checks no more than it needs.
|
|
17
|
+
*/
|
|
18
|
+
export function peekFigmaClipboardSize(html) {
|
|
19
|
+
const payload = markerPayload(html);
|
|
20
|
+
if (payload === null)
|
|
21
|
+
return null;
|
|
22
|
+
const text = /[\t\n\f\r ]/.test(payload) ? payload.replace(/[\t\n\f\r ]+/g, "") : payload;
|
|
23
|
+
if (text.length % 4 !== 0 || text.length === 0)
|
|
24
|
+
return null;
|
|
25
|
+
const padding = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
|
|
26
|
+
const containerBytes = (text.length / 4) * 3 - padding;
|
|
27
|
+
const bytesAt = (start, count) => {
|
|
28
|
+
if (start < 0 || start + count > containerBytes)
|
|
29
|
+
return null;
|
|
30
|
+
const first = Math.floor(start / 3);
|
|
31
|
+
const last = Math.ceil((start + count) / 3);
|
|
32
|
+
let binary;
|
|
33
|
+
try {
|
|
34
|
+
binary = atob(text.slice(first * 4, last * 4));
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const bytes = new Uint8Array(count);
|
|
40
|
+
for (let index = 0; index < count; index += 1)
|
|
41
|
+
bytes[index] = binary.charCodeAt(start - first * 3 + index);
|
|
42
|
+
return bytes;
|
|
43
|
+
};
|
|
44
|
+
const u32 = (start) => {
|
|
45
|
+
const bytes = bytesAt(start, 4);
|
|
46
|
+
return bytes === null ? null : (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24)) >>> 0;
|
|
47
|
+
};
|
|
48
|
+
const schemaBytes = u32(HEADER_BYTES);
|
|
49
|
+
if (schemaBytes === null)
|
|
50
|
+
return null;
|
|
51
|
+
const messageLengthAt = HEADER_BYTES + 4 + schemaBytes;
|
|
52
|
+
const messageCompressedBytes = u32(messageLengthAt);
|
|
53
|
+
if (messageCompressedBytes === null)
|
|
54
|
+
return null;
|
|
55
|
+
const messageAt = messageLengthAt + 4;
|
|
56
|
+
if (messageCompressedBytes === 0 || messageAt + messageCompressedBytes > containerBytes)
|
|
57
|
+
return null;
|
|
58
|
+
const head = bytesAt(messageAt, Math.min(ZSTD_FRAME_HEADER_BYTES, messageCompressedBytes));
|
|
59
|
+
if (head === null)
|
|
60
|
+
return null;
|
|
61
|
+
if (!startsWithZstdMagic(head)) {
|
|
62
|
+
return { containerBytes, messageCompressedBytes, messageCompression: "deflate", messageBytes: null };
|
|
63
|
+
}
|
|
64
|
+
return { containerBytes, messageCompressedBytes, messageCompression: "zstd", messageBytes: zstdContentSize(head) };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The content size a zstd frame header gives (RFC 8878, section 3.1.1.1),
|
|
68
|
+
* or null when the header leaves it out or is cut short.
|
|
69
|
+
*/
|
|
70
|
+
function zstdContentSize(head) {
|
|
71
|
+
if (head.length < 5)
|
|
72
|
+
return null;
|
|
73
|
+
const descriptor = head[4];
|
|
74
|
+
const contentSizeFlag = descriptor >> 6;
|
|
75
|
+
const singleSegment = (descriptor >> 5) & 1;
|
|
76
|
+
const dictionaryIdBytes = [0, 1, 2, 4][descriptor & 3];
|
|
77
|
+
const contentSizeBytes = [singleSegment ? 1 : 0, 2, 4, 8][contentSizeFlag];
|
|
78
|
+
if (contentSizeBytes === 0)
|
|
79
|
+
return null;
|
|
80
|
+
const at = 5 + (singleSegment ? 0 : 1) + dictionaryIdBytes;
|
|
81
|
+
if (at + contentSizeBytes > head.length)
|
|
82
|
+
return null;
|
|
83
|
+
let size = 0;
|
|
84
|
+
for (let index = contentSizeBytes - 1; index >= 0; index -= 1)
|
|
85
|
+
size = size * 256 + head[at + index];
|
|
86
|
+
// A 2-byte size is stored less 256.
|
|
87
|
+
return contentSizeFlag === 1 ? size + 256 : size;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The text between the first `(figma)` opener inside a comment and the
|
|
91
|
+
* `(/figma)` closer after it, or null when there is none. Unlike the full
|
|
92
|
+
* read, a second marker is not looked for.
|
|
93
|
+
*/
|
|
94
|
+
function markerPayload(html) {
|
|
95
|
+
const opener = "(figma)";
|
|
96
|
+
let at = 0;
|
|
97
|
+
for (;;) {
|
|
98
|
+
const open = html.indexOf(opener, at);
|
|
99
|
+
if (open < 0)
|
|
100
|
+
return null;
|
|
101
|
+
at = open + opener.length;
|
|
102
|
+
if (!html.startsWith("<!--", open - 4) && !html.startsWith("<!--", open - 7))
|
|
103
|
+
continue;
|
|
104
|
+
const close = html.indexOf("(/figma)", at);
|
|
105
|
+
return close < 0 ? null : html.slice(at, close);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The four bytes every zstd frame starts with. */
|
|
2
|
+
export declare const ZSTD_MAGIC: readonly [40, 181, 47, 253];
|
|
3
|
+
export declare function startsWithZstdMagic(bytes: Uint8Array): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Decompresses one chunk: zstd when it starts with the zstd magic, raw
|
|
6
|
+
* deflate otherwise. `offset` is where the chunk starts in the container and
|
|
7
|
+
* is reported with any refusal.
|
|
8
|
+
*/
|
|
9
|
+
export declare function decompressChunk(bytes: Uint8Array, offset: number, maxBytes: number): Uint8Array;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Inflate } from "fflate";
|
|
2
|
+
import { Decompress } from "fzstd";
|
|
3
|
+
import { FigmaReadError } from "./error.js";
|
|
4
|
+
/** The four bytes every zstd frame starts with. */
|
|
5
|
+
export const ZSTD_MAGIC = [0x28, 0xb5, 0x2f, 0xfd];
|
|
6
|
+
/**
|
|
7
|
+
* Input is fed to the decompressors in pieces of this size, so the output of
|
|
8
|
+
* one piece stays small (deflate expands at most about 1032 times) and the
|
|
9
|
+
* size limit is checked often.
|
|
10
|
+
*/
|
|
11
|
+
const INPUT_PIECE_BYTES = 4096;
|
|
12
|
+
export function startsWithZstdMagic(bytes) {
|
|
13
|
+
return ZSTD_MAGIC.every((value, index) => bytes[index] === value);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Decompresses one chunk: zstd when it starts with the zstd magic, raw
|
|
17
|
+
* deflate otherwise. `offset` is where the chunk starts in the container and
|
|
18
|
+
* is reported with any refusal.
|
|
19
|
+
*/
|
|
20
|
+
export function decompressChunk(bytes, offset, maxBytes) {
|
|
21
|
+
return startsWithZstdMagic(bytes) ? unzstd(bytes, offset, maxBytes) : inflateRaw(bytes, offset, maxBytes);
|
|
22
|
+
}
|
|
23
|
+
function inflateRaw(bytes, offset, maxBytes) {
|
|
24
|
+
if (bytes.length === 0) {
|
|
25
|
+
throw new FigmaReadError("inflate-failed", "the chunk is empty, so it is not deflate data", offset);
|
|
26
|
+
}
|
|
27
|
+
const output = new OutputCollector("deflate", offset, maxBytes);
|
|
28
|
+
const inflate = new Inflate((piece) => output.add(piece));
|
|
29
|
+
try {
|
|
30
|
+
feed(bytes, (piece, final) => inflate.push(piece, final));
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error instanceof FigmaReadError)
|
|
34
|
+
throw error;
|
|
35
|
+
throw new FigmaReadError("inflate-failed", `deflate data is invalid: ${messageOf(error)}`, offset);
|
|
36
|
+
}
|
|
37
|
+
return output.join();
|
|
38
|
+
}
|
|
39
|
+
function unzstd(bytes, offset, maxBytes) {
|
|
40
|
+
checkZstdWindows(bytes, offset, maxBytes);
|
|
41
|
+
const output = new OutputCollector("zstd", offset, maxBytes);
|
|
42
|
+
const decompress = new Decompress((piece) => output.add(piece));
|
|
43
|
+
try {
|
|
44
|
+
feed(bytes, (piece, final) => decompress.push(piece, final));
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof FigmaReadError)
|
|
48
|
+
throw error;
|
|
49
|
+
throw new FigmaReadError("zstd-failed", `zstd data is invalid: ${messageOf(error)}`, offset);
|
|
50
|
+
}
|
|
51
|
+
return output.join();
|
|
52
|
+
}
|
|
53
|
+
function feed(bytes, push) {
|
|
54
|
+
for (let start = 0; start < bytes.length; start += INPUT_PIECE_BYTES) {
|
|
55
|
+
const end = Math.min(start + INPUT_PIECE_BYTES, bytes.length);
|
|
56
|
+
push(bytes.subarray(start, end), end === bytes.length);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Copies output pieces into one buffer that doubles as needed but never
|
|
61
|
+
* past `maxBytes`, so output past the limit is refused before it is stored
|
|
62
|
+
* and no second full-size copy is made at the end.
|
|
63
|
+
*/
|
|
64
|
+
class OutputCollector {
|
|
65
|
+
buffer = new Uint8Array(0);
|
|
66
|
+
total = 0;
|
|
67
|
+
format;
|
|
68
|
+
offset;
|
|
69
|
+
maxBytes;
|
|
70
|
+
constructor(format, offset, maxBytes) {
|
|
71
|
+
this.format = format;
|
|
72
|
+
this.offset = offset;
|
|
73
|
+
this.maxBytes = maxBytes;
|
|
74
|
+
}
|
|
75
|
+
add(piece) {
|
|
76
|
+
const total = this.total + piece.length;
|
|
77
|
+
if (total > this.maxBytes) {
|
|
78
|
+
throw new FigmaReadError("too-large", `${this.format} chunk expands to more than the limit of ${this.maxBytes} bytes`, this.offset);
|
|
79
|
+
}
|
|
80
|
+
if (total > this.buffer.length) {
|
|
81
|
+
const grown = new Uint8Array(Math.min(this.maxBytes, Math.max(total, this.buffer.length * 2, 64 * 1024)));
|
|
82
|
+
grown.set(this.buffer.subarray(0, this.total));
|
|
83
|
+
this.buffer = grown;
|
|
84
|
+
}
|
|
85
|
+
this.buffer.set(piece, this.total);
|
|
86
|
+
this.total = total;
|
|
87
|
+
}
|
|
88
|
+
join() {
|
|
89
|
+
return this.buffer.subarray(0, this.total);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Walks the zstd frames and refuses any frame whose window (the memory the
|
|
94
|
+
* decoder sets aside for it) is larger than `maxBytes`, before decoding
|
|
95
|
+
* starts. Also refuses frames that need a dictionary, which Figma files do
|
|
96
|
+
* not carry, and reserved block types. Each step moves forward by at least
|
|
97
|
+
* one byte, so the walk ends.
|
|
98
|
+
*/
|
|
99
|
+
function checkZstdWindows(bytes, chunkOffset, maxBytes) {
|
|
100
|
+
const fail = (problem, at) => {
|
|
101
|
+
throw new FigmaReadError("zstd-failed", problem, chunkOffset + at);
|
|
102
|
+
};
|
|
103
|
+
const need = (at, count, what) => {
|
|
104
|
+
if (at + count > bytes.length)
|
|
105
|
+
fail(`zstd data ends inside ${what}`, at);
|
|
106
|
+
};
|
|
107
|
+
let at = 0;
|
|
108
|
+
while (at < bytes.length) {
|
|
109
|
+
need(at, 4, "a frame magic number");
|
|
110
|
+
const magic = readUint32(bytes, at);
|
|
111
|
+
if ((magic & 0xfffffff0) === 0x184d2a50) {
|
|
112
|
+
need(at, 8, "a skippable frame header");
|
|
113
|
+
const size = readUint32(bytes, at + 4);
|
|
114
|
+
need(at + 8, size, "a skippable frame");
|
|
115
|
+
at += 8 + size;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (magic !== 0xfd2fb528)
|
|
119
|
+
fail(`expected a zstd frame magic number, found 0x${magic.toString(16)}`, at);
|
|
120
|
+
need(at, 5, "a frame header");
|
|
121
|
+
const descriptor = bytes[at + 4];
|
|
122
|
+
const contentSizeFlag = descriptor >> 6;
|
|
123
|
+
const singleSegment = (descriptor >> 5) & 1;
|
|
124
|
+
const hasChecksum = (descriptor >> 2) & 1;
|
|
125
|
+
const dictionaryIdFlag = descriptor & 3;
|
|
126
|
+
if (descriptor & 8)
|
|
127
|
+
fail("zstd frame header sets a reserved bit", at + 4);
|
|
128
|
+
if (dictionaryIdFlag !== 0)
|
|
129
|
+
fail("zstd frame needs a dictionary", at + 4);
|
|
130
|
+
let headerAt = at + 5;
|
|
131
|
+
let window = 0;
|
|
132
|
+
if (!singleSegment) {
|
|
133
|
+
need(headerAt, 1, "a frame header");
|
|
134
|
+
const windowDescriptor = bytes[headerAt];
|
|
135
|
+
const base = 2 ** (10 + (windowDescriptor >> 3));
|
|
136
|
+
window = base + (base / 8) * (windowDescriptor & 7);
|
|
137
|
+
headerAt += 1;
|
|
138
|
+
}
|
|
139
|
+
const contentSizeBytes = [singleSegment ? 1 : 0, 2, 4, 8][contentSizeFlag];
|
|
140
|
+
need(headerAt, contentSizeBytes, "a frame header");
|
|
141
|
+
if (singleSegment) {
|
|
142
|
+
let contentSize = 0;
|
|
143
|
+
for (let index = contentSizeBytes - 1; index >= 0; index -= 1) {
|
|
144
|
+
contentSize = contentSize * 256 + bytes[headerAt + index];
|
|
145
|
+
}
|
|
146
|
+
window = contentSize + (contentSizeFlag === 1 ? 256 : 0);
|
|
147
|
+
}
|
|
148
|
+
if (window > maxBytes) {
|
|
149
|
+
throw new FigmaReadError("too-large", `zstd frame asks for a ${window}-byte window, more than the limit of ${maxBytes} bytes`, chunkOffset + at);
|
|
150
|
+
}
|
|
151
|
+
at = headerAt + contentSizeBytes;
|
|
152
|
+
for (let last = false; !last;) {
|
|
153
|
+
need(at, 3, "a block header");
|
|
154
|
+
const header = bytes[at] | (bytes[at + 1] << 8) | (bytes[at + 2] << 16);
|
|
155
|
+
last = (header & 1) === 1;
|
|
156
|
+
const type = (header >> 1) & 3;
|
|
157
|
+
if (type === 3)
|
|
158
|
+
fail("zstd block has the reserved type 3", at);
|
|
159
|
+
const contentBytes = type === 1 ? 1 : header >>> 3;
|
|
160
|
+
need(at + 3, contentBytes, "a block");
|
|
161
|
+
at += 3 + contentBytes;
|
|
162
|
+
}
|
|
163
|
+
if (hasChecksum) {
|
|
164
|
+
need(at, 4, "a frame checksum");
|
|
165
|
+
at += 4;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function readUint32(bytes, at) {
|
|
170
|
+
return (bytes[at] | (bytes[at + 1] << 8) | (bytes[at + 2] << 16) | (bytes[at + 3] << 24)) >>> 0;
|
|
171
|
+
}
|
|
172
|
+
function messageOf(error) {
|
|
173
|
+
return error instanceof Error ? error.message : String(error);
|
|
174
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Why a Figma clipboard payload, Kiwi container or .fig file was refused. */
|
|
2
|
+
export type FigmaReadProblem = "missing-marker" | "duplicate-marker" | "bad-base64" | "bad-meta" | "truncated-header" | "bad-magic" | "too-few-chunks" | "chunk-past-end" | "inflate-failed" | "zstd-failed" | "too-large" | "not-figma" | "not-design-file" | "bad-zip" | "corrupt-entry" | "too-many-entries" | "entry-too-large" | "file-too-large";
|
|
3
|
+
/**
|
|
4
|
+
* Thrown when a Figma clipboard payload or a Kiwi container is refused.
|
|
5
|
+
* `code` names the kind of refusal. `offset` is the byte in the container
|
|
6
|
+
* where the problem was found, or `null` when the problem is in the HTML or
|
|
7
|
+
* in text around the container. Errors from the Kiwi schema and message
|
|
8
|
+
* decoders are not wrapped: they reach the caller as `KiwiDecodeError`.
|
|
9
|
+
*/
|
|
10
|
+
export declare class FigmaReadError extends Error {
|
|
11
|
+
readonly code: FigmaReadProblem;
|
|
12
|
+
readonly problem: string;
|
|
13
|
+
readonly offset: number | null;
|
|
14
|
+
constructor(code: FigmaReadProblem, problem: string, offset?: number | null);
|
|
15
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a Figma clipboard payload or a Kiwi container is refused.
|
|
3
|
+
* `code` names the kind of refusal. `offset` is the byte in the container
|
|
4
|
+
* where the problem was found, or `null` when the problem is in the HTML or
|
|
5
|
+
* in text around the container. Errors from the Kiwi schema and message
|
|
6
|
+
* decoders are not wrapped: they reach the caller as `KiwiDecodeError`.
|
|
7
|
+
*/
|
|
8
|
+
export class FigmaReadError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
problem;
|
|
11
|
+
offset;
|
|
12
|
+
constructor(code, problem, offset = null) {
|
|
13
|
+
super(`Figma: ${problem}${offset === null ? "" : ` at byte ${offset}`}`);
|
|
14
|
+
this.name = "FigmaReadError";
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.problem = problem;
|
|
17
|
+
this.offset = offset;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type RootArrayItem } from "../kiwi/message.ts";
|
|
2
|
+
import type { KiwiObject, KiwiSchema } from "../kiwi/types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The 8-byte names a Kiwi container may start with: Figma design files and
|
|
5
|
+
* clipboard data, FigJam boards, and Slides decks.
|
|
6
|
+
*/
|
|
7
|
+
export declare const KIWI_CONTAINER_MAGICS: readonly ["fig-kiwi", "fig-jam.", "fig-deck"];
|
|
8
|
+
export type KiwiContainerMagic = (typeof KIWI_CONTAINER_MAGICS)[number];
|
|
9
|
+
/** The type every Figma message is decoded as. */
|
|
10
|
+
export declare const FIGMA_MESSAGE_ROOT_TYPE = "Message";
|
|
11
|
+
export interface KiwiContainerHeader {
|
|
12
|
+
readonly magic: KiwiContainerMagic;
|
|
13
|
+
/** The format version Figma wrote, for example 65 or 101. */
|
|
14
|
+
readonly version: number;
|
|
15
|
+
}
|
|
16
|
+
export interface KiwiContainer {
|
|
17
|
+
readonly header: KiwiContainerHeader;
|
|
18
|
+
/**
|
|
19
|
+
* Every chunk as stored, still compressed: the schema, the message, and
|
|
20
|
+
* any chunks after them (Figma sometimes adds a preview image third).
|
|
21
|
+
*/
|
|
22
|
+
readonly chunks: readonly Uint8Array[];
|
|
23
|
+
readonly schema: KiwiSchema;
|
|
24
|
+
readonly message: KiwiObject;
|
|
25
|
+
}
|
|
26
|
+
export interface KiwiContainerOptions {
|
|
27
|
+
/** Allows a file importer to compact each decoded root array item before reading the next. */
|
|
28
|
+
readonly onRootArrayItem?: RootArrayItem;
|
|
29
|
+
/** Defaults to `FIGMA_READ_LIMITS.maxDecompressedChunkBytes`. */
|
|
30
|
+
readonly maxDecompressedChunkBytes?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Reads a Kiwi container, the binary form Figma uses on the clipboard and
|
|
34
|
+
* inside .fig files: an 8-byte magic name, a little-endian u32 version, then
|
|
35
|
+
* chunks that each start with a little-endian u32 length. The first chunk
|
|
36
|
+
* is the binary schema and the second the message; each is zstd when it
|
|
37
|
+
* starts with the zstd magic and raw deflate otherwise. The message is
|
|
38
|
+
* decoded as `Message`. Decoder errors are thrown as `KiwiDecodeError`,
|
|
39
|
+
* with offsets inside the decompressed chunk.
|
|
40
|
+
*/
|
|
41
|
+
export declare function readKiwiContainer(bytes: Uint8Array, options?: KiwiContainerOptions): KiwiContainer;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { decodeKiwiMessage } from "../kiwi/message.js";
|
|
2
|
+
import { decodeKiwiSchema } from "../kiwi/schema.js";
|
|
3
|
+
import { decompressChunk } from "./compression.js";
|
|
4
|
+
import { FigmaReadError } from "./error.js";
|
|
5
|
+
import { FIGMA_READ_LIMITS } from "./limits.js";
|
|
6
|
+
/**
|
|
7
|
+
* The 8-byte names a Kiwi container may start with: Figma design files and
|
|
8
|
+
* clipboard data, FigJam boards, and Slides decks.
|
|
9
|
+
*/
|
|
10
|
+
export const KIWI_CONTAINER_MAGICS = ["fig-kiwi", "fig-jam.", "fig-deck"];
|
|
11
|
+
/** The type every Figma message is decoded as. */
|
|
12
|
+
export const FIGMA_MESSAGE_ROOT_TYPE = "Message";
|
|
13
|
+
const MAGIC_BYTES = 8;
|
|
14
|
+
const HEADER_BYTES = MAGIC_BYTES + 4;
|
|
15
|
+
/**
|
|
16
|
+
* Reads a Kiwi container, the binary form Figma uses on the clipboard and
|
|
17
|
+
* inside .fig files: an 8-byte magic name, a little-endian u32 version, then
|
|
18
|
+
* chunks that each start with a little-endian u32 length. The first chunk
|
|
19
|
+
* is the binary schema and the second the message; each is zstd when it
|
|
20
|
+
* starts with the zstd magic and raw deflate otherwise. The message is
|
|
21
|
+
* decoded as `Message`. Decoder errors are thrown as `KiwiDecodeError`,
|
|
22
|
+
* with offsets inside the decompressed chunk.
|
|
23
|
+
*/
|
|
24
|
+
export function readKiwiContainer(bytes, options = {}) {
|
|
25
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
26
|
+
throw new TypeError("readKiwiContainer expects a Uint8Array");
|
|
27
|
+
}
|
|
28
|
+
const maxBytes = options.maxDecompressedChunkBytes ?? FIGMA_READ_LIMITS.maxDecompressedChunkBytes;
|
|
29
|
+
if (bytes.length < HEADER_BYTES) {
|
|
30
|
+
throw new FigmaReadError("truncated-header", `the container has ${bytes.length} bytes, fewer than the ${HEADER_BYTES}-byte header`, 0);
|
|
31
|
+
}
|
|
32
|
+
const magicText = String.fromCharCode(...bytes.subarray(0, MAGIC_BYTES));
|
|
33
|
+
const magic = KIWI_CONTAINER_MAGICS.find((candidate) => candidate === magicText);
|
|
34
|
+
if (magic === undefined) {
|
|
35
|
+
throw new FigmaReadError("bad-magic", `the container starts with ${JSON.stringify(magicText)}, not a Figma name`, 0);
|
|
36
|
+
}
|
|
37
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
38
|
+
const version = view.getUint32(MAGIC_BYTES, true);
|
|
39
|
+
const chunks = [];
|
|
40
|
+
const chunkOffsets = [];
|
|
41
|
+
let at = HEADER_BYTES;
|
|
42
|
+
while (at < bytes.length) {
|
|
43
|
+
if (bytes.length - at < 4) {
|
|
44
|
+
throw new FigmaReadError("chunk-past-end", `${bytes.length - at} bytes remain, too few for a chunk length`, at);
|
|
45
|
+
}
|
|
46
|
+
const length = view.getUint32(at, true);
|
|
47
|
+
const start = at + 4;
|
|
48
|
+
if (length > bytes.length - start) {
|
|
49
|
+
throw new FigmaReadError("chunk-past-end", `chunk ${chunks.length} is ${length} bytes but only ${bytes.length - start} remain`, at);
|
|
50
|
+
}
|
|
51
|
+
chunks.push(bytes.subarray(start, start + length));
|
|
52
|
+
chunkOffsets.push(start);
|
|
53
|
+
at = start + length;
|
|
54
|
+
}
|
|
55
|
+
if (chunks.length < 2) {
|
|
56
|
+
throw new FigmaReadError("too-few-chunks", `the container has ${chunks.length} chunk${chunks.length === 1 ? "" : "s"}, but needs a schema and a message`, at);
|
|
57
|
+
}
|
|
58
|
+
const schema = decodeKiwiSchema(decompressChunk(chunks[0], chunkOffsets[0], maxBytes));
|
|
59
|
+
const message = decodeKiwiMessage(schema, FIGMA_MESSAGE_ROOT_TYPE, decompressChunk(chunks[1], chunkOffsets[1], maxBytes), options.onRootArrayItem);
|
|
60
|
+
return { header: { magic, version }, chunks, schema, message };
|
|
61
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Bounds applied while reading a Figma clipboard payload, Kiwi container or .fig file. */
|
|
2
|
+
export declare const FIGMA_READ_LIMITS: {
|
|
3
|
+
/**
|
|
4
|
+
* The most bytes one chunk may expand to when it is decompressed. Deflate
|
|
5
|
+
* can expand about 1000 times and zstd far more, so without a limit a
|
|
6
|
+
* small chunk could fill memory. A zstd frame that asks for a larger
|
|
7
|
+
* window than this is refused before any memory is set aside for it.
|
|
8
|
+
*/
|
|
9
|
+
readonly maxDecompressedChunkBytes: number;
|
|
10
|
+
/** The most entries a zipped .fig file may hold: canvas.fig, meta.json, thumbnail.png and the images. */
|
|
11
|
+
readonly maxFileEntries: 10000;
|
|
12
|
+
/** The most bytes one entry of a zipped .fig file may hold once inflated. */
|
|
13
|
+
readonly maxFileEntryBytes: number;
|
|
14
|
+
/** The most bytes all entries of a zipped .fig file may hold together once inflated. */
|
|
15
|
+
readonly maxFileTotalBytes: number;
|
|
16
|
+
/**
|
|
17
|
+
* The same limit for a clipboard paste, used by `readFigmaClipboardHtml`.
|
|
18
|
+
* The largest real paste message seen decompressed to a few MiB, so 64 MiB
|
|
19
|
+
* leaves room for big copies while a crafted paste cannot use much memory.
|
|
20
|
+
*/
|
|
21
|
+
readonly maxPasteChunkBytes: number;
|
|
22
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Bounds applied while reading a Figma clipboard payload, Kiwi container or .fig file. */
|
|
2
|
+
export const FIGMA_READ_LIMITS = {
|
|
3
|
+
/**
|
|
4
|
+
* The most bytes one chunk may expand to when it is decompressed. Deflate
|
|
5
|
+
* can expand about 1000 times and zstd far more, so without a limit a
|
|
6
|
+
* small chunk could fill memory. A zstd frame that asks for a larger
|
|
7
|
+
* window than this is refused before any memory is set aside for it.
|
|
8
|
+
*/
|
|
9
|
+
maxDecompressedChunkBytes: 256 * 1024 * 1024,
|
|
10
|
+
/** The most entries a zipped .fig file may hold: canvas.fig, meta.json, thumbnail.png and the images. */
|
|
11
|
+
maxFileEntries: 10_000,
|
|
12
|
+
/** The most bytes one entry of a zipped .fig file may hold once inflated. */
|
|
13
|
+
maxFileEntryBytes: 256 * 1024 * 1024,
|
|
14
|
+
/** The most bytes all entries of a zipped .fig file may hold together once inflated. */
|
|
15
|
+
maxFileTotalBytes: 1024 * 1024 * 1024,
|
|
16
|
+
/**
|
|
17
|
+
* The same limit for a clipboard paste, used by `readFigmaClipboardHtml`.
|
|
18
|
+
* The largest real paste message seen decompressed to a few MiB, so 64 MiB
|
|
19
|
+
* leaves room for big copies while a crafted paste cannot use much memory.
|
|
20
|
+
*/
|
|
21
|
+
maxPasteChunkBytes: 64 * 1024 * 1024,
|
|
22
|
+
};
|