UNPKG

@iprokit/service

Version:

Powering distributed systems with simplicity and speed.

134 lines (133 loc) 3.98 kB
/** * @iProKit/Service * Copyright (c) 2019-2025 Rutvik Katuri / iProTechs * SPDX-License-Identifier: Apache-2.0 */ /// <reference types="node" /> /// <reference types="node" /> /// <reference types="node" /> import EventEmitter from 'events'; import { Tags } from './signal'; import { Incoming, Outgoing } from './protocol'; /** * `Coordinator` manages the life cycle of multiple `Conductor` instances and coordinates signals. */ export default class Coordinator { /** * Conductors registered. */ readonly conductors: Array<Conductor>; /** * Creates an instance of `Coordinator`. */ constructor(); /** * Manges multiple conductors. * * @param conductors conductors to manage. */ manage(...conductors: Array<Conductor>): this; /** * Writes a signal to all the conductors and returns a promise that resolves with the result of the signals. * * @param event name of the signal. * @param tags optional tags of the signal. */ signal(event: string, tags?: Tags): Promise<{ event: string; tags: Tags; }[]>; /** * Ends all the conductors. */ end(): Promise<void>; } /** * `Conductor` manages the flow of `Payload` and `Signal` between incoming and outgoing streams. * * A payload is a unit of data that is wrapped with `SOP` (Start of payload) and `EOP` (End of payload) signals, * referred to as payload signals, which indicate the payload's boundaries. * * @emits `rfi` when RFI is received on the incoming stream. * @emits `signal` when a signal is received on the incoming stream. * @emits `payload` when a payload is received on the incoming stream. * @emits `end` when end is received on the incoming stream. */ export declare class Conductor extends EventEmitter { /** * Incoming stream to read. */ readonly incoming: Incoming; /** * Outgoing stream to write. */ readonly outgoing: Outgoing; /** * Creates an instance of `Conductor`. * * @param incoming incoming stream to read. * @param outgoing outgoing stream to write. */ constructor(incoming: Incoming, outgoing: Outgoing); /** * Asynchronous iterator. * Reads a `Payload` from the incoming stream. */ [Symbol.asyncIterator](): AsyncGenerator<string | Buffer, void, unknown>; /** * Reads a `Payload` from the incoming stream. * * NOTE: When `END` payload signal is encountered, control is passed to `readSignal`. * * @yields data chunk of the payload received on the incoming stream. */ private readPayload; /** * Reads a `Signal` from the incoming stream. * * NOTE: When `START` payload signal is encountered, control is passed to `readPayload`. * * @emits `signal` when a signal is received on the incoming stream. * @emits `payload` when a payload is received on the incoming stream. */ private readSignal; /** * Writes a `Payload` to the outgoing stream. * * @param chunk data chunk of the payload. */ deliver(chunk: string | Buffer): Promise<void>; /** * Writes a `Signal` to the outgoing stream. * * @param event name of the signal. * @param tags optional tags of the signal. */ signal(event: string, tags?: Tags): Promise<void>; /** * Flushes the stream by writing the RFI frame and an empty data frame into the outgoing stream. */ flush(): Promise<void>; /** * Writes data to the outgoing stream. * * @param chunk chunk to write. */ private write; /** * Ends the outgoing stream. */ end(): Promise<void>; /** * Destroy both the incoming and outgoing streams. */ destroy(): void; /** * Indicates start of payload. */ static readonly START = "SOP"; /** * Indicates end of payload. */ static readonly END = "EOP"; }