Skip to main content
On this page

@std/net

Overview Jump to heading

Network utilities.

import { getAvailablePort } from "@std/net";

const command = new Deno.Command(Deno.execPath(), {
 args: ["test.ts", "--port", getAvailablePort().toString()],
});

// ...

Add to your project Jump to heading

deno add jsr:@std/net

See all symbols in @std/net on

What is @std/net? Jump to heading

Utilities to help with common networking tasks that complement Deno's core APIs:

  • Finding or reserving an open port.
  • Discovering a machine's LAN address to bind servers.
  • Validating IP versions and checking if an address belongs to a CIDR subnet.

Why use @std/net? Jump to heading

  • To pick safe ports and interfaces:
    • Prefer ephemeral ports via Deno.listen({ port: 0 }) when you control the listener.
    • Use getAvailablePort() when you need a free port value to pass to other processes before binding (then bind immediately to avoid races).
  • Bind to the right network: use getNetworkAddress() to serve on your LAN’s IPv4 or IPv6 address instead of loopback.
  • Validate and filter traffic: isIPv4/isIPv6 and match*Subnet helpers let you build allow/deny lists or feature gates by IP ranges.

Examples Jump to heading

const listener = Deno.listen({ hostname: "127.0.0.1", port: 0 });
const { port } = listener.addr as Deno.NetAddr;
// start server on listener ...

Bind to a LAN address (not just localhost) Jump to heading

import { getNetworkAddress } from "@std/net/unstable-get-network-address";

const hostname = getNetworkAddress() ?? "127.0.0.1"; // default IPv4
Deno.serve({ hostname, port: 0 }, () => new Response("Hello from LAN"));

Validate incoming IP family Jump to heading

import { isIPv4, isIPv6 } from "@std/net/unstable-ip";

function describe(ip: string): string {
  if (isIPv4(ip)) return "IPv4";
  if (isIPv6(ip)) return "IPv6";
  return "unknown";
}

Allowlist requests by subnet (IPv4 and IPv6) Jump to heading

import { matchSubnets } from "@std/net/unstable-ip";

const allowlist = [
  "192.168.1.0/24", // home LAN
  "2001:db8::/64", // docs example IPv6 prefix
];

Deno.serve((req, info) => {
  const ip = (info.remoteAddr as Deno.NetAddr).hostname;
  if (!matchSubnets(ip, allowlist)) {
    return new Response("Forbidden", { status: 403 });
  }
  return new Response("OK");
});

Pass a free port to a child process Jump to heading

import { getAvailablePort } from "@std/net";

const port = getAvailablePort();
// Immediately reserve it in your process if possible to avoid races
const listener = Deno.listen({ hostname: "127.0.0.1", port });

const child = new Deno.Command("deno", {
  args: ["run", "--allow-net", "./child_server.ts", String(port)],
});
child.spawn();

Tips Jump to heading

  • Prefer Deno.listen({ port: 0 }) when possible; use getAvailablePort() only when you must pass a port value to another process first.
  • After obtaining a port number, bind right away in the same process to avoid TOCTOU race conditions.
  • unstable-ip helpers are great for allow/deny lists but avoid putting full access control solely in client-controlled headers like X-Forwarded-For.

Did you find what you needed?

Privacy policy