Initial commit

This commit is contained in:
Nicolas Dextraze
2016-03-09 12:46:15 -08:00
parent 3a5a4111c1
commit 9be67bf7c7
54 changed files with 5172 additions and 1 deletions

View File

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

View File

@ -0,0 +1,8 @@
function InspectionResult(decision, description, tcpEndPoint, secureTcpEndPoint) {
this.decision = decision;
this.description = description;
this.tcpEndPoint = tcpEndPoint || null;
this.secureTcpEndPoint = secureTcpEndPoint || null;
}
module.exports = InspectionResult;

View File

@ -0,0 +1,17 @@
var ClientMessage = require('../messages/clientMessage');
var SliceReadStatus = require('../sliceReadStatus');
module.exports = {};
module.exports.convert = function(code) {
switch(code) {
case ClientMessage.ReadStreamEventsCompleted.ReadStreamResult.Success:
return SliceReadStatus.Success;
case ClientMessage.ReadStreamEventsCompleted.ReadStreamResult.NoStream:
return SliceReadStatus.StreamNotFound;
case ClientMessage.ReadStreamEventsCompleted.ReadStreamResult.StreamDeleted:
return SliceReadStatus.StreamDeleted;
default:
throw new Error('Invalid code: ' + code)
}
};

View File

@ -0,0 +1,75 @@
const TcpCommand = {
HeartbeatRequestCommand: 0x01,
HeartbeatResponseCommand: 0x02,
Ping: 0x03,
Pong: 0x04,
PrepareAck: 0x05,
CommitAck: 0x06,
SlaveAssignment: 0x07,
CloneAssignment: 0x08,
SubscribeReplica: 0x10,
ReplicaLogPositionAck: 0x11,
CreateChunk: 0x12,
RawChunkBulk: 0x13,
DataChunkBulk: 0x14,
ReplicaSubscriptionRetry: 0x15,
ReplicaSubscribed: 0x16,
// CLIENT COMMANDS
// CreateStream: 0x80,
// CreateStreamCompleted: 0x81,
WriteEvents: 0x82,
WriteEventsCompleted: 0x83,
TransactionStart: 0x84,
TransactionStartCompleted: 0x85,
TransactionWrite: 0x86,
TransactionWriteCompleted: 0x87,
TransactionCommit: 0x88,
TransactionCommitCompleted: 0x89,
DeleteStream: 0x8A,
DeleteStreamCompleted: 0x8B,
ReadEvent: 0xB0,
ReadEventCompleted: 0xB1,
ReadStreamEventsForward: 0xB2,
ReadStreamEventsForwardCompleted: 0xB3,
ReadStreamEventsBackward: 0xB4,
ReadStreamEventsBackwardCompleted: 0xB5,
ReadAllEventsForward: 0xB6,
ReadAllEventsForwardCompleted: 0xB7,
ReadAllEventsBackward: 0xB8,
ReadAllEventsBackwardCompleted: 0xB9,
SubscribeToStream: 0xC0,
SubscriptionConfirmation: 0xC1,
StreamEventAppeared: 0xC2,
UnsubscribeFromStream: 0xC3,
SubscriptionDropped: 0xC4,
ScavengeDatabase: 0xD0,
ScavengeDatabaseCompleted: 0xD1,
BadRequest: 0xF0,
NotHandled: 0xF1,
Authenticate: 0xF2,
Authenticated: 0xF3,
NotAuthenticated: 0xF4
};
var _reverseLookup = {};
for(var n in TcpCommand) {
var v = TcpCommand[n];
_reverseLookup[v] = n;
}
module.exports = TcpCommand;
module.exports.getName = function(v) {
return _reverseLookup[v];
};

View File

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

View File

@ -0,0 +1,86 @@
var uuid = require('uuid');
var createBufferSegment = require('../common/bufferSegment');
var TcpFlags = require('./tcpFlags');
const CommandOffset = 0;
const FlagsOffset = CommandOffset + 1;
const CorrelationOffset = FlagsOffset + 1;
const AuthOffset = CorrelationOffset + 16;
const MandatorySize = AuthOffset;
function TcpPackage(command, flags, correlationId, login, password, data) {
this.command = command;
this.flags = flags;
this.correlationId = correlationId;
this.login = login || null;
this.password = password || null;
this.data = data || null;
}
TcpPackage.fromBufferSegment = function(data) {
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];
var correlationId = uuid.unparse(data.buffer, data.offset + CorrelationOffset);
var headerSize = MandatorySize;
var login = null, pass = null;
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.");
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.");
headerSize += 1 + loginLen + 1 + passLen;
pass = data.buffer.toString('utf8', data.offset + AuthOffset + 1 + loginLen + 1, data.offset + headerSize);
}
return new TcpPackage(
command, flags, correlationId, login, pass,
createBufferSegment(data.buffer, data.offset + headerSize, data.count - headerSize));
};
TcpPackage.prototype.asBuffer = function() {
if ((this.flags & TcpFlags.Authenticated) != 0) {
var loginBytes = new Buffer(this.login);
if (loginBytes.length > 255) throw new Error("Login serialized length should be less than 256 bytes.");
var passwordBytes = new Buffer(this.password);
if (passwordBytes.length > 255) throw new Error("Password serialized length should be less than 256 bytes.");
var res = new Buffer(MandatorySize + 2 + loginBytes.length + passwordBytes.length + (this.data ? this.data.count : 0));
res[CommandOffset] = this.command;
res[FlagsOffset] = this.flags;
uuid.parse(this.correlationId, res, CorrelationOffset);
res[AuthOffset] = loginBytes.length;
loginBytes.copy(res, AuthOffset + 1);
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);
return res;
} else {
var res = new Buffer(MandatorySize + (this.data ? this.data.count : 0));
res[CommandOffset] = this.command;
res[FlagsOffset] = this.flags;
uuid.parse(this.correlationId, res, CorrelationOffset);
if (this.data)
this.data.copyTo(res, AuthOffset);
return res;
}
};
TcpPackage.prototype.asBufferSegment = function() {
return createBufferSegment(this.asBuffer());
};
module.exports = TcpPackage;

View File

@ -0,0 +1,9 @@
function UserCredentials(username, password) {
if (!username || username === '') throw new TypeError("username must be a non-empty string.");
if (!password || password === '') throw new TypeError("password must be a non-empty string.");
this.username = username;
this.password = password;
}
module.exports = UserCredentials;