diff --git a/lib/archivers/archive-output-stream.js b/lib/archivers/archive-output-stream.js index b5fa4939..d349b5e6 100644 --- a/lib/archivers/archive-output-stream.js +++ b/lib/archivers/archive-output-stream.js @@ -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'); @@ -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; diff --git a/lib/util/index.js b/lib/util/index.js index 20a67833..0fcc60f9 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -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); diff --git a/package-lock.json b/package-lock.json index 15943e33..eb20bcf0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "compress-commons", - "version": "6.0.0", + "version": "6.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "compress-commons", - "version": "6.0.0", + "version": "6.0.1", "license": "MIT", "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" }, @@ -777,6 +778,17 @@ "node": ">=8" } }, + "node_modules/is-stream": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", + "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-unicode-supported": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", @@ -2122,6 +2134,11 @@ "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", "dev": true }, + "is-stream": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", + "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==" + }, "is-unicode-supported": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", diff --git a/package.json b/package.json index 867778b9..20cb5525 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/zip-archive-output-stream.js b/test/zip-archive-output-stream.js index ba2bbf7b..1c1ca8c1 100644 --- a/test/zip-archive-output-stream.js +++ b/test/zip-archive-output-stream.js @@ -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; @@ -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'); @@ -129,4 +144,4 @@ describe('ZipArchiveOutputStream', function() { }); }); -}); \ No newline at end of file +});