Compare commits

...

64 Commits

Author SHA1 Message Date
ad106909e2 3.0.8 2024-02-25 01:54:01 +01:00
b346da01f1 fix(core): update 2024-02-25 01:54:01 +01:00
51fedb270b 3.0.7 2024-02-24 12:20:13 +01:00
fd26b48ff6 fix(core): update 2024-02-24 12:20:13 +01:00
1bfe10691a 3.0.6 2024-02-21 18:29:36 +01:00
bf81c34dbc fix(core): update 2024-02-21 18:29:35 +01:00
f837bb5230 3.0.5 2024-02-20 17:40:31 +01:00
1b76e6882f fix(core): update 2024-02-20 17:40:30 +01:00
83b43f501d 3.0.4 2023-12-08 19:36:25 +01:00
9b626a562d fix(core): update 2023-12-08 19:36:24 +01:00
c216f97bfd 3.0.3 2023-12-05 23:42:44 +01:00
71453877d7 fix(core): update 2023-12-05 23:42:43 +01:00
d1a601d006 3.0.2 2023-10-20 17:39:09 +02:00
0a9236a605 fix(core): update 2023-10-20 17:39:08 +02:00
3a384307ee 3.0.1 2023-08-04 16:55:25 +02:00
c215356b31 fix(core): update 2023-08-04 16:55:24 +02:00
96f37dd470 3.0.0 2023-08-04 16:10:48 +02:00
10fac39d30 BREAKING CHANGE(core): update 2023-08-04 16:10:47 +02:00
912572cba5 2.0.13 2022-10-26 14:23:24 +02:00
b001ebaab8 fix(core): update 2022-10-26 14:23:24 +02:00
aa15da6b92 2.0.12 2022-10-09 18:57:31 +02:00
f144f27daa fix(core): update 2022-10-09 18:57:31 +02:00
a58c9a0541 2.0.11 2022-10-09 18:52:50 +02:00
649db1059c fix(core): update 2022-10-09 18:52:49 +02:00
e0c75716d7 2.0.10 2022-08-19 17:59:47 +02:00
009985c226 fix(core): update 2022-08-19 17:59:47 +02:00
f3f2f8e3bf 2.0.9 2022-08-18 23:24:04 +02:00
cf304ceccd fix(core): update 2022-08-18 23:24:04 +02:00
6f075132c4 2.0.8 2022-07-29 04:48:34 +02:00
4dca98e81d fix(core): update 2022-07-29 04:48:34 +02:00
1e022d6c68 2.0.7 2022-05-29 21:43:37 +02:00
f20d737ecf fix(core): update 2022-05-29 21:43:37 +02:00
d791eca5e8 2.0.6 2022-05-29 21:01:57 +02:00
63c6dac8fa fix(core): update 2022-05-29 21:01:56 +02:00
c2c1dee427 2.0.5 2022-05-29 20:43:30 +02:00
4ae90a5cf6 fix(core): update 2022-05-29 20:43:29 +02:00
803d4d2894 2.0.4 2022-05-29 20:38:47 +02:00
fcc75af1ff fix(core): update 2022-05-29 20:38:46 +02:00
0e3bb07a69 2.0.3 2022-03-29 14:50:40 +02:00
c90aa07ace fix(core): update 2022-03-29 14:50:39 +02:00
362f3f1bd0 2.0.2 2022-03-29 10:50:27 +02:00
12f7348fec fix(core): update 2022-03-29 10:50:26 +02:00
7d478c400e 2.0.1 2022-03-29 09:48:47 +02:00
ab75cf8720 fix(core): update 2022-03-29 09:48:47 +02:00
f7ef8a6828 2.0.0 2022-03-24 21:56:03 +01:00
ece2803d12 1.0.66 2022-03-24 20:02:59 +01:00
2384fc1b76 fix(core): update 2022-03-24 20:02:58 +01:00
309c282379 1.0.65 2021-11-10 13:14:41 +01:00
90c616ca41 fix(core): update 2021-11-10 13:14:40 +01:00
57177074d0 1.0.64 2021-11-10 01:41:55 +01:00
d3b5c802cd fix(core): update 2021-11-10 01:41:55 +01:00
8e64353026 1.0.63 2021-11-07 23:46:33 +01:00
290746c191 fix(core): update 2021-11-07 23:46:32 +01:00
abefef8d7c 1.0.62 2021-11-07 15:45:18 +01:00
81b042e670 fix(core): update 2021-11-07 15:45:17 +01:00
6e3ee011a9 1.0.61 2021-11-07 15:40:02 +01:00
9b5ff4b1b5 fix(core): update 2021-11-07 15:40:01 +01:00
556ba6cb30 1.0.60 2021-11-07 15:26:13 +01:00
7321ac680d fix(core): update 2021-11-07 15:26:12 +01:00
2fd8219849 1.0.59 2021-11-07 14:56:48 +01:00
ea56e2218f fix(types): better types for TypedRouter.routeAndAddResponse 2021-11-07 14:56:48 +01:00
9a07817914 1.0.58 2021-09-27 12:15:47 +02:00
9bc83b0d1e 1.0.57 2021-09-27 12:14:43 +02:00
98c638e1ab fix(core): update 2021-09-27 12:14:43 +02:00
20 changed files with 6592 additions and 11471 deletions

View File

@ -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
View File

@ -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"
}
]
}

View File

@ -3,10 +3,10 @@
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "apiglobal",
"gitscope": "api.global",
"gitrepo": "typedrequest",
"shortDescription": "make typed requests towards apis",
"npmPackagename": "@apiglobal/typedrequest",
"description": "make typed requests towards apis",
"npmPackagename": "@api.global/typedrequest",
"license": "MIT",
"projectDomain": "api.global"
}

11301
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,34 +1,36 @@
{
"name": "@apiglobal/typedrequest",
"version": "1.0.56",
"name": "@api.global/typedrequest",
"version": "3.0.8",
"private": false,
"description": "make typed requests towards apis",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild --web && tsbundle npm)",
"format": "(gitzone format)"
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tsbundle": "^1.0.78",
"@gitzone/tstest": "^1.0.52",
"@pushrocks/smartexpress": "^3.0.76",
"@pushrocks/tapbundle": "^3.2.9",
"@types/node": "^14.11.8",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
"@api.global/typedserver": "^3.0.24",
"@git.zone/tsbuild": "^2.1.72",
"@git.zone/tsbundle": "^2.0.15",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.86",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.11.20"
},
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.15",
"@pushrocks/isounique": "^1.0.4",
"@pushrocks/lik": "^4.0.17",
"@pushrocks/smartdelay": "^2.0.10",
"@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/webrequest": "^2.0.12"
"@api.global/typedrequest-interfaces": "^3.0.17",
"@push.rocks/isounique": "^1.0.5",
"@push.rocks/lik": "^6.0.13",
"@push.rocks/smartbuffer": "^1.0.6",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/webrequest": "^3.0.34"
},
"files": [
"ts/**/*",

5969
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
import { expect, tap } from '@pushrocks/tapbundle';
import { expect, tap } from '@push.rocks/tapbundle';
import * as typedrequest from '../ts/index';
import * as typedrequest from '../ts/index.js';
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;

View File

@ -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';
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.
);
});
@ -57,10 +69,39 @@ tap.test('should fire a request', async () => {
});
console.log('this is the response:');
console.log(response);
expect(response.surname).to.equal('wow');
expect(response.surname).toEqual('wow');
});
tap.test('should end the server', async () => {
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 = data.toString();
expect(data.toString()).toEqual('hello');
})
tap.test('should end the server', async (toolsArg) => {
await toolsArg.delayFor(5000);
await testServer.stop();
});

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@api.global/typedrequest',
version: '3.0.8',
description: 'make typed requests towards apis'
}

View File

@ -1,5 +1,6 @@
export * from './typedrequest.classes.typedrequest';
export * from './typedrequest.classes.typedhandler';
export * from './typedrequest.classes.typedrouter';
export * from './typedrequest.classes.typedresponseerror';
export * from './typedrequest.classes.typedtarget';
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';

14
ts/plugins.ts Normal file
View File

@ -0,0 +1,14 @@
// 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 smartpromise from '@push.rocks/smartpromise';
import * as webrequest from '@push.rocks/webrequest';
export { isounique, lik, smartbuffer, smartdelay, smartpromise, webrequest };

View File

@ -1,5 +1,5 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
import * as plugins from './plugins.js';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
export type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request']
@ -32,7 +32,7 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
if (e instanceof TypedResponseError) {
typedResponseError = e;
} else {
throw e;
console.log(e);
}
});

View File

@ -1,13 +1,12 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
import { TypedRouter } from './typedrequest.classes.typedrouter';
import { TypedTarget } from './typedrequest.classes.typedtarget';
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';
const webrequestInstance = new plugins.webrequest.WebRequest();
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
/**
* in case we post against a url endpoint
*/
@ -21,9 +20,8 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
public method: string;
/**
* note the overloading is thought to deak with promises
* @param postEndPointArg
* @param methodArg
* @param methodArg
*/
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
if (typeof postTarget === 'string') {
@ -37,8 +35,8 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
/**
* fires the request
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
const payload: plugins.typedRequestInterfaces.ITypedRequest = {
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
let payloadSending: plugins.typedRequestInterfaces.ITypedRequest = {
method: this.method,
request: fireArg,
response: null,
@ -48,30 +46,58 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
},
};
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
// 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, payload);
responseBody = response;
const response = await webrequestInstance.postJson(
this.urlEndPoint,
payloadSendingArg,
useCacheArg
);
payloadReceiving = response;
} else {
responseBody = await this.typedTarget.post(payload);
payloadReceiving = await this.typedTarget.post(payloadSendingArg);
}
if (responseBody.error) {
if (payloadReceiving.error) {
console.error(
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
responseBody.error.data
`Got an error ${payloadReceiving.error.text} with data ${JSON.stringify(
payloadReceiving.error.data,
null,
2
)}`
);
if (!responseBody.retry) {
throw new TypedResponseError(responseBody.error.text, responseBody.error.data);
if (!payloadReceiving.retry) {
throw new TypedResponseError(payloadReceiving.error.text, payloadReceiving.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);
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
return await this.fire(fireArg);
payloadReceiving = await this.postTrObject(payloadSendingArg, useCacheArg);
}
return responseBody.response;
return payloadReceiving;
}
}

View File

@ -1,4 +1,4 @@
import * as plugins from './typedrequest.plugins';
import * as plugins from './plugins.js';
export class TypedResponseError {
public errorText: string;

View File

@ -1,7 +1,8 @@
import * as plugins from './typedrequest.plugins';
import * as plugins from './plugins.js';
import { VirtualStream } from './typedrequest.classes.virtualstream.js';
import { TypedHandler } from './typedrequest.classes.typedhandler';
import { TypedRequest } from './typedrequest.classes.typedrequest';
import { TypedHandler } from './typedrequest.classes.typedhandler.js';
import { TypedRequest } from './typedrequest.classes.typedrequest.js';
/**
* A typed router decides on which typed handler to call based on the method
@ -9,13 +10,11 @@ import { TypedRequest } from './typedrequest.classes.typedrequest';
* This is thought for reusing the same url endpoint for different methods
*/
export class TypedRouter {
public upstreamTypedRouter: TypedRouter;
public routerMap = new plugins.lik.ObjectMap<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,
@ -45,11 +44,11 @@ export class TypedRouter {
* @param typedRequest
*/
public addTypedRouter(typedRouterArg: TypedRouter) {
this.routerMap.add(typedRouterArg);
}
public setUpstreamTypedRouter(typedRouterArg: TypedRouter) {
this.upstreamTypedRouter = typedRouterArg;
const routerExists = this.routerMap.findSync((routerArg) => routerArg === typedRouterArg);
if (!routerExists) {
this.routerMap.add(typedRouterArg);
typedRouterArg.addTypedRouter(this);
}
}
public checkForTypedHandler(methodArg: string): boolean {
@ -63,24 +62,22 @@ export class TypedRouter {
*/
public getTypedHandlerForMethod(
methodArg: string,
checkUpstreamRouter = true
checkedRouters: TypedRouter[] = []
): TypedHandler<any> {
checkedRouters.push(this);
let typedHandler: TypedHandler<any>;
if (this.upstreamTypedRouter && checkUpstreamRouter) {
typedHandler = this.upstreamTypedRouter.getTypedHandlerForMethod(methodArg);
} else {
typedHandler = this.handlerMap.find((handler) => {
return handler.method === methodArg;
});
typedHandler = this.handlerMap.findSync((handler) => {
return handler.method === methodArg;
});
if (!typedHandler) {
this.routerMap.getArray().forEach((typedRouter) => {
if (!typedHandler) {
typedHandler = typedRouter.getTypedHandlerForMethod(methodArg, false);
}
});
}
if (!typedHandler) {
this.routerMap.getArray().forEach((typedRouterArg) => {
if (!typedHandler && !checkedRouters.includes(typedRouterArg)) {
typedHandler = typedRouterArg.getTypedHandlerForMethod(methodArg, checkedRouters);
}
});
}
return typedHandler;
@ -91,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(typedRequestArg: plugins.typedRequestInterfaces.ITypedRequest) {
if (!typedRequestArg?.correlation?.phase || 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) {
@ -101,24 +117,50 @@ export class TypedRouter {
text: 'There is no available method for this call on the server side',
data: {},
};
typedRequestArg.correlation.phase = 'response';
// encode again before handing back
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
return typedRequestArg;
}
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
} else if (typedRequestArg.correlation.phase === 'response') {
typedRequestArg.localData = null;
// encode again before handing back
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
return typedRequestArg;
} else if (typedRequestArg?.correlation?.phase === 'response') {
this.fireEventInterestMap
.findInterest(typedRequestArg.correlation.id)
?.fullfillInterest(typedRequestArg);
return null;
} else {
console.log('received weirdly shaped request');
console.log(typedRequestArg);
return {}
return null;
}
return {
...typedRequestArg,
...{
method: 'nullPathFromResponse',
},
};
}
/**
* 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;
}
}

View File

@ -1,11 +1,12 @@
import { TypedRouter } from './typedrequest.classes.typedrouter';
import * as plugins from './typedrequest.plugins';
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
import * as plugins from './plugins.js';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
/**
* this is an alternative to a post url supplied in `new Typedrequest(new TypedTarget(...), 'someMethodName')`
* enables the use of custom post functions
* used for things like broadcast channels
* e.g. @designestate/dees-comms
@ -51,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

View File

@ -0,0 +1,359 @@
import * as plugins from './plugins.js';
import type { TypedRouter } from './typedrequest.classes.typedrouter.js';
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 = ArrayBufferLike> implements plugins.typedRequestInterfaces.IVirtualStream<T> {
// STATIC
public static encodePayloadForNetwork(
objectPayload: any,
commFunctions: ICommFunctions,
originalPayload?: any,
path = []
): any {
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)) {
const returnArray = [];
for (const item of objectPayload) {
returnArray.push(
VirtualStream.encodePayloadForNetwork(
item,
commFunctions,
originalPayload || objectPayload,
path
)
);
}
return returnArray;
} else if (objectPayload !== null && typeof objectPayload === 'object') {
return Object.keys(objectPayload).reduce((acc, key) => {
path.push(key);
acc[key] = VirtualStream.encodePayloadForNetwork(
objectPayload[key],
commFunctions,
originalPayload || objectPayload,
path
);
return acc;
}, {});
} else {
return objectPayload;
}
}
public static decodePayloadFromNetwork(objectPayload: any, commFunctions: ICommFunctions): any {
if (plugins.smartbuffer.isBufferLike(objectPayload)) {
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() {}
/**
* takes care of sending
*/
private async workOnQueue() {
if(this.side === 'requesting') {
// 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;
}
}
// do work loop
let thisSideIsBackpressured = !this.receiveBackpressuredArray.checkSpaceAvailable();
let otherSideHasNext = false;
let otherSideIsBackpressured = false;
while (this.sendBackpressuredArray.data.length > 0 || otherSideHasNext) {
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);
}
thisSideIsBackpressured = this.receiveBackpressuredArray.checkSpaceAvailable();
// lets care about looping
otherSideHasNext = streamTr && streamTr.response && streamTr.response.next;
}
}
}
/**
* 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(),
};
streamTrArg.request = null;
}
// 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,
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,
};
}
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
await plugins.smartdelay.delayFor(0);
let counter = 0;
keepAliveLoop: while (this.keepAlive) {
const triggerResult = 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: true,
},
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;
}
public pipeWebStream(webStream: any) {
// lets do the piping
webStream.on('data', (data: any) => {});
webStream.on('end', () => {});
webStream.on('error', (error: any) => {});
}
}

View File

@ -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 };

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}