diff --git a/src/index.ts b/src/index.ts index 8285339..486e1b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -471,10 +471,27 @@ const _Memory = or( * @example * * ```ts - * import { multiaddr } from '@multiformats/multiaddr' * import { Memory } from '@multiformats/multiaddr-matcher' * * Memory.matches(multiaddr('/memory/0xDEADBEEF')) // true * ``` */ 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) diff --git a/test/index.spec.ts b/test/index.spec.ts index 9a1027e..54e93f5 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -323,6 +323,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) => { @@ -436,4 +449,10 @@ describe('multiaddr matcher', () => { assertExactMatches(mafmt.Memory, exactMemory) assertMismatches(mafmt.Memory, badMemory) }) + + it('Unix addresses', () => { + assertMatches(mafmt.Unix, goodUnix) + assertExactMatches(mafmt.Unix, exactUnix) + assertMismatches(mafmt.Unix, badUnix) + }) })