Touffy / client-zip

A client-side streaming ZIP generator
MIT License
349 stars 22 forks source link

[QUESTION] Lazyfetch array with objects instead of urls #60

Closed StavrosHacker closed 1 year ago

StavrosHacker commented 1 year ago

I've been using client-zip on deno to zip-on-the-fly some files on my origin server using the following code:

async function *lazyFetch() {
      for (const url of urls) yeild await fetch(url)
}

However with this way I cannot put files in different folders.

I would like to be able to have an array with an object for each file, for example:

let files = [
    {
        name: "folder1/file1.txt",
        size: 123,
        input: "https://file-download-url.com/file"
    }
]

and lazyfetch it. Is this doable?

Thank you.

Touffy commented 1 year ago

Absolutely. We get a Response as usual, and instead of yielding it by itself, we put it in an object with its desired filename:

async function *lazyFetch() {
  for (const { name, input } of files) yield { name, input: await fetch(input) }
}

I didn't copy the 'size' property because it doesn't do anything when actually making the Zip. You can pass your original files array in the metadata option, so that downloadZip will set the correct Content-Length header (assuming your sizes are accurate, of course) :

const zipResponse = downloadZip(lazyFetch(), { metadata: files })