Skip to content

Commit

Permalink
Feature: allow queue callback function parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
reabreu committed May 28, 2021
1 parent f6c9cc2 commit d1da40c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
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" }, () => {
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
7 changes: 6 additions & 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 @@ -31,6 +31,7 @@
"archiver-utils": "^2.1.0",
"async": "^3.2.0",
"buffer-crc32": "^0.2.1",
"chai-spies": "^1.0.0",
"readable-stream": "^3.6.0",
"readdir-glob": "^1.0.0",
"tar-stream": "^2.2.0",
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

0 comments on commit d1da40c

Please sign in to comment.