Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unix address matchers #41

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
})
})
Loading