UNPKG

fixparser

Version:

FIX.Latest / 5.0 SP2 Parser

210 lines (209 loc) 7.89 kB
import { type IMessageStore, MessageBuffer } from 'fixparser-common'; import type { ProxyAgent } from 'proxy-agent'; import { type BaseOptions, type ConnectionType, FIXParserBase, type Options as FIXParserOptions, 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'; export type Options = Pick< FIXParserOptions, | 'host' | 'port' | 'sender' | 'target' | 'heartbeatIntervalSeconds' | 'fixVersion' | 'messageStoreIn' | 'messageStoreOut' | 'logging' | 'logOptions' | 'onMessage' | 'onOpen' | 'onError' | 'onClose' | 'onReady' >; declare class FIXParserBrowser implements IFIXParser { static readonly version: Version; readonly parserName: Parser; readonly fixParserBase: FIXParserBase; heartBeatIntervalId: ReturnType<typeof setInterval> | undefined; socket: WebSocket | 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>; connectionType: ConnectionType; logger: Logger; logging: boolean; logOptions: LogOptions | undefined; proxy?: ProxyAgent; 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 FIX server using the specified options. The connection * is made via the WebSocket protocol, and various callbacks can be provided to handle * events like message reception, connection opening, errors, and closing. * * - Sets up logging with optional configurations. * - Validates the license before proceeding with the connection. * - Initializes connection parameters including sender, target, and heartbeat interval. * - Establishes the connection over WebSocket by calling `connectWebsocket()`. * * @param {Options} [options={}] - The configuration options for the connection. * @param {string} [options.host='localhost'] - The host address of the FIX server. * @param {number} [options.port=9878] - The port number for the FIX server. * @param {string} [options.sender='SENDER'] - The sender identifier for the connection. * @param {string} [options.target='TARGET'] - The target identifier for the connection. * @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 for handling incoming messages. * @param {function} [options.onOpen=this.onOpenCallback] - Callback for handling connection open event. * @param {function} [options.onError=this.onErrorCallback] - Callback for handling errors. * @param {function} [options.onClose=this.onCloseCallback] - Callback for handling connection close event. * @param {function} [options.onReady=this.onReadyCallback] - Callback for handling ready event. * * @returns {void} */ connect(options?: Options): void; /** * Establishes a WebSocket connection to the FIX server. This method configures event listeners * for WebSocket events such as connection opening, closing, message reception, and logging the events. * * - Builds the WebSocket URL based on the provided host and port. * - Sets up event listeners for `open`, `close`, and `message` events. * - Logs connection status and relevant details such as `readyState` during connection and closure. * - On receiving a message, it is parsed using `fixParserBase.parse()`, and each parsed message is processed and passed to the callback functions. * - The `onOpenCallback`, `onCloseCallback`, `onMessageCallback` methods are invoked when corresponding events occur. * - The heartbeat mechanism is stopped when the connection closes. * * @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 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 { Logger } from './logger/Logger'; export { LicenseManager } from './licensemanager/LicenseManager'; export { MessageBuffer }; export { Constants }; export { Field }; export { Message }; export { FIXParserBrowser as 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, BaseOptions } from './FIXParserBase'; export type { MessageError } from './message/Message';