@kevisual/router 0.0.57 → 0.0.59

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.
@@ -1,3 +1,7 @@
1
+ import { EventEmitter } from 'eventemitter3';
2
+ import * as http from 'node:http';
3
+ import { IncomingMessage, ServerResponse } from 'node:http';
4
+ import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
1
5
  import { Plugin } from '@opencode-ai/plugin';
2
6
 
3
7
  type RouterContextT = {
@@ -310,6 +314,11 @@ declare class QueryRouter {
310
314
  filter?: (route: Route) => boolean;
311
315
  }): Promise<void>;
312
316
  }
317
+ type QueryRouterServerOpts = {
318
+ handleFn?: HandleFn;
319
+ context?: RouteContext;
320
+ appId?: string;
321
+ };
313
322
  interface HandleFn<T = any> {
314
323
  (msg: {
315
324
  path: string;
@@ -322,10 +331,347 @@ interface HandleFn<T = any> {
322
331
  };
323
332
  (res: RouteContext<T>): any;
324
333
  }
334
+ /**
335
+ * QueryRouterServer
336
+ * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
337
+ */
338
+ declare class QueryRouterServer extends QueryRouter {
339
+ appId: string;
340
+ handle: any;
341
+ constructor(opts?: QueryRouterServerOpts);
342
+ setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
343
+ addRoute(route: Route): void;
344
+ Route: typeof Route;
345
+ route(opts: RouteOpts): Route<Required<RouteContext>>;
346
+ route(path: string, key?: string): Route<Required<RouteContext>>;
347
+ route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
348
+ route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
349
+ prompt(description: string): Route<Required<RouteContext>>;
350
+ prompt(description: Function): Route<Required<RouteContext>>;
351
+ /**
352
+ * 调用了handle
353
+ * @param param0
354
+ * @returns
355
+ */
356
+ run(msg: {
357
+ id?: string;
358
+ path?: string;
359
+ key?: string;
360
+ payload?: any;
361
+ }, ctx?: RouteContext & {
362
+ [key: string]: any;
363
+ }): Promise<any>;
364
+ }
325
365
 
326
- declare const createRouterAgentPluginFn: (opts?: {
366
+ type Cors = {
367
+ /**
368
+ * @default '*''
369
+ */
370
+ origin?: string | undefined;
371
+ };
372
+ type ServerOpts<T = {}> = {
373
+ /**path default `/api/router` */
374
+ path?: string;
375
+ /**handle Fn */
376
+ handle?: (msg?: {
377
+ path: string;
378
+ key?: string;
379
+ [key: string]: any;
380
+ }, ctx?: {
381
+ req: http.IncomingMessage;
382
+ res: http.ServerResponse;
383
+ }) => any;
384
+ cors?: Cors;
385
+ io?: boolean;
386
+ showConnected?: boolean;
387
+ } & T;
388
+ interface ServerType {
389
+ path?: string;
390
+ server?: any;
391
+ handle: ServerOpts['handle'];
392
+ setHandle(handle?: any): void;
393
+ listeners: Listener[];
394
+ listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
395
+ listen(port: number, hostname?: string, listeningListener?: () => void): void;
396
+ listen(port: number, backlog?: number, listeningListener?: () => void): void;
397
+ listen(port: number, listeningListener?: () => void): void;
398
+ listen(path: string, backlog?: number, listeningListener?: () => void): void;
399
+ listen(path: string, listeningListener?: () => void): void;
400
+ listen(handle: any, backlog?: number, listeningListener?: () => void): void;
401
+ listen(handle: any, listeningListener?: () => void): void;
402
+ /**
403
+ * 兜底监听,当除开 `/api/router` 之外的请求,框架只监听一个api,所以有其他的请求都执行其他的监听
404
+ * @description 主要是为了兼容其他的监听
405
+ * @param listener
406
+ */
407
+ on(listener: OnListener): void;
408
+ onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
409
+ onWsClose<T = {}>(ws: WS<T>): void;
410
+ sendConnected<T = {}>(ws: WS<T>): void;
411
+ }
412
+ type OnWebSocketOptions<T = {}> = {
413
+ ws: WS<T>;
414
+ message: string | Buffer;
415
+ pathname: string;
416
+ token?: string;
417
+ id?: string;
418
+ };
419
+ type WS<T = {}> = {
420
+ send: (data: any) => void;
421
+ close: (code?: number, reason?: string) => void;
422
+ data?: {
423
+ url: URL;
424
+ pathname: string;
425
+ token?: string;
426
+ id?: string;
427
+ /**
428
+ * 鉴权后的获取的信息
429
+ */
430
+ userApp?: string;
431
+ } & T;
432
+ };
433
+ type Listener = {
434
+ id?: string;
435
+ io?: boolean;
436
+ path?: string;
437
+ func: WebSocketListenerFun | HttpListenerFun;
438
+ /**
439
+ * @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
440
+ */
441
+ json?: boolean;
442
+ };
443
+ type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
444
+ type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
445
+ type WebSocketReq<T = {}, U = Record<string, any>> = {
446
+ emitter?: EventEmitter;
447
+ ws: WS<T>;
448
+ data?: U;
449
+ message?: string | Buffer;
450
+ pathname?: string;
451
+ token?: string;
452
+ id?: string;
453
+ };
454
+ type WebSocketRes = {
455
+ end: (data: any) => void;
456
+ };
457
+ type ListenerFun = WebSocketListenerFun | HttpListenerFun;
458
+ type OnListener = Listener | ListenerFun | (Listener | ListenerFun)[];
459
+ type RouterReq<T = {}> = {
460
+ url: string;
461
+ method: string;
462
+ headers: Record<string, string>;
463
+ socket?: {
464
+ remoteAddress?: string;
465
+ remotePort?: number;
466
+ };
467
+ body?: string;
468
+ cookies?: Record<string, string>;
469
+ } & T;
470
+ type RouterRes<T = {}> = {
471
+ statusCode: number;
472
+ headersSent: boolean;
473
+ _headers: Record<string, string | string[]>;
474
+ _bodyChunks: any[];
475
+ writableEnded: boolean;
476
+ writeHead: (statusCode: number, headers?: Record<string, string>) => void;
477
+ setHeader: (name: string, value: string | string[]) => void;
478
+ cookie: (name: string, value: string, options?: any) => void;
479
+ write: (chunk: any) => void;
480
+ pipe: (stream: any) => void;
481
+ end: (data?: any) => void;
482
+ } & T;
483
+
484
+ interface StringifyOptions {
485
+ /**
486
+ * Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
487
+ * Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
488
+ * a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
489
+ *
490
+ * @default encodeURIComponent
491
+ */
492
+ encode?: (str: string) => string;
493
+ }
494
+ /**
495
+ * Set-Cookie object.
496
+ */
497
+ interface SetCookie {
498
+ /**
499
+ * Specifies the name of the cookie.
500
+ */
501
+ name: string;
502
+ /**
503
+ * Specifies the string to be the value for the cookie.
504
+ */
505
+ value: string | undefined;
506
+ /**
507
+ * 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).
508
+ *
509
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
510
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
511
+ * so if both are set, they should point to the same date and time.
512
+ */
513
+ maxAge?: number;
514
+ /**
515
+ * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
516
+ * When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
517
+ *
518
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
519
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
520
+ * so if both are set, they should point to the same date and time.
521
+ */
522
+ expires?: Date;
523
+ /**
524
+ * Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
525
+ * When no domain is set, clients consider the cookie to apply to the current domain only.
526
+ */
527
+ domain?: string;
528
+ /**
529
+ * Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
530
+ * When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
531
+ */
532
+ path?: string;
533
+ /**
534
+ * Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
535
+ * When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
536
+ */
537
+ httpOnly?: boolean;
538
+ /**
539
+ * Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
540
+ * When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
541
+ */
542
+ secure?: boolean;
543
+ /**
544
+ * Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
545
+ * When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
546
+ *
547
+ * This is an attribute that has not yet been fully standardized, and may change in the future.
548
+ * This also means clients may ignore this attribute until they understand it. More information
549
+ * about can be found in [the proposal](https://github.com/privacycg/CHIPS).
550
+ */
551
+ partitioned?: boolean;
552
+ /**
553
+ * Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
554
+ *
555
+ * - `'low'` will set the `Priority` attribute to `Low`.
556
+ * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
557
+ * - `'high'` will set the `Priority` attribute to `High`.
558
+ *
559
+ * More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
560
+ */
561
+ priority?: "low" | "medium" | "high";
562
+ /**
563
+ * Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
564
+ *
565
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
566
+ * - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
567
+ * - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
568
+ * - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
569
+ *
570
+ * 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).
571
+ */
572
+ sameSite?: boolean | "lax" | "strict" | "none";
573
+ }
574
+ /**
575
+ * Backward compatibility serialize options.
576
+ */
577
+ type SerializeOptions = StringifyOptions & Omit<SetCookie, "name" | "value">;
578
+
579
+ type CookieFn = (name: string, value: string, options?: SerializeOptions, end?: boolean) => void;
580
+ type HandleCtx = {
581
+ req: IncomingMessage & {
582
+ cookies: Record<string, string>;
583
+ };
584
+ res: ServerResponse & {
585
+ /**
586
+ * cookie 函数, end 参数用于设置是否立即设置到响应头,设置了后面的cookie再设置会覆盖前面的
587
+ */
588
+ cookie: CookieFn;
589
+ };
590
+ };
591
+
592
+ type ServerNodeOpts = ServerOpts<{
593
+ httpType?: 'http' | 'https' | 'http2';
594
+ httpsKey?: string;
595
+ httpsCert?: string;
596
+ }>;
597
+
598
+ type RouterHandle = (msg: {
599
+ path: string;
600
+ [key: string]: any;
601
+ }) => {
602
+ code: string;
603
+ data?: any;
604
+ message?: string;
605
+ [key: string]: any;
606
+ };
607
+ type AppOptions<T = {}> = {
327
608
  router?: QueryRouter;
609
+ server?: ServerType;
610
+ /** handle msg 关联 */
611
+ routerHandle?: RouterHandle;
612
+ routerContext?: RouteContext<T>;
613
+ serverOptions?: ServerNodeOpts;
614
+ appId?: string;
615
+ };
616
+ type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
617
+ app: App<T>;
618
+ };
619
+ /**
620
+ * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
621
+ * U - Route Context的扩展类型
622
+ */
623
+ declare class App<U = {}> extends QueryRouter {
624
+ appId: string;
625
+ router: QueryRouter;
626
+ server: ServerType;
627
+ constructor(opts?: AppOptions<U>);
628
+ listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
629
+ listen(port: number, hostname?: string, listeningListener?: () => void): void;
630
+ listen(port: number, backlog?: number, listeningListener?: () => void): void;
631
+ listen(port: number, listeningListener?: () => void): void;
632
+ listen(path: string, backlog?: number, listeningListener?: () => void): void;
633
+ listen(path: string, listeningListener?: () => void): void;
634
+ listen(handle: any, backlog?: number, listeningListener?: () => void): void;
635
+ listen(handle: any, listeningListener?: () => void): void;
636
+ addRoute(route: Route): void;
637
+ Route: typeof Route;
638
+ route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
639
+ route(path: string, key?: string): Route<AppRouteContext<U>>;
640
+ route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
641
+ route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
642
+ prompt(description: string): Route<AppRouteContext<U>>;
643
+ prompt(description: Function): Route<AppRouteContext<U>>;
644
+ call(message: {
645
+ id?: string;
646
+ path?: string;
647
+ key?: string;
648
+ payload?: any;
649
+ }, ctx?: AppRouteContext<U> & {
650
+ [key: string]: any;
651
+ }): Promise<any>;
652
+ run(msg: {
653
+ id?: string;
654
+ path?: string;
655
+ key?: string;
656
+ payload?: any;
657
+ }, ctx?: Partial<AppRouteContext<U>> & {
658
+ [key: string]: any;
659
+ }): Promise<{
660
+ code: any;
661
+ data: any;
662
+ message: any;
663
+ }>;
664
+ static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
665
+ cookies: Record<string, string>;
666
+ token: string;
667
+ }>;
668
+ onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
669
+ }
670
+
671
+ declare const addCallFn: (app: App) => void;
672
+ declare const createRouterAgentPluginFn: (opts?: {
673
+ router?: App | QueryRouterServer;
328
674
  query?: string;
329
675
  }) => Plugin;
330
676
 
331
- export { createRouterAgentPluginFn };
677
+ export { addCallFn, createRouterAgentPluginFn };