Skip to content

Commit 3ecef6b

Browse files
authored
feat: IP_OR_DOMAIN matcher - e.g. IP or DNS multiaddrs (#19)
Adds a matcher for multiaddrs that start with IP or DNS tuples.
1 parent cddf1a9 commit 3ecef6b

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,23 @@ const _IP = or(
348348
_IP6
349349
)
350350

351-
const IP_OR_DOMAIN = or(_IP, _DNS, _DNS4, _DNS6, _DNSADDR)
351+
const _IP_OR_DOMAIN = or(_IP, _DNS, _DNS4, _DNS6, _DNSADDR)
352+
353+
/**
354+
* A matcher for addresses that start with IP or DNS tuples.
355+
*
356+
* @example
357+
*
358+
* ```ts
359+
* import { multiaddr } from '@multiformats/multiaddr'
360+
* import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher'
361+
*
362+
* IP_OR_DOMAIN.matches(multiaddr('/ip4/123.123.123.123/p2p/QmFoo')) // true
363+
* IP_OR_DOMAIN.matches(multiaddr('/dns/example.com/p2p/QmFoo')) // true
364+
* IP_OR_DOMAIN.matches(multiaddr('/p2p/QmFoo')) // false
365+
* ```
366+
*/
367+
export const IP_OR_DOMAIN = fmt(_IP_OR_DOMAIN)
352368

353369
/**
354370
* Matches ip4 addresses.
@@ -401,8 +417,8 @@ export const IP6 = fmt(_IP6)
401417
*/
402418
export const IP = fmt(_IP)
403419

404-
const _TCP = and(IP_OR_DOMAIN, literal('tcp'), number())
405-
const _UDP = and(IP_OR_DOMAIN, literal('udp'), number())
420+
const _TCP = and(_IP_OR_DOMAIN, literal('tcp'), number())
421+
const _UDP = and(_IP_OR_DOMAIN, literal('udp'), number())
406422

407423
const TCP_OR_UDP = or(_TCP, _UDP)
408424

@@ -468,7 +484,7 @@ export const QUIC = fmt(_QUIC)
468484
export const QUICV1 = fmt(_QUICV1)
469485

470486
const _WEB = or(
471-
IP_OR_DOMAIN,
487+
_IP_OR_DOMAIN,
472488
_TCP,
473489
_UDP,
474490
_QUIC,
@@ -549,7 +565,7 @@ const _P2P = or(
549565
_WebSocketsSecure,
550566
and(_TCP, optional(peerId())),
551567
and(QUIC_V0_OR_V1, optional(peerId())),
552-
and(IP_OR_DOMAIN, optional(peerId())),
568+
and(_IP_OR_DOMAIN, optional(peerId())),
553569
_WebRTCDirect,
554570
_WebTransport,
555571
peerId()

test/index.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@ describe('multiaddr matcher', () => {
238238
'/ip4/10.5.0.2/udp/4001/quic-v1/webtransport/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
239239
]
240240

241+
const exactIPorDomain = [
242+
...exactDNS,
243+
...exactIP
244+
]
245+
246+
const goodIPorDomain = [
247+
...exactIPorDomain,
248+
...exactDNS,
249+
...exactIP
250+
]
251+
252+
const badIPorDomain = [
253+
'/webrtc/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
254+
'/quic',
255+
'/unix/var/log'
256+
]
257+
241258
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
242259
tests.forEach((test) => {
243260
test.forEach((testcase) => {
@@ -327,4 +344,10 @@ describe('multiaddr matcher', () => {
327344
assertMatches(mafmt.WebTransport, goodWebTransport)
328345
assertMismatches(mafmt.WebTransport, badWebTransport)
329346
})
347+
348+
it('IP or Domain addresses', () => {
349+
assertMatches(mafmt.IP_OR_DOMAIN, goodIPorDomain)
350+
assertExactMatches(mafmt.IP_OR_DOMAIN, exactIPorDomain)
351+
assertMismatches(mafmt.IP_OR_DOMAIN, badIPorDomain)
352+
})
330353
})

0 commit comments

Comments
 (0)