Skip to content
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
8 changes: 4 additions & 4 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"LICENSE",
"package-main.json"
],
"limit": "60.20 kB",
"limit": "60.31 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -18,7 +18,7 @@
"path": [
"target/*/core.*"
],
"limit": "41.15 kB",
"limit": "41.25 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -32,7 +32,7 @@
"LICENSE",
"package-main.json"
],
"limit": "18.25 kB",
"limit": "18.30 kB",
"gzip": true
},
{
Expand All @@ -56,7 +56,7 @@
{
"name": "libdefs",
"path": "target/dts",
"limit": "7.00 kB",
"limit": "7.15 kB",
"brotli": false,
"gzip": false
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/ts/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const PRIVATES: Special[] = ['private', 'linklocal', 'loopback', 'unspecified']
// -------------------------------------------------------

type Family = 4 | 6
export type FamilyAlias = Family | '4' | '6' | 'ipv4' | 'ipv6' | 'IPV4' | 'IPV6' | 'IpV4' | 'IpV6'
type Raw = string | number | bigint | BufferLike | Array<number> | Address
type Subnet = {
family: Family
Expand Down Expand Up @@ -106,7 +107,7 @@ export class Address {
return [...this.toBuffer()]
}

toString(family: Family | string | number = this.family, mapped?: boolean): string {
toString(family: FamilyAlias = this.family, mapped?: boolean): string {
const fam = Address.normalizeFamily(family)
const _mapped = mapped ?? (fam === 6 && this.family !== fam)
const { big } = this
Expand Down Expand Up @@ -282,7 +283,7 @@ export class Address {
return v6.big === ((0xffffn << 32n) | v4.big)
}

static fromPrefixLen = (prefixlen: number | `${number}` | string, family?: string | number): Address => {
static fromPrefixLen = (prefixlen: number | `${number}` | string, family?: FamilyAlias): Address => {
if (typeof prefixlen === 'string' && !isDec(prefixlen)) throw new Error(`Invalid prefix: ${prefixlen}`)

const len = +prefixlen | 0
Expand Down Expand Up @@ -515,7 +516,7 @@ export const or: typeof Address['or'] = Address.or.bind(Address)
export const cidr: typeof Address['cidr'] = Address.cidr.bind(Address)
export const normalizeToLong: typeof Address['normalizeToLong'] = Address.normalizeToLong.bind(Address)

export function fromPrefixLen(prefixlen: number, family?: string | number): string {
export function fromPrefixLen(prefixlen: number, family?: FamilyAlias): string {
return Address.fromPrefixLen(prefixlen, family).toString()
}

Expand Down Expand Up @@ -579,7 +580,7 @@ export function isLoopback(addr: Raw): boolean {
return Address.isSpecial(addr, ['loopback', 'unspecified', 'linklocal'])
}

export function loopback(family: string | number = 4): string {
export function loopback(family: FamilyAlias = 4): string {
const fam = Address.normalizeFamily(family)
return fam === 4 ? IPV4_LB : IPV6_LB
}
16 changes: 8 additions & 8 deletions src/main/ts/native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from 'node:os'
import { isLoopback, isPrivate, isPublic, loopback, Address } from './core.ts'
import { isLoopback, isPrivate, isPublic, loopback, Address, type FamilyAlias } from './core.ts'

export {
isIP,
Expand All @@ -13,17 +13,17 @@ const PUBLIC = 'public'
const PRIVATE = 'private'

const {normalizeFamily} = Address
export const addresses = (name?: string, family: string | number = 4): string[] => {
export const addresses = (kind?: string, family: FamilyAlias = 4): string[] => {
const fam = normalizeFamily(family)
const interfaces = os.networkInterfaces()
const check =
name === PUBLIC ? isPublic
: name === PRIVATE ? isPrivate
kind === PUBLIC ? isPublic
: kind === PRIVATE ? isPrivate
: () => true

// specific NIC requested
if (name && name !== PRIVATE && name !== PUBLIC) {
const nic = interfaces[name]
if (kind && kind !== PRIVATE && kind !== PUBLIC) {
const nic = interfaces[kind]
if (!nic) return []
const match = nic.find(details => normalizeFamily(details.family) === fam)
return [match?.address!]
Expand All @@ -42,5 +42,5 @@ export const addresses = (name?: string, family: string | number = 4): string[]
return all.length ? all : [loopback(fam)]
}

export const address = (name?: string, family?: string): string | undefined =>
addresses(name, family)[0]
export const address = (kind?: string, family?: FamilyAlias): string | undefined =>
addresses(kind, family)[0]
10 changes: 5 additions & 5 deletions target/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ var import_node_net = require("net");
var PUBLIC = "public";
var PRIVATE = "private";
var { normalizeFamily } = import_core.Address;
var addresses = (name, family = 4) => {
var addresses = (kind, family = 4) => {
const fam = normalizeFamily(family);
const interfaces = import_node_os.default.networkInterfaces();
const check = name === PUBLIC ? import_core.isPublic : name === PRIVATE ? import_core.isPrivate : () => true;
if (name && name !== PRIVATE && name !== PUBLIC) {
const nic = interfaces[name];
const check = kind === PUBLIC ? import_core.isPublic : kind === PRIVATE ? import_core.isPrivate : () => true;
if (kind && kind !== PRIVATE && kind !== PUBLIC) {
const nic = interfaces[kind];
if (!nic) return [];
const match = nic.find((details) => normalizeFamily(details.family) === fam);
return [match == null ? void 0 : match.address];
Expand All @@ -77,7 +77,7 @@ var addresses = (name, family = 4) => {
}, []);
return all.length ? all : [(0, import_core.loopback)(fam)];
};
var address = (name, family) => addresses(name, family)[0];
var address = (kind, family) => addresses(kind, family)[0];

// src/main/ts/index.ts
var core = __toESM(require("./core.cjs"), 1);
Expand Down
9 changes: 5 additions & 4 deletions target/dts/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type BufferLike } from './polyfill.ts';
export type { BufferLike } from './polyfill.ts';
export type Special = 'loopback' | 'private' | 'linklocal' | 'multicast' | 'documentation' | 'reserved' | 'unspecified';
type Family = 4 | 6;
export type FamilyAlias = Family | '4' | '6' | 'ipv4' | 'ipv6' | 'IPV4' | 'IPV6' | 'IpV4' | 'IpV6';
type Raw = string | number | bigint | BufferLike | Array<number> | Address;
type Subnet = {
family: Family;
Expand All @@ -21,7 +22,7 @@ export declare class Address {
big: bigint;
toBuffer(buff?: BufferLike, offset?: number): BufferLike;
toArray(): number[];
toString(family?: Family | string | number, mapped?: boolean): string;
toString(family?: FamilyAlias, mapped?: boolean): string;
toLong(): number;
get range(): Special | undefined;
private static create;
Expand All @@ -33,7 +34,7 @@ export declare class Address {
static not(addr: Raw): string;
static or(addrA: Raw, addrB: Raw): string;
static isEqual(addrA: Raw, addrB: Raw): boolean;
static fromPrefixLen: (prefixlen: number | `${number}` | string, family?: string | number) => Address;
static fromPrefixLen: (prefixlen: number | `${number}` | string, family?: FamilyAlias) => Address;
private static fromNumber;
private static fromLong;
private static fromBuffer;
Expand All @@ -55,7 +56,7 @@ export declare const not: typeof Address['not'];
export declare const or: typeof Address['or'];
export declare const cidr: typeof Address['cidr'];
export declare const normalizeToLong: typeof Address['normalizeToLong'];
export declare function fromPrefixLen(prefixlen: number, family?: string | number): string;
export declare function fromPrefixLen(prefixlen: number, family?: FamilyAlias): string;
type LegacySubnet = Omit<Subnet, 'numHosts' | 'length'> & {
numHosts: number | bigint;
length: number | bigint;
Expand All @@ -73,4 +74,4 @@ export declare const isIPv4: Checker;
export declare const isIPv6: Checker;
export declare const isIP: (addr: string) => 0 | Family;
export declare function isLoopback(addr: Raw): boolean;
export declare function loopback(family?: string | number): string;
export declare function loopback(family?: FamilyAlias): string;
8 changes: 4 additions & 4 deletions target/dts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export declare const ip: {
isIPv4: typeof native.isIPv4;
isV4Format: typeof native.isIPv4;
isV6Format: typeof native.isIPv6;
addresses: (name?: string, family?: string | number) => string[];
address: (name?: string, family?: string) => string | undefined;
fromPrefixLen(prefixlen: number, family?: string | number): string;
addresses: (kind?: string, family?: core.FamilyAlias) => string[];
address: (kind?: string, family?: core.FamilyAlias) => string | undefined;
fromPrefixLen(prefixlen: number, family?: core.FamilyAlias): string;
subnet(addr: string | number | bigint | number[] | core.BufferLike | core.Address, smask: string | number | bigint | number[] | core.BufferLike | core.Address): Omit<{
family: 4 | 6;
networkAddress: string;
Expand Down Expand Up @@ -46,7 +46,7 @@ export declare const ip: {
toLong(addr: string | number | bigint | number[] | core.BufferLike | core.Address): number;
fromLong(n: number | bigint | `${bigint}`): string;
isLoopback(addr: string | number | bigint | number[] | core.BufferLike | core.Address): boolean;
loopback(family?: string | number): string;
loopback(family?: core.FamilyAlias): string;
Address: typeof core.Address;
isPublic: (typeof core.Address)["isPublic"];
isPrivate: (typeof core.Address)["isPrivate"];
Expand Down
5 changes: 3 additions & 2 deletions target/dts/native.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type FamilyAlias } from './core.ts';
export { isIP, isIPv6, isIPv4, isIPv4 as isV4Format, isIPv6 as isV6Format, } from 'node:net';
export declare const addresses: (name?: string, family?: string | number) => string[];
export declare const address: (name?: string, family?: string) => string | undefined;
export declare const addresses: (kind?: string, family?: FamilyAlias) => string[];
export declare const address: (kind?: string, family?: FamilyAlias) => string | undefined;
10 changes: 5 additions & 5 deletions target/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import {
var PUBLIC = "public";
var PRIVATE = "private";
var { normalizeFamily } = Address;
var addresses = (name, family = 4) => {
var addresses = (kind, family = 4) => {
const fam = normalizeFamily(family);
const interfaces = os.networkInterfaces();
const check = name === PUBLIC ? isPublic : name === PRIVATE ? isPrivate : () => true;
if (name && name !== PRIVATE && name !== PUBLIC) {
const nic = interfaces[name];
const check = kind === PUBLIC ? isPublic : kind === PRIVATE ? isPrivate : () => true;
if (kind && kind !== PRIVATE && kind !== PUBLIC) {
const nic = interfaces[kind];
if (!nic) return [];
const match = nic.find((details) => normalizeFamily(details.family) === fam);
return [match == null ? void 0 : match.address];
Expand All @@ -47,7 +47,7 @@ var addresses = (name, family = 4) => {
}, []);
return all.length ? all : [loopback(fam)];
};
var address = (name, family) => addresses(name, family)[0];
var address = (kind, family) => addresses(kind, family)[0];

// src/main/ts/index.ts
import * as core from "./core.mjs";
Expand Down
Loading