typedrequest/ts/typedrequest.classes.typedrouter.ts

168 lines
5.8 KiB
TypeScript
Raw Normal View History

2024-02-20 16:40:30 +00:00
import * as plugins from './plugins.js';
2024-02-21 17:29:35 +00:00
import { VirtualStream } from './typedrequest.classes.virtualstream.js';
2019-08-25 15:19:12 +00:00
2022-03-24 19:02:58 +00:00
import { TypedHandler } from './typedrequest.classes.typedhandler.js';
import { TypedRequest } from './typedrequest.classes.typedrequest.js';
2019-08-25 15:19:12 +00:00
/**
* A typed router decides on which typed handler to call based on the method
* specified in the typed request
* This is thought for reusing the same url endpoint for different methods
*/
export class TypedRouter {
2020-06-15 16:39:48 +00:00
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
public handlerMap = new plugins.lik.ObjectMap<
2020-12-18 18:14:29 +00:00
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
2019-08-25 15:19:12 +00:00
>();
2024-02-24 11:20:13 +00:00
public registeredVirtualStreams = new plugins.lik.ObjectMap<VirtualStream<any>>();
2019-08-25 15:19:12 +00:00
2020-07-14 16:37:44 +00:00
public fireEventInterestMap = new plugins.lik.InterestMap<
string,
plugins.typedRequestInterfaces.ITypedRequest
>((correlationId: string) => correlationId);
2020-07-14 02:20:15 +00:00
2019-08-25 15:19:12 +00:00
/**
* adds the handler to the routing map
2020-06-15 16:36:53 +00:00
* @param typedHandlerArg
2019-08-25 15:19:12 +00:00
*/
2022-10-09 16:52:49 +00:00
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(
2020-06-15 16:36:53 +00:00
typedHandlerArg: TypedHandler<T>
2020-02-11 18:55:07 +00:00
) {
2020-06-15 16:36:53 +00:00
// lets check for deduplication
const existingTypedHandler = this.getTypedHandlerForMethod(typedHandlerArg.method);
if (existingTypedHandler) {
2020-06-15 16:39:48 +00:00
throw new Error(
`a TypedHandler for ${typedHandlerArg.method} alredy exists! Can't add another one.`
);
2020-06-15 16:36:53 +00:00
}
this.handlerMap.add(typedHandlerArg);
}
/**
* adds another sub typedRouter
2020-06-15 16:39:48 +00:00
* @param typedRequest
2020-06-15 16:36:53 +00:00
*/
2022-10-09 16:52:49 +00:00
public addTypedRouter(typedRouterArg: TypedRouter) {
const routerExists = this.routerMap.findSync((routerArg) => routerArg === typedRouterArg);
2021-11-10 12:14:40 +00:00
if (!routerExists) {
this.routerMap.add(typedRouterArg);
typedRouterArg.addTypedRouter(this);
}
2020-06-15 16:36:53 +00:00
}
2022-10-09 16:52:49 +00:00
public checkForTypedHandler(methodArg: string): boolean {
2020-06-15 16:39:48 +00:00
return !!this.getTypedHandlerForMethod(methodArg);
2020-06-15 16:36:53 +00:00
}
/**
* gets a typed Router from the router chain, upstream and downstream
* @param methodArg
2020-06-15 16:39:48 +00:00
* @param checkUpstreamRouter
2020-06-15 16:36:53 +00:00
*/
2022-10-09 16:52:49 +00:00
public getTypedHandlerForMethod(
2020-06-15 16:39:48 +00:00
methodArg: string,
2021-11-10 12:14:40 +00:00
checkedRouters: TypedRouter[] = []
2020-06-15 16:39:48 +00:00
): TypedHandler<any> {
2021-11-10 12:14:40 +00:00
checkedRouters.push(this);
2020-06-15 16:36:53 +00:00
let typedHandler: TypedHandler<any>;
2020-06-15 16:39:48 +00:00
2021-11-10 12:14:40 +00:00
typedHandler = this.handlerMap.findSync((handler) => {
return handler.method === methodArg;
});
2022-03-29 12:50:39 +00:00
2021-11-10 12:14:40 +00:00
if (!typedHandler) {
this.routerMap.getArray().forEach((typedRouterArg) => {
if (!typedHandler && !checkedRouters.includes(typedRouterArg)) {
typedHandler = typedRouterArg.getTypedHandlerForMethod(methodArg, checkedRouters);
}
2020-06-15 16:36:53 +00:00
});
}
return typedHandler;
}
/**
2020-07-14 02:20:15 +00:00
* if typedrequest object has correlation.phase === 'request' -> routes a typed request object to a handler
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
2020-06-15 16:36:53 +00:00
* @param typedRequestArg
*/
2022-10-09 16:52:49 +00:00
public async routeAndAddResponse<
T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest
>(typedRequestArg: T, localRequestArg = false): Promise<T> {
2024-02-21 17:29:35 +00:00
// 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##') {
2024-02-24 11:20:13 +00:00
const result: any = await this.handleStreamTypedRequest(typedRequestArg as plugins.typedRequestInterfaces.IStreamRequest);
result.localData = null;
2024-02-21 17:29:35 +00:00
return result as T;
}
// lets do normal routing
2022-10-09 16:52:49 +00:00
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
2020-07-14 02:20:15 +00:00
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
2019-08-31 12:11:56 +00:00
2020-07-14 02:20:15 +00:00
if (!typedHandler) {
console.log(`Cannot find handler for methodname ${typedRequestArg.method}`);
typedRequestArg.error = {
text: 'There is no available method for this call on the server side',
data: {},
};
2022-03-29 08:50:26 +00:00
typedRequestArg.correlation.phase = 'response';
2024-02-21 17:29:35 +00:00
// encode again before handing back
2024-02-29 22:12:51 +00:00
typedRequestArg.localData = null;
2024-02-21 17:29:35 +00:00
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
2020-07-14 02:20:15 +00:00
return typedRequestArg;
}
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
2024-02-21 17:29:35 +00:00
typedRequestArg.localData = null;
// encode again before handing back
typedRequestArg = VirtualStream.encodePayloadForNetwork(typedRequestArg, {
typedrouter: this,
});
2022-03-29 07:48:47 +00:00
return typedRequestArg;
} else if (typedRequestArg?.correlation?.phase === 'response') {
2020-07-26 13:48:57 +00:00
this.fireEventInterestMap
.findInterest(typedRequestArg.correlation.id)
?.fullfillInterest(typedRequestArg);
2022-03-29 12:50:39 +00:00
return null;
2020-10-06 21:33:46 +00:00
} else {
console.log('received weirdly shaped request');
console.log(typedRequestArg);
2022-10-09 16:52:49 +00:00
return null;
2020-07-14 02:20:15 +00:00
}
2019-08-25 15:19:12 +00:00
}
2024-02-21 17:29:35 +00:00
/**
* 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;
});
2024-02-24 11:20:13 +00:00
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}`);
}
2024-02-21 17:29:35 +00:00
const result = await relevantVirtualStream.handleStreamTr(streamTrArg);
return result;
}
2019-08-25 15:19:12 +00:00
}