Compare commits

...

24 Commits

Author SHA1 Message Date
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
8 changed files with 916 additions and 685 deletions

65
changelog.md Normal file
View File

@ -0,0 +1,65 @@
# Changelog
## 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.27",
"version": "3.1.5",
"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,23 +14,23 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@api.global/typedserver": "^3.0.50",
"@git.zone/tsbuild": "^2.1.80",
"@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/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.13"
"@push.rocks/tapbundle": "^5.3.0",
"@types/node": "^22.7.5"
},
"dependencies": {
"@api.global/typedrequest-interfaces": "^3.0.19",
"@push.rocks/isounique": "^1.0.5",
"@push.rocks/lik": "^6.0.15",
"@push.rocks/lik": "^6.1.0",
"@push.rocks/smartbuffer": "^3.0.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartguard": "^3.0.2",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/smartguard": "^3.1.0",
"@push.rocks/smartpromise": "^4.0.4",
"@push.rocks/webrequest": "^3.0.37",
"@push.rocks/webstream": "^1.0.10"
},

1466
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

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();
});
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.27',
version: '3.1.5',
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
}

View File

@ -4,7 +4,7 @@ import { TypedTools } from './classes.typedtools.js';
export type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request'],
typedToolsArg?: any
typedToolsArg?: TypedTools
) => Promise<T['response']>;
/**

View File

@ -79,7 +79,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

@ -162,7 +162,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycle: 'request',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
},
response: null,
}).catch(() => {
@ -178,6 +178,13 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
// 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();
@ -204,7 +211,8 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
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;
@ -235,7 +243,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
mainPurpose: 'keepAlive',
keepAlive: this.keepAlive,
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
@ -247,7 +255,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycle: 'response',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
@ -262,7 +270,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycle: 'response',
mainPurpose: 'chunk',
next: this.sendBackpressuredArray.data.length > 1,
backpressure: this.receiveBackpressuredArray.checkSpaceAvailable(),
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
chunkData: this.sendBackpressuredArray.shift(),
};
} else {
@ -272,6 +280,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycle: 'response',
mainPurpose: 'feedback',
next: this.sendBackpressuredArray.data.length > 0,
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
};
}
streamTrArg.request = null;
@ -295,10 +304,14 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
*/
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);
@ -316,7 +329,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
cycleId: plugins.isounique.uni(),
cycle: 'request',
mainPurpose: 'keepAlive',
keepAlive: true,
keepAlive: this.keepAlive,
},
response: null,
}).catch(() => {
@ -359,7 +372,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
* 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) {
@ -369,12 +382,19 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
}
streamIsDone = done;
}
if (closeAfterReading) {
await this.close();
}
}
public async writeToWebstream(writableStreamArg: WritableStream<T>) {
const writer = writableStreamArg.getWriter();
while(this.keepAlive) {
while(this.keepAlive || this.receiveBackpressuredArray.checkHasItems()) {
await writer.write(await this.fetchData());
}
}
public async close() {
this.keepAlive = false;
}
}