Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Sender inheritance from EventEmitter #861

Merged
merged 1 commit into from Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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