@mingzey/typedrpc 1.0.2 → 1.1.0

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.
Files changed (94) hide show
  1. package/dist/api.d.ts +2 -2
  2. package/dist/api.d.ts.map +1 -1
  3. package/dist/client.d.ts +1 -1
  4. package/dist/connection.d.ts +1 -1
  5. package/dist/connection.js +1 -1
  6. package/dist/{connecitons → connections}/basic.d.ts +1 -0
  7. package/dist/connections/basic.d.ts.map +1 -0
  8. package/dist/{connecitons → connections}/basic.js.map +1 -1
  9. package/dist/{connecitons → connections}/http.d.ts.map +1 -1
  10. package/dist/{connecitons → connections}/http.js.map +1 -1
  11. package/dist/{connecitons → connections}/socket.d.ts.map +1 -1
  12. package/dist/{connecitons → connections}/socket.js +10 -10
  13. package/dist/{connecitons → connections}/socket.js.map +1 -1
  14. package/dist/{connecitons → connections}/socketio.d.ts.map +1 -1
  15. package/dist/{connecitons → connections}/socketio.js +10 -10
  16. package/dist/{connecitons → connections}/socketio.js.map +1 -1
  17. package/dist/context.d.ts +1 -1
  18. package/dist/core.d.ts +5 -2
  19. package/dist/core.d.ts.map +1 -1
  20. package/dist/core.js +20 -13
  21. package/dist/core.js.map +1 -1
  22. package/dist/handler.d.ts +1 -1
  23. package/dist/index.d.ts +5 -5
  24. package/dist/index.js +5 -5
  25. package/dist/server.d.ts +6 -8
  26. package/dist/server.d.ts.map +1 -1
  27. package/dist/server.js +3 -1
  28. package/dist/server.js.map +1 -1
  29. package/package.json +28 -29
  30. package/dist/connecitons/basic.d.ts.map +0 -1
  31. package/dist/test/TestCase.d.ts +0 -6
  32. package/dist/test/TestCase.d.ts.map +0 -1
  33. package/dist/test/TestCase.js +0 -5
  34. package/dist/test/TestCase.js.map +0 -1
  35. package/dist/test/authorization.d.ts +0 -8
  36. package/dist/test/authorization.d.ts.map +0 -1
  37. package/dist/test/authorization.js +0 -169
  38. package/dist/test/authorization.js.map +0 -1
  39. package/dist/test/basic.d.ts +0 -7
  40. package/dist/test/basic.d.ts.map +0 -1
  41. package/dist/test/basic.js +0 -35
  42. package/dist/test/basic.js.map +0 -1
  43. package/dist/test/context.d.ts +0 -7
  44. package/dist/test/context.d.ts.map +0 -1
  45. package/dist/test/context.js +0 -55
  46. package/dist/test/context.js.map +0 -1
  47. package/dist/test/expressmix.d.ts +0 -7
  48. package/dist/test/expressmix.d.ts.map +0 -1
  49. package/dist/test/expressmix.js +0 -63
  50. package/dist/test/expressmix.js.map +0 -1
  51. package/dist/test/full-duplex.d.ts +0 -12
  52. package/dist/test/full-duplex.d.ts.map +0 -1
  53. package/dist/test/full-duplex.js +0 -77
  54. package/dist/test/full-duplex.js.map +0 -1
  55. package/dist/test/socket.d.ts +0 -8
  56. package/dist/test/socket.d.ts.map +0 -1
  57. package/dist/test/socket.js +0 -71
  58. package/dist/test/socket.js.map +0 -1
  59. package/dist/test/socketio.d.ts +0 -12
  60. package/dist/test/socketio.d.ts.map +0 -1
  61. package/dist/test/socketio.js +0 -77
  62. package/dist/test/socketio.js.map +0 -1
  63. package/dist/test.d.ts +0 -2
  64. package/dist/test.d.ts.map +0 -1
  65. package/dist/test.js +0 -33
  66. package/dist/test.js.map +0 -1
  67. package/src/api.ts +0 -72
  68. package/src/client.ts +0 -77
  69. package/src/connecitons/basic.ts +0 -49
  70. package/src/connecitons/http.ts +0 -184
  71. package/src/connecitons/socket.ts +0 -247
  72. package/src/connecitons/socketio.ts +0 -200
  73. package/src/connection.ts +0 -10
  74. package/src/context.ts +0 -18
  75. package/src/core.ts +0 -130
  76. package/src/define.ts +0 -63
  77. package/src/handler.ts +0 -202
  78. package/src/index.ts +0 -41
  79. package/src/packet.ts +0 -77
  80. package/src/server.ts +0 -82
  81. package/src/test/TestCase.ts +0 -6
  82. package/src/test/authorization.ts +0 -197
  83. package/src/test/basic.ts +0 -44
  84. package/src/test/context.ts +0 -64
  85. package/src/test/expressmix.ts +0 -81
  86. package/src/test/socket.ts +0 -90
  87. package/src/test/socketio.ts +0 -96
  88. package/src/test.ts +0 -35
  89. package/src/utils.ts +0 -95
  90. /package/dist/{connecitons → connections}/basic.js +0 -0
  91. /package/dist/{connecitons → connections}/http.d.ts +0 -0
  92. /package/dist/{connecitons → connections}/http.js +0 -0
  93. /package/dist/{connecitons → connections}/socket.d.ts +0 -0
  94. /package/dist/{connecitons → connections}/socketio.d.ts +0 -0
package/src/handler.ts DELETED
@@ -1,202 +0,0 @@
1
-
2
- import type { TypedRPCConnection } from "./connecitons/basic.js";
3
- import { TypedRPCContextSymbol, type TypedRPCContext } from "./context.js";
4
- import { TypedRPCPacketFactory, type TypedRPCPacket, type TypedRPCRequestPacket, type TypedRPCResponsePacket } from "./packet.js";
5
- import { TypedEmitter } from "./utils.js";
6
-
7
-
8
- // interface TypedRPCHandlerMiddleware{
9
- // inbound?:(context:TypedRPCContext) => Promise<TypedRPCContext>;
10
- // outbound?:(context:TypedRPCContext) => Promise<TypedRPCContext>;
11
- // }
12
-
13
- class TypedRPCHandlerMiddleware{
14
- async inbound(context:TypedRPCContext):Promise<TypedRPCContext>{
15
- return context;
16
- }
17
- async outbound(context:TypedRPCContext):Promise<TypedRPCContext>{
18
- return context;
19
- }
20
- }
21
-
22
- type TypedRPCHandlerEvents = {
23
-
24
- }
25
- /**
26
- * 处理所有TypedRPC数据包,管理入站出站
27
- */
28
- class TypedRPCHandler{
29
-
30
- public emitter = new TypedEmitter<TypedRPCHandlerEvents>();
31
-
32
- private middlewares:TypedRPCHandlerMiddleware[] = [];
33
-
34
- private hooks:Record<string,Record<string,{
35
- handler:(...args:any[])=>any,
36
- bind:any,
37
- }>> = {};
38
-
39
- constructor(){
40
- this.use({
41
- inbound:async (context) => {
42
- // Fallback 托底返回错误
43
- if(context.inbound && !context.outbound){
44
- context.outbound = TypedRPCPacketFactory.createResponsePacket({
45
- requestId:context.inbound.id,
46
- error:"service not available or not found",
47
- })
48
- }
49
- return context;
50
- },
51
- outbound:async (context) => {
52
- return context;
53
- },
54
- })
55
- this.use({
56
- inbound:async (context) => {
57
- if(!context.inbound
58
- || !TypedRPCPacketFactory.isRequestPacket(context.inbound)){
59
- return context;
60
- }
61
- // 如果已经有出站包,直接返回
62
- if(context.outbound){
63
- return context;
64
- }
65
- const serviceName = context.inbound.serviceName;
66
- const methodName = context.inbound.methodName;
67
- const args = context.inbound.args;
68
- const hook = this.hooks[serviceName]?.[methodName];
69
- if(!hook){
70
- return context;
71
- }
72
- const result = await hook.handler.call(new Proxy(hook.bind || {},{
73
- get(target,prop){
74
- if(prop === TypedRPCContextSymbol){
75
- return context;
76
- }
77
- return Reflect.get(target,prop);
78
- }
79
- }),...args);
80
- const response = TypedRPCPacketFactory.createResponsePacket({
81
- requestId:context.inbound.id,
82
- result:result,
83
- });
84
- context.outbound = response;
85
- return context;
86
- },
87
- outbound:async (context) => {
88
- return context;
89
- },
90
- })
91
- }
92
-
93
- public use(middleware:TypedRPCHandlerMiddleware){
94
- this.middlewares.push(middleware);
95
- return this;
96
- }
97
-
98
- private async middlewareProcesser(context:TypedRPCContext,index:number,direction:'inbound'|'outbound'):Promise<TypedRPCContext>{
99
- const middleware = this.middlewares[index];
100
- if(!middleware){
101
- return context;
102
- }
103
- // 执行中间件
104
- if(direction == 'inbound'){
105
- if(middleware.inbound){
106
- context = await middleware.inbound(context);
107
- }
108
- index--;
109
- }
110
- if(direction == 'outbound'){
111
- if(middleware.outbound){
112
- context = await middleware.outbound(context);
113
- }
114
- index++;
115
- }
116
- return this.middlewareProcesser(context,index,direction);
117
- }
118
-
119
- public async outbound(context:TypedRPCContext):Promise<TypedRPCContext>{
120
- if(this.middlewares.length <= 0){
121
- return context;
122
- }
123
- return this.middlewareProcesser(context,0,'outbound');
124
- }
125
-
126
- public async inbound(context:TypedRPCContext):Promise<TypedRPCContext>{
127
- if(this.middlewares.length <= 0){
128
- return context;
129
- }
130
- return this.middlewareProcesser(context,this.middlewares.length - 1,'inbound');
131
- }
132
-
133
- public async request(connection:TypedRPCConnection,request:TypedRPCRequestPacket):Promise<TypedRPCPacket>{
134
- let context:TypedRPCContext = {
135
- connection:connection,
136
- outbound:request,
137
- }
138
- context = await this.outbound(context);
139
- if(!context.outbound){
140
- throw new Error("Request failed: outbound is empty");
141
- }
142
- const res = await connection.request(JSON.stringify(context.outbound));
143
- if(!res){
144
- throw new Error("Response is empty");
145
- }
146
- let responseObject:any | null = null;
147
- try{
148
- responseObject = JSON.parse(res);
149
- }catch(e){
150
- throw new Error("Response is not a valid JSON string:"+res);
151
- }
152
- if(!TypedRPCPacketFactory.isPacket(responseObject)){
153
- throw new Error("Response is not a TypedRPCPacket");
154
- }
155
- if(!TypedRPCPacketFactory.isResponsePacket(responseObject)){
156
- throw new Error("Response is not a TypedRPCResponsePacket");
157
- }
158
- context.inbound = responseObject;
159
- context = await this.inbound(context);
160
- if(!context.inbound){
161
- throw new Error("Request failed: inbound is empty");
162
- }
163
- return context.inbound;
164
- }
165
-
166
- public async handle(connection:TypedRPCConnection,request:TypedRPCRequestPacket,response:(packet:TypedRPCResponsePacket) => void){
167
- let context:TypedRPCContext = {
168
- connection:connection,
169
- inbound:request,
170
- }
171
- context = await this.inbound(context);
172
- if(!context.outbound){
173
- return null
174
- }
175
- context = await this.outbound(context);
176
- if(!context.outbound
177
- || !TypedRPCPacketFactory.isResponsePacket(context.outbound)){
178
- return null;
179
- }
180
- response(context.outbound);
181
- }
182
-
183
- hook(serviceName:string,methodName:string,config:{
184
- handler:(...args:any[])=>any,
185
- bind?:any,
186
- }){
187
- if(!this.hooks[serviceName]){
188
- this.hooks[serviceName] = {};
189
- }
190
- this.hooks[serviceName]![methodName] = {
191
- handler:config.handler,
192
- bind:config.bind,
193
- };
194
- }
195
-
196
-
197
- }
198
-
199
- export {
200
- TypedRPCHandler,
201
- TypedRPCHandlerMiddleware
202
- }
package/src/index.ts DELETED
@@ -1,41 +0,0 @@
1
- import { TypedRPCClient } from './client.js';
2
- import { TypedRPCServer } from './server.js';
3
- import { TypedRPCAPIDefine } from './define.js';
4
- import { TypedRPCConnectionDefault, TypedRPCConnectionProviderDefault } from './connection.js';
5
- import { TypedRPCHandlerMiddleware } from './handler.js';
6
- import { TypedRPCPacketFactory } from './packet.js';
7
- import { TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from './context.js';
8
- import { TypedRPCConnection, TypedRPCConnectionProvider } from './connecitons/basic.js';
9
- import { TypedRPCConnectionHTTP, TypedRPCConnectionProviderHTTP } from './connecitons/http.js';
10
- import { TypedRPCConnectionProviderSocket, TypedRPCConnectionSocket } from './connecitons/socket.js';
11
- import { TypedRPCConnectionSocketIO } from './connecitons/socketio.js';
12
- import { TypedRPCConnectionProviderSocketIO } from './connecitons/socketio.js';
13
-
14
- export type {
15
- TypedRPCContext,
16
- TypedRPCContextAware,
17
- }
18
-
19
- export {
20
- TypedRPCClient,
21
- TypedRPCServer,
22
- TypedRPCAPIDefine,
23
-
24
- TypedRPCConnection,
25
- TypedRPCConnectionProvider,
26
-
27
- TypedRPCConnectionDefault,
28
- TypedRPCConnectionProviderDefault,
29
- TypedRPCConnectionHTTP,
30
- TypedRPCConnectionProviderHTTP,
31
- TypedRPCConnectionSocket,
32
- TypedRPCConnectionProviderSocket,
33
- TypedRPCConnectionSocketIO,
34
- TypedRPCConnectionProviderSocketIO,
35
-
36
-
37
- TypedRPCHandlerMiddleware,
38
- TypedRPCPacketFactory,
39
-
40
- TypedRPCContextSymbol,
41
- }
package/src/packet.ts DELETED
@@ -1,77 +0,0 @@
1
- import { IdMaker } from "./utils.js";
2
-
3
- export interface TypedRPCPacket {
4
- id:string;
5
- type:string;
6
- meta?:{[key:string]:any};// 元数据,用于传递一些额外的信息
7
- }
8
-
9
- export interface TypedRPCRequestPacket extends TypedRPCPacket {
10
- type:'request';
11
- serviceName:string;
12
- methodName:string;
13
- args:any[];
14
- }
15
-
16
- export interface TypedRPCResponsePacket extends TypedRPCPacket {
17
- type:'response';
18
- requestId:string;
19
- result?:any;
20
- error?:any;
21
- }
22
-
23
-
24
-
25
- export class TypedRPCPacketFactory {
26
-
27
- static createID():string{
28
- return IdMaker.makeId();
29
- }
30
-
31
- static createRequestPacket(data:{
32
- serviceName:string,
33
- methodName:string,
34
- args:any[]
35
- }):TypedRPCRequestPacket{
36
- return {
37
- id:TypedRPCPacketFactory.createID(),
38
- type:'request',
39
- serviceName:data.serviceName,
40
- methodName:data.methodName,
41
- args:data.args,
42
- }
43
- }
44
-
45
- static createResponsePacket(data:{
46
- requestId:string,
47
- result?:any,
48
- error?:any,
49
- }):TypedRPCResponsePacket{
50
- return {
51
- id:TypedRPCPacketFactory.createID(),
52
- type:'response',
53
- requestId:data.requestId,
54
- result:data.result,
55
- error:data.error,
56
- }
57
- }
58
-
59
- static isPacket(data:any):data is TypedRPCPacket{
60
- return data && typeof data === 'object' && 'id' in data && 'type' in data;
61
- }
62
-
63
- static isRequestPacket(data:any):data is TypedRPCRequestPacket{
64
- if(!TypedRPCPacketFactory.isPacket(data)){
65
- return false;
66
- }
67
- return data.type === 'request';
68
- }
69
-
70
- static isResponsePacket(data:any):data is TypedRPCResponsePacket{
71
- if(!TypedRPCPacketFactory.isPacket(data)){
72
- return false;
73
- }
74
- return data.type === 'response';
75
- }
76
- }
77
-
package/src/server.ts DELETED
@@ -1,82 +0,0 @@
1
-
2
- import { TypedRPCAPI } from "./api.js";
3
- import type { TypedRPCConnection, TypedRPCConnectionProvider } from "./connecitons/basic.js";
4
- import { TypedRPCCore } from "./core.js";
5
- import { TypedRPCAPIDefine } from "./define.js";
6
- import { TypedEmitter, type TypedRPCDefineMethodBody, type TypedRPCDefineMethodName, type TypedRPCDefineServiceInstance, type TypedRPCDefineServiceName, type TypedRPCDefineToTypedRPCAPI } from "./utils.js";
7
-
8
- type TypedRPCServerConfig<T extends TypedRPCAPIDefine<any>,R extends TypedRPCAPIDefine<any>> = {
9
- local?:T,
10
- remote?:R,
11
- connection?:{
12
- provider:TypedRPCConnectionProvider,
13
- }
14
- }
15
-
16
- type TypedRPCServerEvents = {
17
- connection:(connection:TypedRPCConnection)=>void,
18
- }
19
-
20
- class TypedRPCServer<T extends TypedRPCAPIDefine<any>,R extends TypedRPCAPIDefine<any>> {
21
-
22
- public emitter = new TypedEmitter<TypedRPCServerEvents>();
23
- private config:TypedRPCServerConfig<T,R>;
24
- public core:TypedRPCCore;
25
-
26
- constructor(config?:TypedRPCServerConfig<T,R>){
27
- const defaultConfig:TypedRPCServerConfig<T,R> = {
28
-
29
- }
30
- this.config = {...defaultConfig,...config};
31
- this.core = new TypedRPCCore(this.config);
32
- this.core.emitter.on('connection',(connection) => {
33
- this.emitter.emit('connection',connection);
34
- })
35
- }
36
-
37
- hook<S extends TypedRPCDefineServiceName<T>,M extends TypedRPCDefineMethodName<T,S>>(serviceName:S,methodName:M,config:{
38
- handler:TypedRPCDefineMethodBody<T,S,M>,
39
- bind?:any,
40
- }){
41
- return this.core.hook(serviceName,methodName,config);
42
- }
43
-
44
- hookService<S extends TypedRPCDefineServiceName<T>>(serviceName:S,instance:TypedRPCDefineServiceInstance<T,S>){
45
- const methodList = TypedRPCAPIDefine.getMethodList(instance);
46
- for(let methodName of methodList){
47
- this.hook(serviceName,methodName as TypedRPCDefineMethodName<T,S>,{
48
- handler:instance[methodName],
49
- bind:instance,
50
- })
51
- }
52
- }
53
-
54
- get use(){
55
- return this.core.handler.use.bind(this.core.handler);
56
- }
57
-
58
- get listen(){
59
- return this.core.listen.bind(this.core);
60
- }
61
-
62
- get close(){
63
- return this.core.close.bind(this.core);
64
- }
65
-
66
- getAPI(connection:TypedRPCConnection):TypedRPCDefineToTypedRPCAPI<R>{
67
- const api = new TypedRPCAPI<R>();
68
- return api.interface(async (context) => {
69
- return await this.core.request({
70
- connection,
71
- serviceName:context.serviceName,
72
- methodName:context.methodName,
73
- args:context.args,
74
- })
75
- })
76
- }
77
-
78
- }
79
-
80
- export {
81
- TypedRPCServer,
82
- }
@@ -1,6 +0,0 @@
1
- export abstract class TestCase{
2
- abstract name():string;
3
- abstract run():Promise<boolean>;
4
- public async finally():Promise<void>{};
5
- }
6
-
@@ -1,197 +0,0 @@
1
- import { TypedRPCClient } from "../index.js";
2
- import type { TypedRPCContext } from "../index.js";
3
- import { TypedRPCAPIDefine } from "../index.js";
4
- import { TypedRPCHandlerMiddleware } from "../index.js";
5
- import { TypedRPCPacketFactory } from "../index.js";
6
- import { TypedRPCServer } from "../index.js";
7
- import { TestCase } from "./TestCase.js";
8
-
9
- interface MathServiceInterface{
10
- add:(a:number,b:number) => number,
11
- }
12
-
13
- interface AuthServiceInterface{
14
- login:(username:string,password:string) => string
15
- }
16
-
17
- const ServerAPIDefine = new TypedRPCAPIDefine<{
18
- math:MathServiceInterface,
19
- auth:AuthServiceInterface,
20
- }>()
21
-
22
- class MathService implements MathServiceInterface{
23
- @TypedRPCAPIDefine.method()
24
- add(a: number, b: number): number {
25
- return a+b;
26
- }
27
- }
28
-
29
- class AuthService implements AuthServiceInterface{
30
-
31
- private authUsers = new Map<string,string>();
32
-
33
- @TypedRPCAPIDefine.method()
34
- login(username: string, password: string): string {
35
- if(username === 'admin' && password === '123456'){
36
- const token = Math.random().toString(36).substring(2);
37
- this.authUsers.set(username,token);
38
- return token;
39
- }
40
- return '';
41
- }
42
-
43
- checkToken(username:string,token:string):boolean{
44
- return this.authUsers.get(username) === token;
45
- }
46
- }
47
-
48
- class AuthMiddleware extends TypedRPCHandlerMiddleware{
49
-
50
- private username?:string;
51
- private token?:string;
52
-
53
- constructor(
54
- private auth?:AuthService
55
- ){
56
- super();
57
- }
58
-
59
- /**
60
- * 对外站进入的请求包进行验证
61
- */
62
- async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
63
- if(context.inbound){
64
- if(TypedRPCPacketFactory.isRequestPacket(context.inbound)){
65
- const protectService = ['math'];
66
- if(protectService.includes(context.inbound.serviceName)){
67
- const unauthorizedPacket = TypedRPCPacketFactory.createResponsePacket({
68
- requestId:context.inbound.id,
69
- error:"unauthorized",
70
- })
71
- // 进行验证
72
- const username = context.inbound.meta?.username;
73
- const token = context.inbound.meta?.token;
74
- if(!token || !username){
75
- context.outbound = unauthorizedPacket;
76
- }else{
77
- if(!this.auth || !this.auth.checkToken(username,token)){
78
- context.outbound = unauthorizedPacket;
79
- }
80
- }
81
-
82
- }
83
- }
84
- // 如果入站的是响应包,判断出站是否是登录包
85
- if(TypedRPCPacketFactory.isResponsePacket(context.inbound)
86
- && TypedRPCPacketFactory.isRequestPacket(context.outbound)
87
- && context.outbound.serviceName == 'auth'
88
- && context.outbound.methodName == 'login'
89
- ){
90
- const username = context.outbound.args?.[0] as string;
91
- const token = context.inbound.result as string;
92
- this.token = token;
93
- this.username = username;
94
- }
95
- }
96
- return context;
97
- }
98
-
99
- async outbound(context: TypedRPCContext): Promise<TypedRPCContext> {
100
- if(context.outbound){
101
- if(TypedRPCPacketFactory.isRequestPacket(context.outbound)){
102
- // 如果有token,附带token
103
- if(this.token){
104
- context.outbound.meta = {
105
- username:this.username,
106
- token:this.token,
107
- }
108
- }
109
- }
110
- }
111
- return context;
112
- }
113
-
114
- }
115
-
116
- class ServerApp{
117
- private math = new MathService();
118
- private auth = new AuthService();
119
- private server = new TypedRPCServer({
120
- local:ServerAPIDefine,
121
- })
122
-
123
- constructor(){
124
- this.server.use(new AuthMiddleware(this.auth));
125
- this.server.hookService('math',this.math);
126
- this.server.hookService('auth',this.auth);
127
- }
128
-
129
- async start(){
130
- return this.server.listen({
131
- port:3698,
132
- })
133
- }
134
-
135
- async stop(){
136
- return this.server.close();
137
- }
138
- }
139
-
140
- const serverApp = new ServerApp();
141
-
142
-
143
- class TestAuthorization extends TestCase{
144
- name(): string {
145
- return 'Authorization';
146
- }
147
- async run(): Promise<boolean> {
148
- await serverApp.start();
149
-
150
- const client = new TypedRPCClient({
151
- remote:ServerAPIDefine,
152
- })
153
- client.use(new AuthMiddleware());
154
- const connection = await client.connect('localhost:3698');
155
- const api = client.getAPI(connection);
156
-
157
- // 未登录调用
158
- const unauthorizedResult = await new Promise((resolve) => {
159
- api.math.add.request({
160
- args:[1,2],
161
- callback(result,req,res) {
162
- resolve(result);
163
- },
164
- error(error,req,res) {
165
- resolve(error);
166
- },
167
- })
168
- })
169
- if(unauthorizedResult != 'unauthorized'){
170
- console.log('Unauthorized call result should be "unauthorized"');
171
- return false;
172
- }
173
-
174
- // 进行登录
175
- const token = await api.auth.login.call('admin','123456');
176
- if(!token){
177
- console.log('Login failed');
178
- return false;
179
- }
180
-
181
- // 重新请求
182
- const authorizedResult = await api.math.add.call(1,2);
183
- if(authorizedResult != 3){
184
- console.log('Authorized call result should be 3');
185
- return false;
186
- }
187
- return true;
188
- }
189
-
190
- public async finally(): Promise<void> {
191
- await serverApp.stop();
192
- return;
193
- }
194
-
195
- }
196
-
197
- export default TestAuthorization;
package/src/test/basic.ts DELETED
@@ -1,44 +0,0 @@
1
- import { TypedRPCClient } from "../index.js";
2
- import { TypedRPCAPIDefine } from "../index.js";
3
- import { TypedRPCServer } from "../index.js";
4
- import { TestCase } from "./TestCase.js";
5
-
6
- const serverAPIDefine = new TypedRPCAPIDefine<{
7
- math:{
8
- add(a:number,b:number):number,
9
- },
10
- }>();
11
-
12
- const server = new TypedRPCServer({
13
- local:serverAPIDefine,
14
- });
15
-
16
- server.hook('math','add',{
17
- handler:(a,b)=>a+b,
18
- });
19
-
20
- const client = new TypedRPCClient({
21
- remote:serverAPIDefine,
22
- });
23
-
24
- export default class TestBasic extends TestCase{
25
- name(): string {
26
- return 'Basic';
27
- }
28
- async run(): Promise<boolean> {
29
- await server.listen({
30
- port:3698,
31
- })
32
- const connection = await client.connect("localhost:3698");
33
- const api = client.getAPI(connection);
34
- const result = await api.math.add.call(1,2);
35
- if(result == 3){
36
- return true;
37
- }
38
- return false;
39
- }
40
-
41
- public async finally(): Promise<void> {
42
- await server.close();
43
- }
44
- }