Compare commits
45 Commits
v0.0.28
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
bc2fbe14e3 | |||
9fc5d64ceb | |||
2bdd74e041 | |||
fde6ad2e77 | |||
4a2065c0a7 | |||
67dd275b5d | |||
8997a8c398 | |||
a5807483a2 | |||
e79ad8f9c4 | |||
bf89354564 | |||
254ddda8a2 | |||
16c081020a | |||
4584517ede | |||
e6af223f0d | |||
990351738c | |||
18e35c56f9 | |||
20de4c3715 | |||
60d2a1e5ee | |||
a8081a280a | |||
7c94e26055 | |||
0b968c40e8 | |||
21ab49845f | |||
6634dee5f3 | |||
1d21facd2c | |||
8735b23bf9 | |||
f66decdfb3 | |||
121e248dd8 | |||
917b89cf3d | |||
a1b9d6db9b | |||
83b88fc015 | |||
4130ab646c | |||
83cfa8fe1c | |||
e0446cfb3e | |||
0302099a16 | |||
f7c13634cc | |||
56c2dee6d6 | |||
85814ed03f | |||
361330629d | |||
883e89abfe | |||
e39280a2dc | |||
05b36f22bc | |||
217c1c6bb8 | |||
eaec15fe74 | |||
d71d565197 | |||
9e29e60cfa |
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES5" />
|
||||
</component>
|
||||
</project>
|
40
README.md
40
README.md
@ -1,11 +1,10 @@
|
||||
# eventstore-node
|
||||
# node-eventstore-client
|
||||
A port of the EventStore .Net ClientAPI to Node.js
|
||||
|
||||
## Status
|
||||
|
||||
### Missing features:
|
||||
|
||||
- Ssl connection
|
||||
- Set system settings
|
||||
|
||||
### Areas to improve
|
||||
@ -19,33 +18,36 @@ A port of the EventStore .Net ClientAPI to Node.js
|
||||
|
||||
## Getting started
|
||||
|
||||
Install using `npm install eventstore-node`
|
||||
Install using `npm install node-eventstore-client`
|
||||
|
||||
### Dependencies
|
||||
|
||||
- Node.js >= 0.12
|
||||
- Node.js >= 4.0
|
||||
- Modules: [long](https://www.npmjs.org/package/long), [protobufjs](https://www.npmjs.org/package/protobufjs), [uuid](https://www.npmjs.org/package/uuid) (installed via `npm install`)
|
||||
|
||||
### Install and run an Eventstore on localhost
|
||||
|
||||
See https://eventstore.org/docs/introduction/4.1.0/
|
||||
|
||||
*Note: If you are using a version of EventStore prior to 3.9.4, you need to use version 0.1.x of this package `npm install node-eventstore-client@^0.1`.*
|
||||
|
||||
|
||||
### API Documentation
|
||||
|
||||
#### Offline
|
||||
|
||||
The offline documentation can be found in the module folder `./node_modules/eventstore-node/docs`.
|
||||
The offline documentation can be found in the module folder `./node_modules/node-eventstore-client/docs`.
|
||||
|
||||
#### Online
|
||||
|
||||
The online documentation can be found at [https://dev.nicdex.com/eventstore-node/docs/](https://dev.nicdex.com/eventstore-node/docs/)
|
||||
|
||||
### Install & run an Eventstore on localhost
|
||||
|
||||
See http://docs.geteventstore.com/introduction/3.9.0/ .
|
||||
|
||||
The online documentation can be found at [https://dev.nicdex.com/node-eventstore-client/docs/](https://dev.nicdex.com/node-eventstore-client/docs/)
|
||||
|
||||
### Example: Storing an event
|
||||
|
||||
Save to ```app.js:```
|
||||
|
||||
```javascript
|
||||
var esClient = require('eventstore-node');
|
||||
var esClient = require('node-eventstore-client');
|
||||
var uuid = require('uuid');
|
||||
|
||||
var streamName = "testStream";
|
||||
@ -88,7 +90,7 @@ Run:
|
||||
|
||||
```json
|
||||
npm install uuid
|
||||
npm install eventstore-node
|
||||
npm install node-eventstore-client
|
||||
node app.js
|
||||
```
|
||||
|
||||
@ -112,7 +114,15 @@ To generate a test event, open a separate console and run:
|
||||
|
||||
To run the tests it is recommended that you use an in-memory instance of the eventstore so you don't pollute your dev instance.
|
||||
|
||||
EventStore.ClusterNode.exe --memdb
|
||||
EventStore.ClusterNode.exe --run-projections=all --memdb –certificate-file=yourcert.pfx
|
||||
or
|
||||
./run-node.sh --run-projections=all --memdb –certificate-file=yourcert.p12
|
||||
|
||||
For SSL setup see:
|
||||
|
||||
https://eventstore.org/docs/server/setting_up_ssl/
|
||||
or
|
||||
https://eventstore.org/docs/server/setting_up_ssl_linux/
|
||||
|
||||
To execute the tests suites simply run
|
||||
|
||||
@ -125,6 +135,6 @@ Any async commands returns a [Promise](https://developer.mozilla.org/en/docs/Web
|
||||
|
||||
## License
|
||||
|
||||
Ported code is released under the MIT license, see [LICENSE](https://github.com/nicdex/eventstore-node/blob/master/LICENSE).
|
||||
Ported code is released under the MIT license, see [LICENSE](https://github.com/nicdex/node-eventstore-client/blob/master/LICENSE).
|
||||
|
||||
Original code is released under the EventStore license and can be found at https://github.com/eventstore/eventstore.
|
||||
|
167
index.d.ts
vendored
167
index.d.ts
vendored
@ -1,14 +1,15 @@
|
||||
/// <reference types="node" />
|
||||
/// <reference types="Long" />
|
||||
|
||||
// Expose classes
|
||||
export class Position {
|
||||
constructor(commitPosition: number|Long, preparePosition: number|Long);
|
||||
readonly commitPosition: Long;
|
||||
readonly preparePosition: Long;
|
||||
static readonly start: number;
|
||||
static readonly end: number;
|
||||
}
|
||||
|
||||
// Expose classes
|
||||
|
||||
export class UserCredentials {
|
||||
constructor(username: string, password: string);
|
||||
readonly username: string;
|
||||
@ -16,17 +17,17 @@ export class UserCredentials {
|
||||
}
|
||||
|
||||
export class PersistentSubscriptionSettings {
|
||||
constructor(resolveLinkTos: boolean, startFrom: number, extraStatistics: boolean, messageTimeout: number,
|
||||
constructor(resolveLinkTos: boolean, startFrom: Long|number, extraStatistics: boolean, messageTimeout: number,
|
||||
maxRetryCount: number, liveBufferSize: number, readBatchSize: number, historyBufferSize: number,
|
||||
checkPointAfter: number, minCheckPointCount: number, maxCheckPointCount: number,
|
||||
maxSubscriberCount: number, namedConsumerStrategy: string)
|
||||
static create();
|
||||
static create(): PersistentSubscriptionSettings;
|
||||
}
|
||||
|
||||
export namespace SystemConsumerStrategies {
|
||||
const DispatchToSingle: string;
|
||||
const RoundRobin: string;
|
||||
const Pinned: string
|
||||
const Pinned: string;
|
||||
}
|
||||
|
||||
export class GossipSeed {
|
||||
@ -35,13 +36,64 @@ export class GossipSeed {
|
||||
readonly hostHeader: string;
|
||||
}
|
||||
|
||||
export interface ProjectionDetails {
|
||||
readonly coreProcessingTime: number,
|
||||
readonly version: number,
|
||||
readonly epoch: number,
|
||||
readonly effectiveName: string,
|
||||
readonly writesInProgress: number,
|
||||
readonly readsInProgress: number,
|
||||
readonly partitionsCached: number,
|
||||
readonly status: string,
|
||||
readonly stateReason: string,
|
||||
readonly name: string,
|
||||
readonly mode: string,
|
||||
readonly position: string,
|
||||
readonly progress: number,
|
||||
readonly lastCheckpoint: string,
|
||||
readonly eventsProcessedAfterRestart: number,
|
||||
readonly statusUrl: string,
|
||||
readonly stateUrl: string,
|
||||
readonly resultUrl: string,
|
||||
readonly queryUrl: string,
|
||||
readonly enableCommandUrl: string,
|
||||
readonly disableCommandUrl: string,
|
||||
readonly checkpointStatus: string,
|
||||
readonly bufferedEvents: number,
|
||||
readonly writePendingEventsBeforeCheckpoint: number,
|
||||
readonly writePendingEventsAfterCheckpoint: number
|
||||
}
|
||||
|
||||
export class ProjectionsManager {
|
||||
constructor(log: Logger, httpEndPoint: string, operationTimeout: number);
|
||||
enable(name: string, userCredentials: UserCredentials): Promise<void>;
|
||||
disable(name: string, userCredentials: UserCredentials): Promise<void>;
|
||||
abort(name: string, userCredentials: UserCredentials): Promise<void>;
|
||||
createOneTime(query: string, userCredentials: UserCredentials): Promise<void>;
|
||||
createTransient(name: string, query: string, userCredentials: UserCredentials): Promise<void>;
|
||||
createContinuous(name: string, query: string, trackEmittedStreams: boolean, userCredentials: UserCredentials): Promise<void>;
|
||||
listAll(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
|
||||
listOneTime(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
|
||||
listContinuous(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
|
||||
getStatus(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getState(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getPartitionState(name: string, partitionId: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getResult(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getPartitionResult(name: string, partitionId: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getStatistics(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getQuery(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
getState(name: string, userCredentials: UserCredentials): Promise<string>;
|
||||
updateQuery(name: string, query: string, userCredentials: UserCredentials): Promise<void>;
|
||||
deleteQuery(name: string, deleteEmittedStreams: boolean, userCredentials: UserCredentials): Promise<void>;
|
||||
}
|
||||
|
||||
// Expose errors
|
||||
export class WrongExpectedVersionError {
|
||||
readonly name: string;
|
||||
readonly action: string;
|
||||
readonly message: string;
|
||||
readonly stream?: string;
|
||||
readonly expectedVersion?: number;
|
||||
readonly expectedVersion?: Long;
|
||||
readonly transactionId?: Long;
|
||||
}
|
||||
|
||||
@ -59,8 +111,12 @@ export class AccessDeniedError {
|
||||
readonly transactionId?: Long;
|
||||
}
|
||||
|
||||
// Expose enums/constants
|
||||
export class ProjectionCommandFailedError {
|
||||
readonly httpStatusCode: number;
|
||||
readonly message: string;
|
||||
}
|
||||
|
||||
// Expose enums/constants
|
||||
export namespace expectedVersion {
|
||||
const any: number;
|
||||
const noStream: number;
|
||||
@ -72,19 +128,24 @@ export namespace positions {
|
||||
const end: Position;
|
||||
}
|
||||
|
||||
export namespace streamPosition {
|
||||
const start: number;
|
||||
const end: number;
|
||||
}
|
||||
|
||||
//TODO
|
||||
// systemMetadata
|
||||
// eventReadStatus
|
||||
// sliceReadStatus
|
||||
|
||||
// Expose loggers
|
||||
|
||||
export interface Logger {
|
||||
debug(fmt: string, ...args: any[]): void;
|
||||
info(fmt: string, ...args: any[]): void;
|
||||
error(fmt: string, ...args: any[]): void;
|
||||
}
|
||||
|
||||
export class NoOpLogger implements Logger {
|
||||
export class NoopLogger implements Logger {
|
||||
constructor()
|
||||
debug(fmt: string, ...args: any[]): void;
|
||||
info(fmt: string, ...args: any[]): void;
|
||||
@ -98,17 +159,16 @@ export class FileLogger implements Logger {
|
||||
error(fmt: string, ...args: any[]): void;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
// Expose results
|
||||
export interface WriteResult {
|
||||
readonly nextExpectedVersion: number;
|
||||
readonly nextExpectedVersion: Long;
|
||||
readonly logPosition: Position;
|
||||
}
|
||||
|
||||
export interface RecordedEvent {
|
||||
readonly eventStreamId: string;
|
||||
readonly eventId: string;
|
||||
readonly eventNumber: number;
|
||||
readonly eventNumber: Long;
|
||||
readonly eventType: string;
|
||||
readonly createdEpoch: number;
|
||||
readonly data?: Buffer;
|
||||
@ -123,17 +183,17 @@ export interface ResolvedEvent {
|
||||
readonly isResolved: boolean;
|
||||
readonly originalPosition?: Position;
|
||||
readonly originalStreamId: string;
|
||||
readonly originalEventNumber: number;
|
||||
readonly originalEventNumber: Long;
|
||||
}
|
||||
|
||||
export interface StreamEventsSlice {
|
||||
readonly status: string; // TODO: enum
|
||||
readonly stream: string;
|
||||
readonly fromEventNumber: number;
|
||||
readonly fromEventNumber: Long;
|
||||
readonly readDirection: string; // TODO: enum
|
||||
readonly events: ResolvedEvent[];
|
||||
readonly nextEventNumber: number;
|
||||
readonly lastEventNumber: number;
|
||||
readonly nextEventNumber: Long;
|
||||
readonly lastEventNumber: Long;
|
||||
readonly isEndOfStream: boolean;
|
||||
}
|
||||
|
||||
@ -159,7 +219,7 @@ export interface EventStoreTransaction {
|
||||
export interface EventReadResult {
|
||||
readonly status: string;
|
||||
readonly stream: string;
|
||||
readonly eventNumber: number;
|
||||
readonly eventNumber: Long;
|
||||
readonly event: ResolvedEvent | null;
|
||||
}
|
||||
|
||||
@ -167,27 +227,52 @@ export interface EventStoreSubscription {
|
||||
readonly isSubscribedToAll: boolean;
|
||||
readonly streamId: string;
|
||||
readonly lastCommitPosition: Position;
|
||||
readonly lastEventNumber: number;
|
||||
readonly lastEventNumber: Long;
|
||||
|
||||
close(): void;
|
||||
unsubscribe(): void;
|
||||
}
|
||||
|
||||
export interface EventStoreCatchUpSubscription {
|
||||
start(): void;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
export enum PersistentSubscriptionNakEventAction {
|
||||
Unknown = 0,
|
||||
Park = 1,
|
||||
Retry = 2,
|
||||
Skip = 3,
|
||||
Stop = 4
|
||||
}
|
||||
|
||||
export interface EventStorePersistentSubscription {
|
||||
acknowledge(events: ResolvedEvent | ResolvedEvent[]): void;
|
||||
fail(events: ResolvedEvent | ResolvedEvent[], action: PersistentSubscriptionNakEventAction, reason: string): void;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
export interface RawStreamMetadataResult {
|
||||
readonly stream: string;
|
||||
readonly isStreamDeleted: boolean;
|
||||
readonly metastreamVersion: number;
|
||||
readonly metastreamVersion: Long;
|
||||
readonly streamMetadata: any;
|
||||
}
|
||||
|
||||
export interface PersistentSubscriptionCreateResult {
|
||||
readonly status: string;
|
||||
}
|
||||
|
||||
export interface PersistentSubscriptionUpdateResult {
|
||||
readonly status: string;
|
||||
}
|
||||
|
||||
export interface PersistentSubscriptionDeleteResult {
|
||||
readonly status: string;
|
||||
}
|
||||
|
||||
// Callbacks
|
||||
export interface EventAppearedCallback<TSubscription> {
|
||||
(subscription: TSubscription, event: ResolvedEvent): void;
|
||||
(subscription: TSubscription, event: ResolvedEvent): void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface LiveProcessingStartedCallback {
|
||||
@ -203,6 +288,15 @@ export interface TcpEndPoint {
|
||||
host: string;
|
||||
}
|
||||
|
||||
export interface HeartbeatInfo {
|
||||
readonly connectionId: string;
|
||||
readonly remoteEndPoint: TcpEndPoint;
|
||||
readonly requestSentAt: number;
|
||||
readonly requestPkgNumber: number;
|
||||
readonly responseReceivedAt: number;
|
||||
readonly responsePkgNumber: number;
|
||||
}
|
||||
|
||||
export interface EventData {
|
||||
readonly eventId: string;
|
||||
readonly type: string;
|
||||
@ -215,31 +309,35 @@ export interface EventStoreNodeConnection {
|
||||
connect(): Promise<void>;
|
||||
close(): void;
|
||||
// write actions
|
||||
deleteStream(stream: string, expectedVersion: number, hardDelete?: boolean, userCredentials?: UserCredentials): Promise<DeleteResult>;
|
||||
appendToStream(stream: string, expectedVersion: number, eventOrEvents: EventData | EventData[], userCredentials?: UserCredentials): Promise<WriteResult>;
|
||||
startTransaction(stream: string, expectedVersion: number, userCredentials?: UserCredentials): Promise<EventStoreTransaction>;
|
||||
deleteStream(stream: string, expectedVersion: Long|number, hardDelete?: boolean, userCredentials?: UserCredentials): Promise<DeleteResult>;
|
||||
appendToStream(stream: string, expectedVersion: Long|number, eventOrEvents: EventData | EventData[], userCredentials?: UserCredentials): Promise<WriteResult>;
|
||||
startTransaction(stream: string, expectedVersion: Long|number, userCredentials?: UserCredentials): Promise<EventStoreTransaction>;
|
||||
continueTransaction(transactionId: number, userCredentials?: UserCredentials): EventStoreTransaction;
|
||||
// read actions
|
||||
readEvent(stream: string, eventNumber: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<EventReadResult>;
|
||||
readStreamEventsForward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
|
||||
readStreamEventsBackward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
|
||||
readEvent(stream: string, eventNumber: Long|number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<EventReadResult>;
|
||||
readStreamEventsForward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
|
||||
readStreamEventsBackward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
|
||||
readAllEventsForward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
|
||||
readAllEventsBackward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
|
||||
// subscription actions
|
||||
subscribeToStream(stream: string, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreSubscription>, userCredentials?: UserCredentials): Promise<EventStoreSubscription>;
|
||||
subscribeToStreamFrom(stream: string, lastCheckpoint: number | null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
|
||||
subscribeToStreamFrom(stream: string, lastCheckpoint: Long|number|null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
|
||||
subscribeToAll(resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreSubscription>, userCredentials?: UserCredentials): Promise<EventStoreSubscription>;
|
||||
subscribeToAllFrom(lastCheckpoint: Position | null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
|
||||
subscribeToAllFrom(lastCheckpoint: Position|null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
|
||||
// persistent subscriptions
|
||||
createPersistentSubscription(stream: string, groupName: string, settings: PersistentSubscriptionSettings, userCredentials?: PersistentSubscriptionSettings): Promise<PersistentSubscriptionCreateResult>;
|
||||
updatePersistentSubscription(stream: string, groupName: string, settings: PersistentSubscriptionSettings, userCredentials?: PersistentSubscriptionSettings): Promise<PersistentSubscriptionUpdateResult>;
|
||||
deletePersistentSubscription(stream: string, groupName: string, userCredentials?: PersistentSubscriptionSettings): Promise<PersistentSubscriptionDeleteResult>
|
||||
connectToPersistentSubscription(stream: string, groupName: string, eventAppeared: EventAppearedCallback<EventStorePersistentSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStorePersistentSubscription>, userCredentials?: UserCredentials, bufferSize?: number, autoAck?: boolean): Promise<EventStorePersistentSubscription>;
|
||||
// metadata actions
|
||||
setStreamMetadataRaw(stream: string, expectedMetastreamVersion: number, metadata: any, userCredentials?: UserCredentials): Promise<WriteResult>;
|
||||
setStreamMetadataRaw(stream: string, expectedMetastreamVersion: Long|number, metadata: any, userCredentials?: UserCredentials): Promise<WriteResult>;
|
||||
getStreamMetadataRaw(stream: string, userCredentials?: UserCredentials): Promise<RawStreamMetadataResult>;
|
||||
|
||||
on(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: (arg: Error | string | TcpEndPoint) => void): this;
|
||||
once(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: (arg: Error | string | TcpEndPoint) => void): this;
|
||||
on(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error" | "heartbeatInfo", listener: (arg: Error | string | TcpEndPoint | HeartbeatInfo) => void): this;
|
||||
once(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error" | "heartbeatInfo", listener: (arg: Error | string | TcpEndPoint | HeartbeatInfo) => void): this;
|
||||
}
|
||||
|
||||
// Expose helper functions
|
||||
|
||||
export interface ConnectionSettings {
|
||||
log?: Logger,
|
||||
verboseLogging?: boolean,
|
||||
@ -272,6 +370,7 @@ export interface ConnectionSettings {
|
||||
gossipTimeout?: number
|
||||
}
|
||||
|
||||
// Expose Helper functions
|
||||
export function createConnection(settings: ConnectionSettings, endPointOrGossipSeed: string | TcpEndPoint | GossipSeed[], connectionName?: string): EventStoreNodeConnection;
|
||||
export function createJsonEventData(eventId: string, event: any, metadata?: any, type?: string): EventData;
|
||||
export function createEventData(eventId: string, type: string, isJson: boolean, data: Buffer, metadata?: Buffer): EventData;
|
||||
|
2
index.js
2
index.js
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* eventstore-node A port of EventStore .Net ClientAPI to Node.js
|
||||
* node-eventstore-client A port of EventStore .Net ClientAPI to Node.js
|
||||
* see README.md for more details
|
||||
* see LICENSE for license info
|
||||
*/
|
||||
|
24
package.json
24
package.json
@ -1,9 +1,13 @@
|
||||
{
|
||||
"name": "eventstore-node",
|
||||
"version": "0.0.28",
|
||||
"name": "node-eventstore-client",
|
||||
"version": "0.2.3",
|
||||
"description": "A port of the EventStore .Net ClientAPI to Node.js",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"engineStrict": true,
|
||||
"scripts": {
|
||||
"clean": "rm lib/dist.js",
|
||||
"build": "webpack",
|
||||
@ -27,7 +31,7 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nicdex/eventstore-node.git"
|
||||
"url": "git+https://github.com/nicdex/node-eventstore-client.git"
|
||||
},
|
||||
"keywords": [
|
||||
"eventstore",
|
||||
@ -37,19 +41,19 @@
|
||||
"author": "Nicolas Dextraze",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/nicdex/eventstore-node/issues"
|
||||
"url": "https://github.com/nicdex/node-eventstore-client/issues"
|
||||
},
|
||||
"homepage": "https://github.com/nicdex/eventstore-node#readme",
|
||||
"homepage": "https://github.com/nicdex/node-eventstore-client#readme",
|
||||
"dependencies": {
|
||||
"@types/long": "^3.0.31",
|
||||
"@types/node": "^6.0.47",
|
||||
"long": "^3.2",
|
||||
"protobufjs": "^5.0",
|
||||
"uuid": "^2.0"
|
||||
"protobufjs": "^6.7.3",
|
||||
"uuid": "^3.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jsdoc": "^3.4.2",
|
||||
"nodeunit": "^0.10.2",
|
||||
"webpack": "^1.13.2"
|
||||
"jsdoc": "^3.5.5",
|
||||
"nodeunit": "^0.11.1",
|
||||
"webpack": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,55 @@
|
||||
var client = require('../src/client');
|
||||
var uuid = require('uuid');
|
||||
const client = require('../src/client')
|
||||
// const client = require("node-eventstore-client")
|
||||
const uuid = require("uuid")
|
||||
|
||||
var settings = {
|
||||
const settings = {
|
||||
verboseLogging: true,
|
||||
log: new client.FileLogger('./simple-verbose.log')
|
||||
};
|
||||
var gossipSeeds = [
|
||||
new client.GossipSeed({host: '192.168.33.10', port: 2113}),
|
||||
new client.GossipSeed({host: '192.168.33.11', port: 2113}),
|
||||
new client.GossipSeed({host: '192.168.33.12', port: 2113})
|
||||
];
|
||||
var conn = client.createConnection(settings, gossipSeeds);
|
||||
conn.connect()
|
||||
.catch(function (err) {
|
||||
console.log(err);
|
||||
//process.exit(-1);
|
||||
});
|
||||
conn.on('connected', function (endPoint) {
|
||||
console.log('connected to endPoint', endPoint);
|
||||
//Start some work
|
||||
setInterval(function () {
|
||||
conn.appendToStream('test-' + uuid.v4(), client.expectedVersion.noStream, [
|
||||
client.createJsonEventData(uuid.v4(), {abc: 123}, null, 'MyEvent')
|
||||
]).then(function (writeResult) {
|
||||
console.log(writeResult);
|
||||
});
|
||||
}, 1000);
|
||||
});
|
||||
conn.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
});
|
||||
conn.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
//process.exit(-1);
|
||||
});
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdin.on('data', process.exit.bind(process, 0));
|
||||
log: new client.FileLogger("./simple-verbose.log")
|
||||
}
|
||||
/*
|
||||
// connecting to Cluster using hard-coded gossip seeds
|
||||
const gossipSeeds = [
|
||||
new client.GossipSeed({host: "192.168.33.10", port: 2113}),
|
||||
new client.GossipSeed({host: "192.168.33.11", port: 2113}),
|
||||
new client.GossipSeed({host: "192.168.33.12", port: 2113})
|
||||
]
|
||||
const connection = client.createConnection(settings, gossipSeeds)
|
||||
*/
|
||||
/*
|
||||
// connecting to Cluster using dns discovery, note that cluster gossip over external http port not tcp port
|
||||
const connection = client.createConnection(settings, 'discover://my.dns:2113')
|
||||
*/
|
||||
const connection = client.createConnection(settings, 'tcp://localhost:1113')
|
||||
|
||||
connection.connect().catch(err => console.log(err))
|
||||
|
||||
connection.on("connected", tcpEndPoint => {
|
||||
console.log(`connected to endPoint ${tcpEndPoint.host}:${tcpEndPoint.port}`)
|
||||
|
||||
setInterval(() => {
|
||||
connection.appendToStream(
|
||||
`test-${uuid.v4()}`,
|
||||
client.expectedVersion.noStream,
|
||||
[
|
||||
client.createJsonEventData(
|
||||
uuid.v4(),
|
||||
{ abc: 123 },
|
||||
null,
|
||||
"MyEvent"
|
||||
)
|
||||
]
|
||||
).then(writeResult => console.log(writeResult))
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
connection.on("error", error =>
|
||||
console.log(`Error occurred on connection: ${error}`)
|
||||
)
|
||||
|
||||
connection.on("closed", reason =>
|
||||
console.log(`Connection closed, reason: ${reason}`)
|
||||
)
|
||||
|
||||
process.stdin.setRawMode(true)
|
||||
process.stdin.resume()
|
||||
process.stdin.on("data", process.exit.bind(process, 0))
|
||||
|
108
samples/speed-tests.js
Normal file
108
samples/speed-tests.js
Normal file
@ -0,0 +1,108 @@
|
||||
//NOTE: this script require node.js v8+ to run (native async/await)
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var conn = client.createConnection({}, 'tcp://localhost:1113');
|
||||
conn.on('connected', onConnected);
|
||||
|
||||
var batchSize = parseInt(process.argv[2], 10) || 500;
|
||||
const MB = 1024*1024;
|
||||
const adminCreds = new client.UserCredentials("admin", "changeit");
|
||||
const nbEvents = 50000;
|
||||
const events = [];
|
||||
const streams = [];
|
||||
const writeOneStream = 'testWrite-' + uuid.v4();
|
||||
for(var i = 0; i < nbEvents; i++) {
|
||||
streams.push('testWrite-' + uuid.v4());
|
||||
events.push(client.createJsonEventData(uuid.v4(), {a:i, b:i+1, c:i+2}, null, 'myEvent'))
|
||||
}
|
||||
conn.connect();
|
||||
|
||||
function rssMB() {
|
||||
return (process.memoryUsage().rss / MB).toFixed(2);
|
||||
}
|
||||
|
||||
function reportResult(action, nbEvents, elapsedMs) {
|
||||
console.log(action, nbEvents, 'events took', elapsedMs, 'ms, avg', (nbEvents/(elapsedMs/1000)).toFixed(2), '/s');
|
||||
console.log('Memory usage:', rssMB(), 'MB\n');
|
||||
}
|
||||
|
||||
async function testWriteOneStreamAnyVersion(batchSize) {
|
||||
console.log(`Test Write One Stream Any Version (batchSize = ${batchSize})...`);
|
||||
const start = Date.now();
|
||||
const promises = [];
|
||||
for(let i = 0; i < nbEvents; i += batchSize) {
|
||||
promises.push(conn.appendToStream(writeOneStream, client.expectedVersion.any, events.slice(i, i + batchSize)))
|
||||
}
|
||||
await Promise.all(promises);
|
||||
var diff = Date.now() - start;
|
||||
reportResult("Writing", nbEvents, diff)
|
||||
}
|
||||
|
||||
async function testWriteOneStreamWithVersion(batchSize) {
|
||||
console.log(`Test Write One Stream With Version (batchSize = ${batchSize})...`);
|
||||
const writeStream = 'testWrite-' + uuid.v4();
|
||||
const start = Date.now();
|
||||
const promises = [];
|
||||
for(let i = 0; i < nbEvents; i += batchSize) {
|
||||
promises.push(conn.appendToStream(writeStream, i-1, events.slice(i, i + batchSize)))
|
||||
}
|
||||
await Promise.all(promises);
|
||||
var diff = Date.now() - start;
|
||||
reportResult("Writing", nbEvents, diff)
|
||||
}
|
||||
|
||||
async function testWriteMultipleStream() {
|
||||
console.log('Test Write Multiple Streams...');
|
||||
var start = Date.now();
|
||||
var promises = [];
|
||||
for(var i = 0; i < nbEvents; i++) {
|
||||
promises.push(conn.appendToStream(streams[i], client.expectedVersion.emptyStream, events[i]))
|
||||
}
|
||||
await Promise.all(promises);
|
||||
const diff = Date.now() - start;
|
||||
reportResult("Writing", nbEvents, diff)
|
||||
}
|
||||
|
||||
async function testRead(batchSize) {
|
||||
console.log(`Test Read One Stream (batchSize = ${batchSize})...`);
|
||||
const start = Date.now();
|
||||
const promises = [];
|
||||
for(let i = 0; i < nbEvents; i += batchSize) {
|
||||
promises.push(conn.readStreamEventsForward(writeOneStream, i, batchSize, false));
|
||||
}
|
||||
const results = await Promise.all(promises);
|
||||
const diff = Date.now() - start;
|
||||
const readEvents = results.reduce((x,y) => x + y.events.length, 0);
|
||||
reportResult("Reading", readEvents, diff)
|
||||
}
|
||||
|
||||
async function testReadAll(batchSize) {
|
||||
console.log(`Test Read from $all (batchSize = ${batchSize})...`);
|
||||
const start = Date.now();
|
||||
let pos = client.positions.start;
|
||||
let eventsCount = 0;
|
||||
for(;;) {
|
||||
var result = await conn.readAllEventsForward(pos, batchSize, false, adminCreds);
|
||||
pos = result.nextPosition;
|
||||
eventsCount += result.events.length;
|
||||
if (result.isEndOfStream) break;
|
||||
}
|
||||
const diff = Date.now() - start;
|
||||
reportResult("Reading", eventsCount, diff)
|
||||
}
|
||||
|
||||
async function onConnected() {
|
||||
try {
|
||||
await testWriteOneStreamAnyVersion(1);
|
||||
await testWriteOneStreamAnyVersion(batchSize);
|
||||
await testWriteOneStreamWithVersion(1);
|
||||
await testWriteOneStreamWithVersion(batchSize);
|
||||
await testWriteMultipleStream();
|
||||
await testRead(1);
|
||||
await testRead(batchSize);
|
||||
await testReadAll(batchSize);
|
||||
conn.close();
|
||||
} catch (e) {
|
||||
console.log('ERROR', e);
|
||||
}
|
||||
}
|
@ -1,40 +1,53 @@
|
||||
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
||||
// var esClient = require('eventstore-node'); // Otherwise
|
||||
var uuid = require('uuid');
|
||||
const client = require('../src/client')
|
||||
// const client = require("node-eventstore-client")
|
||||
const uuid = require("uuid")
|
||||
|
||||
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
||||
esConnection.connect();
|
||||
esConnection.once('connected', function (tcpEndPoint) {
|
||||
console.log('Connected to eventstore at ' + tcpEndPoint.host + ":" + tcpEndPoint.port);
|
||||
var userId = uuid.v4();
|
||||
// This event could happen as a result of (e.g.) a 'CreateUser(id, username, password)' command.
|
||||
var userCreatedEvent = {
|
||||
id: userId,
|
||||
username: "user" + uuid.v4().substring(0,6), // Hard-to-spell exotic username.
|
||||
password: Math.random().toString() // Hard-to-guess password.
|
||||
};
|
||||
var eventId = uuid.v4();
|
||||
var event = esClient.createJsonEventData(eventId, userCreatedEvent, null, "UserCreated");
|
||||
// Every user has her/his own stream of events:
|
||||
var streamName = "user-" + userId;
|
||||
console.log("Storing event. Look for it at http://localhost:2113/web/index.html#/streams/user-" + userId);
|
||||
esConnection.appendToStream(streamName, esClient.expectedVersion.any, event)
|
||||
.then(function(result) {
|
||||
console.log("Event stored.");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
process.exit(-1);
|
||||
});
|
||||
});
|
||||
const settings = {}
|
||||
const endpoint = "tcp://localhost:1113"
|
||||
const connection = client.createConnection(settings, endpoint)
|
||||
|
||||
esConnection.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
process.exit(-1);
|
||||
});
|
||||
connection.connect().catch(err => console.log(err))
|
||||
|
||||
esConnection.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
process.exit(-1);
|
||||
});
|
||||
connection.once("connected", tcpEndPoint => {
|
||||
const userId = uuid.v4()
|
||||
|
||||
const userCreatedEvent = {
|
||||
id: userId,
|
||||
username: `user${uuid.v4().substring(0,6)}`,
|
||||
password: Math.random().toString()
|
||||
}
|
||||
|
||||
const eventId = uuid.v4();
|
||||
const event = client.createJsonEventData(
|
||||
eventId,
|
||||
userCreatedEvent,
|
||||
null,
|
||||
"UserCreated"
|
||||
)
|
||||
|
||||
// Every user has their own stream of events:
|
||||
const streamName = `user-${userId}`
|
||||
|
||||
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
|
||||
console.log(`Storing event ${eventId}. Look for it at http://localhost:2113/web/index.html#/streams/user-${userId}`)
|
||||
|
||||
connection.appendToStream(streamName, client.expectedVersion.any, event)
|
||||
.then(result => {
|
||||
console.log("Event stored.")
|
||||
process.exit(0)
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
process.exit(-1)
|
||||
})
|
||||
})
|
||||
|
||||
connection.on("error", error => {
|
||||
console.log(`Error occurred on connection: ${error}`)
|
||||
process.exit(-1)
|
||||
})
|
||||
|
||||
connection.on("closed", reason => {
|
||||
console.log(`Connection closed, reason: ${reason}`)
|
||||
process.exit(-1)
|
||||
})
|
||||
|
@ -1,48 +1,56 @@
|
||||
// Subscribe to all new events on the $all stream. Filter out any which aren't about "user" aggregates.
|
||||
// Subscribe to all new events on the $all stream. Filter out any which aren"t about "user" aggregates.
|
||||
|
||||
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
||||
// var esClient = require('eventstore-node'); // Otherwise
|
||||
const client = require('../src/client')
|
||||
// const client = require("node-eventstore-client")
|
||||
|
||||
const credentialsForAllEventsStream = new esClient.UserCredentials("admin", "changeit");
|
||||
const resolveLinkTos = false;
|
||||
const resolveLinkTos = false
|
||||
|
||||
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
||||
esConnection.connect();
|
||||
esConnection.once('connected', function (tcpEndPoint) {
|
||||
console.log('Connected to eventstore at ' + tcpEndPoint.host + ":" + tcpEndPoint.port);
|
||||
esConnection.subscribeToAll(resolveLinkTos, eventAppeared, subscriptionDropped, credentialsForAllEventsStream)
|
||||
.then(function(subscription) {
|
||||
console.log("subscription.isSubscribedToAll: " + subscription.isSubscribedToAll);
|
||||
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
|
||||
});
|
||||
});
|
||||
const belongsToAUserAggregate = event =>
|
||||
event.originalEvent.eventStreamId.startsWith("user-")
|
||||
|
||||
function belongsToAUserAggregate(event) {
|
||||
return event.originalEvent.eventStreamId.startsWith("user-")
|
||||
const eventAppeared = (subscription, event) => {
|
||||
if (belongsToAUserAggregate(event)) {
|
||||
const aggregateId = event.originalEvent.eventStreamId
|
||||
const eventId = event.originalEvent.eventId
|
||||
const eventType = event.originalEvent.eventType
|
||||
console.log(aggregateId, eventType, eventId)
|
||||
console.log(event.originalEvent.data.toString())
|
||||
}
|
||||
}
|
||||
|
||||
function eventAppeared(subscription, event) {
|
||||
// Ignore all events which aren't about users:
|
||||
if(belongsToAUserAggregate(event)) {
|
||||
var aggregateId = event.originalEvent.eventStreamId;
|
||||
var eventId = event.originalEvent.eventId;
|
||||
var eventType = event.originalEvent.eventType;
|
||||
console.log(aggregateId, eventType, eventId);
|
||||
console.log(event.originalEvent.data.toString() + "\n");
|
||||
}
|
||||
}
|
||||
const subscriptionDropped = (subscription, reason, error) =>
|
||||
console.log(error ? error : "Subscription dropped.")
|
||||
|
||||
function subscriptionDropped(subscription, reason, error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
}
|
||||
console.log('Subscription dropped.');
|
||||
}
|
||||
const credentials = new client.UserCredentials("admin", "changeit")
|
||||
|
||||
esConnection.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
});
|
||||
const settings = {}
|
||||
const endpoint = "tcp://localhost:1113"
|
||||
const connection = client.createConnection(settings, endpoint)
|
||||
|
||||
esConnection.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
});
|
||||
connection.connect().catch(err => console.log(err))
|
||||
|
||||
connection.on('heartbeatInfo', heartbeatInfo => {
|
||||
console.log('Connected to endpoint', heartbeatInfo.remoteEndPoint)
|
||||
console.log('Heartbeat latency', heartbeatInfo.responseReceivedAt - heartbeatInfo.requestSentAt)
|
||||
})
|
||||
|
||||
connection.once("connected", tcpEndPoint => {
|
||||
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
|
||||
connection.subscribeToAll(
|
||||
resolveLinkTos,
|
||||
eventAppeared,
|
||||
subscriptionDropped,
|
||||
credentials
|
||||
).then(subscription => {
|
||||
console.log(`subscription.isSubscribedToAll: ${subscription.isSubscribedToAll}`),
|
||||
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
|
||||
})
|
||||
})
|
||||
|
||||
connection.on("error", error =>
|
||||
console.log(`Error occurred on connection: ${error}`)
|
||||
)
|
||||
|
||||
connection.on("closed", reason =>
|
||||
console.log(`Connection closed, reason: ${reason}`)
|
||||
)
|
||||
|
@ -1,51 +1,49 @@
|
||||
// Subscribe to all events on the $all stream. Catch up from the beginning, then listen for any new events as they occur.
|
||||
// This can be used (e.g.) for subscribers which populate read models.
|
||||
// This could be used for subscribers which populate read models.
|
||||
|
||||
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
||||
// var esClient = require('eventstore-node'); // Otherwise
|
||||
const client = require('../src/client')
|
||||
// const client = require("node-eventstore-client")
|
||||
|
||||
const credentialsForAllEventsStream = new esClient.UserCredentials("admin", "changeit");
|
||||
const eventAppeared = (stream, event) =>
|
||||
console.log(
|
||||
event.originalEvent.eventStreamId,
|
||||
event.originalEvent.eventId,
|
||||
event.originalEvent.eventType
|
||||
)
|
||||
|
||||
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
||||
esConnection.connect();
|
||||
esConnection.once('connected', function (tcpEndPoint) {
|
||||
console.log('Connected to eventstore at ' + tcpEndPoint.host + ":" + tcpEndPoint.port);
|
||||
var subscription = esConnection.subscribeToAllFrom(null, true, eventAppeared, liveProcessingStarted, subscriptionDropped, credentialsForAllEventsStream);
|
||||
console.log("subscription.isSubscribedToAll: " + subscription.isSubscribedToAll);
|
||||
});
|
||||
|
||||
function eventAppeared(subscription, event) {
|
||||
// This is where to filter out events which the subscriber isn't interested in.
|
||||
// For an example, see 'subscribe-all-events.js'.
|
||||
console.log(event.originalEvent.eventStreamId);
|
||||
const liveProcessingStarted = () => {
|
||||
console.log("Caught up with previously stored events. Listening for new events.")
|
||||
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
|
||||
}
|
||||
|
||||
function subscriptionDropped(subscription, reason, error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
}
|
||||
console.log('Subscription dropped.');
|
||||
}
|
||||
const subscriptionDropped = (subscription, reason, error) =>
|
||||
console.log(error ? error : "Subscription dropped.")
|
||||
|
||||
function liveProcessingStarted() {
|
||||
console.log("Caught up with previously stored events. Listening for new events.");
|
||||
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
|
||||
}
|
||||
const credentials = new client.UserCredentials("admin", "changeit")
|
||||
|
||||
function eventAppeared(stream, event) {
|
||||
console.log(event.originalEvent.eventStreamId, event.originalEvent.eventId, event.originalEvent.eventType);
|
||||
// Data:
|
||||
// console.log(event.originalEvent.data.toString());
|
||||
const settings = {}
|
||||
const endpoint = "tcp://localhost:1113"
|
||||
const connection = client.createConnection(settings, endpoint)
|
||||
|
||||
// Position in the event stream. Can be persisted and used to catch up with missed events when re-starting subscribers instead of re-reading
|
||||
// all events from the beginning.
|
||||
// console.log(event.originalPosition);
|
||||
}
|
||||
connection.connect().catch(err => console.log(err))
|
||||
|
||||
esConnection.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
});
|
||||
connection.once("connected", tcpEndPoint => {
|
||||
const subscription = connection.subscribeToAllFrom(
|
||||
null,
|
||||
true,
|
||||
eventAppeared,
|
||||
liveProcessingStarted,
|
||||
subscriptionDropped,
|
||||
credentials
|
||||
)
|
||||
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
|
||||
console.log(`subscription.isSubscribedToAll: ${subscription.isSubscribedToAll}`)
|
||||
})
|
||||
|
||||
esConnection.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
});
|
||||
connection.on("error", err =>
|
||||
console.log(`Error occurred on connection: ${err}`)
|
||||
)
|
||||
|
||||
connection.on("closed", reason =>
|
||||
console.log(`Connection closed, reason: ${reason}`)
|
||||
)
|
||||
|
@ -4,12 +4,22 @@ var results = require('./results');
|
||||
const expectedVersion = {
|
||||
any: -2,
|
||||
noStream: -1,
|
||||
emptyStream: -1
|
||||
emptyStream: -1,
|
||||
streamExists: -4
|
||||
};
|
||||
Object.freeze(expectedVersion);
|
||||
|
||||
const positions = {
|
||||
start: new results.Position(0, 0),
|
||||
end: new results.Position(-1, -1)
|
||||
};
|
||||
Object.freeze(positions);
|
||||
|
||||
const streamPosition = {
|
||||
start: 0,
|
||||
end: -1
|
||||
};
|
||||
Object.freeze(streamPosition);
|
||||
|
||||
/**
|
||||
* Create an EventData object from JavaScript event/metadata that will be serialized as json
|
||||
@ -49,13 +59,16 @@ module.exports.PersistentSubscriptionSettings = require('./persistentSubscriptio
|
||||
module.exports.SystemConsumerStrategies = require('./systemConsumerStrategies');
|
||||
module.exports.GossipSeed = require('./gossipSeed');
|
||||
module.exports.EventStoreConnection = require('./eventStoreConnection');
|
||||
module.exports.ProjectionsManager = require('./projections/projectionsManager');
|
||||
// Expose errors
|
||||
module.exports.WrongExpectedVersionError = require('./errors/wrongExpectedVersionError');
|
||||
module.exports.StreamDeletedError = require('./errors/streamDeletedError');
|
||||
module.exports.AccessDeniedError = require('./errors/accessDeniedError');
|
||||
module.exports.ProjectionCommandFailedError = require('./errors/projectionCommandFailedError');
|
||||
// Expose enums/constants
|
||||
module.exports.expectedVersion = expectedVersion;
|
||||
module.exports.positions = positions;
|
||||
module.exports.streamPosition = streamPosition;
|
||||
module.exports.systemMetadata = require('./common/systemMetadata');
|
||||
module.exports.eventReadStatus = results.EventReadStatus;
|
||||
module.exports.sliceReadStatus = require('./sliceReadStatus');
|
||||
|
@ -1,5 +1,5 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var guidParse = require('../common/guid-parse');
|
||||
|
||||
var TcpCommand = require('../systemData/tcpCommand');
|
||||
var InspectionDecision = require('../systemData/inspectionDecision');
|
||||
@ -26,17 +26,18 @@ util.inherits(AppendToStreamOperation, OperationBase);
|
||||
|
||||
AppendToStreamOperation.prototype._createRequestDto = function() {
|
||||
var dtos = this._events.map(function(ev) {
|
||||
var eventId = new Buffer(uuid.parse(ev.eventId));
|
||||
return new ClientMessage.NewEvent({
|
||||
event_id: eventId, event_type: ev.type,
|
||||
data_content_type: ev.isJson ? 1 : 0, metadata_content_type: 0,
|
||||
data: ev.data, metadata: ev.metadata});
|
||||
var eventId = guidParse.parse(ev.eventId);
|
||||
return {
|
||||
eventId: eventId, eventType: ev.type,
|
||||
dataContentType: ev.isJson ? 1 : 0, metadataContentType: 0,
|
||||
data: ev.data, metadata: ev.metadata
|
||||
};
|
||||
});
|
||||
return new ClientMessage.WriteEvents({
|
||||
event_stream_id: this._stream,
|
||||
expected_version: this._expectedVersion,
|
||||
eventStreamId: this._stream,
|
||||
expectedVersion: this._expectedVersion,
|
||||
events: dtos,
|
||||
require_master: this._requireMaster});
|
||||
requireMaster: this._requireMaster});
|
||||
};
|
||||
|
||||
AppendToStreamOperation.prototype._inspectResponse = function(response) {
|
||||
@ -72,7 +73,7 @@ AppendToStreamOperation.prototype._inspectResponse = function(response) {
|
||||
};
|
||||
|
||||
AppendToStreamOperation.prototype._transformResponse = function(response) {
|
||||
return new WriteResult(response.last_event_number, new Position(response.prepare_position || -1, response.commit_position || -1));
|
||||
return new WriteResult(response.lastEventNumber, new Position(response.preparePosition || -1, response.commitPosition || -1));
|
||||
};
|
||||
|
||||
AppendToStreamOperation.prototype.toString = function() {
|
||||
|
@ -23,7 +23,10 @@ function CommitTransactionOperation(log, cb, requireMaster, transactionId, userC
|
||||
util.inherits(CommitTransactionOperation, OperationBase);
|
||||
|
||||
CommitTransactionOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.TransactionCommit(this._transactionId, this._requireMaster);
|
||||
return new ClientMessage.TransactionCommit({
|
||||
transactionId: this._transactionId,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
CommitTransactionOperation.prototype._inspectResponse = function(response) {
|
||||
@ -56,8 +59,8 @@ CommitTransactionOperation.prototype._inspectResponse = function(response) {
|
||||
};
|
||||
|
||||
CommitTransactionOperation.prototype._transformResponse = function(response) {
|
||||
var logPosition = new results.Position(response.prepare_position || -1, response.commit_position || -1);
|
||||
return new results.WriteResult(response.last_event_number, logPosition);
|
||||
var logPosition = new results.Position(response.preparePosition || -1, response.commitPosition || -1);
|
||||
return new results.WriteResult(response.lastEventNumber, logPosition);
|
||||
};
|
||||
|
||||
CommitTransactionOperation.prototype.toString = function() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var guidParse = require('../common/guid-parse');
|
||||
|
||||
var SubscriptionOperation = require('./subscriptionOperation');
|
||||
var ClientMessage = require('../messages/clientMessage');
|
||||
@ -27,21 +27,25 @@ function ConnectToPersistentSubscriptionOperation(
|
||||
util.inherits(ConnectToPersistentSubscriptionOperation, SubscriptionOperation);
|
||||
|
||||
ConnectToPersistentSubscriptionOperation.prototype._createSubscriptionPackage = function() {
|
||||
var dto = new ClientMessage.ConnectToPersistentSubscription(this._groupName, this._streamId, this._bufferSize);
|
||||
var dto = new ClientMessage.ConnectToPersistentSubscription({
|
||||
subscriptionId: this._groupName,
|
||||
eventStreamId: this._streamId,
|
||||
allowedInFlightMessages: this._bufferSize
|
||||
});
|
||||
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,
|
||||
createBufferSegment(dto.toBuffer()));
|
||||
createBufferSegment(ClientMessage.ConnectToPersistentSubscription.encode(dto).finish()));
|
||||
};
|
||||
|
||||
ConnectToPersistentSubscriptionOperation.prototype._inspectPackage = function(pkg) {
|
||||
if (pkg.command === TcpCommand.PersistentSubscriptionConfirmation)
|
||||
{
|
||||
var dto = ClientMessage.PersistentSubscriptionConfirmation.decode(pkg.data.toBuffer());
|
||||
this._confirmSubscription(dto.last_commit_position, dto.last_event_number);
|
||||
this._subscriptionId = dto.subscription_id;
|
||||
this._confirmSubscription(dto.lastCommitPosition, dto.lastEventNumber);
|
||||
this._subscriptionId = dto.subscriptionId;
|
||||
return new InspectionResult(InspectionDecision.Subscribed, "SubscriptionConfirmation");
|
||||
}
|
||||
if (pkg.command === TcpCommand.PersistentSubscriptionStreamEventAppeared)
|
||||
@ -86,9 +90,9 @@ ConnectToPersistentSubscriptionOperation.prototype._createSubscriptionObject = f
|
||||
ConnectToPersistentSubscriptionOperation.prototype.notifyEventsProcessed = function(processedEvents) {
|
||||
ensure.notNull(processedEvents, "processedEvents");
|
||||
var dto = new ClientMessage.PersistentSubscriptionAckEvents({
|
||||
subscription_id: this._subscriptionId,
|
||||
processed_event_ids: processedEvents.map(function (x) {
|
||||
return new Buffer(uuid.parse(x));
|
||||
subscriptionId: this._subscriptionId,
|
||||
processedEventIds: processedEvents.map(function (x) {
|
||||
return guidParse.parse(x);
|
||||
})
|
||||
});
|
||||
|
||||
@ -97,25 +101,26 @@ ConnectToPersistentSubscriptionOperation.prototype.notifyEventsProcessed = funct
|
||||
this._correlationId,
|
||||
this._userCredentials !== null ? this._userCredentials.username : null,
|
||||
this._userCredentials !== null ? this._userCredentials.password : null,
|
||||
createBufferSegment(dto.encode().toBuffer()));
|
||||
createBufferSegment(ClientMessage.PersistentSubscriptionAckEvents.encode(dto).finish()));
|
||||
this._enqueueSend(pkg);
|
||||
};
|
||||
|
||||
ConnectToPersistentSubscriptionOperation.prototype.notifyEventsFailed = function(processedEvents, action, reason) {
|
||||
ensure.notNull(processedEvents, "processedEvents");
|
||||
ensure.notNull(reason, "reason");
|
||||
var dto = new ClientMessage.PersistentSubscriptionNakEvents(
|
||||
this._subscriptionId,
|
||||
processedEvents.map(function(x) { return new Buffer(uuid.parse(x)); }),
|
||||
reason,
|
||||
action);
|
||||
var dto = new ClientMessage.PersistentSubscriptionNakEvents({
|
||||
subscriptionId: this._subscriptionId,
|
||||
processedEventIds: processedEvents.map(function(x) { return guidParse.parse(x); }),
|
||||
message: reason,
|
||||
action: action
|
||||
});
|
||||
|
||||
var pkg = new TcpPackage(TcpCommand.PersistentSubscriptionNakEvents,
|
||||
this._userCredentials !== null ? TcpFlags.Authenticated : TcpFlags.None,
|
||||
this._correlationId,
|
||||
this._userCredentials !== null ? this._userCredentials.username : null,
|
||||
this._userCredentials !== null ? this._userCredentials.password : null,
|
||||
createBufferSegment(dto.toBuffer()));
|
||||
createBufferSegment(ClientMessage.PersistentSubscriptionNakEvents.encode(dto).finish()));
|
||||
this._enqueueSend(pkg);
|
||||
};
|
||||
|
||||
|
@ -36,11 +36,24 @@ function CreatePersistentSubscriptionOperation(log, cb, stream, groupName, setti
|
||||
util.inherits(CreatePersistentSubscriptionOperation, OperationBase);
|
||||
|
||||
CreatePersistentSubscriptionOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.CreatePersistentSubscription(this._groupName, this._stream, this._resolveLinkTos,
|
||||
this._startFromBeginning, this._messageTimeoutMilliseconds, this._recordStatistics, this._liveBufferSize,
|
||||
this._readBatchSize, this._bufferSize, this._maxRetryCount,
|
||||
this._namedConsumerStrategy === SystemConsumerStrategies.RoundRobin, this._checkPointAfter,
|
||||
this._maxCheckPointCount, this._minCheckPointCount, this._maxSubscriberCount, this._namedConsumerStrategy);
|
||||
return new ClientMessage.CreatePersistentSubscription({
|
||||
subscriptionGroupName: this._groupName,
|
||||
eventStreamId: this._stream,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
startFrom: this._startFromBeginning,
|
||||
messageTimeoutMilliseconds: this._messageTimeoutMilliseconds,
|
||||
recordStatistics: this._recordStatistics,
|
||||
liveBufferSize: this._liveBufferSize,
|
||||
readBatchSize: this._readBatchSize,
|
||||
bufferSize: this._bufferSize,
|
||||
maxRetryCount: this._maxRetryCount,
|
||||
preferRoundRobin: this._namedConsumerStrategy === SystemConsumerStrategies.RoundRobin,
|
||||
checkpointAfterTime: this._checkPointAfter,
|
||||
checkpointMaxCount: this._maxCheckPointCount,
|
||||
checkpointMinCount: this._minCheckPointCount,
|
||||
subscriberMaxCount: this._maxSubscriberCount,
|
||||
namedConsumerStrategy: this._namedConsumerStrategy
|
||||
});
|
||||
};
|
||||
|
||||
CreatePersistentSubscriptionOperation.prototype._inspectResponse = function(response) {
|
||||
|
@ -21,7 +21,10 @@ function DeletePersistentSubscriptionOperation(log, cb, stream, groupName, userC
|
||||
util.inherits(DeletePersistentSubscriptionOperation, OperationBase);
|
||||
|
||||
DeletePersistentSubscriptionOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.DeletePersistentSubscription(this._groupName, this._stream);
|
||||
return new ClientMessage.DeletePersistentSubscription({
|
||||
subscriptionGroupName: this._groupName,
|
||||
eventStreamId: this._stream
|
||||
});
|
||||
};
|
||||
|
||||
DeletePersistentSubscriptionOperation.prototype._inspectResponse = function(response) {
|
||||
|
@ -25,7 +25,12 @@ function DeleteStreamOperation(log, cb, requireMaster, stream, expectedVersion,
|
||||
util.inherits(DeleteStreamOperation, OperationBase);
|
||||
|
||||
DeleteStreamOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.DeleteStream(this._stream, this._expectedVersion, this._requireMaster, this._hardDelete);
|
||||
return new ClientMessage.DeleteStream({
|
||||
eventStreamId: this._stream,
|
||||
expectedVersion: this._expectedVersion,
|
||||
requireMaster: this._requireMaster,
|
||||
hardDelete: this._hardDelete
|
||||
});
|
||||
};
|
||||
|
||||
DeleteStreamOperation.prototype._inspectResponse = function(response) {
|
||||
@ -58,7 +63,7 @@ DeleteStreamOperation.prototype._inspectResponse = function(response) {
|
||||
};
|
||||
|
||||
DeleteStreamOperation.prototype._transformResponse = function(response) {
|
||||
return new results.DeleteResult(new results.Position(response.prepare_position || -1, response.commit_position || -1));
|
||||
return new results.DeleteResult(new results.Position(response.preparePosition || -1, response.commitPosition || -1));
|
||||
};
|
||||
|
||||
DeleteStreamOperation.prototype.toString = function() {
|
||||
|
@ -51,7 +51,7 @@ OperationBase.prototype._succeed = function() {
|
||||
|
||||
OperationBase.prototype.createNetworkPackage = function(correlationId) {
|
||||
var dto = this._createRequestDto();
|
||||
var buf = dto.toBuffer();
|
||||
var buf = dto.constructor.encode(dto).finish();
|
||||
return new TcpPackage(
|
||||
this._requestCommand,
|
||||
this.userCredentials ? TcpFlags.Authenticated : TcpFlags.None,
|
||||
@ -117,10 +117,10 @@ OperationBase.prototype._inspectNotHandled = function(pkg)
|
||||
return new InspectionResult(InspectionDecision.Retry, "NotHandled - TooBusy");
|
||||
|
||||
case ClientMessage.NotHandled.NotHandledReason.NotMaster:
|
||||
var masterInfo = ClientMessage.NotHandled.MasterInfo.decode(message.additional_info);
|
||||
var masterInfo = ClientMessage.NotHandled.MasterInfo.decode(message.additionalInfo);
|
||||
return new InspectionResult(InspectionDecision.Reconnect, "NotHandled - NotMaster",
|
||||
{host: masterInfo.external_tcp_address, port: masterInfo.external_tcp_port},
|
||||
{host: masterInfo.external_secure_tcp_address, port: masterInfo.external_secure_tcp_port});
|
||||
{host: masterInfo.externalTcpAddress, port: masterInfo.externalTcpPort},
|
||||
{host: masterInfo.externalSecureTcpAddress, port: masterInfo.externalSecureTcpPort});
|
||||
|
||||
default:
|
||||
this.log.error("Unknown NotHandledReason: %s.", message.reason);
|
||||
|
@ -25,7 +25,13 @@ function ReadAllEventsBackwardOperation(
|
||||
util.inherits(ReadAllEventsBackwardOperation, OperationBase);
|
||||
|
||||
ReadAllEventsBackwardOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.ReadAllEvents(this._position.commitPosition, this._position.preparePosition, this._maxCount, this._resolveLinkTos, this._requireMaster);
|
||||
return new ClientMessage.ReadAllEvents({
|
||||
commitPosition: this._position.commitPosition,
|
||||
preparePosition: this._position.preparePosition,
|
||||
maxCount: this._maxCount,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
ReadAllEventsBackwardOperation.prototype._inspectResponse = function(response) {
|
||||
@ -48,8 +54,8 @@ ReadAllEventsBackwardOperation.prototype._inspectResponse = function(response) {
|
||||
ReadAllEventsBackwardOperation.prototype._transformResponse = function(response) {
|
||||
return new results.AllEventsSlice(
|
||||
ReadDirection.Backward,
|
||||
new results.Position(response.commit_position, response.prepare_position),
|
||||
new results.Position(response.next_commit_position, response.next_prepare_position),
|
||||
new results.Position(response.commitPosition, response.preparePosition),
|
||||
new results.Position(response.nextCommitPosition, response.nextPreparePosition),
|
||||
response.events
|
||||
)
|
||||
};
|
||||
|
@ -25,7 +25,13 @@ function ReadAllEventsForwardOperation(
|
||||
util.inherits(ReadAllEventsForwardOperation, OperationBase);
|
||||
|
||||
ReadAllEventsForwardOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.ReadAllEvents(this._position.commitPosition, this._position.preparePosition, this._maxCount, this._resolveLinkTos, this._requireMaster);
|
||||
return new ClientMessage.ReadAllEvents({
|
||||
commitPosition: this._position.commitPosition,
|
||||
preparePosition: this._position.preparePosition,
|
||||
maxCount: this._maxCount,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
ReadAllEventsForwardOperation.prototype._inspectResponse = function(response) {
|
||||
@ -48,8 +54,8 @@ ReadAllEventsForwardOperation.prototype._inspectResponse = function(response) {
|
||||
ReadAllEventsForwardOperation.prototype._transformResponse = function(response) {
|
||||
return new results.AllEventsSlice(
|
||||
ReadDirection.Forward,
|
||||
new results.Position(response.commit_position, response.prepare_position),
|
||||
new results.Position(response.next_commit_position, response.next_prepare_position),
|
||||
new results.Position(response.commitPosition, response.preparePosition),
|
||||
new results.Position(response.nextCommitPosition, response.nextPreparePosition),
|
||||
response.events
|
||||
)
|
||||
};
|
||||
|
@ -21,7 +21,12 @@ function ReadEventOperation(log, cb, stream, eventNumber, resolveLinkTos, requir
|
||||
util.inherits(ReadEventOperation, OperationBase);
|
||||
|
||||
ReadEventOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.ReadEvent(this._stream, this._eventNumber, this._resolveLinkTos, this._requireMaster);
|
||||
return new ClientMessage.ReadEvent({
|
||||
eventStreamId: this._stream,
|
||||
eventNumber: this._eventNumber,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
ReadEventOperation.prototype._inspectResponse = function(response) {
|
||||
|
@ -27,7 +27,13 @@ function ReadStreamEventsBackwardOperation(
|
||||
util.inherits(ReadStreamEventsBackwardOperation, OperationBase);
|
||||
|
||||
ReadStreamEventsBackwardOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.ReadStreamEvents(this._stream, this._fromEventNumber, this._maxCount, this._resolveLinkTos, this._requireMaster);
|
||||
return new ClientMessage.ReadStreamEvents({
|
||||
eventStreamId: this._stream,
|
||||
fromEventNumber: this._fromEventNumber,
|
||||
maxCount: this._maxCount,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
ReadStreamEventsBackwardOperation.prototype._inspectResponse = function(response) {
|
||||
@ -60,9 +66,9 @@ ReadStreamEventsBackwardOperation.prototype._transformResponse = function(respon
|
||||
this._fromEventNumber,
|
||||
ReadDirection.Backward,
|
||||
response.events,
|
||||
response.next_event_number,
|
||||
response.last_event_number,
|
||||
response.is_end_of_stream
|
||||
response.nextEventNumber,
|
||||
response.lastEventNumber,
|
||||
response.isEndOfStream
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -27,7 +27,13 @@ function ReadStreamEventsForwardOperation(
|
||||
util.inherits(ReadStreamEventsForwardOperation, OperationBase);
|
||||
|
||||
ReadStreamEventsForwardOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.ReadStreamEvents(this._stream, this._fromEventNumber, this._maxCount, this._resolveLinkTos, this._requireMaster);
|
||||
return new ClientMessage.ReadStreamEvents({
|
||||
eventStreamId: this._stream,
|
||||
fromEventNumber: this._fromEventNumber,
|
||||
maxCount: this._maxCount,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
ReadStreamEventsForwardOperation.prototype._inspectResponse = function(response) {
|
||||
@ -60,9 +66,9 @@ ReadStreamEventsForwardOperation.prototype._transformResponse = function(respons
|
||||
this._fromEventNumber,
|
||||
ReadDirection.Forward,
|
||||
response.events,
|
||||
response.next_event_number,
|
||||
response.last_event_number,
|
||||
response.is_end_of_stream
|
||||
response.nextEventNumber,
|
||||
response.lastEventNumber,
|
||||
response.isEndOfStream
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -24,7 +24,11 @@ function StartTransactionOperation(log, cb, requireMaster, stream, expectedVersi
|
||||
util.inherits(StartTransactionOperation, OperationBase);
|
||||
|
||||
StartTransactionOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.TransactionStart(this._stream, this._expectedVersion, this._requireMaster);
|
||||
return new ClientMessage.TransactionStart({
|
||||
eventStreamId: this._stream,
|
||||
expectedVersion: this._expectedVersion,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
};
|
||||
|
||||
StartTransactionOperation.prototype._inspectResponse = function(response) {
|
||||
@ -57,7 +61,7 @@ StartTransactionOperation.prototype._inspectResponse = function(response) {
|
||||
};
|
||||
|
||||
StartTransactionOperation.prototype._transformResponse = function(response) {
|
||||
return new EventStoreTransaction(response.transaction_id, this.userCredentials, this._parentConnection);
|
||||
return new EventStoreTransaction(response.transactionId, this.userCredentials, this._parentConnection);
|
||||
};
|
||||
|
||||
StartTransactionOperation.prototype.toString = function() {
|
||||
|
@ -64,7 +64,7 @@ SubscriptionOperation.prototype.unsubscribe = function() {
|
||||
|
||||
SubscriptionOperation.prototype._createUnsubscriptionPackage = function() {
|
||||
var msg = new ClientMessage.UnsubscribeFromStream();
|
||||
var data = new BufferSegment(msg.toBuffer());
|
||||
var data = new BufferSegment(ClientMessage.UnsubscribeFromStream.encode(msg).finish());
|
||||
return new TcpPackage(TcpCommand.UnsubscribeFromStream, TcpFlags.None, this._correlationId, null, null, data);
|
||||
};
|
||||
|
||||
@ -141,10 +141,10 @@ SubscriptionOperation.prototype.inspectPackage = function(pkg) {
|
||||
return new InspectionResult(InspectionDecision.Retry, "NotHandled - TooBusy");
|
||||
|
||||
case ClientMessage.NotHandled.NotHandledReason.NotMaster:
|
||||
var masterInfo = ClientMessage.NotHandled.MasterInfo.decode(message.additional_info);
|
||||
var masterInfo = ClientMessage.NotHandled.MasterInfo.decode(message.additionalInfo);
|
||||
return new InspectionResult(InspectionDecision.Reconnect, "NotHandled - NotMaster",
|
||||
{host: masterInfo.external_tcp_address, port: masterInfo.external_tcp_port},
|
||||
{host: masterInfo.external_secure_tcp_address, port: masterInfo.external_secure_tcp_port});
|
||||
{host: masterInfo.externalTcpAddress, port: masterInfo.externalTcpPort},
|
||||
{host: masterInfo.externalSecureTcpAddress, port: masterInfo.externalSecureTcpPort});
|
||||
|
||||
default:
|
||||
this._log.error("Unknown NotHandledReason: %s.", message.reason);
|
||||
@ -232,7 +232,7 @@ SubscriptionOperation.prototype._onEventAppeared = function(e) {
|
||||
e.originalStreamId, e.originalEventNumber, e.originalEvent.eventType, e.originalPosition);
|
||||
|
||||
var self = this;
|
||||
this._executeAction(function() { self._eventAppeared(self._subscription, e); });
|
||||
this._executeAction(function() { return self._eventAppeared(self._subscription, e); });
|
||||
};
|
||||
|
||||
SubscriptionOperation.prototype._executeAction = function(action) {
|
||||
@ -244,21 +244,30 @@ SubscriptionOperation.prototype._executeAction = function(action) {
|
||||
};
|
||||
|
||||
SubscriptionOperation.prototype._executeActions = function() {
|
||||
//TODO: possible blocking loop for node.js
|
||||
var action = this._actionQueue.shift();
|
||||
while (action)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
catch (err)
|
||||
{
|
||||
this._log.error(err, "Exception during executing user callback: %s.", err.message);
|
||||
}
|
||||
action = this._actionQueue.shift();
|
||||
if (!action) {
|
||||
this._actionExecuting = false;
|
||||
return;
|
||||
}
|
||||
var promise;
|
||||
try
|
||||
{
|
||||
promise = action();
|
||||
}
|
||||
catch (err)
|
||||
{
|
||||
this._log.error(err, "Exception during executing user callback: %s.", err.message);
|
||||
}
|
||||
if (promise && promise.then) {
|
||||
var self = this;
|
||||
promise
|
||||
.catch(function (err) {
|
||||
self._log.error(err, "Exception during executing user callback: %s.", err.message);
|
||||
})
|
||||
.then(this._executeActions.bind(this));
|
||||
} else {
|
||||
setImmediate(this._executeActions.bind(this));
|
||||
}
|
||||
this._actionExecuting = false;
|
||||
};
|
||||
|
||||
SubscriptionOperation.prototype.toString = function() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var guidParse = require('../common/guid-parse');
|
||||
|
||||
var TcpCommand = require('../systemData/tcpCommand');
|
||||
var InspectionDecision = require('../systemData/inspectionDecision');
|
||||
@ -22,13 +22,18 @@ util.inherits(TransactionalWriteOperation, OperationBase);
|
||||
|
||||
TransactionalWriteOperation.prototype._createRequestDto = function() {
|
||||
var dtos = this._events.map(function(ev) {
|
||||
var eventId = new Buffer(uuid.parse(ev.eventId));
|
||||
return new ClientMessage.NewEvent({
|
||||
event_id: eventId, event_type: ev.type,
|
||||
data_content_type: ev.isJson ? 1 : 0, metadata_content_type: 0,
|
||||
data: ev.data, metadata: ev.metadata});
|
||||
var eventId = guidParse.parse(ev.eventId);
|
||||
return {
|
||||
eventId: eventId, eventType: ev.type,
|
||||
dataContentType: ev.isJson ? 1 : 0, metadataContentType: 0,
|
||||
data: ev.data, metadata: ev.metadata
|
||||
};
|
||||
});
|
||||
return new ClientMessage.TransactionWrite({
|
||||
transactionId: this._transactionId,
|
||||
events: dtos,
|
||||
requireMaster: this._requireMaster
|
||||
});
|
||||
return new ClientMessage.TransactionWrite(this._transactionId, dtos, this._requireMaster);
|
||||
};
|
||||
|
||||
TransactionalWriteOperation.prototype._inspectResponse = function(response) {
|
||||
|
@ -36,11 +36,24 @@ function UpdatePersistentSubscriptionOperation(log, cb, stream, groupName, setti
|
||||
util.inherits(UpdatePersistentSubscriptionOperation, OperationBase);
|
||||
|
||||
UpdatePersistentSubscriptionOperation.prototype._createRequestDto = function() {
|
||||
return new ClientMessage.UpdatePersistentSubscription(this._groupName, this._stream, this._resolveLinkTos,
|
||||
this._startFromBeginning, this._messageTimeoutMilliseconds, this._recordStatistics, this._liveBufferSize,
|
||||
this._readBatchSize, this._bufferSize, this._maxRetryCount,
|
||||
this._namedConsumerStrategy === SystemConsumerStrategies.RoundRobin, this._checkPointAfter,
|
||||
this._maxCheckPointCount, this._minCheckPointCount, this._maxSubscriberCount, this._namedConsumerStrategy);
|
||||
return new ClientMessage.UpdatePersistentSubscription({
|
||||
subscriptionGroupName: this._groupName,
|
||||
eventStreamId: this._stream,
|
||||
resolveLinkTos: this._resolveLinkTos,
|
||||
startFrom: this._startFromBeginning,
|
||||
messageTimeoutMilliseconds: this._messageTimeoutMilliseconds,
|
||||
recordStatistics: this._recordStatistics,
|
||||
liveBufferSize: this._liveBufferSize,
|
||||
readBatchSize: this._readBatchSize,
|
||||
bufferSize: this._bufferSize,
|
||||
maxRetryCount: this._maxRetryCount,
|
||||
preferRoundRobin: this._namedConsumerStrategy === SystemConsumerStrategies.RoundRobin,
|
||||
checkpointAfterTime: this._checkPointAfter,
|
||||
checkpointMaxCount: this._maxCheckPointCount,
|
||||
checkpointMinCount: this._minCheckPointCount,
|
||||
subscriberMaxCount: this._maxSubscriberCount,
|
||||
namedConsumerStrategy: this._namedConsumerStrategy
|
||||
});
|
||||
};
|
||||
|
||||
UpdatePersistentSubscriptionOperation.prototype._inspectResponse = function(response) {
|
||||
|
@ -20,20 +20,23 @@ function VolatileSubscriptionOperation(
|
||||
util.inherits(VolatileSubscriptionOperation, SubscriptionOperation);
|
||||
|
||||
VolatileSubscriptionOperation.prototype._createSubscriptionPackage = function() {
|
||||
var dto = new ClientMessage.SubscribeToStream(this._streamId, this._resolveLinkTos);
|
||||
var dto = new ClientMessage.SubscribeToStream({
|
||||
eventStreamId: this._streamId,
|
||||
resolveLinkTos: this._resolveLinkTos
|
||||
});
|
||||
return new TcpPackage(TcpCommand.SubscribeToStream,
|
||||
this._userCredentials !== null ? TcpFlags.Authenticated : TcpFlags.None,
|
||||
this._correlationId,
|
||||
this._userCredentials !== null ? this._userCredentials.username : null,
|
||||
this._userCredentials !== null ? this._userCredentials.password : null,
|
||||
new BufferSegment(dto.toBuffer()));
|
||||
new BufferSegment(ClientMessage.SubscribeToStream.encode(dto).finish()));
|
||||
};
|
||||
|
||||
VolatileSubscriptionOperation.prototype._inspectPackage = function(pkg) {
|
||||
try {
|
||||
if (pkg.command === TcpCommand.SubscriptionConfirmation) {
|
||||
var dto = ClientMessage.SubscriptionConfirmation.decode(pkg.data.toBuffer());
|
||||
this._confirmSubscription(dto.last_commit_position, dto.last_event_number);
|
||||
this._confirmSubscription(dto.lastCommitPosition, dto.lastEventNumber);
|
||||
return new InspectionResult(InspectionDecision.Subscribed, "SubscriptionConfirmation");
|
||||
}
|
||||
if (pkg.command === TcpCommand.StreamEventAppeared) {
|
||||
|
47
src/common/guid-parse.js
Normal file
47
src/common/guid-parse.js
Normal file
@ -0,0 +1,47 @@
|
||||
'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 < 16) { // Don't overflow!
|
||||
buf[i + ii++] = _hexToByte[oct];
|
||||
}
|
||||
});
|
||||
|
||||
var buf2 = new Buffer(buf.slice(i, i + 16));
|
||||
buf[i + 0] = buf2[3];
|
||||
buf[i + 1] = buf2[2];
|
||||
buf[i + 2] = buf2[1];
|
||||
buf[i + 3] = buf2[0];
|
||||
buf[i + 4] = buf2[5];
|
||||
buf[i + 5] = buf2[4];
|
||||
buf[i + 6] = buf2[7];
|
||||
buf[i + 7] = buf2[6];
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
|
||||
function unparse(buf, offset) {
|
||||
var i = offset || 0;
|
||||
return '03020100-0504-0706-0809-101112131415'.replace(/\d{2}/g, function (num) {
|
||||
var j = parseInt(num, 10);
|
||||
return _byteToHex[buf[i+j]];
|
||||
})
|
||||
}
|
||||
|
||||
exports.parse = parse;
|
||||
exports.unparse = unparse;
|
@ -12,5 +12,6 @@ const SystemMetadata = {
|
||||
userStreamAcl: '$userStreamAcl',
|
||||
systemStreamAcl: '$systemStreamAcl'
|
||||
};
|
||||
Object.freeze(SystemMetadata);
|
||||
|
||||
module.exports = SystemMetadata;
|
@ -1,3 +1,5 @@
|
||||
var Long = require('long');
|
||||
|
||||
module.exports.notNullOrEmpty = function(value, name) {
|
||||
if (value === null)
|
||||
throw new TypeError(name + " should not be null.");
|
||||
@ -10,9 +12,18 @@ module.exports.notNull = function(value, name) {
|
||||
throw new TypeError(name + " should not be null.");
|
||||
};
|
||||
|
||||
module.exports.isInteger = function(value, name) {
|
||||
function isInteger(value, name) {
|
||||
if (typeof value !== 'number' || value % 1 !== 0)
|
||||
throw new TypeError(name + " should be an integer.");
|
||||
}
|
||||
module.exports.isInteger = isInteger;
|
||||
|
||||
module.exports.isLongOrInteger = function(value, name) {
|
||||
if (typeof value === 'number') {
|
||||
return isInteger(value, name);
|
||||
} else if (!Long.isLong(value)) {
|
||||
throw new TypeError(name + " should be a Long|number.");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.isArrayOf = function(expectedType, value, name) {
|
||||
|
@ -9,6 +9,8 @@ var SubscriptionsManager = require('./subscriptionsManager');
|
||||
var VolatileSubscriptionOperation = require('../clientOperations/volatileSubscriptionOperation');
|
||||
var ConnectToPersistentSubscriptionOperation = require('../clientOperations/connectToPersistentSubscriptionOperation');
|
||||
var messages = require('./messages');
|
||||
var ClientMessage = require('../messages/clientMessage');
|
||||
var createBufferSegment = require('../common/bufferSegment');
|
||||
|
||||
var TcpPackage = require('../systemData/tcpPackage');
|
||||
var TcpCommand = require('../systemData/tcpCommand');
|
||||
@ -28,12 +30,14 @@ const ConnectingPhase = {
|
||||
EndPointDiscovery: 'endpointDiscovery',
|
||||
ConnectionEstablishing: 'connectionEstablishing',
|
||||
Authentication: 'authentication',
|
||||
Identification: 'identification',
|
||||
Connected: 'connected'
|
||||
};
|
||||
|
||||
const TimerPeriod = 200;
|
||||
const TimerTickMessage = new messages.TimerTickMessage();
|
||||
const EmptyGuid = '00000000-0000-0000-0000-000000000000';
|
||||
const ClientVersion = 1;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@ -43,6 +47,7 @@ const EmptyGuid = '00000000-0000-0000-0000-000000000000';
|
||||
* @property {Number} totalOperationCount
|
||||
*/
|
||||
function EventStoreConnectionLogicHandler(esConnection, settings) {
|
||||
EventEmitter.call(this);
|
||||
this._esConnection = esConnection;
|
||||
this._settings = settings;
|
||||
this._queue = new SimpleQueuedHandler();
|
||||
@ -391,10 +396,21 @@ EventStoreConnectionLogicHandler.prototype._tcpConnectionEstablished = function(
|
||||
}
|
||||
else
|
||||
{
|
||||
this._goToConnectedState();
|
||||
this._goToIdentifiedState();
|
||||
}
|
||||
};
|
||||
|
||||
EventStoreConnectionLogicHandler.prototype._goToIdentifiedState = function() {
|
||||
this._connectingPhase = ConnectingPhase.Identification;
|
||||
this._identityInfo = {
|
||||
correlationId: uuid.v4(),
|
||||
timeStamp: Date.now()
|
||||
};
|
||||
var dto = new ClientMessage.IdentifyClient({version: ClientVersion, connectionName: this._esConnection.connectionName});
|
||||
var buf = dto.constructor.encode(dto).finish();
|
||||
this._connection.enqueueSend(new TcpPackage(TcpCommand.IdentifyClient, TcpFlags.None, this._identityInfo.correlationId, null, null, createBufferSegment(buf)))
|
||||
};
|
||||
|
||||
EventStoreConnectionLogicHandler.prototype._goToConnectedState = function() {
|
||||
this._state = ConnectionState.Connected;
|
||||
this._connectingPhase = ConnectingPhase.Connected;
|
||||
@ -460,7 +476,26 @@ EventStoreConnectionLogicHandler.prototype._handleTcpPackage = function(connecti
|
||||
this._packageNumber += 1;
|
||||
|
||||
if (pkg.command === TcpCommand.HeartbeatResponseCommand)
|
||||
{
|
||||
if (pkg.correlationId === this._heartbeatInfo.correlationId)
|
||||
{
|
||||
var now = Date.now();
|
||||
var heartbeatEvent = {
|
||||
connectionId: this._connection.connectionId,
|
||||
remoteEndPoint: this._connection.remoteEndPoint,
|
||||
requestSentAt: this._heartbeatInfo.timeStamp,
|
||||
requestPkgNumber: this._heartbeatInfo.lastPackageNumber,
|
||||
responseReceivedAt: now,
|
||||
responsePkgNumber: this._packageNumber
|
||||
};
|
||||
try {
|
||||
this.emit('heartbeatInfo', heartbeatEvent);
|
||||
} catch(e) {
|
||||
this._logDebug("IGNORED: emit heartbeat event failed.\n%s", e.stack);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (pkg.command === TcpCommand.HeartbeatRequestCommand)
|
||||
{
|
||||
this._connection.enqueueSend(new TcpPackage(
|
||||
@ -479,6 +514,16 @@ EventStoreConnectionLogicHandler.prototype._handleTcpPackage = function(connecti
|
||||
if (pkg.command === TcpCommand.NotAuthenticated)
|
||||
this.emit('authenticationFailed', "Not authenticated");
|
||||
|
||||
this._goToIdentifiedState();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (pkg.command === TcpCommand.ClientIdentified)
|
||||
{
|
||||
if (this._state === ConnectionState.Connecting
|
||||
&& this._identityInfo.correlationId === pkg.correlationId)
|
||||
{
|
||||
this._goToConnectedState();
|
||||
return;
|
||||
}
|
||||
@ -598,7 +643,11 @@ EventStoreConnectionLogicHandler.prototype._timerTick = function() {
|
||||
else if (this._connectingPhase === ConnectingPhase.Authentication && (Date.now() - this._authInfo.timeStamp) >= this._settings.operationTimeout)
|
||||
{
|
||||
this.emit('authenticationFailed', "Authentication timed out.");
|
||||
this._goToConnectedState();
|
||||
if (this._clientVersion === 1) {
|
||||
this._goToIdentifiedState();
|
||||
} else {
|
||||
this._goToConnectedState();
|
||||
}
|
||||
}
|
||||
else if (this._connectingPhase === ConnectingPhase.Authentication || this._connectingPhase === ConnectingPhase.Connected)
|
||||
this._manageHeartbeats();
|
||||
@ -640,12 +689,13 @@ EventStoreConnectionLogicHandler.prototype._manageHeartbeats = function() {
|
||||
|
||||
if (this._heartbeatInfo.isIntervalStage)
|
||||
{
|
||||
var correlationId = uuid.v4();
|
||||
// TcpMessage.Heartbeat analog
|
||||
this._connection.enqueueSend(new TcpPackage(
|
||||
TcpCommand.HeartbeatRequestCommand,
|
||||
TcpFlags.None,
|
||||
uuid.v4()));
|
||||
this._heartbeatInfo = {lastPackageNumber: this._heartbeatInfo.lastPackageNumber, isIntervalStage: false, timeStamp: Date.now()};
|
||||
correlationId));
|
||||
this._heartbeatInfo = {correlationId: correlationId, lastPackageNumber: this._heartbeatInfo.lastPackageNumber, isIntervalStage: false, timeStamp: Date.now()};
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -8,11 +8,13 @@ function AccessDeniedError(action, streamOrTransactionId) {
|
||||
if (typeof streamOrTransactionId === 'string') {
|
||||
this.message = util.format("%s access denied for stream '%s'.", action, streamOrTransactionId);
|
||||
this.stream = streamOrTransactionId;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
if (Long.isLong(streamOrTransactionId)) {
|
||||
this.message = util.format("%s access denied for transaction %s.", action, streamOrTransactionId);
|
||||
this.transactionId = streamOrTransactionId;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
throw new TypeError("second argument must be a stream name or transaction Id.");
|
||||
|
10
src/errors/projectionCommandFailedError.js
Normal file
10
src/errors/projectionCommandFailedError.js
Normal file
@ -0,0 +1,10 @@
|
||||
const util = require('util');
|
||||
|
||||
function ProjectionCommandFailedError(httpStatusCode, message) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
this.httpStatusCode = httpStatusCode;
|
||||
this.message = message;
|
||||
}
|
||||
util.inherits(ProjectionCommandFailedError, Error);
|
||||
|
||||
module.exports = ProjectionCommandFailedError;
|
@ -7,11 +7,13 @@ function StreamDeletedError(streamOrTransactionId) {
|
||||
if (typeof streamOrTransactionId === 'string') {
|
||||
this.message = util.format("Event stream '%s' is deleted.", streamOrTransactionId);
|
||||
this.stream = streamOrTransactionId;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
if (Long.isLong(streamOrTransactionId)) {
|
||||
this.message = util.format("Stream is deleted for transaction %s.", streamOrTransactionId);
|
||||
this.transactionId = streamOrTransactionId;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
throw new TypeError("second argument must be a stream name or transaction Id.");
|
||||
|
@ -9,11 +9,13 @@ function WrongExpectedVersionError(action, streamOrTransactionId, expectedVersio
|
||||
this.message = util.format("%s failed due to WrongExpectedVersion. Stream: %s Expected version: %d.", action, streamOrTransactionId, expectedVersion);
|
||||
this.stream = streamOrTransactionId;
|
||||
this.expectedVersion = expectedVersion;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
if (Long.isLong(streamOrTransactionId)) {
|
||||
this.message = util.format("%s transaction failed due to WrongExpectedVersion. Transaction Id: %s.", action, streamOrTransactionId);
|
||||
this.transactionId = streamOrTransactionId;
|
||||
Object.freeze(this);
|
||||
return;
|
||||
}
|
||||
throw new TypeError("second argument must be a stream name or a transaction Id.");
|
||||
|
@ -1,13 +1,9 @@
|
||||
var uuid = require('uuid');
|
||||
|
||||
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
function isValidId(id) {
|
||||
if (typeof id !== 'string') return false;
|
||||
var buf = uuid.parse(id);
|
||||
var valid = false;
|
||||
for(var i=0;i<buf.length;i++)
|
||||
if (buf[i] !== 0)
|
||||
valid = true;
|
||||
return valid;
|
||||
return uuidRegex.test(id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -32,6 +28,7 @@ function EventData(eventId, type, isJson, data, metadata) {
|
||||
this.isJson = isJson || false;
|
||||
this.data = data || new Buffer(0);
|
||||
this.metadata = metadata || new Buffer(0);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
module.exports = EventData;
|
||||
|
@ -24,14 +24,10 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
|
||||
var self = this;
|
||||
|
||||
function processEvents(events, index) {
|
||||
index = index || 0;
|
||||
if (index >= events.length) return Promise.resolve();
|
||||
if (events[index].originalPosition === null) throw new Error("Subscription event came up with no OriginalPosition.");
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
self._tryProcess(events[index]);
|
||||
resolve();
|
||||
})
|
||||
return self._tryProcess(events[index])
|
||||
.then(function() {
|
||||
return processEvents(events, index + 1);
|
||||
});
|
||||
@ -40,13 +36,12 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
|
||||
function readNext() {
|
||||
return connection.readAllEventsForward(self._nextReadPosition, self.readBatchSize, resolveLinkTos, userCredentials)
|
||||
.then(function(slice) {
|
||||
return processEvents(slice.events)
|
||||
return processEvents(slice.events, 0)
|
||||
.then(function() {
|
||||
self._nextReadPosition = slice.nextPosition;
|
||||
var done = lastCommitPosition === null
|
||||
return (lastCommitPosition === null)
|
||||
? slice.isEndOfStream
|
||||
: slice.nextPosition.compareTo(new results.Position(lastCommitPosition, lastCommitPosition)) >= 0;
|
||||
return Promise.resolve(done);
|
||||
});
|
||||
})
|
||||
.then(function(done) {
|
||||
@ -67,9 +62,10 @@ EventStoreAllCatchUpSubscription.prototype._readEventsTill = function(
|
||||
|
||||
EventStoreAllCatchUpSubscription.prototype._tryProcess = function(e) {
|
||||
var processed = false;
|
||||
var promise;
|
||||
if (e.originalPosition.compareTo(this._lastProcessedPosition) > 0)
|
||||
{
|
||||
this._eventAppeared(this, e);
|
||||
promise = this._eventAppeared(this, e);
|
||||
this._lastProcessedPosition = e.originalPosition;
|
||||
processed = true;
|
||||
}
|
||||
@ -77,6 +73,7 @@ EventStoreAllCatchUpSubscription.prototype._tryProcess = function(e) {
|
||||
this._log.debug("Catch-up Subscription to %s: %s event (%s, %d, %s @ %s).",
|
||||
this.streamId || '<all>', processed ? "processed" : "skipping",
|
||||
e.originalEvent.eventStreamId, e.originalEvent.eventNumber, e.originalEvent.eventType, e.originalPosition);
|
||||
return (promise && promise.then) ? promise : Promise.resolve();
|
||||
};
|
||||
|
||||
module.exports = EventStoreAllCatchUpSubscription;
|
||||
|
@ -209,26 +209,35 @@ EventStoreCatchUpSubscription.prototype._ensureProcessingPushQueue = function()
|
||||
|
||||
EventStoreCatchUpSubscription.prototype._processLiveQueue = function() {
|
||||
var ev = this._liveQueue.shift();
|
||||
//TODO: possible blocking while, use when
|
||||
while(ev) {
|
||||
if (ev instanceof DropSubscriptionEvent) {
|
||||
if (!this._dropData) this._dropData = {reason: SubscriptionDropReason.Unknown, error: new Error("Drop reason not specified.")};
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this._tryProcess(ev);
|
||||
}
|
||||
catch(err) {
|
||||
this._dropSubscription(SubscriptionDropReason.EventHandlerException, err);
|
||||
return;
|
||||
}
|
||||
ev = this._liveQueue.shift();
|
||||
if (!ev) {
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
if (ev instanceof DropSubscriptionEvent) {
|
||||
if (!this._dropData) this._dropData = {reason: SubscriptionDropReason.Unknown, error: new Error("Drop reason not specified.")};
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
var promise;
|
||||
try {
|
||||
promise = this._tryProcess(ev);
|
||||
}
|
||||
catch(err) {
|
||||
this._dropSubscription(SubscriptionDropReason.EventHandlerException, err);
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
if (promise && promise.then) {
|
||||
var self = this;
|
||||
promise
|
||||
.then(this._processLiveQueue.bind(this), function(err) {
|
||||
self._dropSubscription(SubscriptionDropReason.EventHandlerException, err);
|
||||
self._isProcessing = false;
|
||||
});
|
||||
} else {
|
||||
setImmediate(this._processLiveQueue.bind(this));
|
||||
}
|
||||
|
||||
this._isProcessing = false;
|
||||
};
|
||||
|
||||
EventStoreCatchUpSubscription.prototype._dropSubscription = function(reason, error) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var Long = require('long');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var ensure = require('./common/utils/ensure');
|
||||
|
||||
@ -37,6 +38,7 @@ const MaxReadSize = 4096;
|
||||
* @constructor
|
||||
*/
|
||||
function EventStoreNodeConnection(settings, clusterSettings, endpointDiscoverer, connectionName) {
|
||||
EventEmitter.call(this);
|
||||
this._connectionName = connectionName || ['ES-', uuid.v4()].join('');
|
||||
this._settings = settings;
|
||||
this._clusterSettings = clusterSettings;
|
||||
@ -59,6 +61,9 @@ function EventStoreNodeConnection(settings, clusterSettings, endpointDiscoverer,
|
||||
this._handler.on('error', function(e) {
|
||||
self.emit('error', e);
|
||||
});
|
||||
this._handler.on('heartbeatInfo', function(e) {
|
||||
self.emit('heartbeatInfo', e);
|
||||
});
|
||||
}
|
||||
util.inherits(EventStoreNodeConnection, EventEmitter);
|
||||
|
||||
@ -97,15 +102,16 @@ EventStoreNodeConnection.prototype.close = function() {
|
||||
* Delete a stream (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} expectedVersion
|
||||
* @param {Long|number} expectedVersion
|
||||
* @param {boolean} [hardDelete]
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @returns {Promise.<DeleteResult>}
|
||||
*/
|
||||
EventStoreNodeConnection.prototype.deleteStream = function(stream, expectedVersion, hardDelete, userCredentials) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(expectedVersion, "expectedVersion");
|
||||
hardDelete = !!hardDelete;
|
||||
ensure.isLongOrInteger(expectedVersion, "expectedVersion");
|
||||
expectedVersion = Long.fromValue(expectedVersion);
|
||||
hardDelete = Boolean(hardDelete);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -125,14 +131,15 @@ EventStoreNodeConnection.prototype.deleteStream = function(stream, expectedVersi
|
||||
* Append events to a stream (async)
|
||||
* @public
|
||||
* @param {string} stream The name of the stream to which to append.
|
||||
* @param {number} expectedVersion The version at which we currently expect the stream to be in order that an optimistic concurrency check can be performed.
|
||||
* @param {Long|number} expectedVersion The version at which we currently expect the stream to be in order that an optimistic concurrency check can be performed.
|
||||
* @param {EventData[]|EventData} events The event(s) to append.
|
||||
* @param {UserCredentials} [userCredentials] User credentials
|
||||
* @returns {Promise.<WriteResult>}
|
||||
*/
|
||||
EventStoreNodeConnection.prototype.appendToStream = function(stream, expectedVersion, events, userCredentials) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(expectedVersion, "expectedVersion");
|
||||
ensure.isLongOrInteger(expectedVersion, "expectedVersion");
|
||||
expectedVersion = Long.fromValue(expectedVersion);
|
||||
if (!Array.isArray(events))
|
||||
events = [events];
|
||||
ensure.isArrayOf(EventData, events, "events");
|
||||
@ -154,13 +161,14 @@ EventStoreNodeConnection.prototype.appendToStream = function(stream, expectedVer
|
||||
* Start a transaction (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} expectedVersion
|
||||
* @param {Long|number} expectedVersion
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @returns {Promise.<EventStoreTransaction>}
|
||||
*/
|
||||
EventStoreNodeConnection.prototype.startTransaction = function(stream, expectedVersion, userCredentials) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(expectedVersion, "expectedVersion");
|
||||
ensure.isLongOrInteger(expectedVersion, "expectedVersion");
|
||||
expectedVersion = Long.fromValue(expectedVersion);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -231,21 +239,20 @@ EventStoreNodeConnection.prototype.commitTransaction = function(transaction, use
|
||||
* Read a single event (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} eventNumber
|
||||
* @param {Long|number} eventNumber
|
||||
* @param {boolean} [resolveLinkTos]
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @returns {Promise.<EventReadResult>}
|
||||
*/
|
||||
EventStoreNodeConnection.prototype.readEvent = function(stream, eventNumber, resolveLinkTos, userCredentials) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(eventNumber, "eventNumber");
|
||||
if (eventNumber < -1) throw new Error("eventNumber out of range.");
|
||||
resolveLinkTos = !!resolveLinkTos;
|
||||
ensure.isLongOrInteger(eventNumber, "eventNumber");
|
||||
eventNumber = Long.fromValue(eventNumber);
|
||||
resolveLinkTos = Boolean(resolveLinkTos);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
if (typeof stream !== 'string' || stream === '') throw new TypeError("stream must be an non-empty string.");
|
||||
if (typeof eventNumber !== 'number' || eventNumber % 1 !== 0) throw new TypeError("eventNumber must be an integer.");
|
||||
if (eventNumber < -1) throw new Error("eventNumber out of range.");
|
||||
if (eventNumber.compare(-1) < 0) throw new Error("eventNumber out of range.");
|
||||
if (resolveLinkTos && typeof resolveLinkTos !== 'boolean') throw new TypeError("resolveLinkTos must be a boolean.");
|
||||
|
||||
var self = this;
|
||||
@ -264,7 +271,7 @@ EventStoreNodeConnection.prototype.readEvent = function(stream, eventNumber, res
|
||||
* Reading a specific stream forwards (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} start
|
||||
* @param {Long|number} start
|
||||
* @param {number} count
|
||||
* @param {boolean} [resolveLinkTos]
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
@ -274,12 +281,13 @@ EventStoreNodeConnection.prototype.readStreamEventsForward = function(
|
||||
stream, start, count, resolveLinkTos, userCredentials
|
||||
) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(start, "start");
|
||||
ensure.isLongOrInteger(start, "start");
|
||||
start = Long.fromValue(start);
|
||||
ensure.nonNegative(start, "start");
|
||||
ensure.isInteger(count, "count");
|
||||
ensure.positive(count, "count");
|
||||
if (count > MaxReadSize) throw new Error(util.format("Count should be less than %d. For larger reads you should page.", MaxReadSize));
|
||||
resolveLinkTos = !!resolveLinkTos;
|
||||
resolveLinkTos = Boolean(resolveLinkTos);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -298,7 +306,7 @@ EventStoreNodeConnection.prototype.readStreamEventsForward = function(
|
||||
* Reading a specific stream backwards (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} start
|
||||
* @param {Long|number} start
|
||||
* @param {number} count
|
||||
* @param {boolean} [resolveLinkTos]
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
@ -308,11 +316,12 @@ EventStoreNodeConnection.prototype.readStreamEventsBackward = function(
|
||||
stream, start, count, resolveLinkTos, userCredentials
|
||||
) {
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
ensure.isInteger(start, "start");
|
||||
ensure.isLongOrInteger(start, "start");
|
||||
start = Long.fromValue(start);
|
||||
ensure.isInteger(count, "count");
|
||||
ensure.positive(count, "count");
|
||||
if (count > MaxReadSize) throw new Error(util.format("Count should be less than %d. For larger reads you should page.", MaxReadSize));
|
||||
resolveLinkTos = !!resolveLinkTos;
|
||||
resolveLinkTos = Boolean(resolveLinkTos);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -343,7 +352,7 @@ EventStoreNodeConnection.prototype.readAllEventsForward = function(
|
||||
ensure.isInteger(maxCount, "maxCount");
|
||||
ensure.positive(maxCount, "maxCount");
|
||||
if (maxCount > MaxReadSize) throw new Error(util.format("Count should be less than %d. For larger reads you should page.", MaxReadSize));
|
||||
resolveLinkTos = !!resolveLinkTos;
|
||||
resolveLinkTos = Boolean(resolveLinkTos);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -374,7 +383,7 @@ EventStoreNodeConnection.prototype.readAllEventsBackward = function(
|
||||
ensure.isInteger(maxCount, "maxCount");
|
||||
ensure.positive(maxCount, "maxCount");
|
||||
if (maxCount > MaxReadSize) throw new Error(util.format("Count should be less than %d. For larger reads you should page.", MaxReadSize));
|
||||
resolveLinkTos = !!resolveLinkTos;
|
||||
resolveLinkTos = Boolean(resolveLinkTos);
|
||||
userCredentials = userCredentials || null;
|
||||
|
||||
var self = this;
|
||||
@ -424,7 +433,7 @@ EventStoreNodeConnection.prototype.subscribeToStream = function(
|
||||
* Subscribe to a stream from position
|
||||
* @public
|
||||
* @param {!string} stream
|
||||
* @param {?number} lastCheckpoint
|
||||
* @param {?number|Position} lastCheckpoint
|
||||
* @param {!boolean} resolveLinkTos
|
||||
* @param {!function} eventAppeared
|
||||
* @param {function} [liveProcessingStarted]
|
||||
@ -438,6 +447,10 @@ EventStoreNodeConnection.prototype.subscribeToStreamFrom = function(
|
||||
userCredentials, readBatchSize
|
||||
) {
|
||||
if (typeof stream !== 'string' || stream === '') throw new TypeError("stream must be a non-empty string.");
|
||||
if (lastCheckpoint !== null) {
|
||||
ensure.isLongOrInteger(lastCheckpoint);
|
||||
lastCheckpoint = Long.fromValue(lastCheckpoint);
|
||||
}
|
||||
if (typeof eventAppeared !== 'function') throw new TypeError("eventAppeared must be a function.");
|
||||
|
||||
var catchUpSubscription =
|
||||
@ -515,7 +528,7 @@ EventStoreNodeConnection.prototype.subscribeToAllFrom = function(
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @param {number} [bufferSize]
|
||||
* @param {boolean} [autoAck]
|
||||
* @return {EventStorePersistentSubscription}
|
||||
* @return {Promise<EventStorePersistentSubscription>}
|
||||
*/
|
||||
EventStoreNodeConnection.prototype.connectToPersistentSubscription = function(
|
||||
stream, groupName, eventAppeared, subscriptionDropped, userCredentials, bufferSize, autoAck
|
||||
@ -527,14 +540,12 @@ EventStoreNodeConnection.prototype.connectToPersistentSubscription = function(
|
||||
subscriptionDropped = subscriptionDropped || null;
|
||||
userCredentials = userCredentials || null;
|
||||
bufferSize = bufferSize === undefined ? 10 : bufferSize;
|
||||
autoAck = autoAck === undefined ? true : !!autoAck;
|
||||
autoAck = autoAck === undefined ? true : Boolean(autoAck);
|
||||
|
||||
var subscription = new EventStorePersistentSubscription(
|
||||
groupName, stream, eventAppeared, subscriptionDropped, userCredentials, this._settings.log,
|
||||
this._settings.verboseLogging, this._settings, this._handler, bufferSize, autoAck);
|
||||
subscription.start();
|
||||
|
||||
return subscription;
|
||||
return subscription.start();
|
||||
};
|
||||
|
||||
/**
|
||||
@ -567,7 +578,7 @@ EventStoreNodeConnection.prototype.createPersistentSubscription = function(strea
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {string} groupName
|
||||
* @param {string} settings
|
||||
* @param {PersistentSubscriptionSettings} settings
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @returns {Promise.<PersistentSubscriptionUpdateResult>}
|
||||
*/
|
||||
@ -621,7 +632,7 @@ EventStoreNodeConnection.prototype.setStreamMetadata = function() {
|
||||
* Set stream metadata with raw object (async)
|
||||
* @public
|
||||
* @param {string} stream
|
||||
* @param {number} expectedMetastreamVersion
|
||||
* @param {Long|number} expectedMetastreamVersion
|
||||
* @param {object} metadata
|
||||
* @param {UserCredentials} [userCredentials]
|
||||
* @returns {Promise.<WriteResult>}
|
||||
@ -632,6 +643,8 @@ EventStoreNodeConnection.prototype.setStreamMetadataRaw = function(
|
||||
ensure.notNullOrEmpty(stream, "stream");
|
||||
if (systemStreams.isMetastream(stream))
|
||||
throw new Error(util.format("Setting metadata for metastream '%s' is not supported.", stream));
|
||||
ensure.isLongOrInteger(expectedMetastreamVersion, "expectedMetastreamVersion");
|
||||
expectedMetastreamVersion = Long.fromValue(expectedMetastreamVersion);
|
||||
var self = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
function cb(err, result) {
|
||||
@ -674,12 +687,12 @@ EventStoreNodeConnection.prototype.getStreamMetadataRaw = function(stream, userC
|
||||
var evnt = res.event.originalEvent;
|
||||
var version = evnt ? evnt.eventNumber : -1;
|
||||
var data = evnt ? JSON.parse(evnt.data.toString()) : null;
|
||||
return new results.RawStreamMetadataResult(stream, false, version, data);
|
||||
return new results.RawStreamMetadataResult(stream, false, Long.fromValue(version), data);
|
||||
case results.EventReadStatus.NotFound:
|
||||
case results.EventReadStatus.NoStream:
|
||||
return new results.RawStreamMetadataResult(stream, false, -1, null);
|
||||
return new results.RawStreamMetadataResult(stream, false, Long.fromValue(-1), null);
|
||||
case results.EventReadStatus.StreamDeleted:
|
||||
return new results.RawStreamMetadataResult(stream, true, 0x7fffffff, null);
|
||||
return new results.RawStreamMetadataResult(stream, true, Long.fromValue(0x7fffffff), null);
|
||||
default:
|
||||
throw new Error(util.format("Unexpected ReadEventResult: %s.", res.status));
|
||||
}
|
||||
|
@ -35,11 +35,12 @@ EventStorePersistentSubscriptionBase.prototype.start = function() {
|
||||
this._stopped = false;
|
||||
|
||||
var self = this;
|
||||
this._startSubscription(this._subscriptionId, this._streamId, this._bufferSize, this._userCredentials,
|
||||
return this._startSubscription(this._subscriptionId, this._streamId, this._bufferSize, this._userCredentials,
|
||||
this._onEventAppeared.bind(this), this._onSubscriptionDropped.bind(this), this._settings)
|
||||
.then(function(subscription) {
|
||||
console.log('Subscription started.');
|
||||
self._subscription = subscription;
|
||||
return self;
|
||||
});
|
||||
};
|
||||
|
||||
@ -78,6 +79,7 @@ EventStorePersistentSubscriptionBase.prototype.fail = function(events, action, r
|
||||
this._subscription.notifyEventsFailed(ids, action, reason);
|
||||
};
|
||||
|
||||
//TODO: this should return a promise
|
||||
EventStorePersistentSubscriptionBase.prototype.stop = function() {
|
||||
if (this._verbose) this._log.debug("Persistent Subscription to %s: requesting stop...", this._streamId);
|
||||
this._enqueueSubscriptionDropNotification(SubscriptionDropReason.UserInitiated, null);
|
||||
@ -110,43 +112,56 @@ EventStorePersistentSubscriptionBase.prototype._enqueue = function(resolvedEvent
|
||||
}
|
||||
};
|
||||
|
||||
function runAsync(fn) {
|
||||
try {
|
||||
return Promise.resolve(fn());
|
||||
} catch(e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
EventStorePersistentSubscriptionBase.prototype._processQueue = function() {
|
||||
//do
|
||||
//{
|
||||
var e = this._queue.shift();
|
||||
while (e)
|
||||
{
|
||||
if (e instanceof DropSubscriptionEvent) // drop subscription artificial ResolvedEvent
|
||||
{
|
||||
if (this._dropData === null) throw new Error("Drop reason not specified.");
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
return;
|
||||
}
|
||||
if (this._dropData !== null)
|
||||
{
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
this._eventAppeared(this, e);
|
||||
if(this._autoAck)
|
||||
this._subscription.notifyEventsProcessed([e.originalEvent.eventId]);
|
||||
if (this._verbose)
|
||||
this._log.debug("Persistent Subscription to %s: processed event (%s, %d, %s @ %d).",
|
||||
this._streamId, e.originalEvent.eventStreamId, e.originalEvent.eventNumber, e.originalEvent.eventType,
|
||||
e.originalEventNumber);
|
||||
}
|
||||
catch (err)
|
||||
{
|
||||
//TODO GFY should we autonak here?
|
||||
this._dropSubscription(SubscriptionDropReason.EventHandlerException, err);
|
||||
return;
|
||||
}
|
||||
e = this._queue.shift();
|
||||
}
|
||||
var ev = this._queue.shift();
|
||||
if (!ev) {
|
||||
this._isProcessing = false;
|
||||
//} while (_queue.Count > 0 && Interlocked.CompareExchange(ref _isProcessing, 1, 0) === 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ev instanceof DropSubscriptionEvent) // drop subscription artificial ResolvedEvent
|
||||
{
|
||||
if (this._dropData === null) throw new Error("Drop reason not specified.");
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
if (this._dropData !== null)
|
||||
{
|
||||
this._dropSubscription(this._dropData.reason, this._dropData.error);
|
||||
this._isProcessing = false;
|
||||
return;
|
||||
}
|
||||
var self = this;
|
||||
runAsync(function() {
|
||||
return self._eventAppeared(self, ev);
|
||||
})
|
||||
.then(function() {
|
||||
if(self._autoAck)
|
||||
self._subscription.notifyEventsProcessed([ev.originalEvent.eventId]);
|
||||
if (self._verbose)
|
||||
self._log.debug("Persistent Subscription to %s: processed event (%s, %d, %s @ %d).",
|
||||
self._streamId, ev.originalEvent.eventStreamId, ev.originalEvent.eventNumber, ev.originalEvent.eventType,
|
||||
ev.originalEventNumber);
|
||||
return false;
|
||||
}, function(err) {
|
||||
//TODO GFY should we autonak here?
|
||||
self._dropSubscription(SubscriptionDropReason.EventHandlerException, err);
|
||||
self._isProcessing = false;
|
||||
return true;
|
||||
})
|
||||
.then(function (faulted) {
|
||||
if (faulted) return;
|
||||
self._processQueue();
|
||||
});
|
||||
};
|
||||
|
||||
EventStorePersistentSubscriptionBase.prototype._dropSubscription = function(reason, error) {
|
||||
@ -159,8 +174,13 @@ EventStorePersistentSubscriptionBase.prototype._dropSubscription = function(reas
|
||||
|
||||
if (this._subscription !== null)
|
||||
this._subscription.unsubscribe();
|
||||
if (this._subscriptionDropped !== null)
|
||||
this._subscriptionDropped(this, reason, error);
|
||||
if (this._subscriptionDropped !== null) {
|
||||
try {
|
||||
this._subscriptionDropped(this, reason, error);
|
||||
} catch (e) {
|
||||
this._log.error(e, "Persistent Subscription to %s: subscriptionDropped callback failed.", this._streamId);
|
||||
}
|
||||
}
|
||||
this._stopped = true;
|
||||
}
|
||||
};
|
||||
|
@ -1,4 +1,5 @@
|
||||
var util = require('util');
|
||||
var Long = require('long');
|
||||
|
||||
var EventStoreCatchUpSubscription = require('./eventStoreCatchUpSubscription');
|
||||
var SliceReadStatus = require('./sliceReadStatus');
|
||||
@ -14,24 +15,26 @@ function EventStoreStreamCatchUpSubscription(
|
||||
|
||||
//Ensure.NotNullOrEmpty(streamId, "streamId");
|
||||
|
||||
this._lastProcessedEventNumber = fromEventNumberExclusive || -1;
|
||||
this._nextReadEventNumber = fromEventNumberExclusive || 0;
|
||||
this._lastProcessedEventNumber = fromEventNumberExclusive === null ? Long.fromNumber(-1) : fromEventNumberExclusive;
|
||||
this._nextReadEventNumber = fromEventNumberExclusive === null ? Long.fromNumber(0) : fromEventNumberExclusive.add(1);
|
||||
}
|
||||
util.inherits(EventStoreStreamCatchUpSubscription, EventStoreCatchUpSubscription);
|
||||
|
||||
function delay(ms, result) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms, result);
|
||||
})
|
||||
}
|
||||
|
||||
EventStoreStreamCatchUpSubscription.prototype._readEventsTill = function(
|
||||
connection, resolveLinkTos, userCredentials, lastCommitPosition, lastEventNumber
|
||||
) {
|
||||
var self = this;
|
||||
|
||||
function processEvents(events, index) {
|
||||
index = index || 0;
|
||||
if (index >= events.length) return Promise.resolve();
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
self._tryProcess(events[index]);
|
||||
resolve();
|
||||
})
|
||||
return self._tryProcess(events[index])
|
||||
.then(function() {
|
||||
return processEvents(events, index + 1);
|
||||
});
|
||||
@ -42,17 +45,17 @@ EventStoreStreamCatchUpSubscription.prototype._readEventsTill = function(
|
||||
.then(function(slice) {
|
||||
switch(slice.status) {
|
||||
case SliceReadStatus.Success:
|
||||
return processEvents(slice.events)
|
||||
return processEvents(slice.events, 0)
|
||||
.then(function() {
|
||||
self._nextReadEventNumber = slice.nextEventNumber;
|
||||
var done = Promise.resolve(lastEventNumber === null ? slice.isEndOfStream : slice.nextEventNumber > lastEventNumber);
|
||||
var done = Promise.resolve(lastEventNumber === null ? slice.isEndOfStream : slice.nextEventNumber.compare(lastEventNumber) > 0);
|
||||
if (!done && slice.isEndOfStream)
|
||||
return done.delay(10);
|
||||
return delay(100, false);
|
||||
return done;
|
||||
});
|
||||
break;
|
||||
case SliceReadStatus.StreamNotFound:
|
||||
if (lastEventNumber && lastEventNumber !== -1)
|
||||
if (lastEventNumber && lastEventNumber.compare(-1) !== 0)
|
||||
throw new Error(util.format("Impossible: stream %s disappeared in the middle of catching up subscription.", self.streamId));
|
||||
return true;
|
||||
case SliceReadStatus.StreamDeleted:
|
||||
@ -77,15 +80,17 @@ EventStoreStreamCatchUpSubscription.prototype._readEventsTill = function(
|
||||
|
||||
EventStoreStreamCatchUpSubscription.prototype._tryProcess = function(e) {
|
||||
var processed = false;
|
||||
if (e.originalEventNumber > this._lastProcessedEventNumber) {
|
||||
this._eventAppeared(this, e);
|
||||
var promise;
|
||||
if (e.originalEventNumber.compare(this._lastProcessedEventNumber) > 0) {
|
||||
promise = this._eventAppeared(this, e);
|
||||
this._lastProcessedEventNumber = e.originalEventNumber;
|
||||
processed = true;
|
||||
}
|
||||
if (this._verbose)
|
||||
this._log.debug("Catch-up Subscription to %s: %s event (%s, %d, %s @ %d).",
|
||||
this.isSubscribedToAll ? '<all>' : this.streamId, processed ? "processed" : "skipping",
|
||||
e.originalEvent.eventStreamId, e.originalEvent.eventNumber, e.originalEvent.eventType, e.originalEventNumber)
|
||||
e.originalEvent.eventStreamId, e.originalEvent.eventNumber, e.originalEvent.eventType, e.originalEventNumber);
|
||||
return (promise && promise.then) ? promise : Promise.resolve();
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,13 +1,6 @@
|
||||
module.exports = function GossipSeed(endPoint, hostName) {
|
||||
if (typeof endPoint !== 'object' || !endPoint.host || !endPoint.port) throw new TypeError('endPoint must be have host and port properties.');
|
||||
Object.defineProperties(this, {
|
||||
endPoint: {
|
||||
enumerable: true,
|
||||
value: endPoint
|
||||
},
|
||||
hostName: {
|
||||
enumerable: true,
|
||||
value: hostName
|
||||
}
|
||||
});
|
||||
this.endPoint = endPoint;
|
||||
this.hostName = hostName;
|
||||
Object.freeze(this);
|
||||
};
|
||||
|
12811
src/messages/messages.js
12811
src/messages/messages.js
File diff suppressed because it is too large
Load Diff
@ -23,7 +23,7 @@ message NewEvent {
|
||||
|
||||
message EventRecord {
|
||||
required string event_stream_id = 1;
|
||||
required int32 event_number = 2;
|
||||
required int64 event_number = 2;
|
||||
required bytes event_id = 3;
|
||||
required string event_type = 4;
|
||||
required int32 data_content_type = 5;
|
||||
@ -48,7 +48,7 @@ message ResolvedEvent {
|
||||
|
||||
message WriteEvents {
|
||||
required string event_stream_id = 1;
|
||||
required int32 expected_version = 2;
|
||||
required int64 expected_version = 2;
|
||||
repeated NewEvent events = 3;
|
||||
required bool require_master = 4;
|
||||
}
|
||||
@ -56,15 +56,16 @@ message WriteEvents {
|
||||
message WriteEventsCompleted {
|
||||
required OperationResult result = 1;
|
||||
optional string message = 2;
|
||||
required int32 first_event_number = 3;
|
||||
required int32 last_event_number = 4;
|
||||
required int64 first_event_number = 3;
|
||||
required int64 last_event_number = 4;
|
||||
optional int64 prepare_position = 5;
|
||||
optional int64 commit_position = 6;
|
||||
optional int64 current_version = 7;
|
||||
}
|
||||
|
||||
message DeleteStream {
|
||||
required string event_stream_id = 1;
|
||||
required int32 expected_version = 2;
|
||||
required int64 expected_version = 2;
|
||||
required bool require_master = 3;
|
||||
optional bool hard_delete = 4;
|
||||
}
|
||||
@ -78,7 +79,7 @@ message DeleteStreamCompleted {
|
||||
|
||||
message TransactionStart {
|
||||
required string event_stream_id = 1;
|
||||
required int32 expected_version = 2;
|
||||
required int64 expected_version = 2;
|
||||
required bool require_master = 3;
|
||||
}
|
||||
|
||||
@ -109,15 +110,15 @@ message TransactionCommitCompleted {
|
||||
required int64 transaction_id = 1;
|
||||
required OperationResult result = 2;
|
||||
optional string message = 3;
|
||||
required int32 first_event_number = 4;
|
||||
required int32 last_event_number = 5;
|
||||
required int64 first_event_number = 4;
|
||||
required int64 last_event_number = 5;
|
||||
optional int64 prepare_position = 6;
|
||||
optional int64 commit_position = 7;
|
||||
}
|
||||
|
||||
message ReadEvent {
|
||||
required string event_stream_id = 1;
|
||||
required int32 event_number = 2;
|
||||
required int64 event_number = 2;
|
||||
required bool resolve_link_tos = 3;
|
||||
required bool require_master = 4;
|
||||
}
|
||||
@ -141,7 +142,7 @@ message ReadEventCompleted {
|
||||
|
||||
message ReadStreamEvents {
|
||||
required string event_stream_id = 1;
|
||||
required int32 from_event_number = 2;
|
||||
required int64 from_event_number = 2;
|
||||
required int32 max_count = 3;
|
||||
required bool resolve_link_tos = 4;
|
||||
required bool require_master = 5;
|
||||
@ -160,8 +161,8 @@ message ReadStreamEventsCompleted {
|
||||
|
||||
repeated ResolvedIndexedEvent events = 1;
|
||||
required ReadStreamResult result = 2;
|
||||
required int32 next_event_number = 3;
|
||||
required int32 last_event_number = 4;
|
||||
required int64 next_event_number = 3;
|
||||
required int64 last_event_number = 4;
|
||||
required bool is_end_of_stream = 5;
|
||||
required int64 last_commit_position = 6;
|
||||
|
||||
@ -199,7 +200,7 @@ message CreatePersistentSubscription {
|
||||
required string subscription_group_name = 1;
|
||||
required string event_stream_id = 2;
|
||||
required bool resolve_link_tos = 3;
|
||||
required int32 start_from = 4;
|
||||
required int64 start_from = 4;
|
||||
required int32 message_timeout_milliseconds = 5;
|
||||
required bool record_statistics = 6;
|
||||
required int32 live_buffer_size = 7;
|
||||
@ -223,7 +224,7 @@ message UpdatePersistentSubscription {
|
||||
required string subscription_group_name = 1;
|
||||
required string event_stream_id = 2;
|
||||
required bool resolve_link_tos = 3;
|
||||
required int32 start_from = 4;
|
||||
required int64 start_from = 4;
|
||||
required int32 message_timeout_milliseconds = 5;
|
||||
required bool record_statistics = 6;
|
||||
required int32 live_buffer_size = 7;
|
||||
@ -301,7 +302,7 @@ message PersistentSubscriptionNakEvents {
|
||||
message PersistentSubscriptionConfirmation {
|
||||
required int64 last_commit_position = 1;
|
||||
required string subscription_id = 2;
|
||||
optional int32 last_event_number = 3;
|
||||
optional int64 last_event_number = 3;
|
||||
}
|
||||
|
||||
message PersistentSubscriptionStreamEventAppeared {
|
||||
@ -315,7 +316,7 @@ message SubscribeToStream {
|
||||
|
||||
message SubscriptionConfirmation {
|
||||
required int64 last_commit_position = 1;
|
||||
optional int32 last_event_number = 2;
|
||||
optional int64 last_event_number = 2;
|
||||
}
|
||||
|
||||
message StreamEventAppeared {
|
||||
@ -375,3 +376,11 @@ message ScavengeDatabaseCompleted {
|
||||
required int32 total_time_ms = 3;
|
||||
required int64 total_space_saved = 4;
|
||||
}
|
||||
|
||||
message IdentifyClient {
|
||||
required int32 version = 1;
|
||||
optional string connection_name = 2;
|
||||
}
|
||||
|
||||
message ClientIdentified {
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
var SystemConsumerStrategies = require('./systemConsumerStrategies');
|
||||
var ensure = require('./common/utils/ensure');
|
||||
var Long = require('long');
|
||||
|
||||
function PersistentSubscriptionSettings(
|
||||
resolveLinkTos, startFrom, extraStatistics, messageTimeout,
|
||||
@ -6,6 +8,9 @@ function PersistentSubscriptionSettings(
|
||||
checkPointAfter, minCheckPointCount, maxCheckPointCount,
|
||||
maxSubscriberCount, namedConsumerStrategy
|
||||
) {
|
||||
ensure.isLongOrInteger(startFrom);
|
||||
startFrom = Long.fromValue(startFrom);
|
||||
|
||||
this.resolveLinkTos = resolveLinkTos;
|
||||
this.startFrom = startFrom;
|
||||
this.extraStatistics = extraStatistics;
|
||||
|
56
src/projections/projectionDetails.js
Normal file
56
src/projections/projectionDetails.js
Normal file
@ -0,0 +1,56 @@
|
||||
function ProjectionDetails(
|
||||
coreProcessingTime,
|
||||
version,
|
||||
epoch,
|
||||
effectiveName,
|
||||
writesInProgress,
|
||||
readsInProgress,
|
||||
partitionsCached,
|
||||
status,
|
||||
stateReason,
|
||||
name,
|
||||
mode,
|
||||
position,
|
||||
progress,
|
||||
lastCheckpoint,
|
||||
eventsProcessedAfterRestart,
|
||||
statusUrl,
|
||||
stateUrl,
|
||||
resultUrl,
|
||||
queryUrl,
|
||||
enableCommandUrl,
|
||||
disableCommandUrl,
|
||||
checkpointStatus,
|
||||
bufferedEvents,
|
||||
writePendingEventsBeforeCheckpoint,
|
||||
writePendingEventsAfterCheckpoint
|
||||
) {
|
||||
this.coreProcessingTime = coreProcessingTime;
|
||||
this.version = version;
|
||||
this.epoch = epoch;
|
||||
this.effectiveName = effectiveName;
|
||||
this.writesInProgress = writesInProgress;
|
||||
this.readsInProgress = readsInProgress;
|
||||
this.partitionsCached = partitionsCached;
|
||||
this.status = status;
|
||||
this.stateReason = stateReason;
|
||||
this.name = name;
|
||||
this.mode = mode;
|
||||
this.position = position;
|
||||
this.progress = progress;
|
||||
this.lastCheckpoint = lastCheckpoint;
|
||||
this.eventsProcessedAfterRestart = eventsProcessedAfterRestart;
|
||||
this.statusUrl = statusUrl;
|
||||
this.stateUrl = stateUrl;
|
||||
this.resultUrl = resultUrl;
|
||||
this.queryUrl = queryUrl;
|
||||
this.enableCommandUrl = enableCommandUrl;
|
||||
this.disableCommandUrl = disableCommandUrl;
|
||||
this.checkpointStatus = checkpointStatus;
|
||||
this.bufferedEvents = bufferedEvents;
|
||||
this.writePendingEventsBeforeCheckpoint = writePendingEventsBeforeCheckpoint;
|
||||
this.writePendingEventsAfterCheckpoint = writePendingEventsAfterCheckpoint;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
module.exports = ProjectionDetails;
|
170
src/projections/projectionsClient.js
Normal file
170
src/projections/projectionsClient.js
Normal file
@ -0,0 +1,170 @@
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
const util = require('util');
|
||||
const ProjectionCommandFailedError = require('../errors/projectionCommandFailedError');
|
||||
|
||||
const HTTP_OK = 200;
|
||||
const HTTP_CREATED = 201;
|
||||
|
||||
function safeParseJson(json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch(e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function ProjectionsClient(log, operationTimeout) {
|
||||
this._log = log;
|
||||
this._operationTimeout = operationTimeout;
|
||||
}
|
||||
|
||||
ProjectionsClient.prototype.enable = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/enable', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.disable = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/disable', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.abort = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/abort', '', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createOneTime = function(httpEndPoint, query, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/onetime?type=JS', query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createTransient = function(httpEndPoint, name, query, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/transient?name=' + name + '&type=JS', query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.createContinuous = function(httpEndPoint, name, query, trackEmittedStreams, userCredentials) {
|
||||
return this.sendPost(httpEndPoint + '/projections/continuous?name=' + name + '&type=JS&emit=1&trackEmittedStreams=' + trackEmittedStreams, query, userCredentials, HTTP_CREATED);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listAll = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/any', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listOneTime = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/onetime', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.listContinuous = function(httpEndPoint, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projections/continuous', userCredentials, HTTP_OK)
|
||||
.then(function (json) {
|
||||
var r = safeParseJson(json);
|
||||
if (r && r.projections) return r.projections;
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getStatus = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getState = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/state', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getPartitionState = function(httpEndPoint, name, partitionId, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/state?partition=' + partitionId, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getResult = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/result', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getPartitionResult = function(httpEndPoint, name, partitionId, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/result?partition=' + partitionId, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getStatistics = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/statistics', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.getQuery = function(httpEndPoint, name, userCredentials) {
|
||||
return this.sendGet(httpEndPoint + '/projection/' + name + '/query', userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.updateQuery = function(httpEndPoint, name, query, userCredentials) {
|
||||
return this.sendPut(httpEndPoint + '/projection/' + name + '/query?type=JS', query, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.delete = function(httpEndPoint, name, deleteEmittedStreams, userCredentials) {
|
||||
return this.sendDelete(httpEndPoint + '/projection/' + name + '?deleteEmittedStreams=' + deleteEmittedStreams, userCredentials, HTTP_OK);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.request = function(method, _url, data, userCredentials, expectedCode) {
|
||||
const options = url.parse(_url);
|
||||
options.method = method;
|
||||
if (userCredentials) {
|
||||
options.auth = [userCredentials.username, userCredentials.password].join(':');
|
||||
}
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
const timeout = setTimeout(function () {
|
||||
reject(new Error(util.format('Request timed out for %s on %s', method, _url)))
|
||||
}, self._operationTimeout);
|
||||
const req = http.request(options, function (res) {
|
||||
const hasExpectedCode = res.statusCode === expectedCode;
|
||||
var result = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function (chunk) {
|
||||
result += chunk;
|
||||
});
|
||||
res.on('end', function () {
|
||||
if (hasExpectedCode) {
|
||||
clearTimeout(timeout);
|
||||
resolve(result);
|
||||
} else {
|
||||
clearTimeout(timeout);
|
||||
reject(new ProjectionCommandFailedError(
|
||||
res.statusCode,
|
||||
util.format('Server returned %d (%s) for %s on %s', res.statusCode, res.statusMessage, method, _url)
|
||||
));
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
if (data) {
|
||||
req.setHeader('Content-Type', 'application/json');
|
||||
req.write(data);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
function voidResult() {}
|
||||
|
||||
ProjectionsClient.prototype.sendGet = function(_url, userCredentials, expectedCode) {
|
||||
return this.request('GET', _url, null, userCredentials, expectedCode);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendPost = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('POST', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendPut = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('PUT', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
ProjectionsClient.prototype.sendDelete = function(_url, data, userCredentials, expectedCode) {
|
||||
return this.request('DELETE', _url, data, userCredentials, expectedCode)
|
||||
.then(voidResult);
|
||||
};
|
||||
|
||||
module.exports = ProjectionsClient;
|
202
src/projections/projectionsManager.js
Normal file
202
src/projections/projectionsManager.js
Normal file
@ -0,0 +1,202 @@
|
||||
const ensure = require('../common/utils/ensure');
|
||||
const ProjectionsClient = require('./projectionsClient');
|
||||
|
||||
/**
|
||||
* Creates a new instance of ProjectionsManager.
|
||||
* @param {Logger} log Instance of Logger to use for logging.
|
||||
* @param {string} httpEndPoint HTTP endpoint of an Event Store server.
|
||||
* @param {number} operationTimeout Operation timeout in milliseconds.
|
||||
* @constructor
|
||||
*/
|
||||
function ProjectionsManager(log, httpEndPoint, operationTimeout) {
|
||||
ensure.notNull(log, "log");
|
||||
ensure.notNull(httpEndPoint, "httpEndPoint");
|
||||
this._client = new ProjectionsClient(log, operationTimeout);
|
||||
this._httpEndPoint = httpEndPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to enable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.enable = function(name, userCredentials) {
|
||||
return this._client.enable(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Aborts and disables a projection without writing a checkpoint.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to disable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.disable = function(name, userCredentials) {
|
||||
return this._client.disable(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Disables a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for a user with permission to disable a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.abort = function(name, userCredentials) {
|
||||
return this._client.abort(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createOneTime = function(query, userCredentials) {
|
||||
return this._client.createOneTime(this._httpEndPoint, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param name A name for the query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createTransient = function(name, query, userCredentials) {
|
||||
return this._client.createTransient(this._httpEndPoint, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a one-time query.
|
||||
* @param name The name of the projection.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param trackEmittedStreams Whether the streams emitted by this projection should be tracked.
|
||||
* @param userCredentials Credentials for a user with permission to create a query.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.createContinuous = function(name, query, trackEmittedStreams, userCredentials) {
|
||||
return this._client.createContinuous(this._httpEndPoint, name, query, trackEmittedStreams, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listAll = function(userCredentials) {
|
||||
return this._client.listAll(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all one-time projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listOneTime = function(userCredentials) {
|
||||
return this._client.listOneTime(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists the status of all continuous projections.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<ProjectionDetails[]>}
|
||||
*/
|
||||
ProjectionsManager.prototype.listContinuous = function(userCredentials) {
|
||||
return this._client.listContinuous(this._httpEndPoint, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the status of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection status.
|
||||
*/
|
||||
ProjectionsManager.prototype.getStatus = function(name, userCredentials) {
|
||||
return this._client.getStatus(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getState = function(name, userCredentials) {
|
||||
return this._client.getState(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection for a specified partition.
|
||||
* @param name The name of the projection.
|
||||
* @param partitionId The id of the partition.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getPartitionState = function(name, partitionId, userCredentials) {
|
||||
return this._client.getPartitionState(this._httpEndPoint, name, partitionId, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getResult = function(name, userCredentials) {
|
||||
return this._client.getResult(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the state of a projection for a specified partition.
|
||||
* @param name The name of the projection.
|
||||
* @param partitionId The id of the partition.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection state.
|
||||
*/
|
||||
ProjectionsManager.prototype.getPartitionResult = function(name, partitionId, userCredentials) {
|
||||
return this._client.getPartitionResult(this._httpEndPoint, name, partitionId, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the statistics of a projection.
|
||||
* @param name The name of the projection.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing projection statistics.
|
||||
*/
|
||||
ProjectionsManager.prototype.getStatistics = function(name, userCredentials) {
|
||||
return this._client.getStatistics(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the status of a query.
|
||||
* @param name The name of the query.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<string>} String of JSON containing query status.
|
||||
*/
|
||||
ProjectionsManager.prototype.getQuery = function(name, userCredentials) {
|
||||
return this._client.getQuery(this._httpEndPoint, name, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the definition of a query.
|
||||
* @param name The name of the query.
|
||||
* @param query The JavaScript source code for the query.
|
||||
* @param userCredentials Credentials for the operation.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.updateQuery = function(name, query, userCredentials) {
|
||||
return this._client.updateQuery(this._httpEndPoint, name, query, userCredentials);
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the definition of a query.
|
||||
* @param name The name of the projection.
|
||||
* @param deleteEmittedStreams Whether to delete the streams that were emitted by this projection.
|
||||
* @param userCredentials Credentials for a user with permission to delete a projection.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
ProjectionsManager.prototype.delete = function(name, deleteEmittedStreams, userCredentials) {
|
||||
return this._client.delete(this._httpEndPoint, name, deleteEmittedStreams, userCredentials);
|
||||
};
|
||||
|
||||
module.exports = ProjectionsManager;
|
232
src/results.js
232
src/results.js
@ -1,5 +1,4 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var guidParse = require('./common/guid-parse');
|
||||
var Long = require('long');
|
||||
var ensure = require('./common/utils/ensure');
|
||||
|
||||
@ -14,17 +13,9 @@ var ensure = require('./common/utils/ensure');
|
||||
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: commitPosition
|
||||
},
|
||||
preparePosition: {
|
||||
enumerable: true, value: preparePosition
|
||||
}
|
||||
});
|
||||
this.commitPosition = Long.fromValue(commitPosition);
|
||||
this.preparePosition = Long.fromValue(preparePosition);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
Position.prototype.compareTo = function(other) {
|
||||
@ -39,6 +30,8 @@ Position.prototype.toString = function() {
|
||||
return [this.commitPosition.toString(), this.preparePosition.toString()].join("/");
|
||||
};
|
||||
|
||||
Position.start = new Position(0,0);
|
||||
Position.end = new Position(-1,-1);
|
||||
|
||||
const EventReadStatus = {
|
||||
Success: 'success',
|
||||
@ -46,13 +39,14 @@ const EventReadStatus = {
|
||||
NoStream: 'noStream',
|
||||
StreamDeleted: 'streamDeleted'
|
||||
};
|
||||
Object.freeze(EventReadStatus);
|
||||
|
||||
/**
|
||||
* @param {object} ev
|
||||
* @constructor
|
||||
* @property {string} eventStreamId
|
||||
* @property {string} eventId
|
||||
* @property {number} eventNumber
|
||||
* @property {Long} eventNumber
|
||||
* @property {string} eventType
|
||||
* @property {number} createdEpoch
|
||||
* @property {?Buffer} data
|
||||
@ -60,18 +54,16 @@ const EventReadStatus = {
|
||||
* @property {boolean} isJson
|
||||
*/
|
||||
function RecordedEvent(ev) {
|
||||
Object.defineProperties(this, {
|
||||
eventStreamId: {enumerable: true, value: ev.event_stream_id},
|
||||
eventId: {enumerable: true, value: uuid.unparse(ev.event_id.buffer, ev.event_id.offset)},
|
||||
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 ? ev.created_epoch.toNumber() : 0)},
|
||||
createdEpoch: {enumerable: true, value: ev.created_epoch ? ev.created_epoch.toNumber() : 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}
|
||||
});
|
||||
this.eventStreamId = ev.eventStreamId;
|
||||
this.eventId = guidParse.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;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,138 +75,83 @@ function RecordedEvent(ev) {
|
||||
* @property {boolean} isResolved
|
||||
* @property {?Position} originalPosition
|
||||
* @property {string} originalStreamId
|
||||
* @property {number} originalEventNumber
|
||||
* @property {Long} originalEventNumber
|
||||
*/
|
||||
function ResolvedEvent(ev) {
|
||||
Object.defineProperties(this, {
|
||||
event: {
|
||||
enumerable: true,
|
||||
value: ev.event === null ? null : new RecordedEvent(ev.event)
|
||||
},
|
||||
link: {
|
||||
enumerable: true,
|
||||
value: ev.link === null ? null : new RecordedEvent(ev.link)
|
||||
},
|
||||
originalEvent: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.link || this.event;
|
||||
}
|
||||
},
|
||||
isResolved: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.link !== null && this.event !== null;
|
||||
}
|
||||
},
|
||||
originalPosition: {
|
||||
enumerable: true,
|
||||
value: (ev.commit_position && ev.prepare_position) ? new Position(ev.commit_position, ev.prepare_position) : null
|
||||
},
|
||||
originalStreamId: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.originalEvent.eventStreamId;
|
||||
}
|
||||
},
|
||||
originalEventNumber: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.originalEvent.eventNumber;
|
||||
}
|
||||
}
|
||||
});
|
||||
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;
|
||||
this.originalPosition = (ev.commitPosition && ev.preparePosition) ? new Position(ev.commitPosition, ev.preparePosition) : null;
|
||||
this.originalStreamId = this.originalEvent && this.originalEvent.eventStreamId;
|
||||
this.originalEventNumber = this.originalEvent && this.originalEvent.eventNumber;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} status
|
||||
* @param {string} stream
|
||||
* @param {number} eventNumber
|
||||
* @param {Long} eventNumber
|
||||
* @param {object} event
|
||||
* @constructor
|
||||
* @property {string} status
|
||||
* @property {string} stream
|
||||
* @property {number} eventNumber
|
||||
* @property {Long} eventNumber
|
||||
* @property {ResolvedEvent} event
|
||||
*/
|
||||
function EventReadResult(status, stream, eventNumber, event) {
|
||||
Object.defineProperties(this, {
|
||||
status: {enumerable: true, value: status},
|
||||
stream: {enumerable: true, value: stream},
|
||||
eventNumber: {enumerable: true, value: eventNumber},
|
||||
event: {
|
||||
enumerable: true, value: status === EventReadStatus.Success ? new ResolvedEvent(event) : null
|
||||
}
|
||||
});
|
||||
this.status = status;
|
||||
this.stream = stream;
|
||||
this.eventNumber = eventNumber;
|
||||
this.event = status === EventReadStatus.Success ? new ResolvedEvent(event) : null;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} nextExpectedVersion
|
||||
* @param {Position} logPosition
|
||||
* @constructor
|
||||
* @property {number} nextExpectedVersion
|
||||
* @property {Long} nextExpectedVersion
|
||||
* @property {Position} logPosition
|
||||
*/
|
||||
function WriteResult(nextExpectedVersion, logPosition) {
|
||||
Object.defineProperties(this, {
|
||||
nextExpectedVersion: {
|
||||
enumerable: true, value: nextExpectedVersion
|
||||
},
|
||||
logPosition: {
|
||||
enumerable: true, value: logPosition
|
||||
}
|
||||
});
|
||||
this.nextExpectedVersion = nextExpectedVersion;
|
||||
this.logPosition = logPosition;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} status
|
||||
* @param {string} stream
|
||||
* @param {number} fromEventNumber
|
||||
* @param {Long} fromEventNumber
|
||||
* @param {string} readDirection
|
||||
* @param {object[]} events
|
||||
* @param {number} nextEventNumber
|
||||
* @param {number} lastEventNumber
|
||||
* @param {Long} nextEventNumber
|
||||
* @param {Long} lastEventNumber
|
||||
* @param {boolean} isEndOfStream
|
||||
* @constructor
|
||||
* @property {string} status
|
||||
* @property {string} stream
|
||||
* @property {number} fromEventNumber
|
||||
* @property {Long} fromEventNumber
|
||||
* @property {string} readDirection
|
||||
* @property {ResolvedEvent[]} events
|
||||
* @property {number} nextEventNumber
|
||||
* @property {number} lastEventNumber
|
||||
* @property {Long} nextEventNumber
|
||||
* @property {Long} lastEventNumber
|
||||
* @property {boolean} isEndOfStream
|
||||
*/
|
||||
function StreamEventsSlice(
|
||||
status, stream, fromEventNumber, readDirection, events, nextEventNumber, lastEventNumber, isEndOfStream
|
||||
) {
|
||||
Object.defineProperties(this, {
|
||||
status: {
|
||||
enumerable: true, value: status
|
||||
},
|
||||
stream: {
|
||||
enumerable: true, value: stream
|
||||
},
|
||||
fromEventNumber: {
|
||||
enumerable: true, value: fromEventNumber
|
||||
},
|
||||
readDirection: {
|
||||
enumerable: true, value: readDirection
|
||||
},
|
||||
events: {
|
||||
enumerable: true, value: events ? events.map(function(ev) { return new ResolvedEvent(ev); }) : []
|
||||
},
|
||||
nextEventNumber: {
|
||||
enumerable: true, value: nextEventNumber
|
||||
},
|
||||
lastEventNumber: {
|
||||
enumerable: true, value: lastEventNumber
|
||||
},
|
||||
isEndOfStream: {
|
||||
enumerable: true, value: isEndOfStream
|
||||
}
|
||||
})
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,23 +166,12 @@ function StreamEventsSlice(
|
||||
* @property {ResolvedEvent[]} events
|
||||
*/
|
||||
function AllEventsSlice(readDirection, fromPosition, nextPosition, events) {
|
||||
Object.defineProperties(this, {
|
||||
readDirection: {
|
||||
enumerable: true, value: readDirection
|
||||
},
|
||||
fromPosition: {
|
||||
enumerable: true, value: fromPosition
|
||||
},
|
||||
nextPosition: {
|
||||
enumerable: true, value: nextPosition
|
||||
},
|
||||
events: {
|
||||
enumerable: true, value: events ? events.map(function(ev){ return new ResolvedEvent(ev); }) : []
|
||||
},
|
||||
isEndOfStream: {
|
||||
enumerable: true, value: events === null || events.length === 0
|
||||
}
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -254,32 +180,28 @@ function AllEventsSlice(readDirection, fromPosition, nextPosition, events) {
|
||||
* @property {Position} logPosition
|
||||
*/
|
||||
function DeleteResult(logPosition) {
|
||||
Object.defineProperties(this, {
|
||||
logPosition: {
|
||||
enumerable: true, value: logPosition
|
||||
}
|
||||
});
|
||||
this.logPosition = logPosition;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} stream
|
||||
* @param {boolean} isStreamDeleted
|
||||
* @param {number} metastreamVersion
|
||||
* @param {Long} metastreamVersion
|
||||
* @param {object} streamMetadata
|
||||
* @constructor
|
||||
* @property {string} stream
|
||||
* @property {boolean} isStreamDeleted
|
||||
* @property {number} metastreamVersion
|
||||
* @property {Long} metastreamVersion
|
||||
* @property {object} streamMetadata
|
||||
*/
|
||||
function RawStreamMetadataResult(stream, isStreamDeleted, metastreamVersion, streamMetadata) {
|
||||
ensure.notNullOrEmpty(stream);
|
||||
Object.defineProperties(this, {
|
||||
stream: {enumerable: true, value: stream},
|
||||
isStreamDeleted: {enumerable: true, value: isStreamDeleted},
|
||||
metastreamVersion: {enumerable: true, value: metastreamVersion},
|
||||
streamMetadata: {enumerable: true, value: streamMetadata}
|
||||
});
|
||||
this.stream = stream;
|
||||
this.isStreamDeleted = isStreamDeleted;
|
||||
this.metastreamVersion = metastreamVersion;
|
||||
this.streamMetadata = streamMetadata;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
const PersistentSubscriptionCreateStatus = {
|
||||
@ -287,6 +209,7 @@ const PersistentSubscriptionCreateStatus = {
|
||||
NotFound: 'notFound',
|
||||
Failure: 'failure'
|
||||
};
|
||||
Object.freeze(PersistentSubscriptionCreateStatus);
|
||||
|
||||
/**
|
||||
* @param {string} status
|
||||
@ -294,9 +217,8 @@ const PersistentSubscriptionCreateStatus = {
|
||||
* @property {string} status
|
||||
*/
|
||||
function PersistentSubscriptionCreateResult(status) {
|
||||
Object.defineProperties(this, {
|
||||
status: {enumerable: true, value: status}
|
||||
});
|
||||
this.status = status;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
const PersistentSubscriptionUpdateStatus = {
|
||||
@ -305,6 +227,7 @@ const PersistentSubscriptionUpdateStatus = {
|
||||
Failure: 'failure',
|
||||
AccessDenied: 'accessDenied'
|
||||
};
|
||||
Object.freeze(PersistentSubscriptionUpdateStatus);
|
||||
|
||||
/**
|
||||
* @param {string} status
|
||||
@ -312,15 +235,15 @@ const PersistentSubscriptionUpdateStatus = {
|
||||
* @property {string} status
|
||||
*/
|
||||
function PersistentSubscriptionUpdateResult(status) {
|
||||
Object.defineProperties(this, {
|
||||
status: {enumerable: true, value: status}
|
||||
});
|
||||
this.status = status;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
const PersistentSubscriptionDeleteStatus = {
|
||||
Success: 'success',
|
||||
Failure: 'failure'
|
||||
};
|
||||
Object.freeze(PersistentSubscriptionDeleteStatus);
|
||||
|
||||
/**
|
||||
* @param {string} status
|
||||
@ -328,9 +251,8 @@ const PersistentSubscriptionDeleteStatus = {
|
||||
* @property {string} status
|
||||
*/
|
||||
function PersistentSubscriptionDeleteResult(status) {
|
||||
Object.defineProperties(this, {
|
||||
status: {enumerable: true, value: status}
|
||||
});
|
||||
this.status = status;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
// Exports Constructors
|
||||
|
@ -3,5 +3,6 @@ const SliceReadStatus = {
|
||||
StreamNotFound: 'streamNotFound',
|
||||
StreamDeleted: 'streamDeleted'
|
||||
};
|
||||
Object.freeze(SliceReadStatus);
|
||||
|
||||
module.exports = SliceReadStatus;
|
||||
|
@ -3,5 +3,6 @@ const SystemConsumerStrategies = {
|
||||
RoundRobin: 'RoundRobin',
|
||||
Pinned: 'Pinned'
|
||||
};
|
||||
Object.freeze(SystemConsumerStrategies);
|
||||
|
||||
module.exports = SystemConsumerStrategies;
|
||||
|
@ -71,7 +71,9 @@ const TcpCommand = {
|
||||
NotHandled: 0xF1,
|
||||
Authenticate: 0xF2,
|
||||
Authenticated: 0xF3,
|
||||
NotAuthenticated: 0xF4
|
||||
NotAuthenticated: 0xF4,
|
||||
IdentifyClient: 0xF5,
|
||||
ClientIdentified: 0xF6
|
||||
};
|
||||
|
||||
var _reverseLookup = {};
|
||||
|
@ -1,4 +1,4 @@
|
||||
var uuid = require('uuid');
|
||||
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 = uuid.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;
|
||||
uuid.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;
|
||||
uuid.parse(this.correlationId, res, CorrelationOffset);
|
||||
guidParse.parse(this.correlationId, res, CorrelationOffset);
|
||||
if (this.data)
|
||||
this.data.copyTo(res, AuthOffset);
|
||||
return res;
|
||||
|
@ -10,11 +10,9 @@ var ensure = require('../common/utils/ensure');
|
||||
function UserCredentials(username, password) {
|
||||
ensure.notNullOrEmpty(username, 'username');
|
||||
ensure.notNullOrEmpty(password, 'password');
|
||||
|
||||
Object.defineProperties(this, {
|
||||
username: {enumerable: true, value: username},
|
||||
password: {enumerable: true, value: password}
|
||||
});
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
module.exports = UserCredentials;
|
@ -1,7 +1,8 @@
|
||||
var net = require('net');
|
||||
var tls = require('tls');
|
||||
var createBufferSegment = require('../../common/bufferSegment');
|
||||
|
||||
const MaxSendPacketSize = 64 * 1000;
|
||||
const MaxSendPacketSize = 64 * 1024;
|
||||
|
||||
function TcpConnection(log, connectionId, remoteEndPoint, onConnectionClosed) {
|
||||
this._socket = null;
|
||||
@ -34,8 +35,11 @@ TcpConnection.prototype._initSocket = function(socket) {
|
||||
this._localEndPoint = {host: socket.localAddress, port: socket.localPort};
|
||||
this._remoteEndPoint.host = socket.remoteAddress;
|
||||
|
||||
this._socket.on('drain', this._trySend.bind(this));
|
||||
this._socket.on('error', this._processError.bind(this));
|
||||
this._socket.on('data', this._processReceive.bind(this));
|
||||
|
||||
this._trySend();
|
||||
};
|
||||
|
||||
TcpConnection.prototype.enqueueSend = function(bufSegmentArray) {
|
||||
@ -54,19 +58,20 @@ TcpConnection.prototype._trySend = function() {
|
||||
|
||||
var buffers = [];
|
||||
var bytes = 0;
|
||||
var sendPiece = this._sendQueue.shift();
|
||||
while(sendPiece) {
|
||||
if (bytes + sendPiece.length > MaxSendPacketSize)
|
||||
break;
|
||||
|
||||
var sendPiece;
|
||||
while(sendPiece = this._sendQueue.shift()) {
|
||||
buffers.push(sendPiece);
|
||||
bytes += sendPiece.length;
|
||||
|
||||
sendPiece = this._sendQueue.shift();
|
||||
if (bytes > MaxSendPacketSize)
|
||||
break;
|
||||
}
|
||||
|
||||
var joinedBuffers = Buffer.concat(buffers, bytes);
|
||||
this._socket.write(joinedBuffers);
|
||||
if (!this._socket.write(joinedBuffers)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setImmediate(this._trySend.bind(this));
|
||||
};
|
||||
|
||||
TcpConnection.prototype._processError = function(err) {
|
||||
@ -132,11 +137,16 @@ TcpConnection.prototype._closeInternal = function(err, reason) {
|
||||
};
|
||||
|
||||
TcpConnection.createConnectingConnection = function(
|
||||
log, connectionId, remoteEndPoint, connectionTimeout,
|
||||
onConnectionEstablished, onConnectionFailed, onConnectionClosed
|
||||
log, connectionId, remoteEndPoint, ssl, targetHost, validateServer,
|
||||
connectionTimeout, onConnectionEstablished, onConnectionFailed, onConnectionClosed
|
||||
) {
|
||||
var connection = new TcpConnection(log, connectionId, remoteEndPoint, onConnectionClosed);
|
||||
var socket = net.connect(remoteEndPoint.port, remoteEndPoint.host);
|
||||
var provider = ssl ? tls : net;
|
||||
var options = {
|
||||
servername: targetHost,
|
||||
rejectUnauthorized: validateServer
|
||||
};
|
||||
var socket = provider.connect(remoteEndPoint.port, remoteEndPoint.host, options);
|
||||
function onError(err) {
|
||||
if (onConnectionFailed)
|
||||
onConnectionFailed(connection, err);
|
||||
|
@ -38,15 +38,14 @@ function TcpPackageConnection(
|
||||
this._framer = new LengthPrefixMessageFramer();
|
||||
this._framer.registerMessageArrivedCallback(this._incomingMessageArrived.bind(this));
|
||||
|
||||
//TODO ssl
|
||||
var self = this;
|
||||
this._connection = TcpConnection.createConnectingConnection(
|
||||
log,
|
||||
connectionId,
|
||||
remoteEndPoint,
|
||||
//ssl,
|
||||
//targetHost,
|
||||
//validateServer,
|
||||
ssl,
|
||||
targetHost,
|
||||
validateServer,
|
||||
timeout,
|
||||
function(tcpConnection) {
|
||||
log.debug("TcpPackageConnection: connected to [%j, L%j, %s].", tcpConnection.remoteEndPoint, tcpConnection.localEndPoint, connectionId);
|
||||
|
@ -1,13 +1,17 @@
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
var ANY_VERSION = Long.fromNumber(client.expectedVersion.any);
|
||||
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
||||
|
||||
module.exports = {
|
||||
'Append One Event To Stream Happy Path': function(test) {
|
||||
test.expect(2);
|
||||
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
||||
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, event)
|
||||
this.conn.appendToStream(this.testStreamName, ANY_VERSION, event)
|
||||
.then(function(result) {
|
||||
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, 0);
|
||||
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
test.done();
|
||||
})
|
||||
@ -25,9 +29,44 @@ module.exports = {
|
||||
else
|
||||
events.push(client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent'));
|
||||
}
|
||||
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
|
||||
this.conn.appendToStream(this.testStreamName, ANY_VERSION, events)
|
||||
.then(function(result) {
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, expectedVersion);
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Append Multiple Events To Stream Happy Path Stress Test': function(test) {
|
||||
test.expect(2);
|
||||
const expectedVersion = 1000;
|
||||
var events = [];
|
||||
for(var i = 0; i <= expectedVersion; i++) {
|
||||
if (i % 2 === 0)
|
||||
events.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent'));
|
||||
else
|
||||
events.push(client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent'));
|
||||
}
|
||||
this.conn.appendToStream(this.testStreamName, ANY_VERSION, events)
|
||||
.then(function(result) {
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Append Large event': function(test) {
|
||||
test.expect(2);
|
||||
const largeData = Buffer.alloc(3 * 1024 *1024, " ");
|
||||
const event = client.createJsonEventData(uuid.v4(), {a: largeData.toString()}, null, 'largePayloadEvent');
|
||||
|
||||
this.conn.appendToStream(this.testStreamName, ANY_VERSION, event)
|
||||
.then(function(result) {
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
test.done();
|
||||
})
|
||||
@ -38,7 +77,7 @@ module.exports = {
|
||||
'Append To Stream Wrong Expected Version': function(test) {
|
||||
test.expect(1);
|
||||
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
||||
this.conn.appendToStream(this.testStreamName, 10, event)
|
||||
this.conn.appendToStream(this.testStreamName, Long.fromNumber(10), event)
|
||||
.then(function(result) {
|
||||
test.fail("Append succeeded but should have failed.");
|
||||
test.done();
|
||||
@ -53,10 +92,10 @@ module.exports = {
|
||||
'Append To Stream Deleted': function(test) {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, client.expectedVersion.noStream, true)
|
||||
this.conn.deleteStream(this.testStreamName, NOSTREAM_VERSION, true)
|
||||
.then(function() {
|
||||
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
||||
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
|
||||
return self.conn.appendToStream(self.testStreamName, ANY_VERSION, event)
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("Append succeeded but should have failed.");
|
||||
@ -73,10 +112,10 @@ module.exports = {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
var metadata = {$acl: {$w: "$admins"}};
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.noStream, metadata)
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, NOSTREAM_VERSION, metadata)
|
||||
.then(function() {
|
||||
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
||||
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
|
||||
return self.conn.appendToStream(self.testStreamName, ANY_VERSION, event)
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("Append succeeded but should have failed.");
|
||||
|
@ -71,6 +71,32 @@ function eventEqualEventData(name, resolvedEvent, eventData) {
|
||||
this.ok(Buffer.compare(ev.metadata, eventData.metadata) === 0, name + ".originalEvent.metadata is not equal to original metadata.");
|
||||
}
|
||||
|
||||
function testLiveEvent(name, event, evNumber) {
|
||||
this.ok(event.event, name + ".event not defined (or null)");
|
||||
this.ok(event.originalEvent, name + ".originalEvent not defined (or null)");
|
||||
this.ok(event.isResolved === false, name + ".isResolved should be true");
|
||||
this.ok(event.originalPosition instanceof client.Position, name + ".originalPosition is not an instance of Position");
|
||||
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
|
||||
if (typeof evNumber === 'number') {
|
||||
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
||||
} else {
|
||||
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
|
||||
}
|
||||
}
|
||||
|
||||
function testReadEvent(name, event, evNumber) {
|
||||
this.ok(event.event, name + ".event not defined (or null)");
|
||||
this.ok(event.originalEvent, name + ".originalEvent not defined (or null)");
|
||||
this.ok(event.isResolved === false, name + ".isResolved should be true");
|
||||
this.ok(event.originalPosition === null, name + ".originalPosition is not null");
|
||||
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
|
||||
if (typeof evNumber === 'number') {
|
||||
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
||||
} else {
|
||||
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
|
||||
}
|
||||
}
|
||||
|
||||
var _ = {
|
||||
'setUp': setUp,
|
||||
'tearDown': tearDown
|
||||
@ -84,6 +110,8 @@ function wrap(name, testFunc) {
|
||||
test.areEqual = areEqual.bind(test);
|
||||
test.fail = fail.bind(test);
|
||||
test.eventEqualEventData = eventEqualEventData.bind(test);
|
||||
test.testLiveEvent = testLiveEvent.bind(test);
|
||||
test.testReadEvent = testReadEvent.bind(test);
|
||||
return testFunc.call(this, test);
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,24 @@ module.exports = {
|
||||
if (err) return test.done(err);
|
||||
test.done();
|
||||
}
|
||||
}*/
|
||||
}*/,
|
||||
'Connect to secure tcp endpoint': function(test) {
|
||||
var conn = client.createConnection({
|
||||
useSslConnection: true,
|
||||
targetHost: 'localhost',
|
||||
validateServer: false
|
||||
}, 'tcp://localhost:1115');
|
||||
conn.on('error', function (err) {
|
||||
test.done(err);
|
||||
});
|
||||
conn.connect()
|
||||
.catch(function (err) {
|
||||
test.done(err);
|
||||
});
|
||||
conn.on('connected', function () {
|
||||
test.done();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
testBase.init(module.exports, false);
|
@ -1,5 +1,6 @@
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
@ -16,7 +17,7 @@ module.exports = {
|
||||
'Test Delete Stream Soft Happy Path': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, 1, false)
|
||||
this.conn.deleteStream(this.testStreamName, Long.fromNumber(1), false)
|
||||
.then(function(result) {
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
return self.conn.getStreamMetadataRaw(self.testStreamName);
|
||||
@ -34,7 +35,7 @@ module.exports = {
|
||||
'Test Delete Stream Hard Happy Path': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, 1, true)
|
||||
this.conn.deleteStream(this.testStreamName, Long.fromNumber(1), true)
|
||||
.then(function(result) {
|
||||
test.ok(result.logPosition, "No log position in result.");
|
||||
return self.conn.getStreamMetadataRaw(self.testStreamName);
|
||||
@ -51,7 +52,7 @@ module.exports = {
|
||||
},
|
||||
'Test Delete Stream With Wrong Expected Version': function(test) {
|
||||
test.expect(1);
|
||||
this.conn.deleteStream(this.testStreamName, 10)
|
||||
this.conn.deleteStream(this.testStreamName, Long.fromNumber(10))
|
||||
.then(function(result) {
|
||||
test.fail("Delete succeeded but should have failed.");
|
||||
test.done();
|
||||
@ -68,7 +69,7 @@ module.exports = {
|
||||
var self = this;
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.any, {$acl: {$d: "$admins"}})
|
||||
.then(function() {
|
||||
return self.conn.deleteStream(self.testStreamName, 10);
|
||||
return self.conn.deleteStream(self.testStreamName, Long.fromNumber(10));
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("Delete succeeded but should have failed.");
|
||||
@ -86,7 +87,7 @@ module.exports = {
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, 1, true)
|
||||
.then(function() {
|
||||
return self.conn.deleteStream(self.testStreamName, 1, true);
|
||||
return self.conn.deleteStream(self.testStreamName, Long.fromNumber(1), true);
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("Delete succeeded but should have failed.");
|
||||
|
@ -1,10 +1,11 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
const Long = require('long');
|
||||
|
||||
const EMPTY_VERSION = Long.fromNumber(client.expectedVersion.emptyStream);
|
||||
|
||||
module.exports = {
|
||||
'Test Set Stream Metadata Raw': function(test) {
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.emptyStream, {$maxCount: 100})
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, EMPTY_VERSION, {$maxCount: 100})
|
||||
.then(function(result) {
|
||||
test.done();
|
||||
})
|
||||
|
@ -9,6 +9,20 @@ function createRandomEvent() {
|
||||
|
||||
var testStreamName = 'test-' + uuid.v4();
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
}
|
||||
|
||||
function delayOnlyFirst(count, action) {
|
||||
if (count === 0) return action();
|
||||
return delay(200)
|
||||
.then(function () {
|
||||
action();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Test Create Persistent Subscription': function(test) {
|
||||
var settings = client.PersistentSubscriptionSettings.create();
|
||||
@ -22,7 +36,8 @@ module.exports = {
|
||||
},
|
||||
//TODO: Update Persistent Subscription
|
||||
'Test ConnectTo Persistent Subscription': function(test) {
|
||||
test.expect(2);
|
||||
test.expect(4);
|
||||
var receivedEvents = [];
|
||||
var _doneCount = 0;
|
||||
function done(err) {
|
||||
test.ok(!err, err ? err.stack : '');
|
||||
@ -31,17 +46,26 @@ module.exports = {
|
||||
test.done();
|
||||
}
|
||||
function eventAppeared(s, e) {
|
||||
s.stop();
|
||||
return delayOnlyFirst(receivedEvents.length, function () {
|
||||
receivedEvents.push(e);
|
||||
if (receivedEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
done(error);
|
||||
if (error) return done(error);
|
||||
test.ok(receivedEvents[1].originalEventNumber > receivedEvents[0].originalEventNumber, "Received events are out of order.");
|
||||
done();
|
||||
}
|
||||
var subscription = this.conn.connectToPersistentSubscription(testStreamName, 'consumer-1', eventAppeared, subscriptionDropped);
|
||||
this.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent()])
|
||||
.then(function () {
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
var self = this;
|
||||
this.conn.connectToPersistentSubscription(testStreamName, 'consumer-1', eventAppeared, subscriptionDropped)
|
||||
.then(function(subscription) {
|
||||
test.ok(subscription, "Subscription is null.");
|
||||
return self.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent(), createRandomEvent()]);
|
||||
})
|
||||
.then(function () {
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
},
|
||||
'Test Delete Persistent Subscription': function(test) {
|
||||
this.conn.deletePersistentSubscription(testStreamName, 'consumer-1', adminCredentials)
|
||||
|
47
test/projections_test.js
Normal file
47
test/projections_test.js
Normal file
@ -0,0 +1,47 @@
|
||||
const client = require('../src/client');
|
||||
const userCredentials = new client.UserCredentials('admin', 'changeit');
|
||||
|
||||
const log = new client.NoopLogger();
|
||||
const httpEndpoint = 'http://127.0.0.1:2113';
|
||||
const operationTimeout = 5000;
|
||||
|
||||
const simpleProjection = "\
|
||||
fromStream('$stats-127.0.0.1:2113')\
|
||||
.when({\
|
||||
$init: function(){\
|
||||
return {\
|
||||
count: 0\
|
||||
}\
|
||||
},\
|
||||
$any: function(s,e){\
|
||||
s.count += 1;\
|
||||
}\
|
||||
})\
|
||||
";
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
this.projectionsManager = new client.ProjectionsManager(log, httpEndpoint, operationTimeout);
|
||||
cb();
|
||||
},
|
||||
'Create One Time Projection Happy Path': function(test) {
|
||||
test.expect(1);
|
||||
|
||||
this.projectionsManager.createOneTime(simpleProjection, userCredentials)
|
||||
.then(function (result) {
|
||||
test.equal(result, undefined);
|
||||
test.done();
|
||||
})
|
||||
.catch(test.done);
|
||||
},
|
||||
'List All Happy Path': function(test) {
|
||||
test.expect(1);
|
||||
this.projectionsManager.listAll(userCredentials)
|
||||
.then(function (projections) {
|
||||
test.ok(projections.length > 0, "no projections");
|
||||
test.done();
|
||||
})
|
||||
.catch(test.done);
|
||||
}
|
||||
//TODO: other tests
|
||||
};
|
@ -1,107 +1,145 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
||||
var ANY_VERSION = Long.fromNumber(client.expectedVersion.any);
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
this.expectedEvent = {a: uuid.v4(), b: Math.random()};
|
||||
this.expectedEvent = {
|
||||
a: uuid.v4(),
|
||||
b: Math.random()
|
||||
};
|
||||
this.expectedEventType = 'anEvent';
|
||||
this.expectedEventId = uuid.v4();
|
||||
var event = client.createJsonEventData(this.expectedEventId, this.expectedEvent, null, this.expectedEventType);
|
||||
this.conn.appendToStream(this.testStreamName, client.expectedVersion.noStream, event)
|
||||
.then(function() {
|
||||
cb();
|
||||
})
|
||||
.catch(cb);
|
||||
this.conn.appendToStream(this.testStreamName, NOSTREAM_VERSION, event)
|
||||
.then(function() {
|
||||
cb();
|
||||
})
|
||||
.catch(cb);
|
||||
},
|
||||
'Read Event Happy Path': function(test) {
|
||||
test.expect(8);
|
||||
var self = this;
|
||||
this.conn.readEvent(this.testStreamName, 0)
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.Success);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, 0);
|
||||
test.ok(result.event !== null, "event is null.");
|
||||
test.ok(result.event.originalEvent !== null, "event.originalEvent is null.");
|
||||
var event = JSON.parse(result.event.originalEvent.data.toString());
|
||||
test.areEqual('event.eventId', result.event.originalEvent.eventId, self.expectedEventId);
|
||||
test.areEqual('event.eventType', result.event.originalEvent.eventType, self.expectedEventType);
|
||||
test.areEqual('decoded event.data', event, self.expectedEvent);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
})
|
||||
this.conn.readEvent(this.testStreamName, Long.fromNumber(0))
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.Success);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
|
||||
test.ok(result.event !== null, "event is null.");
|
||||
test.ok(result.event.originalEvent !== null, "event.originalEvent is null.");
|
||||
var event = JSON.parse(result.event.originalEvent.data.toString());
|
||||
test.areEqual('event.eventId', result.event.originalEvent.eventId, self.expectedEventId);
|
||||
test.areEqual('event.eventType', result.event.originalEvent.eventType, self.expectedEventType);
|
||||
test.areEqual('decoded event.data', event, self.expectedEvent);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
})
|
||||
},
|
||||
'Read Event From Non-Existing Stream': function(test) {
|
||||
test.expect(4);
|
||||
var anotherStream = 'test' + uuid.v4();
|
||||
this.conn.readEvent(anotherStream, 0)
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.NoStream);
|
||||
test.areEqual('stream', result.stream, anotherStream);
|
||||
test.areEqual('eventNumber', result.eventNumber, 0);
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
this.conn.readEvent(anotherStream, Long.fromNumber(0))
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.NoStream);
|
||||
test.areEqual('stream', result.stream, anotherStream);
|
||||
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Read Event From Deleted Stream': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, 0, true)
|
||||
.then(function() {
|
||||
return self.conn.readEvent(self.testStreamName, 0)
|
||||
})
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.StreamDeleted);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, 0);
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
.then(function() {
|
||||
return self.conn.readEvent(self.testStreamName, Long.fromNumber(0))
|
||||
})
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.StreamDeleted);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Read Event With Inexisting Version': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
return self.conn.readEvent(self.testStreamName, 1)
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.NotFound);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, 1);
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
return self.conn.readEvent(self.testStreamName, Long.fromNumber(1))
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.NotFound);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(1));
|
||||
test.areEqual('event', result.event, null);
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Read Event With No Access': function(test) {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
var metadata = {$acl: {$r: '$admins'}};
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, client.expectedVersion.noStream, metadata)
|
||||
.then(function(){
|
||||
return self.conn.readEvent(self.testStreamName, 0);
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("readEvent succeeded but should have failed.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isAccessDenied = err instanceof client.AccessDeniedError;
|
||||
test.ok(isAccessDenied, "readEvent should have failed with AccessDeniedError, got " + err.constructor.name);
|
||||
if (isAccessDenied) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
}
|
||||
var metadata = {
|
||||
$acl: {
|
||||
$r: '$admins'
|
||||
}
|
||||
};
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, NOSTREAM_VERSION, metadata)
|
||||
.then(function() {
|
||||
return self.conn.readEvent(self.testStreamName, Long.fromNumber(0));
|
||||
})
|
||||
.then(function(result) {
|
||||
test.fail("readEvent succeeded but should have failed.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isAccessDenied = err instanceof client.AccessDeniedError;
|
||||
test.ok(isAccessDenied, "readEvent should have failed with AccessDeniedError, got " + err.constructor.name);
|
||||
if (isAccessDenied) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Read a Large Event': function(test) {
|
||||
test.expect(8);
|
||||
var self = this;
|
||||
const largeData = Buffer.alloc(3 * 1024 * 1024, " ");
|
||||
|
||||
const largeEvent = client.createJsonEventData(uuid.v4(), {
|
||||
a: largeData.toString()
|
||||
}, null, 'largePayloadEvent');
|
||||
|
||||
this.conn.appendToStream(this.testStreamName, ANY_VERSION, largeEvent)
|
||||
.then(function(result) {
|
||||
self.conn.readEvent(self.testStreamName, Long.fromNumber(1))
|
||||
.then(function(result) {
|
||||
test.areEqual('status', result.status, client.eventReadStatus.Success);
|
||||
test.areEqual('stream', result.stream, self.testStreamName);
|
||||
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(1));
|
||||
test.ok(result.event !== null, "event is null.");
|
||||
test.ok(result.event.originalEvent !== null, "event.originalEvent is null.");
|
||||
var event = JSON.parse(result.event.originalEvent.data.toString());
|
||||
test.areEqual('event.eventId', result.event.originalEvent.eventId, largeEvent.eventId);
|
||||
test.areEqual('event.eventType', result.event.originalEvent.eventType, 'largePayloadEvent');
|
||||
test.areEqual('decoded event.data', event, JSON.parse(largeEvent.data.toString()));
|
||||
test.done();
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
require('./common/base_test').init(module.exports);
|
@ -1,15 +1,17 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
const streamSize = 100;
|
||||
|
||||
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
this.eventsData = [];
|
||||
for(var i = 0; i < streamSize; i++)
|
||||
this.eventsData.push(client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, null, 'anEvent'));
|
||||
this.conn.appendToStream(this.testStreamName, client.expectedVersion.noStream, this.eventsData)
|
||||
this.conn.appendToStream(this.testStreamName, NOSTREAM_VERSION, this.eventsData)
|
||||
.then(function() {
|
||||
cb();
|
||||
})
|
||||
@ -18,19 +20,19 @@ module.exports = {
|
||||
'Read Stream Events Backward Happy Path': function(test) {
|
||||
test.expect(7 + (streamSize * 6));
|
||||
var self = this;
|
||||
this.conn.readStreamEventsBackward(this.testStreamName, streamSize-1, streamSize)
|
||||
this.conn.readStreamEventsBackward(this.testStreamName, Long.fromNumber(streamSize-1), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
||||
test.areEqual('slice.readDirection', slice.readDirection, 'backward');
|
||||
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, -1);
|
||||
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, streamSize-1);
|
||||
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, Long.fromNumber(-1));
|
||||
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, Long.fromNumber(streamSize-1));
|
||||
test.areEqual('slice.isEndOfStream', slice.isEndOfStream, true);
|
||||
for(var i = 0; i < streamSize; i++) {
|
||||
var reverseIndex = streamSize - i - 1;
|
||||
test.eventEqualEventData('slice.events[' + i + ']', slice.events[i], self.eventsData[reverseIndex]);
|
||||
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, reverseIndex);
|
||||
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, Long.fromNumber(reverseIndex));
|
||||
}
|
||||
test.done();
|
||||
})
|
||||
@ -41,11 +43,11 @@ module.exports = {
|
||||
'Read Stream Events Backward With Non-Existing Stream': function(test) {
|
||||
test.expect(4);
|
||||
var anotherStream = 'test' + uuid.v4();
|
||||
this.conn.readStreamEventsBackward(anotherStream, streamSize-1, streamSize)
|
||||
this.conn.readStreamEventsBackward(anotherStream, Long.fromNumber(streamSize-1), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.sliceReadStatus.StreamNotFound);
|
||||
test.areEqual('slice.stream', slice.stream, anotherStream);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -56,14 +58,14 @@ module.exports = {
|
||||
'Read Stream Events Backward With Deleted Stream': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, streamSize-1, true)
|
||||
this.conn.deleteStream(this.testStreamName, Long.fromNumber(streamSize-1), true)
|
||||
.then(function() {
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, streamSize-1, streamSize)
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize-1), streamSize)
|
||||
})
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.StreamDeleted);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -74,11 +76,11 @@ module.exports = {
|
||||
'Read Stream Events Backward With Inexisting Version': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, streamSize * 2, streamSize)
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize * 2), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize*2);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize*2));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -90,9 +92,9 @@ module.exports = {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
var metadata = {$acl: {$r: '$admins'}};
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, client.expectedVersion.noStream, metadata)
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, NOSTREAM_VERSION, metadata)
|
||||
.then(function(){
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, streamSize-1, streamSize);
|
||||
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize-1), streamSize);
|
||||
})
|
||||
.then(function(slice) {
|
||||
test.fail("readStreamEventsBackward succeeded but should have failed.");
|
||||
|
@ -1,35 +1,37 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
const streamSize = 100;
|
||||
|
||||
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
this.eventsData = [];
|
||||
for(var i = 0; i < streamSize; i++)
|
||||
this.eventsData.push(client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, null, 'anEvent'));
|
||||
this.conn.appendToStream(this.testStreamName, client.expectedVersion.noStream, this.eventsData)
|
||||
this.conn.appendToStream(this.testStreamName, NOSTREAM_VERSION, this.eventsData)
|
||||
.then(function() {
|
||||
cb();
|
||||
})
|
||||
.catch(cb);
|
||||
},
|
||||
'Read Stream Events Forward Happy Path': function(test) {
|
||||
test.expect(7 + (streamSize * 6));
|
||||
test.expect(7 + (streamSize * 11));
|
||||
var self = this;
|
||||
this.conn.readStreamEventsForward(this.testStreamName, 0, streamSize)
|
||||
this.conn.readStreamEventsForward(this.testStreamName, Long.fromNumber(0), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
|
||||
test.areEqual('slice.readDirection', slice.readDirection, 'forward');
|
||||
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, streamSize);
|
||||
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, streamSize-1);
|
||||
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, Long.fromNumber(streamSize));
|
||||
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, Long.fromNumber(streamSize-1));
|
||||
test.areEqual('slice.isEndOfStream', slice.isEndOfStream, true);
|
||||
for(var i = 0; i < streamSize; i++) {
|
||||
test.eventEqualEventData('slice.events[' + i + ']', slice.events[i], self.eventsData[i]);
|
||||
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, i);
|
||||
test.testReadEvent('slice.events[' + i + ']', slice.events[i], i);
|
||||
}
|
||||
test.done();
|
||||
})
|
||||
@ -40,11 +42,11 @@ module.exports = {
|
||||
'Read Stream Events Forward With Non-Existing Stream': function(test) {
|
||||
test.expect(4);
|
||||
var anotherStream = 'test' + uuid.v4();
|
||||
this.conn.readStreamEventsForward(anotherStream, 0, streamSize)
|
||||
this.conn.readStreamEventsForward(anotherStream, Long.fromNumber(0), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.sliceReadStatus.StreamNotFound);
|
||||
test.areEqual('slice.stream', slice.stream, anotherStream);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -55,14 +57,14 @@ module.exports = {
|
||||
'Read Stream Events Forward With Deleted Stream': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, streamSize-1, true)
|
||||
this.conn.deleteStream(this.testStreamName, Long.fromNumber(streamSize-1), true)
|
||||
.then(function() {
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, 0, streamSize)
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, Long.fromNumber(0), streamSize)
|
||||
})
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.StreamDeleted);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -73,11 +75,11 @@ module.exports = {
|
||||
'Read Stream Events Forward With Inexisting Version': function(test) {
|
||||
test.expect(4);
|
||||
var self = this;
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, streamSize * 2, streamSize)
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, Long.fromNumber(streamSize * 2), streamSize)
|
||||
.then(function(slice) {
|
||||
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
||||
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize*2);
|
||||
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize*2));
|
||||
test.areEqual('slice.events.length', slice.events.length, 0);
|
||||
test.done();
|
||||
})
|
||||
@ -89,9 +91,9 @@ module.exports = {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
var metadata = {$acl: {$r: '$admins'}};
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, client.expectedVersion.noStream, metadata)
|
||||
this.conn.setStreamMetadataRaw(self.testStreamName, NOSTREAM_VERSION, metadata)
|
||||
.then(function(){
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, 0, streamSize);
|
||||
return self.conn.readStreamEventsForward(self.testStreamName, Long.fromNumber(0), streamSize);
|
||||
})
|
||||
.then(function(slice) {
|
||||
test.fail("readStreamEventsForward succeeded but should have failed.");
|
||||
|
@ -7,9 +7,23 @@ function createRandomEvent() {
|
||||
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
|
||||
}
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
}
|
||||
|
||||
function delayOnlyFirst(count, action) {
|
||||
if (count === 0) return action();
|
||||
return delay(200)
|
||||
.then(function () {
|
||||
action();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Test Subscribe to All From (Start)': function(test) {
|
||||
test.expect(4);
|
||||
test.expect(6);
|
||||
var self = this;
|
||||
var liveProcessing = false;
|
||||
var catchUpEvents = [];
|
||||
@ -19,19 +33,33 @@ module.exports = {
|
||||
test.ok(!err, err ? err.stack : '');
|
||||
_doneCount++;
|
||||
if (_doneCount < 2) return;
|
||||
|
||||
var catchUpInOrder = true;
|
||||
for(var i = 1; i < catchUpEvents.length; i++)
|
||||
catchUpInOrder = catchUpInOrder && (catchUpEvents[i].originalPosition.compareTo(catchUpEvents[i-1].originalPosition) > 0);
|
||||
test.ok(catchUpInOrder, "Catch-up events are out of order.");
|
||||
|
||||
var liveInOrder = true;
|
||||
for(var j = 1; j < liveEvents.length; j++)
|
||||
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
||||
test.ok(liveInOrder, "Live events are out of order.");
|
||||
|
||||
test.done();
|
||||
}
|
||||
function eventAppeared(s, e) {
|
||||
if (liveProcessing) {
|
||||
liveEvents.push(e);
|
||||
s.stop();
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
var isLive = liveProcessing;
|
||||
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
||||
if (isLive) {
|
||||
liveEvents.push(e);
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
if (isLive && liveEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent()];
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
||||
.then(function () {
|
||||
done();
|
||||
@ -39,14 +67,14 @@ module.exports = {
|
||||
.catch(done);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
|
||||
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
||||
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
||||
done(error);
|
||||
}
|
||||
var subscription = this.conn.subscribeToAllFrom(null, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
|
||||
},
|
||||
'Test Subscribe to All From (Position)': function(test) {
|
||||
test.expect(5);
|
||||
test.expect(7);
|
||||
var self = this;
|
||||
var liveProcessing = false;
|
||||
var catchUpEvents = [];
|
||||
@ -56,19 +84,33 @@ module.exports = {
|
||||
test.ok(!err, err ? err.stack : '');
|
||||
_doneCount++;
|
||||
if (_doneCount < 2) return;
|
||||
|
||||
var catchUpInOrder = true;
|
||||
for(var i = 1; i < catchUpEvents.length; i++)
|
||||
catchUpInOrder = catchUpInOrder && (catchUpEvents[i].originalPosition.compareTo(catchUpEvents[i-1].originalPosition) > 0);
|
||||
test.ok(catchUpInOrder, "Catch-up events are out of order.");
|
||||
|
||||
var liveInOrder = true;
|
||||
for(var j = 1; j < liveEvents.length; j++)
|
||||
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
||||
test.ok(liveInOrder, "Live events are out of order.");
|
||||
|
||||
test.done();
|
||||
}
|
||||
function eventAppeared(s, e) {
|
||||
if (liveProcessing) {
|
||||
liveEvents.push(e);
|
||||
s.stop();
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
var isLive = liveProcessing;
|
||||
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
||||
if (isLive) {
|
||||
liveEvents.push(e);
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
if (isLive && liveEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent()];
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
||||
.then(function () {
|
||||
done();
|
||||
@ -76,7 +118,7 @@ module.exports = {
|
||||
.catch(done);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
|
||||
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
||||
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
||||
done(error);
|
||||
}
|
||||
@ -87,7 +129,7 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
'Test Subscribe to All From (End)': function(test) {
|
||||
test.expect(5);
|
||||
test.expect(6);
|
||||
var self = this;
|
||||
var liveProcessing = false;
|
||||
var catchUpEvents = [];
|
||||
@ -97,19 +139,28 @@ module.exports = {
|
||||
test.ok(!err, err ? err.stack : '');
|
||||
_doneCount++;
|
||||
if (_doneCount < 2) return;
|
||||
|
||||
var liveInOrder = true;
|
||||
for(var j = 1; j < liveEvents.length; j++)
|
||||
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
||||
test.ok(liveInOrder, "Live events are out of order.");
|
||||
|
||||
test.done();
|
||||
}
|
||||
function eventAppeared(s, e) {
|
||||
if (liveProcessing) {
|
||||
liveEvents.push(e);
|
||||
s.stop();
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
var isLive = liveProcessing;
|
||||
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
||||
if (isLive) {
|
||||
liveEvents.push(e);
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
if (isLive && liveEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent()];
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
||||
.then(function () {
|
||||
done();
|
||||
@ -117,7 +168,7 @@ module.exports = {
|
||||
.catch(done);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
|
||||
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
||||
test.ok(catchUpEvents.length === 0, "Expecting 0 catchUp event, got " + catchUpEvents.length);
|
||||
done(error);
|
||||
}
|
||||
|
@ -2,6 +2,20 @@ const uuid = require('uuid');
|
||||
const client = require('../src/client');
|
||||
const allCredentials = new client.UserCredentials("admin", "changeit");
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
}
|
||||
|
||||
function delayOnlyFirst(count, action) {
|
||||
if (count === 0) return action();
|
||||
return delay(200)
|
||||
.then(function () {
|
||||
action();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Test Subscribe To All Happy Path': function(test) {
|
||||
const resolveLinkTos = false;
|
||||
@ -30,8 +44,10 @@ module.exports = {
|
||||
|
||||
var receivedEvents = [];
|
||||
function eventAppeared(subscription, event) {
|
||||
receivedEvents.push(event);
|
||||
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
|
||||
delayOnlyFirst(receivedEvents.length, function() {
|
||||
receivedEvents.push(event);
|
||||
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
|
||||
});
|
||||
}
|
||||
function subscriptionDropped(subscription, reason, error) {
|
||||
if (error) return done(error);
|
||||
|
@ -1,14 +1,28 @@
|
||||
var util = require('util');
|
||||
var uuid = require('uuid');
|
||||
var client = require('../src/client');
|
||||
var Long = require('long');
|
||||
|
||||
function createRandomEvent() {
|
||||
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
|
||||
}
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
}
|
||||
|
||||
function delayOnlyFirst(count, action) {
|
||||
if (count === 0) return action();
|
||||
return delay(200)
|
||||
.then(function () {
|
||||
action();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Test Subscribe to Stream From Happy Path': function(test) {
|
||||
test.expect(8);
|
||||
'Test Subscribe to Stream From Beginning (null)': function(test) {
|
||||
test.expect(32);
|
||||
var self = this;
|
||||
var liveProcessing = false;
|
||||
var catchUpEvents = [];
|
||||
@ -22,16 +36,19 @@ module.exports = {
|
||||
}
|
||||
|
||||
function eventAppeared(s, e) {
|
||||
if (liveProcessing) {
|
||||
liveEvents.push(e);
|
||||
s.stop();
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
var isLive = liveProcessing;
|
||||
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
||||
if (isLive) {
|
||||
liveEvents.push(e);
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
if (isLive && liveEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent()];
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
||||
.then(function () {
|
||||
done();
|
||||
@ -39,12 +56,16 @@ module.exports = {
|
||||
.catch(done);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
|
||||
test.ok(catchUpEvents.length >= 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
||||
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
||||
test.testLiveEvent('liveEvents[0]', liveEvents[0], 2);
|
||||
test.testLiveEvent('liveEvents[1]', liveEvents[1], 3);
|
||||
test.ok(catchUpEvents.length === 2, "Expecting 2 catchUp event, got " + catchUpEvents.length);
|
||||
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0], 0);
|
||||
test.testReadEvent('catchUpEvents[1]', catchUpEvents[1], 1);
|
||||
done(error);
|
||||
}
|
||||
|
||||
var events = [createRandomEvent()];
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
this.conn.appendToStream(self.testStreamName, client.expectedVersion.noStream, events)
|
||||
.then(function() {
|
||||
var subscription = self.conn.subscribeToStreamFrom(self.testStreamName, null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
|
||||
@ -55,6 +76,61 @@ module.exports = {
|
||||
test.areEqual("subscription.maxPushQueueSize", subscription.maxPushQueueSize, 10000);
|
||||
})
|
||||
.catch(test.done);
|
||||
},
|
||||
'Test Subscribe to Stream From 0': function(test) {
|
||||
test.expect(26);
|
||||
var self = this;
|
||||
var liveProcessing = false;
|
||||
var catchUpEvents = [];
|
||||
var liveEvents = [];
|
||||
var _doneCount = 0;
|
||||
|
||||
function done(err) {
|
||||
test.ok(!err, err ? err.stack : '');
|
||||
if (++_doneCount < 2) return;
|
||||
test.done();
|
||||
}
|
||||
|
||||
function eventAppeared(s, e) {
|
||||
var isLive = liveProcessing;
|
||||
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
||||
if (isLive) {
|
||||
liveEvents.push(e);
|
||||
} else {
|
||||
catchUpEvents.push(e);
|
||||
}
|
||||
if (isLive && liveEvents.length === 2) s.stop();
|
||||
});
|
||||
}
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
||||
.then(function () {
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
||||
test.testLiveEvent('liveEvents[0]', liveEvents[0], 2);
|
||||
test.testLiveEvent('liveEvents[1]', liveEvents[1], 3);
|
||||
test.ok(catchUpEvents.length === 1, "Expecting 1 catchUp event, got " + catchUpEvents.length);
|
||||
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0], 1);
|
||||
done(error);
|
||||
}
|
||||
|
||||
var events = [createRandomEvent(), createRandomEvent()];
|
||||
this.conn.appendToStream(self.testStreamName, client.expectedVersion.noStream, events)
|
||||
.then(function() {
|
||||
var subscription = self.conn.subscribeToStreamFrom(self.testStreamName, Long.fromNumber(0), false, eventAppeared, liveProcessingStarted, subscriptionDropped);
|
||||
|
||||
test.areEqual("subscription.streamId", subscription.streamId, self.testStreamName);
|
||||
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, false);
|
||||
test.areEqual("subscription.readBatchSize", subscription.readBatchSize, 500);
|
||||
test.areEqual("subscription.maxPushQueueSize", subscription.maxPushQueueSize, 10000);
|
||||
})
|
||||
.catch(test.done);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,20 @@
|
||||
const uuid = require('uuid');
|
||||
const client = require('../src/client');
|
||||
const Long = require('long');
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(resolve, ms);
|
||||
})
|
||||
}
|
||||
|
||||
function delayOnlyFirst(count, action) {
|
||||
if (count === 0) return action();
|
||||
return delay(200)
|
||||
.then(function () {
|
||||
action();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Test Subscribe To Stream Happy Path': function(test) {
|
||||
@ -28,8 +43,10 @@ module.exports = {
|
||||
|
||||
var receivedEvents = [];
|
||||
function eventAppeared(subscription, event) {
|
||||
receivedEvents.push(event);
|
||||
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
|
||||
delayOnlyFirst(receivedEvents.length, function () {
|
||||
receivedEvents.push(event);
|
||||
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
|
||||
});
|
||||
}
|
||||
function subscriptionDropped(subscription, reason, error) {
|
||||
if (error) return done(error);
|
||||
@ -42,7 +59,7 @@ module.exports = {
|
||||
.then(function(subscription) {
|
||||
test.areEqual("subscription.streamId", subscription.streamId, self.testStreamName);
|
||||
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, false);
|
||||
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, client.expectedVersion.emptyStream);
|
||||
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, Long.fromNumber(client.expectedVersion.emptyStream));
|
||||
|
||||
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
|
||||
})
|
||||
|
@ -2,13 +2,17 @@ var uuid = require('uuid');
|
||||
var Long = require('long');
|
||||
var client = require('../src/client');
|
||||
|
||||
var ANY_VERSION = Long.fromNumber(client.expectedVersion.any);
|
||||
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
||||
var EMPTY_VERSION = Long.fromNumber(client.expectedVersion.emptyStream);
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
cb();
|
||||
},
|
||||
'Start A Transaction Happy Path': function(test) {
|
||||
test.expect(1);
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.noStream)
|
||||
this.conn.startTransaction(this.testStreamName, NOSTREAM_VERSION)
|
||||
.then(function(trx) {
|
||||
test.ok(Long.isLong(trx.transactionId), "trx.transactionId should be a Long.");
|
||||
test.done();
|
||||
@ -32,7 +36,7 @@ module.exports = {
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
return self.conn.startTransaction(self.testStreamName, ANY_VERSION);
|
||||
})
|
||||
.then(function(trx) {
|
||||
test.fail("Start Transaction with deleted stream succeeded.");
|
||||
@ -50,9 +54,9 @@ module.exports = {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
var metadata = {$acl: {$w: "$admins"}};
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, -1, metadata)
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, EMPTY_VERSION, metadata)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
return self.conn.startTransaction(self.testStreamName, ANY_VERSION);
|
||||
})
|
||||
.then(function(trx) {
|
||||
test.fail("Start Transaction with no access succeeded.");
|
||||
@ -67,7 +71,7 @@ module.exports = {
|
||||
},
|
||||
'Continue A Transaction Happy Path': function(test) {
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
this.conn.startTransaction(this.testStreamName, EMPTY_VERSION)
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random()}, null, 'anEvent'))
|
||||
.then(function () {
|
||||
@ -88,7 +92,7 @@ module.exports = {
|
||||
'Write/Commit Transaction Happy Path': function(test) {
|
||||
test.expect(2);
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
this.conn.startTransaction(this.testStreamName, EMPTY_VERSION)
|
||||
.then(function(trx) {
|
||||
self.events = [];
|
||||
for(var i = 0; i < 15; i++) {
|
||||
@ -111,7 +115,7 @@ module.exports = {
|
||||
})
|
||||
.then(function(result) {
|
||||
test.ok(result.logPosition, "Missing result.logPosition");
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, self.events.length-1);
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(self.events.length-1));
|
||||
test.done();
|
||||
})
|
||||
.catch(test.done);
|
||||
@ -139,9 +143,9 @@ module.exports = {
|
||||
'Write/Commit Transaction With Deleted Stream': function(test) {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, client.expectedVersion.emptyStream, true)
|
||||
this.conn.deleteStream(this.testStreamName, EMPTY_VERSION, true)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
return self.conn.startTransaction(self.testStreamName, ANY_VERSION);
|
||||
})
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'))
|
||||
@ -163,10 +167,10 @@ module.exports = {
|
||||
'Write/Commit Transaction With No Write Access': function(test) {
|
||||
test.expect(1);
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.any)
|
||||
this.conn.startTransaction(this.testStreamName, ANY_VERSION)
|
||||
.then(function(trx) {
|
||||
var metadata = {$acl: {$w: "$admins"}};
|
||||
return self.conn.setStreamMetadataRaw(self.testStreamName, -1, metadata)
|
||||
return self.conn.setStreamMetadataRaw(self.testStreamName, EMPTY_VERSION, metadata)
|
||||
.then(function () {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'))
|
||||
.then(function () {
|
||||
|
Reference in New Issue
Block a user