diff --git a/.size-limit.json b/.size-limit.json index 109be9f..40f99a2 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -9,7 +9,7 @@ "LICENSE", "package-main.json" ], - "limit": "60.20 kB", + "limit": "60.31 kB", "brotli": false, "gzip": false }, @@ -18,7 +18,7 @@ "path": [ "target/*/core.*" ], - "limit": "41.15 kB", + "limit": "41.25 kB", "brotli": false, "gzip": false }, @@ -32,7 +32,7 @@ "LICENSE", "package-main.json" ], - "limit": "18.25 kB", + "limit": "18.30 kB", "gzip": true }, { @@ -56,7 +56,7 @@ { "name": "libdefs", "path": "target/dts", - "limit": "7.00 kB", + "limit": "7.15 kB", "brotli": false, "gzip": false } diff --git a/src/main/ts/core.ts b/src/main/ts/core.ts index f53f968..bf632fc 100644 --- a/src/main/ts/core.ts +++ b/src/main/ts/core.ts @@ -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 | Address type Subnet = { family: Family @@ -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 @@ -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 @@ -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() } @@ -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 } diff --git a/src/main/ts/native.ts b/src/main/ts/native.ts index 990ea48..9f6fe60 100644 --- a/src/main/ts/native.ts +++ b/src/main/ts/native.ts @@ -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, @@ -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!] @@ -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] \ No newline at end of file +export const address = (kind?: string, family?: FamilyAlias): string | undefined => + addresses(kind, family)[0] diff --git a/target/cjs/index.cjs b/target/cjs/index.cjs index cb57873..04a30e4 100644 --- a/target/cjs/index.cjs +++ b/target/cjs/index.cjs @@ -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]; @@ -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); diff --git a/target/dts/core.d.ts b/target/dts/core.d.ts index c9496d9..4146a58 100644 --- a/target/dts/core.d.ts +++ b/target/dts/core.d.ts @@ -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 | Address; type Subnet = { family: Family; @@ -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; @@ -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; @@ -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 & { numHosts: number | bigint; length: number | bigint; @@ -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; diff --git a/target/dts/index.d.ts b/target/dts/index.d.ts index a30fd5c..83a8b98 100644 --- a/target/dts/index.d.ts +++ b/target/dts/index.d.ts @@ -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; @@ -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"]; diff --git a/target/dts/native.d.ts b/target/dts/native.d.ts index a6e60df..72710b9 100644 --- a/target/dts/native.d.ts +++ b/target/dts/native.d.ts @@ -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; diff --git a/target/esm/index.mjs b/target/esm/index.mjs index ee1a924..03ef6eb 100644 --- a/target/esm/index.mjs +++ b/target/esm/index.mjs @@ -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]; @@ -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";