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

Not working in Node.js 16.13.0 #376

Closed hornta closed 1 year ago

hornta commented 1 year ago

I'm getting this error when running this package in Node.js 16.13.2

ReferenceError: TransformStream is not defined

Version: 2.6.50

It looks like TransformStream was introduced in version 16.5.0 so I expect it should work. This is my code:

import { BlobReader, ZipReader } from "@zip.js/zip.js";

export const loadXlsx = async (data: Blob) => {
  const zipFileReader = new BlobReader(data);
  const zipReader = new ZipReader(zipFileReader);
  const entries = await zipReader.getEntries()
  for(const entry of entries) {
    console.log({ entry: entry.filename, directory: entry.directory })
  }
  await zipReader.close();
}
hornta commented 1 year ago

Closing. I tested version 18 of node and it works so I guess the MDN documentation was lying to me: image

gildas-lormeau commented 1 year ago

Actually, the implementation exists in Node.js 16.18.0 but it is not exposed via the globalThis object. See https://nodejs.org/docs/latest-v16.x/api/webstreams.html#class-transformstream. So you would need to add the code below in your project to make it run in Node.js 16. The same probably applies for ReadableStream and WritableStream classes (and maybe Blob).

import { TransformStream } from "node:stream/web";

globalThis.TransformStream = TransformStream