diff --git a/demo/simple/package-lock.json b/demo/simple/package-lock.json index 0e4853b..948838c 100644 --- a/demo/simple/package-lock.json +++ b/demo/simple/package-lock.json @@ -9,41 +9,49 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@abearxiong/router": "../.." + "@kevisual/router": "../.." }, "devDependencies": { + "cookie": "^1.0.2", "ts-node": "^10.9.2", "typescript": "^5.5.4" } }, "../..": { "name": "@kevisual/router", - "version": "0.0.2", - "license": "ISC", + "version": "0.0.23", + "license": "MIT", "dependencies": { - "ws": "^8.18.0" + "path-to-regexp": "^8.2.0", + "selfsigned": "^2.4.1", + "send": "^1.2.0" }, "devDependencies": { - "@rollup/plugin-commonjs": "^26.0.1", - "@rollup/plugin-node-resolve": "^15.2.4", - "@rollup/plugin-typescript": "^12.1.0", + "@kevisual/local-proxy": "^0.0.3", + "@kevisual/query": "^0.0.29", + "@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/node": "^22.5.5", - "@types/ws": "^8.5.12", + "@types/node": "^24.0.3", + "@types/send": "^0.17.5", + "@types/ws": "^8.18.1", + "@types/xml2js": "^0.4.14", + "cookie": "^1.0.2", "lodash-es": "^4.17.21", - "nanoid": "^5.0.7", - "rollup": "^4.22.4", - "ts-loader": "^9.5.1", + "nanoid": "^5.1.5", + "rollup": "^4.44.0", + "rollup-plugin-dts": "^6.2.1", + "ts-loader": "^9.5.2", "ts-node": "^10.9.2", - "tslib": "^2.7.0", - "typescript": "^5.6.2", - "zod": "^3.23.8" + "tslib": "^2.8.1", + "typescript": "^5.8.3", + "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": { "version": "0.8.1", "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" } }, + "node_modules/@kevisual/router": { + "resolved": "../..", + "link": true + }, "node_modules/@tsconfig/node10": { "version": "1.0.11", "resolved": "https://registry.npmmirror.com/@tsconfig/node10/-/node10-1.0.11.tgz", @@ -157,6 +169,16 @@ "dev": true, "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": { "version": "1.1.1", "resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz", diff --git a/demo/simple/package.json b/demo/simple/package.json index 4d586c0..c52f83b 100644 --- a/demo/simple/package.json +++ b/demo/simple/package.json @@ -11,7 +11,7 @@ "license": "ISC", "description": "", "dependencies": { - "@kevisual/router": "link:../.." + "@kevisual/router": "../.." }, "devDependencies": { "cookie": "^1.0.2", diff --git a/package.json b/package.json index 4d2eee0..b946571 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.0.23", + "version": "0.0.24", "description": "", "type": "module", "main": "./dist/router.js", @@ -21,37 +21,37 @@ "author": "abearxiong", "license": "MIT", "devDependencies": { - "@kevisual/local-proxy": "^0.0.3", + "@kevisual/local-proxy": "^0.0.6", "@kevisual/query": "^0.0.29", "@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", + "@rollup/plugin-node-resolve": "^16.0.3", + "@rollup/plugin-typescript": "^12.1.4", "@types/lodash-es": "^4.17.12", - "@types/node": "^24.0.3", - "@types/send": "^0.17.5", + "@types/node": "^24.7.2", + "@types/send": "^1.2.0", "@types/ws": "^8.18.1", "@types/xml2js": "^0.4.14", "cookie": "^1.0.2", "lodash-es": "^4.17.21", - "nanoid": "^5.1.5", - "rollup": "^4.44.0", - "rollup-plugin-dts": "^6.2.1", - "ts-loader": "^9.5.2", + "nanoid": "^5.1.6", + "rollup": "^4.52.4", + "rollup-plugin-dts": "^6.2.3", + "ts-loader": "^9.5.4", "ts-node": "^10.9.2", "tslib": "^2.8.1", - "typescript": "^5.8.3", + "typescript": "^5.9.3", "ws": "npm:@kevisual/ws", "xml2js": "^0.6.2", - "zod": "^3.25.67" + "zod": "^4.1.12" }, "repository": { "type": "git", "url": "git+https://github.com/abearxiong/kevisual-router.git" }, "dependencies": { - "path-to-regexp": "^8.2.0", - "selfsigned": "^2.4.1", + "path-to-regexp": "^8.3.0", + "selfsigned": "^3.0.1", "send": "^1.2.0" }, "publishConfig": { diff --git a/src/server/ws-server.ts b/src/server/ws-server.ts index bb27c6b..713874a 100644 --- a/src/server/ws-server.ts +++ b/src/server/ws-server.ts @@ -43,10 +43,11 @@ export class WsServerBase { this.listening = true; this.wss.on('connection', (ws) => { - ws.on('message', async (message: string) => { + ws.on('message', async (message: string | Buffer) => { const data = parseIfJson(message); if (typeof data === 'string') { - ws.emit('string', data); + const cleanMessage = data.trim().replace(/^["']|["']$/g, ''); + ws.emit('string', cleanMessage); return; } const { type, data: typeData, ...rest } = data; @@ -83,7 +84,7 @@ export class WsServerBase { if (message === 'close') { ws.close(); } - if (message === 'ping') { + if (message == 'ping') { ws.send('pong'); } }); diff --git a/src/test/ws.ts b/src/test/ws.ts new file mode 100644 index 0000000..397da04 --- /dev/null +++ b/src/test/ws.ts @@ -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"); +}); \ No newline at end of file diff --git a/src/utils/parse.ts b/src/utils/parse.ts index 095b5b8..855eb8c 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -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 { // 尝试解析 JSON - const parsed = JSON.parse(input); + const parsed = JSON.parse(str); // 检查解析结果是否为对象(数组或普通对象) if (typeof parsed === 'object' && parsed !== null) { return parsed; @@ -9,5 +10,5 @@ export const parseIfJson = (input: string): { [key: string]: any } | string => { } catch (e) { // 如果解析失败,直接返回原始字符串 } - return input; + return str; };