@mingzey/typedrpc 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/api.ts +72 -0
- package/src/client.ts +77 -0
- package/src/connecitons/basic.ts +49 -0
- package/src/connecitons/http.ts +184 -0
- package/src/connecitons/socket.ts +247 -0
- package/src/connecitons/socketio.ts +200 -0
- package/src/connection.ts +10 -0
- package/src/context.ts +18 -0
- package/src/core.ts +130 -0
- package/src/define.ts +63 -0
- package/src/handler.ts +202 -0
- package/src/index.ts +41 -0
- package/src/packet.ts +77 -0
- package/src/server.ts +82 -0
- package/src/test/TestCase.ts +6 -0
- package/src/test/authorization.ts +197 -0
- package/src/test/basic.ts +44 -0
- package/src/test/context.ts +64 -0
- package/src/test/expressmix.ts +81 -0
- package/src/test/socket.ts +90 -0
- package/src/test/socketio.ts +96 -0
- package/src/test.ts +35 -0
- package/src/utils.ts +95 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试全双工通信实现双向调用
|
|
3
|
+
* TypedRPC的默认实现是基于http的,全双工必须实现自定义的Provider
|
|
4
|
+
* 这里使用socket.io作为Provider的底层通讯
|
|
5
|
+
*/
|
|
6
|
+
import { TestCase } from "./TestCase.js";
|
|
7
|
+
import { TypedRPCAPIDefine, TypedRPCConnectionProviderSocketIO } from "../index.js";
|
|
8
|
+
import { TypedRPCServer } from "../index.js";
|
|
9
|
+
import { TypedRPCClient } from "../index.js";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
14
|
+
math:{
|
|
15
|
+
add:(a:number,b:number) => number,
|
|
16
|
+
}
|
|
17
|
+
}>
|
|
18
|
+
|
|
19
|
+
const ClientAPIDefine = new TypedRPCAPIDefine<{
|
|
20
|
+
status:{
|
|
21
|
+
time:() => number,
|
|
22
|
+
}
|
|
23
|
+
}>
|
|
24
|
+
|
|
25
|
+
const server = new TypedRPCServer({
|
|
26
|
+
local:ServerAPIDefine,
|
|
27
|
+
remote:ClientAPIDefine,
|
|
28
|
+
connection:{
|
|
29
|
+
provider:new TypedRPCConnectionProviderSocketIO(),
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
server.hook('math','add',{
|
|
34
|
+
handler:(a,b) => {
|
|
35
|
+
return a + b;
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const client = new TypedRPCClient({
|
|
40
|
+
local:ClientAPIDefine,
|
|
41
|
+
remote:ServerAPIDefine,
|
|
42
|
+
connection:{
|
|
43
|
+
provider:new TypedRPCConnectionProviderSocketIO(),
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
client.hook('status','time',{
|
|
48
|
+
handler:() => {
|
|
49
|
+
return Date.now();
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
export default class TestSocketIO extends TestCase{
|
|
54
|
+
name(): string {
|
|
55
|
+
return "SocketIO";
|
|
56
|
+
}
|
|
57
|
+
async run(): Promise<boolean> {
|
|
58
|
+
let requestToServer:Promise<boolean>;
|
|
59
|
+
let requestToClient:Promise<boolean>;
|
|
60
|
+
|
|
61
|
+
requestToClient = new Promise<boolean>((resolve) => {
|
|
62
|
+
server.emitter.on('connection',async (connectionToClient) => {
|
|
63
|
+
const apiToClient = server.getAPI(connectionToClient);
|
|
64
|
+
const result = await apiToClient.status.time.call();
|
|
65
|
+
if(result != null){
|
|
66
|
+
resolve(true);
|
|
67
|
+
}else{
|
|
68
|
+
resolve(false);
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
await server.listen({
|
|
74
|
+
port:3698,
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
requestToServer = new Promise<boolean>(async (resolve) => {
|
|
78
|
+
const connectionToServer = await client.connect("localhost:3698");
|
|
79
|
+
const apiToServer = client.getAPI(connectionToServer);
|
|
80
|
+
const result = await apiToServer.math.add.call(1,2);
|
|
81
|
+
if(result == 3){
|
|
82
|
+
resolve(true);
|
|
83
|
+
}else{
|
|
84
|
+
resolve(false);
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const resultToClient = await requestToClient;
|
|
89
|
+
const resultToServer = await requestToServer;
|
|
90
|
+
return resultToClient && resultToServer;
|
|
91
|
+
}
|
|
92
|
+
public async finally(): Promise<void> {
|
|
93
|
+
await server.close();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { TestCase } from './test/TestCase.js';
|
|
2
|
+
|
|
3
|
+
const testList = [
|
|
4
|
+
import('./test/basic.js'),
|
|
5
|
+
import('./test/context.js'),
|
|
6
|
+
import('./test/socketio.js'),
|
|
7
|
+
import('./test/authorization.js'),
|
|
8
|
+
import('./test/expressmix.js'),
|
|
9
|
+
import('./test/socket.js'),
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
for(let i of testList){
|
|
14
|
+
let exp = await i;
|
|
15
|
+
let test = new (exp.default)() as TestCase;
|
|
16
|
+
let result = await test.run().catch((e) => {
|
|
17
|
+
console.log(`Test:${test.name()} failed. ${e}`);
|
|
18
|
+
throw e;
|
|
19
|
+
})
|
|
20
|
+
await test.finally();
|
|
21
|
+
if(result){
|
|
22
|
+
console.log(`\x1b[32m[PASS]\x1b[0m ${test.name()}`);
|
|
23
|
+
}else{
|
|
24
|
+
console.log(`\x1b[31m[FAIL]\x1b[0m ${test.name()}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
main().then(() => {
|
|
31
|
+
console.log('TypedRPC test finished.');
|
|
32
|
+
}).catch((e) => {
|
|
33
|
+
console.log(`TypedRPC test failed. ${e}`);
|
|
34
|
+
throw e;
|
|
35
|
+
})
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { TypedRPCAPIDefine } from "./define.js";
|
|
2
|
+
import type { TypedRPCRequestPacket, TypedRPCResponsePacket } from "./packet.js";
|
|
3
|
+
|
|
4
|
+
export type FunctionTypeToPromiseFunctionType<T extends (...args: any[]) => any> =
|
|
5
|
+
T extends (...args: infer P) => Promise<infer R> ?
|
|
6
|
+
(...args: P) => Promise<R | undefined>
|
|
7
|
+
: T extends (...args: infer P) => infer R ?
|
|
8
|
+
(...args: P) => Promise<R | undefined>
|
|
9
|
+
: never
|
|
10
|
+
;
|
|
11
|
+
|
|
12
|
+
export type TypedRPCDefineToTypedRPCAPI<T extends TypedRPCAPIDefine<any>> = T extends TypedRPCAPIDefine<infer U> ?
|
|
13
|
+
{
|
|
14
|
+
[S in keyof U]:{
|
|
15
|
+
[M in keyof U[S]]:{
|
|
16
|
+
/** 调用该方法 */
|
|
17
|
+
call:FunctionTypeToPromiseFunctionType<U[S][M]>,
|
|
18
|
+
/** 获取方法id */
|
|
19
|
+
request:(config:{
|
|
20
|
+
args:Parameters<U[S][M]>,
|
|
21
|
+
callback?:(result:ReturnType<U[S][M]>,req:TypedRPCRequestPacket,res:TypedRPCResponsePacket) => void,
|
|
22
|
+
error?:(error:any,req:TypedRPCRequestPacket,res:TypedRPCResponsePacket) => void,
|
|
23
|
+
}) => string,
|
|
24
|
+
id:string,
|
|
25
|
+
/** 获取方法路径 */
|
|
26
|
+
path:string,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
: never;
|
|
31
|
+
|
|
32
|
+
export type TypedRPCDefineServiceName<T extends TypedRPCAPIDefine<any>> = T extends TypedRPCAPIDefine<infer U> ? keyof U & string : never;
|
|
33
|
+
export type TypedRPCDefineMethodName<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>> = T extends TypedRPCAPIDefine<infer U> ? keyof U[S] & string: never;
|
|
34
|
+
export type TypedRPCDefineMethodBody<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>,M extends TypedRPCDefineMethodName<T,S>> = T extends TypedRPCAPIDefine<infer U> ? U[S][M] : never;
|
|
35
|
+
export type TypedRPCDefineServiceInstance<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>> = T extends TypedRPCAPIDefine<infer U> ? U[S] : never;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
export type TypedEmitterEvents = {
|
|
39
|
+
[key:string]:(...args:any[]) => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class TypedEmitter<T extends TypedEmitterEvents> {
|
|
43
|
+
|
|
44
|
+
private record:Map<keyof T,Set<T[keyof T]>> = new Map();
|
|
45
|
+
|
|
46
|
+
on<K extends keyof T>(event:K,callback:T[K]){
|
|
47
|
+
if(!this.record.has(event)){
|
|
48
|
+
this.record.set(event,new Set());
|
|
49
|
+
}
|
|
50
|
+
this.record.get(event)!.add(callback);
|
|
51
|
+
return () => {
|
|
52
|
+
this.off(event,callback);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
off<K extends keyof T>(event:K,callback:T[K]){
|
|
57
|
+
if(!this.record.has(event)){
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
this.record.get(event)!.delete(callback);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
emit<K extends keyof T>(event:K,...args:Parameters<T[K]>){
|
|
64
|
+
if(!this.record.has(event)){
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
for(const callback of this.record.get(event)!){
|
|
68
|
+
callback(...args);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
once<K extends keyof T>(event:K,callback:T[K]){
|
|
73
|
+
const wrapper = (...args:Parameters<T[K]>) => {
|
|
74
|
+
callback(...args);
|
|
75
|
+
this.off(event,wrapper as T[K]);
|
|
76
|
+
}
|
|
77
|
+
this.on(event,wrapper as T[K]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class IdMaker{
|
|
82
|
+
|
|
83
|
+
public static instance = new IdMaker();
|
|
84
|
+
|
|
85
|
+
static makeId():string{
|
|
86
|
+
return IdMaker.instance.makeId();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public makeId():string{
|
|
90
|
+
// 生成12位随机字符串
|
|
91
|
+
const suffix = Math.random().toString(36).substring(2,10);
|
|
92
|
+
const timestamp = Date.now().toString(36);
|
|
93
|
+
return `${timestamp}${suffix}`;
|
|
94
|
+
}
|
|
95
|
+
}
|