Add Object.freeze on private enums

Improve code readability
This commit is contained in:
2018-07-09 10:27:12 -07:00
parent c3a63ff8b7
commit 215708014c
55 changed files with 288 additions and 327 deletions

View File

@@ -31,8 +31,9 @@ LengthPrefixMessageFramer.prototype._parse = function(bytes) {
++this._headerBytes;
if (this._headerBytes === HeaderLength)
{
if (this._packageLength <= 0 || this._packageLength > this._maxPackageSize)
if (this._packageLength <= 0 || this._packageLength > this._maxPackageSize) {
throw new Error(["Package size is out of bounds: ", this._packageLength, "(max: ", this._maxPackageSize, "."].join(''));
}
this._messageBuffer = new Buffer(this._packageLength);
}
@@ -46,8 +47,9 @@ LengthPrefixMessageFramer.prototype._parse = function(bytes) {
if (this._bufferIndex === this._packageLength)
{
if (this._receivedHandler !== null)
if (this._receivedHandler !== null) {
this._receivedHandler(createBufferSegment(this._messageBuffer, 0, this._bufferIndex));
}
this.reset();
}
}
@@ -69,4 +71,4 @@ LengthPrefixMessageFramer.prototype.registerMessageArrivedCallback = function(ha
};
module.exports = LengthPrefixMessageFramer;
module.exports = LengthPrefixMessageFramer;

View File

@@ -62,14 +62,11 @@ TcpConnection.prototype._trySend = function() {
while(sendPiece = this._sendQueue.shift()) {
buffers.push(sendPiece);
bytes += sendPiece.length;
if (bytes > MaxSendPacketSize)
break;
if (bytes > MaxSendPacketSize) break;
}
var joinedBuffers = Buffer.concat(buffers, bytes);
if (!this._socket.write(joinedBuffers)) {
return;
}
if (!this._socket.write(joinedBuffers)) return;
setImmediate(this._trySend.bind(this));
};
@@ -97,8 +94,7 @@ TcpConnection.prototype.receive = function(cb) {
};
TcpConnection.prototype._tryDequeueReceivedData = function() {
if (this._receiveCallback === null || this._receiveQueue.length === 0)
return;
if (this._receiveCallback === null || this._receiveQueue.length === 0) return;
var res = [];
while(this._receiveQueue.length > 0) {
@@ -112,8 +108,9 @@ TcpConnection.prototype._tryDequeueReceivedData = function() {
callback(this, res);
var bytes = 0;
for(var i=0;i<res.length;i++)
for(var i=0;i<res.length;i++) {
bytes += res[i].count;
}
//this._pendingReceivedBytes -= bytes;
};
@@ -132,8 +129,7 @@ TcpConnection.prototype._closeInternal = function(err, reason) {
this._socket = null;
}
if (this._onConnectionClosed !== null)
this._onConnectionClosed(this, err);
if (this._onConnectionClosed !== null) this._onConnectionClosed(this, err);
};
TcpConnection.createConnectingConnection = function(
@@ -155,8 +151,7 @@ TcpConnection.createConnectingConnection = function(
socket.on('connect', function() {
socket.removeListener('error', onError);
connection._initSocket(socket);
if (onConnectionEstablished)
onConnectionEstablished(connection);
if (onConnectionEstablished) onConnectionEstablished(connection);
});
return connection;
};

View File

@@ -1,5 +1,4 @@
var util = require('util');
var uuid = require('uuid');
var LengthPrefixMessageFramer = require('./lengthPrefixMessageFramer');
var TcpConnection = require('./tcpConnection');
@@ -57,8 +56,7 @@ function TcpPackageConnection(
},
function (conn, had_error) {
var error;
if (had_error)
error = new Error('transmission error.');
if (had_error) error = new Error('transmission error.');
log.debug("TcpPackageConnection: connection [%j, L%j, %s] was closed %s", conn.remoteEndPoint, conn.localEndPoint,
connectionId, had_error ? "with error: " + error + "." : "cleanly.");
@@ -120,27 +118,23 @@ TcpPackageConnection.prototype._incomingMessageArrived = function(data) {
var message = util.format("TcpPackageConnection: [%j, L%j, %s] ERROR for %s. Connection will be closed.",
this.remoteEndPoint, this.localEndPoint, this._connectionId,
valid ? TcpCommand.getName(pkg.command) : "<invalid package>");
if (this._onError !== null)
this._onError(this, e);
if (this._onError !== null) this._onError(this, e);
this._log.debug(e, message);
}
};
TcpPackageConnection.prototype.startReceiving = function() {
if (this._connection === null)
throw new Error("Failed connection.");
if (this._connection === null) throw new Error("Failed connection.");
this._connection.receive(this._onRawDataReceived.bind(this));
};
TcpPackageConnection.prototype.enqueueSend = function(pkg) {
if (this._connection === null)
throw new Error("Failed connection.");
if (this._connection === null) throw new Error("Failed connection.");
this._connection.enqueueSend(this._framer.frameData(pkg.asBufferSegment()));
};
TcpPackageConnection.prototype.close = function(reason) {
if (this._connection === null)
throw new Error("Failed connection.");
if (this._connection === null) throw new Error("Failed connection.");
this._connection.close(reason);
};