Skip to content

Commit 0ff2aca

Browse files
authored
refactor: enhance Family type (#27)
1 parent a0c6fa1 commit 0ff2aca

8 files changed

Lines changed: 39 additions & 36 deletions

File tree

.size-limit.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"LICENSE",
1010
"package-main.json"
1111
],
12-
"limit": "60.20 kB",
12+
"limit": "60.31 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},
@@ -18,7 +18,7 @@
1818
"path": [
1919
"target/*/core.*"
2020
],
21-
"limit": "41.15 kB",
21+
"limit": "41.25 kB",
2222
"brotli": false,
2323
"gzip": false
2424
},
@@ -32,7 +32,7 @@
3232
"LICENSE",
3333
"package-main.json"
3434
],
35-
"limit": "18.25 kB",
35+
"limit": "18.30 kB",
3636
"gzip": true
3737
},
3838
{
@@ -56,7 +56,7 @@
5656
{
5757
"name": "libdefs",
5858
"path": "target/dts",
59-
"limit": "7.00 kB",
59+
"limit": "7.15 kB",
6060
"brotli": false,
6161
"gzip": false
6262
}

src/main/ts/core.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const PRIVATES: Special[] = ['private', 'linklocal', 'loopback', 'unspecified']
7070
// -------------------------------------------------------
7171

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

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

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

288289
const len = +prefixlen | 0
@@ -515,7 +516,7 @@ export const or: typeof Address['or'] = Address.or.bind(Address)
515516
export const cidr: typeof Address['cidr'] = Address.cidr.bind(Address)
516517
export const normalizeToLong: typeof Address['normalizeToLong'] = Address.normalizeToLong.bind(Address)
517518

518-
export function fromPrefixLen(prefixlen: number, family?: string | number): string {
519+
export function fromPrefixLen(prefixlen: number, family?: FamilyAlias): string {
519520
return Address.fromPrefixLen(prefixlen, family).toString()
520521
}
521522

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

582-
export function loopback(family: string | number = 4): string {
583+
export function loopback(family: FamilyAlias = 4): string {
583584
const fam = Address.normalizeFamily(family)
584585
return fam === 4 ? IPV4_LB : IPV6_LB
585586
}

src/main/ts/native.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os from 'node:os'
2-
import { isLoopback, isPrivate, isPublic, loopback, Address } from './core.ts'
2+
import { isLoopback, isPrivate, isPublic, loopback, Address, type FamilyAlias } from './core.ts'
33

44
export {
55
isIP,
@@ -13,17 +13,17 @@ const PUBLIC = 'public'
1313
const PRIVATE = 'private'
1414

1515
const {normalizeFamily} = Address
16-
export const addresses = (name?: string, family: string | number = 4): string[] => {
16+
export const addresses = (kind?: string, family: FamilyAlias = 4): string[] => {
1717
const fam = normalizeFamily(family)
1818
const interfaces = os.networkInterfaces()
1919
const check =
20-
name === PUBLIC ? isPublic
21-
: name === PRIVATE ? isPrivate
20+
kind === PUBLIC ? isPublic
21+
: kind === PRIVATE ? isPrivate
2222
: () => true
2323

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

45-
export const address = (name?: string, family?: string): string | undefined =>
46-
addresses(name, family)[0]
45+
export const address = (kind?: string, family?: FamilyAlias): string | undefined =>
46+
addresses(kind, family)[0]

target/cjs/index.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ var import_node_net = require("net");
5757
var PUBLIC = "public";
5858
var PRIVATE = "private";
5959
var { normalizeFamily } = import_core.Address;
60-
var addresses = (name, family = 4) => {
60+
var addresses = (kind, family = 4) => {
6161
const fam = normalizeFamily(family);
6262
const interfaces = import_node_os.default.networkInterfaces();
63-
const check = name === PUBLIC ? import_core.isPublic : name === PRIVATE ? import_core.isPrivate : () => true;
64-
if (name && name !== PRIVATE && name !== PUBLIC) {
65-
const nic = interfaces[name];
63+
const check = kind === PUBLIC ? import_core.isPublic : kind === PRIVATE ? import_core.isPrivate : () => true;
64+
if (kind && kind !== PRIVATE && kind !== PUBLIC) {
65+
const nic = interfaces[kind];
6666
if (!nic) return [];
6767
const match = nic.find((details) => normalizeFamily(details.family) === fam);
6868
return [match == null ? void 0 : match.address];
@@ -77,7 +77,7 @@ var addresses = (name, family = 4) => {
7777
}, []);
7878
return all.length ? all : [(0, import_core.loopback)(fam)];
7979
};
80-
var address = (name, family) => addresses(name, family)[0];
80+
var address = (kind, family) => addresses(kind, family)[0];
8181

8282
// src/main/ts/index.ts
8383
var core = __toESM(require("./core.cjs"), 1);

target/dts/core.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type BufferLike } from './polyfill.ts';
22
export type { BufferLike } from './polyfill.ts';
33
export type Special = 'loopback' | 'private' | 'linklocal' | 'multicast' | 'documentation' | 'reserved' | 'unspecified';
44
type Family = 4 | 6;
5+
export type FamilyAlias = Family | '4' | '6' | 'ipv4' | 'ipv6' | 'IPV4' | 'IPV6' | 'IpV4' | 'IpV6';
56
type Raw = string | number | bigint | BufferLike | Array<number> | Address;
67
type Subnet = {
78
family: Family;
@@ -21,7 +22,7 @@ export declare class Address {
2122
big: bigint;
2223
toBuffer(buff?: BufferLike, offset?: number): BufferLike;
2324
toArray(): number[];
24-
toString(family?: Family | string | number, mapped?: boolean): string;
25+
toString(family?: FamilyAlias, mapped?: boolean): string;
2526
toLong(): number;
2627
get range(): Special | undefined;
2728
private static create;
@@ -33,7 +34,7 @@ export declare class Address {
3334
static not(addr: Raw): string;
3435
static or(addrA: Raw, addrB: Raw): string;
3536
static isEqual(addrA: Raw, addrB: Raw): boolean;
36-
static fromPrefixLen: (prefixlen: number | `${number}` | string, family?: string | number) => Address;
37+
static fromPrefixLen: (prefixlen: number | `${number}` | string, family?: FamilyAlias) => Address;
3738
private static fromNumber;
3839
private static fromLong;
3940
private static fromBuffer;
@@ -55,7 +56,7 @@ export declare const not: typeof Address['not'];
5556
export declare const or: typeof Address['or'];
5657
export declare const cidr: typeof Address['cidr'];
5758
export declare const normalizeToLong: typeof Address['normalizeToLong'];
58-
export declare function fromPrefixLen(prefixlen: number, family?: string | number): string;
59+
export declare function fromPrefixLen(prefixlen: number, family?: FamilyAlias): string;
5960
type LegacySubnet = Omit<Subnet, 'numHosts' | 'length'> & {
6061
numHosts: number | bigint;
6162
length: number | bigint;
@@ -73,4 +74,4 @@ export declare const isIPv4: Checker;
7374
export declare const isIPv6: Checker;
7475
export declare const isIP: (addr: string) => 0 | Family;
7576
export declare function isLoopback(addr: Raw): boolean;
76-
export declare function loopback(family?: string | number): string;
77+
export declare function loopback(family?: FamilyAlias): string;

target/dts/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export declare const ip: {
88
isIPv4: typeof native.isIPv4;
99
isV4Format: typeof native.isIPv4;
1010
isV6Format: typeof native.isIPv6;
11-
addresses: (name?: string, family?: string | number) => string[];
12-
address: (name?: string, family?: string) => string | undefined;
13-
fromPrefixLen(prefixlen: number, family?: string | number): string;
11+
addresses: (kind?: string, family?: core.FamilyAlias) => string[];
12+
address: (kind?: string, family?: core.FamilyAlias) => string | undefined;
13+
fromPrefixLen(prefixlen: number, family?: core.FamilyAlias): string;
1414
subnet(addr: string | number | bigint | number[] | core.BufferLike | core.Address, smask: string | number | bigint | number[] | core.BufferLike | core.Address): Omit<{
1515
family: 4 | 6;
1616
networkAddress: string;
@@ -46,7 +46,7 @@ export declare const ip: {
4646
toLong(addr: string | number | bigint | number[] | core.BufferLike | core.Address): number;
4747
fromLong(n: number | bigint | `${bigint}`): string;
4848
isLoopback(addr: string | number | bigint | number[] | core.BufferLike | core.Address): boolean;
49-
loopback(family?: string | number): string;
49+
loopback(family?: core.FamilyAlias): string;
5050
Address: typeof core.Address;
5151
isPublic: (typeof core.Address)["isPublic"];
5252
isPrivate: (typeof core.Address)["isPrivate"];

target/dts/native.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type FamilyAlias } from './core.ts';
12
export { isIP, isIPv6, isIPv4, isIPv4 as isV4Format, isIPv6 as isV6Format, } from 'node:net';
2-
export declare const addresses: (name?: string, family?: string | number) => string[];
3-
export declare const address: (name?: string, family?: string) => string | undefined;
3+
export declare const addresses: (kind?: string, family?: FamilyAlias) => string[];
4+
export declare const address: (kind?: string, family?: FamilyAlias) => string | undefined;

target/esm/index.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import {
2727
var PUBLIC = "public";
2828
var PRIVATE = "private";
2929
var { normalizeFamily } = Address;
30-
var addresses = (name, family = 4) => {
30+
var addresses = (kind, family = 4) => {
3131
const fam = normalizeFamily(family);
3232
const interfaces = os.networkInterfaces();
33-
const check = name === PUBLIC ? isPublic : name === PRIVATE ? isPrivate : () => true;
34-
if (name && name !== PRIVATE && name !== PUBLIC) {
35-
const nic = interfaces[name];
33+
const check = kind === PUBLIC ? isPublic : kind === PRIVATE ? isPrivate : () => true;
34+
if (kind && kind !== PRIVATE && kind !== PUBLIC) {
35+
const nic = interfaces[kind];
3636
if (!nic) return [];
3737
const match = nic.find((details) => normalizeFamily(details.family) === fam);
3838
return [match == null ? void 0 : match.address];
@@ -47,7 +47,7 @@ var addresses = (name, family = 4) => {
4747
}, []);
4848
return all.length ? all : [loopback(fam)];
4949
};
50-
var address = (name, family) => addresses(name, family)[0];
50+
var address = (kind, family) => addresses(kind, family)[0];
5151

5252
// src/main/ts/index.ts
5353
import * as core from "./core.mjs";

0 commit comments

Comments
 (0)