maxserver 0.0.13 → 0.0.14
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/package.json +1 -1
- package/src/getAddress.js +19 -11
- package/src/index.js +1 -0
package/package.json
CHANGED
package/src/getAddress.js
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
|
-
import os from "os";
|
|
2
|
-
|
|
1
|
+
import os from "node:os";
|
|
3
2
|
|
|
4
3
|
export function setupGetAddress(app) {
|
|
5
|
-
app.decorate("getAddress",
|
|
6
|
-
const addr =
|
|
4
|
+
app.decorate("getAddress", () => {
|
|
5
|
+
const addr = app.server?.address();
|
|
6
|
+
const protocol = app.initialConfig?.https ? "https" : "http";
|
|
7
7
|
|
|
8
8
|
if (!addr) return null;
|
|
9
9
|
if (typeof addr === "string") return addr;
|
|
10
10
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const isPublicBind = addr.address === "0.0.0.0" || addr.address === "::";
|
|
12
|
+
const isLoopback = addr.address === "127.0.0.1" || addr.address === "::1";
|
|
13
|
+
|
|
14
|
+
const envIp = String(process.env.PUBLIC_IP || "").trim() || null;
|
|
15
|
+
const detectedIp = envIp || getLanIp();
|
|
16
|
+
|
|
17
|
+
const ip = (isPublicBind && !isLoopback)
|
|
18
|
+
? (detectedIp || addr.address)
|
|
19
|
+
: "localhost";
|
|
20
|
+
|
|
21
|
+
const host = ip.includes(":") ? `[${ip}]` : ip;
|
|
13
22
|
|
|
14
23
|
return `${protocol}://${host}:${addr.port}`;
|
|
15
24
|
});
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
|
|
19
|
-
function getExternalIp() {
|
|
27
|
+
function getLanIp() {
|
|
20
28
|
const nets = os.networkInterfaces();
|
|
21
29
|
|
|
22
30
|
for (const name of Object.keys(nets)) {
|
|
23
|
-
for (const net of nets[name]) {
|
|
24
|
-
if (net
|
|
31
|
+
for (const net of (nets[name] || [])) {
|
|
32
|
+
if (net?.family === "IPv4" && !net.internal) return net.address;
|
|
25
33
|
}
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
return null;
|
|
29
|
-
}
|
|
37
|
+
}
|
package/src/index.js
CHANGED