chore: update version to 0.0.52, add eventemitter3 dependency, and refactor WebSocket handling

This commit is contained in:
2025-12-30 18:32:16 +08:00
parent 8b16bc2218
commit 5dcc678ad4
6 changed files with 23 additions and 12 deletions

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.51", "version": "0.0.52",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "main": "./dist/router.js",
@@ -32,9 +32,8 @@
"@types/bun": "^1.3.5", "@types/bun": "^1.3.5",
"@types/node": "^25.0.3", "@types/node": "^25.0.3",
"@types/send": "^1.2.1", "@types/send": "^1.2.1",
"@types/ws": "^8.18.1",
"@types/xml2js": "^0.4.14", "@types/xml2js": "^0.4.14",
"cookie": "^1.1.1", "@types/ws": "^8.18.1",
"nanoid": "^5.1.6", "nanoid": "^5.1.6",
"rollup": "^4.54.0", "rollup": "^4.54.0",
"rollup-plugin-dts": "^6.3.0", "rollup-plugin-dts": "^6.3.0",
@@ -43,9 +42,10 @@
"tslib": "^2.8.1", "tslib": "^2.8.1",
"tsx": "^4.21.0", "tsx": "^4.21.0",
"typescript": "^5.9.3", "typescript": "^5.9.3",
"ws": "npm:@kevisual/ws",
"xml2js": "^0.6.2", "xml2js": "^0.6.2",
"zod": "^4.2.1" "zod": "^4.2.1",
"eventemitter3": "^5.0.1",
"ws": "npm:@kevisual/ws"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

11
pnpm-lock.yaml generated
View File

@@ -8,6 +8,9 @@ importers:
.: .:
dependencies: dependencies:
eventemitter3:
specifier: ^5.0.1
version: 5.0.1
path-to-regexp: path-to-regexp:
specifier: ^8.3.0 specifier: ^8.3.0
version: 8.3.0 version: 8.3.0
@@ -51,9 +54,6 @@ importers:
'@types/xml2js': '@types/xml2js':
specifier: ^0.4.14 specifier: ^0.4.14
version: 0.4.14 version: 0.4.14
cookie:
specifier: ^1.1.1
version: 1.1.1
nanoid: nanoid:
specifier: ^5.1.6 specifier: ^5.1.6
version: 5.1.6 version: 5.1.6
@@ -762,6 +762,9 @@ packages:
resolution: {integrity: sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==} resolution: {integrity: sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==}
engines: {node: '>= 0.6'} engines: {node: '>= 0.6'}
eventemitter3@5.0.1:
resolution: {integrity: sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==}
events@3.3.0: events@3.3.0:
resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==} resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==}
engines: {node: '>=0.8.x'} engines: {node: '>=0.8.x'}
@@ -1741,6 +1744,8 @@ snapshots:
etag@1.8.1: {} etag@1.8.1: {}
eventemitter3@5.0.1: {}
events@3.3.0: {} events@3.3.0: {}
fast-deep-equal@3.1.3: {} fast-deep-equal@3.1.3: {}

View File

@@ -3,7 +3,7 @@ import { handleServer } from './handle-server.ts';
import * as cookie from './cookie.ts'; import * as cookie from './cookie.ts';
import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts'; import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
import { parseIfJson } from '../utils/parse.ts'; import { parseIfJson } from '../utils/parse.ts';
import { EventEmitter } from 'events'; import { EventEmitter } from 'eventemitter3';
type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void; type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
export type HandleCtx = { export type HandleCtx = {
@@ -273,6 +273,10 @@ export class ServerBase implements ServerType {
end({ code: 500, message: `${type} server is error` }); end({ code: 500, message: `${type} server is error` });
} }
} }
/**
* 根据emitter提醒关闭ws连接
* @param ws
*/
async onWsClose(ws: WS) { async onWsClose(ws: WS) {
const id = ws?.data?.id || ''; const id = ws?.data?.id || '';
if (id) { if (id) {

View File

@@ -253,6 +253,7 @@ export class BunServer extends ServerBase implements ServerType {
}, },
close: (ws: any) => { close: (ws: any) => {
// WebSocket 连接关闭 // WebSocket 连接关闭
ws?.data?.close?.();
this.onWsClose(ws); this.onWsClose(ws);
}, },
}, },

View File

@@ -1,4 +1,4 @@
import EventEmitter from 'node:events'; import { EventEmitter } from 'eventemitter3';
import * as http from 'node:http'; import * as http from 'node:http';

View File

@@ -2,7 +2,6 @@
import { WebSocketServer } from 'ws'; import { WebSocketServer } from 'ws';
import type { WebSocket } from 'ws'; import type { WebSocket } from 'ws';
import { ServerType } from './server-type.ts' import { ServerType } from './server-type.ts'
import { parseIfJson } from '../utils/parse.ts';
import { isBun } from '../utils/is-engine.ts'; import { isBun } from '../utils/is-engine.ts';
@@ -62,6 +61,8 @@ export class WsServerBase {
}); });
this.server.sendConnected(ws); this.server.sendConnected(ws);
this.wss.on('close', () => { this.wss.on('close', () => {
// @ts-ignore
ws?.data?.close?.();
this.server.onWsClose(ws); this.server.onWsClose(ws);
}); });
}); });