diff --git a/lib/Dicer.js b/lib/Dicer.js index e385a94..beb3d9d 100644 --- a/lib/Dicer.js +++ b/lib/Dicer.js @@ -1,7 +1,7 @@ 'use strict'; const { Writable } = require('stream'); - +inherits = require('util').inherits; const StreamSearch = require('streamsearch'); const PartStream = require('./PartStream'); @@ -29,7 +29,6 @@ class Dicer extends Writable { this._dashes = 0; this._parts = 0; this._finished = false; - this._realFinish = false; this._isPreamble = true; this._justMatched = false; this._firstWrite = true; @@ -56,7 +55,7 @@ class Dicer extends Writable { } emit(ev) { - if (ev !== 'finish' || this._realFinish) { + if (ev !== 'finish') { Writable.prototype.emit.apply(this, arguments); return; } @@ -76,16 +75,12 @@ class Dicer extends Writable { ); this._part.push(null); process.nextTick(() => { - this._realFinish = true; - this.emit('finish'); - this._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); return; } - this._realFinish = true; - this.emit('finish'); - this._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); } @@ -160,9 +155,7 @@ function onInfo(isMatch, data, start, end) { this._finished = true; // No more parts will be added if (this._parts === 0) { - this._realFinish = true; - this.emit('finish'); - this._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } } if (this._dashes) @@ -190,7 +183,7 @@ function onInfo(isMatch, data, start, end) { shouldWriteMore = this._part.push(data.slice(start, end)); if (!shouldWriteMore) this._pause = true; - } else if (!this._isPreamble && this._inHeader) { + } else { if (buf) this._hparser.push(buf); r = this._hparser.push(data.slice(start, end)); @@ -207,9 +200,7 @@ function onInfo(isMatch, data, start, end) { this._part.on('end', () => { if (--this._parts === 0) { if (this._finished) { - this._realFinish = true; - this.emit('finish'); - this._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } else { unpause(this); } diff --git a/lib/HeaderParser.js b/lib/HeaderParser.js index cc08b34..5d338c5 100644 --- a/lib/HeaderParser.js +++ b/lib/HeaderParser.js @@ -27,7 +27,7 @@ class HeaderParser extends EventEmitter { this.ss = new StreamSearch(B_DCRLF, (isMatch, data, start, end) => { if (data && !this.maxed) { if (this.nread + (end - start) > MAX_HEADER_SIZE) { - end = (MAX_HEADER_SIZE - this.nread); + end = (MAX_HEADER_SIZE - self.nread) + start; this.nread = MAX_HEADER_SIZE; } else { this.nread += (end - start);