Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/

import { CODE_P2P, CODE_DNS4, CODE_DNS6, CODE_DNSADDR, CODE_DNS, CODE_IP4, CODE_IP6, CODE_TCP, CODE_UDP, CODE_QUIC, CODE_QUIC_V1, CODE_WS, CODE_WSS, CODE_TLS, CODE_SNI, CODE_WEBRTC_DIRECT, CODE_CERTHASH, CODE_WEBTRANSPORT, CODE_P2P_CIRCUIT, CODE_WEBRTC, CODE_HTTP, CODE_UNIX, CODE_HTTPS, CODE_MEMORY, CODE_IP6ZONE, CODE_IPCIDR } from '@multiformats/multiaddr'
import { and, or, optional, fmt, code, value } from './utils.js'
import { and, or, optional, fmt, code, value, not } from './utils.js'
import type { Multiaddr, Component } from '@multiformats/multiaddr'

/**
Expand Down Expand Up @@ -406,7 +406,7 @@ const _P2P = or(
*/
export const P2P = fmt(_P2P)

const _Circuit = and(_P2P, code(CODE_P2P_CIRCUIT), value(CODE_P2P))
const _Circuit = and(optional(_P2P), code(CODE_P2P_CIRCUIT), not(code(CODE_WEBRTC)), optional(value(CODE_P2P)))

/**
* Matches circuit relay addresses
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export const value = (code: number, value?: string): Matcher => {
}
}

/**
* Matches a multiaddr component with the specified code and value. If the value
* is omitted any non-undefined value is matched.
*/
export const not = (matcher: Matcher): Matcher => {
return {
match: (vals) => {
const result = matcher.match(vals)

if (result === false) {
return vals
}

return false
}
}
}

/**
* An optional matcher
*/
Expand Down
5 changes: 4 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('multiaddr matcher', () => {
]

const goodCircuit = [
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit',
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
'/ip4/0.0.0.0/tcp/12345/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
'/dns/example.org/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
Expand All @@ -231,7 +232,9 @@ describe('multiaddr matcher', () => {
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0/utp',
'/dnsaddr/ipfs.io/ws',
'/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star'
'/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star',
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc',
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
]

const goodIPFS = [
Expand Down
Loading