2021-12-31 21:38:26 +00:00
|
|
|
//@template-ignore
|
2022-01-02 22:02:47 +00:00
|
|
|
import { VerificationError } from "./ts_base";
|
|
|
|
//@template-ignore
|
2024-01-18 18:08:44 +00:00
|
|
|
import { type RequestObject, type ResponseObject, ErrorCodes, Logging } from "./ts_service_base";
|
2021-12-31 21:38:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
export type IMessageCallback = (data: any) => void;
|
|
|
|
|
|
|
|
export type ResponseListener = {
|
|
|
|
ok: (response:any)=>void;
|
|
|
|
err: (error: Error)=>void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Service {
|
|
|
|
public _name: string = null as any;
|
|
|
|
|
|
|
|
constructor(protected _provider: ServiceProvider, name: string) {
|
|
|
|
this._name = name;
|
|
|
|
this._provider.services.set(name, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ServiceProvider {
|
|
|
|
services = new Map<string, Service>();
|
2022-02-14 00:58:05 +00:00
|
|
|
requests = new Map<string, ResponseListener |undefined>();
|
2021-12-31 21:38:26 +00:00
|
|
|
|
|
|
|
constructor(private sendPacket: IMessageCallback) {}
|
|
|
|
|
|
|
|
onPacket(msg: RequestObject | ResponseObject) {
|
|
|
|
Logging.log("CLIENT: Received message:", msg);
|
|
|
|
if("method" in msg) {
|
|
|
|
if(msg.id){
|
|
|
|
Logging.log("CLIENT: Determined type is Request");
|
|
|
|
// Request, which are not supported by client, so ignore
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Logging.log("CLIENT: Determined type is Notification");
|
|
|
|
//Notification. Send to Notification handler
|
2022-01-01 00:39:15 +00:00
|
|
|
const [srvName, fncName] = msg.method.split(".");
|
|
|
|
let service = this.services.get(srvName)
|
|
|
|
if(!service) {
|
|
|
|
Logging.log("CLIENT: Did not find Service wanted by Notification!", srvName);
|
|
|
|
} else {
|
|
|
|
//TODO: Implement Event thingy (or so :))
|
|
|
|
}
|
2021-12-31 21:38:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logging.log("CLIENT: Determined type is Response");
|
|
|
|
// Response
|
|
|
|
let resListener = this.requests.get(msg.id);
|
|
|
|
if(!resListener) return; // Ignore wrong responses
|
|
|
|
if(msg.error) {
|
2022-01-02 22:02:47 +00:00
|
|
|
if(msg.error.data && msg.error.data.$ == "verification_error") {
|
|
|
|
resListener.err(new VerificationError(msg.error.data.type, msg.error.data.field, msg.error.data.value))
|
|
|
|
} else {
|
|
|
|
resListener.err(new Error(msg.error.message));
|
|
|
|
}
|
2021-12-31 21:38:26 +00:00
|
|
|
} else {
|
|
|
|
resListener.ok(msg.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(msg: RequestObject, res?: ResponseListener) {
|
|
|
|
Logging.log("CLIENT: Sending Messgage", msg);
|
|
|
|
if(msg.id) {
|
|
|
|
this.requests.set(msg.id, res);
|
|
|
|
}
|
|
|
|
this.sendPacket(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
declare var require: any;
|
|
|
|
export const getRandomBytes = (
|
|
|
|
typeof self !== "undefined" && (self.crypto || (self as any).msCrypto)
|
|
|
|
? function () {
|
|
|
|
// Browsers
|
|
|
|
var crypto = self.crypto || (self as any).msCrypto;
|
|
|
|
var QUOTA = 65536;
|
|
|
|
return function (n: number) {
|
|
|
|
var a = new Uint8Array(n);
|
|
|
|
for (var i = 0; i < n; i += QUOTA) {
|
|
|
|
crypto.getRandomValues(
|
|
|
|
a.subarray(i, i + Math.min(n - i, QUOTA))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
: function () {
|
|
|
|
// Node
|
|
|
|
return require("crypto").randomBytes;
|
|
|
|
}
|
|
|
|
)() as (cnt: number) => Uint8Array;
|
|
|
|
|
|
|
|
export const getRandomID = (length: number) => {
|
|
|
|
return btoa(String.fromCharCode.apply(null, getRandomBytes(length) as any));
|
|
|
|
};
|