2016-03-09 20:46:15 +00:00
|
|
|
var util = require('util');
|
2017-04-16 19:51:17 +00:00
|
|
|
var uuidParse = require('uuid-parse');
|
2016-03-17 06:18:48 +00:00
|
|
|
var Long = require('long');
|
2016-03-09 20:46:15 +00:00
|
|
|
var ensure = require('./common/utils/ensure');
|
|
|
|
|
|
|
|
/**
|
2016-11-07 06:58:11 +00:00
|
|
|
* @public
|
2016-03-17 06:18:48 +00:00
|
|
|
* @param {!number|!Long} commitPosition
|
|
|
|
* @param {!number|!Long} preparePosition
|
2016-03-09 20:46:15 +00:00
|
|
|
* @constructor
|
2016-03-17 06:18:48 +00:00
|
|
|
* @property {!Long} commitPosition
|
|
|
|
* @property {!Long} preparePosition
|
2016-03-09 20:46:15 +00:00
|
|
|
*/
|
|
|
|
function Position(commitPosition, preparePosition) {
|
2016-03-17 06:18:48 +00:00
|
|
|
ensure.notNull(commitPosition, "commitPosition");
|
|
|
|
ensure.notNull(preparePosition, "preparePosition");
|
2017-04-16 19:51:17 +00:00
|
|
|
this.commitPosition = Long.fromValue(commitPosition);
|
|
|
|
this.preparePosition = Long.fromValue(preparePosition);
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Position.prototype.compareTo = function(other) {
|
2016-03-17 06:18:48 +00:00
|
|
|
if (this.commitPosition.lt(other.commitPosition) || (this.commitPosition.eq(other.commitPosition)&& this.preparePosition.lt(other.preparePosition)))
|
2016-03-09 20:46:15 +00:00
|
|
|
return -1;
|
2016-03-17 06:18:48 +00:00
|
|
|
if (this.commitPosition.gt(other.commitPosition) || (this.commitPosition.eq(other.commitPosition) && this.preparePosition.gt(other.preparePosition)))
|
2016-03-09 20:46:15 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Position.prototype.toString = function() {
|
2016-03-17 06:18:48 +00:00
|
|
|
return [this.commitPosition.toString(), this.preparePosition.toString()].join("/");
|
2016-03-09 20:46:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const EventReadStatus = {
|
|
|
|
Success: 'success',
|
|
|
|
NotFound: 'notFound',
|
|
|
|
NoStream: 'noStream',
|
|
|
|
StreamDeleted: 'streamDeleted'
|
|
|
|
};
|
2017-04-16 19:51:17 +00:00
|
|
|
Object.freeze(EventReadStatus);
|
2016-03-09 20:46:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} ev
|
|
|
|
* @constructor
|
|
|
|
* @property {string} eventStreamId
|
|
|
|
* @property {string} eventId
|
|
|
|
* @property {number} eventNumber
|
|
|
|
* @property {string} eventType
|
|
|
|
* @property {number} createdEpoch
|
|
|
|
* @property {?Buffer} data
|
|
|
|
* @property {?Buffer} metadata
|
|
|
|
* @property {boolean} isJson
|
|
|
|
*/
|
|
|
|
function RecordedEvent(ev) {
|
2017-04-16 22:51:09 +00:00
|
|
|
this.eventStreamId = ev.eventStreamId;
|
|
|
|
this.eventId = uuidParse.unparse(ev.eventId);
|
|
|
|
this.eventNumber = ev.eventNumber;
|
|
|
|
this.eventType = ev.eventType;
|
|
|
|
this.created = new Date(ev.createdEpoch ? ev.createdEpoch.toNumber() : 0);
|
|
|
|
this.createdEpoch = ev.createdEpoch ? ev.createdEpoch.toNumber() : 0;
|
|
|
|
this.data = ev.data ? ev.data : new Buffer(0);
|
|
|
|
this.metadata = ev.metadata ? ev.metadata : new Buffer(0);
|
|
|
|
this.isJson = ev.dataContentType === 1;
|
2017-04-16 19:51:17 +00:00
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} ev
|
|
|
|
* @constructor
|
|
|
|
* @property {?RecordedEvent} event
|
|
|
|
* @property {?RecordedEvent} link
|
|
|
|
* @property {?RecordedEvent} originalEvent
|
|
|
|
* @property {boolean} isResolved
|
|
|
|
* @property {?Position} originalPosition
|
|
|
|
* @property {string} originalStreamId
|
|
|
|
* @property {number} originalEventNumber
|
|
|
|
*/
|
|
|
|
function ResolvedEvent(ev) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.event = ev.event === null ? null : new RecordedEvent(ev.event);
|
|
|
|
this.link = ev.link === null ? null : new RecordedEvent(ev.link);
|
|
|
|
this.originalEvent = this.link || this.event;
|
|
|
|
this.isResolved = this.link !== null && this.event !== null;
|
2017-04-16 22:51:09 +00:00
|
|
|
this.originalPosition = (ev.commitPosition && ev.preparePosition) ? new Position(ev.commitPosition, ev.preparePosition) : null;
|
2017-04-16 19:51:17 +00:00
|
|
|
this.originalStreamId = this.originalEvent && this.originalEvent.eventStreamId;
|
|
|
|
this.originalEventNumber = this.originalEvent && this.originalEvent.eventNumber;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} status
|
|
|
|
* @param {string} stream
|
|
|
|
* @param {number} eventNumber
|
|
|
|
* @param {object} event
|
|
|
|
* @constructor
|
|
|
|
* @property {string} status
|
|
|
|
* @property {string} stream
|
|
|
|
* @property {number} eventNumber
|
|
|
|
* @property {ResolvedEvent} event
|
|
|
|
*/
|
|
|
|
function EventReadResult(status, stream, eventNumber, event) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.status = status;
|
|
|
|
this.stream = stream;
|
|
|
|
this.eventNumber = eventNumber;
|
|
|
|
this.event = status === EventReadStatus.Success ? new ResolvedEvent(event) : null;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} nextExpectedVersion
|
|
|
|
* @param {Position} logPosition
|
|
|
|
* @constructor
|
|
|
|
* @property {number} nextExpectedVersion
|
|
|
|
* @property {Position} logPosition
|
|
|
|
*/
|
|
|
|
function WriteResult(nextExpectedVersion, logPosition) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.nextExpectedVersion = nextExpectedVersion;
|
|
|
|
this.logPosition = logPosition;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} status
|
|
|
|
* @param {string} stream
|
|
|
|
* @param {number} fromEventNumber
|
|
|
|
* @param {string} readDirection
|
|
|
|
* @param {object[]} events
|
|
|
|
* @param {number} nextEventNumber
|
|
|
|
* @param {number} lastEventNumber
|
|
|
|
* @param {boolean} isEndOfStream
|
|
|
|
* @constructor
|
|
|
|
* @property {string} status
|
|
|
|
* @property {string} stream
|
|
|
|
* @property {number} fromEventNumber
|
|
|
|
* @property {string} readDirection
|
|
|
|
* @property {ResolvedEvent[]} events
|
|
|
|
* @property {number} nextEventNumber
|
|
|
|
* @property {number} lastEventNumber
|
|
|
|
* @property {boolean} isEndOfStream
|
|
|
|
*/
|
|
|
|
function StreamEventsSlice(
|
|
|
|
status, stream, fromEventNumber, readDirection, events, nextEventNumber, lastEventNumber, isEndOfStream
|
|
|
|
) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.status = status;
|
|
|
|
this.stream = stream;
|
|
|
|
this.fromEventNumber = fromEventNumber;
|
|
|
|
this.readDirection = readDirection;
|
|
|
|
this.events = events ? events.map(function(ev) { return new ResolvedEvent(ev); }) : [];
|
|
|
|
this.nextEventNumber = nextEventNumber;
|
|
|
|
this.lastEventNumber = lastEventNumber;
|
|
|
|
this.isEndOfStream = isEndOfStream;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} readDirection
|
|
|
|
* @param {Position} fromPosition
|
|
|
|
* @param {Position} nextPosition
|
|
|
|
* @param {ResolvedEvent[]} events
|
|
|
|
* @constructor
|
|
|
|
* @property {string} readDirection
|
|
|
|
* @property {Position} fromPosition
|
|
|
|
* @property {Position} nextPosition
|
|
|
|
* @property {ResolvedEvent[]} events
|
|
|
|
*/
|
|
|
|
function AllEventsSlice(readDirection, fromPosition, nextPosition, events) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.readDirection = readDirection;
|
|
|
|
this.fromPosition = fromPosition;
|
|
|
|
this.nextPosition = nextPosition;
|
|
|
|
this.events = events ? events.map(function(ev){ return new ResolvedEvent(ev); }) : [];
|
|
|
|
this.isEndOfStream = events === null || events.length === 0;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Position} logPosition
|
|
|
|
* @constructor
|
|
|
|
* @property {Position} logPosition
|
|
|
|
*/
|
|
|
|
function DeleteResult(logPosition) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.logPosition = logPosition;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} stream
|
|
|
|
* @param {boolean} isStreamDeleted
|
|
|
|
* @param {number} metastreamVersion
|
|
|
|
* @param {object} streamMetadata
|
|
|
|
* @constructor
|
|
|
|
* @property {string} stream
|
|
|
|
* @property {boolean} isStreamDeleted
|
|
|
|
* @property {number} metastreamVersion
|
|
|
|
* @property {object} streamMetadata
|
|
|
|
*/
|
|
|
|
function RawStreamMetadataResult(stream, isStreamDeleted, metastreamVersion, streamMetadata) {
|
|
|
|
ensure.notNullOrEmpty(stream);
|
2017-04-16 19:51:17 +00:00
|
|
|
this.stream = stream;
|
|
|
|
this.isStreamDeleted = isStreamDeleted;
|
|
|
|
this.metastreamVersion = metastreamVersion;
|
|
|
|
this.streamMetadata = streamMetadata;
|
|
|
|
Object.freeze(this);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 06:57:39 +00:00
|
|
|
const PersistentSubscriptionCreateStatus = {
|
|
|
|
Success: 'success',
|
|
|
|
NotFound: 'notFound',
|
|
|
|
Failure: 'failure'
|
|
|
|
};
|
2017-04-16 19:51:17 +00:00
|
|
|
Object.freeze(PersistentSubscriptionCreateStatus);
|
2016-03-11 06:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} status
|
|
|
|
* @constructor
|
|
|
|
* @property {string} status
|
|
|
|
*/
|
|
|
|
function PersistentSubscriptionCreateResult(status) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.status = status;
|
|
|
|
Object.freeze(this);
|
2016-03-11 06:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PersistentSubscriptionUpdateStatus = {
|
|
|
|
Success: 'success',
|
|
|
|
NotFound: 'notFound',
|
|
|
|
Failure: 'failure',
|
|
|
|
AccessDenied: 'accessDenied'
|
|
|
|
};
|
2017-04-16 19:51:17 +00:00
|
|
|
Object.freeze(PersistentSubscriptionUpdateStatus);
|
2016-03-11 06:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} status
|
|
|
|
* @constructor
|
|
|
|
* @property {string} status
|
|
|
|
*/
|
|
|
|
function PersistentSubscriptionUpdateResult(status) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.status = status;
|
|
|
|
Object.freeze(this);
|
2016-03-11 06:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PersistentSubscriptionDeleteStatus = {
|
|
|
|
Success: 'success',
|
|
|
|
Failure: 'failure'
|
|
|
|
};
|
2017-04-16 19:51:17 +00:00
|
|
|
Object.freeze(PersistentSubscriptionDeleteStatus);
|
2016-03-11 06:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} status
|
|
|
|
* @constructor
|
|
|
|
* @property {string} status
|
|
|
|
*/
|
|
|
|
function PersistentSubscriptionDeleteResult(status) {
|
2017-04-16 19:51:17 +00:00
|
|
|
this.status = status;
|
|
|
|
Object.freeze(this);
|
2016-03-11 06:57:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 20:46:15 +00:00
|
|
|
// Exports Constructors
|
|
|
|
module.exports.Position = Position;
|
|
|
|
module.exports.ResolvedEvent = ResolvedEvent;
|
|
|
|
module.exports.EventReadStatus = EventReadStatus;
|
|
|
|
module.exports.EventReadResult = EventReadResult;
|
|
|
|
module.exports.WriteResult = WriteResult;
|
|
|
|
module.exports.StreamEventsSlice = StreamEventsSlice;
|
|
|
|
module.exports.AllEventsSlice = AllEventsSlice;
|
|
|
|
module.exports.DeleteResult = DeleteResult;
|
2016-03-11 06:57:39 +00:00
|
|
|
module.exports.RawStreamMetadataResult = RawStreamMetadataResult;
|
|
|
|
module.exports.PersistentSubscriptionCreateResult = PersistentSubscriptionCreateResult;
|
|
|
|
module.exports.PersistentSubscriptionCreateStatus = PersistentSubscriptionCreateStatus;
|
|
|
|
module.exports.PersistentSubscriptionUpdateResult = PersistentSubscriptionUpdateResult;
|
|
|
|
module.exports.PersistentSubscriptionUpdateStatus = PersistentSubscriptionUpdateStatus;
|
|
|
|
module.exports.PersistentSubscriptionDeleteResult = PersistentSubscriptionDeleteResult;
|
|
|
|
module.exports.PersistentSubscriptionDeleteStatus = PersistentSubscriptionDeleteStatus;
|