Compare commits
93 Commits
Author | SHA1 | Date | |
---|---|---|---|
7a3a73a244 | |||
74e6205ac3 | |||
57e51543e7 | |||
050966cd6f | |||
11c6559e33 | |||
adff43c0e2 | |||
c61e30fe64 | |||
4887c07ef3 | |||
c0fdc8a1d4 | |||
0af720d901 | |||
43b433f0c2 | |||
d38a225c78 | |||
f9b5c897cf | |||
1a92aa6630 | |||
70e2dcc1b8 | |||
7b6fb9e9bc | |||
03c7150b6b | |||
83cd25d5a2 | |||
04d60e6a95 | |||
549719ede6 | |||
855663eea9 | |||
4d98915dbd | |||
f461f16bfd | |||
6beadb8cfc | |||
360c8a618b | |||
c03854a9fc | |||
cebd903c9b | |||
e7cf5b7694 | |||
839bd138c1 | |||
e1c721d511 | |||
7ce3f83d54 | |||
2e0c6400e8 | |||
58637ed90e | |||
489fa237b9 | |||
4449c82611 | |||
fe31a779e4 | |||
9b96c22d6a | |||
262891ccf3 | |||
0d60b29ff7 | |||
ecca2a9f32 | |||
3643e5e753 | |||
4cba418f13 | |||
a966b0735c | |||
6fed2cdc5c | |||
9fd6bbfc89 | |||
e7a7e1c903 | |||
5e5f4003e0 | |||
23c03fd623 | |||
cab2d8aa2e | |||
1f57f1397c | |||
8a9c2c1505 | |||
b003da7f59 | |||
735890bc3d | |||
69035f49c8 | |||
5ec20ee526 | |||
8b811ffd6b | |||
35df3697c7 | |||
8b4befc828 | |||
77dddd9157 | |||
737f413324 | |||
e613937c43 | |||
9c66752f8b | |||
5c6922c710 | |||
c8e4343ac7 | |||
924bc2c5a7 | |||
2274afcd38 | |||
23aab2adf8 | |||
90311ad65e | |||
407e1383f8 | |||
ad106909e2 | |||
b346da01f1 | |||
51fedb270b | |||
fd26b48ff6 | |||
1bfe10691a | |||
bf81c34dbc | |||
f837bb5230 | |||
1b76e6882f | |||
83b43f501d | |||
9b626a562d | |||
c216f97bfd | |||
71453877d7 | |||
d1a601d006 | |||
0a9236a605 | |||
3a384307ee | |||
c215356b31 | |||
96f37dd470 | |||
10fac39d30 | |||
912572cba5 | |||
b001ebaab8 | |||
aa15da6b92 | |||
f144f27daa | |||
a58c9a0541 | |||
649db1059c |
@ -12,20 +12,12 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
@ -36,6 +28,7 @@ auditProductionDependencies:
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
@ -96,10 +89,9 @@ codequality:
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
@ -119,11 +111,10 @@ trigger:
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci node install stable
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
84
changelog.md
Normal file
84
changelog.md
Normal file
@ -0,0 +1,84 @@
|
||||
# Changelog
|
||||
|
||||
## 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.
|
@ -2,17 +2,33 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "apiglobal",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "api.global",
|
||||
"gitrepo": "typedrequest",
|
||||
"shortDescription": "make typed requests towards apis",
|
||||
"npmPackagename": "@apiglobal/typedrequest",
|
||||
"description": "A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.",
|
||||
"npmPackagename": "@api.global/typedrequest",
|
||||
"license": "MIT",
|
||||
"projectDomain": "api.global"
|
||||
"projectDomain": "api.global",
|
||||
"keywords": [
|
||||
"TypeScript",
|
||||
"API Requests",
|
||||
"HTTP",
|
||||
"Typed Programming",
|
||||
"Request Routing",
|
||||
"Response Handling",
|
||||
"Virtual Streams",
|
||||
"Asynchronous",
|
||||
"Network Communication",
|
||||
"Server Communication",
|
||||
"API Integration"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
14133
package-lock.json
generated
14133
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
51
package.json
51
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@apiglobal/typedrequest",
|
||||
"version": "2.0.10",
|
||||
"name": "@api.global/typedrequest",
|
||||
"version": "3.1.8",
|
||||
"private": false,
|
||||
"description": "make typed requests towards apis",
|
||||
"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",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
@ -11,24 +11,28 @@
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"format": "(gitzone format)"
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.65",
|
||||
"@gitzone/tsbundle": "^2.0.7",
|
||||
"@gitzone/tstest": "^1.0.73",
|
||||
"@pushrocks/smartenv": "^5.0.3",
|
||||
"@pushrocks/smartexpress": "^4.0.21",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.7.6"
|
||||
"@api.global/typedserver": "^3.0.51",
|
||||
"@git.zone/tsbuild": "^2.1.84",
|
||||
"@git.zone/tsbundle": "^2.0.15",
|
||||
"@git.zone/tsrun": "^1.2.49",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/smartenv": "^5.0.12",
|
||||
"@push.rocks/tapbundle": "^5.3.0",
|
||||
"@types/node": "^22.7.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isounique": "^1.0.5",
|
||||
"@pushrocks/lik": "^6.0.0",
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/webrequest": "^3.0.13"
|
||||
"@api.global/typedrequest-interfaces": "^3.0.19",
|
||||
"@push.rocks/isounique": "^1.0.5",
|
||||
"@push.rocks/lik": "^6.1.0",
|
||||
"@push.rocks/smartbuffer": "^3.0.4",
|
||||
"@push.rocks/smartdelay": "^3.0.5",
|
||||
"@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/**/*",
|
||||
@ -44,5 +48,18 @@
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"keywords": [
|
||||
"TypeScript",
|
||||
"API Requests",
|
||||
"HTTP",
|
||||
"Typed Programming",
|
||||
"Request Routing",
|
||||
"Response Handling",
|
||||
"Virtual Streams",
|
||||
"Asynchronous",
|
||||
"Network Communication",
|
||||
"Server Communication",
|
||||
"API Integration"
|
||||
]
|
||||
}
|
||||
|
6917
pnpm-lock.yaml
generated
Normal file
6917
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
214
readme.md
214
readme.md
@ -1,109 +1,135 @@
|
||||
# @apiglobal/typedrequest
|
||||
make typed requests towards apis
|
||||
# @api.global/typedrequest
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiglobal/typedrequest)
|
||||
* [gitlab.com (source)](https://gitlab.com/apiglobal/typedrequest)
|
||||
* [github.com (source mirror)](https://github.com/apiglobal/typedrequest)
|
||||
* [docs (typedoc)](https://apiglobal.gitlab.io/typedrequest/)
|
||||
A TypeScript library for making typed requests towards APIs, providing interfaces and classes to handle request/response cycles using typed definitions. It supports building and handling requests, routing based on request types, and virtual stream handling over networks.
|
||||
|
||||
## Status for master
|
||||
## Install
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
To install `@api.global/typedrequest`, you can use npm or yarn. Run the following command in your project directory:
|
||||
|
||||
```
|
||||
npm install @api.global/typedrequest --save
|
||||
```
|
||||
|
||||
Or, if you prefer yarn:
|
||||
|
||||
```
|
||||
yarn add @api.global/typedrequest
|
||||
```
|
||||
|
||||
This package is designed to facilitate making typed requests to APIs, ensuring that both requests and responses adhere to predefined interfaces. This enhances the predictability and reliability of network communication in applications that interact with APIs.
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class intellisense.
|
||||
Utilizing `@api.global/typedrequest` involves several core concepts, including the creation of typed requests, handling virtual streams, defining request handlers, and routing requests based on their type. Below are comprehensive examples and explanations of how to use these features in your applications.
|
||||
|
||||
### Setting Up a Typed Request
|
||||
|
||||
The foundation of `@api.global/typedrequest` is the `TypedRequest` class, which allows you to define requests that are strictly typed. This ensures that both the request sent and the response received match the expected structures.
|
||||
|
||||
First, define an interface that represents your request and response data structure:
|
||||
|
||||
```typescript
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
|
||||
import * as typedrequest from '../ts/index';
|
||||
|
||||
let testServer: smartexpress.Server;
|
||||
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
|
||||
|
||||
// lets define an interface
|
||||
interface ITestReqRes {
|
||||
method: 'hi';
|
||||
request: {
|
||||
name: string;
|
||||
};
|
||||
response: {
|
||||
surname: string;
|
||||
};
|
||||
// Define an interface for your request/response structure
|
||||
interface IUserRequest {
|
||||
method: 'getUser';
|
||||
request: { userId: string };
|
||||
response: { username: string; email: string; };
|
||||
}
|
||||
|
||||
tap.test('should create a typedHandler', async () => {
|
||||
// lets use the interface in a TypedHandler
|
||||
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
|
||||
return {
|
||||
surname: 'wow',
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should spawn a server to test with', async () => {
|
||||
testServer = new smartexpress.Server({
|
||||
cors: true,
|
||||
forceSsl: false,
|
||||
port: 3000,
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should define a testHandler', async () => {
|
||||
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
|
||||
testTypedRouter.addTypedHandler(testTypedHandler);
|
||||
testServer.addRoute(
|
||||
'/testroute',
|
||||
new smartexpress.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
|
||||
);
|
||||
});
|
||||
|
||||
tap.test('should start the server', async () => {
|
||||
await testServer.start();
|
||||
});
|
||||
|
||||
tap.test('should fire a request', async () => {
|
||||
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
|
||||
'http://localhost:3000/testroute',
|
||||
'hi'
|
||||
);
|
||||
const response = await typedRequest.fire({
|
||||
name: 'really',
|
||||
});
|
||||
console.log('this is the response:');
|
||||
console.log(response);
|
||||
expect(response.surname).to.equal('wow');
|
||||
});
|
||||
|
||||
tap.test('should end the server', async () => {
|
||||
await testServer.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
```
|
||||
|
||||
## Contribution
|
||||
Next, create an instance of `TypedRequest` using this interface, and use it to make an API call:
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
```typescript
|
||||
import { TypedRequest } from '@api.global/typedrequest';
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
// Construct a TypedRequest instance for fetching user data
|
||||
const getUserRequest = new TypedRequest<IUserRequest>('https://your-api.com/users', 'getUser');
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
// Execute the request with a specific userId and log the response
|
||||
const userResponse = await getUserRequest.fire({ userId: 'user-123' });
|
||||
console.log(userResponse.username);
|
||||
```
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
### Handling Virtual Streams
|
||||
|
||||
`@api.global/typedrequest` introduces the concept of virtual streams, allowing you to manage real-time data transfer over the network as streams. You can create a virtual stream, send data, and read data from it as shown below:
|
||||
|
||||
```typescript
|
||||
import { VirtualStream } from '@api.global/typedrequest';
|
||||
|
||||
// Instantiate a VirtualStream for handling real-time data
|
||||
const myStream = new VirtualStream<ArrayBufferLike>();
|
||||
|
||||
// Use the stream to send data
|
||||
await myStream.sendData(new TextEncoder().encode("Hello, World!"));
|
||||
|
||||
// Retrieve data from the stream
|
||||
const receivedData = await myStream.fetchData();
|
||||
console.log(new TextDecoder().decode(receivedData));
|
||||
```
|
||||
|
||||
### Defining and Using Handlers
|
||||
|
||||
To process requests, you can define handlers that are responsible for specific types of requests:
|
||||
|
||||
```typescript
|
||||
import { TypedHandler } from '@api.global/typedrequest';
|
||||
|
||||
interface IAdditionRequest {
|
||||
method: 'add';
|
||||
request: { a: number; b: number; };
|
||||
response: { result: number; };
|
||||
}
|
||||
|
||||
// Create a handler for processing addition requests
|
||||
const additionHandler = new TypedHandler<IAdditionRequest>('add', async (req) => {
|
||||
return { result: req.a + req.b };
|
||||
});
|
||||
```
|
||||
|
||||
### Routing Requests
|
||||
|
||||
`TypedRouter` is used to route requests to their appropriate handlers based on the request type:
|
||||
|
||||
```typescript
|
||||
import { TypedRouter } from '@api.global/typedrequest';
|
||||
|
||||
const router = new TypedRouter();
|
||||
|
||||
// Associate the additionHandler with the router
|
||||
router.addTypedHandler(additionHandler);
|
||||
|
||||
// The router now directs 'add' requests to the additionHandler
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
`@api.global/typedrequest` provides a `TypedResponseError` class to facilitate error handling in typed requests:
|
||||
|
||||
```typescript
|
||||
import { TypedResponseError } from '@api.global/typedrequest';
|
||||
|
||||
// Throw a TypedResponseError with details about the error
|
||||
throw new TypedResponseError('An error occurred', { detail: 'Error details' });
|
||||
```
|
||||
|
||||
This comprehensive overview covers the essential features provided by `@api.global/typedrequest`. By adhering to defined request and response structures, you can create more reliable and maintainable applications that interact seamlessly with APIs.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
import * as typedrequest from '../ts/index.js';
|
||||
|
||||
@ -29,11 +29,19 @@ tap.test('should define a testHandler', async () => {
|
||||
testTypedRouter.addTypedHandler(testTypedHandler);
|
||||
});
|
||||
|
||||
tap.test('should fire a request', async () => {
|
||||
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
|
||||
'http://localhost:3000/testroute',
|
||||
'hi'
|
||||
);
|
||||
tap.skip.test('should fire a request', async () => {
|
||||
let response = await fetch('http://localhost:3000/typedrequest', {
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"body": "{\"correlation\":{\"id\":\"uni_aefe56c1a0f61a3e91082209\",\"phase\":\"request\"},\"method\":\"hi\",\"request\":{\"name\":\"yes\"},\"response\":null}"
|
||||
})
|
||||
console.log(await response.text());
|
||||
});
|
||||
|
||||
tap.start();
|
||||
tap.skip.test('test', async (tools) => {
|
||||
await tools.delayFor(5000);
|
||||
})
|
||||
|
||||
export default tap.start();
|
||||
|
62
test/test.ts
62
test/test.ts
@ -1,9 +1,11 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as typedserver from '@api.global/typedserver';
|
||||
|
||||
import * as typedrequest from '../ts/index.js';
|
||||
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||
|
||||
let testServer: smartexpress.Server;
|
||||
let testServer: typedserver.servertools.Server;
|
||||
let testTypedRouter: typedrequest.TypedRouter;
|
||||
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
|
||||
|
||||
// lets define an interface
|
||||
@ -17,6 +19,16 @@ interface ITestReqRes {
|
||||
};
|
||||
}
|
||||
|
||||
interface ITestStream {
|
||||
method: 'handleStream';
|
||||
request: {
|
||||
requestStream: typedrequestInterfaces.IVirtualStream;
|
||||
};
|
||||
response: {
|
||||
responseStream: typedrequestInterfaces.IVirtualStream;
|
||||
};
|
||||
}
|
||||
|
||||
tap.test('should create a typedHandler', async () => {
|
||||
// lets use the interface in a TypedHandler
|
||||
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
|
||||
@ -27,7 +39,7 @@ tap.test('should create a typedHandler', async () => {
|
||||
});
|
||||
|
||||
tap.test('should spawn a server to test with', async () => {
|
||||
testServer = new smartexpress.Server({
|
||||
testServer = new typedserver.servertools.Server({
|
||||
cors: true,
|
||||
forceSsl: false,
|
||||
port: 3000,
|
||||
@ -35,11 +47,11 @@ tap.test('should spawn a server to test with', async () => {
|
||||
});
|
||||
|
||||
tap.test('should define a testHandler', async () => {
|
||||
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
|
||||
testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
|
||||
testTypedRouter.addTypedHandler(testTypedHandler);
|
||||
testServer.addRoute(
|
||||
'/testroute',
|
||||
new smartexpress.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
|
||||
new typedserver.servertools.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
|
||||
);
|
||||
});
|
||||
|
||||
@ -60,8 +72,40 @@ tap.test('should fire a request', async () => {
|
||||
expect(response.surname).toEqual('wow');
|
||||
});
|
||||
|
||||
tap.test('should end the server', async () => {
|
||||
await testServer.stop();
|
||||
tap.test('should allow VirtualStreams', async () => {
|
||||
const newRequestingVS = new typedrequest.VirtualStream();
|
||||
const newRespondingVS = new typedrequest.VirtualStream();
|
||||
let generatedRequestingVS: typedrequestInterfaces.IVirtualStream;
|
||||
let generatedRespondingVS: typedrequestInterfaces.IVirtualStream;
|
||||
testTypedRouter.addTypedHandler(new typedrequest.TypedHandler<ITestStream>('handleStream', async (reqArg) => {
|
||||
console.log('hey there');
|
||||
console.log(reqArg.requestStream);
|
||||
generatedRequestingVS = reqArg.requestStream;
|
||||
return {
|
||||
responseStream: newRespondingVS,
|
||||
};
|
||||
}));
|
||||
const typedRequest = new typedrequest.TypedRequest<ITestStream>(
|
||||
'http://localhost:3000/testroute',
|
||||
'handleStream'
|
||||
);
|
||||
const response = await typedRequest.fire({
|
||||
requestStream: newRequestingVS,
|
||||
});
|
||||
console.log(response.responseStream);
|
||||
|
||||
newRequestingVS.sendData(Buffer.from('hello'));
|
||||
const data = await generatedRequestingVS.fetchData();
|
||||
const decodedData = new TextDecoder().decode(data);
|
||||
expect(decodedData).toEqual('hello');
|
||||
await newRequestingVS.close();
|
||||
await newRespondingVS.close();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
tap.test('should end the server', async (toolsArg) => {
|
||||
await toolsArg.delayFor(10000);
|
||||
await testServer.stop();
|
||||
await tap.stopForcefully();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiglobal/typedrequest',
|
||||
version: '2.0.10',
|
||||
description: 'make typed requests towards apis'
|
||||
name: '@api.global/typedrequest',
|
||||
version: '3.1.8',
|
||||
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
|
||||
import * as plugins from './plugins.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,8 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
);
|
||||
}
|
||||
let typedResponseError: TypedResponseError;
|
||||
const response = await this.handlerFunction(typedRequestArg.request).catch((e) => {
|
||||
const typedtoolsInstance = new TypedTools();
|
||||
const response = await this.handlerFunction(typedRequestArg.request, typedtoolsInstance).catch((e) => {
|
||||
if (e instanceof TypedResponseError) {
|
||||
typedResponseError = e;
|
||||
} else {
|
103
ts/classes.typedrequest.ts
Normal file
103
ts/classes.typedrequest.ts
Normal file
@ -0,0 +1,103 @@
|
||||
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 { TypedTarget } from './classes.typedtarget.js';
|
||||
|
||||
const webrequestInstance = new plugins.webrequest.WebRequest();
|
||||
|
||||
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
/**
|
||||
* in case we post against a url endpoint
|
||||
*/
|
||||
public urlEndPoint?: string;
|
||||
|
||||
/**
|
||||
* in case we post against a TypedTarget
|
||||
*/
|
||||
typedTarget: TypedTarget;
|
||||
|
||||
public method: string;
|
||||
|
||||
/**
|
||||
* @param postEndPointArg
|
||||
* @param methodArg
|
||||
*/
|
||||
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
|
||||
if (typeof postTarget === 'string') {
|
||||
this.urlEndPoint = postTarget;
|
||||
} else {
|
||||
this.typedTarget = postTarget;
|
||||
}
|
||||
this.method = methodArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* fires the request
|
||||
*/
|
||||
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
|
||||
let payloadSending: plugins.typedRequestInterfaces.ITypedRequest = {
|
||||
method: this.method,
|
||||
request: fireArg,
|
||||
response: null,
|
||||
correlation: {
|
||||
id: plugins.isounique.uni(),
|
||||
phase: 'request',
|
||||
},
|
||||
};
|
||||
|
||||
// lets preprocess the payload
|
||||
payloadSending = VirtualStream.encodePayloadForNetwork(payloadSending, {
|
||||
sendMethod: (payloadArg: plugins.typedRequestInterfaces.IStreamRequest) => {
|
||||
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
|
||||
}
|
||||
});
|
||||
|
||||
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
payloadReceiving = await this.postTrObject(payloadSending, useCacheArg);
|
||||
|
||||
// lets preprocess the response
|
||||
payloadReceiving = VirtualStream.decodePayloadFromNetwork(payloadReceiving, {
|
||||
sendMethod: (payloadArg: plugins.typedRequestInterfaces.IStreamRequest) => {
|
||||
return this.postTrObject(payloadArg) as Promise<plugins.typedRequestInterfaces.IStreamRequest>;
|
||||
}
|
||||
});
|
||||
return payloadReceiving.response;
|
||||
}
|
||||
|
||||
private async postTrObject(payloadSendingArg: plugins.typedRequestInterfaces.ITypedRequest, useCacheArg: boolean = false) {
|
||||
let payloadReceiving: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
if (this.urlEndPoint) {
|
||||
const response = await webrequestInstance.postJson(
|
||||
this.urlEndPoint,
|
||||
payloadSendingArg,
|
||||
useCacheArg
|
||||
);
|
||||
payloadReceiving = response;
|
||||
} else {
|
||||
payloadReceiving = await this.typedTarget.post(payloadSendingArg);
|
||||
}
|
||||
if (payloadReceiving.error) {
|
||||
console.error(
|
||||
`method: >>${this.method}<< got an ERROR: "${payloadReceiving.error.text}" with data ${JSON.stringify(
|
||||
payloadReceiving.error.data,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
);
|
||||
if (!payloadReceiving.retry) {
|
||||
throw new TypedResponseError(payloadReceiving.error.text, payloadReceiving.error.data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (payloadReceiving.retry) {
|
||||
console.log(
|
||||
`server requested retry for the following reason: ${payloadReceiving.retry.reason}`
|
||||
);
|
||||
await plugins.smartdelay.delayFor(payloadReceiving.retry.waitForMs);
|
||||
// tslint:disable-next-line: no-return-await
|
||||
payloadReceiving = await this.postTrObject(payloadSendingArg, useCacheArg);
|
||||
}
|
||||
return payloadReceiving;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export class TypedResponseError {
|
||||
public errorText: string;
|
@ -1,7 +1,8 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import * as plugins from './plugins.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';
|
||||
|
||||
/**
|
||||
* A typed router decides on which typed handler to call based on the method
|
||||
@ -13,6 +14,7 @@ export class TypedRouter {
|
||||
public handlerMap = new plugins.lik.ObjectMap<
|
||||
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
|
||||
>();
|
||||
public registeredVirtualStreams = new plugins.lik.ObjectMap<VirtualStream<any>>();
|
||||
|
||||
public fireEventInterestMap = new plugins.lik.InterestMap<
|
||||
string,
|
||||
@ -23,7 +25,7 @@ export class TypedRouter {
|
||||
* adds the handler to the routing map
|
||||
* @param typedHandlerArg
|
||||
*/
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest> (
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(
|
||||
typedHandlerArg: TypedHandler<T>
|
||||
) {
|
||||
// lets check for deduplication
|
||||
@ -41,15 +43,15 @@ export class TypedRouter {
|
||||
* adds another sub typedRouter
|
||||
* @param typedRequest
|
||||
*/
|
||||
public addTypedRouter (typedRouterArg: TypedRouter) {
|
||||
const routerExists = this.routerMap.findSync(routerArg => routerArg === typedRouterArg)
|
||||
public addTypedRouter(typedRouterArg: TypedRouter) {
|
||||
const routerExists = this.routerMap.findSync((routerArg) => routerArg === typedRouterArg);
|
||||
if (!routerExists) {
|
||||
this.routerMap.add(typedRouterArg);
|
||||
typedRouterArg.addTypedRouter(this);
|
||||
}
|
||||
}
|
||||
|
||||
public checkForTypedHandler (methodArg: string): boolean {
|
||||
public checkForTypedHandler(methodArg: string): boolean {
|
||||
return !!this.getTypedHandlerForMethod(methodArg);
|
||||
}
|
||||
|
||||
@ -58,7 +60,7 @@ export class TypedRouter {
|
||||
* @param methodArg
|
||||
* @param checkUpstreamRouter
|
||||
*/
|
||||
public getTypedHandlerForMethod (
|
||||
public getTypedHandlerForMethod(
|
||||
methodArg: string,
|
||||
checkedRouters: TypedRouter[] = []
|
||||
): TypedHandler<any> {
|
||||
@ -86,8 +88,27 @@ export class TypedRouter {
|
||||
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
|
||||
* @param typedRequestArg
|
||||
*/
|
||||
public async routeAndAddResponse<T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest> (typedRequestArg: T): Promise<T> {
|
||||
if (typedRequestArg?.correlation?.phase === 'request') {
|
||||
public async routeAndAddResponse<
|
||||
T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest
|
||||
>(typedRequestArg: T, localRequestArg = false): Promise<T> {
|
||||
// decoding first
|
||||
typedRequestArg = VirtualStream.decodePayloadFromNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
|
||||
// localdata second
|
||||
typedRequestArg.localData = typedRequestArg.localData || {};
|
||||
typedRequestArg.localData.firstTypedrouter = this;
|
||||
|
||||
// lets do stream processing
|
||||
if (typedRequestArg.method === '##VirtualStream##') {
|
||||
const result: any = await this.handleStreamTypedRequest(typedRequestArg as plugins.typedRequestInterfaces.IStreamRequest);
|
||||
result.localData = null;
|
||||
return result as T;
|
||||
}
|
||||
|
||||
// lets do normal routing
|
||||
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
@ -97,10 +118,21 @@ export class TypedRouter {
|
||||
data: {},
|
||||
};
|
||||
typedRequestArg.correlation.phase = 'response';
|
||||
|
||||
// encode again before handing back
|
||||
typedRequestArg.localData = null;
|
||||
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
||||
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
||||
typedRequestArg.localData = null;
|
||||
// encode again before handing back
|
||||
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
|
||||
typedrouter: this,
|
||||
});
|
||||
return typedRequestArg;
|
||||
} else if (typedRequestArg?.correlation?.phase === 'response') {
|
||||
this.fireEventInterestMap
|
||||
@ -110,7 +142,26 @@ export class TypedRouter {
|
||||
} else {
|
||||
console.log('received weirdly shaped request');
|
||||
console.log(typedRequestArg);
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handle streaming
|
||||
* @param streamTrArg
|
||||
*/
|
||||
public async handleStreamTypedRequest(streamTrArg: plugins.typedRequestInterfaces.IStreamRequest) {
|
||||
const relevantVirtualStream = await this.registeredVirtualStreams.find(async virtualStreamArg => {
|
||||
return virtualStreamArg.streamId === streamTrArg.request.streamId;
|
||||
});
|
||||
if (!relevantVirtualStream) {
|
||||
console.log(`no relevant virtual stream found for stream with id ${streamTrArg.request.streamId}`);
|
||||
console.log(this.registeredVirtualStreams.getArray());
|
||||
return streamTrArg;
|
||||
} else {
|
||||
console.log(`success: found relevant virtual stream with id ${streamTrArg.request.streamId}`);
|
||||
}
|
||||
const result = await relevantVirtualStream.handleStreamTr(streamTrArg);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import { TypedRouter } from './classes.typedrouter.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export type IPostMethod = (
|
||||
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
|
||||
@ -52,7 +52,9 @@ export class TypedTarget {
|
||||
*/
|
||||
public isAsync: boolean;
|
||||
|
||||
public async post<T extends plugins.typedRequestInterfaces.ITypedRequest>(payloadArg: T): Promise<T> {
|
||||
public async post<T extends plugins.typedRequestInterfaces.ITypedRequest>(
|
||||
payloadArg: T
|
||||
): Promise<T> {
|
||||
let responseInterest: plugins.lik.Interest<
|
||||
string,
|
||||
plugins.typedRequestInterfaces.ITypedRequest
|
13
ts/classes.typedtools.ts
Normal file
13
ts/classes.typedtools.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { TypedResponseError } from './classes.typedresponseerror.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export class TypedTools {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
425
ts/classes.virtualstream.ts
Normal file
425
ts/classes.virtualstream.ts
Normal file
@ -0,0 +1,425 @@
|
||||
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
|
||||
) => Promise<plugins.typedRequestInterfaces.IStreamRequest>;
|
||||
typedrouter?: TypedRouter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. A VirtualStream connects over the network
|
||||
* 2. It is always paired to one other VirtualStream
|
||||
* on the other side with the same streamId.
|
||||
* 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 = Uint8Array> implements plugins.typedRequestInterfaces.IVirtualStream<T> {
|
||||
// STATIC
|
||||
public static encodePayloadForNetwork(
|
||||
objectPayload: any,
|
||||
commFunctions: ICommFunctions,
|
||||
originalPayload?: any,
|
||||
path = []
|
||||
): any {
|
||||
if (!objectPayload) {
|
||||
return objectPayload;
|
||||
}
|
||||
if (plugins.smartbuffer.isBufferLike(objectPayload)) {
|
||||
return objectPayload;
|
||||
}
|
||||
if (objectPayload instanceof VirtualStream) {
|
||||
if (!objectPayload.side && commFunctions.sendMethod) {
|
||||
objectPayload.side = 'requesting';
|
||||
objectPayload.sendMethod = commFunctions.sendMethod;
|
||||
}
|
||||
if (!objectPayload.side && commFunctions.typedrouter) {
|
||||
objectPayload.side = 'responding';
|
||||
objectPayload.typedrouter = commFunctions.typedrouter;
|
||||
commFunctions.typedrouter.registeredVirtualStreams.add(objectPayload);
|
||||
}
|
||||
if (!originalPayload.response || path.includes('response')) {
|
||||
objectPayload.startKeepAliveLoop();
|
||||
return {
|
||||
_isVirtualStream: true,
|
||||
streamId: objectPayload.streamId,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
_OBMITTED_VIRTUAL_STREAM: true,
|
||||
reason: 'path is under .request: obmitted for deduplication reasons in response cycle.',
|
||||
};
|
||||
}
|
||||
} else if (Array.isArray(objectPayload)) {
|
||||
// For arrays, we recurse over each item.
|
||||
return objectPayload.map((item, index) =>
|
||||
VirtualStream.encodePayloadForNetwork(
|
||||
item,
|
||||
commFunctions,
|
||||
originalPayload || objectPayload,
|
||||
path.concat(String(index)) // Convert index to string and concatenate to path
|
||||
)
|
||||
);
|
||||
} else if (objectPayload !== null && typeof objectPayload === 'object') {
|
||||
// For objects, we recurse over each key-value pair.
|
||||
return Object.entries(objectPayload).reduce((acc, [key, value]) => {
|
||||
const newPath = path.concat(key); // Concatenate the new key to the path
|
||||
acc[key] = VirtualStream.encodePayloadForNetwork(
|
||||
value,
|
||||
commFunctions,
|
||||
originalPayload || objectPayload,
|
||||
newPath
|
||||
);
|
||||
return acc;
|
||||
}, {});
|
||||
} else {
|
||||
return objectPayload;
|
||||
}
|
||||
}
|
||||
|
||||
public static decodePayloadFromNetwork(objectPayload: any, commFunctions: ICommFunctions): any {
|
||||
|
||||
if (
|
||||
plugins.smartbuffer.isBufferLike(objectPayload)
|
||||
|| objectPayload instanceof TypedRouter
|
||||
) {
|
||||
return objectPayload;
|
||||
}
|
||||
if (objectPayload !== null && typeof objectPayload === 'object') {
|
||||
if (objectPayload._isVirtualStream) {
|
||||
const virtualStream = new VirtualStream();
|
||||
virtualStream.streamId = objectPayload.streamId;
|
||||
if (!virtualStream.side && commFunctions.sendMethod) {
|
||||
virtualStream.side = 'requesting';
|
||||
virtualStream.sendMethod = commFunctions.sendMethod;
|
||||
}
|
||||
if (!virtualStream.side && commFunctions.typedrouter) {
|
||||
virtualStream.side = 'responding';
|
||||
virtualStream.typedrouter = commFunctions.typedrouter;
|
||||
commFunctions.typedrouter.registeredVirtualStreams.add(virtualStream);
|
||||
}
|
||||
virtualStream.startKeepAliveLoop();
|
||||
return virtualStream;
|
||||
} else if (Array.isArray(objectPayload)) {
|
||||
const returnArray = [];
|
||||
for (const item of objectPayload) {
|
||||
returnArray.push(VirtualStream.decodePayloadFromNetwork(item, commFunctions));
|
||||
}
|
||||
return returnArray;
|
||||
} else {
|
||||
return Object.keys(objectPayload).reduce((acc, key) => {
|
||||
acc[key] = VirtualStream.decodePayloadFromNetwork(objectPayload[key], commFunctions);
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
} else {
|
||||
return objectPayload;
|
||||
}
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
|
||||
public side: 'requesting' | 'responding';
|
||||
public streamId: string = plugins.isounique.uni();
|
||||
|
||||
// integration with typedrequest mechanics
|
||||
public sendMethod: ICommFunctions['sendMethod'];
|
||||
public typedrouter: TypedRouter;
|
||||
|
||||
// wether to keep the stream alive
|
||||
private keepAlive = true;
|
||||
private lastKeepAliveEvent: number;
|
||||
|
||||
// backpressured arrays
|
||||
private sendBackpressuredArray =
|
||||
new plugins.lik.BackpressuredArray<T>(
|
||||
16
|
||||
);
|
||||
private receiveBackpressuredArray =
|
||||
new plugins.lik.BackpressuredArray<T>(
|
||||
16
|
||||
);
|
||||
|
||||
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;
|
||||
let otherSideIsBackpressured = false;
|
||||
|
||||
// helper functions
|
||||
const getFeedback = async () => {
|
||||
const streamTr = await this.sendMethod({
|
||||
method: '##VirtualStream##',
|
||||
request: {
|
||||
streamId: this.streamId,
|
||||
cycleId: plugins.isounique.uni(),
|
||||
cycle: 'request',
|
||||
mainPurpose: 'feedback',
|
||||
next: this.sendBackpressuredArray.data.length > 0,
|
||||
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
|
||||
},
|
||||
response: null,
|
||||
}).catch(() => {
|
||||
console.log('stream ended immaturely');
|
||||
this.keepAlive = false;
|
||||
});
|
||||
if (streamTr && streamTr.response) {
|
||||
otherSideIsBackpressured = streamTr.response.backpressure
|
||||
otherSideHasNext = streamTr.response.next;
|
||||
}
|
||||
}
|
||||
await getFeedback();
|
||||
|
||||
// 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();
|
||||
}
|
||||
let streamTr: plugins.typedRequestInterfaces.IStreamRequest;
|
||||
streamTr = await this.sendMethod({
|
||||
method: '##VirtualStream##',
|
||||
request: {
|
||||
streamId: this.streamId,
|
||||
cycleId: plugins.isounique.uni(),
|
||||
cycle: 'request',
|
||||
mainPurpose: dataArg ? 'chunk' : 'read',
|
||||
backpressure: thisSideIsBackpressured,
|
||||
next: this.sendBackpressuredArray.data.length > 0,
|
||||
...dataArg ? { chunkData: dataArg } : {},
|
||||
},
|
||||
response: null,
|
||||
}).catch(() => {
|
||||
console.log('stream ended immaturely');
|
||||
this.keepAlive = false;
|
||||
return null;
|
||||
});
|
||||
|
||||
if (streamTr && streamTr.response && streamTr.response.chunkData) {
|
||||
this.receiveBackpressuredArray.push(streamTr.response.chunkData);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method handles the stream only on the responding side
|
||||
* @param streamTrArg
|
||||
* @returns
|
||||
*/
|
||||
public async handleStreamTr(streamTrArg: plugins.typedRequestInterfaces.IStreamRequest) {
|
||||
if (streamTrArg.request.keepAlive === true && this.keepAlive === true) {
|
||||
this.lastKeepAliveEvent = Date.now();
|
||||
} else if (streamTrArg.request.keepAlive === false) {
|
||||
this.keepAlive = false;
|
||||
}
|
||||
|
||||
// keepAlive handling
|
||||
if (streamTrArg.request.mainPurpose === 'keepAlive') {
|
||||
// if the main purpose is keepAlive, we answer with a keepAlive
|
||||
streamTrArg.response = {
|
||||
streamId: this.streamId,
|
||||
cycleId: streamTrArg.request.cycleId,
|
||||
cycle: 'response',
|
||||
mainPurpose: 'keepAlive',
|
||||
keepAlive: this.keepAlive,
|
||||
next: this.sendBackpressuredArray.data.length > 0,
|
||||
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
|
||||
};
|
||||
}
|
||||
|
||||
// feedback handling
|
||||
if (streamTrArg.request.mainPurpose === 'feedback') {
|
||||
streamTrArg.response = {
|
||||
streamId: this.streamId,
|
||||
cycleId: streamTrArg.request.cycleId,
|
||||
cycle: 'response',
|
||||
mainPurpose: 'feedback',
|
||||
next: this.sendBackpressuredArray.data.length > 0,
|
||||
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
|
||||
};
|
||||
}
|
||||
|
||||
// chunk handling
|
||||
if (streamTrArg.request.mainPurpose === 'chunk') {
|
||||
this.receiveBackpressuredArray.push(streamTrArg.request.chunkData);
|
||||
if (this.sendBackpressuredArray.data.length > 0 && streamTrArg.response.backpressure === false) {
|
||||
const dataArg = this.sendBackpressuredArray.shift();
|
||||
streamTrArg.response = {
|
||||
streamId: this.streamId,
|
||||
cycleId: streamTrArg.request.cycleId,
|
||||
cycle: 'response',
|
||||
mainPurpose: 'chunk',
|
||||
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 {
|
||||
streamTrArg.response = {
|
||||
streamId: this.streamId,
|
||||
cycleId: streamTrArg.request.cycleId,
|
||||
cycle: 'response',
|
||||
mainPurpose: 'feedback',
|
||||
next: this.sendBackpressuredArray.data.length > 0,
|
||||
backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(),
|
||||
};
|
||||
}
|
||||
streamTrArg.request = null;
|
||||
}
|
||||
|
||||
return streamTrArg;
|
||||
}
|
||||
|
||||
// lifecycle methods
|
||||
/**
|
||||
* closes the virtual stream
|
||||
*/
|
||||
public async cleanup() {
|
||||
if (this.typedrouter) {
|
||||
this.typedrouter.registeredVirtualStreams.remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* a keepAlive loop that works across technologies
|
||||
*/
|
||||
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) {
|
||||
await this.triggerKeepAlive();
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
}
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
await this.cleanup();
|
||||
console.log(`cleaned up for stream ${this.streamId}`);
|
||||
}
|
||||
|
||||
private async triggerKeepAlive() {
|
||||
if (this.side === 'requesting') {
|
||||
console.log(`keepalive sent.`);
|
||||
const streamTr = await this.sendMethod({
|
||||
method: '##VirtualStream##',
|
||||
request: {
|
||||
streamId: this.streamId,
|
||||
cycleId: plugins.isounique.uni(),
|
||||
cycle: 'request',
|
||||
mainPurpose: 'keepAlive',
|
||||
keepAlive: this.keepAlive,
|
||||
},
|
||||
response: null,
|
||||
}).catch(() => {
|
||||
this.keepAlive = false;
|
||||
});
|
||||
|
||||
// lets handle keepAlive
|
||||
if (streamTr && streamTr.response && streamTr.response.keepAlive === false) {
|
||||
this.keepAlive = false;
|
||||
} else {
|
||||
this.lastKeepAliveEvent = Date.now();
|
||||
}
|
||||
if (streamTr && streamTr.response && streamTr.response.next) {
|
||||
this.workOnQueue();
|
||||
}
|
||||
}
|
||||
if (Date.now() - this.lastKeepAliveEvent > 10000) {
|
||||
console.log(`closing stream for ${this.streamId}`);
|
||||
this.keepAlive = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Data sending and receiving
|
||||
public async sendData(dataArg: T): Promise<void> {
|
||||
this.sendBackpressuredArray.push(dataArg);
|
||||
this.workOnQueue();
|
||||
await this.sendBackpressuredArray.waitForSpace();
|
||||
}
|
||||
|
||||
public async fetchData(): Promise<T> {
|
||||
if (this.receiveBackpressuredArray.hasSpace) {
|
||||
// do something maybe?
|
||||
}
|
||||
await this.receiveBackpressuredArray.waitForItems();
|
||||
const dataPackage = this.receiveBackpressuredArray.shift();
|
||||
return dataPackage;
|
||||
}
|
||||
|
||||
/**
|
||||
* reads from a Readable and sends it to the other side
|
||||
* @param readableStreamArg
|
||||
*/
|
||||
public async readFromWebstream(readableStreamArg: ReadableStream<T>, closeAfterReading = true) {
|
||||
const reader = readableStreamArg.getReader();
|
||||
let streamIsDone = false;
|
||||
while(!streamIsDone) {
|
||||
const { value, done } = await reader.read();
|
||||
if(value) {
|
||||
await this.sendData(value);
|
||||
}
|
||||
streamIsDone = done;
|
||||
}
|
||||
if (closeAfterReading) {
|
||||
await this.close(true);
|
||||
}
|
||||
}
|
||||
|
||||
public async writeToWebstream(writableStreamArg: WritableStream<T>) {
|
||||
const writer = writableStreamArg.getWriter();
|
||||
while(this.keepAlive || this.receiveBackpressuredArray.checkHasItems()) {
|
||||
const value = await this.fetchData();
|
||||
if (value === closingBit) {
|
||||
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;
|
||||
}
|
||||
}
|
11
ts/index.ts
11
ts/index.ts
@ -1,5 +1,6 @@
|
||||
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 './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';
|
15
ts/plugins.ts
Normal file
15
ts/plugins.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// apiglobal scope
|
||||
import * as typedRequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||
|
||||
export { typedRequestInterfaces };
|
||||
|
||||
// pushrocks scope
|
||||
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, smartguard, smartpromise, webrequest };
|
@ -1,77 +0,0 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
|
||||
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
|
||||
import { TypedTarget } from './typedrequest.classes.typedtarget.js';
|
||||
|
||||
const webrequestInstance = new plugins.webrequest.WebRequest();
|
||||
|
||||
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
|
||||
|
||||
/**
|
||||
* in case we post against a url endpoint
|
||||
*/
|
||||
public urlEndPoint?: string;
|
||||
|
||||
/**
|
||||
* in case we post against a TypedTarget
|
||||
*/
|
||||
typedTarget: TypedTarget;
|
||||
|
||||
public method: string;
|
||||
|
||||
/**
|
||||
* note the overloading is thought to deal with promises
|
||||
* @param postEndPointArg
|
||||
* @param methodArg
|
||||
*/
|
||||
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
|
||||
if (typeof postTarget === 'string') {
|
||||
this.urlEndPoint = postTarget;
|
||||
} else {
|
||||
this.typedTarget = postTarget;
|
||||
}
|
||||
this.method = methodArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* fires the request
|
||||
*/
|
||||
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
|
||||
const payload: plugins.typedRequestInterfaces.ITypedRequest = {
|
||||
method: this.method,
|
||||
request: fireArg,
|
||||
response: null,
|
||||
correlation: {
|
||||
id: plugins.isounique.uni(),
|
||||
phase: 'request',
|
||||
},
|
||||
};
|
||||
|
||||
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
if (this.urlEndPoint) {
|
||||
const response = await webrequestInstance.postJson(this.urlEndPoint, payload, useCacheArg);
|
||||
responseBody = response;
|
||||
} else {
|
||||
responseBody = await this.typedTarget.post(payload);
|
||||
}
|
||||
if (responseBody.error) {
|
||||
console.error(
|
||||
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
|
||||
responseBody.error.data
|
||||
)}`
|
||||
);
|
||||
if (!responseBody.retry) {
|
||||
throw new TypedResponseError(responseBody.error.text, responseBody.error.data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (responseBody.retry) {
|
||||
console.log(`server requested retry for the following reason: ${responseBody.retry.reason}`);
|
||||
await plugins.smartdelay.delayFor(responseBody.retry.waitForMs);
|
||||
// tslint:disable-next-line: no-return-await
|
||||
return await this.fire(fireArg);
|
||||
}
|
||||
return responseBody.response;
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
// apiglobal scope
|
||||
import * as typedRequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
||||
|
||||
export { typedRequestInterfaces };
|
||||
|
||||
// pushrocks scope
|
||||
import * as isounique from '@pushrocks/isounique';
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as webrequest from '@pushrocks/webrequest';
|
||||
|
||||
export { isounique, lik, smartdelay, smartpromise, webrequest };
|
@ -3,8 +3,12 @@
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user