Coda Pack - create ZIP File

Hi!

I’ve tried multiple ZIP-libraries to download a set of attachments from a table as a ZIP file:

  • jszip
  • js.zip
  • pavo (not for zip)
  • archiever
  • yazl

None of them worked.
Either they have dependencies to web stuff (e.g. streams api), or setTimeout doesn’t work with the fake option (–timerStrategy fake).
TemporaryBlobStorage doesn’t support multiple files.

Is there any option, within a coda pack to create a zip file?

Thanks!

1 Like

Haha I tried the same thing, no luck either. The next step for me would have been to create an endpoint with a proper node environment to do the work

The issue is their custom sandboxed environment I think: Using libraries - Coda Pack SDK

Here’s my result:

Name Issue
archiver Uses setTimeout - Shim doesn’t fix
yazl Uses setTimeout - Shim doesn’t fix
zlib Uses setTimeout - Shim doesn’t fix
jszip Uses eval()
client-zip Cannot find File or Blob
adm-zip Cannot create dep graph
1 Like

Thanks for sharing this :smile:
Was so hyped how easy you can add “cloud functions” in coda, but yea… :joy:

1 Like

Using NPM libraries in Packs can be a bit of an emotional roller coaster. I hacked around a bit last night and I was able to get jszip working in a Pack:

import * as coda from "@codahq/packs-sdk";
import JSZip from "jszip"

export const pack = coda.newPack();

pack.addFormula({
  name: "Test",
  description: "",
  parameters: [],
  resultType: coda.ValueType.String,
  codaType: coda.ValueHintType.Url,
  execute: async function (args, context) {
    var zip = new JSZip();
    zip.file("hello.txt", "Hello World!");
    let b64 = await zip.generateAsync({type:"base64"});
    let buffer = Buffer.from(b64, 'base64');
    let tempUrl = context.temporaryBlobStorage.storeBlob(buffer, "application/zip");
    return tempUrl;
  },
});

As @Rickard_Abraham noted it initially failed to upload due to a dynamic code evaluation error. Something I’ve been experimenting with in other Packs, and that worked here, is manually removing code from dependencies that isn’t compatible with Packs.

In this case the sub-dependency setimmediate included a call to new Function(), which was triggering the dynamic code evaluation error. My guess, which has so far held to be true, is that this particular code path isn’t needed by my Pack, and that it could be safely removed. You can manually alter the file in the node_modules directory to test it out. For a more permanent solution, use the tool patch-package to create a patch that can be automatically applied.

That work, in combination with a timer shim, allowed the Pack to upload and function correctly. Let me know if this works for you as well.

5 Likes

I just added a TemporaryZipFile() formula to my Temp File Pack that uses this library to do some basic zip file creation:

You can view the full source code here:

4 Likes

Hi Eric,

thanks so much - that did it! :100:
Love your repository. I’ll visit it more often in the future for sure :joy:

1 Like

I just saw, you included the patch as well.
What a legend, thanks! :racing_car: :wind_face:

1 Like

This is legendary Eric ! I’ve immediately implemented this to my most valued docs creating a button to backup everything as csv+zip (tried to include the temporary csv file into the zip but couldn’t)

It’s always great to read from you :slight_smile:

Glad you like it! I just fixed the bug with including temporary files in the temporary zip file, so that should be working now.

3 Likes