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(unstable): add Deno.networkInterfaces #13475

Merged
merged 1 commit into from
Jan 24, 2022
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
30 changes: 30 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ declare namespace Deno {
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 */
export type NativeType =
| "void"
Expand Down
25 changes: 25 additions & 0 deletions cli/tests/unit/network_interfaces_test.ts
Original file line number Diff line number Diff line change
@@ -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");
}
},
);
5 changes: 5 additions & 0 deletions runtime/js/30_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
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
// behavior of exit when the exit sanitizer is enabled.
let exitHandler = null;
Expand Down Expand Up @@ -89,5 +93,6 @@
systemMemoryInfo,
hostname,
loadavg,
networkInterfaces,
};
})(this);
1 change: 1 addition & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
hostname: __bootstrap.os.hostname,
osRelease: __bootstrap.os.osRelease,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
networkInterfaces: __bootstrap.os.networkInterfaces,
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
sleepSync: __bootstrap.timers.sleepSync,
Expand Down