Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export interface MultiaddrMatcher {
* Returns true if the passed multiaddr can be treated as this type of
* multiaddr
*/
matches(ma: Multiaddr): boolean
matches(ma?: Multiaddr): boolean

/**
* Returns true if the passed multiaddr terminates as this type of
* multiaddr
*/
exactMatch(ma: Multiaddr): boolean
exactMatch(ma?: Multiaddr): boolean
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ export const and = (...matchers: Matcher[]): Matcher => {
* Create a multiaddr matcher from the passed component matchers
*/
export function fmt (...matchers: Matcher[]): MultiaddrMatcher {
function match (ma: Multiaddr): Component[] | false {
function match (ma?: Multiaddr): Component[] | false {
if (ma == null) {
return false
}

let parts = ma.getComponents()

for (const matcher of matchers) {
Expand All @@ -143,13 +147,13 @@ export function fmt (...matchers: Matcher[]): MultiaddrMatcher {
return parts
}

function matches (ma: Multiaddr): boolean {
function matches (ma?: Multiaddr): boolean {
const result = match(ma)

return result !== false
}

function exactMatch (ma: Multiaddr): boolean {
function exactMatch (ma?: Multiaddr): boolean {
const result = match(ma)

if (result === false) {
Expand Down
38 changes: 37 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('multiaddr matcher', () => {
]

const badDNS = [
null,
undefined,
'/ip4/127.0.0.1'
]

Expand All @@ -44,6 +46,8 @@ describe('multiaddr matcher', () => {
]

const badIP = [
null,
undefined,
'/udp/789/ip6/fc00::'
]

Expand All @@ -64,6 +68,8 @@ describe('multiaddr matcher', () => {
]

const badTCP = [
null,
undefined,
'/tcp/12345',
'/ip6/fc00::/udp/5523/tcp/9543',
'/dns4/protocol.ai'
Expand All @@ -81,6 +87,8 @@ describe('multiaddr matcher', () => {
]

const badUDP = [
null,
undefined,
'/udp/12345',
'/ip6/fc00::/tcp/5523/udp/9543'
]
Expand All @@ -99,6 +107,8 @@ describe('multiaddr matcher', () => {
]

const badQUIC = [
null,
undefined,
'/ip4/0.0.0.0/tcp/12345/quic',
'/ip6/fc00::/ip4/0.0.0.0/udp/1234/quic',
'/quic'
Expand All @@ -118,6 +128,8 @@ describe('multiaddr matcher', () => {
]

const badQUICv1 = [
null,
undefined,
'/ip4/0.0.0.0/tcp/12345/quic-v1',
'/ip6/fc00::/ip4/0.0.0.0/udp/1234/quic-v1',
'/quic-v1',
Expand Down Expand Up @@ -186,12 +198,16 @@ describe('multiaddr matcher', () => {
]

const badWS = [
null,
undefined,
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
'/ip6/::/ip4/0.0.0.0/udp/1234/ws',
'/ip4/127.0.0.1/tcp/24642/p2p-webrtc-star/ws'
]

const badWSS = [
null,
undefined,
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
'/ip6/::/ip4/0.0.0.0/udp/1234/wss',
'/ip4/127.0.0.1/tcp/24642/p2p-webrtc-star/wss'
Expand All @@ -209,6 +225,8 @@ describe('multiaddr matcher', () => {
]

const badCircuit = [
null,
undefined,
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0/utp',
Expand Down Expand Up @@ -245,6 +263,8 @@ describe('multiaddr matcher', () => {
]

const badWebRTCDirect = [
null,
undefined,
'/ip4/0.0.0.0/tcp/4004/webrtc-direct',
'/ip4/0.0.0.0/tcp/4004/webrtc-direct/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw',
'/ip4/0.0.0.0/tcp/4004/webrtc-direct/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
Expand All @@ -266,6 +286,8 @@ describe('multiaddr matcher', () => {
]

const badWebRTC = [
null,
undefined,
'/ip4/0.0.0.0/udp/0/webrtc',
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss/webrtc'
]
Expand All @@ -280,6 +302,8 @@ describe('multiaddr matcher', () => {
]

const badWebTransport = [
null,
undefined,
// quic instead of quic-v1
'/ip4/10.5.0.2/udp/4001/quic/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]
Expand All @@ -296,6 +320,8 @@ describe('multiaddr matcher', () => {
]

const badIPorDomain = [
null,
undefined,
'/webrtc/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
'/quic',
'/unix/var%2Flog'
Expand All @@ -316,6 +342,8 @@ describe('multiaddr matcher', () => {
]

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

Expand All @@ -341,6 +369,8 @@ describe('multiaddr matcher', () => {
]

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

Expand All @@ -355,6 +385,8 @@ describe('multiaddr matcher', () => {
]

const badMemory = [
null,
undefined,
'/ip4/0.0.0.0/udp/80/http'
]

Expand All @@ -370,6 +402,8 @@ describe('multiaddr matcher', () => {
]

const badPeer = [
null,
undefined,
'/ip4/0.0.0.0/udp/80/http',
'/memory/0xDEADBEEF'
]
Expand All @@ -384,6 +418,8 @@ describe('multiaddr matcher', () => {
]

const badUnix = [
null,
undefined,
'/ip4/0.0.0.0/tcp/0/https'
]

Expand All @@ -403,7 +439,7 @@ describe('multiaddr matcher', () => {
})
}

function assertMismatches (p: MultiaddrMatcher, ...tests: string[][]): void {
function assertMismatches (p: MultiaddrMatcher, ...tests: Array<Array<string | null | undefined>>): void {
tests.forEach((test) => {
test.forEach((testcase) => {
expect(p.matches(multiaddr(testcase))).to.equal(false, `${testcase} matched when it should not have`)
Expand Down