Skip to content

Commit

Permalink
[major] Remove Sender inheritance from EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Oct 13, 2016
1 parent 98a9121 commit bc52b0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
27 changes: 12 additions & 15 deletions lib/Sender.js
Expand Up @@ -6,24 +6,22 @@

'use strict';

const EventEmitter = require('events');
const ErrorCodes = require('./ErrorCodes');
const bufferUtil = require('./BufferUtil').BufferUtil;
const PerMessageDeflate = require('./PerMessageDeflate');

/**
* HyBi Sender implementation, Inherits from EventEmitter.
*/
class Sender extends EventEmitter {
class Sender {
constructor (socket, extensions) {
super();

this._socket = socket;
this.extensions = extensions || {};
this.firstFragment = true;
this.compress = false;
this.messageHandlers = [];
this.processing = false;
this.onerror = null;
}

/**
Expand All @@ -32,9 +30,8 @@ class Sender extends EventEmitter {
* @api public
*/
close (code, data, mask, cb) {
if (typeof code !== 'undefined') {
if (typeof code !== 'number' ||
!ErrorCodes.isValidErrorCode(code)) throw new Error('first argument must be a valid error code number');
if (code !== undefined && (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code))) {
throw new Error('first argument must be a valid error code number');
}
code = code || 1000;
var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0));
Expand Down Expand Up @@ -148,7 +145,7 @@ class Sender extends EventEmitter {
this.applyExtensions(data, finalFragment, this.compress, (err, data) => {
if (err) {
if (cb) cb(err);
else this.emit('error', err);
else this.onerror(err);
return;
}
this.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
Expand All @@ -167,7 +164,7 @@ class Sender extends EventEmitter {
if (!data) {
var buff = [opcode | (finalFragment ? 0x80 : 0), 0 | (maskData ? 0x80 : 0)]
.concat(maskData ? [0, 0, 0, 0] : []);
sendFramedData.call(this, new Buffer(buff), null, cb);
sendFramedData(this, new Buffer(buff), null, cb);
return;
}

Expand Down Expand Up @@ -233,7 +230,7 @@ class Sender extends EventEmitter {
data.copy(outputBuffer, dataOffset);
}
}
sendFramedData.call(this, outputBuffer, mergeBuffers ? null : data, cb);
sendFramedData(this, outputBuffer, mergeBuffers ? null : data, cb);
}

/**
Expand Down Expand Up @@ -307,16 +304,16 @@ function getRandomMask () {
]);
}

function sendFramedData (outputBuffer, data, cb) {
function sendFramedData (sender, outputBuffer, data, cb) {
try {
if (data) {
this._socket.write(outputBuffer, 'binary');
this._socket.write(data, 'binary', cb);
sender._socket.write(outputBuffer, 'binary');
sender._socket.write(data, 'binary', cb);
} else {
this._socket.write(outputBuffer, 'binary', cb);
sender._socket.write(outputBuffer, 'binary', cb);
}
} catch (e) {
if (cb) cb(e);
else this.emit('error', e);
else sender.onerror(e);
}
}
7 changes: 3 additions & 4 deletions lib/WebSocket.js
Expand Up @@ -850,10 +850,10 @@ function establishConnection (ReceiverClass, SenderClass, socket, upgradeHead) {

// finalize the client
this._sender = new SenderClass(socket, this.extensions);
this._sender.on('error', (error) => {
this._sender.onerror = (error) => {
this.close(1002, '');
this.emit('error', error);
});
};

this.readyState = WebSocket.OPEN;
this.emit('open');
Expand Down Expand Up @@ -939,8 +939,7 @@ function cleanupWebsocketResources (error) {
}

if (this._sender) {
this._sender.removeAllListeners();
this._sender = null;
this._sender = this._sender.onerror = null;
}

if (this._receiver) {
Expand Down

0 comments on commit bc52b0c

Please sign in to comment.