fixparser
Version:
FIX.Latest / 5.0 SP2 Parser
252 lines (251 loc) • 11.2 kB
TypeScript
import { Socket } from 'node:net';
import { type ConnectionOptions, type TLSSocket } from 'node:tls';
import { WebSocket } from 'ws';
import { type IMessageStore, MessageBuffer } from 'fixparser-common';
import type { ProxyAgent } from 'proxy-agent';
import { type BaseOptions, type ConnectionType, FIXParserBase, type Options, type 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';
/**
* FIXParser class.
* @public
*/
declare class FIXParser implements IFIXParser {
static readonly version: Version;
readonly parserName: Parser;
readonly fixParserBase: FIXParserBase;
heartBeatIntervalId: ReturnType<typeof setInterval> | undefined;
socket: Socket | WebSocket | TLSSocket | undefined;
connected: boolean;
host: string | undefined;
port: number | undefined;
protocol: Protocol | undefined;
sender: string | undefined;
target: string | undefined;
heartBeatInterval: number;
fixVersion: string;
messageStoreIn: IMessageStore<Message>;
messageStoreOut: IMessageStore<Message>;
requestedLogout: boolean;
connectionType: ConnectionType;
logger: Logger;
logging: boolean;
logOptions: LogOptions | undefined;
proxy?: ProxyAgent;
tlsOptions?: ConnectionOptions;
tlsSkipStdInPipe?: boolean;
/**
* Constructor for initializing the FIXParser 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 - A Message class instance
*/
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 - Error object
*/
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;
/**
* Establishes a connection to a specified host and port using the given options and protocol type.
* The method supports multiple connection protocols, including TCP, SSL/TCP, and WebSocket. It handles the
* initialization of connection parameters, logging, and license validation before attempting to connect.
*
* - **TCP** (`tcp`): Connects using a basic TCP connection.
* - **Secure TCP** (`ssl-tcp`, `tls-tcp`): Connects using a secure TLS/SSL connection.
* - **WebSocket** (`websocket`): Establishes a WebSocket connection.
*
* The connection will invoke the appropriate callback functions for each connection event:
* - `onMessage`: Called when a new message is received.
* - `onOpen`: Called when the connection is successfully established.
* - `onError`: Called when an error occurs.
* - `onClose`: Called when the connection is closed.
* - `onReady`: Called after a short delay once the connection is ready.
*
* @param {Options} [options={}] - Connection options for setting up the connection.
* @param {string} [options.host='localhost'] - Host to connect to.
* @param {number} [options.port=9878] - Port to connect to.
* @param {string} [options.protocol='tcp'] - Protocol to use for the connection ('tcp', 'ssl-tcp', 'tls-tcp', 'websocket').
* @param {string} [options.sender='SENDER'] - Sender ID for the FIX connection.
* @param {string} [options.target='TARGET'] - Target ID for the FIX connection.
* @param {number} [options.heartbeatIntervalSeconds=DEFAULT_HEARTBEAT_SECONDS] - Interval in seconds for sending heartbeat messages.
* @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 {string | Buffer | undefined} [options.tlsKey] - Optional TLS private key for secure connections.
* @param {string | Buffer | undefined} [options.tlsCert] - Optional TLS certificate for secure connections.
* @param {boolean} [options.tlsUseSNI=false] - Whether to use Server Name Indication (SNI) for secure connections.
* @param {boolean} [options.tlsSkipStdInPipe=false] - Whether to skip piping stdin for secure connections.
* @param {any} [options.proxy=null] - Optional proxy configuration for WebSocket connections.
* @param {Function} [options.onMessage=this.onMessageCallback] - Callback function to handle incoming messages.
* @param {Function} [options.onOpen=this.onOpenCallback] - Callback function to handle connection opening.
* @param {Function} [options.onError=this.onErrorCallback] - Callback function to handle errors.
* @param {Function} [options.onClose=this.onCloseCallback] - Callback function to handle connection closing.
* @param {Function} [options.onReady=this.onReadyCallback] - Callback function to handle connection readiness.
*
* @returns {void}
*/
connect(options?: Options): void;
/**
* Establishes a TCP connection to the specified host and port, sets up the socket for communication,
* and handles various socket events such as 'data', 'close', 'ready', 'timeout', and 'error'.
*
* - Upon successful connection, the socket is ready to receive and process messages.
* - Incoming messages are parsed and processed through a message buffer and the provided callback.
* - Handles error scenarios such as timeout and connection errors, with appropriate cleanup and notifications.
*
* @returns {void}
*/
private connectTcp;
/**
* Establishes a secure TCP (TLS) connection to the specified host and port with optional TLS certificates and keys.
* Configures the socket for secure communication and handles various socket events such as 'data', 'close', 'timeout', and 'error'.
*
* - Supports server-side SSL/TLS authentication with optional key and certificate.
* - Enables Server Name Indication (SNI) for secure connections if `tlsUseSNI` is true.
* - Handles input piping from `stdin` if `tlsSkipStdInPipe` is false.
* - Provides error handling for connection and timeouts, with appropriate cleanup and notifications.
*
* @returns {void}
*/
private connectTlsTcp;
/**
* Establishes a WebSocket connection to the specified host and port, with optional proxy support.
* Configures the socket for communication and handles various WebSocket events such as 'message', 'open', and 'close'.
*
* - If the connection string does not include `ws://` or `wss://`, it prepends `ws://` to the host and port.
* - Optionally uses a proxy if `proxy` is provided.
* - Handles incoming messages, processes them, and invokes appropriate callbacks.
* - Handles WebSocket open and close events, managing connection state and triggering callbacks.
*
* @returns {void}
*/
private connectWebsocket;
/**
* 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 the next incoming message sequence number.
*
* @returns The next outgoing message sequence number.
*/
getNextSenderMsgSeqNum(): number;
/**
* Set the next incoming message sequence number.
*
* @param nextMsgSeqNum - The next message sequence number.
* @returns The next incoming message sequence number.
*/
setNextSenderMsgSeqNum(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;
/**
* Close current connection.
*/
close(): 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 { ConnectionOptions };
export { Logger } from './logger/Logger';
export { LicenseManager } from './licensemanager/LicenseManager';
export { MessageBuffer };
export { Constants };
export { Field };
export { Message };
export { FIXParser };
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';