import { JRPCNotification, JRPCRequest } from "../interfaces.js"; import { MiddlewareContext } from "./MiddlewareContext.js"; import { ContextConstraint, HandleOptions, InferKeyValues, InvalidEngine, JRPCMiddlewareV2, JsonRpcCall, MergedContextOf, MixedParam, Next, RequestOf, RequestState, ResultConstraint, WithoutId } from "./v2interfaces.js"; /** * A JSON-RPC request and response processor. * * Give it a stack of middleware, pass it requests, and get back responses. * * #### Requests vs. notifications * * JSON-RPC requests come in two flavors: * * - [Requests](https://www.jsonrpc.org/specification#request_object), i.e. request objects _with_ an `id` * - [Notifications](https://www.jsonrpc.org/specification#notification), i.e. request objects _without_ an `id` * * For requests, one of the engine's middleware must "end" the request by returning a non-`undefined` result, * or {@link handle} will throw an error: * * For notifications, on the other hand, one of the engine's middleware must return `undefined` to end the request, * and any non-`undefined` return values will cause an error: * * @template Request - The type of request to handle. * @template Result - The type of result to return. * * @example * ```ts * const engine = JRPCEngineV2.create({ * middleware, * }); * * try { * const result = await engine.handle(request); * // Handle result * } catch (error) { * // Handle error * } * ``` */ export declare class JRPCEngineV2 { #private; private constructor(); /** * Create a new JSON-RPC engine. * * @throws If the middleware array is empty. * @param options - The options for the engine. * @param middleware - The middleware to use. * @returns The JSON-RPC engine. */ static create, any> = JRPCMiddlewareV2>({ middleware }: { middleware: Middleware[]; }): MergedContextOf extends never ? InvalidEngine<"Some middleware have incompatible context types"> : JRPCEngineV2, MergedContextOf>; /** * Handle a JSON-RPC request. * * @param request - The JSON-RPC request to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. * @returns The JSON-RPC response. */ handle(request: Extract extends never ? never : Extract, options?: HandleOptions): Promise extends never ? never : ResultConstraint>; /** * Handle a JSON-RPC notification. Notifications do not return a result. * * @param notification - The JSON-RPC notification to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. */ handle(notification: Extract extends never ? never : WithoutId>, options?: HandleOptions): Promise extends never ? never : ResultConstraint>; /** * Handle a JSON-RPC call, i.e. request or notification. Requests return a * result, notifications do not. * * @param call - The JSON-RPC call to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. * @returns The JSON-RPC response, or `undefined` if the call is a notification. */ handle(call: MixedParam, options?: HandleOptions): Promise | void>; /** * Convert the engine into a JSON-RPC middleware. * * @returns The JSON-RPC middleware. */ asMiddleware(): JRPCMiddlewareV2, Context>; /** * Destroy the engine. Calls the `destroy()` method of any middleware that has * one. Attempting to use the engine after destroying it will throw an error. */ destroy(): Promise; /** * Handle a JSON-RPC request. Throws if a middleware performs an invalid * operation. Permits returning an `undefined` result. * * @param originalRequest - The JSON-RPC request to handle. * @param rawContext - The context to pass to the middleware. * @returns The result from the middleware. */ _handle(originalRequest: Request, rawContext?: Context | InferKeyValues): Promise>; /** * Create a factory of `next()` functions for use with a particular request. * The factory is recursive, and a new `next()` is created for each middleware * invocation. * * @param middlewareIterator - The iterator of middleware for the current * request. * @param state - The current values of the request and result. * @param context - The context to pass to the middleware. * @returns The `next()` function factory. */ _makeNextFactory(middlewareIterator: Iterator, Context>>, state: RequestState, context: Context): () => Next; _makeMiddlewareIterator(): Iterator, Context>>; /** * Validate the result from a middleware and, if it's a new value, update the * current result. * * @param result - The result from the middleware. * @param state - The current values of the request and result. */ _updateResult(result: Readonly> | ResultConstraint | void, state: RequestState): void; /** * Assert that a request modified by a middleware is valid. * * @param currentRequest - The current request. * @param nextRequest - The next request. */ _assertValidNextRequest(currentRequest: Request, nextRequest: Request): void; _assertIsNotDestroyed(): void; }