Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1adb5629e4 | |||
| b730a977dc | |||
| 248430b5ad | |||
| c08a501fa5 | |||
| dcf014bf95 | |||
| ef5aa9ece3 | |||
| 7a3a73a244 | |||
| 74e6205ac3 | |||
| 57e51543e7 | |||
| 050966cd6f |
31
changelog.md
31
changelog.md
@@ -1,5 +1,36 @@
|
||||
# Changelog
|
||||
|
||||
## 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
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@api.global/typedrequest",
|
||||
"version": "3.1.6",
|
||||
"version": "3.1.11",
|
||||
"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",
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedrequest',
|
||||
version: '3.1.6',
|
||||
version: '3.1.11',
|
||||
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -278,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(),
|
||||
};
|
||||
@@ -392,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user