Fix GUID bytes ordering

This commit is contained in:
2017-09-02 15:30:19 -07:00
parent 7c94e26055
commit a8081a280a
9 changed files with 454 additions and 122 deletions

View File

@ -1,5 +1,5 @@
var util = require('util');
var uuidParse = require('uuid-parse');
var guidParse = require('../common/guid-parse');
var TcpCommand = require('../systemData/tcpCommand');
var InspectionDecision = require('../systemData/inspectionDecision');
@ -26,7 +26,7 @@ util.inherits(AppendToStreamOperation, OperationBase);
AppendToStreamOperation.prototype._createRequestDto = function() {
var dtos = this._events.map(function(ev) {
var eventId = new Buffer(uuidParse.parse(ev.eventId));
var eventId = new Buffer(guidParse.parse(ev.eventId));
return {
eventId: eventId, eventType: ev.type,
dataContentType: ev.isJson ? 1 : 0, metadataContentType: 0,

View File

@ -1,5 +1,5 @@
var util = require('util');
var uuidParse = require('uuid-parse');
var guidParse = require('../common/guid-parse');
var SubscriptionOperation = require('./subscriptionOperation');
var ClientMessage = require('../messages/clientMessage');
@ -92,7 +92,7 @@ ConnectToPersistentSubscriptionOperation.prototype.notifyEventsProcessed = funct
var dto = new ClientMessage.PersistentSubscriptionAckEvents({
subscriptionId: this._subscriptionId,
processedEventIds: processedEvents.map(function (x) {
return new Buffer(uuidParse.parse(x));
return new Buffer(guidParse.parse(x));
})
});
@ -110,7 +110,7 @@ ConnectToPersistentSubscriptionOperation.prototype.notifyEventsFailed = function
ensure.notNull(reason, "reason");
var dto = new ClientMessage.PersistentSubscriptionNakEvents({
subscriptionId: this._subscriptionId,
processedEventIds: processedEvents.map(function(x) { return new Buffer(uuidParse.parse(x)); }),
processedEventIds: processedEvents.map(function(x) { return new Buffer(guidParse.parse(x)); }),
message: reason,
action: action
});

View File

@ -1,5 +1,5 @@
var util = require('util');
var uuidParse = require('uuid-parse');
var guidParse = require('../common/guid-parse');
var TcpCommand = require('../systemData/tcpCommand');
var InspectionDecision = require('../systemData/inspectionDecision');
@ -22,7 +22,7 @@ util.inherits(TransactionalWriteOperation, OperationBase);
TransactionalWriteOperation.prototype._createRequestDto = function() {
var dtos = this._events.map(function(ev) {
var eventId = new Buffer(uuidParse.parse(ev.eventId));
var eventId = new Buffer(guidParse.parse(ev.eventId));
return {
eventId: eventId, eventType: ev.type,
dataContentType: ev.isJson ? 1 : 0, metadataContentType: 0,

48
src/common/guid-parse.js Normal file
View File

@ -0,0 +1,48 @@
'use strict';
// Maps for number <-> hex string conversion
var _byteToHex = [];
var _hexToByte = {};
for (var i = 0; i < 256; i++) {
_byteToHex[i] = (i + 0x100).toString(16).substr(1);
_hexToByte[_byteToHex[i]] = i;
}
// **`parse()` - Parse a UUID into it's component bytes**
function parse(s, buf, offset) {
const i = (buf && offset) || 0;
var ii = 0;
if (buf) buf.fill(0, i, i + 16);
buf = buf || new Buffer(16);
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
if (ii < 4) {
buf[i + (3 - ii++)] = _hexToByte[oct]
} else if (ii < 6) {
buf[i + 4 + (5 - ii++)] = _hexToByte[oct]
} else if (ii < 8) {
buf[i + 6 + (7 - ii++)] = _hexToByte[oct]
} else if (ii < 16) { // Don't overflow!
buf[i + ii++] = _hexToByte[oct];
}
});
return buf;
}
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
function unparse(buf, offset) {
var i = offset || 0;
const bth = _byteToHex;
return bth[buf[i+3]] + bth[buf[i+2]] +
bth[buf[i+1]] + bth[buf[i+0]] + '-' +
bth[buf[i+5]] + bth[buf[i+4]] + '-' +
bth[buf[i+7]] + bth[buf[i+6]] + '-' +
bth[buf[i+8]] + bth[buf[i+9]] + '-' +
bth[buf[i+10]] + bth[buf[i+11]] +
bth[buf[i+12]] + bth[buf[i+13]] +
bth[buf[i+14]] + bth[buf[i+15]];
}
exports.parse = parse;
exports.unparse = unparse;

View File

@ -1,5 +1,4 @@
var util = require('util');
var uuidParse = require('uuid-parse');
var guidParse = require('./common/guid-parse');
var Long = require('long');
var ensure = require('./common/utils/ensure');
@ -54,7 +53,7 @@ Object.freeze(EventReadStatus);
*/
function RecordedEvent(ev) {
this.eventStreamId = ev.eventStreamId;
this.eventId = uuidParse.unparse(ev.eventId);
this.eventId = guidParse.unparse(ev.eventId);
this.eventNumber = ev.eventNumber;
this.eventType = ev.eventType;
this.created = new Date(ev.createdEpoch ? ev.createdEpoch.toNumber() : 0);

View File

@ -1,4 +1,4 @@
var uuidParse = require('uuid-parse');
var guidParse = require('../common/guid-parse');
var createBufferSegment = require('../common/bufferSegment');
var TcpFlags = require('./tcpFlags');
@ -25,7 +25,7 @@ TcpPackage.fromBufferSegment = function(data) {
var command = data.buffer[data.offset + CommandOffset];
var flags = data.buffer[data.offset + FlagsOffset];
var correlationId = uuidParse.unparse(data.buffer, data.offset + CorrelationOffset);
var correlationId = guidParse.unparse(data.buffer, data.offset + CorrelationOffset);
var headerSize = MandatorySize;
var login = null, pass = null;
@ -57,7 +57,7 @@ TcpPackage.prototype.asBuffer = function() {
var res = new Buffer(MandatorySize + 2 + loginBytes.length + passwordBytes.length + (this.data ? this.data.count : 0));
res[CommandOffset] = this.command;
res[FlagsOffset] = this.flags;
uuidParse.parse(this.correlationId, res, CorrelationOffset);
guidParse.parse(this.correlationId, res, CorrelationOffset);
res[AuthOffset] = loginBytes.length;
loginBytes.copy(res, AuthOffset + 1);
@ -72,7 +72,7 @@ TcpPackage.prototype.asBuffer = function() {
var res = new Buffer(MandatorySize + (this.data ? this.data.count : 0));
res[CommandOffset] = this.command;
res[FlagsOffset] = this.flags;
uuidParse.parse(this.correlationId, res, CorrelationOffset);
guidParse.parse(this.correlationId, res, CorrelationOffset);
if (this.data)
this.data.copyTo(res, AuthOffset);
return res;