From 6a9833739ee79423b642e1aefd72b1a07762efcc Mon Sep 17 00:00:00 2001 From: Evan Oxfeld Date: Thu, 4 Apr 2013 01:37:12 -0400 Subject: [PATCH 1/4] Fix consuming variable length file data w/o entry listener Correctly consumes variable length file data when no entry lisener has been registered and unzip.Parse receives the data as multiple chunks. Without this fix, unzip.Parse always stops piping variable length file data after the first chunk is sent to zlib. --- lib/parse.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index ebbf029..79e615a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -156,10 +156,10 @@ Parse.prototype._readFile = function () { descriptorSig.writeUInt32LE(0x08074b50, 0); var matchStream = new MatchStream({ pattern: descriptorSig }, function (buf, matched, extra) { + if (!matched) { + return hasEntryListener ? this.push(buf) : null; + } if (hasEntryListener) { - if (!matched) { - return this.push(buf); - } this.push(buf); } setImmediate(function() { From 34f30cc3c47c9792fbce7c8dbb48b59463bd9555 Mon Sep 17 00:00:00 2001 From: Evan Oxfeld Date: Thu, 4 Apr 2013 02:22:46 -0400 Subject: [PATCH 2/4] Stop piping when data descriptor is reached setImmediate happens on the next tick and allows I/O, causing writes to the MatchStream after it should have ended. Only fixes this issue in Node 0.10.x. In node 0.8.x, process.nextTick has similar behavior to setImmediate. --- lib/parse.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 79e615a..0e7cfc1 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -162,7 +162,7 @@ Parse.prototype._readFile = function () { if (hasEntryListener) { this.push(buf); } - setImmediate(function() { + process.nextTick(function() { self._pullStream.unpipe(); self._pullStream.prepend(extra); self._processDataDescriptor(entry); diff --git a/package.json b/package.json index e030e02..3bf0665 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "binary": "~0.3.0", "readable-stream": "~1.0.0", "setimmediate": "~1.0.1", - "match-stream": "0.0.1" + "match-stream": "~0.0.2" }, "devDependencies": { "tap": "~0.3.0", From d758ebbe727ce75d46b356f48b942f89fdd8c2a2 Mon Sep 17 00:00:00 2001 From: Evan Oxfeld Date: Thu, 4 Apr 2013 02:25:47 -0400 Subject: [PATCH 3/4] Fix maximum call stack size exceeded errors Recursively calling Parse._flush caused recursive process.nextTick calls. Fixes #16. --- lib/parse.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parse.js b/lib/parse.js index 0e7cfc1..aae160d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -240,7 +240,7 @@ Parse.prototype._readCentralDirectoryFileHeader = function () { if (err) { return self.emit('error', err); } - return self._readRecord(); + return setImmediate(self._readRecord.bind(self)); }); }); }); From 548c16b9f51e2eaed5bc49a8f785ede38db1a02f Mon Sep 17 00:00:00 2001 From: Evan Oxfeld Date: Fri, 5 Apr 2013 00:34:56 -0400 Subject: [PATCH 4/4] Use match-stream wip --- lib/parse.js | 13 ++++++------- package.json | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index aae160d..4b0420a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -155,19 +155,18 @@ Parse.prototype._readFile = function () { var descriptorSig = new Buffer(4); descriptorSig.writeUInt32LE(0x08074b50, 0); - var matchStream = new MatchStream({ pattern: descriptorSig }, function (buf, matched, extra) { + var matchStream = new MatchStream({ pattern: descriptorSig }, function (buf, matched) { if (!matched) { return hasEntryListener ? this.push(buf) : null; } + self._pullStream.unpipe(); if (hasEntryListener) { this.push(buf); } - process.nextTick(function() { - self._pullStream.unpipe(); - self._pullStream.prepend(extra); - self._processDataDescriptor(entry); - }); - return this.push(null); + this.end(); + }, function (extra) { + self._pullStream.prepend(extra); + self._processDataDescriptor(entry); }); self._pullStream.pipe(matchStream); diff --git a/package.json b/package.json index 3bf0665..906f8d6 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "binary": "~0.3.0", "readable-stream": "~1.0.0", "setimmediate": "~1.0.1", - "match-stream": "~0.0.2" + "match-stream": "git://github.com/EvanOxfeld/match-stream.git#callback-on-finish-wip" }, "devDependencies": { "tap": "~0.3.0",