destroy logic for extract stream. closes #2

This commit is contained in:
Mathias Buus 2013-12-20 23:17:37 +01:00
parent 3ee758edd0
commit 61f56ed9bf

View file

@ -28,6 +28,7 @@ var Extract = function(opts) {
this._overflow = null; this._overflow = null;
this._cb = null; this._cb = null;
this._locked = false; this._locked = false;
this._destroyed = false;
var self = this; var self = this;
var b = self._buffer; var b = self._buffer;
@ -38,7 +39,10 @@ var Extract = function(opts) {
var onunlock = function(err) { var onunlock = function(err) {
self._locked = false; self._locked = false;
if (err) return self.emit('error', err); if (err) {
self.emit('error', err);
return self.destroy();
}
if (!self._stream) oncontinue(); if (!self._stream) oncontinue();
}; };
@ -65,13 +69,15 @@ var Extract = function(opts) {
oncontinue(); oncontinue();
return; return;
} }
self._locked = true;
if (!header.size) { if (!header.size) {
self._parse(512, onheader); self._parse(512, onheader);
self.emit('entry', header, emptyStream(), oncontinue); self.emit('entry', header, emptyStream(), onunlock);
return; return;
} }
self._locked = true;
self._stream = new stream.PassThrough(); self._stream = new stream.PassThrough();
self.emit('entry', header, self._stream, onunlock); self.emit('entry', header, self._stream, onunlock);
@ -84,12 +90,21 @@ var Extract = function(opts) {
util.inherits(Extract, stream.Writable); util.inherits(Extract, stream.Writable);
Extract.prototype.destroy = function() {
if (this._destroyed) return;
this._destroyed = true;
this.emit('close');
if (this._stream) this._stream.emit('close');
};
Extract.prototype._parse = function(size, onparse) { Extract.prototype._parse = function(size, onparse) {
if (this._destroyed) return;
this._missing = size; this._missing = size;
this._onparse = onparse; this._onparse = onparse;
}; };
Extract.prototype._continue = function(err) { Extract.prototype._continue = function(err) {
if (this._destroyed) return;
var cb = this._cb; var cb = this._cb;
this._cb = noop; this._cb = noop;
if (this._overflow) this._write(this._overflow, undefined, cb); if (this._overflow) this._write(this._overflow, undefined, cb);
@ -97,6 +112,8 @@ Extract.prototype._continue = function(err) {
}; };
Extract.prototype._write = function(data, enc, cb) { Extract.prototype._write = function(data, enc, cb) {
if (this._destroyed) return;
var s = this._stream; var s = this._stream;
var b = this._buffer; var b = this._buffer;
var missing = this._missing; var missing = this._missing;