This commit is contained in:
2025-10-14 15:16:41 +08:00
parent e8794912b6
commit 27b63e0a2b
6 changed files with 89 additions and 40 deletions

View File

@@ -9,41 +9,49 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@abearxiong/router": "../.." "@kevisual/router": "../.."
}, },
"devDependencies": { "devDependencies": {
"cookie": "^1.0.2",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"typescript": "^5.5.4" "typescript": "^5.5.4"
} }
}, },
"../..": { "../..": {
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.2", "version": "0.0.23",
"license": "ISC", "license": "MIT",
"dependencies": { "dependencies": {
"ws": "^8.18.0" "path-to-regexp": "^8.2.0",
"selfsigned": "^2.4.1",
"send": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1", "@kevisual/local-proxy": "^0.0.3",
"@rollup/plugin-node-resolve": "^15.2.4", "@kevisual/query": "^0.0.29",
"@rollup/plugin-typescript": "^12.1.0", "@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.3",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^22.5.5", "@types/node": "^24.0.3",
"@types/ws": "^8.5.12", "@types/send": "^0.17.5",
"@types/ws": "^8.18.1",
"@types/xml2js": "^0.4.14",
"cookie": "^1.0.2",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"nanoid": "^5.0.7", "nanoid": "^5.1.5",
"rollup": "^4.22.4", "rollup": "^4.44.0",
"ts-loader": "^9.5.1", "rollup-plugin-dts": "^6.2.1",
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tslib": "^2.7.0", "tslib": "^2.8.1",
"typescript": "^5.6.2", "typescript": "^5.8.3",
"zod": "^3.23.8" "ws": "npm:@kevisual/ws",
"xml2js": "^0.6.2",
"zod": "^3.25.67"
} }
}, },
"node_modules/@abearxiong/router": {
"resolved": "../..",
"link": true
},
"node_modules/@cspotcode/source-map-support": { "node_modules/@cspotcode/source-map-support": {
"version": "0.8.1", "version": "0.8.1",
"resolved": "https://registry.npmmirror.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", "resolved": "https://registry.npmmirror.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
@@ -85,6 +93,10 @@
"@jridgewell/sourcemap-codec": "^1.4.10" "@jridgewell/sourcemap-codec": "^1.4.10"
} }
}, },
"node_modules/@kevisual/router": {
"resolved": "../..",
"link": true
},
"node_modules/@tsconfig/node10": { "node_modules/@tsconfig/node10": {
"version": "1.0.11", "version": "1.0.11",
"resolved": "https://registry.npmmirror.com/@tsconfig/node10/-/node10-1.0.11.tgz", "resolved": "https://registry.npmmirror.com/@tsconfig/node10/-/node10-1.0.11.tgz",
@@ -157,6 +169,16 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/cookie": {
"version": "1.0.2",
"resolved": "https://registry.npmmirror.com/cookie/-/cookie-1.0.2.tgz",
"integrity": "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=18"
}
},
"node_modules/create-require": { "node_modules/create-require": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz", "resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz",

View File

@@ -11,7 +11,7 @@
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"dependencies": { "dependencies": {
"@kevisual/router": "link:../.." "@kevisual/router": "../.."
}, },
"devDependencies": { "devDependencies": {
"cookie": "^1.0.2", "cookie": "^1.0.2",

View File

@@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.23", "version": "0.0.24",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "main": "./dist/router.js",
@@ -21,37 +21,37 @@
"author": "abearxiong", "author": "abearxiong",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@kevisual/local-proxy": "^0.0.3", "@kevisual/local-proxy": "^0.0.6",
"@kevisual/query": "^0.0.29", "@kevisual/query": "^0.0.29",
"@rollup/plugin-alias": "^5.1.1", "@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.6", "@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1", "@rollup/plugin-node-resolve": "^16.0.3",
"@rollup/plugin-typescript": "^12.1.3", "@rollup/plugin-typescript": "^12.1.4",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^24.0.3", "@types/node": "^24.7.2",
"@types/send": "^0.17.5", "@types/send": "^1.2.0",
"@types/ws": "^8.18.1", "@types/ws": "^8.18.1",
"@types/xml2js": "^0.4.14", "@types/xml2js": "^0.4.14",
"cookie": "^1.0.2", "cookie": "^1.0.2",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"nanoid": "^5.1.5", "nanoid": "^5.1.6",
"rollup": "^4.44.0", "rollup": "^4.52.4",
"rollup-plugin-dts": "^6.2.1", "rollup-plugin-dts": "^6.2.3",
"ts-loader": "^9.5.2", "ts-loader": "^9.5.4",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typescript": "^5.8.3", "typescript": "^5.9.3",
"ws": "npm:@kevisual/ws", "ws": "npm:@kevisual/ws",
"xml2js": "^0.6.2", "xml2js": "^0.6.2",
"zod": "^3.25.67" "zod": "^4.1.12"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/abearxiong/kevisual-router.git" "url": "git+https://github.com/abearxiong/kevisual-router.git"
}, },
"dependencies": { "dependencies": {
"path-to-regexp": "^8.2.0", "path-to-regexp": "^8.3.0",
"selfsigned": "^2.4.1", "selfsigned": "^3.0.1",
"send": "^1.2.0" "send": "^1.2.0"
}, },
"publishConfig": { "publishConfig": {

View File

@@ -43,10 +43,11 @@ export class WsServerBase {
this.listening = true; this.listening = true;
this.wss.on('connection', (ws) => { this.wss.on('connection', (ws) => {
ws.on('message', async (message: string) => { ws.on('message', async (message: string | Buffer) => {
const data = parseIfJson(message); const data = parseIfJson(message);
if (typeof data === 'string') { if (typeof data === 'string') {
ws.emit('string', data); const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
ws.emit('string', cleanMessage);
return; return;
} }
const { type, data: typeData, ...rest } = data; const { type, data: typeData, ...rest } = data;
@@ -83,7 +84,7 @@ export class WsServerBase {
if (message === 'close') { if (message === 'close') {
ws.close(); ws.close();
} }
if (message === 'ping') { if (message == 'ping') {
ws.send('pong'); ws.send('pong');
} }
}); });

25
src/test/ws.ts Normal file
View File

@@ -0,0 +1,25 @@
import { App } from "../app.ts";
const app = new App({
io: true
});
app
.route('demo', '03')
.define(async (ctx) => {
ctx.body = '03';
return ctx;
})
.addTo(app);
app
.route('test', 'test')
.define(async (ctx) => {
ctx.body = 'test';
return ctx;
})
.addTo(app);
console.log(`http://localhost:4002/api/router?path=demo&key=03`);
app.listen(4002, () => {
console.log("Server started on http://localhost:4002");
});

View File

@@ -1,7 +1,8 @@
export const parseIfJson = (input: string): { [key: string]: any } | string => { export const parseIfJson = (input: string|Buffer): { [key: string]: any } | string => {
const str = typeof input === 'string' ? input : input.toString();
try { try {
// 尝试解析 JSON // 尝试解析 JSON
const parsed = JSON.parse(input); const parsed = JSON.parse(str);
// 检查解析结果是否为对象(数组或普通对象) // 检查解析结果是否为对象(数组或普通对象)
if (typeof parsed === 'object' && parsed !== null) { if (typeof parsed === 'object' && parsed !== null) {
return parsed; return parsed;
@@ -9,5 +10,5 @@ export const parseIfJson = (input: string): { [key: string]: any } | string => {
} catch (e) { } catch (e) {
// 如果解析失败,直接返回原始字符串 // 如果解析失败,直接返回原始字符串
} }
return input; return str;
}; };