Skip to content

Commit 8bfc218

Browse files
authored
feat: add matchers for HTTP and HTTPS addresses (#23)
The spec isn't well defined but these are the addresses in the wild.
1 parent ed36012 commit 8bfc218

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,45 @@ const _WebRTC = or(
620620
* ```
621621
*/
622622
export const WebRTC = fmt(_WebRTC)
623+
624+
const _HTTP = or(
625+
and(_IP_OR_DOMAIN, literal('tcp'), number(), literal('http'), optional(peerId())),
626+
and(_IP_OR_DOMAIN, literal('http'), optional(peerId()))
627+
)
628+
629+
/**
630+
* Matches HTTP addresses
631+
*
632+
* @example
633+
*
634+
* ```ts
635+
* import { multiaddr } from '@multiformats/multiaddr'
636+
* import { HTTP } from '@multiformats/multiaddr-matcher'
637+
*
638+
* HTTP.matches(multiaddr('/dns/example.org/http')) // true
639+
* ```
640+
*/
641+
export const HTTP = fmt(_HTTP)
642+
643+
const _HTTPS = or(
644+
and(_IP_OR_DOMAIN, literal('tcp'), or(
645+
and(literal('443'), literal('http')),
646+
and(number(), literal('https'))
647+
), optional(peerId())),
648+
and(_IP_OR_DOMAIN, literal('tls'), literal('http'), optional(peerId())),
649+
and(_IP_OR_DOMAIN, literal('https'), optional(peerId()))
650+
)
651+
652+
/**
653+
* Matches HTTPS addresses
654+
*
655+
* @example
656+
*
657+
* ```ts
658+
* import { multiaddr } from '@multiformats/multiaddr'
659+
* import { HTTP } from '@multiformats/multiaddr-matcher'
660+
*
661+
* HTTP.matches(multiaddr('/dns/example.org/tls/http')) // true
662+
* ```
663+
*/
664+
export const HTTPS = fmt(_HTTPS)

test/index.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,47 @@ describe('multiaddr matcher', () => {
255255
'/unix/var/log'
256256
]
257257

258+
const exactHTTP = [
259+
'/ip4/0.0.0.0/tcp/80/http',
260+
'/ip6/fc00::/tcp/80/http',
261+
'/dns4/example.org/tcp/80/http',
262+
'/dns6/example.org/tcp/80/http',
263+
'/dnsaddr/example.org/tcp/80/http',
264+
'/dns/example.org/tcp/7777/http',
265+
'/dns/example.org/tcp/7777/http/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
266+
]
267+
268+
const goodHTTP = [
269+
...exactHTTP
270+
]
271+
272+
const badHTTP = [
273+
'/ip4/0.0.0.0/udp/80/http'
274+
]
275+
276+
const exactHTTPS = [
277+
'/ip4/0.0.0.0/tcp/0/https',
278+
'/ip6/fc00::/tcp/0/https',
279+
'/dns4/example.org/tcp/80/https',
280+
'/dns6/example.org/tcp/80/https',
281+
'/dnsaddr/example.org/tcp/80/https',
282+
'/dns/example.org/tcp/7777/https',
283+
'/dns4/example.org/tcp/443/http',
284+
'/dns6/example.org/tcp/443/http',
285+
'/dnsaddr/example.org/tcp/443/http',
286+
'/dns/example.org/tcp/443/http',
287+
'/dns4/example.org/tls/http',
288+
'/dns/example.org/tls/http/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
289+
]
290+
291+
const goodHTTPS = [
292+
...exactHTTPS
293+
]
294+
295+
const badHTTPS = [
296+
'/ip4/0.0.0.0/udp/80/http'
297+
]
298+
258299
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
259300
tests.forEach((test) => {
260301
test.forEach((testcase) => {
@@ -350,4 +391,16 @@ describe('multiaddr matcher', () => {
350391
assertExactMatches(mafmt.IP_OR_DOMAIN, exactIPorDomain)
351392
assertMismatches(mafmt.IP_OR_DOMAIN, badIPorDomain)
352393
})
394+
395+
it('HTTP addresses', () => {
396+
assertMatches(mafmt.HTTP, goodHTTP)
397+
assertExactMatches(mafmt.HTTP, exactHTTP)
398+
assertMismatches(mafmt.HTTP, badHTTP)
399+
})
400+
401+
it('HTTPS addresses', () => {
402+
assertMatches(mafmt.HTTPS, goodHTTPS)
403+
assertExactMatches(mafmt.HTTPS, exactHTTPS)
404+
assertMismatches(mafmt.HTTPS, badHTTPS)
405+
})
353406
})

0 commit comments

Comments
 (0)