From 6cdf9a64a60c41aaaf4c80e55606aa58444c2b8f Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Wed, 16 Mar 2016 23:18:48 -0700 Subject: [PATCH] Use Long for int64 --- package.json | 5 ++- .../startTransactionOperation.js | 2 +- src/eventStoreTransaction.js | 11 +++--- src/results.js | 39 ++++++++----------- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 9f959f1..0245600 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,12 @@ }, "homepage": "https://github.com/nicdex/eventstore-node#readme", "dependencies": { + "long": "^3.0.3", "protobufjs": "^5.0.1", "uuid": "^2.0.1" }, - "engines": { "node": ">=0.12" }, + "engines": { + "node": ">=0.12" + }, "engineStrict": true } diff --git a/src/clientOperations/startTransactionOperation.js b/src/clientOperations/startTransactionOperation.js index 81b851e..57b4173 100644 --- a/src/clientOperations/startTransactionOperation.js +++ b/src/clientOperations/startTransactionOperation.js @@ -56,7 +56,7 @@ StartTransactionOperation.prototype._inspectResponse = function(response) { }; StartTransactionOperation.prototype._transformResponse = function(response) { - return new EventStoreTransaction(results.toNumber(response.transaction_id), this.userCredentials, this._parentConnection); + return new EventStoreTransaction(response.transaction_id, this.userCredentials, this._parentConnection); }; StartTransactionOperation.prototype.toString = function() { diff --git a/src/eventStoreTransaction.js b/src/eventStoreTransaction.js index a3c9d77..388b494 100644 --- a/src/eventStoreTransaction.js +++ b/src/eventStoreTransaction.js @@ -12,12 +12,13 @@ function EventStoreTransaction(transactionId, userCredentials, connection) { this._isCommitted = false; this._isRolledBack = false; + + Object.defineProperties(this, { + transactionId: { + enumerable: true, get: function() { return this._transactionId; } + } + }); } -Object.defineProperty(EventStoreTransaction.prototype, 'transactionId', { - get: function() { - return this._transactionId; - } -}); /** * Commit (async) diff --git a/src/results.js b/src/results.js index 66e0778..205de5c 100644 --- a/src/results.js +++ b/src/results.js @@ -1,45 +1,41 @@ var util = require('util'); var uuid = require('uuid'); +var Long = require('long'); var ensure = require('./common/utils/ensure'); -function toNumber(obj) { - if (typeof obj === 'number') - return obj; - if (typeof obj !== 'object') - throw new TypeError(util.format("'%s' is not a number.", obj)); - if (!obj.hasOwnProperty('low') || !obj.hasOwnProperty('high') || !obj.hasOwnProperty('unsigned')) - throw new Error("Invalid number."); - return (obj.low + (obj.high * 0xffffffff)); -} - /** - * @param {!number} commitPosition - * @param {!number} preparePosition + * @param {!number|!Long} commitPosition + * @param {!number|!Long} preparePosition * @constructor - * @property {!number} commitPosition - * @property {!number} preparePosition + * @property {!Long} commitPosition + * @property {!Long} preparePosition */ function Position(commitPosition, preparePosition) { + ensure.notNull(commitPosition, "commitPosition"); + ensure.notNull(preparePosition, "preparePosition"); + commitPosition = Long.fromValue(commitPosition); + preparePosition = Long.fromValue(preparePosition); + Object.defineProperties(this, { commitPosition: { - enumerable: true, value: toNumber(commitPosition) + enumerable: true, value: commitPosition }, preparePosition: { - enumerable: true, value: toNumber(preparePosition) + enumerable: true, value: preparePosition } }); } Position.prototype.compareTo = function(other) { - if (this.commitPosition < other.commitPosition || (this.commitPosition === other.commitPosition && this.preparePosition < other.preparePosition)) + if (this.commitPosition.lt(other.commitPosition) || (this.commitPosition.eq(other.commitPosition)&& this.preparePosition.lt(other.preparePosition))) return -1; - if (this.commitPosition > other.commitPosition || (this.commitPosition === other.commitPosition && this.preparePosition > other.preparePosition)) + if (this.commitPosition.gt(other.commitPosition) || (this.commitPosition.eq(other.commitPosition) && this.preparePosition.gt(other.preparePosition))) return 1; return 0; }; Position.prototype.toString = function() { - return util.format("%d/%d", this.commitPosition, this.preparePosition); + return [this.commitPosition.toString(), this.preparePosition.toString()].join("/"); }; @@ -69,8 +65,8 @@ function RecordedEvent(ev) { eventNumber: {enumerable: true, value: ev.event_number}, eventType: {enumerable: true, value: ev.event_type}, //Javascript doesn't have .Net precision for time, so we use created_epoch for created - created: {enumerable: true, value: new Date(ev.created_epoch || 0)}, - createdEpoch: {enumerable: true, value: ev.created_epoch ? toNumber(ev.created_epoch) : 0}, + created: {enumerable: true, value: new Date(ev.created_epoch ? ev.created_epoch.toInt() : 0)}, + createdEpoch: {enumerable: true, value: ev.created_epoch ? ev.created_epoch.toInt() : 0}, data: {enumerable: true, value: ev.data ? ev.data.toBuffer() : new Buffer(0)}, metadata: {enumerable: true, value: ev.metadata ? ev.metadata.toBuffer() : new Buffer(0)}, isJson: {enumerable: true, value: ev.data_content_type == 1} @@ -338,7 +334,6 @@ function PersistentSubscriptionDeleteResult(status) { // Exports Constructors module.exports.Position = Position; -module.exports.toNumber = toNumber; module.exports.ResolvedEvent = ResolvedEvent; module.exports.EventReadStatus = EventReadStatus; module.exports.EventReadResult = EventReadResult;