Skip to content

Commit

Permalink
Fix - race in zstd initialization #318
Browse files Browse the repository at this point in the history
While ZSTD decoding can be invoked
from different places, ensure that initialization performed properly.
  • Loading branch information
linev committed Aug 21, 2024
1 parent 8239a88 commit 6d27dd9
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions modules/io.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,10 +2017,23 @@ async function R__unzip(arr, tgtsize, noalert, src_shift) {
const tgt8arr = new Uint8Array(tgtbuf, fullres);

if (fmt === 'ZSTD') {
const promise = internals._ZstdStream
? Promise.resolve(internals._ZstdStream)
: (isNodeJs() ? import('@oneidentity/zstd-js') : import('./base/zstd.mjs'))
.then(({ ZstdInit }) => ZstdInit()).then(({ ZstdStream }) => { internals._ZstdStream = ZstdStream; return ZstdStream; });
let promise;
if (internals._ZstdStream)
promise = Promise.resolve(internals._ZstdStream);
else if (internals._ZstdInit !== undefined)
promise = new Promise(resolveFunc => { internals._ZstdInit.push(resolveFunc); })
else {
internals._ZstdInit = [];
promise = (isNodeJs() ? import('@oneidentity/zstd-js') : import('./base/zstd.mjs'))
.then(({ ZstdInit }) => ZstdInit())
.then(({ ZstdStream }) => {
internals._ZstdStream = ZstdStream;
internals._ZstdInit.forEach(func => func(ZstdStream));
delete internals._ZstdInit;
return ZstdStream;
});
}

return promise.then(ZstdStream => {
const data2 = ZstdStream.decompress(uint8arr),
reslen = data2.length;
Expand Down

0 comments on commit 6d27dd9

Please sign in to comment.