UNPKG

fixparser

Version:

FIX.Latest / 5.0 SP2 Parser

292 lines (291 loc) 11.8 kB
import { type Server, type Socket } from 'node:net'; import { type TlsOptions } from 'node:tls'; import { type WebSocket, WebSocketServer } from 'ws'; import { type IMessageStore, MessageBuffer } from 'fixparser-common'; import { FIXParser } from './FIXParser'; import type { BaseOptions, ConnectionType, Options as FIXParserOptions, Protocol } from './FIXParserBase'; import type { IFIXParser } from './IFIXParser'; import { Field } from './fields/Field'; import * as Constants from './fieldtypes'; import { type LogOptions, Logger } from './logger/Logger'; import { Message } from './message/Message'; import { type Parser, type Version } from './util/util'; type Options = Pick< FIXParserOptions, | 'host' | 'port' | 'protocol' | 'sender' | 'target' | 'heartbeatIntervalSeconds' | 'fixVersion' | 'messageStoreIn' | 'messageStoreOut' | 'logging' | 'logOptions' | 'tlsOptions' | 'tlsSkipStdInPipe' | 'onMessage' | 'onOpen' | 'onError' | 'onClose' | 'onReady' >; declare class FIXServer implements IFIXParser { static readonly version: Version; readonly parserName: Parser; readonly fixParser: FIXParser; heartBeatIntervalId: ReturnType<typeof setInterval> | undefined; socket: WebSocket | Socket | undefined; connected: boolean; host: string; port: number; protocol: Protocol; sender: string; target: string; heartBeatInterval: number; fixVersion: string; messageStoreIn: IMessageStore<Message>; messageStoreOut: IMessageStore<Message>; requestedLogout: boolean; connectionType: ConnectionType; logger: Logger; logging: boolean; logOptions: LogOptions | undefined; tlsOptions?: TlsOptions; tlsSkipStdInPipe?: boolean; server: Server | WebSocketServer | undefined; isLoggedIn: boolean; messageCounter: number; /** * Constructor for initializing an instance with the provided options. * * @param {BaseOptions} [options={ logging: true, logOptions: undefined, fixVersion: DEFAULT_FIX_VERSION, skipValidation: false }] - The options to configure the instance. * @param {boolean} [options.logging=true] - Whether logging is enabled (defaults to `true`). * @param {LogOptions} [options.logOptions=undefined] - Options to customize logging behavior. * @param {string} [options.fixVersion=DEFAULT_FIX_VERSION] - The FIX protocol version to use (defaults to `DEFAULT_FIX_VERSION`). * @param {boolean} [options.skipValidation=false] - Whether to skip validation of FIX messages (defaults to `false`). */ constructor(options?: BaseOptions); /** * onMessageCallback is called when a message has been received * * @remarks FIXParser~onMessageCallback * @param message */ private onMessageCallback; /** * onOpenCallback is called the FIX connection has been initiated * * @remarks FIXParser~onOpenCallback */ private onOpenCallback; /** * onErrorCallback is called the FIX connection failed * * @remarks FIXParser~onErrorCallback * @param error */ private onErrorCallback; /** * onCloseCallback is called the FIX connection has been closed * * @remarks FIXParser~onCloseCallback */ private onCloseCallback; /** * onReadyCallback is called the FIX connection is opened and ready * * @remarks FIXParser~onReadyCallback */ private onReadyCallback; /** * Creates a FIX server that listens for incoming connections. This method sets up * various configurations, including logging, connection options, and callbacks for * events like message reception, connection opening, error handling, and closure. * * - Initializes logging based on the provided options. * - Validates the license before proceeding with server creation. * - Sets up connection parameters such as host, port, sender, and target identifiers. * - Sets the heartbeat interval and FIX version. * - Configures callback functions for handling messages, connection events, and errors. * - Calls the `initialize()` method to begin server operation. * * @param {Options} [options={}] - The configuration options for the server. * @param {string} [options.host=this.host] - The host address the server will bind to. * @param {number} [options.port=this.port] - The port number the server will listen on. * @param {string} [options.protocol=this.protocol] - The protocol to be used ('tcp', 'ssl-tcp', 'websocket', etc.). * @param {string} [options.sender=this.sender] - The sender identifier for the server. * @param {string} [options.target=this.target] - The target identifier for the server. * @param {number} [options.heartbeatIntervalSeconds=DEFAULT_HEARTBEAT_SECONDS] - The heartbeat interval in seconds. * @param {string} [options.messageStoreIn=new MessageBuffer()] - Optional custom message buffer for incoming messages. * @param {string} [options.messageStoreOut=new MessageBuffer()] - Optional custom message buffer for outgoing messages. * @param {function} [options.onMessage=this.onMessageCallback] - Callback function for handling incoming messages. * @param {function} [options.onOpen=this.onOpenCallback] - Callback function for handling connection open event. * @param {function} [options.onError=this.onErrorCallback] - Callback function for handling errors. * @param {function} [options.onClose=this.onCloseCallback] - Callback function for handling connection close event. * @param {function} [options.onReady=this.onReadyCallback] - Callback function for handling the ready event. * * @returns {void} */ createServer(options?: Options): void; /** * Initializes the server by setting up the appropriate connection based on the specified protocol. * * - This method sets the `messageCounter` to zero, which tracks the number of messages sent. * - Depending on the `protocol` (either 'tcp' or 'websocket'), it calls the appropriate server creation method. * - If the protocol is invalid, an error is logged indicating the issue. * * @returns {void} */ private initialize; /** * Creates and starts a TCP server to handle incoming connections. * The server listens for incoming client connections on the specified `host` and `port`. * For each connection, it sets up socket event listeners to manage data transmission, * connection lifecycle (open, close, error), and communication processing. * * This method is used to create a standard TCP server and is invoked when the `protocol` * is set to `'tcp'`. * * @returns {void} * * @throws {Error} Will throw an error if the server fails to start or if there is a problem * with socket connection handling. */ private setupTcpServer; /** * Sets up and starts a TCP/TLS server to handle incoming connections. * The server is created using the `createTcpTlsServer` function and listens for TLS-TCP * connections based on the provided `tlsOptions`. The method sets up the * necessary event listeners to handle connection events, such as data reception, * connection establishment, and errors. * * @returns {void} * * @throws {Error} Will throw an error if the server fails to start or if there is an issue * with the socket connection or TLS setup. */ private setupTcpTlsServer; /** * Sets up event listeners and data handling for the given socket. * This function is used for both TCP and TLS-TCP connections to handle * incoming data, connection lifecycle events (open, close, timeout, error), * and manages communication state (such as heartbeat, message buffer, etc.). * * @param {Socket | TLSSocket} socket - The socket object representing the client connection. * This can be a regular TCP socket (`net.Socket`) or a TLS socket (`TLSSocket`). * * @returns {void} * * @throws {Error} Will throw an error if there is an issue with socket events or data processing. */ private setupSocket; /** * Creates a WebSocket server that listens for incoming connections and handles communication. * * - Sets up a WebSocket server with specified `host` and `port`. * - Handles various WebSocket events: 'connection', 'message', 'close', 'error', and 'listening'. * - Logs connection status, errors, and message reception using the logger. * - Processes incoming messages and invokes the appropriate callbacks for message handling, connection open/close, and errors. * * @returns {void} */ private createWebsocketServer; /** * Get the next outgoing message sequence number. * * @returns The next outgoing message sequence number. */ getNextTargetMsgSeqNum(): number; /** * Set the next outgoing message sequence number. * * @param nextMsgSeqNum - The next message sequence number. * @returns The next outgoing message sequence number. */ setNextTargetMsgSeqNum(nextMsgSeqNum: number): number; /** * Get current timestamp. * * @param dateObject - An instance of a Date class. * @returns The current timestamp. */ getTimestamp(dateObject?: Date): string; /** * Create an instance of a FIX Message. * * @param fields - An array of Fields. * @returns A FIX Message class instance. */ createMessage(...fields: Field[]): Message; /** * Parse a FIX message string into Message instance(s). * * @param data - FIX message string. * @returns FIX Message class instance(s). */ parse(data: string): Message[]; /** * Send a FIX message. * * @param message - FIX Message class instance. */ send(message: Message): void; /** * Get connection status. * * @returns Current connection status. */ isConnected(): boolean; /** * Resets the current session. */ private resetSession; /** * Close current connection. */ close(): void; /** * Destroys the server and closes the connection. * * - Stops the heartbeat mechanism to prevent any ongoing activity. * - If the protocol is TCP, it destroys the socket and closes the server. * - If the protocol is WebSocket, it closes all client connections and shuts down the server. * - Logs the server destruction event. * * @returns {void} */ destroy(): void; /** * Stop heartbeat interval. */ stopHeartbeat(): void; /** * Restart heartbeat interval. */ restartHeartbeat(): void; /** * Start heartbeat interval. * * @param heartBeatInterval - Heartbeat interval in seconds. * @param disableLog - Whether to disable heartbeat logs. */ startHeartbeat(heartBeatInterval?: number, disableLog?: boolean): void; } export * from './fieldtypes'; export type { TlsOptions } from 'node:tls'; export { Logger } from './logger/Logger'; export { LicenseManager } from './licensemanager/LicenseManager'; export { MessageBuffer }; export { Constants }; export { Field }; export { Message }; export { FIXServer }; export type { IFIXParser }; export type { LogMessage, Level, ILogTransporter } from 'fixparser-common'; export type { FIXValue } from './util/util'; export type { LogOptions, Format } from './logger/Logger'; export type { IMessageStore } from 'fixparser-common'; export type { Protocol, Options, BaseOptions } from './FIXParserBase'; export type { MessageError } from './message/Message';