@kevisual/router 0.1.2 → 0.1.4

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/ws.d.ts DELETED
@@ -1,860 +0,0 @@
1
- import { EventEmitter } from 'eventemitter3';
2
- import { z } from 'zod';
3
- import * as http from 'node:http';
4
- import { IncomingMessage, ServerResponse } from 'node:http';
5
- import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
6
-
7
- type ReconnectConfig = {
8
- /**
9
- * 重连配置选项, 最大重试次数,默认无限
10
- */
11
- maxRetries?: number;
12
- /**
13
- * 重连配置选项, 重试延迟(ms),默认1000
14
- */
15
- retryDelay?: number;
16
- /**
17
- * 重连配置选项, 最大延迟(ms),默认30000
18
- */
19
- maxDelay?: number;
20
- /**
21
- * 重连配置选项, 退避倍数,默认2
22
- */
23
- backoffMultiplier?: number;
24
- };
25
- /**
26
- * 一个支持自动重连的 WebSocket 客户端。
27
- * 在连接断开时会根据配置进行重连尝试,支持指数退避。
28
- */
29
- declare class ReconnectingWebSocket {
30
- private ws;
31
- private url;
32
- private config;
33
- private retryCount;
34
- private reconnectTimer;
35
- private isManualClose;
36
- private messageHandlers;
37
- private openHandlers;
38
- private closeHandlers;
39
- private errorHandlers;
40
- constructor(url: string, config?: ReconnectConfig);
41
- log(...args: any[]): void;
42
- error(...args: any[]): void;
43
- connect(): void;
44
- private scheduleReconnect;
45
- send(data: any): boolean;
46
- onMessage(handler: (data: any) => void): void;
47
- onOpen(handler: () => void): void;
48
- onClose(handler: (code: number, reason: Buffer) => void): void;
49
- onError(handler: (error: Error) => void): void;
50
- close(): void;
51
- getReadyState(): number;
52
- getRetryCount(): number;
53
- }
54
-
55
- type CustomErrorOptions = {
56
- cause?: Error | string;
57
- code?: number;
58
- message?: string;
59
- };
60
- interface throwError {
61
- throw(code?: number | string, message?: string): void;
62
- throw(code?: number | string, opts?: CustomErrorOptions): void;
63
- throw(opts?: CustomErrorOptions): void;
64
- }
65
-
66
- declare class MockProcess {
67
- emitter?: EventEmitter;
68
- process?: NodeJS.Process;
69
- constructor(opts?: {
70
- emitter?: EventEmitter;
71
- isNode?: boolean;
72
- });
73
- send(data?: any, callback?: (err?: Error) => void): void;
74
- exit(flag?: number): void;
75
- on(fn: (msg?: any) => any): void;
76
- desctroy(): void;
77
- }
78
-
79
- type RouterContextT = {
80
- code?: number;
81
- [key: string]: any;
82
- };
83
- type BuildRouteContext<M, U> = M extends {
84
- args?: infer A;
85
- } ? A extends z.ZodObject<any> ? RouteContext<{
86
- args?: z.infer<A>;
87
- }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
88
- args?: {
89
- [K in keyof A]: z.infer<A[K]>;
90
- };
91
- }, U> : RouteContext<U> : RouteContext<U>;
92
- type RouteContext<T = {
93
- code?: number;
94
- }, U extends SimpleObject = {}, S = {
95
- [key: string]: any;
96
- }> = {
97
- /**
98
- * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
99
- * 或者不需要登录的,直接调用
100
- */
101
- appId?: string;
102
- query?: {
103
- [key: string]: any;
104
- };
105
- args?: {
106
- [key: string]: any;
107
- };
108
- /** return body */
109
- body?: number | string | Object;
110
- forward?: (response: {
111
- code: number;
112
- data?: any;
113
- message?: any;
114
- }) => void;
115
- /** return code */
116
- code?: number;
117
- /** return msg */
118
- message?: string;
119
- /**
120
- * 传递状态
121
- */
122
- state?: S;
123
- /**
124
- * 当前routerId
125
- */
126
- currentId?: string;
127
- /**
128
- * 当前路径
129
- */
130
- currentPath?: string;
131
- /**
132
- * 当前key
133
- */
134
- currentKey?: string;
135
- /**
136
- * 当前route
137
- */
138
- currentRoute?: Route;
139
- /**
140
- * 进度
141
- */
142
- progress?: [string, string][];
143
- nextQuery?: {
144
- [key: string]: any;
145
- };
146
- end?: boolean;
147
- app?: QueryRouter;
148
- error?: any;
149
- /** 请求 route的返回结果,不解析body为data */
150
- call?: (message: {
151
- path: string;
152
- key?: string;
153
- payload?: any;
154
- [key: string]: any;
155
- } | {
156
- id: string;
157
- apyload?: any;
158
- [key: string]: any;
159
- }, ctx?: RouteContext & {
160
- [key: string]: any;
161
- }) => Promise<any>;
162
- /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
163
- run?: (message: {
164
- path: string;
165
- key?: string;
166
- payload?: any;
167
- }, ctx?: RouteContext) => Promise<any>;
168
- index?: number;
169
- throw?: throwError['throw'];
170
- /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
171
- needSerialize?: boolean;
172
- } & T & U;
173
- type SimpleObject = Record<string, any>;
174
- type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
175
- type RunMessage = {
176
- path?: string;
177
- key?: string;
178
- id?: string;
179
- payload?: any;
180
- };
181
- type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
182
- type RouteMiddleware = {
183
- path?: string;
184
- key?: string;
185
- id?: string;
186
- } | string;
187
- type RouteOpts<U = {}, T = SimpleObject> = {
188
- path?: string;
189
- key?: string;
190
- id?: string;
191
- run?: Run<U>;
192
- nextRoute?: NextRoute;
193
- description?: string;
194
- metadata?: T;
195
- middleware?: RouteMiddleware[];
196
- type?: 'route' | 'middleware' | 'compound';
197
- isDebug?: boolean;
198
- };
199
- type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
200
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
201
- type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
202
- /**
203
- * @M 是 route的 metadate的类型,默认是 SimpleObject
204
- * @U 是 RouteContext 里 state的类型
205
- */
206
- declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject> implements throwError {
207
- /**
208
- * 一级路径
209
- */
210
- path?: string;
211
- /**
212
- * 二级路径
213
- */
214
- key?: string;
215
- id?: string;
216
- run?: Run<BuildRouteContext<M, U>>;
217
- nextRoute?: NextRoute;
218
- description?: string;
219
- metadata?: M;
220
- middleware?: RouteMiddleware[];
221
- type?: string;
222
- /**
223
- * 是否开启debug,开启后会打印错误信息
224
- */
225
- isDebug?: boolean;
226
- constructor(path?: string, key?: string, opts?: RouteOpts);
227
- prompt(description: string): this;
228
- prompt(description: Function): this;
229
- define<T extends {
230
- [key: string]: any;
231
- } = RouterContextT>(opts: DefineRouteOpts): this;
232
- define<T extends {
233
- [key: string]: any;
234
- } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
235
- define<T extends {
236
- [key: string]: any;
237
- } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
238
- define<T extends {
239
- [key: string]: any;
240
- } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
241
- update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
242
- addTo(router: QueryRouter | {
243
- add: (route: Route) => void;
244
- [key: string]: any;
245
- }, opts?: AddOpts): void;
246
- throw(...args: any[]): void;
247
- }
248
- /**
249
- * @parmas overwrite 是否覆盖已存在的route,默认true
250
- */
251
- type AddOpts = {
252
- overwrite?: boolean;
253
- };
254
- declare class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
255
- appId: string;
256
- routes: Route[];
257
- maxNextRoute: number;
258
- context?: RouteContext<T>;
259
- constructor();
260
- /**
261
- * add route
262
- * @param route
263
- * @param opts
264
- */
265
- add(route: Route, opts?: AddOpts): void;
266
- /**
267
- * remove route by path and key
268
- * @param route
269
- */
270
- remove(route: Route | {
271
- path: string;
272
- key?: string;
273
- }): void;
274
- /**
275
- * remove route by id
276
- * @param uniqueId
277
- */
278
- removeById(uniqueId: string): void;
279
- /**
280
- * 执行route
281
- * @param path
282
- * @param key
283
- * @param ctx
284
- * @returns
285
- */
286
- runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
287
- /**
288
- * 第一次执行
289
- * @param message
290
- * @param ctx
291
- * @returns
292
- */
293
- parse(message: {
294
- path: string;
295
- key?: string;
296
- payload?: any;
297
- }, ctx?: RouteContext<T> & {
298
- [key: string]: any;
299
- }): Promise<RouteContext<T, {}, {
300
- [key: string]: any;
301
- }>>;
302
- /**
303
- * 返回的数据包含所有的context的请求返回的内容,可做其他处理
304
- * @param message
305
- * @param ctx
306
- * @returns
307
- */
308
- call(message: {
309
- id?: string;
310
- path?: string;
311
- key?: string;
312
- payload?: any;
313
- }, ctx?: RouteContext<T> & {
314
- [key: string]: any;
315
- }): Promise<RouteContext<T, {}, {
316
- [key: string]: any;
317
- }> | {
318
- code: number;
319
- body: any;
320
- message: string;
321
- }>;
322
- /**
323
- * 请求 result 的数据
324
- * @param message
325
- * @param ctx
326
- * @deprecated use run or call instead
327
- * @returns
328
- */
329
- queryRoute(message: {
330
- id?: string;
331
- path: string;
332
- key?: string;
333
- payload?: any;
334
- }, ctx?: RouteContext & {
335
- [key: string]: any;
336
- }): Promise<{
337
- code: number;
338
- data: any;
339
- message: string;
340
- }>;
341
- /**
342
- * Router Run获取数据
343
- * @param message
344
- * @param ctx
345
- * @returns
346
- */
347
- run(message: {
348
- id?: string;
349
- path?: string;
350
- key?: string;
351
- payload?: any;
352
- }, ctx?: RouteContext<T> & {
353
- [key: string]: any;
354
- }): Promise<{
355
- code: number;
356
- data: any;
357
- message: string;
358
- }>;
359
- /**
360
- * 设置上下文
361
- * @description 这里的上下文是为了在handle函数中使用
362
- * @param ctx
363
- */
364
- setContext(ctx: RouteContext): void;
365
- getList(filter?: (route: Route) => boolean): RouteInfo[];
366
- /**
367
- * 获取handle函数, 这里会去执行parse函数
368
- */
369
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
370
- id?: string;
371
- path?: string;
372
- key?: string;
373
- [key: string]: any;
374
- }, handleContext?: RouteContext<T>) => Promise<{
375
- [key: string]: any;
376
- code: string;
377
- data?: any;
378
- message?: string;
379
- } | {
380
- code: any;
381
- data: any;
382
- message: any;
383
- } | {
384
- code: number;
385
- message: any;
386
- data?: undefined;
387
- }>;
388
- exportRoutes(): Route<SimpleObject, SimpleObject>[];
389
- importRoutes(routes: Route[]): void;
390
- importRouter(router: QueryRouter): void;
391
- throw(...args: any[]): void;
392
- hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
393
- findRoute(opts?: {
394
- path?: string;
395
- key?: string;
396
- id?: string;
397
- }): Route<SimpleObject, SimpleObject>;
398
- createRouteList(opts?: {
399
- force?: boolean;
400
- filter?: (route: Route) => boolean;
401
- middleware?: string[];
402
- }): void;
403
- /**
404
- * 等待程序运行, 获取到message的数据,就执行
405
- * params 是预设参数
406
- * emitter = process
407
- * -- .exit
408
- * -- .on
409
- * -- .send
410
- */
411
- wait(params?: {
412
- message: RunMessage;
413
- }, opts?: {
414
- mockProcess?: MockProcess;
415
- timeout?: number;
416
- getList?: boolean;
417
- force?: boolean;
418
- filter?: (route: Route) => boolean;
419
- routeListMiddleware?: string[];
420
- }): Promise<void>;
421
- toJSONSchema: (args: any, opts?: {
422
- mergeObject?: boolean;
423
- override?: (opts: {
424
- jsonSchema: any;
425
- path: string[];
426
- zodSchema: z.ZodTypeAny;
427
- }) => void;
428
- }) => {
429
- [key: string]: any;
430
- };
431
- fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
432
- mergeObject?: boolean;
433
- }) => Merge extends true ? z.ZodObject<{
434
- [key: string]: any;
435
- }, z.core.$strip> : {
436
- [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
437
- };
438
- }
439
- type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
440
- handleFn?: HandleFn;
441
- context?: RouteContext<C>;
442
- appId?: string;
443
- initHandle?: boolean;
444
- };
445
- interface HandleFn<T = any> {
446
- (msg: {
447
- path: string;
448
- [key: string]: any;
449
- }, ctx?: any): {
450
- code: string;
451
- data?: any;
452
- message?: string;
453
- [key: string]: any;
454
- };
455
- (res: RouteContext<T>): any;
456
- }
457
- /**
458
- * QueryRouterServer
459
- * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
460
- * @template C 自定义 RouteContext 类型
461
- */
462
- declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
463
- appId: string;
464
- handle: any;
465
- context: RouteContext<C>;
466
- constructor(opts?: QueryRouterServerOpts<C>);
467
- setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
468
- addRoute(route: Route, opts?: AddOpts): void;
469
- Route: typeof Route;
470
- route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & {
471
- metadata?: M;
472
- }): Route<M, Required<RouteContext<C>>>;
473
- route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & {
474
- metadata?: M;
475
- }): Route<M, Required<RouteContext<C>>>;
476
- route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
477
- route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & {
478
- metadata?: M;
479
- }): Route<M, Required<RouteContext<C>>>;
480
- prompt(description: string): Route<SimpleObject, SimpleObject>;
481
- /**
482
- * 调用了handle
483
- * @param param0
484
- * @returns
485
- */
486
- run(msg: {
487
- id?: string;
488
- path?: string;
489
- key?: string;
490
- payload?: any;
491
- token?: string;
492
- data?: any;
493
- }, ctx?: Partial<RouteContext<C>>): Promise<any>;
494
- runAction<T extends {
495
- id?: string;
496
- path?: string;
497
- key?: string;
498
- metadata?: {
499
- args?: any;
500
- };
501
- } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<any>;
502
- }
503
- /** JSON Schema 基本类型映射到 TypeScript 类型 */
504
- type JsonSchemaTypeToTS<T> = T extends {
505
- type: "string";
506
- } ? string : T extends {
507
- type: "boolean";
508
- } ? boolean : T extends {
509
- type: "number";
510
- } ? number : T extends {
511
- type: "integer";
512
- } ? number : T extends {
513
- type: "object";
514
- } ? object : T extends {
515
- type: "array";
516
- } ? any[] : any;
517
- /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
518
- type ArgsShapeToPayload<T> = {
519
- [K in keyof T as T[K] extends {
520
- optional: true;
521
- } ? never : K]: JsonSchemaTypeToTS<T[K]>;
522
- } & {
523
- [K in keyof T as T[K] extends {
524
- optional: true;
525
- } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
526
- };
527
- /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
528
- type ArgsToPayload<T> = T extends {
529
- type: "object";
530
- properties: infer P;
531
- } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
532
- /** 从 API 定义中提取 metadata.args */
533
- type ExtractArgs<T> = T extends {
534
- metadata: {
535
- args: infer A;
536
- };
537
- } ? A : {};
538
- /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
539
- type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
540
-
541
- type Cors = {
542
- /**
543
- * @default '*''
544
- */
545
- origin?: string | undefined;
546
- };
547
- type ServerOpts<T = {}> = {
548
- /**path default `/api/router` */
549
- path?: string;
550
- /**handle Fn */
551
- handle?: (msg?: {
552
- path: string;
553
- key?: string;
554
- [key: string]: any;
555
- }, ctx?: {
556
- req: http.IncomingMessage;
557
- res: http.ServerResponse;
558
- }) => any;
559
- cors?: Cors;
560
- io?: boolean;
561
- showConnected?: boolean;
562
- } & T;
563
- interface ServerType {
564
- path?: string;
565
- server?: any;
566
- handle: ServerOpts['handle'];
567
- setHandle(handle?: any): void;
568
- listeners: Listener[];
569
- listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
570
- listen(port: number, hostname?: string, listeningListener?: () => void): void;
571
- listen(port: number, backlog?: number, listeningListener?: () => void): void;
572
- listen(port: number, listeningListener?: () => void): void;
573
- listen(path: string, backlog?: number, listeningListener?: () => void): void;
574
- listen(path: string, listeningListener?: () => void): void;
575
- listen(handle: any, backlog?: number, listeningListener?: () => void): void;
576
- listen(handle: any, listeningListener?: () => void): void;
577
- /**
578
- * 兜底监听,当除开 `/api/router` 之外的请求,框架只监听一个api,所以有其他的请求都执行其他的监听
579
- * @description 主要是为了兼容其他的监听
580
- * @param listener
581
- */
582
- on(listener: OnListener): void;
583
- onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
584
- onWsClose<T = {}>(ws: WS<T>): void;
585
- sendConnected<T = {}>(ws: WS<T>): void;
586
- }
587
- type OnWebSocketOptions<T = {}> = {
588
- ws: WS<T>;
589
- message: string | Buffer;
590
- pathname: string;
591
- token?: string;
592
- /** data 的id提取出来 */
593
- id?: string;
594
- };
595
- type WS<T = {}> = {
596
- send: (data: any) => void;
597
- close: (code?: number, reason?: string) => void;
598
- /**
599
- * ws 自己生成的一个id,主要是为了区分不同的ws连接,方便在onWebSocket中使用
600
- */
601
- wsId?: string;
602
- data?: {
603
- /**
604
- * ws连接时的url,包含pathname和searchParams
605
- */
606
- url: URL;
607
- /**
608
- * ws连接时的pathname
609
- */
610
- pathname: string;
611
- /**
612
- * ws连接时的url中的token参数
613
- */
614
- token?: string;
615
- /**
616
- * ws连接时的url中的id参数.
617
- */
618
- id?: string;
619
- /**
620
- * 鉴权后的获取的信息
621
- */
622
- userApp?: string;
623
- } & T;
624
- };
625
- type Listener = {
626
- id?: string;
627
- io?: boolean;
628
- path?: string;
629
- func: WebSocketListenerFun | HttpListenerFun;
630
- /**
631
- * @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
632
- */
633
- json?: boolean;
634
- };
635
- type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
636
- type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
637
- type WebSocketReq<T = {}, U = Record<string, any>> = {
638
- emitter?: EventEmitter;
639
- ws: WS<T>;
640
- data?: U;
641
- message?: string | Buffer;
642
- pathname?: string;
643
- token?: string;
644
- id?: string;
645
- };
646
- type WebSocketRes = {
647
- end: (data: any) => void;
648
- };
649
- type ListenerFun = WebSocketListenerFun | HttpListenerFun;
650
- type OnListener = Listener | ListenerFun | (Listener | ListenerFun)[];
651
- type RouterReq<T = {}> = {
652
- url: string;
653
- method: string;
654
- headers: Record<string, string>;
655
- socket?: {
656
- remoteAddress?: string;
657
- remotePort?: number;
658
- };
659
- body?: string;
660
- cookies?: Record<string, string>;
661
- bun?: {
662
- request: Bun.BunRequest;
663
- server: Bun.Server<{}>;
664
- resolve: (response: Response) => void;
665
- };
666
- on: (event: 'close', listener: Function) => void;
667
- } & T;
668
- type RouterRes<T = {}> = {
669
- statusCode: number;
670
- headersSent: boolean;
671
- _headers: Record<string, string | string[]>;
672
- _bodyChunks: any[];
673
- writableEnded: boolean;
674
- writeHead: (statusCode: number, headers?: Record<string, string>) => void;
675
- setHeader: (name: string, value: string | string[]) => void;
676
- cookie: (name: string, value: string, options?: any) => void;
677
- write: (chunk: any) => void;
678
- pipe: (stream: ReadableStream) => void;
679
- end: (data?: any) => void;
680
- } & T;
681
-
682
- interface StringifyOptions {
683
- /**
684
- * Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
685
- * Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
686
- * a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
687
- *
688
- * @default encodeURIComponent
689
- */
690
- encode?: (str: string) => string;
691
- }
692
- /**
693
- * Set-Cookie object.
694
- */
695
- interface SetCookie {
696
- /**
697
- * Specifies the name of the cookie.
698
- */
699
- name: string;
700
- /**
701
- * Specifies the string to be the value for the cookie.
702
- */
703
- value: string | undefined;
704
- /**
705
- * Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
706
- *
707
- * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
708
- * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
709
- * so if both are set, they should point to the same date and time.
710
- */
711
- maxAge?: number;
712
- /**
713
- * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
714
- * When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
715
- *
716
- * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
717
- * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
718
- * so if both are set, they should point to the same date and time.
719
- */
720
- expires?: Date;
721
- /**
722
- * Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
723
- * When no domain is set, clients consider the cookie to apply to the current domain only.
724
- */
725
- domain?: string;
726
- /**
727
- * Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
728
- * When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
729
- */
730
- path?: string;
731
- /**
732
- * Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
733
- * When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
734
- */
735
- httpOnly?: boolean;
736
- /**
737
- * Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
738
- * When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
739
- */
740
- secure?: boolean;
741
- /**
742
- * Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
743
- * When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
744
- *
745
- * This is an attribute that has not yet been fully standardized, and may change in the future.
746
- * This also means clients may ignore this attribute until they understand it. More information
747
- * about can be found in [the proposal](https://github.com/privacycg/CHIPS).
748
- */
749
- partitioned?: boolean;
750
- /**
751
- * Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
752
- *
753
- * - `'low'` will set the `Priority` attribute to `Low`.
754
- * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
755
- * - `'high'` will set the `Priority` attribute to `High`.
756
- *
757
- * More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
758
- */
759
- priority?: "low" | "medium" | "high";
760
- /**
761
- * Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
762
- *
763
- * - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
764
- * - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
765
- * - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
766
- * - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
767
- *
768
- * More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
769
- */
770
- sameSite?: boolean | "lax" | "strict" | "none";
771
- }
772
- /**
773
- * Backward compatibility serialize options.
774
- */
775
- type SerializeOptions = StringifyOptions & Omit<SetCookie, "name" | "value">;
776
-
777
- type CookieFn = (name: string, value: string, options?: SerializeOptions, end?: boolean) => void;
778
- type HandleCtx = {
779
- req: IncomingMessage & {
780
- cookies: Record<string, string>;
781
- };
782
- res: ServerResponse & {
783
- /**
784
- * cookie 函数, end 参数用于设置是否立即设置到响应头,设置了后面的cookie再设置会覆盖前面的
785
- */
786
- cookie: CookieFn;
787
- };
788
- };
789
-
790
- type ServerNodeOpts = ServerOpts<{
791
- httpType?: 'http' | 'https' | 'http2';
792
- httpsKey?: string;
793
- httpsCert?: string;
794
- }>;
795
-
796
- type RouterHandle = (msg: {
797
- path: string;
798
- [key: string]: any;
799
- }) => {
800
- code: string;
801
- data?: any;
802
- message?: string;
803
- [key: string]: any;
804
- };
805
- type AppOptions<T = {}> = {
806
- router?: QueryRouterServer;
807
- server?: ServerType;
808
- /** handle msg 关联 */
809
- routerHandle?: RouterHandle;
810
- routerContext?: RouteContext<T>;
811
- serverOptions?: ServerNodeOpts;
812
- appId?: string;
813
- };
814
- type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
815
- app: App<T>;
816
- };
817
- /**
818
- * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
819
- * U - Route Context的扩展类型
820
- */
821
- declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
822
- appId: string;
823
- router: QueryRouterServer;
824
- server: ServerType;
825
- context: AppRouteContext<U>;
826
- constructor(opts?: AppOptions<U>);
827
- listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
828
- listen(port: number, hostname?: string, listeningListener?: () => void): void;
829
- listen(port: number, backlog?: number, listeningListener?: () => void): void;
830
- listen(port: number, listeningListener?: () => void): void;
831
- listen(path: string, backlog?: number, listeningListener?: () => void): void;
832
- listen(path: string, listeningListener?: () => void): void;
833
- listen(handle: any, backlog?: number, listeningListener?: () => void): void;
834
- listen(handle: any, listeningListener?: () => void): void;
835
- Route: typeof Route;
836
- static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
837
- cookies: Record<string, string>;
838
- token: string;
839
- }>;
840
- onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
841
- }
842
-
843
- declare const handleCallWsApp: (ws: ReconnectingWebSocket, app: App, message: any) => Promise<void>;
844
- declare const handleCallApp: (send: (data: any) => void, app: App, message: any) => Promise<void>;
845
- declare class Ws {
846
- wsClient: ReconnectingWebSocket;
847
- app: App;
848
- showLog: boolean;
849
- constructor(opts?: ReconnectConfig & {
850
- url: string;
851
- app: App;
852
- showLog?: boolean;
853
- handleMessage?: (ws: ReconnectingWebSocket, app: App, message: any) => void;
854
- });
855
- send(data: any): boolean;
856
- log(...args: any[]): void;
857
- }
858
-
859
- export { ReconnectingWebSocket, Ws, handleCallApp, handleCallWsApp };
860
- export type { ReconnectConfig };