node-eventstore-client/src/clientOperations/connectToPersistentSubscriptionOperation.js

128 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-03-11 06:57:39 +00:00
var util = require('util');
var uuidParse = require('uuid-parse');
2016-03-11 06:57:39 +00:00
var SubscriptionOperation = require('./subscriptionOperation');
var ClientMessage = require('../messages/clientMessage');
var TcpCommand = require('../systemData/tcpCommand');
var TcpFlags = require('../systemData/tcpFlags');
var TcpPackage = require('../systemData/tcpPackage');
var createBufferSegment = require('../common/bufferSegment');
var InspectionResult = require('./../systemData/inspectionResult');
var InspectionDecision = require('../systemData/inspectionDecision');
var results = require('../results');
var SubscriptionDropReason = require('../subscriptionDropReason');
var PersistentEventStoreSubscription = require('../persistentEventStoreSubscription');
var ensure = require('../common/utils/ensure');
function ConnectToPersistentSubscriptionOperation(
log, cb, groupName, bufferSize, streamId, userCredentials, eventAppeared, subscriptionDropped,
verboseLogging, getConnection
) {
SubscriptionOperation.call(this, log, cb, streamId, false, userCredentials, eventAppeared, subscriptionDropped, verboseLogging, getConnection);
this._groupName = groupName;
this._bufferSize = bufferSize;
this._subscriptionId = null;
}
util.inherits(ConnectToPersistentSubscriptionOperation, SubscriptionOperation);
ConnectToPersistentSubscriptionOperation.prototype._createSubscriptionPackage = function() {
2017-04-16 22:51:09 +00:00
var dto = new ClientMessage.ConnectToPersistentSubscription({
subscriptionId: this._groupName,
eventStreamId: this._streamId,
allowedInFlightMessages: this._bufferSize
});
2016-03-11 06:57:39 +00:00
return new TcpPackage(TcpCommand.ConnectToPersistentSubscription,
this._userCredentials !== null ? TcpFlags.Authenticated : TcpFlags.None,
this._correlationId,
this._userCredentials !== null ? this._userCredentials.username : null,
this._userCredentials !== null ? this._userCredentials.password : null,
2017-04-16 22:51:09 +00:00
createBufferSegment(ClientMessage.ConnectToPersistentSubscription.encode(dto).finish()));
2016-03-11 06:57:39 +00:00
};
ConnectToPersistentSubscriptionOperation.prototype._inspectPackage = function(pkg) {
if (pkg.command === TcpCommand.PersistentSubscriptionConfirmation)
2016-03-11 06:57:39 +00:00
{
var dto = ClientMessage.PersistentSubscriptionConfirmation.decode(pkg.data.toBuffer());
2017-04-16 22:51:09 +00:00
this._confirmSubscription(dto.lastCommitPosition, dto.lastEventNumber);
this._subscriptionId = dto.subscriptionId;
2016-03-11 06:57:39 +00:00
return new InspectionResult(InspectionDecision.Subscribed, "SubscriptionConfirmation");
}
if (pkg.command === TcpCommand.PersistentSubscriptionStreamEventAppeared)
2016-03-11 06:57:39 +00:00
{
var dto = ClientMessage.PersistentSubscriptionStreamEventAppeared.decode(pkg.data.toBuffer());
this._onEventAppeared(new results.ResolvedEvent(dto.event));
return new InspectionResult(InspectionDecision.DoNothing, "StreamEventAppeared");
}
if (pkg.command === TcpCommand.SubscriptionDropped)
2016-03-11 06:57:39 +00:00
{
var dto = ClientMessage.SubscriptionDropped.decode(pkg.data.toBuffer());
if (dto.reason === ClientMessage.SubscriptionDropped.SubscriptionDropReason.AccessDenied)
2016-03-11 06:57:39 +00:00
{
this.dropSubscription(SubscriptionDropReason.AccessDenied, new Error("You do not have access to the stream."));
return new InspectionResult(InspectionDecision.EndOperation, "SubscriptionDropped");
}
if (dto.reason === ClientMessage.SubscriptionDropped.SubscriptionDropReason.NotFound)
2016-03-11 06:57:39 +00:00
{
this.dropSubscription(SubscriptionDropReason.NotFound, new Error("Subscription not found"));
return new InspectionResult(InspectionDecision.EndOperation, "SubscriptionDropped");
}
if (dto.reason === ClientMessage.SubscriptionDropped.SubscriptionDropReason.PersistentSubscriptionDeleted)
2016-03-11 06:57:39 +00:00
{
this.dropSubscription(SubscriptionDropReason.PersistentSubscriptionDeleted, new Error("Persistent subscription deleted."));
return new InspectionResult(InspectionDecision.EndOperation, "SubscriptionDropped");
}
if (dto.reason === ClientMessage.SubscriptionDropped.SubscriptionDropReason.SubscriberMaxCountReached)
2016-03-11 06:57:39 +00:00
{
this.dropSubscription(SubscriptionDropReason.MaxSubscribersReached, new Error("Maximum subscribers reached."));
return new InspectionResult(InspectionDecision.EndOperation, "SubscriptionDropped");
}
this.dropSubscription(SubscriptionDropReason.UserInitiated, null, this._getConnection());
return new InspectionResult(InspectionDecision.EndOperation, "SubscriptionDropped");
}
return null;
};
ConnectToPersistentSubscriptionOperation.prototype._createSubscriptionObject = function(lastCommitPosition, lastEventNumber) {
return new PersistentEventStoreSubscription(this, this._streamId, lastCommitPosition, lastEventNumber);
};
ConnectToPersistentSubscriptionOperation.prototype.notifyEventsProcessed = function(processedEvents) {
ensure.notNull(processedEvents, "processedEvents");
var dto = new ClientMessage.PersistentSubscriptionAckEvents({
2017-04-16 22:51:09 +00:00
subscriptionId: this._subscriptionId,
processedEventIds: processedEvents.map(function (x) {
return new Buffer(uuidParse.parse(x));
2016-03-11 06:57:39 +00:00
})
});
var pkg = new TcpPackage(TcpCommand.PersistentSubscriptionAckEvents,
this._userCredentials !== null ? TcpFlags.Authenticated : TcpFlags.None,
this._correlationId,
this._userCredentials !== null ? this._userCredentials.username : null,
this._userCredentials !== null ? this._userCredentials.password : null,
2017-04-16 22:51:09 +00:00
createBufferSegment(ClientMessage.PersistentSubscriptionAckEvents.encode(dto).finish()));
2016-03-11 06:57:39 +00:00
this._enqueueSend(pkg);
};
ConnectToPersistentSubscriptionOperation.prototype.notifyEventsFailed = function(processedEvents, action, reason) {
ensure.notNull(processedEvents, "processedEvents");
ensure.notNull(reason, "reason");
2017-04-16 22:51:09 +00:00
var dto = new ClientMessage.PersistentSubscriptionNakEvents({
subscriptionId: this._subscriptionId,
processedEventIds: processedEvents.map(function(x) { return new Buffer(uuidParse.parse(x)); }),
message: reason,
action: action
});
2016-03-11 06:57:39 +00:00
var pkg = new TcpPackage(TcpCommand.PersistentSubscriptionNakEvents,
this._userCredentials !== null ? TcpFlags.Authenticated : TcpFlags.None,
2016-03-11 06:57:39 +00:00
this._correlationId,
this._userCredentials !== null ? this._userCredentials.username : null,
this._userCredentials !== null ? this._userCredentials.password : null,
2017-04-16 22:51:09 +00:00
createBufferSegment(ClientMessage.PersistentSubscriptionNakEvents.encode(dto).finish()));
2016-03-11 06:57:39 +00:00
this._enqueueSend(pkg);
};
module.exports = ConnectToPersistentSubscriptionOperation;