From b73f325327b43bf1501208bc75f328d990bcf289 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Wed, 4 Sep 2013 15:52:18 -0600 Subject: [PATCH] Reattach 'readable' event listener when changing streams Parser was not unregistering the 'readable' event listener from the old socket, nor registering it to the new socket when setSocket() was called. This caused event handling to stop during the STARTTLS handoff. Signed-off-by: Kevin Locke --- lib/Parser.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index bed817d..4e8c807 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -40,8 +40,6 @@ function Parser(stream, debug) { this._ignoreReadable = false; this.debug = debug; - this.setStream(stream); - var self = this; this._cbReadable = function() { if (self._ignoreReadable) @@ -51,12 +49,18 @@ function Parser(stream, debug) { else self._tryread(); }; - this._stream.on('readable', this._cbReadable); + + this.setStream(stream); + process.nextTick(this._cbReadable); } inherits(Parser, EventEmitter); Parser.prototype.setStream = function(stream) { + if (this._stream) { + this._stream.removeListener('readable', this._cbReadable); + } + if (/^v0\.8\./.test(process.version)) { this._stream = (new ReadableStream()).wrap(stream); @@ -66,6 +70,8 @@ Parser.prototype.setStream = function(stream) { stream._events.error.pop(); } else this._stream = stream; + + this._stream.on('readable', this._cbReadable); }; Parser.prototype._tryread = function(n) {