@@ -2,18 +2,50 @@ import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
22import { CID } from 'multiformats'
33import { base64url } from 'multiformats/bases/base64'
44import { CODE_CERTHASH , CODE_DCCP , CODE_DNS , CODE_DNS4 , CODE_DNS6 , CODE_DNSADDR , CODE_GARLIC32 , CODE_GARLIC64 , CODE_HTTP , CODE_HTTP_PATH , CODE_HTTPS , CODE_IP4 , CODE_IP6 , CODE_IP6ZONE , CODE_IPCIDR , CODE_MEMORY , CODE_NOISE , CODE_ONION , CODE_ONION3 , CODE_P2P , CODE_P2P_CIRCUIT , CODE_P2P_STARDUST , CODE_P2P_WEBRTC_DIRECT , CODE_P2P_WEBRTC_STAR , CODE_P2P_WEBSOCKET_STAR , CODE_QUIC , CODE_QUIC_V1 , CODE_SCTP , CODE_SNI , CODE_TCP , CODE_TLS , CODE_UDP , CODE_UDT , CODE_UNIX , CODE_UTP , CODE_WEBRTC , CODE_WEBRTC_DIRECT , CODE_WEBTRANSPORT , CODE_WS , CODE_WSS } from './constants.ts'
5- import { InvalidProtocolError , ValidationError } from './errors.ts'
5+ import { UnknownProtocolError , ValidationError } from './errors.ts'
66import { bytes2mb , bytes2onion , bytes2port , bytesToString , ip4ToBytes , ip4ToString , ip6StringToValue , ip6ToBytes , ip6ToString , mb2bytes , onion2bytes , onion32bytes , port2bytes , stringToBytes } from './utils.ts'
77import { validatePort } from './validation.ts'
8+ import type { Registry as RegistryInterface } from './index.ts'
89
910export const V = - 1
1011
1112export interface ProtocolCodec {
13+ /**
14+ * A numeric code that will be used in the binary representation of the tuple.
15+ */
1216 code : number
17+
18+ /**
19+ * A string name that will be used in the string representation of the addr.
20+ */
1321 name : string
22+
23+ /**
24+ * Size defines the expected length of the address part of the tuple - valid
25+ * values are `-1` (or the `V` constant) for variable length (this will be
26+ * varint encoded in the binary representation), `0` for no address part or a
27+ * number that represents a fixed-length address.
28+ */
1429 size ?: number
30+
31+ /**
32+ * If this protocol is a path protocol.
33+ *
34+ * @deprecated This will be removed in a future release
35+ */
1536 path ?: boolean
37+
38+ /**
39+ * If this protocol can be resolved using configured resolvers.
40+ *
41+ * @deprecated This will be removed in a future release
42+ */
1643 resolvable ?: boolean
44+
45+ /**
46+ * If specified this protocol codec will also be used to decode tuples with
47+ * these names from string multiaddrs.
48+ */
1749 aliases ?: string [ ]
1850
1951 /**
@@ -43,11 +75,11 @@ export interface ProtocolCodec {
4375 validate ?( value : string ) : void
4476}
4577
46- class Registry {
78+ class Registry implements RegistryInterface {
4779 private protocolsByCode = new Map < number , ProtocolCodec > ( )
4880 private protocolsByName = new Map < string , ProtocolCodec > ( )
4981
50- getCodec ( key : string | number ) : ProtocolCodec {
82+ getProtocol ( key : string | number ) : ProtocolCodec {
5183 let codec : ProtocolCodec | undefined
5284
5385 if ( typeof key === 'string' ) {
@@ -57,23 +89,23 @@ class Registry {
5789 }
5890
5991 if ( codec == null ) {
60- throw new InvalidProtocolError ( `Protocol ${ key } was unknown` )
92+ throw new UnknownProtocolError ( `Protocol ${ key } was unknown` )
6193 }
6294
6395 return codec
6496 }
6597
66- addCodec ( key : number , codec : ProtocolCodec , aliases ?: string [ ] ) : void {
67- this . protocolsByCode . set ( key , codec )
98+ addProtocol ( codec : ProtocolCodec ) : void {
99+ this . protocolsByCode . set ( codec . code , codec )
68100 this . protocolsByName . set ( codec . name , codec )
69101
70- aliases ?. forEach ( alias => {
102+ codec . aliases ?. forEach ( alias => {
71103 this . protocolsByName . set ( alias , codec )
72104 } )
73105 }
74106
75- deleteCodec ( key : number ) : void {
76- const codec = this . getCodec ( key )
107+ removeProtocol ( code : number ) : void {
108+ const codec = this . protocolsByCode . get ( code )
77109
78110 if ( codec == null ) {
79111 return
@@ -288,5 +320,5 @@ const codecs: ProtocolCodec[] = [{
288320} ]
289321
290322codecs . forEach ( codec => {
291- registry . addCodec ( codec . code , codec , codec . aliases )
323+ registry . addProtocol ( codec )
292324} )
0 commit comments