add getter for internal PromiseIO socket in RPCServer

This commit is contained in:
nitowa
2023-01-09 15:02:32 +01:00
parent b2ffbbfa48
commit 928d2662fd
10 changed files with 382 additions and 10495 deletions
+356 -10465
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -46,8 +46,8 @@
"ts-mocha": "^6.0.0",
"typedoc": "^0.15.0",
"typedoc-plugin-markdown": "^2.2.6",
"typescript": "^3.5.3",
"webpack": "^4.40.2",
"typescript": "^4.6.3",
"webpack": "^5.71.0",
"webpack-cli": "^3.3.9",
"why-is-node-running": "^2.1.2"
},
+3 -1
View File
@@ -115,8 +115,10 @@ export class RPCServer<
}
return infos
})
}
public getSocket(): PromiseIO{
return this.pio
}
close(): void {
-7
View File
@@ -63,12 +63,5 @@ export function Serializable(attr?: any) {
DeserializerFactory.entityClasses[clazz.name] = clazz
return clazz
/*
return class extends clazz{
constructor(...args: any[]) {
super(...args)
}
}
*/
}
}
+1 -1
View File
@@ -234,7 +234,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
const destroy_prefix = DESTROY_PREFIX
const frontendHookStr = `
async (${headerArgs} ${CALLBACK_NAME}) => {
async (${CALLBACK_NAME}, ${headerArgs}) => {
const r = await this.call("${fnName}", ${sesame} ${argParams})
try{
if(r){
+1 -1
View File
@@ -50,7 +50,7 @@ export class PromiseIO {
this.httpServer!.listen(port)
}
on(eventName: string, listener: T.AnyFunction) {
on(eventName: string, listener: T.GenericFunction) {
if (this.listeners[eventName] == null) {
this.listeners[eventName] = []
}
+4 -6
View File
@@ -9,15 +9,15 @@ export type GenericFunction<Parameters extends any[] = any[], Result = any> = {(
export type BackendHook<Func extends GenericFunction> =
GenericFunction<
[
...Head<Parameters<Func>>,
GenericFunction<
Parameters<
AsFunction<
Last<Parameters<Func>>
Parameters<Func>[0]
>
>,
void
>
>,
...Tail<Parameters<Func>>
],
ReturnType<Func>
>
@@ -120,7 +120,6 @@ export type AsyncGenericFunction<F extends GenericFunction = GenericFunction> =
? ((...args: Parameters<F>) => R extends Promise<any> ? R : Promise<R>)
: Promise<any>
type Destroyable = { destroy: () => void }
export type Callback<Params extends any[] = []> =
(this: Destroyable, ...args: Params) => void
@@ -128,8 +127,7 @@ export type Callback<Params extends any[] = []> =
type AsFunction<F> = F extends GenericFunction ? F : GenericFunction
type Last<Tuple extends any[]> = Tuple[ Subtract<Length<Tuple>, 1> ]
type Head<T extends any[]> = T extends [ ...infer Head, any ] ? Head : any[]
type Tail<T extends any[]> = T extends [any, ...infer Tail] ? Tail: []
type Tail<T extends any[]> = T extends [any, ...infer R] ? R : any[]
type Length<T extends any[]> =
T extends { length: infer L } ? L : never;
+3 -3
View File
@@ -110,7 +110,7 @@ export function stripAfterEquals(str: string): string {
const hookGenerator = (rpc: T.HookRPC<any, any>, errorHandler: T.ErrorHandler, sesameFn?: T.SesameFunction, injectSocket?: boolean): T.HookInfo['generator'] => {
const deserializer = DeserializerFactory
let argsArr = extractArgs(rpc.hook)
argsArr.pop() //remove callback param
argsArr.shift()//remove callback param
let callArgs = argsArr.join(',')
const args = sesameFn ? (['sesame', ...argsArr].join(','))
@@ -123,11 +123,11 @@ const hookGenerator = (rpc: T.HookRPC<any, any>, errorHandler: T.ErrorHandler, s
try{
if(sesameFn && !sesameFn(sesame)) return
const uuid = uuidv4()
const res = await rpc.hook(${callArgs} (...cbargs) => {
const res = await rpc.hook((...cbargs) => {
${rpc.onCallback ? `rpc.onCallback.apply({}, cbargs)` : ``}
cbargs = cbargs.map(deserializer.makeDeserializable)
$__socket__$.call.apply($__socket__$, [uuid, ...cbargs])
})
},${callArgs})
${rpc.onDestroy ? `$__socket__$.bind(destroy_prefix+uuid, () => {
rpc.onDestroy(res, rpc)
})` : ``}
+9 -6
View File
@@ -15,11 +15,11 @@ var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const noop = (...args) => { }
const noop = (...args: any[]) => { }
const add = (...args: number[]) => { return args.reduce((a, b) => a + b, 0) }
function makeServer(onCallback = noop, connectionHandler = noop, hookCloseHandler = noop, closeHandler = noop, errorHandler = noop) {
let subcallback
let subcallback: GenericFunction
const serv = new RPCServer([{
name: 'test',
RPCs: [
@@ -68,7 +68,7 @@ describe('PromiseIO', () => {
const server = new PromiseIO()
server.attach(new http.Server())
server.on("socket", clientSocket => {
clientSocket.bind("test123", (p1, p2) => {
clientSocket.bind("test123", (p1: string, p2:string) => {
server.close()
if (p1 === "p1" && p2 === "p2")
done()
@@ -549,7 +549,7 @@ describe('It should do unhook', () => {
},
{
name: 'subscribeWithParam',
hook: async (param, callback): Promise<{ uuid: string }> => {
hook: async (callback, param): Promise<{ uuid: string }> => {
if (param != "OK") {
console.log("param was" + param);
@@ -583,7 +583,7 @@ describe('It should do unhook', () => {
})
it('Subscribe with param', async () => {
const res = await client['test'].subscribeWithParam("OK", noop)
const res = await client['test'].subscribeWithParam(noop, "OK")
expect(res.uuid).to.be.equal(candy)
})
@@ -625,6 +625,7 @@ describe('Sesame should unlock the socket', () => {
let server: RPCServer<SesameTestIfc>
let cb: Function = (...args) => { }
before((done) => {
server = new RPCServer<SesameTestIfc>([{
name: "test",
@@ -1096,7 +1097,9 @@ describe("class (de-)serialization", () => {
})
it("receives class object in hook response", async function () {
const obj: TestClass = await mySocket.Test.classCallback(noop)
const obj: TestClass = await mySocket.Test.classCallback(function(x){
x
})
verifyObject(obj)
})
+1 -1
View File
@@ -9,6 +9,6 @@
"strict": true,
"experimentalDecorators": true
},
"include": ["src/**/*.ts", "test/**/*.ts", "Index.ts", "demo.ts", "wat.ts"],
"include": ["src/**/*.ts", "test/**/*.ts", "Index.ts"],
"exclude": ["node_modules"]
}