@esengine/server 4.3.0 → 4.5.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.
- package/dist/{Room-BnKpl5Sj.d.ts → Room-5owFVIFR.d.ts} +18 -0
- package/dist/auth/index.d.ts +4 -4
- package/dist/auth/testing/index.d.ts +2 -2
- package/dist/chunk-NWZLKNGV.js +2045 -0
- package/dist/chunk-NWZLKNGV.js.map +1 -0
- package/dist/{chunk-FACTBKJ3.js → chunk-T3QJOPNG.js} +37 -2
- package/dist/chunk-T3QJOPNG.js.map +1 -0
- package/dist/chunk-ZUTL4RI7.js +285 -0
- package/dist/chunk-ZUTL4RI7.js.map +1 -0
- package/dist/ecs/index.d.ts +31 -4
- package/dist/ecs/index.js +3 -280
- package/dist/ecs/index.js.map +1 -1
- package/dist/index-B1sr5YAl.d.ts +1076 -0
- package/dist/index.d.ts +1453 -7
- package/dist/index.js +1502 -4
- package/dist/index.js.map +1 -1
- package/dist/ratelimit/index.d.ts +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/{types-C7sS8Sfi.d.ts → types-BCTRacMF.d.ts} +2 -2
- package/package.json +1 -1
- package/dist/chunk-FACTBKJ3.js.map +0 -1
- package/dist/chunk-M7VONMZJ.js +0 -938
- package/dist/chunk-M7VONMZJ.js.map +0 -1
- package/dist/decorators-DY8nZ8Nh.d.ts +0 -26
- package/dist/index-lcuKuQsL.d.ts +0 -470
package/dist/index-lcuKuQsL.d.ts
DELETED
|
@@ -1,470 +0,0 @@
|
|
|
1
|
-
import { Connection } from '@esengine/rpc';
|
|
2
|
-
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @zh HTTP 路由类型定义
|
|
6
|
-
* @en HTTP router type definitions
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @zh HTTP 请求上下文
|
|
11
|
-
* @en HTTP request context
|
|
12
|
-
*/
|
|
13
|
-
interface HttpRequest {
|
|
14
|
-
/**
|
|
15
|
-
* @zh 原始请求对象
|
|
16
|
-
* @en Raw request object
|
|
17
|
-
*/
|
|
18
|
-
raw: IncomingMessage;
|
|
19
|
-
/**
|
|
20
|
-
* @zh 请求方法
|
|
21
|
-
* @en Request method
|
|
22
|
-
*/
|
|
23
|
-
method: string;
|
|
24
|
-
/**
|
|
25
|
-
* @zh 请求路径
|
|
26
|
-
* @en Request path
|
|
27
|
-
*/
|
|
28
|
-
path: string;
|
|
29
|
-
/**
|
|
30
|
-
* @zh 路由参数(从 URL 路径提取,如 /users/:id)
|
|
31
|
-
* @en Route parameters (extracted from URL path, e.g., /users/:id)
|
|
32
|
-
*/
|
|
33
|
-
params: Record<string, string>;
|
|
34
|
-
/**
|
|
35
|
-
* @zh 查询参数
|
|
36
|
-
* @en Query parameters
|
|
37
|
-
*/
|
|
38
|
-
query: Record<string, string>;
|
|
39
|
-
/**
|
|
40
|
-
* @zh 请求头
|
|
41
|
-
* @en Request headers
|
|
42
|
-
*/
|
|
43
|
-
headers: Record<string, string | string[] | undefined>;
|
|
44
|
-
/**
|
|
45
|
-
* @zh 解析后的 JSON 请求体
|
|
46
|
-
* @en Parsed JSON body
|
|
47
|
-
*/
|
|
48
|
-
body: unknown;
|
|
49
|
-
/**
|
|
50
|
-
* @zh 客户端 IP
|
|
51
|
-
* @en Client IP
|
|
52
|
-
*/
|
|
53
|
-
ip: string;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @zh HTTP 响应工具
|
|
57
|
-
* @en HTTP response utilities
|
|
58
|
-
*/
|
|
59
|
-
interface HttpResponse {
|
|
60
|
-
/**
|
|
61
|
-
* @zh 原始响应对象
|
|
62
|
-
* @en Raw response object
|
|
63
|
-
*/
|
|
64
|
-
raw: ServerResponse;
|
|
65
|
-
/**
|
|
66
|
-
* @zh 设置状态码
|
|
67
|
-
* @en Set status code
|
|
68
|
-
*/
|
|
69
|
-
status(code: number): HttpResponse;
|
|
70
|
-
/**
|
|
71
|
-
* @zh 设置响应头
|
|
72
|
-
* @en Set response header
|
|
73
|
-
*/
|
|
74
|
-
header(name: string, value: string): HttpResponse;
|
|
75
|
-
/**
|
|
76
|
-
* @zh 发送 JSON 响应
|
|
77
|
-
* @en Send JSON response
|
|
78
|
-
*/
|
|
79
|
-
json(data: unknown): void;
|
|
80
|
-
/**
|
|
81
|
-
* @zh 发送文本响应
|
|
82
|
-
* @en Send text response
|
|
83
|
-
*/
|
|
84
|
-
text(data: string): void;
|
|
85
|
-
/**
|
|
86
|
-
* @zh 发送错误响应
|
|
87
|
-
* @en Send error response
|
|
88
|
-
*/
|
|
89
|
-
error(code: number, message: string): void;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* @zh HTTP 路由处理器
|
|
93
|
-
* @en HTTP route handler
|
|
94
|
-
*/
|
|
95
|
-
type HttpHandler = (req: HttpRequest, res: HttpResponse) => void | Promise<void>;
|
|
96
|
-
/**
|
|
97
|
-
* @zh HTTP 中间件函数
|
|
98
|
-
* @en HTTP middleware function
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* const authMiddleware: HttpMiddleware = async (req, res, next) => {
|
|
103
|
-
* if (!req.headers.authorization) {
|
|
104
|
-
* res.error(401, 'Unauthorized');
|
|
105
|
-
* return;
|
|
106
|
-
* }
|
|
107
|
-
* await next();
|
|
108
|
-
* };
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
type HttpMiddleware = (req: HttpRequest, res: HttpResponse, next: () => Promise<void>) => void | Promise<void>;
|
|
112
|
-
/**
|
|
113
|
-
* @zh 带中间件和超时的路由处理器定义
|
|
114
|
-
* @en Route handler definition with middleware and timeout support
|
|
115
|
-
*/
|
|
116
|
-
interface HttpHandlerDefinition {
|
|
117
|
-
/**
|
|
118
|
-
* @zh 处理函数
|
|
119
|
-
* @en Handler function
|
|
120
|
-
*/
|
|
121
|
-
handler: HttpHandler;
|
|
122
|
-
/**
|
|
123
|
-
* @zh 路由级中间件
|
|
124
|
-
* @en Route-level middlewares
|
|
125
|
-
*/
|
|
126
|
-
middlewares?: HttpMiddleware[];
|
|
127
|
-
/**
|
|
128
|
-
* @zh 路由级超时时间(毫秒),覆盖全局设置
|
|
129
|
-
* @en Route-level timeout in milliseconds, overrides global setting
|
|
130
|
-
*/
|
|
131
|
-
timeout?: number;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* @zh HTTP 路由方法配置(支持简单处理器或完整定义)
|
|
135
|
-
* @en HTTP route method configuration (supports simple handler or full definition)
|
|
136
|
-
*/
|
|
137
|
-
type HttpMethodHandler = HttpHandler | HttpHandlerDefinition;
|
|
138
|
-
/**
|
|
139
|
-
* @zh HTTP 路由方法映射
|
|
140
|
-
* @en HTTP route methods mapping
|
|
141
|
-
*/
|
|
142
|
-
interface HttpRouteMethods {
|
|
143
|
-
GET?: HttpMethodHandler;
|
|
144
|
-
POST?: HttpMethodHandler;
|
|
145
|
-
PUT?: HttpMethodHandler;
|
|
146
|
-
DELETE?: HttpMethodHandler;
|
|
147
|
-
PATCH?: HttpMethodHandler;
|
|
148
|
-
OPTIONS?: HttpMethodHandler;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* @zh HTTP 路由配置
|
|
152
|
-
* @en HTTP routes configuration
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* const routes: HttpRoutes = {
|
|
157
|
-
* // 简单处理器
|
|
158
|
-
* '/health': (req, res) => res.json({ ok: true }),
|
|
159
|
-
*
|
|
160
|
-
* // 按方法分开
|
|
161
|
-
* '/users': {
|
|
162
|
-
* GET: (req, res) => res.json([]),
|
|
163
|
-
* POST: (req, res) => res.json({ created: true })
|
|
164
|
-
* },
|
|
165
|
-
*
|
|
166
|
-
* // 路由参数
|
|
167
|
-
* '/users/:id': {
|
|
168
|
-
* GET: (req, res) => res.json({ id: req.params.id }),
|
|
169
|
-
* DELETE: {
|
|
170
|
-
* handler: (req, res) => res.json({ deleted: true }),
|
|
171
|
-
* middlewares: [authMiddleware],
|
|
172
|
-
* timeout: 5000
|
|
173
|
-
* }
|
|
174
|
-
* }
|
|
175
|
-
* };
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
type HttpRoutes = Record<string, HttpMethodHandler | HttpRouteMethods>;
|
|
179
|
-
/**
|
|
180
|
-
* @zh CORS 配置
|
|
181
|
-
* @en CORS configuration
|
|
182
|
-
*/
|
|
183
|
-
interface CorsOptions {
|
|
184
|
-
/**
|
|
185
|
-
* @zh 允许的来源
|
|
186
|
-
* @en Allowed origins
|
|
187
|
-
*/
|
|
188
|
-
origin?: string | string[] | boolean;
|
|
189
|
-
/**
|
|
190
|
-
* @zh 允许的方法
|
|
191
|
-
* @en Allowed methods
|
|
192
|
-
*/
|
|
193
|
-
methods?: string[];
|
|
194
|
-
/**
|
|
195
|
-
* @zh 允许的请求头
|
|
196
|
-
* @en Allowed headers
|
|
197
|
-
*/
|
|
198
|
-
allowedHeaders?: string[];
|
|
199
|
-
/**
|
|
200
|
-
* @zh 是否允许携带凭证
|
|
201
|
-
* @en Allow credentials
|
|
202
|
-
*/
|
|
203
|
-
credentials?: boolean;
|
|
204
|
-
/**
|
|
205
|
-
* @zh 预检请求缓存时间(秒)
|
|
206
|
-
* @en Preflight cache max age
|
|
207
|
-
*/
|
|
208
|
-
maxAge?: number;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* @zh HTTP 路由器选项
|
|
212
|
-
* @en HTTP router options
|
|
213
|
-
*/
|
|
214
|
-
interface HttpRouterOptions {
|
|
215
|
-
/**
|
|
216
|
-
* @zh CORS 配置
|
|
217
|
-
* @en CORS configuration
|
|
218
|
-
*/
|
|
219
|
-
cors?: CorsOptions | boolean;
|
|
220
|
-
/**
|
|
221
|
-
* @zh 全局请求超时时间(毫秒)
|
|
222
|
-
* @en Global request timeout in milliseconds
|
|
223
|
-
*/
|
|
224
|
-
timeout?: number;
|
|
225
|
-
/**
|
|
226
|
-
* @zh 全局中间件
|
|
227
|
-
* @en Global middlewares
|
|
228
|
-
*/
|
|
229
|
-
middlewares?: HttpMiddleware[];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* @zh ESEngine Server 类型定义
|
|
234
|
-
* @en ESEngine Server type definitions
|
|
235
|
-
*/
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* @zh 服务器配置
|
|
239
|
-
* @en Server configuration
|
|
240
|
-
*/
|
|
241
|
-
interface ServerConfig {
|
|
242
|
-
/**
|
|
243
|
-
* @zh 监听端口
|
|
244
|
-
* @en Listen port
|
|
245
|
-
* @default 3000
|
|
246
|
-
*/
|
|
247
|
-
port?: number;
|
|
248
|
-
/**
|
|
249
|
-
* @zh API 目录路径
|
|
250
|
-
* @en API directory path
|
|
251
|
-
* @default 'src/api'
|
|
252
|
-
*/
|
|
253
|
-
apiDir?: string;
|
|
254
|
-
/**
|
|
255
|
-
* @zh 消息处理器目录路径
|
|
256
|
-
* @en Message handlers directory path
|
|
257
|
-
* @default 'src/msg'
|
|
258
|
-
*/
|
|
259
|
-
msgDir?: string;
|
|
260
|
-
/**
|
|
261
|
-
* @zh HTTP 路由目录路径
|
|
262
|
-
* @en HTTP routes directory path
|
|
263
|
-
* @default 'src/http'
|
|
264
|
-
*
|
|
265
|
-
* @zh 文件命名规则:
|
|
266
|
-
* - `login.ts` → POST /api/login
|
|
267
|
-
* - `users/[id].ts` → /api/users/:id
|
|
268
|
-
* - `health.ts` (method: 'GET') → GET /api/health
|
|
269
|
-
* @en File naming convention:
|
|
270
|
-
* - `login.ts` → POST /api/login
|
|
271
|
-
* - `users/[id].ts` → /api/users/:id
|
|
272
|
-
* - `health.ts` (method: 'GET') → GET /api/health
|
|
273
|
-
*/
|
|
274
|
-
httpDir?: string;
|
|
275
|
-
/**
|
|
276
|
-
* @zh HTTP 路由前缀
|
|
277
|
-
* @en HTTP routes prefix
|
|
278
|
-
* @default '/api'
|
|
279
|
-
*/
|
|
280
|
-
httpPrefix?: string;
|
|
281
|
-
/**
|
|
282
|
-
* @zh 游戏 Tick 速率 (每秒)
|
|
283
|
-
* @en Game tick rate (per second)
|
|
284
|
-
* @default 20
|
|
285
|
-
*/
|
|
286
|
-
tickRate?: number;
|
|
287
|
-
/**
|
|
288
|
-
* @zh HTTP 路由配置(内联定义,与 httpDir 文件路由合并)
|
|
289
|
-
* @en HTTP routes configuration (inline definition, merged with httpDir file routes)
|
|
290
|
-
*/
|
|
291
|
-
http?: HttpRoutes;
|
|
292
|
-
/**
|
|
293
|
-
* @zh CORS 配置
|
|
294
|
-
* @en CORS configuration
|
|
295
|
-
* @default true
|
|
296
|
-
*/
|
|
297
|
-
cors?: CorsOptions | boolean;
|
|
298
|
-
/**
|
|
299
|
-
* @zh 服务器启动回调
|
|
300
|
-
* @en Server start callback
|
|
301
|
-
*/
|
|
302
|
-
onStart?: (port: number) => void;
|
|
303
|
-
/**
|
|
304
|
-
* @zh 连接建立回调
|
|
305
|
-
* @en Connection established callback
|
|
306
|
-
*/
|
|
307
|
-
onConnect?: (conn: ServerConnection) => void | Promise<void>;
|
|
308
|
-
/**
|
|
309
|
-
* @zh 连接断开回调
|
|
310
|
-
* @en Connection closed callback
|
|
311
|
-
*/
|
|
312
|
-
onDisconnect?: (conn: ServerConnection) => void | Promise<void>;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* @zh 服务器连接(扩展 RPC Connection)
|
|
316
|
-
* @en Server connection (extends RPC Connection)
|
|
317
|
-
*/
|
|
318
|
-
interface ServerConnection<TData = Record<string, unknown>> extends Connection<TData> {
|
|
319
|
-
/**
|
|
320
|
-
* @zh 连接唯一标识(继承自 Connection)
|
|
321
|
-
* @en Connection unique identifier (inherited from Connection)
|
|
322
|
-
*/
|
|
323
|
-
readonly id: string;
|
|
324
|
-
/**
|
|
325
|
-
* @zh 用户自定义数据
|
|
326
|
-
* @en User-defined data
|
|
327
|
-
*/
|
|
328
|
-
data: TData;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* @zh API 处理器上下文
|
|
332
|
-
* @en API handler context
|
|
333
|
-
*/
|
|
334
|
-
interface ApiContext<TData = Record<string, unknown>> {
|
|
335
|
-
/**
|
|
336
|
-
* @zh 当前连接
|
|
337
|
-
* @en Current connection
|
|
338
|
-
*/
|
|
339
|
-
conn: ServerConnection<TData>;
|
|
340
|
-
/**
|
|
341
|
-
* @zh 服务器实例
|
|
342
|
-
* @en Server instance
|
|
343
|
-
*/
|
|
344
|
-
server: GameServer;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* @zh API 定义选项
|
|
348
|
-
* @en API definition options
|
|
349
|
-
*/
|
|
350
|
-
interface ApiDefinition<TReq = unknown, TRes = unknown, TData = Record<string, unknown>> {
|
|
351
|
-
/**
|
|
352
|
-
* @zh API 处理函数
|
|
353
|
-
* @en API handler function
|
|
354
|
-
*/
|
|
355
|
-
handler: (req: TReq, ctx: ApiContext<TData>) => TRes | Promise<TRes>;
|
|
356
|
-
/**
|
|
357
|
-
* @zh 请求验证函数(可选)
|
|
358
|
-
* @en Request validation function (optional)
|
|
359
|
-
*/
|
|
360
|
-
validate?: (req: unknown) => req is TReq;
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* @zh 消息处理器上下文
|
|
364
|
-
* @en Message handler context
|
|
365
|
-
*/
|
|
366
|
-
interface MsgContext<TData = Record<string, unknown>> {
|
|
367
|
-
/**
|
|
368
|
-
* @zh 当前连接
|
|
369
|
-
* @en Current connection
|
|
370
|
-
*/
|
|
371
|
-
conn: ServerConnection<TData>;
|
|
372
|
-
/**
|
|
373
|
-
* @zh 服务器实例
|
|
374
|
-
* @en Server instance
|
|
375
|
-
*/
|
|
376
|
-
server: GameServer;
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* @zh 消息定义选项
|
|
380
|
-
* @en Message definition options
|
|
381
|
-
*/
|
|
382
|
-
interface MsgDefinition<TMsg = unknown, TData = Record<string, unknown>> {
|
|
383
|
-
/**
|
|
384
|
-
* @zh 消息处理函数
|
|
385
|
-
* @en Message handler function
|
|
386
|
-
*/
|
|
387
|
-
handler: (msg: TMsg, ctx: MsgContext<TData>) => void | Promise<void>;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* @zh 游戏服务器接口
|
|
391
|
-
* @en Game server interface
|
|
392
|
-
*/
|
|
393
|
-
interface GameServer {
|
|
394
|
-
/**
|
|
395
|
-
* @zh 启动服务器
|
|
396
|
-
* @en Start server
|
|
397
|
-
*/
|
|
398
|
-
start(): Promise<void>;
|
|
399
|
-
/**
|
|
400
|
-
* @zh 停止服务器
|
|
401
|
-
* @en Stop server
|
|
402
|
-
*/
|
|
403
|
-
stop(): Promise<void>;
|
|
404
|
-
/**
|
|
405
|
-
* @zh 广播消息
|
|
406
|
-
* @en Broadcast message
|
|
407
|
-
*/
|
|
408
|
-
broadcast<T>(name: string, data: T): void;
|
|
409
|
-
/**
|
|
410
|
-
* @zh 发送消息给指定连接
|
|
411
|
-
* @en Send message to specific connection
|
|
412
|
-
*/
|
|
413
|
-
send<T>(conn: ServerConnection, name: string, data: T): void;
|
|
414
|
-
/**
|
|
415
|
-
* @zh 获取所有连接
|
|
416
|
-
* @en Get all connections
|
|
417
|
-
*/
|
|
418
|
-
readonly connections: ReadonlyArray<ServerConnection>;
|
|
419
|
-
/**
|
|
420
|
-
* @zh 当前 Tick
|
|
421
|
-
* @en Current tick
|
|
422
|
-
*/
|
|
423
|
-
readonly tick: number;
|
|
424
|
-
/**
|
|
425
|
-
* @zh 注册房间类型
|
|
426
|
-
* @en Define room type
|
|
427
|
-
*/
|
|
428
|
-
define(name: string, roomClass: new () => unknown): void;
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* @zh HTTP 请求方法
|
|
432
|
-
* @en HTTP request method
|
|
433
|
-
*/
|
|
434
|
-
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
435
|
-
/**
|
|
436
|
-
* @zh HTTP 定义选项
|
|
437
|
-
* @en HTTP definition options
|
|
438
|
-
*
|
|
439
|
-
* @example
|
|
440
|
-
* ```typescript
|
|
441
|
-
* // src/http/login.ts
|
|
442
|
-
* import { defineHttp } from '@esengine/server'
|
|
443
|
-
*
|
|
444
|
-
* export default defineHttp({
|
|
445
|
-
* method: 'POST',
|
|
446
|
-
* handler: async (req, res) => {
|
|
447
|
-
* const { username, password } = req.body
|
|
448
|
-
* // ... authentication logic
|
|
449
|
-
* res.json({ token: '...', userId: '...' })
|
|
450
|
-
* }
|
|
451
|
-
* })
|
|
452
|
-
* ```
|
|
453
|
-
*/
|
|
454
|
-
interface HttpDefinition<TBody = unknown> {
|
|
455
|
-
/**
|
|
456
|
-
* @zh 请求方法
|
|
457
|
-
* @en Request method
|
|
458
|
-
* @default 'POST'
|
|
459
|
-
*/
|
|
460
|
-
method?: HttpMethod;
|
|
461
|
-
/**
|
|
462
|
-
* @zh 处理函数
|
|
463
|
-
* @en Handler function
|
|
464
|
-
*/
|
|
465
|
-
handler: (req: HttpRequest & {
|
|
466
|
-
body: TBody;
|
|
467
|
-
}, res: HttpResponse) => void | Promise<void>;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
export type { ApiDefinition as A, CorsOptions as C, GameServer as G, HttpDefinition as H, MsgDefinition as M, ServerConfig as S, HttpRoutes as a, HttpRouterOptions as b, ServerConnection as c, ApiContext as d, MsgContext as e, HttpMethod as f, HttpRequest as g, HttpResponse as h, HttpHandler as i };
|