v 1.3.0 improve pseudointerfaces

This commit is contained in:
2019-09-30 23:48:05 +02:00
parent 74bfa2545b
commit cc78248e44
8 changed files with 58 additions and 82 deletions
+2 -2
View File
@@ -107,7 +107,7 @@ It is possible to declare pseudo-interfaces for servers and clients by using ser
This feature is currently still in development and considered **unstable and untested**. Use with caution.
```typescript
type MyInterface = RPCInterface<{
type MyInterface = {
Group1: {
triggerCallbacks: (...args:any[]) => Promise<void>,
subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<{a: string}>>,
@@ -116,7 +116,7 @@ type MyInterface = RPCInterface<{
Group2: {
echo: (x:string) => Promise<string>
}
}>
}
```
Create a client using
```typescript
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rpclibrary",
"version": "1.2.6",
"version": "1.3.0",
"description": "rpclibrary is a websocket on steroids!",
"main": "./js/Index.js",
"repository": {
+1 -1
View File
@@ -29,7 +29,7 @@ export class RPCServer<
*/
constructor(
private port:number,
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof T.RPCInterface<InterfaceT>, SubResType>[] = [],
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [],
conf: T.SocketConf = {}
){
+1 -1
View File
@@ -129,7 +129,7 @@ export class RPCSocket implements I.Socket{
* @param fnName The function name
* @param fnArgs A string-list of parameters
*/
private callGenerator(fnName: string, fnArgs:string[]): T.AsyncFunction{
private callGenerator(fnName: string, fnArgs:string[]): T.AnyFunction{
const headerArgs = fnArgs.join(",")
const argParams = fnArgs.map(stripAfterEquals).join(",")
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
+4 -4
View File
@@ -2,15 +2,15 @@ import * as T from "./Types";
import * as I from "./Interfaces"
/**
* Interface for all classes that may export RPCs
* Interface for all classes that export RPCs
*/
export interface RPCExporter<
Ifc extends T.RPCInterface,
K extends keyof Ifc,
Name extends keyof Ifc,
SubresT = {}
>{
name: K
exportRPCs() : T.RPC<Ifc[K], keyof Ifc[K], SubresT>[]
name: Name
exportRPCs() : T.RPCInterfaceArray<Ifc, SubresT>[Name]
}
/**
+25 -21
View File
@@ -1,9 +1,7 @@
import * as I from "./Interfaces";
export type AnyFunction = (...any:any)=>any
export type AsyncFunction<Fn extends AnyFunction = AnyFunction> = ReturnType<Fn> extends Promise<any> ? Fn : (...any:Parameters<Fn>) => Promise<ReturnType<Fn>>
export type RPCGroup = { [name in string] : AsyncFunction }
export type RPCInterface<Impl extends RPCInterface = {}> = { [groupName in string]: RPCGroup } & Impl
export type AnyFunction = (...args:any) => any
export type HookFunction<F extends AnyFunction = AnyFunction, SubresT = {}> = (...args: Parameters<F>) => Promise<SubscriptionResponse<SubresT> | ErrorResponse>
export type Visibility = "127.0.0.1" | "0.0.0.0"
export type ConnectionHandler = (socket:I.Socket) => void
@@ -19,7 +17,6 @@ export type SocketConf = {
export type ResponseType = "Subscribe" | "Success" | "Error"
export type Outcome = "Success" | "Error"
export type Respose<T> = T & { result: Outcome }
export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
@@ -27,22 +24,31 @@ export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uui
export type RPCType = 'Hook' | 'Unhook' | 'Call'
export type HookT<G extends RPCGroup, K extends keyof G, SubresT> = AsyncFunction<HookFunction<G[K], SubresT>>
export type CallT<G extends RPCGroup, K extends keyof G> = AsyncFunction<G[K]>
export type CallRPC<N, F> = {
name: N
call: F
}
export type HookRPC<G extends RPCGroup, K extends keyof G, SubresT = {}> = {
name: K
hook: HookT<G, K, SubresT>
onCallback?: CallbackFunction,
export type HookRPC<N, F extends AnyFunction, SubresT = {}> = {
name: N
hook: HookFunction<F, SubresT>
onCallback?: AnyFunction
onClose?: HookCloseFunction<SubresT>
}
export type CallRPC<G extends RPCGroup, K extends keyof G> = {
name: K
call: CallT<G,K>
}
export type RPC<N, F extends AnyFunction, SubresT = {}> = HookRPC<N, F, SubresT> | CallRPC<N,F> | F
export type RPC<G extends RPCGroup, Kg extends keyof G, SubresT> = CallRPC<G, Kg> | HookRPC<G, Kg, SubresT>
export type RPCInterface<Impl extends RPCInterface = {}> = {
[grp in string] : {
[rpc in string] : AnyFunction
}
} & Impl
export type RPCInterfaceArray<Itfc extends RPCInterface, SubresT = {}> = {
[grp in keyof Itfc]: Array<
{ [rpc in keyof Itfc[grp]]: RPC<rpc, Itfc[grp][rpc], SubresT> }[keyof Itfc[grp]]
>
}
export type BaseInfo = {
name: string,
@@ -50,14 +56,14 @@ export type BaseInfo = {
argNames: string[],
}
export type HookInfo<T = {}> = BaseInfo & {
export type HookInfo<SubresT = {}> = BaseInfo & {
type: 'Hook',
generator: (socket?:I.Socket) => HookFunction<AnyFunction, T>
generator: (socket?:I.Socket) => (...args:any) => SubresT
}
export type CallInfo = BaseInfo & {
type: 'Call',
call: AsyncFunction
call: AnyFunction
}
export type RpcInfo = HookInfo | CallInfo
@@ -65,5 +71,3 @@ export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any
export type HookFunction<F extends AnyFunction = AnyFunction, SubResT = {}> = AsyncFunction<(...args:Parameters<F>) => SubscriptionResponse<SubResT> | ErrorResponse>
export type CallbackFunction = (callback:AnyFunction, ...args:any) => any
+3 -10
View File
@@ -4,6 +4,7 @@ import { RPCServer } from '../src/Backend'
import * as uuidv4 from "uuid/v4"
import { RPCSocket } from "../src/Frontend";
import { SubscriptionResponse } from "../src/Types";
import { makeSubResponse } from "../src/Utils";
const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
function makeServer(){
@@ -18,21 +19,13 @@ function makeServer(){
name: 'simpleSubscribe',
hook: async(callback) => {
subcallback = callback
return {
result: "Success",
uuid: uuidv4(),
topic: "test"
}
return makeSubResponse<{topic: string}>({topic: "test"})
}
},{
name: 'subscribe',
hook: async (callback) => {
subcallback = callback
return {
result: "Success",
uuid: uuidv4(),
topic: "test"
}
return makeSubResponse<{topic: string}>({topic: "test"})
},
onClose: (res, rpc) => {
console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
+19 -40
View File
@@ -1,55 +1,40 @@
import { RPCServer } from "../src/Backend";
import { SubscriptionResponse, CallRPC, HookRPC, RPCInterface } from "../src/Types";
import { SubscriptionResponse, RPCInterface } from "../src/Types";
import { RPCSocket } from "../src/Frontend";
import { makeSubResponse } from "../src/Utils";
type MyInterface = RPCInterface<{
type SubresExtension = {a:string}
type MyInterface = {
Group1: {
triggerCallbacks: (...args:any[]) => Promise<void>,
subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<{a: string}>>,
subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<SubresExtension>>,
unsubscribe: (uuid:string) => Promise<void>
},
Group2: {
echo: (x:string) => Promise<string>
}
}>
}
const callbacks:Map<string, Function> = new Map()
new RPCServer<{a:string}, MyInterface>(20000,
new RPCServer<SubresExtension, MyInterface>(20000,
[{
name: "Group1",
exportRPCs: () => [
<HookRPC<MyInterface['Group1'], 'subscribe', {a:string}>>{
name: 'subscribe',
hook: async (param, callback) => { const _uuid = ""+Math.random(); console.log(param); callbacks.set(_uuid, callback); return { result: 'Success', a: '3', uuid: _uuid} }
},
<CallRPC<MyInterface['Group1'], 'unsubscribe'>>{
name: 'unsubscribe',
call: async (uuid) => { callbacks.delete(uuid) }
},
<CallRPC<MyInterface['Group1'], 'triggerCallbacks'>>{
name: 'Group1',
exportRPCs: () => [{
name: 'triggerCallbacks',
call: async (...args) => { callbacks.forEach(cb => cb.apply({}, args)) }
}]
call: async () => { /*...*/ }
},{
name: 'Group2',
exportRPCs: () => [
<CallRPC<MyInterface['Group2'], 'echo'>>{
name: 'echo',
call: async (x) => x
}]
}]
)
new RPCServer<{a:string}, MyInterface>(20001,
[{
name: "Group1",
exportRPCs: () => []
name: 'subscribe',
hook: async (param, callback) => { return makeSubResponse<SubresExtension>({a: "test"}) }
},{
name: 'unsubscribe',
call: async(uuid) => { }
}
]
},{
name: 'Group2',
exportRPCs: () => [{
name: 'echo',
call: async (x) => x+" lol"
call: async (x) => "..."
}]
}]
)
@@ -66,9 +51,3 @@ RPCSocket.makeSocket<MyInterface>(20000, 'localhost').then((async (client) => {
await client.Group1.triggerCallbacks("Hello", "World", "Callbacks")
}))
RPCSocket.makeSocket<MyInterface>(20001, 'localhost').then((async (client) => {
console.log(client)
const r = await client.Group2.echo("hee")
console.log(r)
}))