Skip to content

Commit

Permalink
feat: add unix address matchers
Browse files Browse the repository at this point in the history
Need to resolve how to encode unix paths so peer ids can be appended
to them - multiformats/multiaddr#174
  • Loading branch information
achingbrain committed Nov 4, 2024
1 parent d9337aa commit 72b0c54
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,21 @@ const _HTTPS = or(
* ```
*/
export const HTTPS = fmt(_HTTPS)

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 @@ -309,6 +309,19 @@ describe('multiaddr matcher', () => {
'/ip4/0.0.0.0/udp/80/http'
]

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 @@ -416,4 +429,10 @@ describe('multiaddr matcher', () => {
assertExactMatches(mafmt.HTTPS, exactHTTPS)
assertMismatches(mafmt.HTTPS, badHTTPS)
})

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

0 comments on commit 72b0c54

Please sign in to comment.