Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-cameras-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/rspeedy": patch
---

Fix error "'wmic' is not recognized as an internal or external command"
2 changes: 0 additions & 2 deletions packages/rspeedy/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"@rsdoctor/rspack-plugin": "^0.4.13",
"chokidar": "^4.0.3",
"commander": "^13.1.0",
"default-gateway": "^7.2.2",
"exit-hook": "^4.0.0",
"ipaddr.js": "^2.2.0",
"javascript-stringify": "^2.1.0",
Expand All @@ -75,7 +74,6 @@
"@lynx-js/vitest-setup": "workspace:*",
"@rollup/plugin-typescript": "^12.1.2",
"@rsbuild/webpack": "1.2.3",
"@types/default-gateway": "^7.2.2",
"eventemitter3": "^5.0.1",
"type-fest": "^4.37.0",
"vitest": "^3.0.7",
Expand Down
71 changes: 38 additions & 33 deletions packages/rspeedy/core/src/plugins/dev.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,50 +133,55 @@ export function pluginDev(
}
}

export async function findIp(family: 'v4' | 'v6'): Promise<string> {
export async function findIp(
family: 'v4' | 'v6',
isInternal = false,
): Promise<string> {
const [
{ default: defaultGateway },
{ default: ipaddr },
os,
] = await Promise.all([
import('default-gateway'),

import('ipaddr.js'),
import('node:os'),
])
const gateway = await (async () => {
if (family === 'v4') {
const { gateway } = await defaultGateway.gateway4async()
return gateway
} else {
const { gateway } = await defaultGateway.gateway6async()
return gateway
}
})()
const gatewayIp = ipaddr.parse(gateway)

// Look for the matching interface in all local interfaces.
for (const addresses of Object.values(os.networkInterfaces())) {
if (!addresses) {
continue
}

for (const { cidr, internal } of addresses) {
if (!cidr || internal) {
continue

let host: string | undefined

Object.values(os.networkInterfaces())
.flatMap((networks) => networks ?? [])
.filter((network) => {
if (!network || !network.address) {
return false
}

const net = ipaddr.parseCIDR(cidr)
if (network.family !== `IP${family}`) {
return false
}

if (
net[0]
&& net[0].kind() === gatewayIp.kind()
&& gatewayIp.match(net)
) {
return net[0].toString()
if (network.internal !== isInternal) {
return false
}
}

if (family === 'v6') {
const range = ipaddr.parse(network.address).range()

if (range !== 'ipv4Mapped' && range !== 'uniqueLocal') {
return false
}
}

return network.address
})
.forEach((network) => {
host = network.address
if (host.includes(':')) {
host = `[${host}]`
}
})

if (!host) {
throw new Error(`No valid IP found`)
}

throw new Error(`No valid IP found for the default gateway ${gateway}`)
return host
}
41 changes: 0 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading