Skip to content

Commit 4d5197a

Browse files
authored
feat: add unix address matchers (#41)
Match Unix addresses
1 parent fbc637b commit 4d5197a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,21 @@ const _Memory = or(
496496
* ```
497497
*/
498498
export const Memory = fmt(_Memory)
499+
500+
const _Unix = or(
501+
and(literal('unix'), string(), optional(peerId()))
502+
)
503+
504+
/**
505+
* Matches Unix addresses
506+
*
507+
* @example
508+
*
509+
* ```ts
510+
* import { multiaddr } from '@multiformats/multiaddr'
511+
* import { Unix } from '@multiformats/multiaddr-matcher'
512+
*
513+
* Unix.matches(multiaddr('/unix/%2Fpath%2Fto%2Funix.socket')) // true
514+
* ```
515+
*/
516+
export const Unix = fmt(_Unix)

test/index.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,19 @@ describe('multiaddr matcher', () => {
372372
'/memory/0xDEADBEEF'
373373
]
374374

375+
const exactUnix = [
376+
'/unix/%2Fpath%2Fto%2Funix.socket',
377+
'/unix/%2Fpath%2Fto%2Funix.socket/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
378+
]
379+
380+
const goodUnix = [
381+
...exactUnix
382+
]
383+
384+
const badUnix = [
385+
'/ip4/0.0.0.0/tcp/0/https'
386+
]
387+
375388
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
376389
tests.forEach((test) => {
377390
test.forEach((testcase) => {
@@ -498,4 +511,10 @@ describe('multiaddr matcher', () => {
498511
assertExactMatches(mafmt.PEER_ID, exactPeer)
499512
assertMismatches(mafmt.PEER_ID, badPeer)
500513
})
514+
515+
it('Unix addresses', () => {
516+
assertMatches(mafmt.Unix, goodUnix)
517+
assertExactMatches(mafmt.Unix, exactUnix)
518+
assertMismatches(mafmt.Unix, badUnix)
519+
})
501520
})

0 commit comments

Comments
 (0)