@ac-kit/net-address 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @ac-kit/net-address
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 99ca4b9: - `parseIpAddress(text)` and `tryParseIpAddress(text)`: an IP address of
8
+ either family, strictly — no brackets, zone, port or prefix.
9
+ - `printIpAddress(address)`: the dotted quad or the RFC 5952 form, by width.
10
+ - `canonicalIpAddress(address)`: an IPv4-mapped IPv6 address as its IPv4
11
+ address, any other address unchanged, so one host keys alike over either
12
+ family.
13
+
14
+ ### Patch Changes
15
+
16
+ - Updated dependencies [99ca4b9]
17
+ - @ac-kit/core@0.8.0
18
+ - @ac-kit/algo@0.3.1
19
+ - @ac-kit/math-numbers@0.2.5
20
+
3
21
  ## 0.3.1
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -48,7 +48,8 @@ The same reasoning runs through the rest of the package. `parsePort` rejects
48
48
  `080`. `parseEndpoint` refuses an unbracketed IPv6 literal, because `::1:8080`
49
49
  has no single reading. An all-digit host such as `2130706433` stays a *name*,
50
50
  since only a resolver can say what it points at — classify what the resolver
51
- returns, never the string.
51
+ returns, never the string. `parseIpAddress` takes either family, and nothing
52
+ else: no brackets, zone, port or prefix.
52
53
 
53
54
  ## Printing is canonical
54
55
 
@@ -61,6 +62,11 @@ tie, never for a single group — and IPv4-mapped addresses in dotted form.
61
62
  `EUI_PRINT_IETF` when comparing against IETF-style strings; `EUI_PRINT_IEEE`
62
63
  names the default.
63
64
 
65
+ One host can still have two addresses: a dual-stack socket reports an IPv4
66
+ peer as `::ffff:192.0.2.1`. `canonicalIpAddress` turns such an address into its
67
+ IPv4 one, so that keying, counting or comparing by client address sees one
68
+ client; `printIpAddress` then prints either family.
69
+
64
70
  See the
65
71
  [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/net-address/)
66
72
  for the full reference.
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./eui/eui-convert.js";
5
5
  export * from "./eui/interface-identifier.js";
6
6
  export * from "./eui/parse-eui.js";
7
7
  export * from "./eui/print-eui.js";
8
+ export * from "./ip/ip-address.js";
8
9
  export * from "./ipv4/classify-ipv4.js";
9
10
  export * from "./ipv4/constants.js";
10
11
  export * from "./ipv4/ipv4-prefix.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export * from "./eui/eui-convert.js";
5
5
  export * from "./eui/interface-identifier.js";
6
6
  export * from "./eui/parse-eui.js";
7
7
  export * from "./eui/print-eui.js";
8
+ export * from "./ip/ip-address.js";
8
9
  export * from "./ipv4/classify-ipv4.js";
9
10
  export * from "./ipv4/constants.js";
10
11
  export * from "./ipv4/ipv4-prefix.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,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BAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,44 @@
1
+ import type { BitAddress } from "../prefix/bit-address.js";
2
+ /**
3
+ * Parses an IP address of either family: a strict dotted quad, 32 bits wide, or
4
+ * an RFC 4291 literal, 128 bits wide. Reach for it where a peer may be either —
5
+ * a socket's remote address, a forwarded client — and for the family-specific
6
+ * parsers where only one is valid.
7
+ *
8
+ * Brackets, a zone identifier, a port and a prefix are all rejected:
9
+ * `parseEndpoint` takes the first three, `parseIpv6ScopedAddress` a zone
10
+ * alone.
11
+ *
12
+ * @throws {NetAddressSyntaxError} When `text` is neither.
13
+ */
14
+ export declare function parseIpAddress(text: string): BitAddress;
15
+ /** {@link parseIpAddress}, answering `null` instead of throwing. O(1). */
16
+ export declare function tryParseIpAddress(text: string): BitAddress | null;
17
+ /**
18
+ * Prints an IP address by its width: the dotted quad, or the RFC 5952 form.
19
+ *
20
+ * Two spellings of one address print alike, but an IPv4-mapped IPv6 address
21
+ * stays distinct from its IPv4 address; pass it through
22
+ * {@link canonicalIpAddress} first for that.
23
+ *
24
+ * @throws {RangeError} When the address is neither 32 nor 128 bits wide.
25
+ */
26
+ export declare function printIpAddress(address: BitAddress): string;
27
+ /**
28
+ * The address one host is known by whichever family reached it: an IPv4-mapped
29
+ * IPv6 address (`::ffff:192.0.2.1`), as a dual-stack socket reports an IPv4
30
+ * peer, becomes its IPv4 address; any other address is returned as it is. Apply
31
+ * it before comparing, counting or keying by client address, or one client is
32
+ * two.
33
+ *
34
+ * The deprecated IPv4-compatible form (`::192.0.2.1`) is left alone: no socket
35
+ * reports a peer that way, and `ipv4FromIpv6` unwraps it where wanted.
36
+ *
37
+ * @example
38
+ * printIpAddress(canonicalIpAddress(parseIpAddress("::ffff:192.0.2.1")));
39
+ * // "192.0.2.1"
40
+ *
41
+ * @throws {RangeError} When the address is neither 32 nor 128 bits wide.
42
+ */
43
+ export declare function canonicalIpAddress(address: BitAddress): BitAddress;
44
+ //# sourceMappingURL=ip-address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ip-address.d.ts","sourceRoot":"","sources":["../../src/ip/ip-address.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAQvD;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAEjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAW1D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU,CAOlE"}
@@ -0,0 +1,73 @@
1
+ import { NetAddressSyntaxError } from "../errors.js";
2
+ import { IPV4_BIT_WIDTH } from "../ipv4/constants.js";
3
+ import { tryParseIpv4Address } from "../ipv4/parse-ipv4.js";
4
+ import { printIpv4Address } from "../ipv4/print-ipv4.js";
5
+ import { IPV6_BIT_WIDTH } from "../ipv6/constants.js";
6
+ import { ipv4FromIpv6, isIpv6Ipv4Mapped } from "../ipv6/ipv4-mapped.js";
7
+ import { tryParseIpv6Address } from "../ipv6/parse-ipv6.js";
8
+ import { printIpv6Address } from "../ipv6/print-ipv6.js";
9
+ /**
10
+ * Parses an IP address of either family: a strict dotted quad, 32 bits wide, or
11
+ * an RFC 4291 literal, 128 bits wide. Reach for it where a peer may be either —
12
+ * a socket's remote address, a forwarded client — and for the family-specific
13
+ * parsers where only one is valid.
14
+ *
15
+ * Brackets, a zone identifier, a port and a prefix are all rejected:
16
+ * `parseEndpoint` takes the first three, `parseIpv6ScopedAddress` a zone
17
+ * alone.
18
+ *
19
+ * @throws {NetAddressSyntaxError} When `text` is neither.
20
+ */
21
+ export function parseIpAddress(text) {
22
+ const address = tryParseIpAddress(text);
23
+ if (address === null) {
24
+ throw new NetAddressSyntaxError(text, "not an IP address");
25
+ }
26
+ return address;
27
+ }
28
+ /** {@link parseIpAddress}, answering `null` instead of throwing. O(1). */
29
+ export function tryParseIpAddress(text) {
30
+ return tryParseIpv4Address(text) ?? tryParseIpv6Address(text);
31
+ }
32
+ /**
33
+ * Prints an IP address by its width: the dotted quad, or the RFC 5952 form.
34
+ *
35
+ * Two spellings of one address print alike, but an IPv4-mapped IPv6 address
36
+ * stays distinct from its IPv4 address; pass it through
37
+ * {@link canonicalIpAddress} first for that.
38
+ *
39
+ * @throws {RangeError} When the address is neither 32 nor 128 bits wide.
40
+ */
41
+ export function printIpAddress(address) {
42
+ if (address.bitWidth === IPV4_BIT_WIDTH) {
43
+ return printIpv4Address(address);
44
+ }
45
+ if (address.bitWidth === IPV6_BIT_WIDTH) {
46
+ return printIpv6Address(address);
47
+ }
48
+ throw new RangeError(`An IP address is 32 or 128 bits wide, got ${address.bitWidth}`);
49
+ }
50
+ /**
51
+ * The address one host is known by whichever family reached it: an IPv4-mapped
52
+ * IPv6 address (`::ffff:192.0.2.1`), as a dual-stack socket reports an IPv4
53
+ * peer, becomes its IPv4 address; any other address is returned as it is. Apply
54
+ * it before comparing, counting or keying by client address, or one client is
55
+ * two.
56
+ *
57
+ * The deprecated IPv4-compatible form (`::192.0.2.1`) is left alone: no socket
58
+ * reports a peer that way, and `ipv4FromIpv6` unwraps it where wanted.
59
+ *
60
+ * @example
61
+ * printIpAddress(canonicalIpAddress(parseIpAddress("::ffff:192.0.2.1")));
62
+ * // "192.0.2.1"
63
+ *
64
+ * @throws {RangeError} When the address is neither 32 nor 128 bits wide.
65
+ */
66
+ export function canonicalIpAddress(address) {
67
+ if (address.bitWidth === IPV4_BIT_WIDTH) {
68
+ return address;
69
+ }
70
+ // Non-null: a mapped address always embeds an IPv4 address.
71
+ return isIpv6Ipv4Mapped(address) ? ipv4FromIpv6(address) : address;
72
+ }
73
+ //# sourceMappingURL=ip-address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ip-address.js","sourceRoot":"","sources":["../../src/ip/ip-address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAGzD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IAC1C,MAAM,OAAO,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAExC,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,MAAM,IAAI,qBAAqB,CAAC,IAAI,EAAE,mBAAmB,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,OAAO,CAAC;AAChB,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC7C,OAAO,mBAAmB,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,OAAmB;IACjD,IAAI,OAAO,CAAC,QAAQ,KAAK,cAAc,EAAE,CAAC;QACzC,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IACD,IAAI,OAAO,CAAC,QAAQ,KAAK,cAAc,EAAE,CAAC;QACzC,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IAED,MAAM,IAAI,UAAU,CACnB,6CAA6C,OAAO,CAAC,QAAQ,EAAE,CAC/D,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAmB;IACrD,IAAI,OAAO,CAAC,QAAQ,KAAK,cAAc,EAAE,CAAC;QACzC,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,4DAA4D;IAC5D,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AACrE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/net-address",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Network addresses: IPv4, IPv6, EUI-48/EUI-64 and ports, over one fixed-width prefix core.",
5
5
  "keywords": [
6
6
  "cidr",
@@ -46,10 +46,10 @@
46
46
  "prepack": "yarn build"
47
47
  },
48
48
  "dependencies": {
49
- "@ac-kit/algo": "^0.3.0",
50
- "@ac-kit/core": "^0.7.0",
51
- "@ac-kit/math-numbers": "^0.2.4",
52
- "@types/web": "^0.0.365"
49
+ "@ac-kit/algo": "^0.3.1",
50
+ "@ac-kit/core": "^0.8.0",
51
+ "@ac-kit/math-numbers": "^0.2.5",
52
+ "@types/web": "^0.0.367"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@ac-kit/tsconfig": "0.1.0",