Comments (9)
I haven't tried cloning your repo yet but based on that error message I'm pretty sure I know why you're having issues. A "detached ArrayBuffer" is essentially an ArrayBuffer (i.e. the underlying block of memory for a Uint8Array) that has either been invalidated, or been moved to a separate thread or process.
In the context of WebAssembly, this usually happens when you construct a Uint8Array that points to a location in the WASM memory (likely as an output pointer for your WASM code to write the converted image to), but then you have to grow the WASM memory to do some temporary heap allocations. When you grow the WASM memory all pointers to the old memory are invalidated, and their ArrayBuffers detached.
In this case I'd guess that the WASM executed by convertImageWithImageMagick
is allocating a new output buffer for the image each time it's called. Since you aren't manually freeing the memory it returns (you'd probably have to call some release
function on the returned Uint8Array), the allocator is running out of memory space and is therefore growing the entire WASM memory on each call. In other words, every time you call convertImageWithImageMagick
, all its previous return values become detached and therefore break.
This issue gets resolved if you immediately copy the data out of the WASM memory into JavaScript-owned memory (e.g. with out.slice(0)
, as you observed) because then you're no longer keeping a long-term reference to the WASM memory. But when you use ZipPassThrough
directly, fflate
directly moves your input to the output stream with zero copies, and here, that input is a Uint8Array that points to WASM memory that gets invalidated after future calls to convertImageWithImageMagick
. So when you try to concatenate all the output buffers and download them with new Blob(streams)
, you're still using those invalid WASM pointers.
My recommendation here is to modify your convertImageWithImageMagick
function to return a copy of the WASM memory, rather than a view into WASM memory (e.g. if it's currently returning origBuffer
, return origBuffer.slice()
instead), and deallocate the original view back using your WASM binary's allocator (maybe your bindings have some sort of free(origBuffer)
function?). This should fix your current problem and any future unexpected behavior you could encounter due to that function returning a "temporary" buffer.
Let me know if you need any more clarification or if you have any further issues!
from fflate.
I tried the fflate.zipSync
and same error. Also, the files which are corrupt are random it seems, it changes on each download. Maybe I'm doing something wrong?
from fflate.
By any chance have you tried this with fflate v0.8.2? I released it just a few hours ago and this issue seems very similar to something I fixed in that release.
from fflate.
I'm using 0.8.1, let me try 0.8.2 then :) Thanks.
from fflate.
Ah now with 0.8.2 I'm getting this when I create the zip with 2 files:
from fflate.
Can you send me a repo with this code or at least some examples of data you're compressing here? The corrupted ZIP would help too.
from fflate.
@101arrowz okay I whipped together this demo, which seems to be demonstrating the latest error of the unopenable archive.
https://github.com/lancejpollard/zip-test-with-fflate
I use pnpm install && pnpm dev
to start it, then upload 2 or 3 PNGs and it will convert them to JPG with imagemagick-wasm.
Here are the PNGs I used if it makes a difference.
I noticed before I added imagemagick-wasm just now, if I simply selected PNGs and zipped them without conversion, it worked. SO it could also be something with imagemagick, I'm not sure. Oh I also tried UZIP.js and it worked for more files but still resulted in corrupted images, so if that is also failing, it could also be something I'm doing, not sure.
Thanks for taking a look.
from fflate.
I just tried once using this:
zipFile.push(out.slice(0), true);
And it seems to have not corrupted the zipped images? Could it be something to do with the error you sometimes get with array buffers like:
Cannot perform %TypedArray%.prototype.set on a detached ArrayBuffer
from fflate.
Wow thank you for that! It really clears things up, I was just about to ask about that on the web. This is a really great explanation and makes total sense. I will move the .slice()
to inside convertImageWithImageMagick, so it's abstracted away I guess. Thanks again!
from fflate.
Related Issues (20)
- Deflate/Inflate does not work with zlib/gzip c++ HOT 7
- Add Bzip2?
- Hangs on gunzipSync of compressed content with invalid dictionary HOT 3
- Option to shorten output as much as possible HOT 3
- Incorrect argument order in AsyncFlateStreamHandler HOT 5
- Truncated output of gunzip if SIZE footer is incorrect HOT 5
- return uncompressed size instead of resized output buffer HOT 4
- Improve zip documentation HOT 1
- The encoded data was not valid for encoding utf-8 HOT 1
- Streams onData do not work as expected anymore HOT 8
- CDN bundle for 0.8.1 is broken HOT 2
- Async unzip maxing out CPU and memory for some files HOT 3
- fflate.decompressSync is not a function when using `nifti-reader-js` (React) HOT 7
- Occasional CRC Errors When Streaming Data into Zip using AsyncZipDeflate HOT 7
- `zipSync` can get order of ZIP entries wrong, due to how objects work HOT 3
- delete the rust code
- `gunzipSync` failing (0 gzip length) on some npm package tarballs HOT 1
- Unexpected EOF when inflating ZIP HOT 3
- Unzip error thrown when big file as compression type 9 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fflate.