Skip to content

Commit 06078d5

Browse files
authored
Improve stream detection to allow Stream-like interfaces (#179)
* expand stream test * add is-stream@^2.0.0 * use isStream * rework test
1 parent 3c95a39 commit 06078d5

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

lib/archivers/archive-output-stream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
77
*/
88
var inherits = require('util').inherits;
9+
var isStream = require('is-stream');
910
var Transform = require('readable-stream').Transform;
1011

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

8586
if (Buffer.isBuffer(source)) {
8687
this._appendBuffer(ae, source, callback);
87-
} else if (util.isStream(source)) {
88+
} else if (isStream(source)) {
8889
this._appendStream(ae, source, callback);
8990
} else {
9091
this._archive.processing = false;

lib/util/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
*/
88
var Stream = require('stream').Stream;
99
var PassThrough = require('readable-stream').PassThrough;
10+
var isStream = require('is-stream');
1011

1112
var util = module.exports = {};
1213

13-
util.isStream = function(source) {
14-
return source instanceof Stream;
15-
};
16-
1714
util.normalizeInputSource = function(source) {
1815
if (source === null) {
1916
return Buffer.alloc(0);
2017
} else if (typeof source === 'string') {
2118
return Buffer.from(source);
22-
} else if (util.isStream(source) && !source._readableState) {
19+
} else if (isStream(source) && !source._readableState) {
2320
var normalized = new PassThrough();
2421
source.pipe(normalized);
2522

package-lock.json

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"crc-32": "^1.2.0",
3030
"crc32-stream": "^6.0.0",
31+
"is-stream": "^2.0.1",
3132
"normalize-path": "^3.0.0",
3233
"readable-stream": "^4.0.0"
3334
},

test/zip-archive-output-stream.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var stream = require('stream');
44
var assert = require('chai').assert;
55
var mkdir = require('mkdirp');
6+
var Readable = require('readable-stream').Readable;
67

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

52+
it('should append Stream-like sources', function(done) {
53+
var archive = new ZipArchiveOutputStream();
54+
var testStream = new WriteHashStream('tmp/zip-stream-like.zip');
55+
var entry = new ZipArchiveEntry('stream-like.txt');
56+
57+
testStream.on('close', function() {
58+
done();
59+
});
60+
61+
archive.pipe(testStream);
62+
63+
archive.entry(entry, Readable.from(['test'])).finish();
64+
});
65+
5166
it('should stop streaming on Stream error', function(done) {
5267
var archive = new ZipArchiveOutputStream();
5368
var testStream = new WriteHashStream('tmp/zip-stream.zip');
@@ -129,4 +144,4 @@ describe('ZipArchiveOutputStream', function() {
129144
});
130145
});
131146

132-
});
147+
});

0 commit comments

Comments
 (0)