From 72b0c545c33ad6635856744a43fc1f2e432b000e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 4 Nov 2024 09:23:48 +0000 Subject: [PATCH 1/2] feat: add unix address matchers Need to resolve how to encode unix paths so peer ids can be appended to them - https://github.com/multiformats/multiaddr/pull/174 --- src/index.ts | 18 ++++++++++++++++++ test/index.spec.ts | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/index.ts b/src/index.ts index 0a2a920..4f896d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/test/index.spec.ts b/test/index.spec.ts index 475dddc..ed7039d 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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) => { @@ -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) + }) }) From 1b0bec6b001b60f01c63ab7960f859cb2866dc7d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 7 Jul 2025 11:08:09 +0200 Subject: [PATCH 2/2] chore: revert line removal --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 00251ad..389cf96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -489,6 +489,7 @@ const _Memory = or( * @example * * ```ts + * import { multiaddr } from '@multiformats/multiaddr' * import { Memory } from '@multiformats/multiaddr-matcher' * * Memory.matches(multiaddr('/memory/0xDEADBEEF')) // true