npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@viviengraffin/ip-context

v0.1.6

Published

TypeScript library to have datas for network context or an IPv4/IPv6 address

Readme

ip-context JSR JSR Score NPM

Si vous êtes francophone, vous pouvez aller ici

ip-context is a Typescript library for performing calculations based on IPv4/IPv6 addresses or network context.

How to contribute?

If you would like to contribute to this project, go here

Basic Examples

Retrieving the size of a network

To retrieve the size of a network, you need to create a context that will contain an address and a subnet mask. Here are several ways to do this:

With the context function

See more about context function here

import { context } from "@viviengraffin/ip-context";

const ctx = context("192.168.1.1/24"); // CIDR notation
console.log(ctx.size); // Calculates and retrieves the network size
console.log(ctx.hosts); // Calculates and retrieves the number of addressable IP addresses in this network

With the IPv4Context class and the static method fromStringWithCidr

See more about IPv4Context class here

import { IPv4Context } from "@viviengraffin/ip-context";

const ctx = IPv4Context.fromStringWithCidr("192.168.1.1/24"); // Uses the IPv4Context class to directly determine the type
console.log(ctx.size);
console.log(ctx.hosts);

With the IPv4Context, IPv4Address, and IPv4Submask classes

See more about IPv4Context, IPv4Address and IPv4Submask

import {
  IPv4Address,
  IPv4Context,
  IPv4Submask,
} from "@viviengraffin/ip-context";

const ctx = new IPv4Context(
  IPv4Address.fromString("192.168.1.1"),
  IPv4Submask.fromCidr(24),
); // Uses the classes to build the network context
console.log(ctx.size);
console.log(ctx.hosts);

With the IPv6Context, IPv6Address, IPv6Submask

See more about IPv6Context, IPv6Address and IPv6Submask

import {
  IPv6Address,
  IPv6Context,
  IPv6Submask,
} from "@viviengraffin/ip-context";

const ctx = new IPv6Context(
  IPv6Address.fromString("2001:db6::1"),
  IPv6Submask.fromCidr(64),
);
console.log(ctx.size);
console.log(ctx.hosts);

For IPv4, if no CIDR or subnet mask is passed to the context function, it is based on the IPv4 class.

See more about context function here

import { context, IPv4Address } from "@viviengraffin/ip-context";

const ctx = context("192.168.1.1") as IPv4Address; // Cast the result of context so that TypeScript understands that we are expecting an instance of IPv4Context
console.log(ctx.class); // IPv4 address class (here C)
console.log(ctx.size);
console.log(ctx.hosts);

Retrieve the network addresses, broadcast addresses, first addressable address, last addressable address

// IPv4
import { context } from "@viviengraffin/ip-context";

const ctx = context("192.168.1.1/24");
console.log(ctx.network.toString()); // Displays the network address
console.log(ctx.broadcast.toString()); // Displays the broadcast address
console.log(ctx.firstHost.toString()); // Displays the first addressable address
console.log(ctx.lastHost.toString()); // Displays the last addressable address

// IPv6
import { context } from "@viviengraffin/ip-context";

const ctx = context("2001:db6::1/64");
console.log(ctx.network.toString()); // Displays the network address
console.log(ctx.firstHost.toString()); // Displays the first addressable address
console.log(ctx.lastHost.toString()); // Displays the last addressable address

Checking for the presence of an address in a network context

There are two methods for checking the presence of an IP address in a network context:

  • includes: Checks if this IP address is present in this network
  • isHost: Checks if this IP address is addressable in this network
// IPv4 (works the same way in IPv6)
import { context } from "@viviengraffin/ip-context";

const ctx = context("192.168.1.1/24");
ctx.includes("192.168.1.0"); // True, even if 192.168.1.0 is the network address, it is present in this network
ctx.isHost("192.168.1.0"); // False, even if this address is present in this network, it is the network address that is not addressable

Create a context for a number of hosts

There are three methods for that:

Create an IPAddress (IPv4Address or IPv6Address) and use the createContextWithHosts method

import { IPv4Address } from "@viviengraffin/ip-context";

const ip=IPv4Address.fromString("192.168.1.1");
const ctx=ip.createContextWithHosts(512);
console.log(ctx.hosts); // 1022

Use the Context class, IPAddress class and Submask class

With IPv4 classes

import { IPv4Context, IPv4Address, IPv4Submask } from "@viviengraffin/ip-context";

const ctx=new IPv4Context(
  IPv4Address.fromString("192.168.1.1"),
  IPv4Submask.fromHosts(512)
);
console.log(ctx.hosts); // 1022

With contextWithHosts function

See more about contextWithHosts function here

import { contextWithHosts } from "@viviengraffin/ip-context";

const ctx=contextWithHosts("192.168.1.1",512);
console.log(ctx.hosts); // 1022