Skip to content

Commit 72b0c54

Browse files
committed
feat: add unix address matchers
Need to resolve how to encode unix paths so peer ids can be appended to them - multiformats/multiaddr#174
1 parent d9337aa commit 72b0c54

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
@@ -460,3 +460,21 @@ const _HTTPS = or(
460460
* ```
461461
*/
462462
export const HTTPS = fmt(_HTTPS)
463+
464+
const _Unix = or(
465+
and(literal('unix'), string(), optional(peerId()))
466+
)
467+
468+
/**
469+
* Matches Unix addresses
470+
*
471+
* @example
472+
*
473+
* ```ts
474+
* import { multiaddr } from '@multiformats/multiaddr'
475+
* import { Unix } from '@multiformats/multiaddr-matcher'
476+
*
477+
* Unix.matches(multiaddr('/unix/%2Fpath%2Fto%2Funix.socket')) // true
478+
* ```
479+
*/
480+
export const Unix = fmt(_Unix)

test/index.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,19 @@ describe('multiaddr matcher', () => {
309309
'/ip4/0.0.0.0/udp/80/http'
310310
]
311311

312+
const exactUnix = [
313+
'/unix/%2Fpath%2Fto%2Funix.socket',
314+
'/unix/%2Fpath%2Fto%2Funix.socket/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
315+
]
316+
317+
const goodUnix = [
318+
...exactUnix
319+
]
320+
321+
const badUnix = [
322+
'/ip4/0.0.0.0/tcp/0/https'
323+
]
324+
312325
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
313326
tests.forEach((test) => {
314327
test.forEach((testcase) => {
@@ -416,4 +429,10 @@ describe('multiaddr matcher', () => {
416429
assertExactMatches(mafmt.HTTPS, exactHTTPS)
417430
assertMismatches(mafmt.HTTPS, badHTTPS)
418431
})
432+
433+
it('Unix addresses', () => {
434+
assertMatches(mafmt.Unix, goodUnix)
435+
assertExactMatches(mafmt.Unix, exactUnix)
436+
assertMismatches(mafmt.Unix, badUnix)
437+
})
419438
})

0 commit comments

Comments
 (0)