@@ -21,6 +21,9 @@ const subdomainProtocolMatch = 2
21
21
// Fully qualified domain name (FQDN) that has an explicit .tld suffix
22
22
const 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 ] ) $ /
23
23
24
+ /**
25
+ * @param {* } hash
26
+ */
24
27
function isMultihash ( hash ) {
25
28
const formatted = convertToString ( hash )
26
29
try {
@@ -31,6 +34,9 @@ function isMultihash (hash) {
31
34
}
32
35
}
33
36
37
+ /**
38
+ * @param {* } hash
39
+ */
34
40
function isMultibase ( hash ) {
35
41
try {
36
42
return multibase . isEncoded ( hash )
@@ -39,6 +45,9 @@ function isMultibase (hash) {
39
45
}
40
46
}
41
47
48
+ /**
49
+ * @param {* } hash
50
+ */
42
51
function isCID ( hash ) {
43
52
try {
44
53
new CID ( hash ) // eslint-disable-line no-new
@@ -48,6 +57,9 @@ function isCID (hash) {
48
57
}
49
58
}
50
59
60
+ /**
61
+ * @param {* } input
62
+ */
51
63
function isMultiaddr ( input ) {
52
64
if ( ! input ) return false
53
65
if ( Multiaddr . isMultiaddr ( input ) ) return true
@@ -59,10 +71,19 @@ function isMultiaddr (input) {
59
71
}
60
72
}
61
73
74
+ /**
75
+ * @param {string | Uint8Array | Multiaddr } input
76
+ */
62
77
function isPeerMultiaddr ( input ) {
63
78
return isMultiaddr ( input ) && ( mafmt . P2P . matches ( input ) || mafmt . DNS . matches ( input ) )
64
79
}
65
80
81
+ /**
82
+ * @param {string | Uint8Array } input
83
+ * @param {RegExp | string } pattern
84
+ * @param {number } [protocolMatch=1]
85
+ * @param {number } [hashMatch=2]
86
+ */
66
87
function isIpfs ( input , pattern , protocolMatch = defaultProtocolMatch , hashMatch = defaultHashMath ) {
67
88
const formatted = convertToString ( input )
68
89
if ( ! formatted ) {
@@ -83,14 +104,21 @@ function isIpfs (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
83
104
if ( hash && pattern === subdomainGatewayPattern ) {
84
105
// when doing checks for subdomain context
85
106
// ensure hash is case-insensitive
86
- // (browsers force-lowercase authority compotent anyway)
107
+ // (browsers force-lowercase authority component anyway)
87
108
hash = hash . toLowerCase ( )
88
109
}
89
110
90
111
return isCID ( hash )
91
112
}
92
113
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 ) {
94
122
const formatted = convertToString ( input )
95
123
if ( ! formatted ) {
96
124
return false
@@ -133,10 +161,16 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
133
161
return true
134
162
}
135
163
164
+ /**
165
+ * @param {any } input
166
+ */
136
167
function isString ( input ) {
137
168
return typeof input === 'string'
138
169
}
139
170
171
+ /**
172
+ * @param {Uint8Array | string } input
173
+ */
140
174
function convertToString ( input ) {
141
175
if ( input instanceof Uint8Array ) {
142
176
return uint8ArrayToString ( input , 'base58btc' )
@@ -149,21 +183,45 @@ function convertToString (input) {
149
183
return false
150
184
}
151
185
186
+ /**
187
+ * @param {string | Uint8Array } url
188
+ */
152
189
const ipfsSubdomain = ( url ) => isIpfs ( url , subdomainGatewayPattern , subdomainProtocolMatch , subdomainIdMatch )
190
+ /**
191
+ * @param {string | Uint8Array } url
192
+ */
153
193
const ipnsSubdomain = ( url ) => isIpns ( url , subdomainGatewayPattern , subdomainProtocolMatch , subdomainIdMatch )
194
+ /**
195
+ * @param {string | Uint8Array } url
196
+ */
154
197
const subdomain = ( url ) => ipfsSubdomain ( url ) || ipnsSubdomain ( url )
155
198
199
+ /**
200
+ * @param {string | Uint8Array } url
201
+ */
156
202
const ipfsUrl = ( url ) => isIpfs ( url , pathGatewayPattern ) || ipfsSubdomain ( url )
203
+ /**
204
+ * @param {string | Uint8Array } url
205
+ */
157
206
const ipnsUrl = ( url ) => isIpns ( url , pathGatewayPattern ) || ipnsSubdomain ( url )
207
+ /**
208
+ * @param {string | Uint8Array } url
209
+ */
158
210
const url = ( url ) => ipfsUrl ( url ) || ipnsUrl ( url ) || subdomain ( url )
159
211
212
+ /**
213
+ * @param {string | Uint8Array } path
214
+ */
160
215
const path = ( path ) => isIpfs ( path , pathPattern ) || isIpns ( path , pathPattern )
161
216
162
217
module . exports = {
163
218
multihash : isMultihash ,
164
219
multiaddr : isMultiaddr ,
165
220
peerMultiaddr : isPeerMultiaddr ,
166
221
cid : isCID ,
222
+ /**
223
+ * @param {CID | string | Uint8Array } cid
224
+ */
167
225
base32cid : ( cid ) => ( isMultibase ( cid ) === 'base32' && isCID ( cid ) ) ,
168
226
ipfsSubdomain,
169
227
ipnsSubdomain,
@@ -173,10 +231,22 @@ module.exports = {
173
231
ipnsUrl,
174
232
url,
175
233
pathGatewayPattern : pathGatewayPattern ,
234
+ /**
235
+ * @param {string | Uint8Array } path
236
+ */
176
237
ipfsPath : ( path ) => isIpfs ( path , pathPattern ) ,
238
+ /**
239
+ * @param {string | Uint8Array } path
240
+ */
177
241
ipnsPath : ( path ) => isIpns ( path , pathPattern ) ,
178
242
path,
179
243
pathPattern,
244
+ /**
245
+ * @param {string | Uint8Array } x
246
+ */
180
247
urlOrPath : ( x ) => url ( x ) || path ( x ) ,
248
+ /**
249
+ * @param {string | Uint8Array | CID } path
250
+ */
181
251
cidPath : path => isString ( path ) && ! isCID ( path ) && isIpfs ( `/ipfs/${ path } ` , pathPattern )
182
252
}
0 commit comments