@mingzey/typedrpc 1.0.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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +228 -0
  3. package/README.zh-CN.md +228 -0
  4. package/dist/api.d.ts +16 -0
  5. package/dist/api.d.ts.map +1 -0
  6. package/dist/api.js +52 -0
  7. package/dist/api.js.map +1 -0
  8. package/dist/client.d.ts +28 -0
  9. package/dist/client.d.ts.map +1 -0
  10. package/dist/client.js +50 -0
  11. package/dist/client.js.map +1 -0
  12. package/dist/connecitons/basic.d.ts +42 -0
  13. package/dist/connecitons/basic.d.ts.map +1 -0
  14. package/dist/connecitons/basic.js +9 -0
  15. package/dist/connecitons/basic.js.map +1 -0
  16. package/dist/connecitons/http.d.ts +38 -0
  17. package/dist/connecitons/http.d.ts.map +1 -0
  18. package/dist/connecitons/http.js +143 -0
  19. package/dist/connecitons/http.js.map +1 -0
  20. package/dist/connecitons/socket.d.ts +33 -0
  21. package/dist/connecitons/socket.d.ts.map +1 -0
  22. package/dist/connecitons/socket.js +210 -0
  23. package/dist/connecitons/socket.js.map +1 -0
  24. package/dist/connecitons/socketio.d.ts +32 -0
  25. package/dist/connecitons/socketio.d.ts.map +1 -0
  26. package/dist/connecitons/socketio.js +162 -0
  27. package/dist/connecitons/socketio.js.map +1 -0
  28. package/dist/connection.d.ts +5 -0
  29. package/dist/connection.d.ts.map +1 -0
  30. package/dist/connection.js +5 -0
  31. package/dist/connection.js.map +1 -0
  32. package/dist/context.d.ts +13 -0
  33. package/dist/context.d.ts.map +1 -0
  34. package/dist/context.js +2 -0
  35. package/dist/context.js.map +1 -0
  36. package/dist/core.d.ts +40 -0
  37. package/dist/core.d.ts.map +1 -0
  38. package/dist/core.js +97 -0
  39. package/dist/core.js.map +1 -0
  40. package/dist/define.d.ts +14 -0
  41. package/dist/define.d.ts.map +1 -0
  42. package/dist/define.js +40 -0
  43. package/dist/define.js.map +1 -0
  44. package/dist/handler.d.ts +30 -0
  45. package/dist/handler.d.ts.map +1 -0
  46. package/dist/handler.js +172 -0
  47. package/dist/handler.js.map +1 -0
  48. package/dist/index.d.ts +15 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +14 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/packet.d.ts +36 -0
  53. package/dist/packet.d.ts.map +1 -0
  54. package/dist/packet.js +40 -0
  55. package/dist/packet.js.map +1 -0
  56. package/dist/server.d.ts +34 -0
  57. package/dist/server.d.ts.map +1 -0
  58. package/dist/server.js +51 -0
  59. package/dist/server.js.map +1 -0
  60. package/dist/test/TestCase.d.ts +6 -0
  61. package/dist/test/TestCase.d.ts.map +1 -0
  62. package/dist/test/TestCase.js +5 -0
  63. package/dist/test/TestCase.js.map +1 -0
  64. package/dist/test/authorization.d.ts +8 -0
  65. package/dist/test/authorization.d.ts.map +1 -0
  66. package/dist/test/authorization.js +169 -0
  67. package/dist/test/authorization.js.map +1 -0
  68. package/dist/test/basic.d.ts +7 -0
  69. package/dist/test/basic.d.ts.map +1 -0
  70. package/dist/test/basic.js +35 -0
  71. package/dist/test/basic.js.map +1 -0
  72. package/dist/test/context.d.ts +7 -0
  73. package/dist/test/context.d.ts.map +1 -0
  74. package/dist/test/context.js +55 -0
  75. package/dist/test/context.js.map +1 -0
  76. package/dist/test/expressmix.d.ts +7 -0
  77. package/dist/test/expressmix.d.ts.map +1 -0
  78. package/dist/test/expressmix.js +63 -0
  79. package/dist/test/expressmix.js.map +1 -0
  80. package/dist/test/full-duplex.d.ts +12 -0
  81. package/dist/test/full-duplex.d.ts.map +1 -0
  82. package/dist/test/full-duplex.js +77 -0
  83. package/dist/test/full-duplex.js.map +1 -0
  84. package/dist/test/socket.d.ts +8 -0
  85. package/dist/test/socket.d.ts.map +1 -0
  86. package/dist/test/socket.js +71 -0
  87. package/dist/test/socket.js.map +1 -0
  88. package/dist/test.d.ts +2 -0
  89. package/dist/test.d.ts.map +1 -0
  90. package/dist/test.js +33 -0
  91. package/dist/test.js.map +1 -0
  92. package/dist/utils.d.ts +40 -0
  93. package/dist/utils.d.ts.map +1 -0
  94. package/dist/utils.js +46 -0
  95. package/dist/utils.js.map +1 -0
  96. package/package.json +27 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # TypedRPC
2
+
3
+ TypeScript-based RPC framework with support for multiple connection types including HTTP, Socket, and SocketIO.
4
+
5
+ ## Features
6
+
7
+ - **Type-Safe RPC calls** - Leverage TypeScript's type system for end-to-end type safety
8
+ - **Multiple connection types** - Support for HTTP, Socket, and SocketIO connections, or custom connection providers
9
+ - **Middleware support** - Extensible middleware system for request/response handling
10
+ - **Context-aware** - Built-in context system for passing data between handlers
11
+ - **Bidirectional communication** - Support for two-way RPC calls between client and server, capability depends on connection type
12
+ - **Easy to use** - Simple API for defining services and methods
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install typedrpc
18
+ ```
19
+
20
+ ## Basic Usage
21
+
22
+ ### Server Setup
23
+
24
+ ```typescript
25
+ import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
26
+
27
+ // Define your API interface
28
+ const ServerAPIDefine = new TypedRPCAPIDefine<{
29
+ // Service layer - Define your services here or use interface inheritance
30
+ math:{
31
+ // Method layer - Define your methods here
32
+ add(a:number,b:number):number,
33
+ },
34
+ }>();
35
+
36
+ // Create server instance
37
+ const server = new TypedRPCServer({
38
+ // let typescript infer the type from ServerAPIDefine
39
+ local:ServerAPIDefine,
40
+ // Connection layer - use your connection provider here
41
+ // connection:{
42
+ // provider:new TypedRPCConnectionProviderHTTP(),
43
+ // }
44
+ });
45
+
46
+ // Hook service methods
47
+ server.hook('math','add',{
48
+ handler:(a,b)=>a+b,
49
+ });
50
+
51
+ // Start listening
52
+ server.listen({
53
+ port:3698,
54
+ })
55
+ ```
56
+
57
+ ### Client Setup
58
+
59
+ ```typescript
60
+ import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
61
+ // Reuse or import the same API definition
62
+ const ServerAPIDefine = new TypedRPCAPIDefine<{
63
+ math:{
64
+ add(a:number,b:number):number,
65
+ },
66
+ }>();
67
+
68
+ // Create client instance
69
+ const client = new TypedRPCClient({
70
+ // let typescript infer the type from ServerAPIDefine
71
+ remote:ServerAPIDefine,
72
+ });
73
+
74
+ // Connect to server
75
+ const connection = await client.connect("localhost:3698");
76
+
77
+ // Get API instance
78
+ const api = client.getAPI(connection);
79
+
80
+ // Make RPC calls
81
+ const result = await api.math.add.call(1,2); // Returns 3
82
+ ```
83
+
84
+ ## Connection Types
85
+
86
+ TypedRPC supports multiple connection types:
87
+
88
+ 1. **HTTP** - RESTful HTTP API
89
+ 2. **Socket** - Raw socket connections
90
+ 3. **SocketIO** - Socket.IO connections
91
+ 4. **Custom** - User-defined connection providers, see abstract class `TypedRPCConnectionProvider`
92
+
93
+ ```typescript
94
+ new TypedRPCServer({
95
+ local:ServerAPIDefine,
96
+ connection:{
97
+ provider:new TypedRPCConnectionProviderHTTP(),
98
+ // provider:new TypedRPCConnectionProviderSocket(),
99
+ // provider:new TypedRPCConnectionProviderSocketIO(),
100
+ // provider:implaement your own connection provider
101
+ }
102
+ })
103
+ ```
104
+
105
+ ## API Documentation
106
+
107
+ ### Server API
108
+
109
+ - `new TypedRPCServer(config)` - Create a new server instance
110
+ - `server.hook(serviceName, methodName, config)` - Hook a single method
111
+ - `server.hookService(serviceName, instance)` - Hook an entire service
112
+ - `server.use(middleware)` - Add middleware
113
+ - `server.listen()` - Start listening for connections
114
+ - `server.close()` - Close the server
115
+
116
+ ### Client API
117
+
118
+ - `new TypedRPCClient(config)` - Create a new client instance
119
+ - `client.connect()` - Connect to the server
120
+ - `client.getAPI(connection)` - Get API instance for a connection
121
+ - `client.use(middleware)` - Add middleware
122
+
123
+ ## Middleware Usage
124
+
125
+ TypedRPC supports middleware for both servers and clients:
126
+
127
+ ```typescript
128
+ class MyMiddleware extends TypedRPCHandlerMiddleware{
129
+
130
+ async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
131
+ // Do something when inbound packet
132
+ return context;
133
+ }
134
+
135
+ async outbound(context: TypedRPCContext): Promise<TypedRPCContext> {
136
+ // Do something when outbound packet
137
+ return context;
138
+ }
139
+
140
+ }
141
+ ```
142
+
143
+ ## Context Usage
144
+
145
+ TypedRPC provides a built-in context system that allows you to access request context in your service methods. This is particularly useful for accessing connection information, authentication data, or other request-specific data.
146
+
147
+ ### Example: Using Context in Services
148
+
149
+ ```typescript
150
+ // Server-side code
151
+ import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
152
+
153
+ // Define service interface
154
+ interface MathServiceInterface {
155
+ add(a: number, b: number):number;
156
+ }
157
+
158
+ // Create API definition
159
+ const serverAPIDefine = new TypedRPCAPIDefine<{
160
+ math: MathServiceInterface,
161
+ }>();
162
+
163
+ // Create server instance
164
+ const server = new TypedRPCServer({
165
+ local: serverAPIDefine,
166
+ });
167
+
168
+ // Implement service with context awareness
169
+ class MathService implements MathServiceInterface, TypedRPCContextAware {
170
+ // Inject context using TypedRPCContextSymbol
171
+ [TypedRPCContextSymbol]: TypedRPCContext | null = null;
172
+
173
+ // For safety, must use @TypedRPCAPIDefine.method() to mark method as RPC method in service usage
174
+ @TypedRPCAPIDefine.method()
175
+ add(a: number, b: number): number {
176
+ // Access context
177
+ const context = this[TypedRPCContextSymbol];
178
+ if (!context) {
179
+ throw new Error('Context is not available');
180
+ }
181
+
182
+ // Use context information (e.g., connection details, authentication)
183
+ console.log('Request received from:', context.connection);
184
+
185
+ return a + b;
186
+ }
187
+ }
188
+
189
+ // Hook service to server, only methods marked with @TypedRPCAPIDefine.method() will be hooked
190
+ server.hookService('math', new MathService());
191
+
192
+ // Start server
193
+ server.listen({
194
+ port: 3698,
195
+ });
196
+ ```
197
+
198
+ ```typescript
199
+ // Client-side code
200
+ import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
201
+
202
+ // Reuse API definition
203
+ const serverAPIDefine = new TypedRPCAPIDefine<{
204
+ math: MathServiceInterface,
205
+ }>();
206
+
207
+ // Create client
208
+ const client = new TypedRPCClient({
209
+ remote: serverAPIDefine,
210
+ });
211
+
212
+ // Connect and make RPC call
213
+ const connection = await client.connect("localhost:3698");
214
+ const api = client.getAPI(connection);
215
+ const result = await api.math.add.call(1, 2); // Returns 3
216
+ ```
217
+
218
+ ## More Usage
219
+
220
+ see `./test/*.ts` for more usage.
221
+
222
+ ## Contributing
223
+
224
+ Contributions are welcome! Please feel free to submit a Pull Request.
225
+
226
+ ## License
227
+
228
+ MIT License - see the [LICENSE](https://github.com/TypedRPC/TypedRPC/blob/main/LICENSE) file for details.
@@ -0,0 +1,228 @@
1
+ # TypedRPC
2
+
3
+ 基于 TypeScript 的 RPC 框架,支持多种连接类型,包括 HTTP、Socket 和 SocketIO。
4
+
5
+ ## 特性
6
+
7
+ - **类型安全的 RPC 调用** - 利用 TypeScript 的类型系统实现端到端的类型安全
8
+ - **多种连接类型** - 支持 HTTP、Socket 和 SocketIO 连接,或自定义`TypedRPCConnectionProvider`
9
+ - **中间件支持** - 可扩展的中间件系统,用于请求/响应处理
10
+ - **上下文感知** - 内置上下文系统,用于在处理程序之间传递数据
11
+ - **双向通信** - 支持客户端和服务器之间的双向 RPC 调用,能力取决于连接类型
12
+ - **易于使用** - 用于定义服务和方法的简单 API
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ npm install typedrpc
18
+ ```
19
+
20
+ ## 基本使用
21
+
22
+ ### 服务器设置
23
+
24
+ ```typescript
25
+ import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
26
+
27
+ // 定义 API 接口
28
+ const ServerAPIDefine = new TypedRPCAPIDefine<{
29
+ // 服务层 - 在此处定义服务或使用接口继承
30
+ math:{
31
+ // 方法层 - 在此处定义方法
32
+ add(a:number,b:number):number,
33
+ },
34
+ }>();
35
+
36
+ // 创建服务器实例
37
+ const server = new TypedRPCServer({
38
+ // 让 TypeScript 从 ServerAPIDefine 推断类型
39
+ local:ServerAPIDefine,
40
+ // 连接层 - 在此处使用TypedRPCConnectionProvider的实现类
41
+ // connection:{
42
+ // provider:new TypedRPCConnectionProviderHTTP(),
43
+ // }
44
+ });
45
+
46
+ // 挂钩服务方法
47
+ server.hook('math','add',{
48
+ handler:(a,b)=>a+b,
49
+ });
50
+
51
+ // 开始监听
52
+ server.listen({
53
+ port:3698,
54
+ })
55
+ ```
56
+
57
+ ### 客户端设置
58
+
59
+ ```typescript
60
+ import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
61
+ // 重用或导入相同的 API 定义
62
+ const ServerAPIDefine = new TypedRPCAPIDefine<{
63
+ math:{
64
+ add(a:number,b:number):number,
65
+ },
66
+ }>();
67
+
68
+ // 创建客户端实例
69
+ const client = new TypedRPCClient({
70
+ // 让 TypeScript 从 ServerAPIDefine 推断类型
71
+ remote:ServerAPIDefine,
72
+ });
73
+
74
+ // 连接到服务器
75
+ const connection = await client.connect("localhost:3698");
76
+
77
+ // 获取 API 实例
78
+ const api = client.getAPI(connection);
79
+
80
+ // 进行 RPC 调用
81
+ const result = await api.math.add.call(1,2); // 返回 3
82
+ ```
83
+
84
+ ## 连接类型
85
+
86
+ TypedRPC 支持多种连接类型:
87
+
88
+ 1. **HTTP** - RESTful HTTP API
89
+ 2. **Socket** - 原始套接字连接
90
+ 3. **SocketIO** - Socket.IO 连接
91
+ 4. **自定义** - 用户定义的连接提供者,参见抽象类 `TypedRPCConnectionProvider`
92
+
93
+ ```typescript
94
+ new TypedRPCServer({
95
+ local:ServerAPIDefine,
96
+ connection:{
97
+ provider:new TypedRPCConnectionProviderHTTP(),
98
+ // provider:new TypedRPCConnectionProviderSocket(),
99
+ // provider:new TypedRPCConnectionProviderSocketIO(),
100
+ // provider:实现您自己的TypedRPCConnectionProvider的子类
101
+ }
102
+ })
103
+ ```
104
+
105
+ ## API 文档
106
+
107
+ ### 服务器 API
108
+
109
+ - `new TypedRPCServer(config)` - 创建新的服务器实例
110
+ - `server.hook(serviceName, methodName, config)` - 挂钩单个方法
111
+ - `server.hookService(serviceName, instance)` - 挂钩整个服务
112
+ - `server.use(middleware)` - 添加中间件
113
+ - `server.listen()` - 开始监听连接
114
+ - `server.close()` - 关闭服务器
115
+
116
+ ### 客户端 API
117
+
118
+ - `new TypedRPCClient(config)` - 创建新的客户端实例
119
+ - `client.connect()` - 连接到服务器
120
+ - `client.getAPI(connection)` - 获取连接的 API 实例
121
+ - `client.use(middleware)` - 添加中间件
122
+
123
+ ## 中间件使用
124
+
125
+ TypedRPC 支持服务器和客户端的中间件:
126
+
127
+ ```typescript
128
+ class MyMiddleware extends TypedRPCHandlerMiddleware{
129
+
130
+ async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
131
+ // 处理入站数据包时执行操作
132
+ return context;
133
+ }
134
+
135
+ async outbound(context: TypedRPCContext): Promise<TypedRPCContext> {
136
+ // 处理出站数据包时执行操作
137
+ return context;
138
+ }
139
+
140
+ }
141
+ ```
142
+
143
+ ## 上下文使用
144
+
145
+ TypedRPC 提供内置的上下文系统,允许您在服务方法中访问请求上下文。这对于访问连接信息、认证数据或其他请求特定数据特别有用。
146
+
147
+ ### 示例:在服务中使用上下文
148
+
149
+ ```typescript
150
+ // 服务器端代码
151
+ import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
152
+
153
+ // 定义服务接口
154
+ interface MathServiceInterface {
155
+ add(a: number, b: number):number;
156
+ }
157
+
158
+ // 创建 API 定义
159
+ const serverAPIDefine = new TypedRPCAPIDefine<{
160
+ math: MathServiceInterface,
161
+ }>();
162
+
163
+ // 创建服务器实例
164
+ const server = new TypedRPCServer({
165
+ local: serverAPIDefine,
166
+ });
167
+
168
+ // 实现具有上下文感知的服务
169
+ class MathService implements MathServiceInterface, TypedRPCContextAware {
170
+ // 使用 TypedRPCContextSymbol 注入上下文
171
+ [TypedRPCContextSymbol]: TypedRPCContext | null = null;
172
+
173
+ // 为安全起见,必须使用 @TypedRPCAPIDefine.method() 将方法标记为服务使用中的 RPC 方法
174
+ @TypedRPCAPIDefine.method()
175
+ add(a: number, b: number): number {
176
+ // 访问上下文
177
+ const context = this[TypedRPCContextSymbol];
178
+ if (!context) {
179
+ throw new Error('上下文不可用');
180
+ }
181
+
182
+ // 使用上下文信息(例如,连接详情、认证)
183
+ console.log('请求来自:', context.connection);
184
+
185
+ return a + b;
186
+ }
187
+ }
188
+
189
+ // 将服务挂钩到服务器,只有标记有 @TypedRPCAPIDefine.method() 的方法会被挂钩
190
+ server.hookService('math', new MathService());
191
+
192
+ // 启动服务器
193
+ server.listen({
194
+ port: 3698,
195
+ });
196
+ ```
197
+
198
+ ```typescript
199
+ // 客户端代码
200
+ import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
201
+
202
+ // 重用 API 定义
203
+ const serverAPIDefine = new TypedRPCAPIDefine<{
204
+ math: MathServiceInterface,
205
+ }>();
206
+
207
+ // 创建客户端
208
+ const client = new TypedRPCClient({
209
+ remote: serverAPIDefine,
210
+ });
211
+
212
+ // 连接并进行 RPC 调用
213
+ const connection = await client.connect("localhost:3698");
214
+ const api = client.getAPI(connection);
215
+ const result = await api.math.add.call(1, 2); // 返回 3
216
+ ```
217
+
218
+ ## 更多使用
219
+
220
+ 请参阅 `./test/*.ts` 获取更多使用示例。
221
+
222
+ ## 贡献
223
+
224
+ 欢迎贡献!请随时提交 Pull Request。
225
+
226
+ ## 许可证
227
+
228
+ MIT 许可证 - 详情请参阅 [LICENSE](https://github.com/TypedRPC/TypedRPC/blob/main/LICENSE) 文件。
package/dist/api.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { TypeRPCAPIDefine } from "./define.js";
2
+ import type { TypeRPCRequestPacket, TypeRPCResponsePacket } from "./packet.js";
3
+ import type { TypeRPCDefineToTypeRPCAPI } from "./utils.js";
4
+ declare class TypeRPCAPI<T extends TypeRPCAPIDefine<any>> {
5
+ constructor();
6
+ interface(callback: (context: {
7
+ serviceName: string;
8
+ methodName: string;
9
+ args: any[];
10
+ }) => Promise<{
11
+ request: TypeRPCRequestPacket;
12
+ response: TypeRPCResponsePacket;
13
+ }>): TypeRPCDefineToTypeRPCAPI<T>;
14
+ }
15
+ export { TypeRPCAPI };
16
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAG5D,cAAM,UAAU,CAAC,CAAC,SAAS,gBAAgB,CAAC,GAAG,CAAC;;IAK5C,SAAS,CAAC,QAAQ,EAAC,CAAC,OAAO,EAAC;QACxB,WAAW,EAAC,MAAM,CAAC;QAAA,UAAU,EAAC,MAAM,CAAC;QAAA,IAAI,EAAC,GAAG,EAAE,CAAA;KAClD,KAAK,OAAO,CAAC;QACV,OAAO,EAAC,oBAAoB,CAAC;QAC7B,QAAQ,EAAC,qBAAqB,CAAC;KAClC,CAAC,GAAE,yBAAyB,CAAC,CAAC,CAAC;CAmDnC;AAGD,OAAO,EACH,UAAU,EACb,CAAA"}
package/dist/api.js ADDED
@@ -0,0 +1,52 @@
1
+ class TypeRPCAPI {
2
+ constructor() {
3
+ }
4
+ interface(callback) {
5
+ return new Proxy({}, {
6
+ get(target, serviceName, receiver) {
7
+ if (typeof serviceName !== 'string') {
8
+ return Reflect.get(target, serviceName, receiver);
9
+ }
10
+ return new Proxy({}, {
11
+ get(target, methodName, receiver) {
12
+ if (typeof methodName !== 'string') {
13
+ return Reflect.get(target, methodName, receiver);
14
+ }
15
+ const path = `${serviceName}.${methodName}`;
16
+ const id = `${serviceName}.${methodName}`;
17
+ return {
18
+ call: async (...args) => {
19
+ const result = await callback({
20
+ serviceName: serviceName,
21
+ methodName: methodName,
22
+ args: args,
23
+ });
24
+ if (result.response.error) {
25
+ throw result.response.error;
26
+ }
27
+ return result.response.result;
28
+ },
29
+ request: async (config) => {
30
+ const result = await callback({
31
+ serviceName: serviceName,
32
+ methodName: methodName,
33
+ args: config.args || [],
34
+ });
35
+ if (result.response.error) {
36
+ config.error?.(result.response.error, result.request, result.response);
37
+ }
38
+ else {
39
+ config.callback?.(result.response.result, result.request, result.response);
40
+ }
41
+ },
42
+ id,
43
+ path
44
+ };
45
+ }
46
+ });
47
+ }
48
+ });
49
+ }
50
+ }
51
+ export { TypeRPCAPI };
52
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU;IACZ;IAEA,CAAC;IAED,SAAS,CAAC,QAKR;QACE,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE;YACjB,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,QAAQ;gBAC7B,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE,CAAC;oBAClC,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAC;gBACtD,CAAC;gBACD,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE;oBACjB,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,QAAQ;wBAC5B,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;4BACjC,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;wBACrD,CAAC;wBAED,MAAM,IAAI,GAAG,GAAG,WAAW,IAAI,UAAU,EAAE,CAAC;wBAC5C,MAAM,EAAE,GAAG,GAAG,WAAW,IAAI,UAAU,EAAE,CAAC;wBAE1C,OAAO;4BACH,IAAI,EAAE,KAAK,EAAE,GAAG,IAAW,EAAE,EAAE;gCAC3B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC;oCAC1B,WAAW,EAAC,WAAW;oCACvB,UAAU,EAAC,UAAU;oCACrB,IAAI,EAAC,IAAI;iCACZ,CAAC,CAAC;gCACH,IAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAC,CAAC;oCACtB,MAAM,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;gCAChC,CAAC;gCACD,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;4BAClC,CAAC;4BACD,OAAO,EAAC,KAAK,EAAE,MAId,EAAE,EAAE;gCACD,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC;oCAC1B,WAAW,EAAC,WAAW;oCACvB,UAAU,EAAC,UAAU;oCACrB,IAAI,EAAC,MAAM,CAAC,IAAI,IAAI,EAAE;iCACzB,CAAC,CAAC;gCACH,IAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAC,CAAC;oCACtB,MAAM,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAC,MAAM,CAAC,OAAO,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;gCACzE,CAAC;qCAAI,CAAC;oCACF,MAAM,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAC,MAAM,CAAC,OAAO,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;gCAC7E,CAAC;4BACL,CAAC;4BACD,EAAE;4BACF,IAAI;yBACP,CAAC;oBACN,CAAC;iBACJ,CAAC,CAAC;YACP,CAAC;SACJ,CAAiC,CAAC;IACvC,CAAC;CACJ;AAGD,OAAO,EACH,UAAU,EACb,CAAA"}
@@ -0,0 +1,28 @@
1
+ import type { TypeRPCConnection, TypeRPCConnectionProvider } from "./connecitons/basic.js";
2
+ import { TypeRPCCore } from "./core.js";
3
+ import { TypeRPCAPIDefine } from "./define.js";
4
+ import { TypedEmitter, type TypeRPCDefineMethodBody, type TypeRPCDefineMethodName, type TypeRPCDefineServiceInstance, type TypeRPCDefineServiceName, type TypeRPCDefineToTypeRPCAPI } from "./utils.js";
5
+ type TypeRPCClientConfig<T extends TypeRPCAPIDefine<any>, R extends TypeRPCAPIDefine<any>> = {
6
+ local?: T;
7
+ remote?: R;
8
+ connection?: {
9
+ provider: TypeRPCConnectionProvider;
10
+ };
11
+ };
12
+ type TypeRPCClientEvents = {};
13
+ declare class TypeRPCClient<T extends TypeRPCAPIDefine<any>, R extends TypeRPCAPIDefine<any>> {
14
+ emitter: TypedEmitter<TypeRPCClientEvents>;
15
+ private config;
16
+ core: TypeRPCCore;
17
+ constructor(config?: TypeRPCClientConfig<T, R>);
18
+ hook<S extends TypeRPCDefineServiceName<T>, M extends TypeRPCDefineMethodName<T, S>>(serviceName: S, methodName: M, config: {
19
+ handler: TypeRPCDefineMethodBody<T, S, M>;
20
+ bind?: any;
21
+ }): void;
22
+ hookService<S extends TypeRPCDefineServiceName<T>>(serviceName: S, instance: TypeRPCDefineServiceInstance<T, S>): void;
23
+ get use(): (middleware: import("./handler.js").TypeRPCHandlerMiddleware) => import("./handler.js").TypeRPCHandler;
24
+ get connect(): (target: string) => Promise<TypeRPCConnection>;
25
+ getAPI(connection: TypeRPCConnection): TypeRPCDefineToTypeRPCAPI<R>;
26
+ }
27
+ export { TypeRPCClient, };
28
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AAE3F,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,KAAK,uBAAuB,EAAE,KAAK,uBAAuB,EAAE,KAAK,4BAA4B,EAAE,KAAK,wBAAwB,EAAE,KAAK,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAExM,KAAK,mBAAmB,CAAC,CAAC,SAAS,gBAAgB,CAAC,GAAG,CAAC,EAAC,CAAC,SAAS,gBAAgB,CAAC,GAAG,CAAC,IAAI;IACxF,KAAK,CAAC,EAAC,CAAC,CAAC;IACT,MAAM,CAAC,EAAC,CAAC,CAAC;IACV,UAAU,CAAC,EAAC;QACR,QAAQ,EAAC,yBAAyB,CAAC;KACtC,CAAA;CACJ,CAAA;AAED,KAAK,mBAAmB,GAAG,EAE1B,CAAA;AAED,cAAM,aAAa,CAAC,CAAC,SAAS,gBAAgB,CAAC,GAAG,CAAC,EAAC,CAAC,SAAS,gBAAgB,CAAC,GAAG,CAAC;IAExE,OAAO,oCAA2C;IACzD,OAAO,CAAC,MAAM,CAA0B;IACjC,IAAI,EAAC,WAAW,CAAC;gBAEZ,MAAM,CAAC,EAAC,mBAAmB,CAAC,CAAC,EAAC,CAAC,CAAC;IAU5C,IAAI,CAAC,CAAC,SAAS,wBAAwB,CAAC,CAAC,CAAC,EAAC,CAAC,SAAS,uBAAuB,CAAC,CAAC,EAAC,CAAC,CAAC,EAAE,WAAW,EAAC,CAAC,EAAC,UAAU,EAAC,CAAC,EAAC,MAAM,EAAC;QACjH,OAAO,EAAC,uBAAuB,CAAC,CAAC,EAAC,CAAC,EAAC,CAAC,CAAC,CAAC;QACvC,IAAI,CAAC,EAAC,GAAG,CAAC;KACb;IAID,WAAW,CAAC,CAAC,SAAS,wBAAwB,CAAC,CAAC,CAAC,EAAE,WAAW,EAAC,CAAC,EAAC,QAAQ,EAAC,4BAA4B,CAAC,CAAC,EAAC,CAAC,CAAC;IAU3G,IAAI,GAAG,2GAEN;IAED,IAAI,OAAO,mDAEV;IAED,MAAM,CAAC,UAAU,EAAC,iBAAiB,GAAE,yBAAyB,CAAC,CAAC,CAAC;CAWpE;AAED,OAAO,EACH,aAAa,GAChB,CAAA"}
package/dist/client.js ADDED
@@ -0,0 +1,50 @@
1
+ import { TypeRPCAPI } from "./api.js";
2
+ import { TypeRPCConnectionProviderDefault } from "./connection.js";
3
+ import { TypeRPCCore } from "./core.js";
4
+ import { TypeRPCAPIDefine } from "./define.js";
5
+ import { TypedEmitter } from "./utils.js";
6
+ class TypeRPCClient {
7
+ emitter = new TypedEmitter();
8
+ config;
9
+ core;
10
+ constructor(config) {
11
+ const defaultConfig = {
12
+ connection: {
13
+ provider: new TypeRPCConnectionProviderDefault(),
14
+ }
15
+ };
16
+ this.config = { ...defaultConfig, ...config };
17
+ this.core = new TypeRPCCore(this.config);
18
+ }
19
+ hook(serviceName, methodName, config) {
20
+ return this.core.hook(serviceName, methodName, config);
21
+ }
22
+ hookService(serviceName, instance) {
23
+ const methodList = TypeRPCAPIDefine.getMethodList(instance);
24
+ for (let methodName of methodList) {
25
+ this.hook(serviceName, methodName, {
26
+ handler: instance[methodName],
27
+ bind: instance,
28
+ });
29
+ }
30
+ }
31
+ get use() {
32
+ return this.core.handler.use.bind(this.core.handler);
33
+ }
34
+ get connect() {
35
+ return this.core.connect.bind(this.core);
36
+ }
37
+ getAPI(connection) {
38
+ const api = new TypeRPCAPI();
39
+ return api.interface(async (context) => {
40
+ return await this.core.request({
41
+ connection,
42
+ serviceName: context.serviceName,
43
+ methodName: context.methodName,
44
+ args: context.args,
45
+ });
46
+ });
47
+ }
48
+ }
49
+ export { TypeRPCClient, };
50
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAE,gCAAgC,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAgK,MAAM,YAAY,CAAC;AAcxM,MAAM,aAAa;IAER,OAAO,GAAG,IAAI,YAAY,EAAuB,CAAC;IACjD,MAAM,CAA0B;IACjC,IAAI,CAAa;IAExB,YAAY,MAAgC;QACxC,MAAM,aAAa,GAA4B;YAC3C,UAAU,EAAC;gBACP,QAAQ,EAAC,IAAI,gCAAgC,EAAE;aAClD;SACJ,CAAA;QACD,IAAI,CAAC,MAAM,GAAG,EAAC,GAAG,aAAa,EAAC,GAAG,MAAM,EAAC,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED,IAAI,CAA+E,WAAa,EAAC,UAAY,EAAC,MAG7G;QACG,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAC,UAAU,EAAC,MAAM,CAAC,CAAC;IACzD,CAAC;IAED,WAAW,CAAwC,WAAa,EAAC,QAA0C;QACvG,MAAM,UAAU,GAAG,gBAAgB,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAC5D,KAAI,IAAI,UAAU,IAAI,UAAU,EAAC,CAAC;YAC9B,IAAI,CAAC,IAAI,CAAC,WAAW,EAAC,UAA0C,EAAC;gBAC7D,OAAO,EAAC,QAAQ,CAAC,UAAU,CAAC;gBAC5B,IAAI,EAAC,QAAQ;aAChB,CAAC,CAAA;QACN,CAAC;IACL,CAAC;IAED,IAAI,GAAG;QACH,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzD,CAAC;IAED,IAAI,OAAO;QACP,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,UAA4B;QAC/B,MAAM,GAAG,GAAG,IAAI,UAAU,EAAK,CAAC;QAChC,OAAO,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YACnC,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;gBAC3B,UAAU;gBACV,WAAW,EAAC,OAAO,CAAC,WAAW;gBAC/B,UAAU,EAAC,OAAO,CAAC,UAAU;gBAC7B,IAAI,EAAC,OAAO,CAAC,IAAI;aACpB,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAED,OAAO,EACH,aAAa,GAChB,CAAA"}
@@ -0,0 +1,42 @@
1
+ import { TypedEmitter } from "../utils.js";
2
+ type TypeRPCConnectionEvents = {
3
+ /** 有新的请求进入时,应该由Provider调起 */
4
+ request: (context: {
5
+ data: string;
6
+ response: (data: string) => void;
7
+ }) => void;
8
+ };
9
+ declare abstract class TypeRPCConnection {
10
+ emitter: TypedEmitter<TypeRPCConnectionEvents>;
11
+ abstract request(data: string): Promise<string>;
12
+ /** 获取连接的id */
13
+ abstract getId(): string;
14
+ /** 关闭连接 */
15
+ abstract close(): boolean;
16
+ /** 是否关闭 */
17
+ abstract isClosed(): boolean;
18
+ }
19
+ type TypeRPCConnectionProviderEvents = {
20
+ /** 新的连接建立时 */
21
+ connection: (connection: TypeRPCConnection) => void;
22
+ };
23
+ declare abstract class TypeRPCConnectionProvider {
24
+ emitter: TypedEmitter<TypeRPCConnectionProviderEvents>;
25
+ /**
26
+ * Server用,用于监听一个端口
27
+ */
28
+ abstract listen(config: {
29
+ port: number;
30
+ hostname?: string;
31
+ }): Promise<boolean>;
32
+ /**
33
+ * Server用,用于关闭监听的端口
34
+ */
35
+ abstract close(): Promise<boolean>;
36
+ /**
37
+ * Client用,用于建立一个连接
38
+ */
39
+ abstract connect(target: string): Promise<TypeRPCConnection>;
40
+ }
41
+ export { TypeRPCConnection, TypeRPCConnectionProvider, };
42
+ //# sourceMappingURL=basic.d.ts.map