@hile/micro 1.0.2 → 1.0.3
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/application.d.ts +1 -1
- package/dist/application.js +1 -1
- package/dist/server.d.ts +6 -2
- package/dist/server.js +42 -25
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +3 -20
- package/package.json +3 -2
package/dist/application.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare class Application extends Server {
|
|
|
12
12
|
private readonly _registry_address;
|
|
13
13
|
private readonly namespaces;
|
|
14
14
|
constructor(props: ApplicationProps);
|
|
15
|
-
listen(port
|
|
15
|
+
listen(port?: number): Promise<() => Promise<void>>;
|
|
16
16
|
private reconnectToRegistry;
|
|
17
17
|
private findFromRegistry;
|
|
18
18
|
get(namespace: string): Promise<Client>;
|
package/dist/application.js
CHANGED
|
@@ -15,7 +15,7 @@ export class Application extends Server {
|
|
|
15
15
|
super(namespace, loaderProps);
|
|
16
16
|
this._registry_address = registry;
|
|
17
17
|
}
|
|
18
|
-
async listen(port) {
|
|
18
|
+
async listen(port = 0) {
|
|
19
19
|
const callback = await super.listen(port);
|
|
20
20
|
await this.reconnectToRegistry();
|
|
21
21
|
return async () => {
|
package/dist/server.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { MessageLoader, MessageLoaderProps } from "@hile/message-loader";
|
|
2
2
|
import { Client } from './client.js';
|
|
3
|
+
import { IncomingMessage } from 'http';
|
|
3
4
|
import { EventEmitter } from 'node:events';
|
|
5
|
+
import type { Duplex } from "node:stream";
|
|
4
6
|
export declare class Server extends MessageLoader {
|
|
5
7
|
private readonly namespace;
|
|
6
8
|
private wss?;
|
|
@@ -9,8 +11,10 @@ export declare class Server extends MessageLoader {
|
|
|
9
11
|
private readonly ipv4;
|
|
10
12
|
readonly events: EventEmitter<any>;
|
|
11
13
|
constructor(namespace: string, props?: MessageLoaderProps);
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
+
private upstream;
|
|
15
|
+
private createClient;
|
|
14
16
|
protected connect(host: string, port: number, timeout?: number): Promise<Client>;
|
|
15
17
|
listen(port?: number): Promise<() => Promise<void>>;
|
|
18
|
+
setPort(port: number): this;
|
|
19
|
+
handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer): this;
|
|
16
20
|
}
|
package/dist/server.js
CHANGED
|
@@ -22,7 +22,7 @@ export class Server extends MessageLoader {
|
|
|
22
22
|
client.events.emit('disconnect', extras);
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
upstream(ws, req) {
|
|
26
26
|
const path = req.url?.split('?')[0];
|
|
27
27
|
if (!path)
|
|
28
28
|
return ws.close();
|
|
@@ -34,9 +34,9 @@ export class Server extends MessageLoader {
|
|
|
34
34
|
const [host, port, ...extras] = sp;
|
|
35
35
|
if (!host || !port)
|
|
36
36
|
return ws.close();
|
|
37
|
-
this.
|
|
37
|
+
this.createClient(ws, host, Number(port), extras);
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
createClient(ws, host, port, extras = []) {
|
|
40
40
|
const key = `${host}:${port}`;
|
|
41
41
|
const client = new Client({ server: this, ws, host, port });
|
|
42
42
|
ws.on('close', () => {
|
|
@@ -55,6 +55,8 @@ export class Server extends MessageLoader {
|
|
|
55
55
|
if (this.clients.has(key)) {
|
|
56
56
|
return this.clients.get(key);
|
|
57
57
|
}
|
|
58
|
+
if (!this.port)
|
|
59
|
+
throw new Error('You can not connect to a server without a local port, please use `.setPort(port)` for local port.');
|
|
58
60
|
const ws = await new Promise((resolve, reject) => {
|
|
59
61
|
const ws = new WebSocket(`ws://${host}:${port}/${this.ipv4}/${this.port}/${this.namespace}`);
|
|
60
62
|
const timer = setTimeout(() => {
|
|
@@ -82,36 +84,39 @@ export class Server extends MessageLoader {
|
|
|
82
84
|
ws.on('open', onopen);
|
|
83
85
|
ws.on('error', onerror);
|
|
84
86
|
});
|
|
85
|
-
return this.
|
|
87
|
+
return this.createClient(ws, host, port);
|
|
86
88
|
}
|
|
87
|
-
async listen(port) {
|
|
88
|
-
|
|
89
|
+
async listen(port = 0) {
|
|
90
|
+
if (port > 0) {
|
|
89
91
|
const wss = new WebSocketServer({ port });
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
92
|
+
this.wss = await new Promise((resolve, reject) => {
|
|
93
|
+
const clear = () => {
|
|
94
|
+
wss.off('error', onerror);
|
|
95
|
+
wss.off('listening', onlistening);
|
|
96
|
+
};
|
|
97
|
+
const onerror = (err) => {
|
|
98
|
+
clear();
|
|
99
|
+
reject(err);
|
|
100
|
+
};
|
|
101
|
+
const onlistening = () => {
|
|
102
|
+
clear();
|
|
103
|
+
resolve(wss);
|
|
104
|
+
};
|
|
105
|
+
wss.on('error', onerror);
|
|
106
|
+
wss.on('listening', onlistening);
|
|
107
|
+
});
|
|
108
|
+
this.wss.on('connection', (ws, req) => this.upstream(ws, req));
|
|
109
|
+
this.setPort(port);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
this.wss = new WebSocketServer({ noServer: true });
|
|
113
|
+
}
|
|
108
114
|
return async () => {
|
|
109
115
|
for (const client of this.clients.values()) {
|
|
110
116
|
client.dispose();
|
|
111
117
|
}
|
|
112
118
|
this.clients.clear();
|
|
113
119
|
if (this.wss) {
|
|
114
|
-
this.wss.off('connection', onConnection);
|
|
115
120
|
await new Promise((resolve, reject) => {
|
|
116
121
|
this.wss.close((err) => {
|
|
117
122
|
if (err)
|
|
@@ -124,4 +129,16 @@ export class Server extends MessageLoader {
|
|
|
124
129
|
this.port = undefined;
|
|
125
130
|
};
|
|
126
131
|
}
|
|
132
|
+
setPort(port) {
|
|
133
|
+
this.port = port;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
handleUpgrade(req, socket, head) {
|
|
137
|
+
if (!this.wss)
|
|
138
|
+
throw new Error('WebSocket server not initialized');
|
|
139
|
+
this.wss.handleUpgrade(req, socket, head, ws => {
|
|
140
|
+
this.upstream(ws, req);
|
|
141
|
+
});
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
127
144
|
}
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
function isIPv4(family) {
|
|
3
|
-
return family === 'IPv4' || family === 4;
|
|
4
|
-
}
|
|
1
|
+
import { internalIpV4Sync } from 'internal-ip';
|
|
5
2
|
/**
|
|
6
|
-
*
|
|
3
|
+
* 本机用于「宣告」给其他节点的 IPv4(与 {@link internalIpV4Sync} 行为一致:多网卡无法唯一确定时可能为 `undefined`)。
|
|
7
4
|
*/
|
|
8
5
|
export function getLocalIPv4() {
|
|
9
|
-
|
|
10
|
-
if (!ifaces) {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
|
-
for (const addrs of Object.values(ifaces)) {
|
|
14
|
-
if (!addrs) {
|
|
15
|
-
continue;
|
|
16
|
-
}
|
|
17
|
-
for (const addr of addrs) {
|
|
18
|
-
if (isIPv4(addr.family) && !addr.internal) {
|
|
19
|
-
return addr.address;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return undefined;
|
|
6
|
+
return internalIpV4Sync();
|
|
24
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/micro",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@hile/message-loader": "^1.0.8",
|
|
27
27
|
"@hile/message-ws": "^1.0.6",
|
|
28
|
+
"internal-ip": "^9.0.0",
|
|
28
29
|
"ws": "^8.19.0"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "fc98c3e1ecf57f6cb4aded9caef28da0a35b7f30"
|
|
31
32
|
}
|