1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

feat(unstable): add Deno.networkInterfaces (#13475)

This commit is contained in:
Yoshiya Hinosawa 2022-01-24 18:39:28 +09:00 committed by Bartek Iwańczuk
parent b851caaee1
commit 05e531009a
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
4 changed files with 61 additions and 0 deletions

View file

@ -103,6 +103,36 @@ declare namespace Deno {
swapFree: number; swapFree: number;
} }
/** The information of the network interface */
export interface NetworkInterfaceInfo {
/** The network interface name */
name: string;
/** The IP protocol version */
family: "IPv4" | "IPv6";
/** The IP address */
address: string;
/** The netmask */
netmask: string;
/** The IPv6 scope id or null */
scopeid: number | null;
/** The CIDR range */
cidr: string;
/** The MAC address */
mac: string;
}
/** **Unstable** new API. yet to be vetted.
*
* Returns an array of the network interface informations.
*
* ```ts
* console.log(Deno.networkInterfaces());
* ```
*
* Requires `allow-env` permission.
*/
export function networkInterfaces(): NetworkInterfaceInfo[];
/** All possible types for interfacing with foreign functions */ /** All possible types for interfacing with foreign functions */
export type NativeType = export type NativeType =
| "void" | "void"

View file

@ -0,0 +1,25 @@
import { assert } from "./test_util.ts";
Deno.test(
{ name: "Deno.networkInterfaces", permissions: { env: true } },
() => {
const networkInterfaces = Deno.networkInterfaces();
assert(Array.isArray(networkInterfaces));
assert(networkInterfaces.length > 0);
for (
const { name, family, address, netmask, scopeid, cidr, mac }
of networkInterfaces
) {
assert(typeof name === "string");
assert(family === "IPv4" || family === "IPv6");
assert(typeof address === "string");
assert(typeof netmask === "string");
assert(
(family === "IPv6" && typeof scopeid === "number") ||
(family === "IPv4" && scopeid === null),
);
assert(typeof cidr === "string");
assert(typeof mac === "string");
}
},
);

View file

@ -24,6 +24,10 @@
return core.opSync("op_system_memory_info"); return core.opSync("op_system_memory_info");
} }
function networkInterfaces() {
return core.opSync("op_network_interfaces");
}
// This is an internal only method used by the test harness to override the // This is an internal only method used by the test harness to override the
// behavior of exit when the exit sanitizer is enabled. // behavior of exit when the exit sanitizer is enabled.
let exitHandler = null; let exitHandler = null;
@ -89,5 +93,6 @@
systemMemoryInfo, systemMemoryInfo,
hostname, hostname,
loadavg, loadavg,
networkInterfaces,
}; };
})(this); })(this);

View file

@ -121,6 +121,7 @@
hostname: __bootstrap.os.hostname, hostname: __bootstrap.os.hostname,
osRelease: __bootstrap.os.osRelease, osRelease: __bootstrap.os.osRelease,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo, systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
networkInterfaces: __bootstrap.os.networkInterfaces,
applySourceMap: __bootstrap.errorStack.opApplySourceMap, applySourceMap: __bootstrap.errorStack.opApplySourceMap,
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics, formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
sleepSync: __bootstrap.timers.sleepSync, sleepSync: __bootstrap.timers.sleepSync,