Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stream detection to allow Stream-like interfaces #179

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
});
});

});
});