Skip to content

Commit

Permalink
fix: ignore ipv6 link-local addresses from hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 6, 2023
1 parent c59fc0c commit 9e76e76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ export function _getLocalHosts(additional: HostAddress[]): HostAddress[] {
const hosts = new Set<HostAddress>(additional);
for (const _interface of Object.values(networkInterfaces())) {
for (const config of _interface || []) {
hosts.add(config.address);
if (
config.address &&
!config.internal &&
!config.address.startsWith("fe80::") // Link-Local
) {
hosts.add(config.address);
}
}
}
return [...hosts];
Expand Down
10 changes: 8 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Server } from "node:net";
import { describe, test, expect, afterEach, vi } from "vitest";
import { getPort } from "../src";
import { getPort, getRandomPort } from "../src";
import { blockPort } from "./utils";

const isWindows = process.platform === "win32";
Expand Down Expand Up @@ -66,13 +66,19 @@ describe("getPort (host)", () => {
});
});

describe("getPort: random", () => {
describe("random port", () => {
test("{ random: true }", async () => {
const port = await getPort({ random: true });
expect(typeof port).toBe("number");
expect(port).not.toBe(3000);
});

test("getRandomPort", async () => {
const port = await getRandomPort();
expect(typeof port).toBe("number");
expect(port).not.toBe(3000);
});

test("{ port: 0 }", async () => {
let port = await getPort({ port: 0 });
expect(typeof port).toBe("number");
Expand Down

0 comments on commit 9e76e76

Please sign in to comment.