@kevisual/router 0.0.26 → 0.0.28
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-browser.d.ts +2 -2
- package/dist/router-browser.js +1381 -113
- package/dist/router-sign.d.ts +16 -0
- package/dist/router-sign.js +28698 -0
- package/dist/router-simple-lib.d.ts +3 -0
- package/dist/router-simple-lib.js +35 -0
- package/dist/router-simple.js +153 -147
- package/dist/router.d.ts +39 -7
- package/dist/router.js +1556 -222
- package/package.json +26 -27
- package/src/app.ts +1 -1
- package/src/connect.ts +67 -0
- package/src/index.ts +3 -3
- package/src/io.ts +6 -0
- package/src/route.ts +10 -5
- package/src/server/ws-server.ts +4 -3
- package/src/sign.ts +1 -1
- package/src/test/ws.ts +25 -0
- package/src/utils/parse.ts +4 -3
- package/auto.ts +0 -20
- package/dist/auto.d.ts +0 -472
- package/dist/auto.js +0 -4789
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.
|
|
4
|
+
"version": "0.0.28",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -10,15 +10,12 @@
|
|
|
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"
|
|
14
|
-
"auto:bun": "bun test/auto/app.ts",
|
|
15
|
-
"auto:deno": "bun test/auto/app.ts"
|
|
13
|
+
"clean": "rm -rf dist"
|
|
16
14
|
},
|
|
17
15
|
"files": [
|
|
18
16
|
"dist",
|
|
19
17
|
"src",
|
|
20
|
-
"mod.ts"
|
|
21
|
-
"auto.ts"
|
|
18
|
+
"mod.ts"
|
|
22
19
|
],
|
|
23
20
|
"keywords": [],
|
|
24
21
|
"author": "abearxiong",
|
|
@@ -26,33 +23,34 @@
|
|
|
26
23
|
"devDependencies": {
|
|
27
24
|
"@kevisual/local-proxy": "^0.0.6",
|
|
28
25
|
"@kevisual/query": "^0.0.29",
|
|
29
|
-
"@kevisual/use-config": "^1.0.19",
|
|
30
26
|
"@rollup/plugin-alias": "^5.1.1",
|
|
31
27
|
"@rollup/plugin-commonjs": "^28.0.6",
|
|
32
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
28
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
33
29
|
"@rollup/plugin-typescript": "^12.1.4",
|
|
34
|
-
"@types/
|
|
35
|
-
"@types/node": "^24.2
|
|
36
|
-
"@types/send": "^
|
|
30
|
+
"@types/lodash-es": "^4.17.12",
|
|
31
|
+
"@types/node": "^24.7.2",
|
|
32
|
+
"@types/send": "^1.2.0",
|
|
37
33
|
"@types/ws": "^8.18.1",
|
|
38
34
|
"@types/xml2js": "^0.4.14",
|
|
39
35
|
"cookie": "^1.0.2",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"rollup": "^
|
|
44
|
-
"
|
|
45
|
-
"
|
|
36
|
+
"lodash-es": "^4.17.21",
|
|
37
|
+
"nanoid": "^5.1.6",
|
|
38
|
+
"rollup": "^4.52.4",
|
|
39
|
+
"rollup-plugin-dts": "^6.2.3",
|
|
40
|
+
"ts-loader": "^9.5.4",
|
|
41
|
+
"ts-node": "^10.9.2",
|
|
42
|
+
"tslib": "^2.8.1",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
46
44
|
"ws": "npm:@kevisual/ws",
|
|
47
45
|
"xml2js": "^0.6.2",
|
|
48
|
-
"zod": "^4.
|
|
46
|
+
"zod": "^4.1.12"
|
|
49
47
|
},
|
|
50
48
|
"repository": {
|
|
51
49
|
"type": "git",
|
|
52
50
|
"url": "git+https://github.com/abearxiong/kevisual-router.git"
|
|
53
51
|
},
|
|
54
52
|
"dependencies": {
|
|
55
|
-
"path-to-regexp": "^8.
|
|
53
|
+
"path-to-regexp": "^8.3.0",
|
|
56
54
|
"selfsigned": "^3.0.1",
|
|
57
55
|
"send": "^1.2.0"
|
|
58
56
|
},
|
|
@@ -70,6 +68,11 @@
|
|
|
70
68
|
"require": "./dist/router-browser.js",
|
|
71
69
|
"types": "./dist/router-browser.d.ts"
|
|
72
70
|
},
|
|
71
|
+
"./sign": {
|
|
72
|
+
"import": "./dist/router-sign.js",
|
|
73
|
+
"require": "./dist/router-sign.js",
|
|
74
|
+
"types": "./dist/router-sign.d.ts"
|
|
75
|
+
},
|
|
73
76
|
"./simple": {
|
|
74
77
|
"import": "./dist/router-simple.js",
|
|
75
78
|
"require": "./dist/router-simple.js",
|
|
@@ -80,20 +83,16 @@
|
|
|
80
83
|
"require": "./dist/router-define.js",
|
|
81
84
|
"types": "./dist/router-define.d.ts"
|
|
82
85
|
},
|
|
83
|
-
"./
|
|
84
|
-
"import": "./dist/
|
|
85
|
-
"
|
|
86
|
+
"./simple-lib": {
|
|
87
|
+
"import": "./dist/router-simple-lib.js",
|
|
88
|
+
"require": "./dist/router-simple-lib.js",
|
|
89
|
+
"types": "./dist/router-simple-lib.d.ts"
|
|
86
90
|
},
|
|
87
91
|
"./mod.ts": {
|
|
88
92
|
"import": "./mod.ts",
|
|
89
93
|
"require": "./mod.ts",
|
|
90
94
|
"types": "./mod.d.ts"
|
|
91
95
|
},
|
|
92
|
-
"./auto.ts": {
|
|
93
|
-
"import": "./auto.ts",
|
|
94
|
-
"require": "./auto.ts",
|
|
95
|
-
"types": "./auto.ts"
|
|
96
|
-
},
|
|
97
96
|
"./src/*": {
|
|
98
97
|
"import": "./src/*",
|
|
99
98
|
"require": "./src/*"
|
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 '
|
|
6
|
+
import { IncomingMessage, ServerResponse } from '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 = {}> = {
|
package/src/connect.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { Route, QueryRouter, QueryRouterServer } from './route.ts';
|
|
2
|
+
export { Connect, QueryConnect } from './connect.ts';
|
|
2
3
|
|
|
3
4
|
export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
|
|
4
5
|
|
|
@@ -10,9 +11,8 @@ export { Server, handleServer } from './server/index.ts';
|
|
|
10
11
|
*/
|
|
11
12
|
export { CustomError } from './result/error.ts';
|
|
12
13
|
|
|
13
|
-
export { createSchema } from './validator/index.ts';
|
|
14
|
-
|
|
15
|
-
export type { Schema } from 'zod';
|
|
14
|
+
export { Rule, Schema, createSchema } from './validator/index.ts';
|
|
15
|
+
|
|
16
16
|
export { App } from './app.ts';
|
|
17
17
|
|
|
18
18
|
export * from './router-define.ts';
|
package/src/io.ts
ADDED
package/src/route.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { nanoid } from 'nanoid';
|
|
|
2
2
|
import { CustomError } from './result/error.ts';
|
|
3
3
|
import { Schema, Rule, createSchema } from './validator/index.ts';
|
|
4
4
|
import { pick } from './utils/pick.ts';
|
|
5
|
+
import { get } from 'lodash-es';
|
|
5
6
|
|
|
6
7
|
export type RouterContextT = { code?: number; [key: string]: any };
|
|
7
8
|
export type RouteContext<T = { code?: number }, S = any> = {
|
|
@@ -86,7 +87,7 @@ export type RouteOpts = {
|
|
|
86
87
|
* }
|
|
87
88
|
*/
|
|
88
89
|
validator?: { [key: string]: Rule };
|
|
89
|
-
schema?: { [key: string]: any };
|
|
90
|
+
schema?: { [key: string]: Schema<any> };
|
|
90
91
|
isVerify?: boolean;
|
|
91
92
|
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
|
92
93
|
verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
|
|
@@ -121,7 +122,7 @@ export class Route<U = { [key: string]: any }> {
|
|
|
121
122
|
middleware?: RouteMiddleware[]; // middleware
|
|
122
123
|
type? = 'route';
|
|
123
124
|
private _validator?: { [key: string]: Rule };
|
|
124
|
-
schema?: { [key: string]: any };
|
|
125
|
+
schema?: { [key: string]: Schema<any> };
|
|
125
126
|
data?: any;
|
|
126
127
|
/**
|
|
127
128
|
* 是否需要验证
|
|
@@ -205,8 +206,12 @@ export class Route<U = { [key: string]: any }> {
|
|
|
205
206
|
if (schema[key]) {
|
|
206
207
|
const result = schema[key].safeParse(value);
|
|
207
208
|
if (!result.success) {
|
|
208
|
-
|
|
209
|
-
|
|
209
|
+
const path = result.error.errors[0]?.path?.join?.('.properties.');
|
|
210
|
+
let message = 'Invalid params';
|
|
211
|
+
if (path) {
|
|
212
|
+
const keyS = `${key}.properties.${path}.message`;
|
|
213
|
+
message = get(validator, keyS, 'Invalid params') as any;
|
|
214
|
+
}
|
|
210
215
|
throw new CustomError(500, message);
|
|
211
216
|
}
|
|
212
217
|
}
|
|
@@ -603,7 +608,7 @@ export class QueryRouter {
|
|
|
603
608
|
* @description 这里的上下文是为了在handle函数中使用
|
|
604
609
|
* @param ctx
|
|
605
610
|
*/
|
|
606
|
-
|
|
611
|
+
setContext(ctx: RouteContext) {
|
|
607
612
|
this.context = ctx;
|
|
608
613
|
}
|
|
609
614
|
getList(): RouteInfo[] {
|
package/src/server/ws-server.ts
CHANGED
|
@@ -43,10 +43,11 @@ export class WsServerBase {
|
|
|
43
43
|
this.listening = true;
|
|
44
44
|
|
|
45
45
|
this.wss.on('connection', (ws) => {
|
|
46
|
-
ws.on('message', async (message: string) => {
|
|
46
|
+
ws.on('message', async (message: string | Buffer) => {
|
|
47
47
|
const data = parseIfJson(message);
|
|
48
48
|
if (typeof data === 'string') {
|
|
49
|
-
|
|
49
|
+
const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
|
|
50
|
+
ws.emit('string', cleanMessage);
|
|
50
51
|
return;
|
|
51
52
|
}
|
|
52
53
|
const { type, data: typeData, ...rest } = data;
|
|
@@ -83,7 +84,7 @@ export class WsServerBase {
|
|
|
83
84
|
if (message === 'close') {
|
|
84
85
|
ws.close();
|
|
85
86
|
}
|
|
86
|
-
if (message
|
|
87
|
+
if (message == 'ping') {
|
|
87
88
|
ws.send('pong');
|
|
88
89
|
}
|
|
89
90
|
});
|
package/src/sign.ts
CHANGED
package/src/test/ws.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { App } from "../app.ts";
|
|
2
|
+
|
|
3
|
+
const app = new App({
|
|
4
|
+
io: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
app
|
|
8
|
+
.route('demo', '03')
|
|
9
|
+
.define(async (ctx) => {
|
|
10
|
+
ctx.body = '03';
|
|
11
|
+
return ctx;
|
|
12
|
+
})
|
|
13
|
+
.addTo(app);
|
|
14
|
+
app
|
|
15
|
+
.route('test', 'test')
|
|
16
|
+
.define(async (ctx) => {
|
|
17
|
+
ctx.body = 'test';
|
|
18
|
+
return ctx;
|
|
19
|
+
})
|
|
20
|
+
.addTo(app);
|
|
21
|
+
console.log(`http://localhost:4002/api/router?path=demo&key=03`);
|
|
22
|
+
|
|
23
|
+
app.listen(4002, () => {
|
|
24
|
+
console.log("Server started on http://localhost:4002");
|
|
25
|
+
});
|
package/src/utils/parse.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export const parseIfJson = (input: string): { [key: string]: any } | string => {
|
|
1
|
+
export const parseIfJson = (input: string|Buffer): { [key: string]: any } | string => {
|
|
2
|
+
const str = typeof input === 'string' ? input : input.toString();
|
|
2
3
|
try {
|
|
3
4
|
// 尝试解析 JSON
|
|
4
|
-
const parsed = JSON.parse(
|
|
5
|
+
const parsed = JSON.parse(str);
|
|
5
6
|
// 检查解析结果是否为对象(数组或普通对象)
|
|
6
7
|
if (typeof parsed === 'object' && parsed !== null) {
|
|
7
8
|
return parsed;
|
|
@@ -9,5 +10,5 @@ export const parseIfJson = (input: string): { [key: string]: any } | string => {
|
|
|
9
10
|
} catch (e) {
|
|
10
11
|
// 如果解析失败,直接返回原始字符串
|
|
11
12
|
}
|
|
12
|
-
return
|
|
13
|
+
return str;
|
|
13
14
|
};
|
package/auto.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { loadTS, getMatchFiles } from './src/auto/load-ts.ts';
|
|
2
|
-
import { listenSocket } from './src/auto/listen-sock.ts';
|
|
3
|
-
import { Route, QueryRouter, QueryRouterServer } from './src/route.ts';
|
|
4
|
-
|
|
5
|
-
export { Route, QueryRouter, QueryRouterServer };
|
|
6
|
-
|
|
7
|
-
export const App = QueryRouterServer;
|
|
8
|
-
|
|
9
|
-
export { createSchema } from './src/validator/index.ts';
|
|
10
|
-
export type { Rule } from './src/validator/rule.ts';
|
|
11
|
-
export type { Schema } from 'zod';
|
|
12
|
-
export type { RouteContext, RouteOpts } from './src/route.ts';
|
|
13
|
-
|
|
14
|
-
export type { Run } from './src/route.ts';
|
|
15
|
-
|
|
16
|
-
export { CustomError } from './src/result/error.ts';
|
|
17
|
-
|
|
18
|
-
export { listenSocket, loadTS, getMatchFiles };
|
|
19
|
-
|
|
20
|
-
export { autoCall } from './src/auto/call-sock.ts';
|