101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import type { TypedRouter } from './typedrequest.classes.typedrouter.js';
|
|
|
|
export interface ICommFunctions {
|
|
sendMethod?: (
|
|
sendPayload: plugins.typedRequestInterfaces.ITypedRequest
|
|
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
|
|
typedrouter?: TypedRouter;
|
|
}
|
|
|
|
export class VirtualStream {
|
|
// STATIC
|
|
public static encodePayloadForNetwork(
|
|
objectPayload: any,
|
|
commFunctions: ICommFunctions
|
|
): any {
|
|
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;
|
|
}
|
|
return {
|
|
_isVirtualStream: true,
|
|
streamId: objectPayload.streamId,
|
|
};
|
|
} else if (Array.isArray(objectPayload)) {
|
|
const returnArray = [];
|
|
for (const item of objectPayload) {
|
|
returnArray.push(VirtualStream.encodePayloadForNetwork(item, commFunctions));
|
|
}
|
|
return returnArray;
|
|
} else if (objectPayload !== null && typeof objectPayload === 'object') {
|
|
return Object.keys(objectPayload).reduce((acc, key) => {
|
|
acc[key] = VirtualStream.encodePayloadForNetwork(objectPayload[key], commFunctions);
|
|
return acc;
|
|
}, {});
|
|
} else {
|
|
return objectPayload;
|
|
}
|
|
}
|
|
|
|
public static decodePayloadFromNetwork(objectPayload: any, commFunctions: ICommFunctions): any {
|
|
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;
|
|
}
|
|
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();
|
|
public typedrouter: TypedRouter;
|
|
public sendMethod: (
|
|
sendPayloadArg: plugins.typedRequestInterfaces.ITypedRequest
|
|
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
|
|
|
|
constructor() {}
|
|
|
|
public async handleStreamTr(streamTrArg: plugins.typedRequestInterfaces.IStreamRequest) {
|
|
return streamTrArg;
|
|
}
|
|
|
|
/**
|
|
* closes the virtual stream
|
|
*/
|
|
close() {
|
|
if (this.typedrouter) {
|
|
this.typedrouter.registeredVirtualStreams.remove(this);
|
|
}
|
|
}
|
|
}
|