ZJONSSON / node-unzipper

node.js cross-platform unzip using streams
Other
424 stars 114 forks source link

stream is not async iterable (DuplexWrapper) #282

Open lapo-luchini opened 1 year ago

lapo-luchini commented 1 year ago

I'm trying to decode a single file content to string using async iterators:

async function streamToString(stream) {
    // https://stackoverflow.com/a/63361543/166524
    const chunks = [];
    for await (const chunk of stream) {
        chunks.push(Buffer.from(chunk));
    }
    return Buffer.concat(chunks).toString("utf8");
}

const stream = fs.createReadStream(proj)
    .pipe(unzipper.ParseOne(reFile));
const data = await streamToString(stream);

but it fails like this:

TypeError: stream is not async iterable

OTOH this works fine:

function streamToString(stream) {
    const chunks = [];
    return new Promise((resolve, reject) => {
        stream.on('data', chunk => chunks.push(chunk));
        stream.on('error', err => reject(err));
        stream.on('end', () => resolve(Buffer.concat(chunks).toString("utf8")));
    });
}

I see that nodejs' Duplex itself had this bug two years ago, might this be related?