@moondraft/import 0.0.0-stage → 0.1.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.
Files changed (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -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,38 @@
1
+ import type { KiwiObject, KiwiSchema } from "../kiwi/types.ts";
2
+ /**
3
+ * The 8-byte names a Kiwi container may start with: Figma design files and
4
+ * clipboard data, FigJam boards, and Slides decks.
5
+ */
6
+ export declare const KIWI_CONTAINER_MAGICS: readonly ["fig-kiwi", "fig-jam.", "fig-deck"];
7
+ export type KiwiContainerMagic = (typeof KIWI_CONTAINER_MAGICS)[number];
8
+ /** The type every Figma message is decoded as. */
9
+ export declare const FIGMA_MESSAGE_ROOT_TYPE = "Message";
10
+ export interface KiwiContainerHeader {
11
+ readonly magic: KiwiContainerMagic;
12
+ /** The format version Figma wrote, for example 65 or 101. */
13
+ readonly version: number;
14
+ }
15
+ export interface KiwiContainer {
16
+ readonly header: KiwiContainerHeader;
17
+ /**
18
+ * Every chunk as stored, still compressed: the schema, the message, and
19
+ * any chunks after them (Figma sometimes adds a preview image third).
20
+ */
21
+ readonly chunks: readonly Uint8Array[];
22
+ readonly schema: KiwiSchema;
23
+ readonly message: KiwiObject;
24
+ }
25
+ export interface KiwiContainerOptions {
26
+ /** Defaults to `FIGMA_READ_LIMITS.maxDecompressedChunkBytes`. */
27
+ readonly maxDecompressedChunkBytes?: number;
28
+ }
29
+ /**
30
+ * Reads a Kiwi container, the binary form Figma uses on the clipboard and
31
+ * inside .fig files: an 8-byte magic name, a little-endian u32 version, then
32
+ * chunks that each start with a little-endian u32 length. The first chunk
33
+ * is the binary schema and the second the message; each is zstd when it
34
+ * starts with the zstd magic and raw deflate otherwise. The message is
35
+ * decoded as `Message`. Decoder errors are thrown as `KiwiDecodeError`,
36
+ * with offsets inside the decompressed chunk.
37
+ */
38
+ 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));
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
+ };
@@ -0,0 +1,14 @@
1
+ export interface ZipLimits {
2
+ readonly maxEntries: number;
3
+ readonly maxEntryBytes: number;
4
+ readonly maxTotalBytes: number;
5
+ }
6
+ /**
7
+ * Unzips in two steps. The first reads the central directory (APPNOTE 4.3.12
8
+ * to 4.3.16, with the ZIP64 records) and checks the entry count and the
9
+ * declared sizes against the limits, before anything is inflated. The
10
+ * second inflates each entry in small steps and counts the bytes it really
11
+ * produces, so an entry whose data inflates to more than its declared size
12
+ * is refused instead of being inflated in full.
13
+ */
14
+ export declare function unzipWithLimits(bytes: Uint8Array, limits: ZipLimits): Map<string, Uint8Array>;
@@ -0,0 +1,154 @@
1
+ import { Inflate } from "fflate";
2
+ import { FigmaReadError } from "./error.js";
3
+ const END_OF_DIRECTORY = 0x06054b50;
4
+ const ZIP64_LOCATOR = 0x07064b50;
5
+ const ZIP64_END_OF_DIRECTORY = 0x06064b50;
6
+ const DIRECTORY_HEADER = 0x02014b50;
7
+ const LOCAL_HEADER = 0x04034b50;
8
+ const STORED = 0;
9
+ const DEFLATED = 8;
10
+ /**
11
+ * Compressed bytes given to the inflater at a time. DEFLATE expands at most
12
+ * about 1032 times, so one step yields at most about 8 MiB beyond the
13
+ * declared size before the count stops it.
14
+ */
15
+ const INFLATE_STEP = 8 * 1024;
16
+ /**
17
+ * Unzips in two steps. The first reads the central directory (APPNOTE 4.3.12
18
+ * to 4.3.16, with the ZIP64 records) and checks the entry count and the
19
+ * declared sizes against the limits, before anything is inflated. The
20
+ * second inflates each entry in small steps and counts the bytes it really
21
+ * produces, so an entry whose data inflates to more than its declared size
22
+ * is refused instead of being inflated in full.
23
+ */
24
+ export function unzipWithLimits(bytes, limits) {
25
+ const entries = readDirectory(bytes, limits);
26
+ const files = new Map();
27
+ for (const entry of entries) {
28
+ files.set(entry.name, readEntry(bytes, entry));
29
+ }
30
+ return files;
31
+ }
32
+ function readDirectory(bytes, limits) {
33
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
34
+ const u16 = (at) => view.getUint16(at, true);
35
+ const u32 = (at) => view.getUint32(at, true);
36
+ const u64 = (at) => u32(at) + u32(at + 4) * 2 ** 32;
37
+ const fail = (reason) => {
38
+ throw new FigmaReadError("bad-zip", `the ZIP directory is unreadable: ${reason}`);
39
+ };
40
+ const inside = (at, length) => {
41
+ if (at < 0 || at + length > bytes.length)
42
+ fail("a record points past the end of the file");
43
+ };
44
+ // The end record is 22 bytes followed by a comment of at most 65535 bytes.
45
+ let end = bytes.length - 22;
46
+ const lowest = Math.max(0, end - 0xffff);
47
+ while (end >= lowest && !(end >= 0 && u32(end) === END_OF_DIRECTORY))
48
+ end -= 1;
49
+ if (end < lowest || end < 0)
50
+ fail("no end of central directory record");
51
+ let count = u16(end + 10);
52
+ let offset = u32(end + 16);
53
+ const zip64 = end >= 20 && u32(end - 20) === ZIP64_LOCATOR;
54
+ if (zip64) {
55
+ const record = u64(end - 12);
56
+ inside(record, 56);
57
+ if (u32(record) !== ZIP64_END_OF_DIRECTORY)
58
+ fail("the ZIP64 end record is missing");
59
+ count = u64(record + 32);
60
+ offset = u64(record + 48);
61
+ }
62
+ if (count > limits.maxEntries) {
63
+ throw new FigmaReadError("too-many-entries", `the ZIP has more than maxFileEntries (${limits.maxEntries}) entries`);
64
+ }
65
+ const entries = [];
66
+ let total = 0;
67
+ for (let index = 0; index < count; index += 1) {
68
+ inside(offset, 46);
69
+ if (u32(offset) !== DIRECTORY_HEADER)
70
+ fail(`entry ${index} has no central directory header`);
71
+ const flags = u16(offset + 8);
72
+ const method = u16(offset + 10);
73
+ let compressedSize = u32(offset + 20);
74
+ let size = u32(offset + 24);
75
+ const nameLength = u16(offset + 28);
76
+ const extraLength = u16(offset + 30);
77
+ const commentLength = u16(offset + 32);
78
+ let localHeader = u32(offset + 42);
79
+ const nameStart = offset + 46;
80
+ inside(nameStart, nameLength + extraLength);
81
+ const nameBytes = bytes.subarray(nameStart, nameStart + nameLength);
82
+ // Bit 11 marks a UTF-8 name; other names are read byte by byte, as fflate does.
83
+ const name = flags & 0x800 ? new TextDecoder().decode(nameBytes) : String.fromCharCode(...nameBytes);
84
+ // APPNOTE 4.5.3: the ZIP64 extra field holds only the fields whose
85
+ // 32-bit value is 0xFFFFFFFF, in this order.
86
+ if (zip64 && (size === 0xffffffff || compressedSize === 0xffffffff || localHeader === 0xffffffff)) {
87
+ const extraEnd = nameStart + nameLength + extraLength;
88
+ let field = nameStart + nameLength;
89
+ while (field + 4 <= extraEnd && u16(field) !== 1)
90
+ field += 4 + u16(field + 2);
91
+ if (field + 4 > extraEnd)
92
+ fail(`entry ${JSON.stringify(name)} has no ZIP64 sizes`);
93
+ let at = field + 4;
94
+ if (size === 0xffffffff)
95
+ (size = u64(at)), (at += 8);
96
+ if (compressedSize === 0xffffffff)
97
+ (compressedSize = u64(at)), (at += 8);
98
+ if (localHeader === 0xffffffff)
99
+ localHeader = u64(at);
100
+ }
101
+ const largest = Math.max(size, compressedSize);
102
+ if (largest > limits.maxEntryBytes) {
103
+ throw new FigmaReadError("entry-too-large", `the ZIP entry ${JSON.stringify(name)} is ${largest} bytes, more than maxFileEntryBytes (${limits.maxEntryBytes})`);
104
+ }
105
+ total += largest;
106
+ if (total > limits.maxTotalBytes) {
107
+ throw new FigmaReadError("file-too-large", `the ZIP entries add up to more than maxFileTotalBytes (${limits.maxTotalBytes}) bytes`);
108
+ }
109
+ entries.push({ name, method, compressedSize, size, localHeader });
110
+ offset = nameStart + nameLength + extraLength + commentLength;
111
+ }
112
+ return entries;
113
+ }
114
+ function readEntry(bytes, entry) {
115
+ const corrupt = (reason) => {
116
+ throw new FigmaReadError("corrupt-entry", `the ZIP entry ${JSON.stringify(entry.name)} is corrupt: ${reason}`);
117
+ };
118
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
119
+ const header = entry.localHeader;
120
+ if (header + 30 > bytes.length || view.getUint32(header, true) !== LOCAL_HEADER)
121
+ corrupt("no local header");
122
+ const start = header + 30 + view.getUint16(header + 26, true) + view.getUint16(header + 28, true);
123
+ if (start + entry.compressedSize > bytes.length)
124
+ corrupt("its data runs past the end of the file");
125
+ const data = bytes.subarray(start, start + entry.compressedSize);
126
+ if (entry.method === STORED) {
127
+ if (entry.compressedSize !== entry.size)
128
+ corrupt("a stored entry's two sizes differ");
129
+ return data.slice();
130
+ }
131
+ if (entry.method !== DEFLATED)
132
+ corrupt(`compression method ${entry.method} is not supported`);
133
+ const out = new Uint8Array(entry.size);
134
+ let written = 0;
135
+ const inflater = new Inflate((chunk) => {
136
+ if (written + chunk.length > entry.size)
137
+ corrupt(`it inflates to more than its declared ${entry.size} bytes`);
138
+ out.set(chunk, written);
139
+ written += chunk.length;
140
+ });
141
+ try {
142
+ for (let at = 0; at < data.length || at === 0; at += INFLATE_STEP) {
143
+ inflater.push(data.subarray(at, at + INFLATE_STEP), at + INFLATE_STEP >= data.length);
144
+ }
145
+ }
146
+ catch (error) {
147
+ if (error instanceof FigmaReadError)
148
+ throw error;
149
+ corrupt(error instanceof Error ? error.message : String(error));
150
+ }
151
+ if (written !== entry.size)
152
+ corrupt(`it inflates to ${written} bytes, not its declared ${entry.size}`);
153
+ return out;
154
+ }
@@ -0,0 +1,34 @@
1
+ import { type KiwiContainerHeader, type KiwiContainerOptions } from "./container/kiwiContainer.ts";
2
+ import type { KiwiObject, KiwiSchema } from "./kiwi/types.ts";
3
+ export interface FigFileOptions extends KiwiContainerOptions {
4
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileEntries`. */
5
+ readonly maxFileEntries?: number;
6
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileEntryBytes`. */
7
+ readonly maxFileEntryBytes?: number;
8
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileTotalBytes`. */
9
+ readonly maxFileTotalBytes?: number;
10
+ }
11
+ export interface FigFile {
12
+ /** The parsed `meta.json`, or `null` when the file has none (older files are a bare container). */
13
+ readonly meta: Readonly<Record<string, unknown>> | null;
14
+ readonly header: KiwiContainerHeader;
15
+ readonly schema: KiwiSchema;
16
+ readonly message: KiwiObject;
17
+ /** Image bytes by the name under `images/`, which is the hex SHA-1 a paint's `image.hash` holds. */
18
+ readonly images: ReadonlyMap<string, Uint8Array>;
19
+ /**
20
+ * The `thumbnail.png` bytes, when the file has one. A bare container's
21
+ * third chunk is used instead when it holds a PNG.
22
+ */
23
+ readonly thumbnail?: Uint8Array;
24
+ }
25
+ /**
26
+ * Reads a saved Figma design file. A current .fig file is a ZIP holding
27
+ * `canvas.fig` (a Kiwi container, the same form the clipboard uses),
28
+ * `meta.json`, `thumbnail.png` and `images/<sha1 hex>`; an older one is a
29
+ * bare Kiwi container. ZIP entries are stored or deflated. Every entry's
30
+ * declared size is checked against the limits before anything is inflated,
31
+ * and an entry that inflates past its declared size is refused. FigJam
32
+ * boards and Slides decks are refused with `not-design-file`.
33
+ */
34
+ export declare function readFigFile(bytes: Uint8Array, options?: FigFileOptions): FigFile;
@@ -0,0 +1,91 @@
1
+ import { FigmaReadError } from "./container/error.js";
2
+ import { KIWI_CONTAINER_MAGICS, readKiwiContainer, } from "./container/kiwiContainer.js";
3
+ import { FIGMA_READ_LIMITS } from "./container/limits.js";
4
+ import { unzipWithLimits } from "./container/zip.js";
5
+ const CANVAS_ENTRY = "canvas.fig";
6
+ const META_ENTRY = "meta.json";
7
+ const THUMBNAIL_ENTRY = "thumbnail.png";
8
+ const IMAGES_FOLDER = "images/";
9
+ const ZIP_LOCAL_HEADER = [0x50, 0x4b, 0x03, 0x04];
10
+ const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
11
+ const DESIGN_MAGIC = "fig-kiwi";
12
+ /**
13
+ * Reads a saved Figma design file. A current .fig file is a ZIP holding
14
+ * `canvas.fig` (a Kiwi container, the same form the clipboard uses),
15
+ * `meta.json`, `thumbnail.png` and `images/<sha1 hex>`; an older one is a
16
+ * bare Kiwi container. ZIP entries are stored or deflated. Every entry's
17
+ * declared size is checked against the limits before anything is inflated,
18
+ * and an entry that inflates past its declared size is refused. FigJam
19
+ * boards and Slides decks are refused with `not-design-file`.
20
+ */
21
+ export function readFigFile(bytes, options = {}) {
22
+ if (!(bytes instanceof Uint8Array)) {
23
+ throw new TypeError("readFigFile expects a Uint8Array");
24
+ }
25
+ if (!startsWith(bytes, ZIP_LOCAL_HEADER)) {
26
+ if (!isKiwiMagic(magicOf(bytes))) {
27
+ throw new FigmaReadError("not-figma", "the file is neither a ZIP nor a Figma container", 0);
28
+ }
29
+ const { preview, ...canvas } = readCanvas(bytes, "the file", options);
30
+ return { ...canvas, meta: null, images: new Map(), ...(preview === undefined ? {} : { thumbnail: preview }) };
31
+ }
32
+ const entries = unzipWithLimits(bytes, {
33
+ maxEntries: options.maxFileEntries ?? FIGMA_READ_LIMITS.maxFileEntries,
34
+ maxEntryBytes: options.maxFileEntryBytes ?? FIGMA_READ_LIMITS.maxFileEntryBytes,
35
+ maxTotalBytes: options.maxFileTotalBytes ?? FIGMA_READ_LIMITS.maxFileTotalBytes,
36
+ });
37
+ const canvas = entries.get(CANVAS_ENTRY);
38
+ if (canvas === undefined) {
39
+ throw new FigmaReadError("not-figma", `the ZIP has no ${CANVAS_ENTRY} entry, so it is not a Figma file`);
40
+ }
41
+ const images = new Map();
42
+ for (const [name, data] of entries) {
43
+ if (name.startsWith(IMAGES_FOLDER) && name.length > IMAGES_FOLDER.length && !name.endsWith("/")) {
44
+ images.set(name.slice(IMAGES_FOLDER.length), data);
45
+ }
46
+ }
47
+ const metaBytes = entries.get(META_ENTRY);
48
+ const thumbnail = entries.get(THUMBNAIL_ENTRY);
49
+ const { header, schema, message } = readCanvas(canvas, CANVAS_ENTRY, options);
50
+ return {
51
+ header,
52
+ schema,
53
+ message,
54
+ meta: metaBytes === undefined ? null : readMeta(metaBytes),
55
+ images,
56
+ ...(thumbnail === undefined ? {} : { thumbnail }),
57
+ };
58
+ }
59
+ function readCanvas(bytes, where, options) {
60
+ const magic = magicOf(bytes);
61
+ if (magic !== DESIGN_MAGIC && isKiwiMagic(magic)) {
62
+ const kind = magic === "fig-jam." ? "a FigJam board" : "a Slides deck";
63
+ throw new FigmaReadError("not-design-file", `${where} is ${kind}; only Figma design files are read`, 0);
64
+ }
65
+ const { header, schema, message, chunks } = readKiwiContainer(bytes, options);
66
+ const third = chunks[2];
67
+ // Figma sometimes adds a preview image as the third chunk; only a PNG is taken as one.
68
+ const preview = third !== undefined && startsWith(third, PNG_SIGNATURE) ? third : undefined;
69
+ return { header, schema, message, preview };
70
+ }
71
+ function readMeta(bytes) {
72
+ let value;
73
+ try {
74
+ value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
75
+ }
76
+ catch (error) {
77
+ throw new FigmaReadError("bad-meta", `${META_ENTRY} is not UTF-8 JSON: ${messageOf(error)}`);
78
+ }
79
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
80
+ throw new FigmaReadError("bad-meta", `${META_ENTRY} is not a JSON object`);
81
+ }
82
+ return value;
83
+ }
84
+ const magicOf = (bytes) => String.fromCharCode(...bytes.subarray(0, 8));
85
+ const isKiwiMagic = (magic) => KIWI_CONTAINER_MAGICS.includes(magic);
86
+ function startsWith(bytes, prefix) {
87
+ return prefix.every((byte, index) => bytes[index] === byte);
88
+ }
89
+ function messageOf(error) {
90
+ return error instanceof Error ? error.message : String(error);
91
+ }