2024-05-30 02:17:40 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
/**
|
|
|
|
* Gets the IPv4 or IPv6 network address of the machine.
|
|
|
|
*
|
docs(archive,assert,cache,cli,encoding,html,http,net,streams,text): remove unstable Markdown alert (#5672)
* docs(archive,cli,html,http,net,streams,text): remove unstable Markdown alert
* update
* fix
* update
* fmt
* fix
2024-08-22 06:55:17 +00:00
|
|
|
* @experimental **UNSTABLE**: New API, yet to be vetted.
|
|
|
|
|
2024-07-09 11:15:05 +00:00
|
|
|
*
|
2024-05-30 02:17:40 +00:00
|
|
|
* 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.
|
2024-07-18 04:10:09 +00:00
|
|
|
* @returns The IPv4 network address of the machine or `undefined` if not found.
|
2024-05-30 02:17:40 +00:00
|
|
|
*
|
|
|
|
* @example Get the IPv4 network address (default)
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts no-assert ignore
|
2024-09-12 04:05:40 +00:00
|
|
|
* import { getNetworkAddress } from "@std/net/unstable-get-network-address";
|
2024-05-30 02:17:40 +00:00
|
|
|
*
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
* const hostname = getNetworkAddress()!;
|
2024-05-30 02:17:40 +00:00
|
|
|
*
|
2024-06-07 03:54:15 +00:00
|
|
|
* Deno.serve({ port: 0, hostname }, () => new Response("Hello, world!"));
|
2024-05-30 02:17:40 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Get the IPv6 network address
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts no-assert ignore
|
2024-09-12 04:05:40 +00:00
|
|
|
* import { getNetworkAddress } from "@std/net/unstable-get-network-address";
|
2024-05-30 02:17:40 +00:00
|
|
|
*
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
* const hostname = getNetworkAddress("IPv6")!;
|
2024-05-30 02:17:40 +00:00
|
|
|
*
|
2024-06-07 03:54:15 +00:00
|
|
|
* Deno.serve({ port: 0, hostname }, () => new Response("Hello, world!"));
|
2024-05-30 02:17:40 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function getNetworkAddress(
|
|
|
|
family: Deno.NetworkInterfaceInfo["family"] = "IPv4",
|
|
|
|
): string | undefined {
|
|
|
|
return Deno.networkInterfaces()
|
|
|
|
.find((i) =>
|
|
|
|
i.family === family &&
|
2024-09-11 23:11:25 +00:00
|
|
|
i.mac !== "00:00:00:00:00:00" &&
|
2024-05-30 02:17:40 +00:00
|
|
|
(family === "IPv4"
|
|
|
|
// Cannot lie within 127.0.0.0/8
|
|
|
|
? !i.address.startsWith("127")
|
|
|
|
// Cannot be loopback or link-local addresses
|
|
|
|
: !(i.address === "::1" || i.address === "fe80::1") && i.scopeid === 0)
|
|
|
|
)
|
|
|
|
?.address;
|
|
|
|
}
|