Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net): getNetworkAddress() #4677

Merged
merged 17 commits into from
May 30, 2024
14 changes: 1 addition & 13 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { parseArgs } from "@std/cli/parse-args";
import { red } from "@std/fmt/colors";
import denoConfig from "./deno.json" with { type: "json" };
import { format as formatBytes } from "@std/fmt/bytes";
import { getNetworkAddress } from "@std/net/get-network-address";

interface EntryInfo {
mode: string;
Expand Down Expand Up @@ -833,19 +834,6 @@ function main() {
}
}

/**
* Gets the network address of the machine,
* inspired by the util of the same name in `npm:serve`
* https://github.com/vercel/serve/blob/1ea55b1b5004f468159b54775e4fb3090fedbb2b/source/utilities/http.ts#L33
*/
function getNetworkAddress() {
for (const { family, address } of Deno.networkInterfaces()) {
if (family === "IPv4" && !address.startsWith("127.")) {
return address;
}
}
}

function printUsage() {
console.log(`Deno File Server ${denoConfig.version}
Serves a local directory in HTTP.
Expand Down
3 changes: 2 additions & 1 deletion net/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.224.0",
"exports": {
".": "./mod.ts",
"./get-available-port": "./get_available_port.ts"
"./get-available-port": "./get_available_port.ts",
"./get-network-address": "./get_network_address.ts"
}
}
48 changes: 48 additions & 0 deletions net/get_network_address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
* Gets the IPv4 or IPv6 network address of the machine.
*
* This is inspired by the util of the same name in
* {@linkcode https://www.npmjs.com/package/serve | npm:serve}.
*
* For more advanced use, use {@linkcode Deno.networkInterfaces} directly.
*
* @see {@link https://github.com/vercel/serve/blob/1ea55b1b5004f468159b54775e4fb3090fedbb2b/source/utilities/http.ts#L33}
*
* @param family The IP protocol version of the interface to get the address of.
* @returns The IPv4 network address of the machine.
*
* @example Get the IPv4 network address (default)
* ```ts
* import { getNetworkAddress } from "@std/net/get-network-address";
* import { assert } from "@std/assert/assert";
*
* const address = getNetworkAddress();
*
* assert(address !== undefined);
* ```
*
* @example Get the IPv6 network address
* ```ts
* import { getNetworkAddress } from "@std/net/get-network-address";
* import { assert } from "@std/assert/assert";
*
* const address = getNetworkAddress("IPv6");
*
* assert(address !== undefined);
* ```
*/
export function getNetworkAddress(
family: Deno.NetworkInterfaceInfo["family"] = "IPv4",
): string | undefined {
return Deno.networkInterfaces()
.find((i) =>
i.family === family &&
(family === "IPv4"
// Cannot lie within 127.0.0.0/8
? !i.address.startsWith("127")
// Cannot lie within ::1/128 or fe80::/10
: (!i.address.startsWith("::1")) || !i.address.startsWith("fe80::"))
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
)
?.address;
}
18 changes: 18 additions & 0 deletions net/get_network_address_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { getNetworkAddress } from "./get_network_address.ts";
import { assertNotEquals } from "../assert/assert_not_equals.ts";

Deno.test("getNetworkAddress() works with IPv4", () => {
const hostname = getNetworkAddress();
assertNotEquals(hostname, undefined);
// Fails if the IPv4 address doesn't belong to the machine
using _listener = Deno.listen({ hostname, port: 0 });
});

Deno.test("getNetworkAddress() works with IPv6", () => {
const hostname = getNetworkAddress("IPv6");
assertNotEquals(hostname, undefined);
console.log(hostname);
// Fails if the IPv6 address doesn't belong to the machine
using _listener = Deno.listen({ hostname, port: 0 });
});
1 change: 1 addition & 0 deletions net/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export * from "./get_available_port.ts";
export * from "./get_network_address.ts";