Skip to content

Commit c6bdace

Browse files
committed
fix(rspeedy): replace default-gateway
This is a mirror of webpack/webpack-dev-server#5255 Fixes lynx-family#83
1 parent c871635 commit c6bdace

File tree

4 files changed

+43
-76
lines changed

4 files changed

+43
-76
lines changed

.changeset/twenty-cameras-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Fix error "'wmic' is not recognized as an internal or external command"

packages/rspeedy/core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"@rsdoctor/rspack-plugin": "^0.4.13",
6363
"chokidar": "^4.0.3",
6464
"commander": "^13.1.0",
65-
"default-gateway": "^7.2.2",
6665
"exit-hook": "^4.0.0",
6766
"ipaddr.js": "^2.2.0",
6867
"javascript-stringify": "^2.1.0",
@@ -75,7 +74,6 @@
7574
"@lynx-js/vitest-setup": "workspace:*",
7675
"@rollup/plugin-typescript": "^12.1.2",
7776
"@rsbuild/webpack": "1.2.3",
78-
"@types/default-gateway": "^7.2.2",
7977
"eventemitter3": "^5.0.1",
8078
"type-fest": "^4.37.0",
8179
"vitest": "^3.0.7",

packages/rspeedy/core/src/plugins/dev.plugin.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -133,50 +133,55 @@ export function pluginDev(
133133
}
134134
}
135135

136-
export async function findIp(family: 'v4' | 'v6'): Promise<string> {
136+
export async function findIp(
137+
family: 'v4' | 'v6',
138+
isInternal = false,
139+
): Promise<string> {
137140
const [
138-
{ default: defaultGateway },
139141
{ default: ipaddr },
140142
os,
141143
] = await Promise.all([
142-
import('default-gateway'),
143-
144144
import('ipaddr.js'),
145145
import('node:os'),
146146
])
147-
const gateway = await (async () => {
148-
if (family === 'v4') {
149-
const { gateway } = await defaultGateway.gateway4async()
150-
return gateway
151-
} else {
152-
const { gateway } = await defaultGateway.gateway6async()
153-
return gateway
154-
}
155-
})()
156-
const gatewayIp = ipaddr.parse(gateway)
157-
158-
// Look for the matching interface in all local interfaces.
159-
for (const addresses of Object.values(os.networkInterfaces())) {
160-
if (!addresses) {
161-
continue
162-
}
163-
164-
for (const { cidr, internal } of addresses) {
165-
if (!cidr || internal) {
166-
continue
147+
148+
let host: string | undefined
149+
150+
Object.values(os.networkInterfaces())
151+
.flatMap((networks) => networks ?? [])
152+
.filter((network) => {
153+
if (!network || !network.address) {
154+
return false
167155
}
168156

169-
const net = ipaddr.parseCIDR(cidr)
157+
if (network.family !== `IP${family}`) {
158+
return false
159+
}
170160

171-
if (
172-
net[0]
173-
&& net[0].kind() === gatewayIp.kind()
174-
&& gatewayIp.match(net)
175-
) {
176-
return net[0].toString()
161+
if (network.internal !== isInternal) {
162+
return false
177163
}
178-
}
164+
165+
if (family === 'v6') {
166+
const range = ipaddr.parse(network.address).range()
167+
168+
if (range !== 'ipv4Mapped' && range !== 'uniqueLocal') {
169+
return false
170+
}
171+
}
172+
173+
return network.address
174+
})
175+
.forEach((network) => {
176+
host = network.address
177+
if (host.includes(':')) {
178+
host = `[${host}]`
179+
}
180+
})
181+
182+
if (!host) {
183+
throw new Error(`No valid IP found`)
179184
}
180185

181-
throw new Error(`No valid IP found for the default gateway ${gateway}`)
186+
return host
182187
}

pnpm-lock.yaml

Lines changed: 0 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)