Skip to content

Commit

Permalink
Improve stream detection to allow Stream-like interfaces (#179)
Browse files Browse the repository at this point in the history
* expand stream test

* add is-stream@^2.0.0

* use isStream

* rework test
  • Loading branch information
ctalkington authored Mar 10, 2024
1 parent 3c95a39 commit 06078d5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/archivers/archive-output-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
*/
var inherits = require('util').inherits;
var isStream = require('is-stream');
var Transform = require('readable-stream').Transform;

var ArchiveEntry = require('./archive-entry');
Expand Down Expand Up @@ -84,7 +85,7 @@ ArchiveOutputStream.prototype.entry = function(ae, source, callback) {

if (Buffer.isBuffer(source)) {
this._appendBuffer(ae, source, callback);
} else if (util.isStream(source)) {
} else if (isStream(source)) {
this._appendStream(ae, source, callback);
} else {
this._archive.processing = false;
Expand Down
7 changes: 2 additions & 5 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
*/
var Stream = require('stream').Stream;
var PassThrough = require('readable-stream').PassThrough;
var isStream = require('is-stream');

var util = module.exports = {};

util.isStream = function(source) {
return source instanceof Stream;
};

util.normalizeInputSource = function(source) {
if (source === null) {
return Buffer.alloc(0);
} else if (typeof source === 'string') {
return Buffer.from(source);
} else if (util.isStream(source) && !source._readableState) {
} else if (isStream(source) && !source._readableState) {
var normalized = new PassThrough();
source.pipe(normalized);

Expand Down
21 changes: 19 additions & 2 deletions 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 @@ -28,6 +28,7 @@
"dependencies": {
"crc-32": "^1.2.0",
"crc32-stream": "^6.0.0",
"is-stream": "^2.0.1",
"normalize-path": "^3.0.0",
"readable-stream": "^4.0.0"
},
Expand Down
17 changes: 16 additions & 1 deletion test/zip-archive-output-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs');
var stream = require('stream');
var assert = require('chai').assert;
var mkdir = require('mkdirp');
var Readable = require('readable-stream').Readable;

var helpers = require('./helpers');
var WriteHashStream = helpers.WriteHashStream;
Expand Down Expand Up @@ -48,6 +49,20 @@ describe('ZipArchiveOutputStream', function() {
archive.entry(entry, fs.createReadStream('test/fixtures/test.txt')).finish();
});

it('should append Stream-like sources', function(done) {
var archive = new ZipArchiveOutputStream();
var testStream = new WriteHashStream('tmp/zip-stream-like.zip');
var entry = new ZipArchiveEntry('stream-like.txt');

testStream.on('close', function() {
done();
});

archive.pipe(testStream);

archive.entry(entry, Readable.from(['test'])).finish();
});

it('should stop streaming on Stream error', function(done) {
var archive = new ZipArchiveOutputStream();
var testStream = new WriteHashStream('tmp/zip-stream.zip');
Expand Down Expand Up @@ -129,4 +144,4 @@ describe('ZipArchiveOutputStream', function() {
});
});

});
});

0 comments on commit 06078d5

Please sign in to comment.