Touffy / client-zip

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

Fix safari+firefox with service worker: clone file.encodedName on yield #83

Closed zakstucke closed 5 months ago

zakstucke commented 5 months ago

Love the library. I encountered a very weird issue i painstakingly debugged, resulting in this PR.

Setup:

Error occurs on closing the zip (i.e. once all files have been included and the stream is being closed by client-zip) Firefox error: MessagePort.postMessage: attempting to access detached ArrayBuffer

Safari error: Unhandled Promise Rejection: DataCloneError: The object can not be cloned.

The firefox error is the actual cause, the safari one gets hidden behind the above opaque one.

What I think is happening and why this PR fixes it:

This change works for me now on both safari and firefox.

Thanks!

Touffy commented 5 months ago

Your analysis is correct. The file name buffer is used twice in the stream — in the file header and the central repository (the "index" of the archive) — and of course it can't be transferred twice (which is happening because you're running client-zip in the client window, and the Native File System adapter is transferring the stream to the Service Worker).

zakstucke commented 5 months ago

Great. Thanks!

Touffy commented 5 months ago

I'll publish this evening.