Compare commits

..

56 Commits

Author SHA1 Message Date
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
c61e30fe64 3.1.5 2024-10-16 02:16:30 +02:00
4887c07ef3 fix(virtualstream): Add console log for debugging backpressure feedback loop 2024-10-16 02:16:29 +02:00
c0fdc8a1d4 3.1.4 2024-10-16 02:15:17 +02:00
0af720d901 fix(VirtualStream): Corrected the logic for backpressure handling in response 2024-10-16 02:15:17 +02:00
43b433f0c2 3.1.3 2024-10-14 01:42:45 +02:00
d38a225c78 fix(VirtualStream): Fix keepAlive flag handling in VirtualStream and added stream closure in tests 2024-10-14 01:42:44 +02:00
f9b5c897cf 3.1.2 2024-10-14 01:31:58 +02:00
1a92aa6630 fix(core): Fix incorrect backpressure logic in VirtualStream class 2024-10-14 01:31:58 +02:00
70e2dcc1b8 3.1.1 2024-10-14 00:13:29 +02:00
7b6fb9e9bc fix(virtualstream): Fix handling of virtual streams for proper shutdown 2024-10-14 00:13:28 +02:00
03c7150b6b 3.1.0 2024-10-11 02:15:46 +02:00
83cd25d5a2 feat(virtualstream): Enhance VirtualStream with optional closure when reading from webstream 2024-10-11 02:15:45 +02:00
04d60e6a95 3.0.33 2024-10-11 02:09:51 +02:00
549719ede6 fix(test): Increase delay duration before stopping the server in test suite. 2024-10-11 02:09:50 +02:00
855663eea9 3.0.32 2024-09-06 13:46:10 +02:00
4d98915dbd fix(virtualstream): Fix keep-alive loop handling and test cleanup 2024-09-06 13:46:09 +02:00
f461f16bfd 3.0.31 2024-09-06 13:02:05 +02:00
6beadb8cfc fix(core): Updated dependencies and added close method to VirtualStream 2024-09-06 13:02:04 +02:00
360c8a618b 3.0.30 2024-05-31 22:41:18 +02:00
c03854a9fc fix(core): update 2024-05-31 22:41:17 +02:00
cebd903c9b 3.0.29 2024-05-31 16:34:55 +02:00
e7cf5b7694 fix(error handling): now alos logs the method that an error has been given to. 2024-05-31 16:34:54 +02:00
839bd138c1 3.0.28 2024-05-30 22:47:03 +02:00
e1c721d511 fix(core): update 2024-05-30 22:47:02 +02:00
7ce3f83d54 3.0.27 2024-05-30 22:41:50 +02:00
2e0c6400e8 fix(core): update 2024-05-30 22:41:49 +02:00
58637ed90e 3.0.26 2024-05-30 19:01:40 +02:00
489fa237b9 fix(core): update 2024-05-30 19:01:39 +02:00
4449c82611 3.0.25 2024-05-25 02:18:46 +02:00
fe31a779e4 fix(core): update 2024-05-25 02:18:46 +02:00
9b96c22d6a 3.0.24 2024-05-25 02:15:09 +02:00
262891ccf3 fix(core): update 2024-05-25 02:15:08 +02:00
0d60b29ff7 3.0.23 2024-05-05 17:15:44 +02:00
ecca2a9f32 fix(core): update 2024-05-05 17:15:44 +02:00
3643e5e753 3.0.22 2024-05-05 16:42:22 +02:00
4cba418f13 fix(core): update 2024-05-05 16:42:22 +02:00
a966b0735c 3.0.21 2024-04-17 20:35:42 +02:00
6fed2cdc5c fix(core): update 2024-04-17 20:35:41 +02:00
15 changed files with 5373 additions and 3065 deletions

126
changelog.md Normal file
View File

@@ -0,0 +1,126 @@
# Changelog
## 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
- Inserted a console log message to provide insight when waiting due to backpressure in the workOnQueue method.
## 2024-10-16 - 3.1.4 - fix(VirtualStream)
Corrected the logic for backpressure handling in response
- Fixed backpressure flag assignment in the response handling logic of VirtualStream.
- Ensured correct negation logic for checking receive backpressure status.
## 2024-10-14 - 3.1.3 - fix(VirtualStream)
Fix keepAlive flag handling in VirtualStream and added stream closure in tests
- Ensure that the keepAlive status is correctly maintained in the keepAlive trigger method.
- Added closure of VirtualStreams in the test suite for proper resource cleanup.
## 2024-10-14 - 3.1.2 - fix(core)
Fix incorrect backpressure logic in VirtualStream class
- Corrected the logic for determining backpressure status by checking the available space in the receiveBackpressuredArray.
- Introduced a looping mechanism to wait when the other side is backpressured before sending more data.
## 2024-10-14 - 3.1.1 - fix(virtualstream)
Fix handling of virtual streams for proper shutdown
- Ensured that writeToWebstream method checks for remaining items in receiveBackpressuredArray before closing.
- Corrected package.json dependency for @push.rocks/tapbundle.
- Updated @types/node to version 22.7.5.
## 2024-10-11 - 3.1.0 - feat(virtualstream)
Enhance VirtualStream with optional closure when reading from webstream
- Added an optional parameter `closeAfterReading` to the `readFromWebstream` method.
- The stream will close automatically after reading if `closeAfterReading` is set to true.
## 2024-10-11 - 3.0.33 - fix(test)
Increase delay duration before stopping the server in test suite.
- Adjusted the delay time from 1000 ms to 10000 ms before stopping the server to ensure tests complete smoothly.
## 2024-09-06 - 3.0.32 - fix(virtualstream)
Fix keep-alive loop handling and test cleanup
- Prevent unnecessary keep-alive loop from starting on the responding side
- Add logging for keep-alive loop initiation in VirtualStream
- Temporarily comment out stream close and tap forceful stop in test to avoid abrupt termination
## 2024-09-06 - 3.0.31 - fix(core)
Updated dependencies and added close method to VirtualStream
- Updated dependencies in package.json for better compatibility
- Added close method to VirtualStream class in ts/classes.virtualstream.ts for more graceful stream termination
## 2024-05-31 - 3.0.28 - Error Handling
Enhancement to error handling mechanisms.
- Logs now include the method to which an error was given.
## 2023-08-04 - 3.0.0 - Core
Introduced a breaking change.
- Major update to core functionalities.

View File

@@ -1,6 +1,6 @@
{
"name": "@api.global/typedrequest",
"version": "3.0.20",
"version": "3.2.2",
"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",
@@ -14,24 +14,25 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@api.global/typedserver": "^3.0.27",
"@git.zone/tsbuild": "^2.1.72",
"@api.global/typedserver": "^3.0.51",
"@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsbundle": "^2.0.15",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.88",
"@git.zone/tsrun": "^1.2.49",
"@git.zone/tstest": "^1.0.90",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.12.7"
"@push.rocks/tapbundle": "^5.3.0",
"@types/node": "^22.7.5"
},
"dependencies": {
"@api.global/typedrequest-interfaces": "^3.0.18",
"@api.global/typedrequest-interfaces": "^3.0.19",
"@push.rocks/isounique": "^1.0.5",
"@push.rocks/lik": "^6.0.14",
"@push.rocks/smartbuffer": "^2.0.3",
"@push.rocks/lik": "^6.1.0",
"@push.rocks/smartbuffer": "^3.0.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/webrequest": "^3.0.36",
"@push.rocks/webstream": "^1.0.8"
"@push.rocks/smartguard": "^3.1.0",
"@push.rocks/smartpromise": "^4.0.4",
"@push.rocks/webrequest": "^3.0.37",
"@push.rocks/webstream": "^1.0.10"
},
"files": [
"ts/**/*",

7935
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -44,4 +44,4 @@ tap.skip.test('test', async (tools) => {
await tools.delayFor(5000);
})
tap.start();
export default tap.start();

View File

@@ -98,12 +98,14 @@ tap.test('should allow VirtualStreams', async () => {
const data = await generatedRequestingVS.fetchData();
const decodedData = new TextDecoder().decode(data);
expect(decodedData).toEqual('hello');
await newRequestingVS.close();
await newRespondingVS.close();
});
tap.test('should end the server', async (toolsArg) => {
await toolsArg.delayFor(1000);
await toolsArg.delayFor(10000);
await testServer.stop();
setTimeout(() => process.exit(0), 100);
await tap.stopForcefully();
});
tap.start();
export default tap.start();

View File

@@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@api.global/typedrequest',
version: '3.0.20',
version: '3.2.2',
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
}

View File

@@ -1,8 +1,10 @@
import * as plugins from './plugins.js';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
import { TypedResponseError } from './classes.typedresponseerror.js';
import { TypedTools } from './classes.typedtools.js';
export type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request']
requestArg: T['request'],
typedToolsArg?: TypedTools
) => Promise<T['response']>;
/**
@@ -28,7 +30,12 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
);
}
let typedResponseError: TypedResponseError;
const response = await this.handlerFunction(typedRequestArg.request).catch((e) => {
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;
} else {

View File

@@ -1,11 +1,25 @@
import * as plugins from './plugins.js';
import { VirtualStream } from './typedrequest.classes.virtualstream.js';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
import { TypedTarget } from './typedrequest.classes.typedtarget.js';
import { VirtualStream } from './classes.virtualstream.js';
import { TypedResponseError } from './classes.typedresponseerror.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;
}
@@ -79,7 +128,7 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
}
if (payloadReceiving.error) {
console.error(
`Got an error ${payloadReceiving.error.text} with data ${JSON.stringify(
`method: >>${this.method}<< got an ERROR: "${payloadReceiving.error.text}" with data ${JSON.stringify(
payloadReceiving.error.data,
null,
2

View File

@@ -1,8 +1,32 @@
import * as plugins from './plugins.js';
import { VirtualStream } from './typedrequest.classes.virtualstream.js';
import { VirtualStream } from './classes.virtualstream.js';
import { TypedHandler } from './typedrequest.classes.typedhandler.js';
import { TypedRequest } from './typedrequest.classes.typedrequest.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
@@ -10,6 +34,61 @@ import { TypedRequest } from './typedrequest.classes.typedrequest.js';
* 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>
@@ -109,6 +188,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 +215,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 +237,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);

View File

@@ -1,4 +1,4 @@
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
import { TypedRouter } from './classes.typedrouter.js';
import * as plugins from './plugins.js';
export type IPostMethod = (

19
ts/classes.typedtools.ts Normal file
View File

@@ -0,0 +1,19 @@
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);
if (!guardResult) {
const failedHint = await guardSet.getFailedHint(dataArg);
throw new TypedResponseError(`guard failed: ${failedHint}`, { failedHint });
}
}
}

View File

@@ -1,5 +1,8 @@
import * as plugins from './plugins.js';
import type { TypedRouter } from './typedrequest.classes.typedrouter.js';
import { TypedRouter } from './classes.typedrouter.js';
const closingBit: any = '#############CLOSING BIT#############';
export interface ICommFunctions {
sendMethod?: (
@@ -15,7 +18,7 @@ export interface ICommFunctions {
* 3. It has a Readable and Writable side.
* 4. The Writable side is Readable on the other side and vice versa.
*/
export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestInterfaces.IVirtualStream<T> {
export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterfaces.IVirtualStream<T> {
// STATIC
public static encodePayloadForNetwork(
objectPayload: any,
@@ -79,10 +82,26 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
}
public static decodePayloadFromNetwork(objectPayload: any, commFunctions: ICommFunctions): any {
if (plugins.smartbuffer.isBufferLike(objectPayload)) {
if (
plugins.smartbuffer.isBufferLike(objectPayload)
|| objectPayload instanceof TypedRouter
) {
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;
@@ -139,10 +158,17 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
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;
@@ -158,7 +184,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
cycle: 'request',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
},
response: null,
}).catch(() => {
@@ -174,6 +200,13 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
// do work loop
while (this.sendBackpressuredArray.data.length > 0 || otherSideHasNext) {
if (otherSideIsBackpressured) {
while (otherSideIsBackpressured) {
console.log('waiting for feedback because of backpressure...');
await plugins.smartdelay.delayFor(50);
await getFeedback();
}
}
let dataArg: typeof this.sendBackpressuredArray.data[0];
if (this.sendBackpressuredArray.data.length > 0) {
dataArg = this.sendBackpressuredArray.shift();
@@ -200,13 +233,16 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
if (streamTr && streamTr.response && streamTr.response.chunkData) {
this.receiveBackpressuredArray.push(streamTr.response.chunkData);
}
thisSideIsBackpressured = this.receiveBackpressuredArray.checkSpaceAvailable();
otherSideIsBackpressured = streamTr && streamTr.response && streamTr.response.backpressure;
thisSideIsBackpressured = !this.receiveBackpressuredArray.checkSpaceAvailable();
// lets care about looping
otherSideHasNext = streamTr && streamTr.response && streamTr.response.next;
}
}
this.workingDeferred.resolve();
this.workingDeferred = null;
}
/**
@@ -231,7 +267,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
mainPurpose: 'keepAlive',
keepAlive: this.keepAlive,
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
@@ -243,7 +279,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
cycle: 'response',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
@@ -257,8 +293,8 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
cycleId: streamTrArg.request.cycleId,
cycle: 'response',
mainPurpose: 'chunk',
next: this.sendBackpressuredArray.data.length > 1,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
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(),
};
} else {
@@ -268,6 +304,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
cycle: 'response',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
streamTrArg.request = null;
@@ -291,10 +328,14 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
*/
private async startKeepAliveLoop() {
// initially wait for a second
if (this.side === 'responding') {
return;
}
await plugins.smartdelay.delayFor(0);
console.log(`starting keepalive loop on side ${this.side}`);
let counter = 0;
keepAliveLoop: while (this.keepAlive) {
const triggerResult = await this.triggerKeepAlive();
await this.triggerKeepAlive();
await plugins.smartdelay.delayFor(1000);
}
await plugins.smartdelay.delayFor(1000);
@@ -312,7 +353,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
cycleId: plugins.isounique.uni(),
cycle: 'request',
mainPurpose: 'keepAlive',
keepAlive: true,
keepAlive: this.keepAlive,
},
response: null,
}).catch(() => {
@@ -355,7 +396,7 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
* reads from a Readable and sends it to the other side
* @param readableStreamArg
*/
public async readFromWebstream(readableStreamArg: ReadableStream<T>) {
public async readFromWebstream(readableStreamArg: ReadableStream<T>, closeAfterReading = true) {
const reader = readableStreamArg.getReader();
let streamIsDone = false;
while(!streamIsDone) {
@@ -365,12 +406,33 @@ export class VirtualStream<T = ArrayBufferLike> implements plugins.typedRequestI
}
streamIsDone = done;
}
if (closeAfterReading) {
await this.close(true);
}
}
public async writeToWebstream(writableStreamArg: WritableStream<T>) {
const writer = writableStreamArg.getWriter();
while(this.keepAlive) {
await writer.write(await this.fetchData());
while(this.keepAlive || this.receiveBackpressuredArray.checkHasItems()) {
const value = await this.fetchData();
if (value === closingBit) {
writer.releaseLock();
await writableStreamArg.close();
break;
}
await writer.write(value);
}
}
/**
* 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

@@ -1,6 +1,9 @@
export * from './typedrequest.classes.typedrequest.js';
export * from './typedrequest.classes.typedhandler.js';
export * from './typedrequest.classes.typedrouter.js';
export * from './typedrequest.classes.typedresponseerror.js';
export * from './typedrequest.classes.typedtarget.js';
export * from './typedrequest.classes.virtualstream.js';
export * from './classes.typedrequest.js';
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';
// Re-export hook interfaces from typedrouter
export type { ITypedRequestLogEntry, ITypedRouterHooks } from './classes.typedrouter.js';

View File

@@ -8,8 +8,8 @@ import * as isounique from '@push.rocks/isounique';
import * as lik from '@push.rocks/lik';
import * as smartbuffer from '@push.rocks/smartbuffer';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartguard from '@push.rocks/smartguard';
import * as smartpromise from '@push.rocks/smartpromise';
import * as webrequest from '@push.rocks/webrequest';
export { isounique, lik, smartbuffer, smartdelay, smartpromise, webrequest };
export { isounique, lik, smartbuffer, smartdelay, smartguard, smartpromise, webrequest };