Skip to content

Commit

Permalink
feat: add matchers for HTTP and HTTPS addresses (#23)
Browse files Browse the repository at this point in the history
The spec isn't well defined but these are the addresses in the wild.
  • Loading branch information
achingbrain committed Mar 14, 2024
1 parent ed36012 commit 8bfc218
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,45 @@ const _WebRTC = or(
* ```
*/
export const WebRTC = fmt(_WebRTC)

const _HTTP = or(
and(_IP_OR_DOMAIN, literal('tcp'), number(), literal('http'), optional(peerId())),
and(_IP_OR_DOMAIN, literal('http'), optional(peerId()))
)

/**
* Matches HTTP addresses
*
* @example
*
* ```ts
* import { multiaddr } from '@multiformats/multiaddr'
* import { HTTP } from '@multiformats/multiaddr-matcher'
*
* HTTP.matches(multiaddr('/dns/example.org/http')) // true
* ```
*/
export const HTTP = fmt(_HTTP)

const _HTTPS = or(
and(_IP_OR_DOMAIN, literal('tcp'), or(
and(literal('443'), literal('http')),
and(number(), literal('https'))
), optional(peerId())),
and(_IP_OR_DOMAIN, literal('tls'), literal('http'), optional(peerId())),
and(_IP_OR_DOMAIN, literal('https'), optional(peerId()))
)

/**
* Matches HTTPS addresses
*
* @example
*
* ```ts
* import { multiaddr } from '@multiformats/multiaddr'
* import { HTTP } from '@multiformats/multiaddr-matcher'
*
* HTTP.matches(multiaddr('/dns/example.org/tls/http')) // true
* ```
*/
export const HTTPS = fmt(_HTTPS)
53 changes: 53 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,47 @@ describe('multiaddr matcher', () => {
'/unix/var/log'
]

const exactHTTP = [
'/ip4/0.0.0.0/tcp/80/http',
'/ip6/fc00::/tcp/80/http',
'/dns4/example.org/tcp/80/http',
'/dns6/example.org/tcp/80/http',
'/dnsaddr/example.org/tcp/80/http',
'/dns/example.org/tcp/7777/http',
'/dns/example.org/tcp/7777/http/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const goodHTTP = [
...exactHTTP
]

const badHTTP = [
'/ip4/0.0.0.0/udp/80/http'
]

const exactHTTPS = [
'/ip4/0.0.0.0/tcp/0/https',
'/ip6/fc00::/tcp/0/https',
'/dns4/example.org/tcp/80/https',
'/dns6/example.org/tcp/80/https',
'/dnsaddr/example.org/tcp/80/https',
'/dns/example.org/tcp/7777/https',
'/dns4/example.org/tcp/443/http',
'/dns6/example.org/tcp/443/http',
'/dnsaddr/example.org/tcp/443/http',
'/dns/example.org/tcp/443/http',
'/dns4/example.org/tls/http',
'/dns/example.org/tls/http/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const goodHTTPS = [
...exactHTTPS
]

const badHTTPS = [
'/ip4/0.0.0.0/udp/80/http'
]

function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
tests.forEach((test) => {
test.forEach((testcase) => {
Expand Down Expand Up @@ -350,4 +391,16 @@ describe('multiaddr matcher', () => {
assertExactMatches(mafmt.IP_OR_DOMAIN, exactIPorDomain)
assertMismatches(mafmt.IP_OR_DOMAIN, badIPorDomain)
})

it('HTTP addresses', () => {
assertMatches(mafmt.HTTP, goodHTTP)
assertExactMatches(mafmt.HTTP, exactHTTP)
assertMismatches(mafmt.HTTP, badHTTP)
})

it('HTTPS addresses', () => {
assertMatches(mafmt.HTTPS, goodHTTPS)
assertExactMatches(mafmt.HTTPS, exactHTTPS)
assertMismatches(mafmt.HTTPS, badHTTPS)
})
})

0 comments on commit 8bfc218

Please sign in to comment.