@kevisual/router 0.0.23 → 0.0.24

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/router.d.ts CHANGED
@@ -5,7 +5,6 @@ import https from 'node:https';
5
5
  import http2 from 'node:http2';
6
6
  import * as cookie from 'cookie';
7
7
  import { WebSocketServer, WebSocket } from 'ws';
8
- import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
9
8
  import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
10
9
  import { Query, DataOpts, Result } from '@kevisual/query/query';
11
10
 
@@ -407,36 +406,6 @@ declare class QueryRouterServer extends QueryRouter {
407
406
  }): Promise<any>;
408
407
  }
409
408
 
410
- declare class Connect {
411
- path: string;
412
- key?: string;
413
- _fn?: (ctx?: RouteContext) => Promise<RouteContext>;
414
- description?: string;
415
- connects: {
416
- path: string;
417
- key?: string;
418
- }[];
419
- share: boolean;
420
- constructor(path: string);
421
- use(path: string): void;
422
- useList(paths: string[]): void;
423
- useConnect(connect: Connect): void;
424
- useConnectList(connects: Connect[]): void;
425
- getPathList(): string[];
426
- set fn(fn: (ctx?: RouteContext) => Promise<RouteContext>);
427
- get fn(): (ctx?: RouteContext) => Promise<RouteContext>;
428
- }
429
- declare class QueryConnect {
430
- connects: Connect[];
431
- constructor();
432
- add(connect: Connect): void;
433
- remove(connect: Connect): void;
434
- getList(): {
435
- path: string;
436
- key: string;
437
- }[];
438
- }
439
-
440
409
  type Listener = (...args: any[]) => void;
441
410
  type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
442
411
  type HandleCtx = {
@@ -720,12 +689,12 @@ declare class App<T = {}, U = AppReqRes> {
720
689
  importRoutes(routes: any[]): void;
721
690
  importApp(app: App): void;
722
691
  throw(code?: number | string, message?: string, tips?: string): void;
723
- static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
692
+ static handleRequest(req: IncomingMessage, res: ServerResponse): Promise<{
724
693
  cookies: Record<string, string>;
725
694
  token: string;
726
695
  }>;
727
- onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
696
+ onServerRequest(fn: (req: IncomingMessage, res: ServerResponse) => void): void;
728
697
  }
729
698
 
730
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
699
+ export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
731
700
  export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run };
package/dist/router.js CHANGED
@@ -22584,70 +22584,6 @@ class QueryRouterServer extends QueryRouter {
22584
22584
  }
22585
22585
  }
22586
22586
 
22587
- class Connect {
22588
- path;
22589
- key;
22590
- _fn;
22591
- description;
22592
- connects;
22593
- share = false;
22594
- constructor(path) {
22595
- this.path = path;
22596
- this.key = nanoid();
22597
- }
22598
- use(path) {
22599
- this.connects.push({ path });
22600
- }
22601
- useList(paths) {
22602
- paths.forEach((path) => {
22603
- this.connects.push({ path });
22604
- });
22605
- }
22606
- useConnect(connect) {
22607
- this.connects.push({ path: connect.path, key: connect.key });
22608
- }
22609
- useConnectList(connects) {
22610
- connects.forEach((connect) => {
22611
- this.connects.push({ path: connect.path, key: connect.key });
22612
- });
22613
- }
22614
- getPathList() {
22615
- return this.connects.map((c) => c.path).filter(Boolean);
22616
- }
22617
- set fn(fn) {
22618
- this._fn = fn;
22619
- }
22620
- get fn() {
22621
- return this._fn;
22622
- }
22623
- }
22624
- class QueryConnect {
22625
- connects;
22626
- constructor() {
22627
- this.connects = [];
22628
- }
22629
- add(connect) {
22630
- const has = this.connects.find((c) => c.path === connect.path && c.key === connect.key);
22631
- if (has) {
22632
- // remove the old connect
22633
- console.log('[replace connect]:', connect.path, connect.key);
22634
- this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
22635
- }
22636
- this.connects.push(connect);
22637
- }
22638
- remove(connect) {
22639
- this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
22640
- }
22641
- getList() {
22642
- return this.connects.map((c) => {
22643
- return {
22644
- path: c.path,
22645
- key: c.key,
22646
- };
22647
- });
22648
- }
22649
- }
22650
-
22651
22587
  const parseBody = async (req) => {
22652
22588
  return new Promise((resolve, reject) => {
22653
22589
  const arr = [];
@@ -28483,4 +28419,4 @@ class App {
28483
28419
  }
28484
28420
  }
28485
28421
 
28486
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
28422
+ export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.23",
4
+ "version": "0.0.24",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -10,7 +10,9 @@
10
10
  "build": "npm run clean && rollup -c",
11
11
  "build:app": "npm run build && rsync dist/*browser* ../deploy/dist",
12
12
  "watch": "rollup -c -w",
13
- "clean": "rm -rf dist"
13
+ "clean": "rm -rf dist",
14
+ "auto:bun": "bun test/auto/app.ts",
15
+ "auto:deno": "bun test/auto/app.ts"
14
16
  },
15
17
  "files": [
16
18
  "dist",
@@ -27,12 +29,15 @@
27
29
  "@rollup/plugin-commonjs": "^28.0.6",
28
30
  "@rollup/plugin-node-resolve": "^16.0.1",
29
31
  "@rollup/plugin-typescript": "^12.1.3",
32
+ "@types/bun": "^1.2.10",
33
+ "@types/deno": "^2.3.0",
30
34
  "@types/lodash-es": "^4.17.12",
31
35
  "@types/node": "^24.0.3",
32
36
  "@types/send": "^0.17.5",
33
37
  "@types/ws": "^8.18.1",
34
38
  "@types/xml2js": "^0.4.14",
35
39
  "cookie": "^1.0.2",
40
+ "deno": "^2.4.3",
36
41
  "lodash-es": "^4.17.21",
37
42
  "nanoid": "^5.1.5",
38
43
  "rollup": "^4.44.0",
@@ -93,6 +98,11 @@
93
98
  "require": "./mod.ts",
94
99
  "types": "./mod.d.ts"
95
100
  },
101
+ "./auto.ts": {
102
+ "import": "./auto.ts",
103
+ "require": "./auto.ts",
104
+ "types": "./auto.ts"
105
+ },
96
106
  "./src/*": {
97
107
  "import": "./src/*",
98
108
  "require": "./src/*"
package/readme.md CHANGED
@@ -20,3 +20,17 @@ app
20
20
  })
21
21
  .addTo(app);
22
22
  ```
23
+ ## 兼容服务器
24
+ ```
25
+ import { App } from '@kevisual/router';
26
+
27
+ const app = new App();
28
+ app.listen(4002);
29
+ import { proxyRoute, initProxy } from '@kevisual/local-proxy/proxy.ts';
30
+ initProxy({
31
+ pagesDir: './demo',
32
+ watch: true,
33
+ });
34
+
35
+ app.onServerRequest(proxyRoute);
36
+ ```
package/src/app.ts CHANGED
@@ -3,7 +3,7 @@ import { Server, ServerOpts, HandleCtx } from './server/server.ts';
3
3
  import { WsServer } from './server/ws-server.ts';
4
4
  import { CustomError } from './result/error.ts';
5
5
  import { handleServer } from './server/handle-server.ts';
6
- import { IncomingMessage, ServerResponse } from 'http';
6
+ import { IncomingMessage, ServerResponse } from 'node:http';
7
7
 
8
8
  type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
9
9
  type AppOptions<T = {}> = {
@@ -0,0 +1,151 @@
1
+ import { createConnection } from 'node:net';
2
+
3
+ type QueryData = {
4
+ path?: string;
5
+ key?: string;
6
+ payload?: any;
7
+ [key: string]: any;
8
+ };
9
+
10
+ type CallSockOptions = {
11
+ socketPath?: string;
12
+ timeout?: number;
13
+ method?: 'GET' | 'POST';
14
+ };
15
+
16
+ export const callSock = async (data: QueryData, options: CallSockOptions = {}): Promise<any> => {
17
+ const { socketPath = './app.sock', timeout = 5000, method = 'POST' } = options;
18
+
19
+ return new Promise((resolve, reject) => {
20
+ const client = createConnection(socketPath);
21
+ let responseData = '';
22
+ let timer: NodeJS.Timeout;
23
+
24
+ // 设置超时
25
+ if (timeout > 0) {
26
+ timer = setTimeout(() => {
27
+ client.destroy();
28
+ reject(new Error(`Socket call timeout after ${timeout}ms`));
29
+ }, timeout);
30
+ }
31
+
32
+ client.on('connect', () => {
33
+ try {
34
+ let request: string;
35
+
36
+ if (method === 'GET') {
37
+ // GET 请求:参数放在 URL 中
38
+ const searchParams = new URLSearchParams();
39
+ Object.entries(data).forEach(([key, value]) => {
40
+ if (key === 'payload' && typeof value === 'object') {
41
+ searchParams.append(key, JSON.stringify(value));
42
+ } else {
43
+ searchParams.append(key, String(value));
44
+ }
45
+ });
46
+
47
+ const queryString = searchParams.toString();
48
+ const url = queryString ? `/?${queryString}` : '/';
49
+
50
+ request = [`GET ${url} HTTP/1.1`, 'Host: localhost', 'Connection: close', '', ''].join('\r\n');
51
+ } else {
52
+ // POST 请求:数据放在 body 中
53
+ const body = JSON.stringify(data);
54
+ const contentLength = Buffer.byteLength(body, 'utf8');
55
+
56
+ request = [
57
+ 'POST / HTTP/1.1',
58
+ 'Host: localhost',
59
+ 'Content-Type: application/json',
60
+ `Content-Length: ${contentLength}`,
61
+ 'Connection: close',
62
+ '',
63
+ body,
64
+ ].join('\r\n');
65
+ }
66
+
67
+ client.write(request);
68
+ } catch (error) {
69
+ if (timer) clearTimeout(timer);
70
+ client.destroy();
71
+ reject(error);
72
+ }
73
+ });
74
+
75
+ client.on('data', (chunk) => {
76
+ responseData += chunk.toString();
77
+ });
78
+
79
+ client.on('end', () => {
80
+ if (timer) clearTimeout(timer);
81
+
82
+ try {
83
+ // 解析 HTTP 响应
84
+ const response = parseHttpResponse(responseData);
85
+
86
+ if (response.statusCode >= 400) {
87
+ reject(new Error(`HTTP ${response.statusCode}: ${response.body}`));
88
+ return;
89
+ }
90
+
91
+ // 尝试解析 JSON 响应
92
+ try {
93
+ const result = JSON.parse(response.body);
94
+ resolve(result);
95
+ } catch {
96
+ // 如果不是 JSON,直接返回文本
97
+ resolve(response.body);
98
+ }
99
+ } catch (error) {
100
+ reject(error);
101
+ }
102
+ });
103
+
104
+ client.on('error', (error) => {
105
+ if (timer) clearTimeout(timer);
106
+ reject(error);
107
+ });
108
+
109
+ client.on('timeout', () => {
110
+ if (timer) clearTimeout(timer);
111
+ client.destroy();
112
+ reject(new Error('Socket connection timeout'));
113
+ });
114
+ });
115
+ };
116
+
117
+ // 解析 HTTP 响应的辅助函数
118
+ function parseHttpResponse(responseData: string) {
119
+ const [headerSection, ...bodyParts] = responseData.split('\r\n\r\n');
120
+ const body = bodyParts.join('\r\n\r\n');
121
+
122
+ const lines = headerSection.split('\r\n');
123
+ const statusLine = lines[0];
124
+ const statusMatch = statusLine.match(/HTTP\/\d\.\d (\d+)/);
125
+ const statusCode = statusMatch ? parseInt(statusMatch[1]) : 200;
126
+
127
+ const headers: Record<string, string> = {};
128
+ for (let i = 1; i < lines.length; i++) {
129
+ const [key, ...valueParts] = lines[i].split(':');
130
+ if (key && valueParts.length > 0) {
131
+ headers[key.trim().toLowerCase()] = valueParts.join(':').trim();
132
+ }
133
+ }
134
+
135
+ return {
136
+ statusCode,
137
+ headers,
138
+ body: body || '',
139
+ };
140
+ }
141
+
142
+ // 便捷方法
143
+ export const callSockGet = (data: QueryData, options?: Omit<CallSockOptions, 'method'>) => {
144
+ return callSock(data, { ...options, method: 'GET' });
145
+ };
146
+
147
+ export const callSockPost = (data: QueryData, options?: Omit<CallSockOptions, 'method'>) => {
148
+ return callSock(data, { ...options, method: 'POST' });
149
+ };
150
+
151
+ export const autoCall = callSockPost;
@@ -0,0 +1,102 @@
1
+ import { getRuntime } from '../runtime.ts';
2
+
3
+ let isClean = false;
4
+ export const deleteFileDetached = async (path: string, pidPath: string = './app.pid') => {
5
+ const runtime = getRuntime();
6
+ if (runtime.isDeno) {
7
+ // Deno 实现 - 启动后不等待结果
8
+ const process = new Deno.Command('sh', {
9
+ args: ['-c', `rm -f "${path}" & rm -f "${pidPath}"`],
10
+ stdout: 'null',
11
+ stderr: 'null',
12
+ });
13
+ process.spawn(); // 不等待结果
14
+ console.log(`[DEBUG] Fire-and-forget delete initiated for ${path}`);
15
+ return;
16
+ }
17
+ const { spawn } = await import('node:child_process');
18
+ const child = spawn('sh', ['-c', `rm -f "${path}" & rm -f "${pidPath}"`], {
19
+ detached: true,
20
+ stdio: 'ignore',
21
+ });
22
+ child.unref(); // 完全分离
23
+ console.log(`[DEBUG] Fire-and-forget delete initiated for ${path}`);
24
+ };
25
+
26
+ type CleanupOptions = {
27
+ path: string;
28
+ close?: () => Promise<void>;
29
+ pidPath?: string;
30
+ };
31
+ export const cleanup = async ({ path, close = async () => {}, pidPath = './app.pid' }: CleanupOptions) => {
32
+ const runtime = getRuntime();
33
+
34
+ // 检查文件是否存在并删除
35
+ const cleanupFile = async () => {
36
+ if (isClean) return;
37
+ isClean = true;
38
+ if (runtime.isDeno) {
39
+ await deleteFileDetached(path, pidPath);
40
+ }
41
+ await close();
42
+ if (!runtime.isDeno) {
43
+ await deleteFileDetached(path, pidPath);
44
+ }
45
+ };
46
+
47
+ // 根据运行时环境注册不同的退出监听器
48
+ if (runtime.isDeno) {
49
+ // Deno 环境
50
+ const handleSignal = () => {
51
+ cleanupFile();
52
+ Deno.exit(0);
53
+ };
54
+
55
+ try {
56
+ Deno.addSignalListener('SIGINT', handleSignal);
57
+ Deno.addSignalListener('SIGTERM', handleSignal);
58
+ } catch (error) {
59
+ console.warn('[DEBUG] Failed to add signal listeners:', error);
60
+ }
61
+
62
+ // 对于 beforeunload 和 unload,使用异步清理
63
+ const handleUnload = () => {
64
+ cleanupFile();
65
+ };
66
+
67
+ globalThis.addEventListener('beforeunload', handleUnload);
68
+ globalThis.addEventListener('unload', handleUnload);
69
+ } else if (runtime.isNode || runtime.isBun) {
70
+ // Node.js 和 Bun 环境
71
+ import('process').then(({ default: process }) => {
72
+ // 信号处理使用同步清理,然后退出
73
+ const signalHandler = async (signal: string) => {
74
+ await cleanupFile();
75
+ process.exit(0);
76
+ };
77
+
78
+ process.on('SIGINT', () => signalHandler('SIGINT'));
79
+ process.on('SIGTERM', () => signalHandler('SIGTERM'));
80
+ process.on('SIGUSR1', () => signalHandler('SIGUSR1'));
81
+ process.on('SIGUSR2', () => signalHandler('SIGUSR2'));
82
+
83
+ process.on('exit', async () => {
84
+ await cleanupFile();
85
+ });
86
+
87
+ process.on('uncaughtException', async (error) => {
88
+ console.error('Uncaught Exception:', error);
89
+ await cleanupFile();
90
+ process.exit(1);
91
+ });
92
+
93
+ process.on('unhandledRejection', async (reason, promise) => {
94
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
95
+ await cleanupFile();
96
+ });
97
+ });
98
+ }
99
+
100
+ // 返回手动清理函数,以便需要时主动调用
101
+ return cleanupFile;
102
+ };
@@ -0,0 +1,50 @@
1
+ import { getRuntime } from '../runtime.ts';
2
+
3
+ export const getPid = async () => {
4
+ const runtime = getRuntime();
5
+
6
+ let pid = 0;
7
+ if (runtime.isDeno) {
8
+ pid = Deno.pid;
9
+ } else {
10
+ pid = process.pid;
11
+ }
12
+ return pid;
13
+ };
14
+ export const writeAppid = async (pidPath = './app.pid') => {
15
+ const fs = await import('node:fs');
16
+ const pid = await getPid();
17
+ fs.writeFileSync(pidPath, pid + '');
18
+ };
19
+
20
+ export const getPidFromFileAndStop = async () => {
21
+ const fs = await import('node:fs');
22
+ if (fs.existsSync('./app.pid')) {
23
+ const pid = parseInt(fs.readFileSync('./app.pid', 'utf-8'), 10);
24
+ if (!isNaN(pid)) {
25
+ if (pid === 0) {
26
+ return;
27
+ }
28
+ try {
29
+ process.kill(pid);
30
+ console.log(`Stopped process with PID ${pid}`);
31
+ } catch (error) {
32
+ console.error(`Failed to stop process with PID ${pid}:`, error);
33
+ }
34
+ }
35
+ }
36
+ };
37
+
38
+ export const runFirstCheck = async (path: string, pidPath: string) => {
39
+ await getPidFromFileAndStop();
40
+ await writeAppid(pidPath);
41
+ try {
42
+ const fs = await import('node:fs');
43
+ if (fs.existsSync(path)) {
44
+ fs.unlinkSync(path);
45
+ console.log(`Socket file ${path} cleaned up during first check`);
46
+ }
47
+ } catch (error) {
48
+ console.error(`Failed to clean up socket file ${path} during first check:`, error);
49
+ }
50
+ };
@@ -0,0 +1,245 @@
1
+ import type { IncomingMessage } from 'http';
2
+ import { QueryRouterServer } from '../index.ts';
3
+ import { getRuntime } from './runtime.ts';
4
+ import { runFirstCheck } from './listen/run-check.ts';
5
+ import { cleanup } from './listen/cleanup.ts';
6
+
7
+ type ListenSocketOptions = {
8
+ path?: string;
9
+ app?: QueryRouterServer;
10
+ pidPath?: string;
11
+ };
12
+ const server = async (req, app: QueryRouterServer) => {
13
+ const runtime = getRuntime();
14
+ let data;
15
+ if (!runtime.isNode) {
16
+ data = await getRequestParams(req);
17
+ } else {
18
+ data = await parseBody(req);
19
+ }
20
+
21
+ const result = await app.queryRoute(data as any);
22
+ const response = new Response(JSON.stringify(result));
23
+ response.headers.set('Content-Type', 'application/json');
24
+ return response;
25
+ };
26
+
27
+ export const listenSocket = async (options?: ListenSocketOptions) => {
28
+ const path = options?.path || './app.sock';
29
+ const pidPath = options?.pidPath || './app.pid';
30
+ const runtime = getRuntime();
31
+ let app = options?.app || globalThis.context?.app;
32
+ if (!app) {
33
+ app = new QueryRouterServer();
34
+ }
35
+ await runFirstCheck(path, pidPath);
36
+ let close = async () => {};
37
+ cleanup({ path, close });
38
+ if (runtime.isDeno) {
39
+ // 检查 Deno 版本是否支持 Unix domain socket
40
+ try {
41
+ // @ts-ignore
42
+ const listener = Deno.listen({
43
+ transport: 'unix',
44
+ path: path,
45
+ });
46
+
47
+ // 处理连接
48
+ (async () => {
49
+ for await (const conn of listener) {
50
+ (async () => {
51
+ // @ts-ignore
52
+ const httpConn = Deno.serveHttp(conn);
53
+ for await (const requestEvent of httpConn) {
54
+ try {
55
+ const response = await server(requestEvent.request, app);
56
+ await requestEvent.respondWith(response);
57
+ } catch (error) {
58
+ await requestEvent.respondWith(new Response('Internal Server Error', { status: 500 }));
59
+ }
60
+ }
61
+ })();
62
+ }
63
+ })();
64
+ close = async () => {
65
+ listener.close();
66
+ };
67
+ return listener;
68
+ } catch (error) {
69
+ // 如果 Unix socket 不支持,回退到 HTTP 服务器
70
+ console.warn('Unix socket not supported in this Deno environment, falling back to HTTP server');
71
+
72
+ // @ts-ignore
73
+ const listener = Deno.listen({ port: 0 }); // 使用随机端口
74
+
75
+ // @ts-ignore
76
+ console.log(`Deno server listening on port ${listener.addr.port}`);
77
+
78
+ (async () => {
79
+ for await (const conn of listener) {
80
+ (async () => {
81
+ // @ts-ignore
82
+ const httpConn = Deno.serveHttp(conn);
83
+ for await (const requestEvent of httpConn) {
84
+ try {
85
+ const response = await server(requestEvent.request, app);
86
+ await requestEvent.respondWith(response);
87
+ } catch (error) {
88
+ await requestEvent.respondWith(new Response('Internal Server Error', { status: 500 }));
89
+ }
90
+ }
91
+ })();
92
+ }
93
+ })();
94
+
95
+ return listener;
96
+ }
97
+ }
98
+
99
+ if (runtime.isBun) {
100
+ const bunServer = Bun.serve({
101
+ unix: path,
102
+ fetch(req) {
103
+ return server(req, app);
104
+ },
105
+ });
106
+ close = async () => {
107
+ await bunServer.stop();
108
+ };
109
+ return bunServer;
110
+ }
111
+
112
+ // Node.js 环境
113
+ const http = await import('http');
114
+
115
+ const httpServer = http.createServer(async (req, res) => {
116
+ try {
117
+ const response = await server(req, app);
118
+
119
+ // 设置响应头
120
+ response.headers.forEach((value, key) => {
121
+ res.setHeader(key, value);
122
+ });
123
+
124
+ // 设置状态码
125
+ res.statusCode = response.status;
126
+
127
+ // 读取响应体并写入
128
+ const body = await response.text();
129
+ res.end(body);
130
+ } catch (error) {
131
+ console.error('Error handling request:', error);
132
+ res.statusCode = 500;
133
+ res.end('Internal Server Error');
134
+ }
135
+ });
136
+
137
+ httpServer.listen(path);
138
+ close = async () => {
139
+ httpServer.close();
140
+ };
141
+ return httpServer;
142
+ };
143
+
144
+ export const getRequestParams = async (req: Request) => {
145
+ let urlParams: Record<string, any> = {};
146
+ let bodyParams: Record<string, any> = {};
147
+
148
+ // 获取URL参数
149
+ const url = new URL(req.url);
150
+ for (const [key, value] of url.searchParams.entries()) {
151
+ // 尝试解析JSON payload
152
+ if (key === 'payload') {
153
+ try {
154
+ urlParams[key] = JSON.parse(value);
155
+ } catch {
156
+ urlParams[key] = value;
157
+ }
158
+ } else {
159
+ urlParams[key] = value;
160
+ }
161
+ }
162
+
163
+ // 获取body参数
164
+ if (req.method.toLowerCase() === 'post' && req.body) {
165
+ const contentType = req.headers.get('content-type') || '';
166
+ if (contentType.includes('application/json')) {
167
+ try {
168
+ bodyParams = await req.json();
169
+ } catch {
170
+ // 如果解析失败,保持空对象
171
+ }
172
+ } else if (contentType.includes('application/x-www-form-urlencoded')) {
173
+ const formData = await req.text();
174
+ const params = new URLSearchParams(formData);
175
+ for (const [key, value] of params.entries()) {
176
+ bodyParams[key] = value;
177
+ }
178
+ } else if (contentType.includes('multipart/form-data')) {
179
+ try {
180
+ const formData = await req.formData();
181
+ for (const [key, value] of formData.entries()) {
182
+ // @ts-ignore
183
+ bodyParams[key] = value instanceof File ? value : value.toString();
184
+ }
185
+ } catch {
186
+ // 如果解析失败,保持空对象
187
+ }
188
+ }
189
+ }
190
+
191
+ // body参数优先,合并数据
192
+ return {
193
+ ...urlParams,
194
+ ...bodyParams,
195
+ };
196
+ };
197
+
198
+ export const parseBody = async <T = Record<string, any>>(req: IncomingMessage) => {
199
+ return new Promise<T>((resolve, reject) => {
200
+ const arr: any[] = [];
201
+ req.on('data', (chunk) => {
202
+ arr.push(chunk);
203
+ });
204
+ req.on('end', () => {
205
+ try {
206
+ const body = Buffer.concat(arr).toString();
207
+
208
+ // 获取 Content-Type 头信息
209
+ const contentType = req.headers['content-type'] || '';
210
+
211
+ // 处理 application/json
212
+ if (contentType.includes('application/json')) {
213
+ resolve(JSON.parse(body) as T);
214
+ return;
215
+ }
216
+ // 处理 application/x-www-form-urlencoded
217
+ if (contentType.includes('application/x-www-form-urlencoded')) {
218
+ const formData = new URLSearchParams(body);
219
+ const result: Record<string, any> = {};
220
+
221
+ formData.forEach((value, key) => {
222
+ // 尝试将值解析为 JSON,如果失败则保留原始字符串
223
+ try {
224
+ result[key] = JSON.parse(value);
225
+ } catch {
226
+ result[key] = value;
227
+ }
228
+ });
229
+
230
+ resolve(result as T);
231
+ return;
232
+ }
233
+
234
+ // 默认尝试 JSON 解析
235
+ try {
236
+ resolve(JSON.parse(body) as T);
237
+ } catch {
238
+ resolve({} as T);
239
+ }
240
+ } catch (e) {
241
+ resolve({} as T);
242
+ }
243
+ });
244
+ });
245
+ };
@@ -0,0 +1,37 @@
1
+ import { getRuntime } from './runtime.ts';
2
+ import { glob } from './utils/glob.ts';
3
+ type GlobOptions = {
4
+ cwd?: string;
5
+ };
6
+
7
+ export const getMatchFiles = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}): Promise<string[]> => {
8
+ const runtime = getRuntime();
9
+ if (runtime.isNode) {
10
+ console.error(`Node.js is not supported`);
11
+ return [];
12
+ }
13
+ if (runtime.isDeno) {
14
+ // Deno 环境下
15
+ return await glob(match);
16
+ }
17
+ if (runtime.isBun) {
18
+ // Bun 环境下
19
+ // @ts-ignore
20
+ const { Glob } = await import('bun');
21
+ const path = await import('path');
22
+ // @ts-ignore
23
+ const glob = new Glob(match, { cwd, absolute: true, onlyFiles: true });
24
+ const files: string[] = [];
25
+ for await (const file of glob.scan('.')) {
26
+ files.push(path.join(cwd, file));
27
+ }
28
+ // @ts-ignore
29
+ return Array.from(files);
30
+ }
31
+ return [];
32
+ };
33
+
34
+ export const loadTS = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}): Promise<any[]> => {
35
+ const files = await getMatchFiles(match, { cwd });
36
+ return Promise.all(files.map((file) => import(file)));
37
+ };
@@ -0,0 +1,19 @@
1
+ type RuntimeEngine = 'node' | 'deno' | 'bun';
2
+
3
+ type Runtime = {
4
+ isNode?: boolean;
5
+ isDeno?: boolean;
6
+ isBun?: boolean;
7
+ engine: RuntimeEngine;
8
+ };
9
+ export const getRuntime = (): Runtime => {
10
+ // @ts-ignore
11
+ if (typeof Deno !== 'undefined') {
12
+ return { isDeno: true, engine: 'deno' };
13
+ }
14
+ // @ts-ignore
15
+ if (typeof Bun !== 'undefined') {
16
+ return { isBun: true, engine: 'bun' };
17
+ }
18
+ return { isNode: true, engine: 'node' };
19
+ };
@@ -0,0 +1,83 @@
1
+ type GlobOptions = {
2
+ cwd?: string;
3
+ };
4
+
5
+ export const glob = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}) => {
6
+ const fs = await import('node:fs');
7
+ const path = await import('node:path');
8
+
9
+ // 将 glob 模式转换为正则表达式
10
+ const globToRegex = (pattern: string): RegExp => {
11
+ const escaped = pattern
12
+ .replace(/\./g, '\\.')
13
+ .replace(/\*\*/g, '__DOUBLE_STAR__') // 临时替换 **
14
+ .replace(/\*/g, '[^/]*') // * 匹配除 / 外的任意字符
15
+ .replace(/__DOUBLE_STAR__/g, '.*') // ** 匹配任意字符包括 /
16
+ .replace(/\?/g, '[^/]'); // ? 匹配除 / 外的单个字符
17
+ return new RegExp(`^${escaped}$`);
18
+ };
19
+
20
+ // 递归读取目录
21
+ const readDirRecursive = async (dir: string): Promise<string[]> => {
22
+ const files: string[] = [];
23
+
24
+ try {
25
+ const entries = await fs.promises.readdir(dir, { withFileTypes: true });
26
+
27
+ for (const entry of entries) {
28
+ const fullPath = path.join(dir, entry.name);
29
+
30
+ if (entry.isFile()) {
31
+ files.push(fullPath);
32
+ } else if (entry.isDirectory()) {
33
+ // 递归搜索子目录
34
+ const subFiles = await readDirRecursive(fullPath);
35
+ files.push(...subFiles);
36
+ }
37
+ }
38
+ } catch (error) {
39
+ // 忽略无法访问的目录
40
+ }
41
+
42
+ return files;
43
+ };
44
+
45
+ // 解析模式是否包含递归搜索
46
+ const hasRecursive = match.includes('**');
47
+
48
+ try {
49
+ let allFiles: string[] = [];
50
+
51
+ if (hasRecursive) {
52
+ // 处理递归模式
53
+ const basePath = match.split('**')[0];
54
+ const startDir = path.resolve(cwd, basePath || '.');
55
+ allFiles = await readDirRecursive(startDir);
56
+ } else {
57
+ // 处理非递归模式
58
+ const dir = path.resolve(cwd, path.dirname(match));
59
+ const entries = await fs.promises.readdir(dir, { withFileTypes: true });
60
+
61
+ for (const entry of entries) {
62
+ if (entry.isFile()) {
63
+ allFiles.push(path.join(dir, entry.name));
64
+ }
65
+ }
66
+ }
67
+
68
+ // 创建相对于 cwd 的匹配模式
69
+ const normalizedMatch = path.resolve(cwd, match);
70
+ const regex = globToRegex(normalizedMatch);
71
+
72
+ // 过滤匹配的文件
73
+ const matchedFiles = allFiles.filter(file => {
74
+ const normalizedFile = path.resolve(file);
75
+ return regex.test(normalizedFile);
76
+ });
77
+
78
+ return matchedFiles;
79
+ } catch (error) {
80
+ console.error(`Error in glob pattern "${match}":`, error);
81
+ return [];
82
+ }
83
+ };
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { Route, QueryRouter, QueryRouterServer } from './route.ts';
2
- export { Connect, QueryConnect } from './connect.ts';
3
2
 
4
3
  export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
5
4
 
@@ -11,8 +10,9 @@ export { Server, handleServer } from './server/index.ts';
11
10
  */
12
11
  export { CustomError } from './result/error.ts';
13
12
 
14
- export { Rule, Schema, createSchema } from './validator/index.ts';
15
-
13
+ export { createSchema } from './validator/index.ts';
14
+ export type { Rule } from './validator/rule.ts';
15
+ export type { Schema } from 'zod';
16
16
  export { App } from './app.ts';
17
17
 
18
18
  export * from './router-define.ts';
@@ -1,2 +1,3 @@
1
- export type { Rule, Schema } from './rule.ts';
2
1
  export { schemaFormRule, createSchema, createSchemaList } from './rule.ts';
2
+
3
+ export type { Rule, Schema } from './rule.ts';
package/src/connect.ts DELETED
@@ -1,67 +0,0 @@
1
- import { nanoid } from 'nanoid';
2
- import { RouteContext } from './route.ts';
3
-
4
- export class Connect {
5
- path: string;
6
- key?: string;
7
- _fn?: (ctx?: RouteContext) => Promise<RouteContext>;
8
- description?: string;
9
- connects: { path: string; key?: string }[];
10
- share = false;
11
-
12
- constructor(path: string) {
13
- this.path = path;
14
- this.key = nanoid();
15
- }
16
- use(path: string) {
17
- this.connects.push({ path });
18
- }
19
- useList(paths: string[]) {
20
- paths.forEach((path) => {
21
- this.connects.push({ path });
22
- });
23
- }
24
- useConnect(connect: Connect) {
25
- this.connects.push({ path: connect.path, key: connect.key });
26
- }
27
- useConnectList(connects: Connect[]) {
28
- connects.forEach((connect) => {
29
- this.connects.push({ path: connect.path, key: connect.key });
30
- });
31
- }
32
- getPathList() {
33
- return this.connects.map((c) => c.path).filter(Boolean);
34
- }
35
- set fn(fn: (ctx?: RouteContext) => Promise<RouteContext>) {
36
- this._fn = fn;
37
- }
38
- get fn() {
39
- return this._fn;
40
- }
41
- }
42
- export class QueryConnect {
43
- connects: Connect[];
44
- constructor() {
45
- this.connects = [];
46
- }
47
- add(connect: Connect) {
48
- const has = this.connects.find((c) => c.path === connect.path && c.key === connect.key);
49
- if (has) {
50
- // remove the old connect
51
- console.log('[replace connect]:', connect.path, connect.key);
52
- this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
53
- }
54
- this.connects.push(connect);
55
- }
56
- remove(connect: Connect) {
57
- this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
58
- }
59
- getList() {
60
- return this.connects.map((c) => {
61
- return {
62
- path: c.path,
63
- key: c.key,
64
- };
65
- });
66
- }
67
- }