UNPKG

@jsonjoy.com/reactive-rpc

Version:

Reactive-RPC is a library for building reactive APIs over WebSocket, HTTP, and other RPCs.

75 lines (74 loc) 3.28 kB
import * as http from 'http'; import * as https from 'https'; import type * as tls from 'tls'; import { Writer } from '@jsonjoy.com/util/lib/buffers/Writer'; import { WsServerConnection } from '../ws/server/WsServerConnection'; import { WsFrameEncoder } from '../ws/codec/WsFrameEncoder'; import { Router, type RouteMatcher } from '@jsonjoy.com/jit-router'; import type { Printable } from 'sonic-forest/lib/print/types'; import { Http1ConnectionContext, WsConnectionContext } from './context'; import { RpcCodecs } from '../../common/codec/RpcCodecs'; import type { RpcMessageCodec } from '../../common/codec/types'; export type Http1Handler = (ctx: Http1ConnectionContext) => void | Promise<void>; export type Http1NotFoundHandler = (res: http.ServerResponse, req: http.IncomingMessage) => void; export type Http1InternalErrorHandler = (error: unknown, res: http.ServerResponse, req: http.IncomingMessage) => void; export declare class Http1EndpointMatch { readonly handler: Http1Handler; readonly msgCodec: RpcMessageCodec; constructor(handler: Http1Handler, msgCodec: RpcMessageCodec); } export interface Http1EndpointDefinition { method?: string | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT'; path: string; handler: Http1Handler; msgCodec?: RpcMessageCodec; } export interface WsEndpointDefinition { path: string; maxIncomingMessage?: number; maxOutgoingBackpressure?: number; onWsUpgrade?(req: http.IncomingMessage, connection: WsServerConnection): void; handler(ctx: WsConnectionContext, req: http.IncomingMessage): void; } export interface Http1ServerOpts { server: http.Server; codecs?: RpcCodecs; writer?: Writer; } export type Http1CreateServerOpts = Http1CreateHttpServerOpts | Http1CreateHttpsServerOpts; export interface Http1CreateHttpServerOpts { tls?: false; conf?: http.ServerOptions; } export interface Http1CreateHttpsServerOpts { tls: true; conf?: https.ServerOptions; secureContext?: () => Promise<tls.SecureContextOptions>; secureContextRefreshInterval?: number; } export declare class Http1Server implements Printable { protected readonly opts: Http1ServerOpts; static create: (opts?: Http1CreateServerOpts) => Promise<http.Server | https.Server>; readonly codecs: RpcCodecs; readonly server: http.Server; constructor(opts: Http1ServerOpts); start(): Promise<void>; stop(): Promise<void>; onnotfound: Http1NotFoundHandler; oninternalerror: Http1InternalErrorHandler; protected readonly httpRouter: Router<Http1EndpointMatch>; protected httpMatcher: RouteMatcher<Http1EndpointMatch>; route(def: Http1EndpointDefinition): void; private readonly onRequest; protected readonly wsEncoder: WsFrameEncoder; protected readonly wsRouter: Router<WsEndpointDefinition>; protected wsMatcher: RouteMatcher<WsEndpointDefinition>; private readonly onUpgrade; private readonly onWsUpgrade; ws(def: WsEndpointDefinition): void; findIp(req: http.IncomingMessage): string; findToken(req: http.IncomingMessage): string; enableHttpPing(path?: string): void; enableKamalPing(path?: string, response?: string | Uint8Array): void; toString(tab?: string): string; }