Compare commits

..

20 Commits

Author SHA1 Message Date
224dfd7520 v4.0.0 2025-12-04 23:52:18 +00:00
271f6bc0a3 BREAKING CHANGE(typedrouter): Introduce options object for TypedRouter.routeAndAddResponse (localRequest, skipHooks); add defaultRouteOptions and make hook calls respect skipHooks; bump package version to 3.2.3 2025-12-04 23:52:18 +00:00
472d009de1 v3.2.2 2025-12-04 22:48:44 +00:00
0d3d5cb562 fix(typedrequest): Add skipHooks flag to TypedRequest to optionally suppress global hooks for internal requests 2025-12-04 22:48:44 +00:00
2a89e88dd1 v3.2.1 2025-12-04 22:15:26 +00:00
2ff5602430 fix(typedrouter): Use globalThis-backed globalHooks for TypedRouter to enable cross-bundle sharing; fix merging and clearing of global hooks. 2025-12-04 22:15:26 +00:00
5444e1be88 v3.2.0 2025-12-04 21:37:08 +00:00
94ba38b4d4 feat(typedrouter): Add request/response hooks and monitoring to TypedRouter; emit hooks from TypedRequest; improve VirtualStream encoding/decoding; re-export hook types 2025-12-04 21:37:08 +00:00
1adb5629e4 v3.1.11 2025-12-03 23:51:47 +00:00
b730a977dc fix(virtualstream): Expose transport localData to handlers via TypedTools; improve VirtualStream payload encode/decode to preserve built-ins and handle nested arrays/objects 2025-12-03 23:51:47 +00:00
248430b5ad 3.1.10 2024-10-16 10:10:00 +02:00
c08a501fa5 fix(VirtualStream): Fix stream closure logic in method 2024-10-16 10:09:59 +02:00
dcf014bf95 3.1.9 2024-10-16 10:08:04 +02:00
ef5aa9ece3 fix(VirtualStream): Ensure writable streams are correctly closed asynchronously to prevent potential sync issues. 2024-10-16 10:08:04 +02:00
7a3a73a244 3.1.8 2024-10-16 09:56:33 +02:00
74e6205ac3 fix(VirtualStream): Fix stream closing behavior to correctly handle closing bits 2024-10-16 09:56:32 +02:00
57e51543e7 3.1.7 2024-10-16 02:47:36 +02:00
050966cd6f fix(VirtualStream): Fix issue in VirtualStream to handle null values during data writing. 2024-10-16 02:47:35 +02:00
11c6559e33 3.1.6 2024-10-16 02:22:45 +02:00
adff43c0e2 fix(VirtualStream): Fix backpressure handling in VirtualStream workOnQueue method 2024-10-16 02:22:44 +02:00
9 changed files with 328 additions and 11 deletions

View File

@@ -1,5 +1,74 @@
# Changelog
## 2025-12-04 - 4.0.0 - BREAKING CHANGE(typedrouter)
Introduce options object for TypedRouter.routeAndAddResponse (localRequest, skipHooks); add defaultRouteOptions and make hook calls respect skipHooks; bump package version to 3.2.3
- Changed TypedRouter.routeAndAddResponse signature to accept an options object ({ localRequest?: boolean; skipHooks?: boolean }) instead of a boolean second argument — this is a breaking API change.
- Added TypedRouter.defaultRouteOptions with defaults { localRequest: false, skipHooks: false }.
- Routing now respects options.localRequest and options.skipHooks; hook calls (onIncomingRequest, onOutgoingResponse, onIncomingResponse) are skipped when skipHooks is true to avoid hook recursion or duplicate handling (useful for broadcast-received messages).
- Bumped package.json version to 3.2.3.
## 2025-12-04 - 3.2.2 - fix(typedrequest)
Add skipHooks flag to TypedRequest to optionally suppress global hooks for internal requests
- Introduce public skipHooks boolean on TypedRequest (default false) with documentation comment explaining it should be used for internal/logging requests to prevent infinite loops.
- Guard calls to global hooks (onOutgoingRequest and onIncomingResponse) in TypedRequest.fire() so hooks are not invoked when skipHooks is true.
## 2025-12-04 - 3.2.1 - fix(typedrouter)
Use globalThis-backed globalHooks for TypedRouter to enable cross-bundle sharing; fix merging and clearing of global hooks.
- Replace static globalHooks field with getter/setter that stores hooks on globalThis so hooks are shared across bundles.
- Fix setGlobalHooks to merge new hooks with existing ones (avoiding accidental overwrite).
- Update clearGlobalHooks to clear the globalThis storage used for hooks.
## 2025-12-04 - 3.2.0 - feat(typedrouter)
Add request/response hooks and monitoring to TypedRouter; emit hooks from TypedRequest; improve VirtualStream encoding/decoding; re-export hook types
- Introduce ITypedRequestLogEntry and ITypedRouterHooks interfaces to represent structured traffic log entries and hook callbacks.
- Add static globalHooks on TypedRouter with helper APIs TypedRouter.setGlobalHooks and TypedRouter.clearGlobalHooks for global traffic monitoring.
- Add instance-level hooks on TypedRouter (setHooks) and a unified callHook() that invokes both global and instance hooks safely (errors are caught and logged).
- TypedRequest now emits onOutgoingRequest before sending and onIncomingResponse after receiving, including timestamps, duration and payload/error details.
- TypedRouter now emits lifecycle hooks while routing: onIncomingRequest when a request arrives, onOutgoingResponse for responses (both success and handler-missing error cases), and onIncomingResponse when responses arrive to be fulfilled.
- VirtualStream.encodePayloadForNetwork and decodePayloadFromNetwork were enhanced to recurse into arrays and nested objects (preserving special built-ins) to correctly handle embedded virtual streams.
- Re-export ITypedRequestLogEntry and ITypedRouterHooks from the package index for external consumption.
## 2025-12-03 - 3.1.11 - fix(virtualstream)
Expose transport localData to handlers via TypedTools; improve VirtualStream payload encode/decode to preserve built-ins and handle nested arrays/objects
- TypedHandler: pass typedRequest.localData into a TypedTools instance so handlers can access transport-layer context (e.g. websocket peer).
- TypedTools: add a public localData property to store transport-specific context available to handlers.
- VirtualStream.decodePayloadFromNetwork: preserve built-in objects (Set, Map, Date, RegExp, Error, Promise or thenable) to avoid incorrect transformation.
- VirtualStream.encodePayloadForNetwork / decodePayloadFromNetwork: added proper recursion for arrays and objects to correctly handle nested payloads and virtual streams, with path tracking to support deduplication logic.
## 2024-10-16 - 3.1.10 - fix(VirtualStream)
Fix stream closure logic in `writeToWebstream` method
- Added `writer.releaseLock()` call before closing WritableStream when `closingBit` is received in `writeToWebstream` method.
## 2024-10-16 - 3.1.9 - fix(VirtualStream)
Ensure writable streams are correctly closed asynchronously to prevent potential sync issues.
- Updated VirtualStream to use 'await' when closing writable streams, ensuring proper asynchronous handling.
## 2024-10-16 - 3.1.8 - fix(VirtualStream)
Fix stream closing behavior to correctly handle closing bits
- Introduced a 'closingBit' constant to properly signal the end of stream data.
- Updated the 'readFromWebstream' function to send a closing bit upon completion if 'closeAfterReading' is true.
- Modified the 'close' method to optionally send a closing bit when terminating the stream.
## 2024-10-16 - 3.1.7 - fix(VirtualStream)
Fix issue in VirtualStream to handle null values during data writing.
- Ensured writableStream closes gracefully when null values are encountered.
- Added a null check before writing data to the writableStream to prevent errors.
## 2024-10-16 - 3.1.6 - fix(VirtualStream)
Fix backpressure handling in VirtualStream workOnQueue method
- Resolved an issue in the workOnQueue method of VirtualStream where concurrent execution was not properly managed.
- Introduced a workingDeferred promise to ensure proper queue handling and resolve potential race conditions.
## 2024-10-16 - 3.1.5 - fix(virtualstream)
Add console log for debugging backpressure feedback loop

View File

@@ -1,6 +1,6 @@
{
"name": "@api.global/typedrequest",
"version": "3.1.5",
"version": "4.0.0",
"private": false,
"description": "A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.",
"main": "dist_ts/index.js",

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@api.global/typedrequest',
version: '3.1.5',
version: '4.0.0',
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
}

View File

@@ -31,6 +31,10 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
}
let typedResponseError: TypedResponseError;
const typedtoolsInstance = new TypedTools();
// Pass localData from the request to TypedTools so handlers can access transport-layer context
if (typedRequestArg.localData) {
typedtoolsInstance.localData = typedRequestArg.localData;
}
const response = await this.handlerFunction(typedRequestArg.request, typedtoolsInstance).catch((e) => {
if (e instanceof TypedResponseError) {
typedResponseError = e;

View File

@@ -1,11 +1,25 @@
import * as plugins from './plugins.js';
import { VirtualStream } from './classes.virtualstream.js';
import { TypedResponseError } from './classes.typedresponseerror.js';
import { TypedRouter } from './classes.typedrouter.js';
import { TypedRouter, type ITypedRequestLogEntry } from './classes.typedrouter.js';
import { TypedTarget } from './classes.typedtarget.js';
const webrequestInstance = new plugins.webrequest.WebRequest();
/**
* Helper to call global hooks from TypedRequest
*/
function callGlobalHook(
hookName: keyof typeof TypedRouter.globalHooks,
entry: ITypedRequestLogEntry
): void {
try {
TypedRouter.globalHooks[hookName]?.(entry);
} catch (err) {
console.error(`TypedRequest hook error (${hookName}):`, err);
}
}
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
/**
* in case we post against a url endpoint
@@ -19,6 +33,12 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
public method: string;
/**
* When true, hooks will not be called for this request.
* Use this for internal/logging requests to prevent infinite loops.
*/
public skipHooks: boolean = false;
/**
* @param postEndPointArg
* @param methodArg
@@ -36,6 +56,8 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
* fires the request
*/
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
const requestStartTime = Date.now();
let payloadSending: plugins.typedRequestInterfaces.ITypedRequest = {
method: this.method,
request: fireArg,
@@ -53,6 +75,18 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
}
});
// Hook: outgoing request (skip if this is an internal request)
if (!this.skipHooks) {
callGlobalHook('onOutgoingRequest', {
correlationId: payloadSending.correlation.id,
method: this.method,
direction: 'outgoing',
phase: 'request',
timestamp: requestStartTime,
payload: fireArg,
});
}
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
payloadReceiving = await this.postTrObject(payloadSending, useCacheArg);
@@ -62,6 +96,21 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
}
});
// Hook: incoming response (skip if this is an internal request)
if (!this.skipHooks) {
callGlobalHook('onIncomingResponse', {
correlationId: payloadSending.correlation.id,
method: this.method,
direction: 'incoming',
phase: 'response',
timestamp: Date.now(),
durationMs: Date.now() - requestStartTime,
payload: payloadReceiving?.response,
error: payloadReceiving?.error?.text,
});
}
return payloadReceiving.response;
}

View File

@@ -4,12 +4,91 @@ 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 {
// Use globalThis for cross-bundle hook sharing
public static get globalHooks(): ITypedRouterHooks {
if (!(globalThis as any).__typedRouterGlobalHooks) {
(globalThis as any).__typedRouterGlobalHooks = {};
}
return (globalThis as any).__typedRouterGlobalHooks;
}
public static set globalHooks(value: ITypedRouterHooks) {
(globalThis as any).__typedRouterGlobalHooks = value;
}
/**
* Set global hooks for monitoring all TypedRequest traffic
* Hooks are shared across all bundles via globalThis
*/
public static setGlobalHooks(hooks: ITypedRouterHooks): void {
const current = TypedRouter.globalHooks;
TypedRouter.globalHooks = { ...current, ...hooks };
}
/**
* Clear all global hooks
*/
public static clearGlobalHooks(): void {
(globalThis as any).__typedRouterGlobalHooks = {};
}
// 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>
@@ -83,14 +162,26 @@ export class TypedRouter {
return typedHandler;
}
/**
* Options for routeAndAddResponse
*/
public static defaultRouteOptions = {
localRequest: false,
skipHooks: false,
};
/**
* if typedrequest object has correlation.phase === 'request' -> routes a typed request object to a handler
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
* @param typedRequestArg
* @param optionsArg - Options object with:
* - localRequest: treat as local request (default: false)
* - skipHooks: skip calling hooks for this routing (default: false, use for broadcast-received messages)
*/
public async routeAndAddResponse<
T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest
>(typedRequestArg: T, localRequestArg = false): Promise<T> {
>(typedRequestArg: T, optionsArg: { localRequest?: boolean; skipHooks?: boolean } = {}): Promise<T> {
const options = { ...TypedRouter.defaultRouteOptions, ...optionsArg };
// decoding first
typedRequestArg = VirtualStream.decodePayloadFromNetwork(typedRequestArg, {
typedrouter: this,
@@ -108,7 +199,21 @@ export class TypedRouter {
}
// lets do normal routing
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
if (typedRequestArg?.correlation?.phase === 'request' || options.localRequest) {
const requestStartTime = Date.now();
// Hook: incoming request (skip if routing broadcast-received messages)
if (!options.skipHooks) {
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 +229,21 @@ export class TypedRouter {
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
// Hook: outgoing response (error - no handler)
if (!options.skipHooks) {
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 +253,36 @@ export class TypedRouter {
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
// Hook: outgoing response (success)
if (!options.skipHooks) {
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
if (!options.skipHooks) {
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);

View File

@@ -2,6 +2,12 @@ import { TypedResponseError } from './classes.typedresponseerror.js';
import * as plugins from './plugins.js';
export class TypedTools {
/**
* Local data passed from the transport layer.
* This can contain connection-specific context like the WebSocket peer.
*/
public localData: Record<string, any> = {};
public async passGuards<T = any>(guardsArg: plugins.smartguard.Guard<T>[], dataArg: T) {
const guardSet = new plugins.smartguard.GuardSet<T>(guardsArg);
const guardResult = await guardSet.allGuardsPass(dataArg);

View File

@@ -1,6 +1,9 @@
import * as plugins from './plugins.js';
import { TypedRouter } from './classes.typedrouter.js';
const closingBit: any = '#############CLOSING BIT#############';
export interface ICommFunctions {
sendMethod?: (
sendPayload: plugins.typedRequestInterfaces.IStreamRequest
@@ -79,7 +82,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
}
public static decodePayloadFromNetwork(objectPayload: any, commFunctions: ICommFunctions): any {
if (
plugins.smartbuffer.isBufferLike(objectPayload)
|| objectPayload instanceof TypedRouter
@@ -87,6 +90,18 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
return objectPayload;
}
if (objectPayload !== null && typeof objectPayload === 'object') {
// Preserve built-in objects that shouldn't be transformed
if (
objectPayload instanceof Set ||
objectPayload instanceof Map ||
objectPayload instanceof Date ||
objectPayload instanceof RegExp ||
objectPayload instanceof Error ||
objectPayload instanceof Promise ||
typeof objectPayload.then === 'function'
) {
return objectPayload;
}
if (objectPayload._isVirtualStream) {
const virtualStream = new VirtualStream();
virtualStream.streamId = objectPayload.streamId;
@@ -143,10 +158,17 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
constructor() {}
workingDeferred: plugins.smartpromise.Deferred<void>;
/**
* takes care of sending
*/
private async workOnQueue() {
if (this.workingDeferred) {
return this.workingDeferred.promise;
} else {
this.workingDeferred = plugins.smartpromise.defer();
}
if(this.side === 'requesting') {
let thisSideIsBackpressured = !this.receiveBackpressuredArray.checkSpaceAvailable();
let otherSideHasNext = false;
@@ -219,6 +241,8 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
}
}
this.workingDeferred.resolve();
this.workingDeferred = null;
}
/**
@@ -269,7 +293,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycleId: streamTrArg.request.cycleId,
cycle: 'response',
mainPurpose: 'chunk',
next: this.sendBackpressuredArray.data.length > 1,
next: this.sendBackpressuredArray.data.length > 1, // 1 and not 0 because we call shift a few lines down
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
chunkData: this.sendBackpressuredArray.shift(),
};
@@ -383,18 +407,32 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
streamIsDone = done;
}
if (closeAfterReading) {
await this.close();
await this.close(true);
}
}
public async writeToWebstream(writableStreamArg: WritableStream<T>) {
const writer = writableStreamArg.getWriter();
while(this.keepAlive || this.receiveBackpressuredArray.checkHasItems()) {
await writer.write(await this.fetchData());
const value = await this.fetchData();
if (value === closingBit) {
writer.releaseLock();
await writableStreamArg.close();
break;
}
await writer.write(value);
}
}
public async close() {
/**
* closes the stream
* if sendClosingBitArg is true, the stream will send a closing bit
* @param sendClosingBitArg
*/
public async close(sendClosingBitArg = false) {
if (sendClosingBitArg) {
this.sendData(closingBit);
}
this.keepAlive = false;
}
}

View File

@@ -3,4 +3,7 @@ export * from './classes.typedhandler.js';
export * from './classes.typedrouter.js';
export * from './classes.typedresponseerror.js';
export * from './classes.typedtarget.js';
export * from './classes.virtualstream.js';
export * from './classes.virtualstream.js';
// Re-export hook interfaces from typedrouter
export type { ITypedRequestLogEntry, ITypedRouterHooks } from './classes.typedrouter.js';