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

Feature: allow queue callback function parameter #519

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ archive.directory('subdir/', false);
// append files from a glob pattern
archive.glob('file*.txt', {cwd:__dirname});

// append a file with a callback for when the queue has been processed.
// usefull for implementing backpressure prevention mechanisms
archive.file("file1.txt", { name: "file4.txt" }, () => {

Choose a reason for hiding this comment

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

[✓] creating this documentation is an excellent touch
[ ] it should be moved from the archive.file example to the archive.append example(s)

console.log("task complete, do something....");
});

// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
Expand Down
5 changes: 3 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,10 @@ Archiver.prototype.abort = function() {
* @fires Archiver#entry
* @param {(Buffer|Stream|String)} source The input source.
* @param {EntryData} data See also {@link ZipEntryData} and {@link TarEntryData}.
* @param {Function} callback
* @return {this}
*/
Archiver.prototype.append = function(source, data) {
Archiver.prototype.append = function(source, data, cb = () => {}) {
if (this._state.finalize || this._state.aborted) {
this.emit('error', new ArchiverError('QUEUECLOSED'));
return this;
Expand Down Expand Up @@ -588,7 +589,7 @@ Archiver.prototype.append = function(source, data) {
this._queue.push({
data: data,
source: source
});
}, cb);

return this;
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"devDependencies": {
"archiver-jsdoc-theme": "^1.1.3",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",

Choose a reason for hiding this comment

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

[✓] I successfully ran the tests locally with this helper
[ ] It is up to the maintainer if they want to add a new library, or if they have other techniques for this type of test...

"jsdoc": "^3.6.4",
"mkdirp": "^1.0.4",
"mocha": "^8.0.1",
Expand Down
28 changes: 27 additions & 1 deletion test/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ var fs = require('fs');
var PassThrough = require('readable-stream').PassThrough;
var WriteStream = fs.createWriteStream;

var assert = require('chai').assert;
var spies = require('chai-spies');
var chai = require('chai')
var assert = chai.assert;
var mkdir = require('mkdirp');

chai.use(spies);

var helpers = require('./helpers');
var HashStream = helpers.HashStream;
var UnBufferedStream = helpers.UnBufferedStream;
Expand Down Expand Up @@ -352,6 +356,28 @@ describe('archiver', function() {
});
});

describe('#callback function', function() {
var archive;

it('should call callback function', function(done) {
archive = archiver('json');
var testStream = new WriteStream('tmp/promise.json');

const noop = () => {}
const spy = chai.spy(noop);

archive.pipe(testStream);

archive
.append(testBuffer, { name: 'buffer.txt', date: testDate }, spy)
.finalize()
.then(function() {
chai.expect(spy).to.have.been.called();
done()
})
});
});

describe('#errors', function() {
var archive;

Expand Down