Selaa lähdekoodia

add getter for internal PromiseIO socket in RPCServer

master
nitowa 1 vuosi sitten
vanhempi
commit
928d2662fd
10 muutettua tiedostoa jossa 1150 lisäystä ja 11263 poistoa
  1. 1124
    11233
      package-lock.json
  2. 2
    2
      package.json
  3. 4
    2
      src/Backend.ts
  4. 0
    7
      src/Decorator.ts
  5. 1
    1
      src/Frontend.ts
  6. 1
    1
      src/PromiseIO/Server.ts
  7. 4
    6
      src/Types.ts
  8. 3
    3
      src/Utils.ts
  9. 10
    7
      test/Test.ts
  10. 1
    1
      tsconfig.json

+ 1124
- 11233
package-lock.json
File diff suppressed because it is too large
Näytä tiedosto


+ 2
- 2
package.json Näytä tiedosto

@@ -46,8 +46,8 @@
46 46
     "ts-mocha": "^6.0.0",
47 47
     "typedoc": "^0.15.0",
48 48
     "typedoc-plugin-markdown": "^2.2.6",
49
-    "typescript": "^3.5.3",
50
-    "webpack": "^4.40.2",
49
+    "typescript": "^4.6.3",
50
+    "webpack": "^5.71.0",
51 51
     "webpack-cli": "^3.3.9",
52 52
     "why-is-node-running": "^2.1.2"
53 53
   },

+ 4
- 2
src/Backend.ts Näytä tiedosto

@@ -115,10 +115,12 @@ export class RPCServer<
115 115
             }
116 116
             return infos
117 117
         })
118
-
119
-
120 118
     }
121 119
 
120
+    public getSocket(): PromiseIO{
121
+        return this.pio
122
+    } 
123
+
122 124
     close(): void {
123 125
         this.pio.close()
124 126
     }

+ 0
- 7
src/Decorator.ts Näytä tiedosto

@@ -63,12 +63,5 @@ export function Serializable(attr?: any) {
63 63
         DeserializerFactory.entityClasses[clazz.name] = clazz
64 64
         
65 65
         return clazz
66
-        /*
67
-        return class extends clazz{
68
-            constructor(...args: any[]) {
69
-                super(...args)
70
-            }
71
-        }
72
-        */
73 66
     }
74 67
 }

+ 1
- 1
src/Frontend.ts Näytä tiedosto

@@ -234,7 +234,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
234 234
         const destroy_prefix = DESTROY_PREFIX
235 235
 
236 236
         const frontendHookStr = `
237
-        async (${headerArgs} ${CALLBACK_NAME}) => {
237
+        async (${CALLBACK_NAME}, ${headerArgs}) => {
238 238
             const r = await this.call("${fnName}", ${sesame} ${argParams})
239 239
             try{
240 240
                 if(r){

+ 1
- 1
src/PromiseIO/Server.ts Näytä tiedosto

@@ -50,7 +50,7 @@ export class PromiseIO {
50 50
         this.httpServer!.listen(port)
51 51
     }
52 52
 
53
-    on(eventName: string, listener: T.AnyFunction) {
53
+    on(eventName: string, listener: T.GenericFunction) {
54 54
         if (this.listeners[eventName] == null) {
55 55
             this.listeners[eventName] = []
56 56
         }

+ 4
- 6
src/Types.ts Näytä tiedosto

@@ -9,15 +9,15 @@ export type GenericFunction<Parameters extends any[] = any[], Result = any> = {(
9 9
 export type BackendHook<Func extends GenericFunction> =
10 10
     GenericFunction<
11 11
         [
12
-            ...Head<Parameters<Func>>,
13 12
             GenericFunction<
14 13
                 Parameters<
15 14
                     AsFunction<
16
-                        Last<Parameters<Func>>
15
+                        Parameters<Func>[0]
17 16
                     >
18 17
                 >,
19 18
                 void
20
-            >
19
+            >,
20
+            ...Tail<Parameters<Func>>
21 21
         ],
22 22
         ReturnType<Func>
23 23
     >
@@ -120,7 +120,6 @@ export type AsyncGenericFunction<F extends GenericFunction = GenericFunction> =
120 120
     ? ((...args: Parameters<F>) => R extends Promise<any> ? R : Promise<R>)
121 121
     : Promise<any>
122 122
 
123
-
124 123
 type Destroyable = { destroy: () => void }
125 124
 export type Callback<Params extends any[] = []> =
126 125
     (this: Destroyable, ...args: Params) => void
@@ -128,8 +127,7 @@ export type Callback<Params extends any[] = []> =
128 127
 type AsFunction<F> = F extends GenericFunction ? F : GenericFunction
129 128
 
130 129
 type Last<Tuple extends any[]> = Tuple[ Subtract<Length<Tuple>, 1> ]
131
-type Head<T extends any[]> = T extends [ ...infer Head, any ] ? Head : any[]
132
-type Tail<T extends any[]> = T extends [any, ...infer Tail] ? Tail: []
130
+type Tail<T extends any[]> = T extends [any, ...infer R] ? R : any[]
133 131
 
134 132
 type Length<T extends any[]> = 
135 133
     T extends { length: infer L } ? L : never;

+ 3
- 3
src/Utils.ts Näytä tiedosto

@@ -110,7 +110,7 @@ export function stripAfterEquals(str: string): string {
110 110
 const hookGenerator = (rpc: T.HookRPC<any, any>, errorHandler: T.ErrorHandler, sesameFn?: T.SesameFunction, injectSocket?: boolean): T.HookInfo['generator'] => {
111 111
     const deserializer = DeserializerFactory 
112 112
     let argsArr = extractArgs(rpc.hook)
113
-    argsArr.pop() //remove callback param
113
+    argsArr.shift()//remove callback param
114 114
 
115 115
     let callArgs = argsArr.join(',')
116 116
     const args = sesameFn ? (['sesame', ...argsArr].join(','))
@@ -123,11 +123,11 @@ const hookGenerator = (rpc: T.HookRPC<any, any>, errorHandler: T.ErrorHandler, s
123 123
         try{
124 124
             if(sesameFn && !sesameFn(sesame)) return
125 125
             const uuid = uuidv4()
126
-            const res = await rpc.hook(${callArgs} (...cbargs) => {
126
+            const res = await rpc.hook((...cbargs) => {
127 127
                 ${rpc.onCallback ? `rpc.onCallback.apply({}, cbargs)` : ``}
128 128
                 cbargs = cbargs.map(deserializer.makeDeserializable)
129 129
                 $__socket__$.call.apply($__socket__$, [uuid, ...cbargs])
130
-            })
130
+            },${callArgs})
131 131
             ${rpc.onDestroy ? `$__socket__$.bind(destroy_prefix+uuid, () => {
132 132
                 rpc.onDestroy(res, rpc)
133 133
             })` : ``}

+ 10
- 7
test/Test.ts Näytä tiedosto

@@ -15,11 +15,11 @@ var chai = require("chai");
15 15
 var chaiAsPromised = require("chai-as-promised");
16 16
 
17 17
 chai.use(chaiAsPromised);
18
-const noop = (...args) => { }
18
+const noop = (...args: any[]) => { }
19 19
 
20 20
 const add = (...args: number[]) => { return args.reduce((a, b) => a + b, 0) }
21 21
 function makeServer(onCallback = noop, connectionHandler = noop, hookCloseHandler = noop, closeHandler = noop, errorHandler = noop) {
22
-    let subcallback
22
+    let subcallback: GenericFunction
23 23
     const serv = new RPCServer([{
24 24
         name: 'test',
25 25
         RPCs: [
@@ -68,7 +68,7 @@ describe('PromiseIO', () => {
68 68
         const server = new PromiseIO()
69 69
         server.attach(new http.Server())
70 70
         server.on("socket", clientSocket => {
71
-            clientSocket.bind("test123", (p1, p2) => {
71
+            clientSocket.bind("test123", (p1: string, p2:string) => {
72 72
                 server.close()
73 73
                 if (p1 === "p1" && p2 === "p2")
74 74
                     done()
@@ -549,7 +549,7 @@ describe('It should do unhook', () => {
549 549
             },
550 550
             {
551 551
                 name: 'subscribeWithParam',
552
-                hook: async (param, callback): Promise<{ uuid: string }> => {
552
+                hook: async (callback, param): Promise<{ uuid: string }> => {
553 553
 
554 554
                     if (param != "OK") {
555 555
                         console.log("param was" + param);
@@ -583,7 +583,7 @@ describe('It should do unhook', () => {
583 583
     })
584 584
 
585 585
     it('Subscribe with param', async () => {
586
-        const res = await client['test'].subscribeWithParam("OK", noop)
586
+        const res = await client['test'].subscribeWithParam(noop, "OK")
587 587
         expect(res.uuid).to.be.equal(candy)
588 588
     })
589 589
 
@@ -625,6 +625,7 @@ describe('Sesame should unlock the socket', () => {
625 625
     let server: RPCServer<SesameTestIfc>
626 626
     let cb: Function = (...args) => { }
627 627
 
628
+
628 629
     before((done) => {
629 630
         server = new RPCServer<SesameTestIfc>([{
630 631
             name: "test",
@@ -1096,7 +1097,9 @@ describe("class (de-)serialization", () => {
1096 1097
         })
1097 1098
 
1098 1099
         it("receives class object in hook response", async function () {
1099
-            const obj: TestClass = await mySocket.Test.classCallback(noop)
1100
+            const obj: TestClass = await mySocket.Test.classCallback(function(x){
1101
+                x
1102
+            })
1100 1103
             verifyObject(obj)
1101 1104
         })
1102 1105
 
@@ -1129,4 +1132,4 @@ describe("class (de-)serialization", () => {
1129 1132
             })
1130 1133
         })
1131 1134
     })
1132
-})
1135
+})

+ 1
- 1
tsconfig.json Näytä tiedosto

@@ -9,6 +9,6 @@
9 9
       "strict": true,
10 10
       "experimentalDecorators": true
11 11
     },
12
-    "include": ["src/**/*.ts", "test/**/*.ts", "Index.ts", "demo.ts", "wat.ts"],
12
+    "include": ["src/**/*.ts", "test/**/*.ts", "Index.ts"],
13 13
     "exclude": ["node_modules"]
14 14
   }

Loading…
Peruuta
Tallenna