import type { RPCClient } from './client.js'; import type { OrphanProbeOptions, RPCError } from './types.js'; /** What a request sets up besides a plain call. */ export type SessionKind = 'stream' | 'pull' | 'pullCallback' | 'callback'; /** A registered method, as registerHandler and services find it on the handler object. */ export type MethodHandler = (...args: any[]) => unknown; /** * The AbortSignal of the stream, pull-iterator, pull-callback or callback * session whose handler or generator runs right now. It aborts when the * session ends: the client cancels, the generator finishes or throws, a * value or frame does not fit the payload limit, a frame does not encode, * the handler is unregistered, the service stops, or the client * disconnects, suspends or loses its connection. A producer that waits for * data can hand it on and stop at once instead of at its next yield. * * Undefined outside a session (plain calls included), where * AsyncLocalStorage is not available (browsers), and where it runs on async * hooks (--no-async-context-frame), since that slows down every await of the * process. * * @example * async *frames(cameraId: string) { * const signal = sessionSignal(); * while (true) yield await nextFrame(cameraId, { signal }); * } */ export declare function sessionSignal(): AbortSignal | undefined; /** * Run `fn` outside of any session, so a connection it opens does not carry * the session of the code that triggered it. */ export declare function outsideSession(fn: () => R): R; /** * The sessions of one handler registration or service. Its stop() reaches * every session, also one whose handler still sets up: that one ends with * CONNECTION_CLOSED once its handler returns. * * Internal: client.ts and service.ts use it. */ export declare class SessionOwner { private stopped; private readonly teardowns; /** * Add the teardown of a session. False once the owner stopped, the session * then ends on its own. */ add(teardown: () => Promise): boolean; delete(teardown: () => Promise): void; /** * End every session. Each stops publishing before this returns, the * promises settle once their generators are closed. */ stop(): Promise[]; } /** Orphan probe settings with the defaults applied. */ export interface ProbeSettings { intervalMs: number; graceMs: number; } /** * The settings of an orphanProbe option, undefined when it turns probing off. * * Internal: client.ts and service.ts use it. */ export declare function probeSettings(option: OrphanProbeOptions | false | undefined): ProbeSettings | undefined; /** * Handle streaming request - common logic for client and service. * * The cancel listener exists before the handler runs. A cancel or a teardown * ends the session at once, also while the producer waits for its next item: * nothing is published after it except the CONNECTION_CLOSED error a teardown * sends, and the generator is closed without waiting for a step in flight. * A handler that fails ends the stream with an error message. * * @param owner - The registration or service the session belongs to */ export declare function handleStreamRequest(handler: Function, args: any[], streamSubject: string, requestId: string, client: RPCClient, owner?: SessionOwner, probeOptions?: ProbeSettings): Promise; /** * The session a request sets up, undefined for a plain call. Pull and * callback params come as an object or wrapped in the args array of call(). * * Internal: client.ts and service.ts use it. */ export declare function sessionKind(params: any): SessionKind | undefined; /** * Open the pull-iterator, pull-callback or callback session a request sets * up and return the result its setup call answers with. * * Internal: client.ts and service.ts use it. */ export declare function openSession(kind: Exclude, handler: MethodHandler, params: any, requestId: string, client: RPCClient, owner?: SessionOwner, probe?: ProbeSettings): Promise<{ iteratorId: string; } | { ok: true; }>; /** * Handle normal RPC call - common logic for client and service */ export declare function handleNormalRPC(handler: Function, params: any): Promise; /** * Invoke the handler of a normal RPC call and return what it returns, a * promise or a plain value. Throws synchronously when the handler does. */ export declare function callNormalRPC(handler: Function, params: any): unknown; /** * Format exception as error object. A no-responder error this client saw * itself becomes INTERNAL_ERROR 'upstream unavailable' with the subject in * `data`: the caller's responder exists, only its own upstream is missing. */ export declare function formatErrorObject(error: any): RPCError; /** * Handle pull-based iterator request */ export declare function handlePullIteratorRequest(handler: Function, args: any[], iteratorId: string, client: RPCClient, owner?: SessionOwner, probe?: ProbeSettings): Promise<() => Promise>; /** * Handle a pull-iterator-with-callbacks request. * * Builds a callback proxy whose methods publish oneway to `callbackSubject`, * invokes the user handler with (...args, callbackProxy), then drives the * returned async generator from iterator `next`/`cancel` requests. The proxy * publishes nothing once the session ended. A frame bigger than a payload * limit too small for chunking ends the session: the batch it belongs to is * answered with PAYLOAD_TOO_LARGE. A frame that does not encode ends it the * same way with INTERNAL_ERROR; the producer is closed at its next yield * and its sessionSignal() aborts with the error. * * Once 2 ms passed since the last time, a proxy call returns a promise that * lets the event loop run. A producer that awaits its callbacks has its * cancel read during a batch, also when it waits for nothing else; one that * does not await them stops at its next wait for I/O or a timer. * * The iterator yields `undefined` — its sole purpose is client-driven flow * control. Meaningful data travels over the callback channel. The first * batch starts once the session is set up, before the consumer asks for it. */ export declare function handlePullCallbackRequest(handler: Function, args: any[], iteratorId: string, callbackSubject: string, callbackMethods: string[], onewayMethods: string[], client: RPCClient, owner?: SessionOwner, probeOptions?: ProbeSettings): Promise<() => Promise>; /** * Handle callback subscription request. * Creates a wrapper function, calls the handler with it, * and manages the subscription lifecycle. The handler sees the session's * signal through sessionSignal(), it aborts when the subscription ends. */ export declare function handleCallbackRequest(handler: Function, args: any[], callbackSubject: string, requestId: string, client: RPCClient, owner?: SessionOwner, probeOptions?: ProbeSettings): Promise<() => Promise>;