new readme and some small fixes

This commit is contained in:
2020-03-18 03:17:31 +01:00
parent 7d580c4c23
commit 81e2115ad9
8 changed files with 526 additions and 132 deletions
-4
View File
@@ -6,10 +6,6 @@ import * as T from './Types';
import * as U from './Utils';
import * as I from './Interfaces';
/**
* A Websocket-server-on-steroids with built-in RPC capabilities
*/
export class RPCServer<
InterfaceT extends T.RPCInterface = T.RPCInterface,
> implements I.Destroyable {
+4 -4
View File
@@ -13,8 +13,8 @@ import { stripAfterEquals, appendComma } from './Utils';
export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I.Socket{
static async makeSocket<T extends T.RPCInterface = T.RPCInterface>(port:number, server: string, sesame?:string, conf?:T.SocketConf): Promise<T.ConnectedSocket<T>> {
const socket = new RPCSocket(port, server, conf)
return await socket.connect<T>(sesame)
const socket = new RPCSocket<T>(port, server, conf)
return await socket.connect(sesame)
}
private socket: I.Socket
@@ -128,7 +128,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
/**
* Connects to the server and attaches available RPCs to this object
*/
public async connect<T extends T.RPCInterface= Ifc>( sesame?: string ) : Promise<T.ConnectedSocket<T>> {
public async connect( sesame?: string ) : Promise<T.ConnectedSocket<Ifc>> {
this.socket = await bsock.connect(this.port, this.server, this.conf.tls?this.conf.tls:false)
this.errorHandlers.forEach(h => this.socket.on('error', h))
this.closeHandlers.forEach(h => this.socket.on('close', h))
@@ -153,7 +153,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
this[i.owner][i.name] = f
this[i.owner][i.name].bind(this)
})
return <RPCSocket & T.RPCInterface<T>> (this as any)
return <T.ConnectedSocket<Ifc>> (this as any)
}
/**
+1 -1
View File
@@ -6,7 +6,7 @@ import * as I from "./Interfaces"
*/
export type RPCExporter<
Ifc extends T.RPCInterface = T.RPCInterface,
Name extends keyof Ifc = string,
Name extends keyof Ifc = keyof Ifc,
> = {
name: Name
exportRPCs() : T.RPCDefinitions<Ifc>[Name]
+7 -4
View File
@@ -3,13 +3,12 @@ import { RPCSocket } from "./Frontend";
export type AnyFunction = (...args:any) => any
export type HookFunction = AnyFunction
export type AccessFilter<InterfaceT extends RPCInterface = RPCInterface> = (sesame:string|undefined, exporter: I.RPCExporter<InterfaceT, keyof InterfaceT>) => Promise<boolean>
export type AccessFilter<InterfaceT extends RPCInterface = RPCInterface> = (sesame:string|undefined, exporter: I.RPCExporter<InterfaceT, keyof InterfaceT>) => Promise<boolean> | boolean
export type Visibility = "127.0.0.1" | "0.0.0.0"
export type ConnectionHandler = (socket:I.Socket) => void
export type ErrorHandler = (socket:I.Socket, error:any, rpcName: string, args: any[]) => void
export type CloseHandler = (socket:I.Socket) => void
export type SesameFunction = (sesame : string) => boolean
export type ExceptionHandling = 'local' | 'remote'
export type SesameConf = {
sesame?: string | SesameFunction
}
@@ -20,7 +19,7 @@ export type FrontEndHandlerType = {
export type ExporterArray<InterfaceT extends RPCInterface = RPCInterface> = I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT>[]
export type ConnectedSocket<T extends RPCInterface = RPCInterface> = RPCSocket & T
export type ConnectedSocket<T extends RPCInterface = RPCInterface> = RPCSocket & AsyncIfc<T>
export type ServerConf<InterfaceT extends RPCInterface> = {
accessFilter?: AccessFilter<InterfaceT>
@@ -28,7 +27,6 @@ export type ServerConf<InterfaceT extends RPCInterface> = {
errorHandler?: ErrorHandler
closeHandler?: CloseHandler
visibility?: Visibility
exceptionHandling?: ExceptionHandling
} & SesameConf
export type SocketConf = {
@@ -95,3 +93,8 @@ export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
export type OnFunction = <T extends "error" | "close">(type: T, f: FrontEndHandlerType[T]) => void
export type HookCloseFunction<T> = (res: T, rpc:HookRPC<any, any>) => any
export type AsyncIfc<Ifc extends RPCInterface> = { [grp in keyof Ifc]: {[rpcname in keyof Ifc[grp]] : AsyncAnyFunction<Ifc[grp][rpcname]> } }
export type AsyncAnyFunction<F extends AnyFunction = AnyFunction> = F extends (...args: Parameters<F>) => infer R ? ((...args: Parameters<F>) => R extends Promise<any> ? R : Promise<R> ) : Promise<any>