import { DataEvent, EventToken, EventTokenSync } from '@deepkit/event'; import { InjectorContext } from '@deepkit/injector'; import { RpcKernelBaseConnection } from './server/kernel.js'; import { RpcControllerAccess, Session } from './server/security.js'; export type WithFail = Omit & { phase: 'fail'; error: Error }; export interface RpcConnectionEvent { context: { connection: RpcKernelBaseConnection; injector: InjectorContext; }; } export const onRpcConnection = new EventTokenSync>('rpc.connection'); export interface RpcConnectionCloseEvent extends RpcConnectionEvent { /** * There could be multiple reasons why a connection is closed. This could be a normal * close, an error, or a timeout. * If the connection was closed because of an error, this property contains the error object. */ reason: string | Error; } export const onRpcConnectionClose = new EventTokenSync>('rpc.connectionClose'); /** * All times are in milliseconds (using performance.now()). */ export interface RpcActionTimings { /** * The start of the action (performance.now()) */ start: number; /** * The end of the action (performance.now()) */ end: number; /** * Time it took to check the controller access (since start) */ types?: number; /** * Time it took to parse the body (since start) */ parseBody?: number; /** * Time it took to validate the parameters (since start) */ validate?: number; /** * Time it took to check the controller access (since start) */ controllerAccess?: number; } export interface RpcActionEventBase extends RpcConnectionEvent { phase: 'start' | 'success'; controller: RpcControllerAccess; timing: RpcActionTimings; } export type RpcActionEvent = RpcActionEventBase | WithFail; export const onRpcAction = new EventTokenSync>('rpc.action'); export interface RpcAuthEventStart extends RpcConnectionEvent { token: any; phase: 'start'; /** * Set this to the session object if the authentication was successful. * If set this will bypass RpcKernelSecurity and directly use this session. */ session?: Session; } export interface RpcAuthEventSuccess extends RpcConnectionEvent { token: any; phase: 'success'; session: Session; } export type RpcAuthEvent = RpcAuthEventStart | RpcAuthEventSuccess | WithFail; export const onRpcAuth = new EventToken>('rpc.auth'); export interface RpcControllerAccessEventStart extends RpcConnectionEvent { phase: 'start'; session: Session; controller: RpcControllerAccess; /** * Set this to true if you want to grant access to the controller. * If set this will bypass RpcKernelSecurity and directly grant access. */ granted?: boolean; } export interface RpcControllerAccessEventBase extends RpcConnectionEvent { phase: 'success' | 'denied'; session: Session; controller: RpcControllerAccess; } export type RpcControllerAccessEvent = RpcControllerAccessEventStart | RpcControllerAccessEventBase | WithFail; export const onRpcControllerAccess = new EventToken>('rpc.controllerAccess');