yagudaev / figma-to-json

💾 Read/Write Figma Files as JSON
https://figma2json.com
MIT License
199 stars 27 forks source link

Support reading large files #20

Open yagudaev opened 8 months ago

yagudaev commented 8 months ago

Many of the community files are large .fig files. For example take this file: https://www.figma.com/community/file/1325597018063319916/free-dark-admin-dashboards, it converts to a ~125MB file.

This results in a stack-overflow when we try to process it with the btoa method.

function convertBlobsToBase64(json: any): object {
  if (!json.blobs) return json

  return {
    ...json,
    blobs: json.blobs.map((blob: any) => {
      return btoa(String.fromCharCode(...blob.bytes))
    })
  }
}

There are more efficient ways to convert a blob to a Base64 we should use.

zhiyuang commented 7 months ago

Actually I'm curious why we need to convert to Base64. What is the data meaning in blob.bytes?

yagudaev commented 7 months ago

I've been trying to figure it out. The reason for converting it to a string is that it is a UInt8Array and if it is not converted it will be serialized as { "0": 233, "1": 111, ... }

But we don't do it for image fills yet, if you look there is a hash that points to the image (.fig is a zip and it will reference the image in disk in case of image fills)

If I remember correctly there is a faster way to convert a UInt8Array to Base64 string. Switching to that method should fix this