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