Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Catch errors from stream events in net.js
Browse files Browse the repository at this point in the history
Pipe into 'error' event.
  • Loading branch information
ry committed Apr 23, 2010
1 parent e72b7b8 commit 16f0240
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions lib/net.js
Expand Up @@ -267,8 +267,13 @@ function _doFlush () {
// Stream becomes writeable on connect() but don't flush if there's
// nothing actually to write
if (socket.flush()) {
if (socket._events && socket._events['drain']) socket.emit("drain");
if (socket.ondrain) socket.ondrain(); // Optimization
try {
if (socket._events && socket._events['drain']) socket.emit("drain");
if (socket.ondrain) socket.ondrain(); // Optimization
} catch (e) {
socket.destroy(e);
return;
}
}
}

Expand Down Expand Up @@ -305,7 +310,7 @@ function initStream (self) {
if (secureBytesRead === null && !self.server) {
// Client needs to write as part of handshake
self._writeWatcher.start();
return;
return;
}
} else {
bytesRead = read(self.fd,
Expand All @@ -320,7 +325,7 @@ function initStream (self) {

//debug('bytesRead ' + bytesRead + '\n');

if (self.secure && bytesRead == 0 && secureBytesRead > 0){
if (self.secure && bytesRead == 0 && secureBytesRead > 0) {
// Deal with SSL handshake
if (self.server) {
self._checkForSecureHandshake();
Expand All @@ -338,8 +343,13 @@ function initStream (self) {
if (!self.writable) self.destroy();
// Note: 'close' not emitted until nextTick.

if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
try {
if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
} catch (e) {
self.destroy(e);
return;
}
} else if (bytesRead > 0) {

timeout.active(self);
Expand All @@ -348,17 +358,22 @@ function initStream (self) {
var end = pool.used + bytesRead;
pool.used += bytesRead;

if (!self._encoding) {
if (self._events && self._events['data']) {
// emit a slice
self.emit('data', pool.slice(start, end));
}
try {
if (!self._encoding) {
if (self._events && self._events['data']) {
// emit a slice
self.emit('data', pool.slice(start, end));
}

// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(pool, start, end);
} else {
var string = pool.toString(self._encoding, start, end);
self.emit('data', string);
// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(pool, start, end);
} else {
var string = pool.toString(self._encoding, start, end);
self.emit('data', string);
}
} catch (e) {
self.destroy(e);
return;
}
}
};
Expand Down Expand Up @@ -586,9 +601,14 @@ Stream.prototype._writeOut = function (data, encoding) {
} else {
var secureBytesWritten = write(this.fd, securePool, 0, secureLen);
}
if(!this.secureEstablished && this.secureStream.isInitFinished()) {
if (!this.secureEstablished && this.secureStream.isInitFinished()) {
this.secureEstablished = true;
if (this._events && this._events['secure']) this.emit('secure');
try {
if (this._events && this._events['secure']) this.emit('secure');
} catch (e) {
this.destroy(e);
return;
}
}
} else {
bytesWritten = write(this.fd, buffer, off, len);
Expand Down Expand Up @@ -696,7 +716,12 @@ function doConnect (socket, port, host) {
socket.resume();
socket.readable = socket.writable = true;
socket._writeWatcher.callback = _doFlush;
socket.emit('connect');
try {
socket.emit('connect');
} catch (e) {
socket.destroy(e);
return;
}
} else if (errno != EINPROGRESS) {
socket.destroy(errnoException(errno, 'connect'));
}
Expand Down Expand Up @@ -902,9 +927,15 @@ function Server (listener) {
s.resume();

self.emit('connection', s);

// The 'connect' event probably should be removed for server-side
// sockets. It's redundant.
s.emit('connect');
try {
s.emit('connect');
} catch (e) {
s.destroy(e);
return;
}
}
};
}
Expand Down

0 comments on commit 16f0240

Please sign in to comment.