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.