1
- // @ts -check
2
- /* eslint-disable guard-for-in */
3
1
/**
4
2
* Multihash implementation in JavaScript.
5
3
*
@@ -14,21 +12,20 @@ const uint8ArrayToString = require('uint8arrays/to-string')
14
12
const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
15
13
const uint8ArrayConcat = require ( 'uint8arrays/concat' )
16
14
17
- const codes = { }
15
+ const codes = /** @type { import('./types').CodeNameMap } */ ( { } )
18
16
17
+ // eslint-disable-next-line guard-for-in
19
18
for ( const key in names ) {
20
19
codes [ names [ key ] ] = key
21
20
}
22
- exports . names = names
23
- exports . codes = Object . freeze ( codes )
24
21
25
22
/**
26
23
* Convert the given multihash to a hex encoded string.
27
24
*
28
25
* @param {Uint8Array } hash
29
26
* @returns {string }
30
27
*/
31
- exports . toHexString = function toHexString ( hash ) {
28
+ function toHexString ( hash ) {
32
29
if ( ! ( hash instanceof Uint8Array ) ) {
33
30
throw new Error ( 'must be passed a Uint8Array' )
34
31
}
@@ -42,7 +39,7 @@ exports.toHexString = function toHexString (hash) {
42
39
* @param {string } hash
43
40
* @returns {Uint8Array }
44
41
*/
45
- exports . fromHexString = function fromHexString ( hash ) {
42
+ function fromHexString ( hash ) {
46
43
return uint8ArrayFromString ( hash , 'base16' )
47
44
}
48
45
@@ -52,7 +49,7 @@ exports.fromHexString = function fromHexString (hash) {
52
49
* @param {Uint8Array } hash
53
50
* @returns {string }
54
51
*/
55
- exports . toB58String = function toB58String ( hash ) {
52
+ function toB58String ( hash ) {
56
53
if ( ! ( hash instanceof Uint8Array ) ) {
57
54
throw new Error ( 'must be passed a Uint8Array' )
58
55
}
@@ -66,7 +63,7 @@ exports.toB58String = function toB58String (hash) {
66
63
* @param {string|Uint8Array } hash
67
64
* @returns {Uint8Array }
68
65
*/
69
- exports . fromB58String = function fromB58String ( hash ) {
66
+ function fromB58String ( hash ) {
70
67
const encoded = hash instanceof Uint8Array
71
68
? uint8ArrayToString ( hash )
72
69
: hash
@@ -78,9 +75,9 @@ exports.fromB58String = function fromB58String (hash) {
78
75
* Decode a hash from the given multihash.
79
76
*
80
77
* @param {Uint8Array } bytes
81
- * @returns {{code: number , name: string , length: number, digest: Uint8Array} } result
78
+ * @returns {{code: HashCode , name: HashName , length: number, digest: Uint8Array} } result
82
79
*/
83
- exports . decode = function decode ( bytes ) {
80
+ function decode ( bytes ) {
84
81
if ( ! ( bytes instanceof Uint8Array ) ) {
85
82
throw new Error ( 'multihash must be a Uint8Array' )
86
83
}
@@ -90,7 +87,7 @@ exports.decode = function decode (bytes) {
90
87
}
91
88
92
89
const code = varint . decode ( bytes )
93
- if ( ! exports . isValidCode ( code ) ) {
90
+ if ( ! isValidCode ( code ) ) {
94
91
throw new Error ( `multihash unknown function code: 0x${ code . toString ( 16 ) } ` )
95
92
}
96
93
bytes = bytes . slice ( varint . decode . bytes )
@@ -114,22 +111,22 @@ exports.decode = function decode (bytes) {
114
111
}
115
112
116
113
/**
117
- * Encode a hash digest along with the specified function code.
114
+ * Encode a hash digest along with the specified function code.
118
115
*
119
116
* > **Note:** the length is derived from the length of the digest itself.
120
117
*
121
118
* @param {Uint8Array } digest
122
- * @param {string|number } code
119
+ * @param {HashName | HashCode } code
123
120
* @param {number } [length]
124
121
* @returns {Uint8Array }
125
122
*/
126
- exports . encode = function encode ( digest , code , length ) {
123
+ function encode ( digest , code , length ) {
127
124
if ( ! digest || code === undefined ) {
128
125
throw new Error ( 'multihash encode requires at least two args: digest, code' )
129
126
}
130
127
131
128
// ensure it's a hashfunction code.
132
- const hashfn = exports . coerceCode ( code )
129
+ const hashfn = coerceCode ( code )
133
130
134
131
if ( ! ( digest instanceof Uint8Array ) ) {
135
132
throw new Error ( 'digest should be a Uint8Array' )
@@ -151,10 +148,11 @@ exports.encode = function encode (digest, code, length) {
151
148
/**
152
149
* Converts a hash function name into the matching code.
153
150
* If passed a number it will return the number if it's a valid code.
154
- * @param {string|number } name
151
+ *
152
+ * @param {HashName | number } name
155
153
* @returns {number }
156
154
*/
157
- exports . coerceCode = function coerceCode ( name ) {
155
+ function coerceCode ( name ) {
158
156
let code = name
159
157
160
158
if ( typeof name === 'string' ) {
@@ -168,31 +166,31 @@ exports.coerceCode = function coerceCode (name) {
168
166
throw new Error ( `Hash function code should be a number. Got: ${ code } ` )
169
167
}
170
168
171
- if ( codes [ code ] === undefined && ! exports . isAppCode ( code ) ) {
169
+ if ( codes [ code ] === undefined && ! isAppCode ( code ) ) {
172
170
throw new Error ( `Unrecognized function code: ${ code } ` )
173
171
}
174
172
175
173
return code
176
174
}
177
175
178
176
/**
179
- * Checks wether a code is part of the app range
177
+ * Checks if a code is part of the app range
180
178
*
181
179
* @param {number } code
182
180
* @returns {boolean }
183
181
*/
184
- exports . isAppCode = function appCode ( code ) {
182
+ function isAppCode ( code ) {
185
183
return code > 0 && code < 0x10
186
184
}
187
185
188
186
/**
189
187
* Checks whether a multihash code is valid.
190
188
*
191
- * @param {number } code
189
+ * @param {HashCode } code
192
190
* @returns {boolean }
193
191
*/
194
- exports . isValidCode = function validCode ( code ) {
195
- if ( exports . isAppCode ( code ) ) {
192
+ function isValidCode ( code ) {
193
+ if ( isAppCode ( code ) ) {
196
194
return true
197
195
}
198
196
@@ -211,9 +209,8 @@ exports.isValidCode = function validCode (code) {
211
209
* @throws {Error }
212
210
*/
213
211
function validate ( multihash ) {
214
- exports . decode ( multihash ) // throws if bad.
212
+ decode ( multihash ) // throws if bad.
215
213
}
216
- exports . validate = validate
217
214
218
215
/**
219
216
* Returns a prefix from a valid multihash. Throws an error if it is not valid.
@@ -222,8 +219,29 @@ exports.validate = validate
222
219
* @returns {Uint8Array }
223
220
* @throws {Error }
224
221
*/
225
- exports . prefix = function prefix ( multihash ) {
222
+ function prefix ( multihash ) {
226
223
validate ( multihash )
227
224
228
225
return multihash . subarray ( 0 , 2 )
229
226
}
227
+
228
+ module . exports = {
229
+ names,
230
+ codes : Object . freeze ( codes ) ,
231
+ toHexString,
232
+ fromHexString,
233
+ toB58String,
234
+ fromB58String,
235
+ decode,
236
+ encode,
237
+ coerceCode,
238
+ isAppCode,
239
+ validate,
240
+ prefix,
241
+ isValidCode
242
+ }
243
+
244
+ /**
245
+ * @typedef { import("./constants").HashCode } HashCode
246
+ * @typedef { import("./constants").HashName } HashName
247
+ */
0 commit comments