gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.38k stars 510 forks source link

Cannot send ZipWritter content to Response #406

Closed sntran closed 1 year ago

sntran commented 1 year ago

Using the Hello World with Streams example, I try to write a server that responds with zip file, but it does not work.

The minimal code for such server is below:

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

import {
  ZipWriter,
} from "https://deno.land/x/zipjs@v2.6.79/index.js";

serve(async (req: Request) => {
  // Creates a TransformStream object, the zip content will be written in the
  // `writable` property. The `readable` property is responded to client.
  const { readable, writable } = new TransformStream();
  const zipWriter = new ZipWriter(writable);
  const entry = await zipWriter.add("hello.txt", new Blob(["Hello world!"]).stream());
  console.log(entry);
  await zipWriter.close();

  const headers = new Headers();
  headers.set("Content-Disposition", `attachment; filename="hello.zip"`);

  return new Response(readable, {
    // headers,
  });
});

You can also see it in action on Deno Deploy Playground: https://dash.deno.com/playground/zipjs

The console.log(entry) is never called. The whole process just hangs.

gildas-lormeau commented 1 year ago

This is due to the usage of await. Here's an example showing how to make it work: https://dash.deno.com/playground/bitter-kingfisher-23