feat(typedrouter): Add request/response hooks and monitoring to TypedRouter; emit hooks from TypedRequest; improve VirtualStream encoding/decoding; re-export hook types

This commit is contained in:
2025-12-04 21:37:08 +00:00
parent 1adb5629e4
commit 94ba38b4d4
5 changed files with 173 additions and 3 deletions

View File

@@ -4,12 +4,80 @@ import { VirtualStream } from './classes.virtualstream.js';
import { TypedHandler } from './classes.typedhandler.js';
import { TypedRequest } from './classes.typedrequest.js';
/**
* Log entry for TypedRequest traffic monitoring
*/
export interface ITypedRequestLogEntry {
correlationId: string;
method: string;
direction: 'outgoing' | 'incoming';
phase: 'request' | 'response';
timestamp: number;
durationMs?: number;
payload: any;
error?: string;
}
/**
* Hooks for intercepting TypedRequest traffic
*/
export interface ITypedRouterHooks {
onOutgoingRequest?: (entry: ITypedRequestLogEntry) => void;
onIncomingResponse?: (entry: ITypedRequestLogEntry) => void;
onIncomingRequest?: (entry: ITypedRequestLogEntry) => void;
onOutgoingResponse?: (entry: ITypedRequestLogEntry) => void;
}
/**
* A typed router decides on which typed handler to call based on the method
* specified in the typed request
* This is thought for reusing the same url endpoint for different methods
*/
export class TypedRouter {
// Static hooks for global traffic monitoring
public static globalHooks: ITypedRouterHooks = {};
/**
* Set global hooks for monitoring all TypedRequest traffic
*/
public static setGlobalHooks(hooks: ITypedRouterHooks): void {
TypedRouter.globalHooks = { ...TypedRouter.globalHooks, ...hooks };
}
/**
* Clear all global hooks
*/
public static clearGlobalHooks(): void {
TypedRouter.globalHooks = {};
}
// Instance-level hooks (for per-router monitoring)
public hooks: ITypedRouterHooks = {};
/**
* Set instance-level hooks for monitoring traffic through this router
*/
public setHooks(hooks: ITypedRouterHooks): void {
this.hooks = { ...this.hooks, ...hooks };
}
/**
* Helper to call both global and instance hooks
*/
private callHook(
hookName: keyof ITypedRouterHooks,
entry: ITypedRequestLogEntry
): void {
try {
// Call global hooks
TypedRouter.globalHooks[hookName]?.(entry);
// Call instance hooks
this.hooks[hookName]?.(entry);
} catch (err) {
console.error(`TypedRouter hook error (${hookName}):`, err);
}
}
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
public handlerMap = new plugins.lik.ObjectMap<
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
@@ -109,6 +177,18 @@ export class TypedRouter {
// lets do normal routing
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
const requestStartTime = Date.now();
// Hook: incoming request
this.callHook('onIncomingRequest', {
correlationId: typedRequestArg.correlation?.id || 'unknown',
method: typedRequestArg.method,
direction: 'incoming',
phase: 'request',
timestamp: requestStartTime,
payload: typedRequestArg.request,
});
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
if (!typedHandler) {
@@ -124,6 +204,19 @@ export class TypedRouter {
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
// Hook: outgoing response (error - no handler)
this.callHook('onOutgoingResponse', {
correlationId: typedRequestArg.correlation?.id || 'unknown',
method: typedRequestArg.method,
direction: 'outgoing',
phase: 'response',
timestamp: Date.now(),
durationMs: Date.now() - requestStartTime,
payload: typedRequestArg.response,
error: typedRequestArg.error?.text,
});
return typedRequestArg;
}
@@ -133,8 +226,32 @@ export class TypedRouter {
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
// Hook: outgoing response (success)
this.callHook('onOutgoingResponse', {
correlationId: typedRequestArg.correlation?.id || 'unknown',
method: typedRequestArg.method,
direction: 'outgoing',
phase: 'response',
timestamp: Date.now(),
durationMs: Date.now() - requestStartTime,
payload: typedRequestArg.response,
error: typedRequestArg.error?.text,
});
return typedRequestArg;
} else if (typedRequestArg?.correlation?.phase === 'response') {
// Hook: incoming response
this.callHook('onIncomingResponse', {
correlationId: typedRequestArg.correlation?.id || 'unknown',
method: typedRequestArg.method,
direction: 'incoming',
phase: 'response',
timestamp: Date.now(),
payload: typedRequestArg.response,
error: typedRequestArg.error?.text,
});
this.fireEventInterestMap
.findInterest(typedRequestArg.correlation.id)
?.fullfillInterest(typedRequestArg);