@jsenv/server 13.0.0 → 14.1.0

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.
@@ -1,74 +0,0 @@
1
- import { networkInterfaces } from "node:os"
2
-
3
- import { applyDnsResolution } from "./dns_resolution.js"
4
-
5
- export const getServerOrigins = async ({ protocol, host, port }) => {
6
- const isLocal = LOOPBACK_HOSTNAMES.includes(host)
7
- const localhostDnsResolution = await applyDnsResolution("localhost")
8
- const localOrigin = createServerOrigin({
9
- protocol,
10
- hostname:
11
- localhostDnsResolution.address === "127.0.0.1"
12
- ? "localhost"
13
- : "127.0.0.1",
14
- port,
15
- })
16
- if (isLocal) {
17
- return { local: localOrigin }
18
- }
19
- const isAnyIp = WILDCARD_HOSTNAMES.includes(host)
20
- const networkOrigin = createServerOrigin({
21
- protocol,
22
- hostname: isAnyIp ? getExternalIp() : host,
23
- port,
24
- })
25
- return {
26
- local: localOrigin,
27
- network: networkOrigin,
28
- }
29
- }
30
-
31
- const LOOPBACK_HOSTNAMES = [
32
- "localhost",
33
- "127.0.0.1",
34
- "::1",
35
- "0000:0000:0000:0000:0000:0000:0000:0001",
36
- ]
37
-
38
- const WILDCARD_HOSTNAMES = [
39
- undefined,
40
- "0.0.0.0",
41
- "::",
42
- "0000:0000:0000:0000:0000:0000:0000:0000",
43
- ]
44
-
45
- const createServerOrigin = ({ protocol, hostname, port }) => {
46
- const url = new URL("https://127.0.0.1:80")
47
- url.protocol = protocol
48
- url.hostname = hostname
49
- url.port = port
50
- return url.origin
51
- }
52
-
53
- const getExternalIp = () => {
54
- const networkInterfaceMap = networkInterfaces()
55
- let internalIPV4NetworkAddress
56
- Object.keys(networkInterfaceMap).find((key) => {
57
- const networkAddressArray = networkInterfaceMap[key]
58
- return networkAddressArray.find((networkAddress) => {
59
- if (networkAddress.internal) return false
60
- if (!isIpV4(networkAddress)) return false
61
- internalIPV4NetworkAddress = networkAddress
62
- return true
63
- })
64
- })
65
- return internalIPV4NetworkAddress ? internalIPV4NetworkAddress.address : null
66
- }
67
-
68
- const isIpV4 = (networkAddress) => {
69
- // node 18+
70
- if (typeof networkAddress.family === "number") {
71
- return networkAddress.family === 4
72
- }
73
- return networkAddress.family === "IPv4"
74
- }