Home Reference Source Test
public class | source

TCPConnection

Extends:

events~EventEmitter → TCPConnection

Raw tcp communication to eventstore This class handles basic communication with eventstore

Constructor Summary

Public Constructor
public

Creates an instance of TCPConnection.

Member Summary

Public Members
public get

Returns true if connected to eventstore otherwise false

public

logger instance

Protected Members
protected

config after discovery process

protected

connection id

protected

initial config

protected

indicates if connection close is wanted by user or not

protected

message length of tcp data

protected

message offset of tcp data

protected

messageData: Buffer | null

message buffer of tcp data

protected

list of pending requests

protected

list of persistent subscriptions

protected

counter for re-connections

protected

socket: SOMEUNKNOWN | SOMEUNKNOWN

tcp socket

protected

state: connectionState

connection state

protected

list of subscriptions

protected

timeoutInterval: null | SOMEUNKNOWN

timeout interval for timed out pending requests

Method Summary

Public Methods
public

async connect(): Promise

Called to connect to eventstore

public

async connectToPersistentSubscription(subscription: PersistentSubscription, allowedInFlightMessages: number, credentials: UserCredentials | null): Promise

Connects to persistent subscription

public

async disconnect(): Promise

Disconnect from eventstore.

public

sendCommand(correlationId: , command: EventstoreCommand, data: Buffer | null, credentials: UserCredentials | null, promise: null): void

Creates and sends raw data message to eventstore and adds given promise to pending queue

public

subscribeToStream(stream: Stream, resolveLinkTos: boolean, credentials: UserCredentials | null): Promise

Subscribes to stream

public

async unsubscribeFromPersistentSubscription(subscriptionId: , credentials: UserCredentials | null): Promise

Stop listening on persistent subscription

public

async unsubscribeFromStream(subscriptionId: ): Promise

Unsubscribes from stream

Protected Methods
protected

checkOperationResult(correlationId: , result: , message: string): boolean

CHecks if given result is an error code It returns true for successful result otherwise it returns false.

protected

checkTimeout(): void

Called by interval function to check if there are some pending requests which should be rejected with time out error

protected

handleCreatePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void

Handle response for command CreatePersistentSubscription

protected

handleDeletePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void

Handle response for command DeletePersistentSubscription

protected

handleDeleteStreamCompleted(correlationId: , payload: Buffer): void

Handle response for command DeleteStreamCompleted

protected

handleMultiPacketResponseData(data: Buffer): Buffer | null

This function handles raw buffer responses received within multiple tcp data package

protected

handleNewResponseData(data: Buffer): Buffer | null

Gets called as soon as new data over tcp connection arrives as raw buffer data Checks if

  • new received data is part of previously received data
  • new data contains multiple responses
  • new data is single response
protected

handlePersistentSubscriptionConfirmation(correlationId: , payload: Buffer): void

Handles persistent subscription confirmation

protected

handlePersistentSubscriptionStreamEventAppeared(correlationId: , payload: Buffer): void

Handles persistent subscription stream event appeared

protected

handleReadAllEventsCompleted(correlationId: , payload: Buffer): void

Handle response for command ReadAllEvents

protected

handleReadEventCompleted(correlationId: , payload: Buffer): void

Handle response for command ReadEvent

protected

handleReadStreamEventsCompleted(correlationId: , payload: Buffer): void

Handle response for command ReadStreamEvents

protected

handleSingleResponseData(data: Buffer): void

This function handles a single raw buffer response

protected

handleStreamEventAppeared(correlationId: , payload: Buffer): void

Handle incoming event for subscription

protected

handleSubscriptionConfirmation(correlationId: , payload: Buffer): void

Handle response for command Subscription

protected

handleSubscriptionDropped(correlationId: , payload: Buffer): void

Handle subscription drop

protected

handleTransactionCommitCompleted(correlationId: , payload: Buffer): void

Handle response for command TransactionCommit

protected

handleTransactionStartCompleted(correlationId: , payload: Buffer): void

Handle response for command TransactionStart

protected

handleTransactionWriteCompleted(correlationId: , payload: Buffer): void

Handles transaction write completed

protected

handleUpdatePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void

Handles update persistent subscription completed

protected

handleWriteEventsCompleted(correlationId: , payload: Buffer): void

Handles write events completed

protected

onClose(): void

Emit as soon as connection to eventstore is closed

protected

onConnect(): void

Emit as soon as connection to eventstore was established successfully

protected

onData(data: Buffer | null): void

Emitted as soon as data arrives over tcp connection

protected

onDrain(): void

Emit when connection starts draining

protected

onError(err: Error): void

Emit general low level connection errors (communication errors).

protected

onSecureConnect(): void

Emit when connection secured

protected

rejectCommandPromise(correlationId: , error: ): void

Will be called if a command send to eventstore was replied with an error In this case corresponding promise will be rejected and removed from queue

protected

resolveCommandPromise(correlationId: , result: null | T): void

Will be called if a command send to eventstore was replied with success response In this case corresponding promise will be resolved with result received from eventstore

protected

Connect to eventstore

Public Constructors

public constructor() source

Creates an instance of TCPConnection.

Public Members

public get isConnected: boolean source

Returns true if connected to eventstore otherwise false

public log: bunyan source

logger instance

Protected Members

protected connectionConfig: EventstoreSettings source

config after discovery process

protected connectionId: string | null source

connection id

protected initialConfig: EventstoreSettings source

initial config

protected isUnexpectedClosed: boolean source

indicates if connection close is wanted by user or not

protected messageCurrentLength: number source

message length of tcp data

protected messageCurrentOffset: number source

message offset of tcp data

protected messageData: Buffer | null source

message buffer of tcp data

protected pendingRequests: Map source

list of pending requests

protected persistentSubscriptionList: Map source

list of persistent subscriptions

protected reconnectCount: number source

counter for re-connections

protected socket: SOMEUNKNOWN | SOMEUNKNOWN source

tcp socket

protected state: connectionState source

connection state

protected subscriptionList: Map source

list of subscriptions

protected timeoutInterval: null | SOMEUNKNOWN source

timeout interval for timed out pending requests

Public Methods

public async connect(): Promise source

Called to connect to eventstore

public async connectToPersistentSubscription(subscription: PersistentSubscription, allowedInFlightMessages: number, credentials: UserCredentials | null): Promise source

Connects to persistent subscription

Params:

NameTypeAttributeDescription
subscription PersistentSubscription
allowedInFlightMessages number
  • optional
  • default: 10
credentials UserCredentials | null

public async disconnect(): Promise source

Disconnect from eventstore. It tries to drain pending queue to prevent data loose before connection gets closed If disconnect() is call no new outgoing requests accepted

public sendCommand(correlationId: , command: EventstoreCommand, data: Buffer | null, credentials: UserCredentials | null, promise: null): void source

Creates and sends raw data message to eventstore and adds given promise to pending queue

Params:

NameTypeAttributeDescription
correlationId
command EventstoreCommand
data Buffer | null
  • optional
  • default: null
credentials UserCredentials | null
  • optional
  • default: null
promise null
  • optional
  • default: null

public subscribeToStream(stream: Stream, resolveLinkTos: boolean, credentials: UserCredentials | null): Promise source

Subscribes to stream

Params:

NameTypeAttributeDescription
stream Stream
resolveLinkTos boolean
  • optional
  • default: true
credentials UserCredentials | null

public async unsubscribeFromPersistentSubscription(subscriptionId: , credentials: UserCredentials | null): Promise source

Stop listening on persistent subscription

Params:

NameTypeAttributeDescription
subscriptionId
credentials UserCredentials | null

public async unsubscribeFromStream(subscriptionId: ): Promise source

Unsubscribes from stream

Params:

NameTypeAttributeDescription
subscriptionId

Protected Methods

protected checkOperationResult(correlationId: , result: , message: string): boolean source

CHecks if given result is an error code It returns true for successful result otherwise it returns false. If result is an error this function rejects corresponding command promise and remove it from command queue

Params:

NameTypeAttributeDescription
correlationId
result
message string
  • optional
  • default:

protected checkTimeout(): void source

Called by interval function to check if there are some pending requests which should be rejected with time out error

protected handleCreatePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void source

Handle response for command CreatePersistentSubscription

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleDeletePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void source

Handle response for command DeletePersistentSubscription

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleDeleteStreamCompleted(correlationId: , payload: Buffer): void source

Handle response for command DeleteStreamCompleted

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleMultiPacketResponseData(data: Buffer): Buffer | null source

This function handles raw buffer responses received within multiple tcp data package

Params:

NameTypeAttributeDescription
data Buffer

protected handleNewResponseData(data: Buffer): Buffer | null source

Gets called as soon as new data over tcp connection arrives as raw buffer data Checks if

  • new received data is part of previously received data
  • new data contains multiple responses
  • new data is single response

Params:

NameTypeAttributeDescription
data Buffer

protected handlePersistentSubscriptionConfirmation(correlationId: , payload: Buffer): void source

Handles persistent subscription confirmation

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handlePersistentSubscriptionStreamEventAppeared(correlationId: , payload: Buffer): void source

Handles persistent subscription stream event appeared

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleReadAllEventsCompleted(correlationId: , payload: Buffer): void source

Handle response for command ReadAllEvents

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleReadEventCompleted(correlationId: , payload: Buffer): void source

Handle response for command ReadEvent

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleReadStreamEventsCompleted(correlationId: , payload: Buffer): void source

Handle response for command ReadStreamEvents

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleSingleResponseData(data: Buffer): void source

This function handles a single raw buffer response

Params:

NameTypeAttributeDescription
data Buffer

protected handleStreamEventAppeared(correlationId: , payload: Buffer): void source

Handle incoming event for subscription

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleSubscriptionConfirmation(correlationId: , payload: Buffer): void source

Handle response for command Subscription

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleSubscriptionDropped(correlationId: , payload: Buffer): void source

Handle subscription drop

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleTransactionCommitCompleted(correlationId: , payload: Buffer): void source

Handle response for command TransactionCommit

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleTransactionStartCompleted(correlationId: , payload: Buffer): void source

Handle response for command TransactionStart

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleTransactionWriteCompleted(correlationId: , payload: Buffer): void source

Handles transaction write completed

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleUpdatePersistentSubscriptionCompleted(correlationId: , payload: Buffer): void source

Handles update persistent subscription completed

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected handleWriteEventsCompleted(correlationId: , payload: Buffer): void source

Handles write events completed

Params:

NameTypeAttributeDescription
correlationId
payload Buffer

protected onClose(): void source

Emit as soon as connection to eventstore is closed

protected onConnect(): void source

Emit as soon as connection to eventstore was established successfully

protected onData(data: Buffer | null): void source

Emitted as soon as data arrives over tcp connection

Params:

NameTypeAttributeDescription
data Buffer | null

protected onDrain(): void source

Emit when connection starts draining

protected onError(err: Error): void source

Emit general low level connection errors (communication errors). Will not emit errors on business level

Params:

NameTypeAttributeDescription
err Error

protected onSecureConnect(): void source

Emit when connection secured

protected rejectCommandPromise(correlationId: , error: ): void source

Will be called if a command send to eventstore was replied with an error In this case corresponding promise will be rejected and removed from queue

Params:

NameTypeAttributeDescription
correlationId
error

protected resolveCommandPromise(correlationId: , result: null | T): void source

Will be called if a command send to eventstore was replied with success response In this case corresponding promise will be resolved with result received from eventstore

Params:

NameTypeAttributeDescription
correlationId
result null | T
  • optional
  • default: null

protected async tryToConnect(): Promise source

Connect to eventstore