Skip to content

Commit 7ec36d2

Browse files
authored
fix: allow optional multiaddrs (#62)
Allow passing undefined multiaddrs which would then fail to match.
1 parent 1c5ea96 commit 7ec36d2

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ export interface MultiaddrMatcher {
5959
* Returns true if the passed multiaddr can be treated as this type of
6060
* multiaddr
6161
*/
62-
matches(ma: Multiaddr): boolean
62+
matches(ma?: Multiaddr): boolean
6363

6464
/**
6565
* Returns true if the passed multiaddr terminates as this type of
6666
* multiaddr
6767
*/
68-
exactMatch(ma: Multiaddr): boolean
68+
exactMatch(ma?: Multiaddr): boolean
6969
}
7070

7171
/**

src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ export const and = (...matchers: Matcher[]): Matcher => {
127127
* Create a multiaddr matcher from the passed component matchers
128128
*/
129129
export function fmt (...matchers: Matcher[]): MultiaddrMatcher {
130-
function match (ma: Multiaddr): Component[] | false {
130+
function match (ma?: Multiaddr): Component[] | false {
131+
if (ma == null) {
132+
return false
133+
}
134+
131135
let parts = ma.getComponents()
132136

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

146-
function matches (ma: Multiaddr): boolean {
150+
function matches (ma?: Multiaddr): boolean {
147151
const result = match(ma)
148152

149153
return result !== false
150154
}
151155

152-
function exactMatch (ma: Multiaddr): boolean {
156+
function exactMatch (ma?: Multiaddr): boolean {
153157
const result = match(ma)
154158

155159
if (result === false) {

test/index.spec.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ describe('multiaddr matcher', () => {
2222
]
2323

2424
const badDNS = [
25+
null,
26+
undefined,
2527
'/ip4/127.0.0.1'
2628
]
2729

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

4648
const badIP = [
49+
null,
50+
undefined,
4751
'/udp/789/ip6/fc00::'
4852
]
4953

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

6670
const badTCP = [
71+
null,
72+
undefined,
6773
'/tcp/12345',
6874
'/ip6/fc00::/udp/5523/tcp/9543',
6975
'/dns4/protocol.ai'
@@ -81,6 +87,8 @@ describe('multiaddr matcher', () => {
8187
]
8288

8389
const badUDP = [
90+
null,
91+
undefined,
8492
'/udp/12345',
8593
'/ip6/fc00::/tcp/5523/udp/9543'
8694
]
@@ -99,6 +107,8 @@ describe('multiaddr matcher', () => {
99107
]
100108

101109
const badQUIC = [
110+
null,
111+
undefined,
102112
'/ip4/0.0.0.0/tcp/12345/quic',
103113
'/ip6/fc00::/ip4/0.0.0.0/udp/1234/quic',
104114
'/quic'
@@ -118,6 +128,8 @@ describe('multiaddr matcher', () => {
118128
]
119129

120130
const badQUICv1 = [
131+
null,
132+
undefined,
121133
'/ip4/0.0.0.0/tcp/12345/quic-v1',
122134
'/ip6/fc00::/ip4/0.0.0.0/udp/1234/quic-v1',
123135
'/quic-v1',
@@ -186,12 +198,16 @@ describe('multiaddr matcher', () => {
186198
]
187199

188200
const badWS = [
201+
null,
202+
undefined,
189203
'/ip4/0.0.0.0/tcp/12345/udp/2222/ws',
190204
'/ip6/::/ip4/0.0.0.0/udp/1234/ws',
191205
'/ip4/127.0.0.1/tcp/24642/p2p-webrtc-star/ws'
192206
]
193207

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

211227
const badCircuit = [
228+
null,
229+
undefined,
212230
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
213231
'/ip4/0.0.7.6/udp/1234',
214232
'/ip6/::/udp/0/utp',
@@ -245,6 +263,8 @@ describe('multiaddr matcher', () => {
245263
]
246264

247265
const badWebRTCDirect = [
266+
null,
267+
undefined,
248268
'/ip4/0.0.0.0/tcp/4004/webrtc-direct',
249269
'/ip4/0.0.0.0/tcp/4004/webrtc-direct/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw',
250270
'/ip4/0.0.0.0/tcp/4004/webrtc-direct/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
@@ -266,6 +286,8 @@ describe('multiaddr matcher', () => {
266286
]
267287

268288
const badWebRTC = [
289+
null,
290+
undefined,
269291
'/ip4/0.0.0.0/udp/0/webrtc',
270292
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss/webrtc'
271293
]
@@ -280,6 +302,8 @@ describe('multiaddr matcher', () => {
280302
]
281303

282304
const badWebTransport = [
305+
null,
306+
undefined,
283307
// quic instead of quic-v1
284308
'/ip4/10.5.0.2/udp/4001/quic/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
285309
]
@@ -296,6 +320,8 @@ describe('multiaddr matcher', () => {
296320
]
297321

298322
const badIPorDomain = [
323+
null,
324+
undefined,
299325
'/webrtc/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
300326
'/quic',
301327
'/unix/var%2Flog'
@@ -316,6 +342,8 @@ describe('multiaddr matcher', () => {
316342
]
317343

318344
const badHTTP = [
345+
null,
346+
undefined,
319347
'/ip4/0.0.0.0/udp/80/http'
320348
]
321349

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

343371
const badHTTPS = [
372+
null,
373+
undefined,
344374
'/ip4/0.0.0.0/udp/80/http'
345375
]
346376

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

357387
const badMemory = [
388+
null,
389+
undefined,
358390
'/ip4/0.0.0.0/udp/80/http'
359391
]
360392

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

372404
const badPeer = [
405+
null,
406+
undefined,
373407
'/ip4/0.0.0.0/udp/80/http',
374408
'/memory/0xDEADBEEF'
375409
]
@@ -384,6 +418,8 @@ describe('multiaddr matcher', () => {
384418
]
385419

386420
const badUnix = [
421+
null,
422+
undefined,
387423
'/ip4/0.0.0.0/tcp/0/https'
388424
]
389425

@@ -403,7 +439,7 @@ describe('multiaddr matcher', () => {
403439
})
404440
}
405441

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

0 commit comments

Comments
 (0)