Skip to content

Commit d1da40c

Browse files
committed
Feature: allow queue callback function parameter
1 parent f6c9cc2 commit d1da40c

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ archive.directory('subdir/', false);
7878
// append files from a glob pattern
7979
archive.glob('file*.txt', {cwd:__dirname});
8080

81+
// append a file with a callback for when the queue has been processed.
82+
// usefull for implementing backpressure prevention mechanisms
83+
archive.file("file1.txt", { name: "file4.txt" }, () => {
84+
console.log("task complete, do something....");
85+
});
86+
8187
// finalize the archive (ie we are done appending files but streams have to finish yet)
8288
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
8389
archive.finalize();

lib/core.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,10 @@ Archiver.prototype.abort = function() {
553553
* @fires Archiver#entry
554554
* @param {(Buffer|Stream|String)} source The input source.
555555
* @param {EntryData} data See also {@link ZipEntryData} and {@link TarEntryData}.
556+
* @param {Function} callback
556557
* @return {this}
557558
*/
558-
Archiver.prototype.append = function(source, data) {
559+
Archiver.prototype.append = function(source, data, cb = () => {}) {
559560
if (this._state.finalize || this._state.aborted) {
560561
this.emit('error', new ArchiverError('QUEUECLOSED'));
561562
return this;
@@ -588,7 +589,7 @@ Archiver.prototype.append = function(source, data) {
588589
this._queue.push({
589590
data: data,
590591
source: source
591-
});
592+
}, cb);
592593

593594
return this;
594595
};

package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"archiver-utils": "^2.1.0",
3232
"async": "^3.2.0",
3333
"buffer-crc32": "^0.2.1",
34+
"chai-spies": "^1.0.0",
3435
"readable-stream": "^3.6.0",
3536
"readdir-glob": "^1.0.0",
3637
"tar-stream": "^2.2.0",

test/archiver.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ var fs = require('fs');
33
var PassThrough = require('readable-stream').PassThrough;
44
var WriteStream = fs.createWriteStream;
55

6-
var assert = require('chai').assert;
6+
var spies = require('chai-spies');
7+
var chai = require('chai')
8+
var assert = chai.assert;
79
var mkdir = require('mkdirp');
810

11+
chai.use(spies);
12+
913
var helpers = require('./helpers');
1014
var HashStream = helpers.HashStream;
1115
var UnBufferedStream = helpers.UnBufferedStream;
@@ -352,6 +356,28 @@ describe('archiver', function() {
352356
});
353357
});
354358

359+
describe('#callback function', function() {
360+
var archive;
361+
362+
it('should call callback function', function(done) {
363+
archive = archiver('json');
364+
var testStream = new WriteStream('tmp/promise.json');
365+
366+
const noop = () => {}
367+
const spy = chai.spy(noop);
368+
369+
archive.pipe(testStream);
370+
371+
archive
372+
.append(testBuffer, { name: 'buffer.txt', date: testDate }, spy)
373+
.finalize()
374+
.then(function() {
375+
chai.expect(spy).to.have.been.called();
376+
done()
377+
})
378+
});
379+
});
380+
355381
describe('#errors', function() {
356382
var archive;
357383

0 commit comments

Comments
 (0)