UNPKG

@imqueue/rpc

Version:

RPC-like client-service implementation over messaging queue

136 lines (135 loc) 4.57 kB
/*! * IMQClient implementation * * I'm Queue Software Project * Copyright (C) 2025 imqueue.com <support@imqueue.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * * If you want to use this code in a closed source (commercial) project, you can * purchase a proprietary commercial license. Please contact us at * <support@imqueue.com> to get commercial licensing options. */ import { JsonObject } from '@imqueue/core'; import { IMQClientOptions, IMQRPCResponse, IMQRPCRequest, IMQDelay, Description } from '.'; import { EventEmitter } from 'events'; /** * Class IMQClient - base abstract class for service clients. */ export declare abstract class IMQClient extends EventEmitter { readonly options: IMQClientOptions; readonly id: number; readonly name: string; readonly hostName: string; readonly serviceName: string; readonly queueName: string; private readonly baseName; private readonly imq; private readonly subscriptionImq; private static singleImq; private readonly logger; private resolvers; /** * Class constructor * * @constructor * @param {Partial<IMQClientOptions>} options * @param {string} serviceName * @param {string} name */ constructor(options?: Partial<IMQClientOptions>, serviceName?: string, name?: string); private createImq; private createSubscriptionImq; /** * Sends call to remote service method * * @access protected * @param {...any[]} args * @template T * @returns {Promise<T>} */ protected remoteCall<T>(...args: any[]): Promise<T>; /** * Adds subscription to service event channel * * @param {(data: JsonObject) => any} handler * @return {Promise<void>} */ subscribe(handler: (data: JsonObject) => any): Promise<void>; /** * Destroys subscription channel to service * * @return {Promise<void>} */ unsubscribe(): Promise<void>; /** * Broadcasts given payload to all other service clients subscribed. * So this is like client-to-clients publishing. * * @param {JsonObject} payload * @return {Promise<void>} */ broadcast(payload: JsonObject): Promise<void>; /** * Initializes client work * * @returns {Promise<void>} */ start(): Promise<void>; /** * Stops client work * * @returns {Promise<void>} */ stop(): Promise<void>; /** * Destroys client * * @returns {Promise<void>} */ destroy(): Promise<void>; /** * Returns service description metadata. * * @param {IMQDelay} delay * @returns {Promise<Description>} */ describe(delay?: IMQDelay): Promise<Description>; /** * Creates client for a service with the given name * * @param {string} name * @param {Partial<IMQServiceOptions>} options * @returns {IMQClient} */ static create(name: string, options?: Partial<IMQClientOptions>): Promise<any>; } /** * Builds and returns call resolver, which supports after call optional hook * * @param {(...args: any[]) => void} resolve - source promise like resolver * @param {IMQRPCRequest} req - request message * @param {IMQClient} client - imq client * @return {(data: any, res: IMQRPCResponse) => void} - hook-supported resolve */ export declare function imqCallResolver(resolve: (data: any) => void, req: IMQRPCRequest, client: IMQClient): (data: any, res: IMQRPCResponse) => void; /** * Builds and returns call rejector, which supports after call optional hook * * @param {(err: any) => void} reject - source promise like rejector * @param {IMQRPCRequest} req - call request * @param {IMQClient} client - imq client * @return {(err: any) => void} - hook-supported reject */ export declare function imqCallRejector(reject: (err: any) => void, req: IMQRPCRequest, client: IMQClient): (err: any, res?: IMQRPCResponse) => void;