net-address 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Luolapeikko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # net-address
2
+
3
+ ## Runtime agnostic core IpAddr classes base on Rust implementation.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i net-address @luolapeikko/result-option
9
+ ```
10
+
11
+ ## Examples
12
+
13
+ ```typescript
14
+ const addrFromString = Ipv4Addr.from("192.168.0.1").unwrap();
15
+ const addr = new Ipv4Addr(192, 168, 0, 1);
16
+ if (addr.isPrivate()) {
17
+ //
18
+ }
19
+ if (addr.isGlobal()) {
20
+ //
21
+ }
22
+ ```
23
+
24
+ ## Full [Documentation](https://luolapeikko.github.io/net-address-base/)
25
+
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@luolapeikko/result-option`);var t=class t{static regex=/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;static from(n){if(!t.regex.test(n))return(0,e.Err)(TypeError(`${n} is invalid ipv6 value`));try{let{leftParts:r,rightParts:i,embeddedIpv4:a}=t.#e(n),o=r.map(e=>parseInt(e,16)),s=i.map(e=>parseInt(e,16)),c=8-(o.length+s.length+a.length);return(0,e.Ok)(new t([...o,...Array(c).fill(0),...s,...a]))}catch(t){return(0,e.Err)(t)}}static fromBuffer(n,r){try{let i=new DataView(n);if(r){let n=i.getBigUint64(0,!0);return(0,e.Ok)(new t(i.getBigUint64(8,!0)<<64n|n))}else{let n=i.getBigUint64(0,!1),r=i.getBigUint64(8,!1);return(0,e.Ok)(new t(n<<64n|r))}}catch(t){return(0,e.Err)(t)}}static#e(e){let n=e.split(`::`),r=n[0].split(`:`).filter(e=>e!==``),i=n.length>1?n[1].split(`:`).filter(e=>e!==``):[],a=[],o=i[i.length-1]??r[r.length-1];return o?.includes(`.`)&&(a=t.#t(o),i.length>0?i.pop():r.pop()),{leftParts:r,rightParts:i,embeddedIpv4:a}}static#t(e){let t=e.split(`.`).map(Number);return[t[0]<<8|t[1],t[2]<<8|t[3]]}static BITS=128;static LOCALHOST=new t(1n);static UNSPECIFIED=new t(0n);family=`ipv6`;#n;constructor(e){Array.isArray(e)?this.#n=this.#r(e):this.#n=e}isBenchmarking(){return this.#a(42540488320432167789079031612388147200n,48)}isDocumentation(){return this.#a(42540766411282592856903984951653826560n,32)||this.#a(85065399433376081038215121361612832768n,20)}isLoopback(){return this.#n===1n}isUnspecified(){return this.#n===0n}isMulticast(){return this.#n>>120n==255n}isIpv4Mapped(){return this.#a(281470681743360n,96)}isUnicast(){return!this.isMulticast()}isUnicastLinkLocal(){return this.#a(338288524927261089654018896841347694592n,10)}isUnicastGlobal(){return this.isUnicast()&&!this.isLoopback()&&!this.isUnicastLinkLocal()&&!this.isUniqueLocal()&&!this.isUnspecified()&&!this.isDocumentation()}isGlobal(){return!this.isUnspecified()&&!this.isLoopback()&&!this.isIpv4Mapped()&&!this.isBenchmarking()&&!this.isDocumentation()&&!this.isUniqueLocal()&&!this.isUnicastLinkLocal()&&!this.isMulticast()}isUniqueLocal(){return this.#a(334965454937798799971759379190646833152n,7)}toIpv4(){if(this.isIpv4Mapped()||this.#a(0n,96)){let t=this.#i(this.#n);return(0,e.Some)(new n(t[6]>>8,t[6]&255,t[7]>>8,t[7]&255))}return(0,e.None)()}toIpv4Mapped(){if(this.isIpv4Mapped()){let t=this.#i(this.#n);return(0,e.Some)(new n(t[6]>>8,t[6]&255,t[7]>>8,t[7]&255))}return(0,e.None)()}toString(){let e=this.#i(this.#n),t=-1,n=0,r=-1,i=0;for(let a=0;a<e.length;a++)e[a]===0?(r===-1?(r=a,i=1):i++,i>n&&(n=i,t=r)):(r=-1,i=0);if(n<2)return e.map(e=>e.toString(16)).join(`:`);let a=e.slice(0,t).map(e=>e.toString(16)),o=e.slice(t+n).map(e=>e.toString(16));return`${a.join(`:`)}::${o.join(`:`)}`}toBuffer(e){let t=new ArrayBuffer(16),n=new DataView(t);return e?(n.setBigUint64(0,this.#n&18446744073709551615n,!0),n.setBigUint64(8,this.#n>>64n,!0)):(n.setBigUint64(0,this.#n>>64n,!1),n.setBigUint64(8,this.#n&18446744073709551615n,!1)),t}#r(e){let t=0n;for(let n of e){if(n<0||n>65535)throw Error(`IPv6 segment must be between 0 and 65535`);t=t<<16n|BigInt(n)}return t}#i(e){return[Number(e>>112n&65535n),Number(e>>96n&65535n),Number(e>>80n&65535n),Number(e>>64n&65535n),Number(e>>48n&65535n),Number(e>>32n&65535n),Number(e>>16n&65535n),Number(e&65535n)]}#a(e,t){if(t<0||t>128)throw Error(`Invalid prefix length`);if(t===0)return!0;let n=(1n<<128n)-1n,r=n<<BigInt(128-t)&n;return(this.#n&r)===(e&r)}},n=class n{static from(t){let r=t.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);if(!r)return(0,e.Err)(TypeError(`${t} is invalid ipv4 value`));let i=r.slice(1).map(Number);return i.some(e=>e>255)?(0,e.Err)(TypeError(`${t} is invalid ipv4 value`)):(0,e.Ok)(new n(i[0],i[1],i[2],i[3]))}static fromBuffer(t,r){try{return(0,e.Ok)(new n(new DataView(t).getUint32(0,r)))}catch(t){return(0,e.Err)(t)}}static BITS=32;static BROADCAST=new n(4294967295);static LOCALHOST=new n(2130706433);static UNSPECIFIED=new n(0);family=`ipv4`;#e;constructor(...e){e.length===1?this.#e=e[0]:this.#e=this.#t(e[0],e[1],e[2],e[3])}isBroadcast(){return this.#e===4294967295}isDocumentation(){return this.#r(3221225984,24)||this.#r(3325256704,24)||this.#r(3405803776,24)}isBenchmarking(){return this.#r(3323068416,15)}isGlobal(){return!(this.isUnspecified()||this.isPrivate()||this.isShared()||this.isLoopback()||this.isLinkLocal()||this.isDocumentation()||this.isBenchmarking()||this.isReserved()||this.isMulticast()||this.isBroadcast())}isLinkLocal(){return this.#r(2851995648,16)}isLoopback(){return this.#r(2130706432,8)}isMulticast(){return this.#r(3758096384,4)}isPrivate(){return this.#r(167772160,8)||this.#r(2886729728,12)||this.#r(3232235520,16)}isReserved(){return this.#r(4026531840,4)&&!this.isBroadcast()}isShared(){return this.#r(1681915904,10)}isUnspecified(){return this.#e===0}toIpv6(){return t.from(`::${this.toString()}`).unwrap()}toIpv6Mapped(){return t.from(`::ffff:${this.toString()}`).unwrap()}toString(){let[e,t,n,r]=this.#n(this.#e);return`${e}.${t}.${n}.${r}`}toBuffer(e){let t=new ArrayBuffer(4);return new DataView(t).setUint32(0,this.#e,e),t}#t(e,t,n,r){for(let i of[e,t,n,r])if(i<0||i>255)throw Error(`IPv4 octet must be between 0 and 255`);return(e<<24|t<<16|n<<8|r)>>>0}#n(e){return[e>>24&255,e>>16&255,e>>8&255,e&255]}#r(e,t){if(t<0||t>32)throw Error(`Invalid prefix length`);let n=t===0?0:-1<<32-t>>>0;return(this.#e&n)===(e&n)}};exports.Ipv4Addr=n,exports.Ipv6Addr=t;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["#parseComponents","#parseEmbeddedIpv4","#integerAddress","#toInteger","#match","#fromInteger","#integerAddress","#toInteger","#match","#fromInteger"],"sources":["../src/Ipv6Addr.ts","../src/Ipv4Addr.ts"],"sourcesContent":["import {Err, type IOption, type IResult, None, Ok, Some} from '@luolapeikko/result-option';\nimport {Ipv4Addr} from './Ipv4Addr';\n\n/**\n * Represents an IPv6 address.\n * @example\n * const addr1 = Ipv6Addr.from('2001:db8::1').unwrap();\n * const addr2 = new Ipv6Addr([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]);\n * const addr3 = new Ipv6Addr(0x20010db8000000000000000000000001n);\n * @since v0.0.1\n */\nexport class Ipv6Addr {\n\tprivate static regex =\n\t\t/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;\n\n\t/**\n\t * Creates an IPv6 address from text.\n\t * @returns A successful result with an IPv6 address, or an error when the input is invalid.\n\t * @example\n\t * const addr = Ipv6Addr.from('2001:db8::1').unwrap();\n\t * @since v0.0.1\n\t */\n\tpublic static from(value: string): IResult<Ipv6Addr> {\n\t\tif (!Ipv6Addr.regex.test(value)) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv6 value`));\n\t\t}\n\t\ttry {\n\t\t\tconst {leftParts, rightParts, embeddedIpv4} = Ipv6Addr.#parseComponents(value);\n\n\t\t\tconst leftSegs = leftParts.map((p) => parseInt(p, 16));\n\t\t\tconst rightSegs = rightParts.map((p) => parseInt(p, 16));\n\n\t\t\tconst missingLen = 8 - (leftSegs.length + rightSegs.length + embeddedIpv4.length);\n\t\t\tconst segments = [...leftSegs, ...Array(missingLen).fill(0), ...rightSegs, ...embeddedIpv4];\n\n\t\t\treturn Ok(new Ipv6Addr(segments as [number, number, number, number, number, number, number, number]));\n\t\t} catch (err) {\n\t\t\treturn Err(err);\n\t\t}\n\t}\n\n\t/**\n\t * Creates an IPv6 address from a 16-byte buffer.\n\t * @returns A successful result with an IPv6 address, or an error when the buffer cannot be read.\n\t * @since v0.0.1\n\t */\n\tpublic static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv6Addr> {\n\t\ttry {\n\t\t\tconst view = new DataView(buffer);\n\t\t\tif (littleEndian) {\n\t\t\t\tconst low = view.getBigUint64(0, true);\n\t\t\t\tconst high = view.getBigUint64(8, true);\n\t\t\t\treturn Ok(new Ipv6Addr((high << 64n) | low));\n\t\t\t} else {\n\t\t\t\tconst high = view.getBigUint64(0, false);\n\t\t\t\tconst low = view.getBigUint64(8, false);\n\t\t\t\treturn Ok(new Ipv6Addr((high << 64n) | low));\n\t\t\t}\n\t\t} catch (err) {\n\t\t\treturn Err(err as Error);\n\t\t}\n\t}\n\n\tstatic #parseComponents(value: string) {\n\t\tconst components = value.split('::');\n\t\tconst leftParts = components[0].split(':').filter((x) => x !== '');\n\t\tconst rightParts = components.length > 1 ? components[1].split(':').filter((x) => x !== '') : [];\n\t\tlet embeddedIpv4: number[] = [];\n\t\tconst lastPart = rightParts[rightParts.length - 1] ?? leftParts[leftParts.length - 1];\n\t\tif (lastPart?.includes('.')) {\n\t\t\tembeddedIpv4 = Ipv6Addr.#parseEmbeddedIpv4(lastPart);\n\t\t\tif (rightParts.length > 0) {\n\t\t\t\trightParts.pop();\n\t\t\t} else {\n\t\t\t\tleftParts.pop();\n\t\t\t}\n\t\t}\n\t\treturn {leftParts, rightParts, embeddedIpv4};\n\t}\n\n\tstatic #parseEmbeddedIpv4(value: string): number[] {\n\t\tconst ipv4Octets = value.split('.').map(Number);\n\t\treturn [(ipv4Octets[0] << 8) | ipv4Octets[1], (ipv4Octets[2] << 8) | ipv4Octets[3]];\n\t}\n\n\t/**\n\t * The number of bits in an IPv6 address.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BITS = 128;\n\n\t/**\n\t * The loopback address (`::1`).\n\t * @since v0.0.1\n\t */\n\tpublic static readonly LOCALHOST: Ipv6Addr = new Ipv6Addr(0x00000000000000000000000000000001n);\n\n\t/**\n\t * The unspecified address (`::`).\n\t * @since v0.0.1\n\t */\n\tpublic static readonly UNSPECIFIED: Ipv6Addr = new Ipv6Addr(0x00000000000000000000000000000000n);\n\n\t/**\n\t * The address family identifier for IPv6 addresses.\n\t * @since v0.0.1\n\t */\n\tpublic readonly family = 'ipv6';\n\n\t#integerAddress: bigint;\n\tpublic constructor(value: bigint);\n\tpublic constructor(segments: [number, number, number, number, number, number, number, number]);\n\tpublic constructor(valueOrSegments: [number, number, number, number, number, number, number, number] | bigint) {\n\t\tif (Array.isArray(valueOrSegments)) {\n\t\t\tthis.#integerAddress = this.#toInteger(valueOrSegments);\n\t\t} else {\n\t\t\tthis.#integerAddress = valueOrSegments;\n\t\t}\n\t}\n\n\t/**\n\t * Checks whether this address is in the benchmarking range.\n\t * @see https://tools.ietf.org/html/rfc5180 and https://www.rfc-editor.org/errata_search.php?eid=1752\n\t * @returns `true` when the address is in `2001:2::/48`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBenchmarking(): boolean {\n\t\treturn this.#match(0x20010002000000000000000000000000n, 48);\n\t}\n\n\t/**\n\t * Checks whether this address is in a documentation-only range.\n\t * @see https://tools.ietf.org/html/rfc3849 and https://tools.ietf.org/html/rfc9637\n\t * @returns `true` for `2001:db8::/32` or `3fff::/20`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isDocumentation(): boolean {\n\t\treturn this.#match(0x20010db8000000000000000000000000n, 32) || this.#match(0x3fff0000000000000000000000000000n, 20);\n\t}\n\n\t/**\n\t * Checks whether this address is the loopback address.\n\t * @see https://tools.ietf.org/html/rfc4291#section-2.5.3\n\t * @returns `true` when the address is `::1`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLoopback(): boolean {\n\t\treturn this.#integerAddress === 1n;\n\t}\n\n\t/**\n\t * Checks whether this address is the unspecified address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is `::`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnspecified(): boolean {\n\t\treturn this.#integerAddress === 0n;\n\t}\n\n\t/**\n\t * Checks whether this address is a multicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is in `ff00::/8`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isMulticast(): boolean {\n\t\treturn this.#integerAddress >> 120n === 0xffn;\n\t}\n\n\t/**\n\t * Checks whether this address is an IPv4-mapped IPv6 address.\n\t * @returns `true` when the address is in `::ffff:0:0/96`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isIpv4Mapped(): boolean {\n\t\treturn this.#match(0x00000000000000000000ffff00000000n, 96);\n\t}\n\n\t/**\n\t * Checks whether this address is a unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is not multicast, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicast(): boolean {\n\t\treturn !this.isMulticast();\n\t}\n\n\t/**\n\t * Checks whether this address is a link-local unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is in `fe80::/10`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicastLinkLocal(): boolean {\n\t\treturn this.#match(0xfe800000000000000000000000000000n, 10);\n\t}\n\n\t/**\n\t * Checks whether this address is a global unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291#section-2.5.7\n\t * @returns `true` when the address is unicast and not loopback, link-local, private, unspecified, or documentation.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicastGlobal(): boolean {\n\t\treturn this.isUnicast() && !this.isLoopback() && !this.isUnicastLinkLocal() && !this.isUniqueLocal() && !this.isUnspecified() && !this.isDocumentation();\n\t}\n\n\t/**\n\t * Checks whether this address appears globally reachable.\n\t * @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml\n\t * @returns `true` when the address is not in known non-global special ranges.\n\t * @since v0.0.1\n\t */\n\tpublic isGlobal(): boolean {\n\t\treturn (\n\t\t\t!this.isUnspecified() &&\n\t\t\t!this.isLoopback() &&\n\t\t\t!this.isIpv4Mapped() &&\n\t\t\t!this.isBenchmarking() &&\n\t\t\t!this.isDocumentation() &&\n\t\t\t!this.isUniqueLocal() &&\n\t\t\t!this.isUnicastLinkLocal() &&\n\t\t\t!this.isMulticast()\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the unique-local range.\n\t * @see https://tools.ietf.org/html/rfc4193\n\t * @returns `true` when the address is in `fc00::/7`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUniqueLocal(): boolean {\n\t\treturn this.#match(0xfc000000000000000000000000000000n, 7);\n\t}\n\n\t/**\n\t * Converts this address to IPv4 when compatible or mapped.\n\t * @returns An IPv4 address for `::a.b.c.d` or `::ffff:a.b.c.d`; otherwise `None`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv4(): IOption<Ipv4Addr> {\n\t\tif (this.isIpv4Mapped() || this.#match(0n, 96)) {\n\t\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\t\treturn Some(new Ipv4Addr(segments[6] >> 8, segments[6] & 0xff, segments[7] >> 8, segments[7] & 0xff));\n\t\t}\n\t\treturn None();\n\t}\n\n\t/**\n\t * Converts this address to IPv4 only when it is IPv4-mapped.\n\t * @returns An IPv4 address for `::ffff:a.b.c.d`; otherwise `None`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv4Mapped(): IOption<Ipv4Addr> {\n\t\tif (this.isIpv4Mapped()) {\n\t\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\t\treturn Some(new Ipv4Addr(segments[6] >> 8, segments[6] & 0xff, segments[7] >> 8, segments[7] & 0xff));\n\t\t}\n\t\treturn None();\n\t}\n\n\t/**\n\t * Formats this address as a compressed IPv6 string.\n\t * @returns The shortest standard IPv6 text form.\n\t * @since v0.0.1\n\t */\n\tpublic toString(): string {\n\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\tlet maxZeroStart = -1;\n\t\tlet maxZeroLen = 0;\n\t\tlet currentZeroStart = -1;\n\t\tlet currentZeroLen = 0;\n\n\t\tfor (let i = 0; i < segments.length; i++) {\n\t\t\tif (segments[i] === 0) {\n\t\t\t\tif (currentZeroStart === -1) {\n\t\t\t\t\tcurrentZeroStart = i;\n\t\t\t\t\tcurrentZeroLen = 1;\n\t\t\t\t} else {\n\t\t\t\t\tcurrentZeroLen++;\n\t\t\t\t}\n\t\t\t\tif (currentZeroLen > maxZeroLen) {\n\t\t\t\t\tmaxZeroLen = currentZeroLen;\n\t\t\t\t\tmaxZeroStart = currentZeroStart;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tcurrentZeroStart = -1;\n\t\t\t\tcurrentZeroLen = 0;\n\t\t\t}\n\t\t}\n\n\t\tif (maxZeroLen < 2) {\n\t\t\treturn segments.map((s) => s.toString(16)).join(':');\n\t\t}\n\n\t\tconst before = segments.slice(0, maxZeroStart).map((s) => s.toString(16));\n\t\tconst after = segments.slice(maxZeroStart + maxZeroLen).map((s) => s.toString(16));\n\n\t\treturn `${before.join(':')}::${after.join(':')}`;\n\t}\n\n\t/**\n\t * Encodes this address to a 16-byte buffer.\n\t * @param littleEndian Whether to use little-endian byte order. Defaults to `false`.\n\t * @returns An `ArrayBuffer` containing the IPv6 integer value.\n\t * @since v0.0.1\n\t */\n\tpublic toBuffer(littleEndian?: boolean): ArrayBuffer {\n\t\tconst buffer = new ArrayBuffer(16);\n\t\tconst view = new DataView(buffer);\n\t\tif (littleEndian) {\n\t\t\tview.setBigUint64(0, this.#integerAddress & 0xffffffffffffffffn, true);\n\t\t\tview.setBigUint64(8, this.#integerAddress >> 64n, true);\n\t\t} else {\n\t\t\tview.setBigUint64(0, this.#integerAddress >> 64n, false);\n\t\t\tview.setBigUint64(8, this.#integerAddress & 0xffffffffffffffffn, false);\n\t\t}\n\t\treturn buffer;\n\t}\n\n\t#toInteger(segments: [number, number, number, number, number, number, number, number]): bigint {\n\t\tlet result = 0n;\n\t\tfor (const segment of segments) {\n\t\t\tif (segment < 0 || segment > 0xffff) {\n\t\t\t\tthrow new Error('IPv6 segment must be between 0 and 65535');\n\t\t\t}\n\t\t\tresult = (result << 16n) | BigInt(segment);\n\t\t}\n\t\treturn result;\n\t}\n\n\t#fromInteger(integer: bigint): [number, number, number, number, number, number, number, number] {\n\t\treturn [\n\t\t\tNumber((integer >> 112n) & 0xffffn),\n\t\t\tNumber((integer >> 96n) & 0xffffn),\n\t\t\tNumber((integer >> 80n) & 0xffffn),\n\t\t\tNumber((integer >> 64n) & 0xffffn),\n\t\t\tNumber((integer >> 48n) & 0xffffn),\n\t\t\tNumber((integer >> 32n) & 0xffffn),\n\t\t\tNumber((integer >> 16n) & 0xffffn),\n\t\t\tNumber(integer & 0xffffn),\n\t\t];\n\t}\n\n\t#match(network: bigint, prefix: number): boolean {\n\t\tif (prefix < 0 || prefix > 128) {\n\t\t\tthrow new Error('Invalid prefix length');\n\t\t}\n\t\tif (prefix === 0) {\n\t\t\treturn true;\n\t\t}\n\t\tconst ALL_ONES = (1n << 128n) - 1n;\n\t\tconst mask = (ALL_ONES << BigInt(128 - prefix)) & ALL_ONES;\n\t\treturn (this.#integerAddress & mask) === (network & mask);\n\t}\n}\n","import {Err, type IResult, Ok} from '@luolapeikko/result-option';\nimport {Ipv6Addr} from './Ipv6Addr';\n\n/**\n * Represents an IPv4 address.\n * @example\n * const addr1 = Ipv4Addr.from('192.168.0.1').unwrap();\n * const addr2 = new Ipv4Addr(192, 168, 0, 1);\n * const addr3 = new Ipv4Addr(0xc0a80001);\n * @since v0.0.1\n */\nexport class Ipv4Addr {\n\t/**\n\t * Creates an IPv4 address from dotted-decimal text.\n\t * @returns A successful {@link IResult} with an IPv4 address, or an error when the input is invalid.\n\t * @since v0.0.1\n\t */\n\tpublic static from(value: string): IResult<Ipv4Addr, TypeError> {\n\t\tconst match = value.match(/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/);\n\t\tif (!match) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv4 value`));\n\t\t}\n\t\tconst octets = match.slice(1).map(Number);\n\t\tif (octets.some((o) => o > 255)) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv4 value`));\n\t\t}\n\t\treturn Ok(new Ipv4Addr(octets[0], octets[1], octets[2], octets[3]));\n\t}\n\n\t/**\n\t * Creates an IPv4 address from a 4-byte buffer.\n\t * @returns A successful {@link IResult} with an IPv4 address, or an error when the buffer cannot be read.\n\t * @since v0.0.1\n\t */\n\tpublic static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv4Addr> {\n\t\ttry {\n\t\t\tconst view = new DataView(buffer);\n\t\t\treturn Ok(new Ipv4Addr(view.getUint32(0, littleEndian)));\n\t\t} catch (err) {\n\t\t\treturn Err(err);\n\t\t}\n\t}\n\n\t/**\n\t * The number of bits in an IPv4 address.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BITS = 32;\n\n\t/**\n\t * The broadcast address `255.255.255.255`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BROADCAST: Ipv4Addr = new Ipv4Addr(0xffffffff);\n\n\t/**\n\t * The localhost address `127.0.0.1`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly LOCALHOST: Ipv4Addr = new Ipv4Addr(0x7f000001);\n\n\t/**\n\t * The unspecified address `0.0.0.0`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly UNSPECIFIED: Ipv4Addr = new Ipv4Addr(0x00000000);\n\n\t/**\n\t * The address family identifier for IPv4 addresses.\n\t * @since v0.0.1\n\t */\n\tpublic readonly family = 'ipv4';\n\n\t#integerAddress: number;\n\n\t/**\n\t * Creates a new IPv4 address from four octets.\n\t * @param num1 The first octet.\n\t * @param num2 The second octet.\n\t * @param num3 The third octet.\n\t * @param num4 The fourth octet.\n\t * @example\n\t * new Ipv4Addr(192, 168, 0, 1) // creates the address `192.168.0.1`\n\t */\n\tpublic constructor(num1: number, num2: number, num3: number, num4: number);\n\t/**\n\t * Creates a new IPv4 address from an integer value.\n\t * @param integerValue The integer representation of the IPv4 address.\n\t * @example\n\t * new Ipv4Addr(0xc0a80001) // creates the address `192.168.0.1` from the integer value `0xc0a80001`\n\t */\n\tpublic constructor(integerValue: number);\n\tpublic constructor(...args: [number] | [number, number, number, number]) {\n\t\tif (args.length === 1) {\n\t\t\tthis.#integerAddress = args[0];\n\t\t} else {\n\t\t\tthis.#integerAddress = this.#toInteger(args[0], args[1], args[2], args[3]);\n\t\t}\n\t}\n\n\t/**\n\t * Checks whether this address is the broadcast address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc919#section-7\n\t * @returns `true` when the address is `255.255.255.255`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBroadcast(): boolean {\n\t\treturn this.#integerAddress === 0xffffffff;\n\t}\n\n\t/**\n\t * Checks whether this address is in a documentation-only range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc5737\n\t * @returns `true` for `192.0.2.0/24`, `198.51.100.0/24`, or `203.0.113.0/24`; otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isDocumentation(): boolean {\n\t\treturn (\n\t\t\tthis.#match(0xc0000200, 24) || // 192.0.2.0/24\n\t\t\tthis.#match(0xc6336400, 24) || // 198.51.100.0/24\n\t\t\tthis.#match(0xcb007100, 24) // 203.0.113.0/24\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the benchmarking range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc2544\n\t * @returns `true` when the address is in `198.18.0.0/15`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBenchmarking(): boolean {\n\t\treturn this.#match(0xc6120000, 15);\n\t}\n\n\t/**\n\t * Checks whether this address is globally reachable.\n\t * @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml\n\t * @returns `true` when the address is not in any special non-global range.\n\t * @since v0.0.1\n\t */\n\tpublic isGlobal(): boolean {\n\t\treturn !(\n\t\t\tthis.isUnspecified() ||\n\t\t\tthis.isPrivate() ||\n\t\t\tthis.isShared() ||\n\t\t\tthis.isLoopback() ||\n\t\t\tthis.isLinkLocal() ||\n\t\t\tthis.isDocumentation() ||\n\t\t\tthis.isBenchmarking() ||\n\t\t\tthis.isReserved() ||\n\t\t\tthis.isMulticast() ||\n\t\t\tthis.isBroadcast()\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is link-local.\n\t * @see https://datatracker.ietf.org/doc/html/rfc3927\n\t * @returns `true` when the address is in `169.254.0.0/16`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLinkLocal(): boolean {\n\t\treturn this.#match(0xa9fe0000, 16);\n\t}\n\n\t/**\n\t * Checks whether this address is a loopback address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1122\n\t * @returns `true` when the address is in `127.0.0.0/8`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLoopback(): boolean {\n\t\treturn this.#match(0x7f000000, 8);\n\t}\n\n\t/**\n\t * Checks whether this address is a multicast address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc5771\n\t * @returns `true` when the address is in `224.0.0.0/4`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isMulticast(): boolean {\n\t\treturn this.#match(0xe0000000, 4);\n\t}\n\n\t/**\n\t * Checks whether this address is in a private-use range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1918\n\t * @returns `true` for `10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`; otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isPrivate(): boolean {\n\t\treturn (\n\t\t\tthis.#match(0x0a000000, 8) || // 10.0.0.0/8\n\t\t\tthis.#match(0xac100000, 12) || // 172.16.0.0/12\n\t\t\tthis.#match(0xc0a80000, 16) // 192.168.0.0/16\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the reserved range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1112\n\t * @returns `true` when the address is in `240.0.0.0/4` except the broadcast address.\n\t * @since v0.0.1\n\t */\n\tpublic isReserved(): boolean {\n\t\treturn this.#match(0xf0000000, 4) && !this.isBroadcast();\n\t}\n\n\t/**\n\t * Checks whether this address is in the shared Carrier-Grade NAT range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc6598\n\t * @returns `true` when the address is in `100.64.0.0/10`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isShared(): boolean {\n\t\treturn this.#match(0x64400000, 10);\n\t}\n\t\n\t/**\n\t * Checks whether this address is the unspecified address.\n\t * @returns `true` when the address is `0.0.0.0`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnspecified(): boolean {\n\t\treturn this.#integerAddress === 0;\n\t}\n\n\t/**\n\t * Converts this address to an IPv4-compatible IPv6 address.\n\t * @returns An IPv6 address in the form `::a.b.c.d`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv6(): Ipv6Addr {\n\t\treturn Ipv6Addr.from(`::${this.toString()}`).unwrap();\n\t}\n\n\t/**\n\t * Converts this address to an IPv4-mapped IPv6 address.\n\t * @returns An IPv6 address in the form `::ffff:a.b.c.d`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv6Mapped(): Ipv6Addr {\n\t\treturn Ipv6Addr.from(`::ffff:${this.toString()}`).unwrap();\n\t}\n\n\t/**\n\t * Formats this address as dotted-decimal text.\n\t * @returns The IPv4 string representation, such as `192.168.0.1`.\n\t * @since v0.0.1\n\t */\n\tpublic toString(): string {\n\t\tconst [num1, num2, num3, num4] = this.#fromInteger(this.#integerAddress);\n\t\treturn `${num1}.${num2}.${num3}.${num4}`;\n\t}\n\n\t/**\n\t * Encodes this address to a 4-byte buffer.\n\t * @returns An `ArrayBuffer` containing the IPv4 integer value.\n\t * @since v0.0.1\n\t */\n\tpublic toBuffer(littleEndian?: boolean): ArrayBuffer {\n\t\tconst buffer = new ArrayBuffer(4);\n\t\tconst view = new DataView(buffer);\n\t\tview.setUint32(0, this.#integerAddress, littleEndian);\n\t\treturn buffer;\n\t}\n\n\t#toInteger(num1: number, num2: number, num3: number, num4: number): number {\n\t\tfor (const octet of [num1, num2, num3, num4]) {\n\t\t\tif (octet < 0 || octet > 255) {\n\t\t\t\tthrow new Error('IPv4 octet must be between 0 and 255');\n\t\t\t}\n\t\t}\n\t\treturn ((num1 << 24) | (num2 << 16) | (num3 << 8) | num4) >>> 0;\n\t}\n\n\t#fromInteger(integer: number): [number, number, number, number] {\n\t\tconst num1 = (integer >> 24) & 0xff;\n\t\tconst num2 = (integer >> 16) & 0xff;\n\t\tconst num3 = (integer >> 8) & 0xff;\n\t\tconst num4 = integer & 0xff;\n\t\treturn [num1, num2, num3, num4];\n\t}\n\n\t#match(network: number, prefix: number): boolean {\n\t\tif (prefix < 0 || prefix > 32) {\n\t\t\tthrow new Error('Invalid prefix length');\n\t\t}\n\t\tconst mask = prefix === 0 ? 0 : (~0 << (32 - prefix)) >>> 0;\n\t\treturn (this.#integerAddress & mask) === (network & mask);\n\t}\n}\n"],"mappings":"+GAWA,IAAa,EAAb,MAAa,CAAS,CACrB,OAAe,MACd,wpBASD,OAAc,KAAK,EAAkC,CACpD,GAAI,CAAC,EAAS,MAAM,KAAK,EAAM,CAC9B,OAAA,EAAA,EAAA,KAAe,UAAU,GAAG,EAAM,wBAAwB,CAAC,CAE5D,GAAI,CACH,GAAM,CAAC,YAAW,aAAY,gBAAgB,GAAA,EAA0B,EAAM,CAExE,EAAW,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6 address.
6
+ * @example
7
+ * const addr1 = Ipv6Addr.from('2001:db8::1').unwrap();
8
+ * const addr2 = new Ipv6Addr([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]);
9
+ * const addr3 = new Ipv6Addr(0x20010db8000000000000000000000001n);
10
+ * @since v0.0.1
11
+ */
12
+ declare class Ipv6Addr {
13
+ #private;
14
+ private static regex;
15
+ /**
16
+ * Creates an IPv6 address from text.
17
+ * @returns A successful result with an IPv6 address, or an error when the input is invalid.
18
+ * @example
19
+ * const addr = Ipv6Addr.from('2001:db8::1').unwrap();
20
+ * @since v0.0.1
21
+ */
22
+ static from(value: string): IResult<Ipv6Addr>;
23
+ /**
24
+ * Creates an IPv6 address from a 16-byte buffer.
25
+ * @returns A successful result with an IPv6 address, or an error when the buffer cannot be read.
26
+ * @since v0.0.1
27
+ */
28
+ static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv6Addr>;
29
+ /**
30
+ * The number of bits in an IPv6 address.
31
+ * @since v0.0.1
32
+ */
33
+ static readonly BITS = 128;
34
+ /**
35
+ * The loopback address (`::1`).
36
+ * @since v0.0.1
37
+ */
38
+ static readonly LOCALHOST: Ipv6Addr;
39
+ /**
40
+ * The unspecified address (`::`).
41
+ * @since v0.0.1
42
+ */
43
+ static readonly UNSPECIFIED: Ipv6Addr;
44
+ /**
45
+ * The address family identifier for IPv6 addresses.
46
+ * @since v0.0.1
47
+ */
48
+ readonly family = "ipv6";
49
+ constructor(value: bigint);
50
+ constructor(segments: [number, number, number, number, number, number, number, number]);
51
+ /**
52
+ * Checks whether this address is in the benchmarking range.
53
+ * @see https://tools.ietf.org/html/rfc5180 and https://www.rfc-editor.org/errata_search.php?eid=1752
54
+ * @returns `true` when the address is in `2001:2::/48`, otherwise `false`.
55
+ * @since v0.0.1
56
+ */
57
+ isBenchmarking(): boolean;
58
+ /**
59
+ * Checks whether this address is in a documentation-only range.
60
+ * @see https://tools.ietf.org/html/rfc3849 and https://tools.ietf.org/html/rfc9637
61
+ * @returns `true` for `2001:db8::/32` or `3fff::/20`, otherwise `false`.
62
+ * @since v0.0.1
63
+ */
64
+ isDocumentation(): boolean;
65
+ /**
66
+ * Checks whether this address is the loopback address.
67
+ * @see https://tools.ietf.org/html/rfc4291#section-2.5.3
68
+ * @returns `true` when the address is `::1`, otherwise `false`.
69
+ * @since v0.0.1
70
+ */
71
+ isLoopback(): boolean;
72
+ /**
73
+ * Checks whether this address is the unspecified address.
74
+ * @see https://tools.ietf.org/html/rfc4291
75
+ * @returns `true` when the address is `::`, otherwise `false`.
76
+ * @since v0.0.1
77
+ */
78
+ isUnspecified(): boolean;
79
+ /**
80
+ * Checks whether this address is a multicast address.
81
+ * @see https://tools.ietf.org/html/rfc4291
82
+ * @returns `true` when the address is in `ff00::/8`, otherwise `false`.
83
+ * @since v0.0.1
84
+ */
85
+ isMulticast(): boolean;
86
+ /**
87
+ * Checks whether this address is an IPv4-mapped IPv6 address.
88
+ * @returns `true` when the address is in `::ffff:0:0/96`, otherwise `false`.
89
+ * @since v0.0.1
90
+ */
91
+ isIpv4Mapped(): boolean;
92
+ /**
93
+ * Checks whether this address is a unicast address.
94
+ * @see https://tools.ietf.org/html/rfc4291
95
+ * @returns `true` when the address is not multicast, otherwise `false`.
96
+ * @since v0.0.1
97
+ */
98
+ isUnicast(): boolean;
99
+ /**
100
+ * Checks whether this address is a link-local unicast address.
101
+ * @see https://tools.ietf.org/html/rfc4291
102
+ * @returns `true` when the address is in `fe80::/10`, otherwise `false`.
103
+ * @since v0.0.1
104
+ */
105
+ isUnicastLinkLocal(): boolean;
106
+ /**
107
+ * Checks whether this address is a global unicast address.
108
+ * @see https://tools.ietf.org/html/rfc4291#section-2.5.7
109
+ * @returns `true` when the address is unicast and not loopback, link-local, private, unspecified, or documentation.
110
+ * @since v0.0.1
111
+ */
112
+ isUnicastGlobal(): boolean;
113
+ /**
114
+ * Checks whether this address appears globally reachable.
115
+ * @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
116
+ * @returns `true` when the address is not in known non-global special ranges.
117
+ * @since v0.0.1
118
+ */
119
+ isGlobal(): boolean;
120
+ /**
121
+ * Checks whether this address is in the unique-local range.
122
+ * @see https://tools.ietf.org/html/rfc4193
123
+ * @returns `true` when the address is in `fc00::/7`, otherwise `false`.
124
+ * @since v0.0.1
125
+ */
126
+ isUniqueLocal(): boolean;
127
+ /**
128
+ * Converts this address to IPv4 when compatible or mapped.
129
+ * @returns An IPv4 address for `::a.b.c.d` or `::ffff:a.b.c.d`; otherwise `None`.
130
+ * @since v0.0.1
131
+ */
132
+ toIpv4(): IOption<Ipv4Addr>;
133
+ /**
134
+ * Converts this address to IPv4 only when it is IPv4-mapped.
135
+ * @returns An IPv4 address for `::ffff:a.b.c.d`; otherwise `None`.
136
+ * @since v0.0.1
137
+ */
138
+ toIpv4Mapped(): IOption<Ipv4Addr>;
139
+ /**
140
+ * Formats this address as a compressed IPv6 string.
141
+ * @returns The shortest standard IPv6 text form.
142
+ * @since v0.0.1
143
+ */
144
+ toString(): string;
145
+ /**
146
+ * Encodes this address to a 16-byte buffer.
147
+ * @param littleEndian Whether to use little-endian byte order. Defaults to `false`.
148
+ * @returns An `ArrayBuffer` containing the IPv6 integer value.
149
+ * @since v0.0.1
150
+ */
151
+ toBuffer(littleEndian?: boolean): ArrayBuffer;
152
+ }
153
+ //#endregion
154
+ //#region src/Ipv4Addr.d.ts
155
+ /**
156
+ * Represents an IPv4 address.
157
+ * @example
158
+ * const addr1 = Ipv4Addr.from('192.168.0.1').unwrap();
159
+ * const addr2 = new Ipv4Addr(192, 168, 0, 1);
160
+ * const addr3 = new Ipv4Addr(0xc0a80001);
161
+ * @since v0.0.1
162
+ */
163
+ declare class Ipv4Addr {
164
+ #private;
165
+ /**
166
+ * Creates an IPv4 address from dotted-decimal text.
167
+ * @returns A successful {@link IResult} with an IPv4 address, or an error when the input is invalid.
168
+ * @since v0.0.1
169
+ */
170
+ static from(value: string): IResult<Ipv4Addr, TypeError>;
171
+ /**
172
+ * Creates an IPv4 address from a 4-byte buffer.
173
+ * @returns A successful {@link IResult} with an IPv4 address, or an error when the buffer cannot be read.
174
+ * @since v0.0.1
175
+ */
176
+ static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv4Addr>;
177
+ /**
178
+ * The number of bits in an IPv4 address.
179
+ * @since v0.0.1
180
+ */
181
+ static readonly BITS = 32;
182
+ /**
183
+ * The broadcast address `255.255.255.255`.
184
+ * @since v0.0.1
185
+ */
186
+ static readonly BROADCAST: Ipv4Addr;
187
+ /**
188
+ * The localhost address `127.0.0.1`.
189
+ * @since v0.0.1
190
+ */
191
+ static readonly LOCALHOST: Ipv4Addr;
192
+ /**
193
+ * The unspecified address `0.0.0.0`.
194
+ * @since v0.0.1
195
+ */
196
+ static readonly UNSPECIFIED: Ipv4Addr;
197
+ /**
198
+ * The address family identifier for IPv4 addresses.
199
+ * @since v0.0.1
200
+ */
201
+ readonly family = "ipv4";
202
+ /**
203
+ * Creates a new IPv4 address from four octets.
204
+ * @param num1 The first octet.
205
+ * @param num2 The second octet.
206
+ * @param num3 The third octet.
207
+ * @param num4 The fourth octet.
208
+ * @example
209
+ * new Ipv4Addr(192, 168, 0, 1) // creates the address `192.168.0.1`
210
+ */
211
+ constructor(num1: number, num2: number, num3: number, num4: number);
212
+ /**
213
+ * Creates a new IPv4 address from an integer value.
214
+ * @param integerValue The integer representation of the IPv4 address.
215
+ * @example
216
+ * new Ipv4Addr(0xc0a80001) // creates the address `192.168.0.1` from the integer value `0xc0a80001`
217
+ */
218
+ constructor(integerValue: number);
219
+ /**
220
+ * Checks whether this address is the broadcast address.
221
+ * @see https://datatracker.ietf.org/doc/html/rfc919#section-7
222
+ * @returns `true` when the address is `255.255.255.255`, otherwise `false`.
223
+ * @since v0.0.1
224
+ */
225
+ isBroadcast(): boolean;
226
+ /**
227
+ * Checks whether this address is in a documentation-only range.
228
+ * @see https://datatracker.ietf.org/doc/html/rfc5737
229
+ * @returns `true` for `192.0.2.0/24`, `198.51.100.0/24`, or `203.0.113.0/24`; otherwise `false`.
230
+ * @since v0.0.1
231
+ */
232
+ isDocumentation(): boolean;
233
+ /**
234
+ * Checks whether this address is in the benchmarking range.
235
+ * @see https://datatracker.ietf.org/doc/html/rfc2544
236
+ * @returns `true` when the address is in `198.18.0.0/15`, otherwise `false`.
237
+ * @since v0.0.1
238
+ */
239
+ isBenchmarking(): boolean;
240
+ /**
241
+ * Checks whether this address is globally reachable.
242
+ * @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
243
+ * @returns `true` when the address is not in any special non-global range.
244
+ * @since v0.0.1
245
+ */
246
+ isGlobal(): boolean;
247
+ /**
248
+ * Checks whether this address is link-local.
249
+ * @see https://datatracker.ietf.org/doc/html/rfc3927
250
+ * @returns `true` when the address is in `169.254.0.0/16`, otherwise `false`.
251
+ * @since v0.0.1
252
+ */
253
+ isLinkLocal(): boolean;
254
+ /**
255
+ * Checks whether this address is a loopback address.
256
+ * @see https://datatracker.ietf.org/doc/html/rfc1122
257
+ * @returns `true` when the address is in `127.0.0.0/8`, otherwise `false`.
258
+ * @since v0.0.1
259
+ */
260
+ isLoopback(): boolean;
261
+ /**
262
+ * Checks whether this address is a multicast address.
263
+ * @see https://datatracker.ietf.org/doc/html/rfc5771
264
+ * @returns `true` when the address is in `224.0.0.0/4`, otherwise `false`.
265
+ * @since v0.0.1
266
+ */
267
+ isMulticast(): boolean;
268
+ /**
269
+ * Checks whether this address is in a private-use range.
270
+ * @see https://datatracker.ietf.org/doc/html/rfc1918
271
+ * @returns `true` for `10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`; otherwise `false`.
272
+ * @since v0.0.1
273
+ */
274
+ isPrivate(): boolean;
275
+ /**
276
+ * Checks whether this address is in the reserved range.
277
+ * @see https://datatracker.ietf.org/doc/html/rfc1112
278
+ * @returns `true` when the address is in `240.0.0.0/4` except the broadcast address.
279
+ * @since v0.0.1
280
+ */
281
+ isReserved(): boolean;
282
+ /**
283
+ * Checks whether this address is in the shared Carrier-Grade NAT range.
284
+ * @see https://datatracker.ietf.org/doc/html/rfc6598
285
+ * @returns `true` when the address is in `100.64.0.0/10`, otherwise `false`.
286
+ * @since v0.0.1
287
+ */
288
+ isShared(): boolean;
289
+ /**
290
+ * Checks whether this address is the unspecified address.
291
+ * @returns `true` when the address is `0.0.0.0`, otherwise `false`.
292
+ * @since v0.0.1
293
+ */
294
+ isUnspecified(): boolean;
295
+ /**
296
+ * Converts this address to an IPv4-compatible IPv6 address.
297
+ * @returns An IPv6 address in the form `::a.b.c.d`.
298
+ * @since v0.0.1
299
+ */
300
+ toIpv6(): Ipv6Addr;
301
+ /**
302
+ * Converts this address to an IPv4-mapped IPv6 address.
303
+ * @returns An IPv6 address in the form `::ffff:a.b.c.d`.
304
+ * @since v0.0.1
305
+ */
306
+ toIpv6Mapped(): Ipv6Addr;
307
+ /**
308
+ * Formats this address as dotted-decimal text.
309
+ * @returns The IPv4 string representation, such as `192.168.0.1`.
310
+ * @since v0.0.1
311
+ */
312
+ toString(): string;
313
+ /**
314
+ * Encodes this address to a 4-byte buffer.
315
+ * @returns An `ArrayBuffer` containing the IPv4 integer value.
316
+ * @since v0.0.1
317
+ */
318
+ toBuffer(littleEndian?: boolean): ArrayBuffer;
319
+ }
320
+ //#endregion
321
+ export { Ipv4Addr, Ipv6Addr };
322
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/Ipv6Addr.ts","../src/Ipv4Addr.ts"],"mappings":";;;;AAWA;;;;;;;cAAa,QAAA;EAAA;iBACG,KAAA;;;;;;;;SAUD,IAAA,CAAK,KAAA,WAAgB,OAAA,CAAQ,QAAA;;;;;;SAwB7B,UAAA,CAAW,MAAA,EAAQ,WAAA,EAAa,YAAA,aAAyB,OAAA,CAAQ,QAAA;;;;;kBA2CxD,IAAA;;;;;kBAMA,SAAA,EAAW,QAA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AA;;;;;;;EAUA,QAAA,CAAA;;;;;;EASA,aAAA,CAAA;;;;;;EASA,MAAA,CAAA,GAAiB,QAAA;;;;;;EASjB,YAAA,CAAA,GAAuB,QAAA;;;;;;EASvB,QAAA,CAAA;;;;;;EAUA,QAAA,CAAgB,YAAA,aAAyB,WAAA;AAAA"}
@@ -0,0 +1,322 @@
1
+ import { IOption, IResult } from "@luolapeikko/result-option";
2
+
3
+ //#region src/Ipv6Addr.d.ts
4
+ /**
5
+ * Represents an IPv6 address.
6
+ * @example
7
+ * const addr1 = Ipv6Addr.from('2001:db8::1').unwrap();
8
+ * const addr2 = new Ipv6Addr([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]);
9
+ * const addr3 = new Ipv6Addr(0x20010db8000000000000000000000001n);
10
+ * @since v0.0.1
11
+ */
12
+ declare class Ipv6Addr {
13
+ #private;
14
+ private static regex;
15
+ /**
16
+ * Creates an IPv6 address from text.
17
+ * @returns A successful result with an IPv6 address, or an error when the input is invalid.
18
+ * @example
19
+ * const addr = Ipv6Addr.from('2001:db8::1').unwrap();
20
+ * @since v0.0.1
21
+ */
22
+ static from(value: string): IResult<Ipv6Addr>;
23
+ /**
24
+ * Creates an IPv6 address from a 16-byte buffer.
25
+ * @returns A successful result with an IPv6 address, or an error when the buffer cannot be read.
26
+ * @since v0.0.1
27
+ */
28
+ static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv6Addr>;
29
+ /**
30
+ * The number of bits in an IPv6 address.
31
+ * @since v0.0.1
32
+ */
33
+ static readonly BITS = 128;
34
+ /**
35
+ * The loopback address (`::1`).
36
+ * @since v0.0.1
37
+ */
38
+ static readonly LOCALHOST: Ipv6Addr;
39
+ /**
40
+ * The unspecified address (`::`).
41
+ * @since v0.0.1
42
+ */
43
+ static readonly UNSPECIFIED: Ipv6Addr;
44
+ /**
45
+ * The address family identifier for IPv6 addresses.
46
+ * @since v0.0.1
47
+ */
48
+ readonly family = "ipv6";
49
+ constructor(value: bigint);
50
+ constructor(segments: [number, number, number, number, number, number, number, number]);
51
+ /**
52
+ * Checks whether this address is in the benchmarking range.
53
+ * @see https://tools.ietf.org/html/rfc5180 and https://www.rfc-editor.org/errata_search.php?eid=1752
54
+ * @returns `true` when the address is in `2001:2::/48`, otherwise `false`.
55
+ * @since v0.0.1
56
+ */
57
+ isBenchmarking(): boolean;
58
+ /**
59
+ * Checks whether this address is in a documentation-only range.
60
+ * @see https://tools.ietf.org/html/rfc3849 and https://tools.ietf.org/html/rfc9637
61
+ * @returns `true` for `2001:db8::/32` or `3fff::/20`, otherwise `false`.
62
+ * @since v0.0.1
63
+ */
64
+ isDocumentation(): boolean;
65
+ /**
66
+ * Checks whether this address is the loopback address.
67
+ * @see https://tools.ietf.org/html/rfc4291#section-2.5.3
68
+ * @returns `true` when the address is `::1`, otherwise `false`.
69
+ * @since v0.0.1
70
+ */
71
+ isLoopback(): boolean;
72
+ /**
73
+ * Checks whether this address is the unspecified address.
74
+ * @see https://tools.ietf.org/html/rfc4291
75
+ * @returns `true` when the address is `::`, otherwise `false`.
76
+ * @since v0.0.1
77
+ */
78
+ isUnspecified(): boolean;
79
+ /**
80
+ * Checks whether this address is a multicast address.
81
+ * @see https://tools.ietf.org/html/rfc4291
82
+ * @returns `true` when the address is in `ff00::/8`, otherwise `false`.
83
+ * @since v0.0.1
84
+ */
85
+ isMulticast(): boolean;
86
+ /**
87
+ * Checks whether this address is an IPv4-mapped IPv6 address.
88
+ * @returns `true` when the address is in `::ffff:0:0/96`, otherwise `false`.
89
+ * @since v0.0.1
90
+ */
91
+ isIpv4Mapped(): boolean;
92
+ /**
93
+ * Checks whether this address is a unicast address.
94
+ * @see https://tools.ietf.org/html/rfc4291
95
+ * @returns `true` when the address is not multicast, otherwise `false`.
96
+ * @since v0.0.1
97
+ */
98
+ isUnicast(): boolean;
99
+ /**
100
+ * Checks whether this address is a link-local unicast address.
101
+ * @see https://tools.ietf.org/html/rfc4291
102
+ * @returns `true` when the address is in `fe80::/10`, otherwise `false`.
103
+ * @since v0.0.1
104
+ */
105
+ isUnicastLinkLocal(): boolean;
106
+ /**
107
+ * Checks whether this address is a global unicast address.
108
+ * @see https://tools.ietf.org/html/rfc4291#section-2.5.7
109
+ * @returns `true` when the address is unicast and not loopback, link-local, private, unspecified, or documentation.
110
+ * @since v0.0.1
111
+ */
112
+ isUnicastGlobal(): boolean;
113
+ /**
114
+ * Checks whether this address appears globally reachable.
115
+ * @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
116
+ * @returns `true` when the address is not in known non-global special ranges.
117
+ * @since v0.0.1
118
+ */
119
+ isGlobal(): boolean;
120
+ /**
121
+ * Checks whether this address is in the unique-local range.
122
+ * @see https://tools.ietf.org/html/rfc4193
123
+ * @returns `true` when the address is in `fc00::/7`, otherwise `false`.
124
+ * @since v0.0.1
125
+ */
126
+ isUniqueLocal(): boolean;
127
+ /**
128
+ * Converts this address to IPv4 when compatible or mapped.
129
+ * @returns An IPv4 address for `::a.b.c.d` or `::ffff:a.b.c.d`; otherwise `None`.
130
+ * @since v0.0.1
131
+ */
132
+ toIpv4(): IOption<Ipv4Addr>;
133
+ /**
134
+ * Converts this address to IPv4 only when it is IPv4-mapped.
135
+ * @returns An IPv4 address for `::ffff:a.b.c.d`; otherwise `None`.
136
+ * @since v0.0.1
137
+ */
138
+ toIpv4Mapped(): IOption<Ipv4Addr>;
139
+ /**
140
+ * Formats this address as a compressed IPv6 string.
141
+ * @returns The shortest standard IPv6 text form.
142
+ * @since v0.0.1
143
+ */
144
+ toString(): string;
145
+ /**
146
+ * Encodes this address to a 16-byte buffer.
147
+ * @param littleEndian Whether to use little-endian byte order. Defaults to `false`.
148
+ * @returns An `ArrayBuffer` containing the IPv6 integer value.
149
+ * @since v0.0.1
150
+ */
151
+ toBuffer(littleEndian?: boolean): ArrayBuffer;
152
+ }
153
+ //#endregion
154
+ //#region src/Ipv4Addr.d.ts
155
+ /**
156
+ * Represents an IPv4 address.
157
+ * @example
158
+ * const addr1 = Ipv4Addr.from('192.168.0.1').unwrap();
159
+ * const addr2 = new Ipv4Addr(192, 168, 0, 1);
160
+ * const addr3 = new Ipv4Addr(0xc0a80001);
161
+ * @since v0.0.1
162
+ */
163
+ declare class Ipv4Addr {
164
+ #private;
165
+ /**
166
+ * Creates an IPv4 address from dotted-decimal text.
167
+ * @returns A successful {@link IResult} with an IPv4 address, or an error when the input is invalid.
168
+ * @since v0.0.1
169
+ */
170
+ static from(value: string): IResult<Ipv4Addr, TypeError>;
171
+ /**
172
+ * Creates an IPv4 address from a 4-byte buffer.
173
+ * @returns A successful {@link IResult} with an IPv4 address, or an error when the buffer cannot be read.
174
+ * @since v0.0.1
175
+ */
176
+ static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv4Addr>;
177
+ /**
178
+ * The number of bits in an IPv4 address.
179
+ * @since v0.0.1
180
+ */
181
+ static readonly BITS = 32;
182
+ /**
183
+ * The broadcast address `255.255.255.255`.
184
+ * @since v0.0.1
185
+ */
186
+ static readonly BROADCAST: Ipv4Addr;
187
+ /**
188
+ * The localhost address `127.0.0.1`.
189
+ * @since v0.0.1
190
+ */
191
+ static readonly LOCALHOST: Ipv4Addr;
192
+ /**
193
+ * The unspecified address `0.0.0.0`.
194
+ * @since v0.0.1
195
+ */
196
+ static readonly UNSPECIFIED: Ipv4Addr;
197
+ /**
198
+ * The address family identifier for IPv4 addresses.
199
+ * @since v0.0.1
200
+ */
201
+ readonly family = "ipv4";
202
+ /**
203
+ * Creates a new IPv4 address from four octets.
204
+ * @param num1 The first octet.
205
+ * @param num2 The second octet.
206
+ * @param num3 The third octet.
207
+ * @param num4 The fourth octet.
208
+ * @example
209
+ * new Ipv4Addr(192, 168, 0, 1) // creates the address `192.168.0.1`
210
+ */
211
+ constructor(num1: number, num2: number, num3: number, num4: number);
212
+ /**
213
+ * Creates a new IPv4 address from an integer value.
214
+ * @param integerValue The integer representation of the IPv4 address.
215
+ * @example
216
+ * new Ipv4Addr(0xc0a80001) // creates the address `192.168.0.1` from the integer value `0xc0a80001`
217
+ */
218
+ constructor(integerValue: number);
219
+ /**
220
+ * Checks whether this address is the broadcast address.
221
+ * @see https://datatracker.ietf.org/doc/html/rfc919#section-7
222
+ * @returns `true` when the address is `255.255.255.255`, otherwise `false`.
223
+ * @since v0.0.1
224
+ */
225
+ isBroadcast(): boolean;
226
+ /**
227
+ * Checks whether this address is in a documentation-only range.
228
+ * @see https://datatracker.ietf.org/doc/html/rfc5737
229
+ * @returns `true` for `192.0.2.0/24`, `198.51.100.0/24`, or `203.0.113.0/24`; otherwise `false`.
230
+ * @since v0.0.1
231
+ */
232
+ isDocumentation(): boolean;
233
+ /**
234
+ * Checks whether this address is in the benchmarking range.
235
+ * @see https://datatracker.ietf.org/doc/html/rfc2544
236
+ * @returns `true` when the address is in `198.18.0.0/15`, otherwise `false`.
237
+ * @since v0.0.1
238
+ */
239
+ isBenchmarking(): boolean;
240
+ /**
241
+ * Checks whether this address is globally reachable.
242
+ * @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
243
+ * @returns `true` when the address is not in any special non-global range.
244
+ * @since v0.0.1
245
+ */
246
+ isGlobal(): boolean;
247
+ /**
248
+ * Checks whether this address is link-local.
249
+ * @see https://datatracker.ietf.org/doc/html/rfc3927
250
+ * @returns `true` when the address is in `169.254.0.0/16`, otherwise `false`.
251
+ * @since v0.0.1
252
+ */
253
+ isLinkLocal(): boolean;
254
+ /**
255
+ * Checks whether this address is a loopback address.
256
+ * @see https://datatracker.ietf.org/doc/html/rfc1122
257
+ * @returns `true` when the address is in `127.0.0.0/8`, otherwise `false`.
258
+ * @since v0.0.1
259
+ */
260
+ isLoopback(): boolean;
261
+ /**
262
+ * Checks whether this address is a multicast address.
263
+ * @see https://datatracker.ietf.org/doc/html/rfc5771
264
+ * @returns `true` when the address is in `224.0.0.0/4`, otherwise `false`.
265
+ * @since v0.0.1
266
+ */
267
+ isMulticast(): boolean;
268
+ /**
269
+ * Checks whether this address is in a private-use range.
270
+ * @see https://datatracker.ietf.org/doc/html/rfc1918
271
+ * @returns `true` for `10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`; otherwise `false`.
272
+ * @since v0.0.1
273
+ */
274
+ isPrivate(): boolean;
275
+ /**
276
+ * Checks whether this address is in the reserved range.
277
+ * @see https://datatracker.ietf.org/doc/html/rfc1112
278
+ * @returns `true` when the address is in `240.0.0.0/4` except the broadcast address.
279
+ * @since v0.0.1
280
+ */
281
+ isReserved(): boolean;
282
+ /**
283
+ * Checks whether this address is in the shared Carrier-Grade NAT range.
284
+ * @see https://datatracker.ietf.org/doc/html/rfc6598
285
+ * @returns `true` when the address is in `100.64.0.0/10`, otherwise `false`.
286
+ * @since v0.0.1
287
+ */
288
+ isShared(): boolean;
289
+ /**
290
+ * Checks whether this address is the unspecified address.
291
+ * @returns `true` when the address is `0.0.0.0`, otherwise `false`.
292
+ * @since v0.0.1
293
+ */
294
+ isUnspecified(): boolean;
295
+ /**
296
+ * Converts this address to an IPv4-compatible IPv6 address.
297
+ * @returns An IPv6 address in the form `::a.b.c.d`.
298
+ * @since v0.0.1
299
+ */
300
+ toIpv6(): Ipv6Addr;
301
+ /**
302
+ * Converts this address to an IPv4-mapped IPv6 address.
303
+ * @returns An IPv6 address in the form `::ffff:a.b.c.d`.
304
+ * @since v0.0.1
305
+ */
306
+ toIpv6Mapped(): Ipv6Addr;
307
+ /**
308
+ * Formats this address as dotted-decimal text.
309
+ * @returns The IPv4 string representation, such as `192.168.0.1`.
310
+ * @since v0.0.1
311
+ */
312
+ toString(): string;
313
+ /**
314
+ * Encodes this address to a 4-byte buffer.
315
+ * @returns An `ArrayBuffer` containing the IPv4 integer value.
316
+ * @since v0.0.1
317
+ */
318
+ toBuffer(littleEndian?: boolean): ArrayBuffer;
319
+ }
320
+ //#endregion
321
+ export { Ipv4Addr, Ipv6Addr };
322
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"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B,IAAA,UAAc,IAAA,UAAc,IAAA,UAAc,IAAA;;;;;;;EAO7D,WAAA,CAAmB,YAAA;;;AAhFpB;;;;EA+FC,WAAA,CAAA;;;;;;;EAUA,eAAA,CAAA;;;;;;;EAcA,cAAA,CAAA;;;;;;;EAUA,QAAA,CAAA;;;;;;;EAqBA,WAAA,CAAA;;;;;;;EAUA,UAAA,CAAA;;;;;;;EAUA,WAAA,CAAA;;;;;;;EAUA,SAAA,CAAA;;;;;;;EAcA,UAAA,CAAA;;;;;;;EAUA,QAAA,CAAA;;;;;;EASA,aAAA,CAAA;;;;;;EASA,MAAA,CAAA,GAAiB,QAAA;;;;;;EASjB,YAAA,CAAA,GAAuB,QAAA;;;;;;EASvB,QAAA,CAAA;;;;;;EAUA,QAAA,CAAgB,YAAA,aAAyB,WAAA;AAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import{Err as e,None as t,Ok as n,Some as r}from"@luolapeikko/result-option";var i=class i{static regex=/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;static from(t){if(!i.regex.test(t))return e(TypeError(`${t} is invalid ipv6 value`));try{let{leftParts:e,rightParts:r,embeddedIpv4:a}=i.#e(t),o=e.map(e=>parseInt(e,16)),s=r.map(e=>parseInt(e,16)),c=8-(o.length+s.length+a.length);return n(new i([...o,...Array(c).fill(0),...s,...a]))}catch(t){return e(t)}}static fromBuffer(t,r){try{let e=new DataView(t);if(r){let t=e.getBigUint64(0,!0);return n(new i(e.getBigUint64(8,!0)<<64n|t))}else{let t=e.getBigUint64(0,!1),r=e.getBigUint64(8,!1);return n(new i(t<<64n|r))}}catch(t){return e(t)}}static#e(e){let t=e.split(`::`),n=t[0].split(`:`).filter(e=>e!==``),r=t.length>1?t[1].split(`:`).filter(e=>e!==``):[],a=[],o=r[r.length-1]??n[n.length-1];return o?.includes(`.`)&&(a=i.#t(o),r.length>0?r.pop():n.pop()),{leftParts:n,rightParts:r,embeddedIpv4:a}}static#t(e){let t=e.split(`.`).map(Number);return[t[0]<<8|t[1],t[2]<<8|t[3]]}static BITS=128;static LOCALHOST=new i(1n);static UNSPECIFIED=new i(0n);family=`ipv6`;#n;constructor(e){Array.isArray(e)?this.#n=this.#r(e):this.#n=e}isBenchmarking(){return this.#a(42540488320432167789079031612388147200n,48)}isDocumentation(){return this.#a(42540766411282592856903984951653826560n,32)||this.#a(85065399433376081038215121361612832768n,20)}isLoopback(){return this.#n===1n}isUnspecified(){return this.#n===0n}isMulticast(){return this.#n>>120n==255n}isIpv4Mapped(){return this.#a(281470681743360n,96)}isUnicast(){return!this.isMulticast()}isUnicastLinkLocal(){return this.#a(338288524927261089654018896841347694592n,10)}isUnicastGlobal(){return this.isUnicast()&&!this.isLoopback()&&!this.isUnicastLinkLocal()&&!this.isUniqueLocal()&&!this.isUnspecified()&&!this.isDocumentation()}isGlobal(){return!this.isUnspecified()&&!this.isLoopback()&&!this.isIpv4Mapped()&&!this.isBenchmarking()&&!this.isDocumentation()&&!this.isUniqueLocal()&&!this.isUnicastLinkLocal()&&!this.isMulticast()}isUniqueLocal(){return this.#a(334965454937798799971759379190646833152n,7)}toIpv4(){if(this.isIpv4Mapped()||this.#a(0n,96)){let e=this.#i(this.#n);return r(new a(e[6]>>8,e[6]&255,e[7]>>8,e[7]&255))}return t()}toIpv4Mapped(){if(this.isIpv4Mapped()){let e=this.#i(this.#n);return r(new a(e[6]>>8,e[6]&255,e[7]>>8,e[7]&255))}return t()}toString(){let e=this.#i(this.#n),t=-1,n=0,r=-1,i=0;for(let a=0;a<e.length;a++)e[a]===0?(r===-1?(r=a,i=1):i++,i>n&&(n=i,t=r)):(r=-1,i=0);if(n<2)return e.map(e=>e.toString(16)).join(`:`);let a=e.slice(0,t).map(e=>e.toString(16)),o=e.slice(t+n).map(e=>e.toString(16));return`${a.join(`:`)}::${o.join(`:`)}`}toBuffer(e){let t=new ArrayBuffer(16),n=new DataView(t);return e?(n.setBigUint64(0,this.#n&18446744073709551615n,!0),n.setBigUint64(8,this.#n>>64n,!0)):(n.setBigUint64(0,this.#n>>64n,!1),n.setBigUint64(8,this.#n&18446744073709551615n,!1)),t}#r(e){let t=0n;for(let n of e){if(n<0||n>65535)throw Error(`IPv6 segment must be between 0 and 65535`);t=t<<16n|BigInt(n)}return t}#i(e){return[Number(e>>112n&65535n),Number(e>>96n&65535n),Number(e>>80n&65535n),Number(e>>64n&65535n),Number(e>>48n&65535n),Number(e>>32n&65535n),Number(e>>16n&65535n),Number(e&65535n)]}#a(e,t){if(t<0||t>128)throw Error(`Invalid prefix length`);if(t===0)return!0;let n=(1n<<128n)-1n,r=n<<BigInt(128-t)&n;return(this.#n&r)===(e&r)}},a=class t{static from(r){let i=r.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);if(!i)return e(TypeError(`${r} is invalid ipv4 value`));let a=i.slice(1).map(Number);return a.some(e=>e>255)?e(TypeError(`${r} is invalid ipv4 value`)):n(new t(a[0],a[1],a[2],a[3]))}static fromBuffer(r,i){try{return n(new t(new DataView(r).getUint32(0,i)))}catch(t){return e(t)}}static BITS=32;static BROADCAST=new t(4294967295);static LOCALHOST=new t(2130706433);static UNSPECIFIED=new t(0);family=`ipv4`;#e;constructor(...e){e.length===1?this.#e=e[0]:this.#e=this.#t(e[0],e[1],e[2],e[3])}isBroadcast(){return this.#e===4294967295}isDocumentation(){return this.#r(3221225984,24)||this.#r(3325256704,24)||this.#r(3405803776,24)}isBenchmarking(){return this.#r(3323068416,15)}isGlobal(){return!(this.isUnspecified()||this.isPrivate()||this.isShared()||this.isLoopback()||this.isLinkLocal()||this.isDocumentation()||this.isBenchmarking()||this.isReserved()||this.isMulticast()||this.isBroadcast())}isLinkLocal(){return this.#r(2851995648,16)}isLoopback(){return this.#r(2130706432,8)}isMulticast(){return this.#r(3758096384,4)}isPrivate(){return this.#r(167772160,8)||this.#r(2886729728,12)||this.#r(3232235520,16)}isReserved(){return this.#r(4026531840,4)&&!this.isBroadcast()}isShared(){return this.#r(1681915904,10)}isUnspecified(){return this.#e===0}toIpv6(){return i.from(`::${this.toString()}`).unwrap()}toIpv6Mapped(){return i.from(`::ffff:${this.toString()}`).unwrap()}toString(){let[e,t,n,r]=this.#n(this.#e);return`${e}.${t}.${n}.${r}`}toBuffer(e){let t=new ArrayBuffer(4);return new DataView(t).setUint32(0,this.#e,e),t}#t(e,t,n,r){for(let i of[e,t,n,r])if(i<0||i>255)throw Error(`IPv4 octet must be between 0 and 255`);return(e<<24|t<<16|n<<8|r)>>>0}#n(e){return[e>>24&255,e>>16&255,e>>8&255,e&255]}#r(e,t){if(t<0||t>32)throw Error(`Invalid prefix length`);let n=t===0?0:-1<<32-t>>>0;return(this.#e&n)===(e&n)}};export{a as Ipv4Addr,i as Ipv6Addr};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["#parseComponents","#parseEmbeddedIpv4","#integerAddress","#toInteger","#match","#fromInteger","#integerAddress","#toInteger","#match","#fromInteger"],"sources":["../src/Ipv6Addr.ts","../src/Ipv4Addr.ts"],"sourcesContent":["import {Err, type IOption, type IResult, None, Ok, Some} from '@luolapeikko/result-option';\nimport {Ipv4Addr} from './Ipv4Addr';\n\n/**\n * Represents an IPv6 address.\n * @example\n * const addr1 = Ipv6Addr.from('2001:db8::1').unwrap();\n * const addr2 = new Ipv6Addr([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]);\n * const addr3 = new Ipv6Addr(0x20010db8000000000000000000000001n);\n * @since v0.0.1\n */\nexport class Ipv6Addr {\n\tprivate static regex =\n\t\t/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;\n\n\t/**\n\t * Creates an IPv6 address from text.\n\t * @returns A successful result with an IPv6 address, or an error when the input is invalid.\n\t * @example\n\t * const addr = Ipv6Addr.from('2001:db8::1').unwrap();\n\t * @since v0.0.1\n\t */\n\tpublic static from(value: string): IResult<Ipv6Addr> {\n\t\tif (!Ipv6Addr.regex.test(value)) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv6 value`));\n\t\t}\n\t\ttry {\n\t\t\tconst {leftParts, rightParts, embeddedIpv4} = Ipv6Addr.#parseComponents(value);\n\n\t\t\tconst leftSegs = leftParts.map((p) => parseInt(p, 16));\n\t\t\tconst rightSegs = rightParts.map((p) => parseInt(p, 16));\n\n\t\t\tconst missingLen = 8 - (leftSegs.length + rightSegs.length + embeddedIpv4.length);\n\t\t\tconst segments = [...leftSegs, ...Array(missingLen).fill(0), ...rightSegs, ...embeddedIpv4];\n\n\t\t\treturn Ok(new Ipv6Addr(segments as [number, number, number, number, number, number, number, number]));\n\t\t} catch (err) {\n\t\t\treturn Err(err);\n\t\t}\n\t}\n\n\t/**\n\t * Creates an IPv6 address from a 16-byte buffer.\n\t * @returns A successful result with an IPv6 address, or an error when the buffer cannot be read.\n\t * @since v0.0.1\n\t */\n\tpublic static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv6Addr> {\n\t\ttry {\n\t\t\tconst view = new DataView(buffer);\n\t\t\tif (littleEndian) {\n\t\t\t\tconst low = view.getBigUint64(0, true);\n\t\t\t\tconst high = view.getBigUint64(8, true);\n\t\t\t\treturn Ok(new Ipv6Addr((high << 64n) | low));\n\t\t\t} else {\n\t\t\t\tconst high = view.getBigUint64(0, false);\n\t\t\t\tconst low = view.getBigUint64(8, false);\n\t\t\t\treturn Ok(new Ipv6Addr((high << 64n) | low));\n\t\t\t}\n\t\t} catch (err) {\n\t\t\treturn Err(err as Error);\n\t\t}\n\t}\n\n\tstatic #parseComponents(value: string) {\n\t\tconst components = value.split('::');\n\t\tconst leftParts = components[0].split(':').filter((x) => x !== '');\n\t\tconst rightParts = components.length > 1 ? components[1].split(':').filter((x) => x !== '') : [];\n\t\tlet embeddedIpv4: number[] = [];\n\t\tconst lastPart = rightParts[rightParts.length - 1] ?? leftParts[leftParts.length - 1];\n\t\tif (lastPart?.includes('.')) {\n\t\t\tembeddedIpv4 = Ipv6Addr.#parseEmbeddedIpv4(lastPart);\n\t\t\tif (rightParts.length > 0) {\n\t\t\t\trightParts.pop();\n\t\t\t} else {\n\t\t\t\tleftParts.pop();\n\t\t\t}\n\t\t}\n\t\treturn {leftParts, rightParts, embeddedIpv4};\n\t}\n\n\tstatic #parseEmbeddedIpv4(value: string): number[] {\n\t\tconst ipv4Octets = value.split('.').map(Number);\n\t\treturn [(ipv4Octets[0] << 8) | ipv4Octets[1], (ipv4Octets[2] << 8) | ipv4Octets[3]];\n\t}\n\n\t/**\n\t * The number of bits in an IPv6 address.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BITS = 128;\n\n\t/**\n\t * The loopback address (`::1`).\n\t * @since v0.0.1\n\t */\n\tpublic static readonly LOCALHOST: Ipv6Addr = new Ipv6Addr(0x00000000000000000000000000000001n);\n\n\t/**\n\t * The unspecified address (`::`).\n\t * @since v0.0.1\n\t */\n\tpublic static readonly UNSPECIFIED: Ipv6Addr = new Ipv6Addr(0x00000000000000000000000000000000n);\n\n\t/**\n\t * The address family identifier for IPv6 addresses.\n\t * @since v0.0.1\n\t */\n\tpublic readonly family = 'ipv6';\n\n\t#integerAddress: bigint;\n\tpublic constructor(value: bigint);\n\tpublic constructor(segments: [number, number, number, number, number, number, number, number]);\n\tpublic constructor(valueOrSegments: [number, number, number, number, number, number, number, number] | bigint) {\n\t\tif (Array.isArray(valueOrSegments)) {\n\t\t\tthis.#integerAddress = this.#toInteger(valueOrSegments);\n\t\t} else {\n\t\t\tthis.#integerAddress = valueOrSegments;\n\t\t}\n\t}\n\n\t/**\n\t * Checks whether this address is in the benchmarking range.\n\t * @see https://tools.ietf.org/html/rfc5180 and https://www.rfc-editor.org/errata_search.php?eid=1752\n\t * @returns `true` when the address is in `2001:2::/48`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBenchmarking(): boolean {\n\t\treturn this.#match(0x20010002000000000000000000000000n, 48);\n\t}\n\n\t/**\n\t * Checks whether this address is in a documentation-only range.\n\t * @see https://tools.ietf.org/html/rfc3849 and https://tools.ietf.org/html/rfc9637\n\t * @returns `true` for `2001:db8::/32` or `3fff::/20`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isDocumentation(): boolean {\n\t\treturn this.#match(0x20010db8000000000000000000000000n, 32) || this.#match(0x3fff0000000000000000000000000000n, 20);\n\t}\n\n\t/**\n\t * Checks whether this address is the loopback address.\n\t * @see https://tools.ietf.org/html/rfc4291#section-2.5.3\n\t * @returns `true` when the address is `::1`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLoopback(): boolean {\n\t\treturn this.#integerAddress === 1n;\n\t}\n\n\t/**\n\t * Checks whether this address is the unspecified address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is `::`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnspecified(): boolean {\n\t\treturn this.#integerAddress === 0n;\n\t}\n\n\t/**\n\t * Checks whether this address is a multicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is in `ff00::/8`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isMulticast(): boolean {\n\t\treturn this.#integerAddress >> 120n === 0xffn;\n\t}\n\n\t/**\n\t * Checks whether this address is an IPv4-mapped IPv6 address.\n\t * @returns `true` when the address is in `::ffff:0:0/96`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isIpv4Mapped(): boolean {\n\t\treturn this.#match(0x00000000000000000000ffff00000000n, 96);\n\t}\n\n\t/**\n\t * Checks whether this address is a unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is not multicast, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicast(): boolean {\n\t\treturn !this.isMulticast();\n\t}\n\n\t/**\n\t * Checks whether this address is a link-local unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291\n\t * @returns `true` when the address is in `fe80::/10`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicastLinkLocal(): boolean {\n\t\treturn this.#match(0xfe800000000000000000000000000000n, 10);\n\t}\n\n\t/**\n\t * Checks whether this address is a global unicast address.\n\t * @see https://tools.ietf.org/html/rfc4291#section-2.5.7\n\t * @returns `true` when the address is unicast and not loopback, link-local, private, unspecified, or documentation.\n\t * @since v0.0.1\n\t */\n\tpublic isUnicastGlobal(): boolean {\n\t\treturn this.isUnicast() && !this.isLoopback() && !this.isUnicastLinkLocal() && !this.isUniqueLocal() && !this.isUnspecified() && !this.isDocumentation();\n\t}\n\n\t/**\n\t * Checks whether this address appears globally reachable.\n\t * @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml\n\t * @returns `true` when the address is not in known non-global special ranges.\n\t * @since v0.0.1\n\t */\n\tpublic isGlobal(): boolean {\n\t\treturn (\n\t\t\t!this.isUnspecified() &&\n\t\t\t!this.isLoopback() &&\n\t\t\t!this.isIpv4Mapped() &&\n\t\t\t!this.isBenchmarking() &&\n\t\t\t!this.isDocumentation() &&\n\t\t\t!this.isUniqueLocal() &&\n\t\t\t!this.isUnicastLinkLocal() &&\n\t\t\t!this.isMulticast()\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the unique-local range.\n\t * @see https://tools.ietf.org/html/rfc4193\n\t * @returns `true` when the address is in `fc00::/7`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUniqueLocal(): boolean {\n\t\treturn this.#match(0xfc000000000000000000000000000000n, 7);\n\t}\n\n\t/**\n\t * Converts this address to IPv4 when compatible or mapped.\n\t * @returns An IPv4 address for `::a.b.c.d` or `::ffff:a.b.c.d`; otherwise `None`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv4(): IOption<Ipv4Addr> {\n\t\tif (this.isIpv4Mapped() || this.#match(0n, 96)) {\n\t\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\t\treturn Some(new Ipv4Addr(segments[6] >> 8, segments[6] & 0xff, segments[7] >> 8, segments[7] & 0xff));\n\t\t}\n\t\treturn None();\n\t}\n\n\t/**\n\t * Converts this address to IPv4 only when it is IPv4-mapped.\n\t * @returns An IPv4 address for `::ffff:a.b.c.d`; otherwise `None`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv4Mapped(): IOption<Ipv4Addr> {\n\t\tif (this.isIpv4Mapped()) {\n\t\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\t\treturn Some(new Ipv4Addr(segments[6] >> 8, segments[6] & 0xff, segments[7] >> 8, segments[7] & 0xff));\n\t\t}\n\t\treturn None();\n\t}\n\n\t/**\n\t * Formats this address as a compressed IPv6 string.\n\t * @returns The shortest standard IPv6 text form.\n\t * @since v0.0.1\n\t */\n\tpublic toString(): string {\n\t\tconst segments = this.#fromInteger(this.#integerAddress);\n\t\tlet maxZeroStart = -1;\n\t\tlet maxZeroLen = 0;\n\t\tlet currentZeroStart = -1;\n\t\tlet currentZeroLen = 0;\n\n\t\tfor (let i = 0; i < segments.length; i++) {\n\t\t\tif (segments[i] === 0) {\n\t\t\t\tif (currentZeroStart === -1) {\n\t\t\t\t\tcurrentZeroStart = i;\n\t\t\t\t\tcurrentZeroLen = 1;\n\t\t\t\t} else {\n\t\t\t\t\tcurrentZeroLen++;\n\t\t\t\t}\n\t\t\t\tif (currentZeroLen > maxZeroLen) {\n\t\t\t\t\tmaxZeroLen = currentZeroLen;\n\t\t\t\t\tmaxZeroStart = currentZeroStart;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tcurrentZeroStart = -1;\n\t\t\t\tcurrentZeroLen = 0;\n\t\t\t}\n\t\t}\n\n\t\tif (maxZeroLen < 2) {\n\t\t\treturn segments.map((s) => s.toString(16)).join(':');\n\t\t}\n\n\t\tconst before = segments.slice(0, maxZeroStart).map((s) => s.toString(16));\n\t\tconst after = segments.slice(maxZeroStart + maxZeroLen).map((s) => s.toString(16));\n\n\t\treturn `${before.join(':')}::${after.join(':')}`;\n\t}\n\n\t/**\n\t * Encodes this address to a 16-byte buffer.\n\t * @param littleEndian Whether to use little-endian byte order. Defaults to `false`.\n\t * @returns An `ArrayBuffer` containing the IPv6 integer value.\n\t * @since v0.0.1\n\t */\n\tpublic toBuffer(littleEndian?: boolean): ArrayBuffer {\n\t\tconst buffer = new ArrayBuffer(16);\n\t\tconst view = new DataView(buffer);\n\t\tif (littleEndian) {\n\t\t\tview.setBigUint64(0, this.#integerAddress & 0xffffffffffffffffn, true);\n\t\t\tview.setBigUint64(8, this.#integerAddress >> 64n, true);\n\t\t} else {\n\t\t\tview.setBigUint64(0, this.#integerAddress >> 64n, false);\n\t\t\tview.setBigUint64(8, this.#integerAddress & 0xffffffffffffffffn, false);\n\t\t}\n\t\treturn buffer;\n\t}\n\n\t#toInteger(segments: [number, number, number, number, number, number, number, number]): bigint {\n\t\tlet result = 0n;\n\t\tfor (const segment of segments) {\n\t\t\tif (segment < 0 || segment > 0xffff) {\n\t\t\t\tthrow new Error('IPv6 segment must be between 0 and 65535');\n\t\t\t}\n\t\t\tresult = (result << 16n) | BigInt(segment);\n\t\t}\n\t\treturn result;\n\t}\n\n\t#fromInteger(integer: bigint): [number, number, number, number, number, number, number, number] {\n\t\treturn [\n\t\t\tNumber((integer >> 112n) & 0xffffn),\n\t\t\tNumber((integer >> 96n) & 0xffffn),\n\t\t\tNumber((integer >> 80n) & 0xffffn),\n\t\t\tNumber((integer >> 64n) & 0xffffn),\n\t\t\tNumber((integer >> 48n) & 0xffffn),\n\t\t\tNumber((integer >> 32n) & 0xffffn),\n\t\t\tNumber((integer >> 16n) & 0xffffn),\n\t\t\tNumber(integer & 0xffffn),\n\t\t];\n\t}\n\n\t#match(network: bigint, prefix: number): boolean {\n\t\tif (prefix < 0 || prefix > 128) {\n\t\t\tthrow new Error('Invalid prefix length');\n\t\t}\n\t\tif (prefix === 0) {\n\t\t\treturn true;\n\t\t}\n\t\tconst ALL_ONES = (1n << 128n) - 1n;\n\t\tconst mask = (ALL_ONES << BigInt(128 - prefix)) & ALL_ONES;\n\t\treturn (this.#integerAddress & mask) === (network & mask);\n\t}\n}\n","import {Err, type IResult, Ok} from '@luolapeikko/result-option';\nimport {Ipv6Addr} from './Ipv6Addr';\n\n/**\n * Represents an IPv4 address.\n * @example\n * const addr1 = Ipv4Addr.from('192.168.0.1').unwrap();\n * const addr2 = new Ipv4Addr(192, 168, 0, 1);\n * const addr3 = new Ipv4Addr(0xc0a80001);\n * @since v0.0.1\n */\nexport class Ipv4Addr {\n\t/**\n\t * Creates an IPv4 address from dotted-decimal text.\n\t * @returns A successful {@link IResult} with an IPv4 address, or an error when the input is invalid.\n\t * @since v0.0.1\n\t */\n\tpublic static from(value: string): IResult<Ipv4Addr, TypeError> {\n\t\tconst match = value.match(/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/);\n\t\tif (!match) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv4 value`));\n\t\t}\n\t\tconst octets = match.slice(1).map(Number);\n\t\tif (octets.some((o) => o > 255)) {\n\t\t\treturn Err(new TypeError(`${value} is invalid ipv4 value`));\n\t\t}\n\t\treturn Ok(new Ipv4Addr(octets[0], octets[1], octets[2], octets[3]));\n\t}\n\n\t/**\n\t * Creates an IPv4 address from a 4-byte buffer.\n\t * @returns A successful {@link IResult} with an IPv4 address, or an error when the buffer cannot be read.\n\t * @since v0.0.1\n\t */\n\tpublic static fromBuffer(buffer: ArrayBuffer, littleEndian?: boolean): IResult<Ipv4Addr> {\n\t\ttry {\n\t\t\tconst view = new DataView(buffer);\n\t\t\treturn Ok(new Ipv4Addr(view.getUint32(0, littleEndian)));\n\t\t} catch (err) {\n\t\t\treturn Err(err);\n\t\t}\n\t}\n\n\t/**\n\t * The number of bits in an IPv4 address.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BITS = 32;\n\n\t/**\n\t * The broadcast address `255.255.255.255`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly BROADCAST: Ipv4Addr = new Ipv4Addr(0xffffffff);\n\n\t/**\n\t * The localhost address `127.0.0.1`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly LOCALHOST: Ipv4Addr = new Ipv4Addr(0x7f000001);\n\n\t/**\n\t * The unspecified address `0.0.0.0`.\n\t * @since v0.0.1\n\t */\n\tpublic static readonly UNSPECIFIED: Ipv4Addr = new Ipv4Addr(0x00000000);\n\n\t/**\n\t * The address family identifier for IPv4 addresses.\n\t * @since v0.0.1\n\t */\n\tpublic readonly family = 'ipv4';\n\n\t#integerAddress: number;\n\n\t/**\n\t * Creates a new IPv4 address from four octets.\n\t * @param num1 The first octet.\n\t * @param num2 The second octet.\n\t * @param num3 The third octet.\n\t * @param num4 The fourth octet.\n\t * @example\n\t * new Ipv4Addr(192, 168, 0, 1) // creates the address `192.168.0.1`\n\t */\n\tpublic constructor(num1: number, num2: number, num3: number, num4: number);\n\t/**\n\t * Creates a new IPv4 address from an integer value.\n\t * @param integerValue The integer representation of the IPv4 address.\n\t * @example\n\t * new Ipv4Addr(0xc0a80001) // creates the address `192.168.0.1` from the integer value `0xc0a80001`\n\t */\n\tpublic constructor(integerValue: number);\n\tpublic constructor(...args: [number] | [number, number, number, number]) {\n\t\tif (args.length === 1) {\n\t\t\tthis.#integerAddress = args[0];\n\t\t} else {\n\t\t\tthis.#integerAddress = this.#toInteger(args[0], args[1], args[2], args[3]);\n\t\t}\n\t}\n\n\t/**\n\t * Checks whether this address is the broadcast address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc919#section-7\n\t * @returns `true` when the address is `255.255.255.255`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBroadcast(): boolean {\n\t\treturn this.#integerAddress === 0xffffffff;\n\t}\n\n\t/**\n\t * Checks whether this address is in a documentation-only range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc5737\n\t * @returns `true` for `192.0.2.0/24`, `198.51.100.0/24`, or `203.0.113.0/24`; otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isDocumentation(): boolean {\n\t\treturn (\n\t\t\tthis.#match(0xc0000200, 24) || // 192.0.2.0/24\n\t\t\tthis.#match(0xc6336400, 24) || // 198.51.100.0/24\n\t\t\tthis.#match(0xcb007100, 24) // 203.0.113.0/24\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the benchmarking range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc2544\n\t * @returns `true` when the address is in `198.18.0.0/15`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isBenchmarking(): boolean {\n\t\treturn this.#match(0xc6120000, 15);\n\t}\n\n\t/**\n\t * Checks whether this address is globally reachable.\n\t * @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml\n\t * @returns `true` when the address is not in any special non-global range.\n\t * @since v0.0.1\n\t */\n\tpublic isGlobal(): boolean {\n\t\treturn !(\n\t\t\tthis.isUnspecified() ||\n\t\t\tthis.isPrivate() ||\n\t\t\tthis.isShared() ||\n\t\t\tthis.isLoopback() ||\n\t\t\tthis.isLinkLocal() ||\n\t\t\tthis.isDocumentation() ||\n\t\t\tthis.isBenchmarking() ||\n\t\t\tthis.isReserved() ||\n\t\t\tthis.isMulticast() ||\n\t\t\tthis.isBroadcast()\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is link-local.\n\t * @see https://datatracker.ietf.org/doc/html/rfc3927\n\t * @returns `true` when the address is in `169.254.0.0/16`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLinkLocal(): boolean {\n\t\treturn this.#match(0xa9fe0000, 16);\n\t}\n\n\t/**\n\t * Checks whether this address is a loopback address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1122\n\t * @returns `true` when the address is in `127.0.0.0/8`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isLoopback(): boolean {\n\t\treturn this.#match(0x7f000000, 8);\n\t}\n\n\t/**\n\t * Checks whether this address is a multicast address.\n\t * @see https://datatracker.ietf.org/doc/html/rfc5771\n\t * @returns `true` when the address is in `224.0.0.0/4`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isMulticast(): boolean {\n\t\treturn this.#match(0xe0000000, 4);\n\t}\n\n\t/**\n\t * Checks whether this address is in a private-use range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1918\n\t * @returns `true` for `10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`; otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isPrivate(): boolean {\n\t\treturn (\n\t\t\tthis.#match(0x0a000000, 8) || // 10.0.0.0/8\n\t\t\tthis.#match(0xac100000, 12) || // 172.16.0.0/12\n\t\t\tthis.#match(0xc0a80000, 16) // 192.168.0.0/16\n\t\t);\n\t}\n\n\t/**\n\t * Checks whether this address is in the reserved range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc1112\n\t * @returns `true` when the address is in `240.0.0.0/4` except the broadcast address.\n\t * @since v0.0.1\n\t */\n\tpublic isReserved(): boolean {\n\t\treturn this.#match(0xf0000000, 4) && !this.isBroadcast();\n\t}\n\n\t/**\n\t * Checks whether this address is in the shared Carrier-Grade NAT range.\n\t * @see https://datatracker.ietf.org/doc/html/rfc6598\n\t * @returns `true` when the address is in `100.64.0.0/10`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isShared(): boolean {\n\t\treturn this.#match(0x64400000, 10);\n\t}\n\t\n\t/**\n\t * Checks whether this address is the unspecified address.\n\t * @returns `true` when the address is `0.0.0.0`, otherwise `false`.\n\t * @since v0.0.1\n\t */\n\tpublic isUnspecified(): boolean {\n\t\treturn this.#integerAddress === 0;\n\t}\n\n\t/**\n\t * Converts this address to an IPv4-compatible IPv6 address.\n\t * @returns An IPv6 address in the form `::a.b.c.d`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv6(): Ipv6Addr {\n\t\treturn Ipv6Addr.from(`::${this.toString()}`).unwrap();\n\t}\n\n\t/**\n\t * Converts this address to an IPv4-mapped IPv6 address.\n\t * @returns An IPv6 address in the form `::ffff:a.b.c.d`.\n\t * @since v0.0.1\n\t */\n\tpublic toIpv6Mapped(): Ipv6Addr {\n\t\treturn Ipv6Addr.from(`::ffff:${this.toString()}`).unwrap();\n\t}\n\n\t/**\n\t * Formats this address as dotted-decimal text.\n\t * @returns The IPv4 string representation, such as `192.168.0.1`.\n\t * @since v0.0.1\n\t */\n\tpublic toString(): string {\n\t\tconst [num1, num2, num3, num4] = this.#fromInteger(this.#integerAddress);\n\t\treturn `${num1}.${num2}.${num3}.${num4}`;\n\t}\n\n\t/**\n\t * Encodes this address to a 4-byte buffer.\n\t * @returns An `ArrayBuffer` containing the IPv4 integer value.\n\t * @since v0.0.1\n\t */\n\tpublic toBuffer(littleEndian?: boolean): ArrayBuffer {\n\t\tconst buffer = new ArrayBuffer(4);\n\t\tconst view = new DataView(buffer);\n\t\tview.setUint32(0, this.#integerAddress, littleEndian);\n\t\treturn buffer;\n\t}\n\n\t#toInteger(num1: number, num2: number, num3: number, num4: number): number {\n\t\tfor (const octet of [num1, num2, num3, num4]) {\n\t\t\tif (octet < 0 || octet > 255) {\n\t\t\t\tthrow new Error('IPv4 octet must be between 0 and 255');\n\t\t\t}\n\t\t}\n\t\treturn ((num1 << 24) | (num2 << 16) | (num3 << 8) | num4) >>> 0;\n\t}\n\n\t#fromInteger(integer: number): [number, number, number, number] {\n\t\tconst num1 = (integer >> 24) & 0xff;\n\t\tconst num2 = (integer >> 16) & 0xff;\n\t\tconst num3 = (integer >> 8) & 0xff;\n\t\tconst num4 = integer & 0xff;\n\t\treturn [num1, num2, num3, num4];\n\t}\n\n\t#match(network: number, prefix: number): boolean {\n\t\tif (prefix < 0 || prefix > 32) {\n\t\t\tthrow new Error('Invalid prefix length');\n\t\t}\n\t\tconst mask = pref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EAAK,IAAO,GAAK,IAAa,EAC1D,OAAQ,MAAA,EAAuB,MAAW,EAAU"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "net-address",
3
+ "version": "0.0.1",
4
+ "description": "Network address instances",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.mts",
8
+ "exports": {
9
+ "import": {
10
+ "types": "./dist/index.d.mts",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.cts",
15
+ "default": "./dist/index.cjs"
16
+ },
17
+ "default": "./dist/index.mjs"
18
+ },
19
+ "scripts": {
20
+ "build": "tsdown src/index.ts --sourcemap --minify --format cjs,esm --dts --clean -c tsconfig.build.json",
21
+ "validate": "tsc --noEmit"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "repository": "github:luolapeikko/net-address-base",
27
+ "keywords": [
28
+ "network",
29
+ "address"
30
+ ],
31
+ "author": "mharj",
32
+ "license": "MIT",
33
+ "bugs": {
34
+ "url": "https://github.com/luolapeikko/net-address-base/issues"
35
+ },
36
+ "homepage": "https://github.com/luolapeikko/net-address-base#readme",
37
+ "devDependencies": {
38
+ "@luolapeikko/result-option": "2.2.1"
39
+ },
40
+ "peerDependencies": {
41
+ "@luolapeikko/result-option": ">= 2.0.0"
42
+ }
43
+ }