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
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,21 @@ const _Memory = or(
* ```
*/
export const Memory = fmt(_Memory)

const _Unix = or(
and(literal('unix'), string(), optional(peerId()))
)

/**
* Matches Unix addresses
*
* @example
*
* ```ts
* import { multiaddr } from '@multiformats/multiaddr'
* import { Unix } from '@multiformats/multiaddr-matcher'
*
* Unix.matches(multiaddr('/unix/%2Fpath%2Fto%2Funix.socket')) // true
* ```
*/
export const Unix = fmt(_Unix)
19 changes: 19 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ describe('multiaddr matcher', () => {
'/memory/0xDEADBEEF'
]

const exactUnix = [
'/unix/%2Fpath%2Fto%2Funix.socket',
'/unix/%2Fpath%2Fto%2Funix.socket/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const goodUnix = [
...exactUnix
]

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

function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
tests.forEach((test) => {
test.forEach((testcase) => {
Expand Down Expand Up @@ -498,4 +511,10 @@ describe('multiaddr matcher', () => {
assertExactMatches(mafmt.PEER_ID, exactPeer)
assertMismatches(mafmt.PEER_ID, badPeer)
})

it('Unix addresses', () => {
assertMatches(mafmt.Unix, goodUnix)
assertExactMatches(mafmt.Unix, exactUnix)
assertMismatches(mafmt.Unix, badUnix)
})
})