Browse Source

add getter for internal PromiseIO socket in RPCServer

master
nitowa 1 year ago
parent
commit
928d2662fd
10 changed files with 1150 additions and 11263 deletions
  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
View File


+ 2
- 2
package.json View File

46
     "ts-mocha": "^6.0.0",
46
     "ts-mocha": "^6.0.0",
47
     "typedoc": "^0.15.0",
47
     "typedoc": "^0.15.0",
48
     "typedoc-plugin-markdown": "^2.2.6",
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
     "webpack-cli": "^3.3.9",
51
     "webpack-cli": "^3.3.9",
52
     "why-is-node-running": "^2.1.2"
52
     "why-is-node-running": "^2.1.2"
53
   },
53
   },

+ 4
- 2
src/Backend.ts View File

115
             }
115
             }
116
             return infos
116
             return infos
117
         })
117
         })
118
-
119
-
120
     }
118
     }
121
 
119
 
120
+    public getSocket(): PromiseIO{
121
+        return this.pio
122
+    } 
123
+
122
     close(): void {
124
     close(): void {
123
         this.pio.close()
125
         this.pio.close()
124
     }
126
     }

+ 0
- 7
src/Decorator.ts View File

63
         DeserializerFactory.entityClasses[clazz.name] = clazz
63
         DeserializerFactory.entityClasses[clazz.name] = clazz
64
         
64
         
65
         return clazz
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 View File

234
         const destroy_prefix = DESTROY_PREFIX
234
         const destroy_prefix = DESTROY_PREFIX
235
 
235
 
236
         const frontendHookStr = `
236
         const frontendHookStr = `
237
-        async (${headerArgs} ${CALLBACK_NAME}) => {
237
+        async (${CALLBACK_NAME}, ${headerArgs}) => {
238
             const r = await this.call("${fnName}", ${sesame} ${argParams})
238
             const r = await this.call("${fnName}", ${sesame} ${argParams})
239
             try{
239
             try{
240
                 if(r){
240
                 if(r){

+ 1
- 1
src/PromiseIO/Server.ts View File

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

+ 4
- 6
src/Types.ts View File

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

+ 3
- 3
src/Utils.ts View File

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

+ 10
- 7
test/Test.ts View File

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

+ 1
- 1
tsconfig.json View File

9
       "strict": true,
9
       "strict": true,
10
       "experimentalDecorators": true
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
     "exclude": ["node_modules"]
13
     "exclude": ["node_modules"]
14
   }
14
   }

Loading…
Cancel
Save