From 637df9706d4c4306767f1df6c7dbd7d9e6e36554 Mon Sep 17 00:00:00 2001 From: Ziggy Jonsson Date: Sat, 4 May 2024 19:57:31 +0000 Subject: [PATCH] Open methods - only stream up to length (#310) * Open methods - only stream up to length * bump version --- lib/Open/directory.js | 8 +++++++- lib/Open/index.js | 14 +++++++++----- lib/Open/unzip.js | 4 ++-- lib/PullStream.js | 2 +- package.json | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/Open/directory.js b/lib/Open/directory.js index d095185..0ffae26 100644 --- a/lib/Open/directory.js +++ b/lib/Open/directory.js @@ -215,7 +215,13 @@ module.exports = function centralDirectory(source, options) { vars.comment = comment; vars.type = (vars.uncompressedSize === 0 && /[\/\\]$/.test(vars.path)) ? 'Directory' : 'File'; vars.stream = function(_password) { - return unzip(source, vars.offsetToLocalFileHeader,_password, vars); + var totalSize = 30 + + 10 // add an extra buffer + + (vars.extraFieldLength || 0) + + (vars.fileNameLength || 0) + + vars.compressedSize; + + return unzip(source, vars.offsetToLocalFileHeader,_password, vars, totalSize); }; vars.buffer = function(_password) { return BufferStream(vars.stream(_password)); diff --git a/lib/Open/index.js b/lib/Open/index.js index 98c7901..4196086 100644 --- a/lib/Open/index.js +++ b/lib/Open/index.js @@ -8,7 +8,8 @@ module.exports = { var source = { stream: function(offset, length) { var stream = Stream.PassThrough(); - stream.end(buffer.slice(offset, length)); + var end = length ? offset + length : undefined; + stream.end(buffer.slice(offset, end)); return stream; }, size: function() { @@ -19,8 +20,9 @@ module.exports = { }, file: function(filename, options) { var source = { - stream: function(offset,length) { - return fs.createReadStream(filename,{start: offset, end: length && offset+length}); + stream: function(start,length) { + var end = length ? start + length : undefined; + return fs.createReadStream(filename,{start, end}); }, size: function() { return new Promise(function(resolve,reject) { @@ -46,8 +48,9 @@ module.exports = { var source = { stream : function(offset,length) { var options = Object.create(params); + var end = length ? offset + length : ''; options.headers = Object.create(params.headers); - options.headers.range = 'bytes='+offset+'-' + (length ? length : ''); + options.headers.range = 'bytes='+offset+'-' + end; return request(options); }, size: function() { @@ -83,7 +86,8 @@ module.exports = { var d = {}; for (var key in params) d[key] = params[key]; - d.Range = 'bytes='+offset+'-' + (length ? length : ''); + var end = length ? offset + length : ''; + d.Range = 'bytes='+offset+'-' + end; return client.getObject(d).createReadStream(); } }; diff --git a/lib/Open/unzip.js b/lib/Open/unzip.js index 81204f0..6205ab4 100644 --- a/lib/Open/unzip.js +++ b/lib/Open/unzip.js @@ -7,11 +7,11 @@ var parseExtraField = require('../parseExtraField'); var parseDateTime = require('../parseDateTime'); var parseBuffer = require('../parseBuffer'); -module.exports = function unzip(source,offset,_password, directoryVars) { +module.exports = function unzip(source, offset, _password, directoryVars, length) { var file = PullStream(), entry = Stream.PassThrough(); - var req = source.stream(offset); + var req = source.stream(offset, length); req.pipe(file).on('error', function(e) { entry.emit('error', e); }); diff --git a/lib/PullStream.js b/lib/PullStream.js index 1b2c90b..0195d01 100644 --- a/lib/PullStream.js +++ b/lib/PullStream.js @@ -46,7 +46,7 @@ PullStream.prototype.stream = function(eof,includeEof) { packet = self.buffer.slice(0,eof); self.buffer = self.buffer.slice(eof); eof -= packet.length; - done = !eof; + done = done || !eof; } else { var match = self.buffer.indexOf(eof); if (match !== -1) { diff --git a/package.json b/package.json index eeaf216..ec64ed2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unzipper", - "version": "0.11.4", + "version": "0.11.5", "description": "Unzip cross-platform streaming API ", "author": "Evan Oxfeld ", "contributors": [