Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async iteration #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ fs.createReadStream('path/to/archive.zip')
});
```

and the same example using async iterators:
and the same example using async iterators on node v12 and above:

```js
const zip = fs.createReadStream('path/to/archive.zip').pipe(unzipper.Parse({forceStream: true}));
for await (const entry of zip) {
for await (const entry of unzipper.iterateStream(zip)) {
const fileName = entry.path;
const type = entry.type; // 'Directory' or 'File'
const size = entry.vars.uncompressedSize; // There is also compressedSize;
if (fileName === "this IS the file I'm looking for") {
entry.pipe(fs.createWriteStream('output/path'));
for await (const chunk of unzipper.iterateStream(entry)) {
console.log(chunk);
}
} else {
entry.autodrain();
}
Expand Down
35 changes: 35 additions & 0 deletions lib/iterateStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {once} = require('events');
Copy link
Author

@alubbe alubbe Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is the same helper as used (and tested) in the exceljs code base


module.exports = async function* iterateStream(stream) {
const contents = [];
stream.on('data', data => contents.push(data));

let resolveStreamEndedPromise;
const streamEndedPromise = new Promise(resolve => (resolveStreamEndedPromise = resolve));

let ended = false;
stream.on('end', () => {
ended = true;
resolveStreamEndedPromise();
});

let error = false;
stream.on('error', err => {
error = err;
resolveStreamEndedPromise();
});

while (!ended || contents.length > 0) {
if (contents.length === 0) {
stream.resume();
// eslint-disable-next-line no-await-in-loop
await Promise.race([once(stream, 'data'), streamEndedPromise]);
} else {
stream.pause();
const data = contents.shift();
yield data;
}
if (error) throw error;
}
resolveStreamEndedPromise();
};
6 changes: 5 additions & 1 deletion unzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ require('setimmediate');
exports.Parse = require('./lib/parse');
exports.ParseOne = require('./lib/parseOne');
exports.Extract = require('./lib/extract');
exports.Open = require('./lib/Open');
exports.Open = require('./lib/Open');

if (parseInt(process.versions.node.split('.')[0], 10) >= 12) {
exports.iterateStream = require('./lib/iterateStream');
}