@@ -21,6 +21,9 @@ const subdomainProtocolMatch = 2
2121// Fully qualified domain name (FQDN) that has an explicit .tld suffix
2222const fqdnWithTld = / ^ ( ( [ a - z 0 - 9 ] | [ a - z 0 - 9 ] [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) \. ) + ( [ a - z 0 - 9 ] | [ a - z 0 - 9 ] [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) $ /
2323
24+ /**
25+ * @param {* } hash
26+ */
2427function isMultihash ( hash ) {
2528 const formatted = convertToString ( hash )
2629 try {
@@ -31,6 +34,9 @@ function isMultihash (hash) {
3134 }
3235}
3336
37+ /**
38+ * @param {* } hash
39+ */
3440function isMultibase ( hash ) {
3541 try {
3642 return multibase . isEncoded ( hash )
@@ -39,6 +45,9 @@ function isMultibase (hash) {
3945 }
4046}
4147
48+ /**
49+ * @param {* } hash
50+ */
4251function isCID ( hash ) {
4352 try {
4453 new CID ( hash ) // eslint-disable-line no-new
@@ -48,6 +57,9 @@ function isCID (hash) {
4857 }
4958}
5059
60+ /**
61+ * @param {* } input
62+ */
5163function isMultiaddr ( input ) {
5264 if ( ! input ) return false
5365 if ( Multiaddr . isMultiaddr ( input ) ) return true
@@ -59,10 +71,19 @@ function isMultiaddr (input) {
5971 }
6072}
6173
74+ /**
75+ * @param {string | Uint8Array | Multiaddr } input
76+ */
6277function isPeerMultiaddr ( input ) {
6378 return isMultiaddr ( input ) && ( mafmt . P2P . matches ( input ) || mafmt . DNS . matches ( input ) )
6479}
6580
81+ /**
82+ * @param {string | Uint8Array } input
83+ * @param {RegExp | string } pattern
84+ * @param {number } [protocolMatch=1]
85+ * @param {number } [hashMatch=2]
86+ */
6687function isIpfs ( input , pattern , protocolMatch = defaultProtocolMatch , hashMatch = defaultHashMath ) {
6788 const formatted = convertToString ( input )
6889 if ( ! formatted ) {
@@ -83,14 +104,21 @@ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
83104 if ( hash && pattern === subdomainGatewayPattern ) {
84105 // when doing checks for subdomain context
85106 // ensure hash is case-insensitive
86- // (browsers force-lowercase authority compotent anyway)
107+ // (browsers force-lowercase authority component anyway)
87108 hash = hash . toLowerCase ( )
88109 }
89110
90111 return isCID ( hash )
91112}
92113
93- function isIpns ( input , pattern , protocolMatch = defaultProtocolMatch , hashMatch ) {
114+ /**
115+ *
116+ * @param {string | Uint8Array } input
117+ * @param {string | RegExp } pattern
118+ * @param {number } [protocolMatch=1]
119+ * @param {number } [hashMatch=1]
120+ */
121+ function isIpns ( input , pattern , protocolMatch = defaultProtocolMatch , hashMatch = defaultHashMath ) {
94122 const formatted = convertToString ( input )
95123 if ( ! formatted ) {
96124 return false
@@ -133,10 +161,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
133161 return true
134162}
135163
164+ /**
165+ * @param {any } input
166+ */
136167function isString ( input ) {
137168 return typeof input === 'string'
138169}
139170
171+ /**
172+ * @param {Uint8Array | string } input
173+ */
140174function convertToString ( input ) {
141175 if ( input instanceof Uint8Array ) {
142176 return uint8ArrayToString ( input , 'base58btc' )
@@ -149,21 +183,45 @@ function convertToString (input) {
149183 return false
150184}
151185
186+ /**
187+ * @param {string | Uint8Array } url
188+ */
152189const ipfsSubdomain = ( url ) => isIpfs ( url , subdomainGatewayPattern , subdomainProtocolMatch , subdomainIdMatch )
190+ /**
191+ * @param {string | Uint8Array } url
192+ */
153193const ipnsSubdomain = ( url ) => isIpns ( url , subdomainGatewayPattern , subdomainProtocolMatch , subdomainIdMatch )
194+ /**
195+ * @param {string | Uint8Array } url
196+ */
154197const subdomain = ( url ) => ipfsSubdomain ( url ) || ipnsSubdomain ( url )
155198
199+ /**
200+ * @param {string | Uint8Array } url
201+ */
156202const ipfsUrl = ( url ) => isIpfs ( url , pathGatewayPattern ) || ipfsSubdomain ( url )
203+ /**
204+ * @param {string | Uint8Array } url
205+ */
157206const ipnsUrl = ( url ) => isIpns ( url , pathGatewayPattern ) || ipnsSubdomain ( url )
207+ /**
208+ * @param {string | Uint8Array } url
209+ */
158210const url = ( url ) => ipfsUrl ( url ) || ipnsUrl ( url ) || subdomain ( url )
159211
212+ /**
213+ * @param {string | Uint8Array } path
214+ */
160215const path = ( path ) => isIpfs ( path , pathPattern ) || isIpns ( path , pathPattern )
161216
162217module . exports = {
163218 multihash : isMultihash ,
164219 multiaddr : isMultiaddr ,
165220 peerMultiaddr : isPeerMultiaddr ,
166221 cid : isCID ,
222+ /**
223+ * @param {CID | string | Uint8Array } cid
224+ */
167225 base32cid : ( cid ) => ( isMultibase ( cid ) === 'base32' && isCID ( cid ) ) ,
168226 ipfsSubdomain,
169227 ipnsSubdomain,
@@ -173,10 +231,22 @@ module.exports = {
173231 ipnsUrl,
174232 url,
175233 pathGatewayPattern : pathGatewayPattern ,
234+ /**
235+ * @param {string | Uint8Array } path
236+ */
176237 ipfsPath : ( path ) => isIpfs ( path , pathPattern ) ,
238+ /**
239+ * @param {string | Uint8Array } path
240+ */
177241 ipnsPath : ( path ) => isIpns ( path , pathPattern ) ,
178242 path,
179243 pathPattern,
244+ /**
245+ * @param {string | Uint8Array } x
246+ */
180247 urlOrPath : ( x ) => url ( x ) || path ( x ) ,
248+ /**
249+ * @param {string | Uint8Array | CID } path
250+ */
181251 cidPath : path => isString ( path ) && ! isCID ( path ) && isIpfs ( `/ipfs/${ path } ` , pathPattern )
182252}
0 commit comments