Skip to content

Commit

Permalink
Handle 0-length buffers.
Browse files Browse the repository at this point in the history
- Work around behavioral differences in different NodeJS versions with respect
  0-length buffers. Explicitly avoid attempting a copy() unless we're going
  to be operating on > 0 bytes.
- Change 'error' event to 'wserror' to avoid EventEmitter throwing an error.
  • Loading branch information
pgriess committed Aug 8, 2010
1 parent 21bb2b1 commit 0f57020
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/websocket.js
Expand Up @@ -444,8 +444,13 @@ var WebSocket = function(url, proto) {
data = d;
} else {
var data2 = new buffer.Buffer(data.length + d.length);
data.copy(data2, 0, 0, data.length);
d.copy(data2, data.length, 0, d.length);

if (data.length) {
data.copy(data2, 0, 0, data.length);
}
if (data2.length) {
d.copy(data2, data.length, 0, d.length);
}

data = data2;
}
Expand All @@ -461,8 +466,23 @@ var WebSocket = function(url, proto) {
'actual=\'' + str2hex(actual) + '\''
);

self.emit('error', new Error('Invalid handshake from server'));
self.close();
process.nextTick(function() {
// XXX: Emit 'wserror' here, as 'error' is a reserved word in the
// EventEmitter world, and gets thrown.
self.emit(
'wserror',
new Error('Invalid handshake from server:' +
'expected \'' + str2hex(expected) + '\', ' +
'actual \'' + str2hex(actual) + '\''
)
);

if (self.onerror) {
self.onerror();
}

self.close();
});
}

// Un-register our data handler and add the one to be used
Expand Down

0 comments on commit 0f57020

Please sign in to comment.