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

@ -1,9 +1,9 @@
var InspectionDecision = {
var InspectionDecision = Object.freeze({
DoNothing: 'doNothing',
EndOperation: 'endOperation',
Retry: 'retry',
Reconnect: 'reconnect',
Subscribed: 'subscribed'
};
});
module.exports = InspectionDecision;

View File

@ -5,4 +5,4 @@ function InspectionResult(decision, description, tcpEndPoint, secureTcpEndPoint)
this.secureTcpEndPoint = secureTcpEndPoint || null;
}
module.exports = InspectionResult;
module.exports = InspectionResult;

View File

@ -1,9 +1,7 @@
var ClientMessage = require('../messages/clientMessage');
var SliceReadStatus = require('../sliceReadStatus');
module.exports = {};
module.exports.convert = function(code) {
exports.convert = function(code) {
switch(code) {
case ClientMessage.ReadStreamEventsCompleted.ReadStreamResult.Success:
return SliceReadStatus.Success;

View File

@ -1,4 +1,4 @@
const TcpCommand = {
const TcpCommand = Object.freeze({
HeartbeatRequestCommand: 0x01,
HeartbeatResponseCommand: 0x02,
@ -73,16 +73,18 @@ const TcpCommand = {
Authenticated: 0xF3,
NotAuthenticated: 0xF4,
IdentifyClient: 0xF5,
ClientIdentified: 0xF6
};
ClientIdentified: 0xF6,
getName: function(v) {
return _reverseLookup[v];
}
});
var _reverseLookup = {};
for(var n in TcpCommand) {
if (n === 'getName') continue;
var v = TcpCommand[n];
_reverseLookup[v] = n;
}
module.exports = TcpCommand;
module.exports.getName = function(v) {
return _reverseLookup[v];
};
module.exports = TcpCommand;

View File

@ -1,6 +1,6 @@
const TcpFlags = {
const TcpFlags = Object.freeze({
None: 0x0,
Authenticated: 0x01
};
});
module.exports = TcpFlags;

View File

@ -19,8 +19,7 @@ function TcpPackage(command, flags, correlationId, login, password, data) {
}
TcpPackage.fromBufferSegment = function(data) {
if (data.length < MandatorySize)
throw new Error("ArraySegment too short, length: " + data.length);
if (data.length < MandatorySize) throw new Error("ArraySegment too short, length: " + data.length);
var command = data.buffer[data.offset + CommandOffset];
var flags = data.buffer[data.offset + FlagsOffset];
@ -32,13 +31,15 @@ TcpPackage.fromBufferSegment = function(data) {
if ((flags & TcpFlags.Authenticated) !== 0)
{
var loginLen = data.buffer[data.offset + AuthOffset];
if (AuthOffset + 1 + loginLen + 1 >= data.count)
throw new Error("Login length is too big, it doesn't fit into TcpPackage.");
if (AuthOffset + 1 + loginLen + 1 >= data.count) {
throw new Error("Login length is too big, it doesn't fit into TcpPackage.");
}
login = data.buffer.toString('utf8', data.offset + AuthOffset + 1, data.offset + AuthOffset + 1 + loginLen);
var passLen = data.buffer[data.offset + AuthOffset + 1 + loginLen];
if (AuthOffset + 1 + loginLen + 1 + passLen > data.count)
throw new Error("Password length is too big, it doesn't fit into TcpPackage.");
if (AuthOffset + 1 + loginLen + 1 + passLen > data.count) {
throw new Error("Password length is too big, it doesn't fit into TcpPackage.");
}
headerSize += 1 + loginLen + 1 + passLen;
pass = data.buffer.toString('utf8', data.offset + AuthOffset + 1 + loginLen + 1, data.offset + headerSize);
}
@ -64,8 +65,7 @@ TcpPackage.prototype.asBuffer = function() {
res[AuthOffset + 1 + loginBytes.length] = passwordBytes.length;
passwordBytes.copy(res, AuthOffset + 2 + loginBytes.length);
if (this.data)
this.data.copyTo(res, res.length - this.data.count);
if (this.data) this.data.copyTo(res, res.length - this.data.count);
return res;
} else {
@ -73,8 +73,7 @@ TcpPackage.prototype.asBuffer = function() {
res[CommandOffset] = this.command;
res[FlagsOffset] = this.flags;
guidParse.parse(this.correlationId, res, CorrelationOffset);
if (this.data)
this.data.copyTo(res, AuthOffset);
if (this.data) this.data.copyTo(res, AuthOffset);
return res;
}
};