@zhin.js/http 1.0.6 → 1.0.8
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/CHANGELOG.md +56 -0
- package/lib/index.d.ts +25 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +235 -410
- package/lib/index.js.map +1 -1
- package/lib/router.d.ts +11 -6
- package/lib/router.d.ts.map +1 -1
- package/lib/router.js +16 -8
- package/lib/router.js.map +1 -1
- package/package.json +25 -9
- package/app/index.ts +0 -732
- package/app/router.ts +0 -55
package/app/router.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { remove } from '@zhin.js/core';
|
|
2
|
-
import { Layer, RouterOptions } from '@koa/router';
|
|
3
|
-
import KoaRouter from "@koa/router";
|
|
4
|
-
import * as http from 'http';
|
|
5
|
-
import { ServerOptions, WebSocketServer } from 'ws';
|
|
6
|
-
import { parse } from 'url';
|
|
7
|
-
|
|
8
|
-
type Path = string | RegExp;
|
|
9
|
-
|
|
10
|
-
export class Router extends KoaRouter {
|
|
11
|
-
wsStack: WebSocketServer[] = [];
|
|
12
|
-
whiteList: Path[] = []; //用于historyApi排除
|
|
13
|
-
constructor(
|
|
14
|
-
public server: http.Server,
|
|
15
|
-
options?: RouterOptions,
|
|
16
|
-
) {
|
|
17
|
-
super(options);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
register(...args: Parameters<KoaRouter['register']>) {
|
|
21
|
-
const path: Path = args[0] as any;
|
|
22
|
-
this.whiteList.push(path);
|
|
23
|
-
return super.register(...args);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
destroy(layer: Layer) {
|
|
27
|
-
remove(this.stack, layer);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
destroyWs(wsServer: WebSocketServer) {
|
|
31
|
-
wsServer.close();
|
|
32
|
-
remove(this.wsStack, wsServer);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
ws(path: string, options: Omit<ServerOptions, 'noServer' | 'path'> = {}): WebSocketServer {
|
|
36
|
-
const wsServer = new WebSocketServer({
|
|
37
|
-
noServer: true,
|
|
38
|
-
path,
|
|
39
|
-
...options,
|
|
40
|
-
});
|
|
41
|
-
this.wsStack.push(wsServer);
|
|
42
|
-
|
|
43
|
-
this.server!.on('upgrade', (request, socket, head) => {
|
|
44
|
-
const { pathname } = parse(request.url!);
|
|
45
|
-
if (this.wsStack.findIndex(wss => wss.options.path === path) === -1) {
|
|
46
|
-
socket.destroy();
|
|
47
|
-
} else if (pathname === path) {
|
|
48
|
-
wsServer.handleUpgrade(request, socket, head, ws => {
|
|
49
|
-
wsServer.emit('connection', ws, request);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return wsServer;
|
|
54
|
-
}
|
|
55
|
-
}
|