gildas-lormeau/zip.js

`Bun` support

ghoullier opened this issue · 4 comments

Purpose

The example below does not works with bun.

import { BlobWriter, TextReader, ZipWriter } from "@zip.js/zip.js";

const zipFileWriter = new BlobWriter();
const helloWorldReader = new TextReader("Hello world!");

const zipWriter = new ZipWriter(zipFileWriter);
await zipWriter.add("hello.txt", helloWorldReader);
await zipWriter.close();

It raises the following exception:

227 |   if (classicWorkersSupported) {
228 |     try {
229 |       worker = new Worker(scriptUrl);
230 |     } catch (_error) {
231 |       classicWorkersSupported = false;
232 |       worker = new Worker(scriptUrl, workerOptions);
                        ^
TypeError: BuildMessage: ModuleNotFound resolving "/Users/gregory.houllier/Playground/play-bun/node_modules/@zip.js/zip.js/lib/undefined" (entry point)
      at getWebWorker (~/play-bun/node_modules/@zip.js/zip.js/lib/core/codec-worker.js:232:16)
      at createWebWorkerInterface (~/play-bun/node_modules/@zip.js/zip.js/lib/core/codec-worker.js:124:15)
      at new CodecWorker (~/play-bun/node_modules/@zip.js/zip.js/lib/core/codec-worker.js:74:6)
      at ~/play-bun/node_modules/@zip.js/zip.js/lib/core/codec-pool.js:75:14
      at runWorker (~/play-bun/node_modules/@zip.js/zip.js/lib/core/codec-pool.js:56:26)
      at ~/play-bun/node_modules/@zip.js/zip.js/lib/core/zip-writer.js:573:24

The problem seems to came here, the url() call return undefined.

Context

  • Environment: Darwin 23.0.0 arm64 arm
  • bun version: 1.0.25

Hi @ghoullier!

First of all, I'd like to take this opportunity to wish you a happy new year :)

Thanks for reporting the bug, I've reproduced it and fixed it. The next version should be compatible with Bun out of the box. However, it looks like Bun (still) doesn't support Blob URIs (returned by URL.createObjectURL()) or data URIs with workers. The CompressionStream API is missing (but polyfilled) and the crypto API is also buggy.

As a consequence, in order to use workers in Bun you have to call configure and pass the path to z-worker.js. This file must also be in your public folder. See below.

import { configure } from "@zip.js/zip.js";

configure({
  workerScripts: {
    deflate: ["path/to/z-worker.js"],
    inflate: ["path/to/z-worker.js"]
  }
});

The new version 2.7.33 which should fix this issue is available for download.

Happy new year too gildas 😀

2.7.33 fixed the issue! Thanks!

You're welcome!