You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

392 lines
15 KiB

  1. /// <reference types="node" />
  2. /// <reference types="Long" />
  3. import { EventEmitter } from 'events';
  4. import { StrictEventEmitter } from 'strict-event-emitter-types';
  5. // Expose classes
  6. export class Position {
  7. constructor(commitPosition: number|Long, preparePosition: number|Long);
  8. readonly commitPosition: Long;
  9. readonly preparePosition: Long;
  10. static readonly start: number;
  11. static readonly end: number;
  12. }
  13. export class UserCredentials {
  14. constructor(username: string, password: string);
  15. readonly username: string;
  16. readonly password: string;
  17. }
  18. export class PersistentSubscriptionSettings {
  19. constructor(resolveLinkTos: boolean, startFrom: Long|number, extraStatistics: boolean, messageTimeout: number,
  20. maxRetryCount: number, liveBufferSize: number, readBatchSize: number, historyBufferSize: number,
  21. checkPointAfter: number, minCheckPointCount: number, maxCheckPointCount: number,
  22. maxSubscriberCount: number, namedConsumerStrategy: string)
  23. static create(): PersistentSubscriptionSettings;
  24. }
  25. export namespace SystemConsumerStrategies {
  26. const DispatchToSingle: string;
  27. const RoundRobin: string;
  28. const Pinned: string;
  29. }
  30. export class GossipSeed {
  31. constructor(endPoint: TcpEndPoint, hostHeader: string);
  32. readonly endPoint: TcpEndPoint;
  33. readonly hostHeader: string;
  34. }
  35. export interface ProjectionDetails {
  36. readonly coreProcessingTime: number,
  37. readonly version: number,
  38. readonly epoch: number,
  39. readonly effectiveName: string,
  40. readonly writesInProgress: number,
  41. readonly readsInProgress: number,
  42. readonly partitionsCached: number,
  43. readonly status: string,
  44. readonly stateReason: string,
  45. readonly name: string,
  46. readonly mode: string,
  47. readonly position: string,
  48. readonly progress: number,
  49. readonly lastCheckpoint: string,
  50. readonly eventsProcessedAfterRestart: number,
  51. readonly statusUrl: string,
  52. readonly stateUrl: string,
  53. readonly resultUrl: string,
  54. readonly queryUrl: string,
  55. readonly enableCommandUrl: string,
  56. readonly disableCommandUrl: string,
  57. readonly checkpointStatus: string,
  58. readonly bufferedEvents: number,
  59. readonly writePendingEventsBeforeCheckpoint: number,
  60. readonly writePendingEventsAfterCheckpoint: number
  61. }
  62. export class ProjectionsManager {
  63. constructor(log: Logger, httpEndPoint: string, operationTimeout: number);
  64. enable(name: string, userCredentials: UserCredentials): Promise<void>;
  65. disable(name: string, userCredentials: UserCredentials): Promise<void>;
  66. abort(name: string, userCredentials: UserCredentials): Promise<void>;
  67. reset(name: string, userCredentials: UserCredentials): Promise<void>;
  68. createOneTime(query: string, userCredentials: UserCredentials): Promise<void>;
  69. createTransient(name: string, query: string, userCredentials: UserCredentials): Promise<void>;
  70. createContinuous(name: string, query: string, trackEmittedStreams: boolean, userCredentials: UserCredentials): Promise<void>;
  71. listAll(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
  72. listOneTime(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
  73. listContinuous(userCredentials: UserCredentials): Promise<ProjectionDetails[]>;
  74. getStatus(name: string, userCredentials: UserCredentials): Promise<string>;
  75. getState(name: string, userCredentials: UserCredentials): Promise<string>;
  76. getPartitionState(name: string, partitionId: string, userCredentials: UserCredentials): Promise<string>;
  77. getResult(name: string, userCredentials: UserCredentials): Promise<string>;
  78. getPartitionResult(name: string, partitionId: string, userCredentials: UserCredentials): Promise<string>;
  79. getStatistics(name: string, userCredentials: UserCredentials): Promise<string>;
  80. getQuery(name: string, userCredentials: UserCredentials): Promise<string>;
  81. getState(name: string, userCredentials: UserCredentials): Promise<string>;
  82. updateQuery(name: string, query: string, userCredentials: UserCredentials): Promise<void>;
  83. delete(name: string, deleteEmittedStreams: boolean, deleteStateStream: boolean, deleteCheckpointStream: boolean, userCredentials: UserCredentials): Promise<void>;
  84. }
  85. // Expose errors
  86. export class WrongExpectedVersionError {
  87. readonly name: string;
  88. readonly action: string;
  89. readonly message: string;
  90. readonly stream?: string;
  91. readonly expectedVersion?: Long;
  92. readonly transactionId?: Long;
  93. }
  94. export class StreamDeletedError {
  95. readonly message: string;
  96. readonly stream?: string;
  97. readonly transactionId?: Long;
  98. }
  99. export class AccessDeniedError {
  100. readonly name: string;
  101. readonly action: string;
  102. readonly message: string;
  103. readonly stream?: string;
  104. readonly transactionId?: Long;
  105. }
  106. export class ProjectionCommandFailedError {
  107. readonly httpStatusCode: number;
  108. readonly message: string;
  109. }
  110. // Expose enums/constants
  111. export namespace expectedVersion {
  112. const any: number;
  113. const noStream: number;
  114. const emptyStream: number;
  115. const streamExists: number;
  116. }
  117. export namespace positions {
  118. const start: Position;
  119. const end: Position;
  120. }
  121. export namespace streamPosition {
  122. const start: number;
  123. const end: number;
  124. }
  125. //TODO
  126. // systemMetadata
  127. // eventReadStatus
  128. // sliceReadStatus
  129. // Expose loggers
  130. export interface Logger {
  131. debug(fmt: string, ...args: any[]): void;
  132. info(fmt: string, ...args: any[]): void;
  133. error(fmt: string, ...args: any[]): void;
  134. }
  135. export class NoopLogger implements Logger {
  136. constructor()
  137. debug(fmt: string, ...args: any[]): void;
  138. info(fmt: string, ...args: any[]): void;
  139. error(fmt: string, ...args: any[]): void;
  140. }
  141. export class FileLogger implements Logger {
  142. constructor(filePath: string, append: boolean);
  143. debug(fmt: string, ...args: any[]): void;
  144. info(fmt: string, ...args: any[]): void;
  145. error(fmt: string, ...args: any[]): void;
  146. }
  147. // Expose results
  148. export interface WriteResult {
  149. readonly nextExpectedVersion: Long;
  150. readonly logPosition: Position;
  151. }
  152. export interface RecordedEvent {
  153. readonly eventStreamId: string;
  154. readonly eventId: string;
  155. readonly eventNumber: Long;
  156. readonly eventType: string;
  157. readonly created: Date;
  158. readonly createdEpoch: number;
  159. readonly data?: Buffer;
  160. readonly metadata?: Buffer;
  161. readonly isJson: boolean;
  162. }
  163. export interface ResolvedEvent {
  164. readonly event?: RecordedEvent;
  165. readonly link?: RecordedEvent;
  166. readonly originalEvent?: RecordedEvent;
  167. readonly isResolved: boolean;
  168. readonly originalPosition?: Position;
  169. readonly originalStreamId: string;
  170. readonly originalEventNumber: Long;
  171. }
  172. export interface StreamEventsSlice {
  173. readonly status: string; // TODO: enum
  174. readonly stream: string;
  175. readonly fromEventNumber: Long;
  176. readonly readDirection: string; // TODO: enum
  177. readonly events: ResolvedEvent[];
  178. readonly nextEventNumber: Long;
  179. readonly lastEventNumber: Long;
  180. readonly isEndOfStream: boolean;
  181. }
  182. export interface AllEventsSlice {
  183. readonly readDirection: string; // TODO enum
  184. readonly fromPosition: Position;
  185. readonly nextPosition: Position;
  186. readonly events: ResolvedEvent[];
  187. readonly isEndOfStream: boolean;
  188. }
  189. export interface DeleteResult {
  190. readonly logPosition: Position;
  191. }
  192. export interface EventStoreTransaction {
  193. readonly transactionId: number;
  194. commit(): Promise<WriteResult>;
  195. write(eventOrEvents: EventData | EventData[]): Promise<void>;
  196. rollback(): void;
  197. }
  198. export interface EventReadResult {
  199. readonly status: string;
  200. readonly stream: string;
  201. readonly eventNumber: Long;
  202. readonly event: ResolvedEvent | null;
  203. }
  204. export interface EventStoreSubscription {
  205. readonly isSubscribedToAll: boolean;
  206. readonly streamId: string;
  207. readonly lastCommitPosition: Position;
  208. readonly lastEventNumber: Long;
  209. close(): void;
  210. unsubscribe(): void;
  211. }
  212. export interface EventStoreCatchUpSubscription {
  213. stop(): void;
  214. }
  215. export enum PersistentSubscriptionNakEventAction {
  216. Unknown = 0,
  217. Park = 1,
  218. Retry = 2,
  219. Skip = 3,
  220. Stop = 4
  221. }
  222. export interface EventStorePersistentSubscription {
  223. acknowledge(events: ResolvedEvent | ResolvedEvent[]): void;
  224. fail(events: ResolvedEvent | ResolvedEvent[], action: PersistentSubscriptionNakEventAction, reason: string): void;
  225. stop(): void;
  226. }
  227. export interface RawStreamMetadataResult {
  228. readonly stream: string;
  229. readonly isStreamDeleted: boolean;
  230. readonly metastreamVersion: Long;
  231. readonly streamMetadata: any;
  232. }
  233. export interface PersistentSubscriptionCreateResult {
  234. readonly status: string;
  235. }
  236. export interface PersistentSubscriptionUpdateResult {
  237. readonly status: string;
  238. }
  239. export interface PersistentSubscriptionDeleteResult {
  240. readonly status: string;
  241. }
  242. // Callbacks
  243. export interface EventAppearedCallback<TSubscription> {
  244. (subscription: TSubscription, event: ResolvedEvent): void | Promise<void>;
  245. }
  246. export interface LiveProcessingStartedCallback {
  247. (subscription: EventStoreCatchUpSubscription): void;
  248. }
  249. export interface SubscriptionDroppedCallback<TSubscription> {
  250. (subscription: TSubscription, reason: string, error?: Error): void;
  251. }
  252. export interface TcpEndPoint {
  253. port: number;
  254. host: string;
  255. }
  256. export interface HeartbeatInfo {
  257. readonly connectionId: string;
  258. readonly remoteEndPoint: TcpEndPoint;
  259. readonly requestSentAt: number;
  260. readonly requestPkgNumber: number;
  261. readonly responseReceivedAt: number;
  262. readonly responsePkgNumber: number;
  263. }
  264. export interface EventData {
  265. readonly eventId: string;
  266. readonly type: string;
  267. readonly isJson: boolean;
  268. readonly data: Buffer;
  269. readonly metadata: Buffer;
  270. }
  271. interface EventStoreNodeConnectionEvents {
  272. connected: TcpEndPoint;
  273. disconnected: TcpEndPoint;
  274. reconnecting: void;
  275. closed:string;
  276. error: Error;
  277. heartbeatInfo: HeartbeatInfo;
  278. }
  279. type EventStoreNodeConnectionEventEmitter = StrictEventEmitter<EventEmitter, EventStoreNodeConnectionEvents>;
  280. export class EventStoreNodeConnection extends (EventEmitter as { new(): EventStoreNodeConnectionEventEmitter }) {
  281. connect(): Promise<void>;
  282. close(): void;
  283. // write actions
  284. deleteStream(stream: string, expectedVersion: Long|number, hardDelete?: boolean, userCredentials?: UserCredentials): Promise<DeleteResult>;
  285. appendToStream(stream: string, expectedVersion: Long|number, eventOrEvents: EventData | EventData[], userCredentials?: UserCredentials): Promise<WriteResult>;
  286. startTransaction(stream: string, expectedVersion: Long|number, userCredentials?: UserCredentials): Promise<EventStoreTransaction>;
  287. continueTransaction(transactionId: number, userCredentials?: UserCredentials): EventStoreTransaction;
  288. // read actions
  289. readEvent(stream: string, eventNumber: Long|number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<EventReadResult>;
  290. readStreamEventsForward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
  291. readStreamEventsBackward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
  292. readAllEventsForward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
  293. readAllEventsBackward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
  294. // subscription actions
  295. subscribeToStream(stream: string, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreSubscription>, userCredentials?: UserCredentials): Promise<EventStoreSubscription>;
  296. subscribeToStreamFrom(stream: string, lastCheckpoint: Long|number|null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
  297. subscribeToAll(resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreSubscription>, userCredentials?: UserCredentials): Promise<EventStoreSubscription>;
  298. subscribeToAllFrom(lastCheckpoint: Position|null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback<EventStoreCatchUpSubscription>, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback<EventStoreCatchUpSubscription>, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription;
  299. // persistent subscriptions
  300. createPersistentSubscription(stream: string, groupName: string, settings: PersistentSubscriptionSettings, userCredentials?: UserCredentials): Promise<PersistentSubscriptionCreateResult>;
  301. updatePersistentSubscription(stream: string, groupName: string, settings: PersistentSubscriptionSettings, userCredentials?: UserCredentials): Promise<PersistentSubscriptionUpdateResult>;
  302. deletePersistentSubscription(stream: string, groupName: string, userCredentials?: UserCredentials): Promise<PersistentSubscriptionDeleteResult>
  303. connectToPersistentSubscription(stream: string, groupName: string, eventAppeared: EventAppearedCallback<EventStorePersistentSubscription>, subscriptionDropped?: SubscriptionDroppedCallback<EventStorePersistentSubscription>, userCredentials?: UserCredentials, bufferSize?: number, autoAck?: boolean): Promise<EventStorePersistentSubscription>;
  304. // metadata actions
  305. setStreamMetadataRaw(stream: string, expectedMetastreamVersion: Long|number, metadata: any, userCredentials?: UserCredentials): Promise<WriteResult>;
  306. getStreamMetadataRaw(stream: string, userCredentials?: UserCredentials): Promise<RawStreamMetadataResult>;
  307. }
  308. // Expose helper functions
  309. export interface ConnectionSettings {
  310. log?: Logger,
  311. verboseLogging?: boolean,
  312. maxQueueSize?: number,
  313. maxConcurrentItems?: number,
  314. maxRetries?: number,
  315. maxReconnections?: number,
  316. requireMaster?: boolean,
  317. reconnectionDelay?: number,
  318. operationTimeout?: number,
  319. operationTimeoutCheckPeriod?: number,
  320. defaultUserCredentials?: UserCredentials,
  321. useSslConnection?: boolean,
  322. targetHost?: TcpEndPoint,
  323. validateServer?: boolean,
  324. failOnNoServerResponse?: boolean,
  325. heartbeatInterval?: number,
  326. heartbeatTimeout?: number,
  327. clientConnectionTimeout?: number,
  328. // Cluster Settings
  329. clusterDns?: string,
  330. maxDiscoverAttempts?: number,
  331. discoverDelay?: number,
  332. externalGossipPort?: number,
  333. gossipTimeout?: number
  334. }
  335. // Expose Helper functions
  336. export function createConnection(settings: ConnectionSettings, endPointOrGossipSeed: string | TcpEndPoint | GossipSeed[], connectionName?: string): EventStoreNodeConnection;
  337. export function createJsonEventData(eventId: string, event: any, metadata?: any, type?: string): EventData;
  338. export function createEventData(eventId: string, type: string, isJson: boolean, data: Buffer, metadata?: Buffer): EventData;