ZIP aggregated multiple requests


#1

Is there a way how to return aggregated multiple requests as a single zip file?


#2

Hi @jiri, in order to literally return a zip file, you would need a ZIP archive creator implemented in pure JavaScript – browserifying jszip might do the trick, but performance will of course not be as good as a native implementation.

However, if all you need is to compress large aggregated responses, you can apply a gzip Content-Encoding by adding the appropriate header to the Response object. We could modify the Aggregating Multiple Requests example like so:

  // ...

  const responseInit = {
    headers: {
      'Content-Type': 'application/json',
      'Content-Encoding': 'gzip'  // <-- ADDED
    }
  }
  return new Response(JSON.stringify(combined), responseInit)
}

If your subrequest responses are huge, it would be better to use the TransformStream API to concatenate the responses, but the principle is the same: add the Content-Encoding: gzip header, and your response will be gzipped.

The trouble with that is that most HTTP clients will automatically un-gzip such responses, so the compression would only exist during transport – the client wouldn’t see it as a gzip file.

Could you tell us a bit more about your use case? We may be able to come up with other options.


#3

Hi @harris, thank you for your reply. I thought the first option ‘browserifying jszip’. I can imagine the performance issues, so probably script would exceed the time limit, especially by the large files. In other words the Workers are probably not intended for this kind of work.