1- // @ts -check
2- /* eslint-disable guard-for-in */
31/**
42 * Multihash implementation in JavaScript.
53 *
@@ -14,21 +12,20 @@ const uint8ArrayToString = require('uint8arrays/to-string')
1412const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
1513const uint8ArrayConcat = require ( 'uint8arrays/concat' )
1614
17- const codes = { }
15+ const codes = /** @type { import('./types').CodeNameMap } */ ( { } )
1816
17+ // eslint-disable-next-line guard-for-in
1918for ( const key in names ) {
2019 codes [ names [ key ] ] = key
2120}
22- exports . names = names
23- exports . codes = Object . freeze ( codes )
2421
2522/**
2623 * Convert the given multihash to a hex encoded string.
2724 *
2825 * @param {Uint8Array } hash
2926 * @returns {string }
3027 */
31- exports . toHexString = function toHexString ( hash ) {
28+ function toHexString ( hash ) {
3229 if ( ! ( hash instanceof Uint8Array ) ) {
3330 throw new Error ( 'must be passed a Uint8Array' )
3431 }
@@ -42,7 +39,7 @@ exports.toHexString = function toHexString (hash) {
4239 * @param {string } hash
4340 * @returns {Uint8Array }
4441 */
45- exports . fromHexString = function fromHexString ( hash ) {
42+ function fromHexString ( hash ) {
4643 return uint8ArrayFromString ( hash , 'base16' )
4744}
4845
@@ -52,7 +49,7 @@ exports.fromHexString = function fromHexString (hash) {
5249 * @param {Uint8Array } hash
5350 * @returns {string }
5451 */
55- exports . toB58String = function toB58String ( hash ) {
52+ function toB58String ( hash ) {
5653 if ( ! ( hash instanceof Uint8Array ) ) {
5754 throw new Error ( 'must be passed a Uint8Array' )
5855 }
@@ -66,7 +63,7 @@ exports.toB58String = function toB58String (hash) {
6663 * @param {string|Uint8Array } hash
6764 * @returns {Uint8Array }
6865 */
69- exports . fromB58String = function fromB58String ( hash ) {
66+ function fromB58String ( hash ) {
7067 const encoded = hash instanceof Uint8Array
7168 ? uint8ArrayToString ( hash )
7269 : hash
@@ -78,9 +75,9 @@ exports.fromB58String = function fromB58String (hash) {
7875 * Decode a hash from the given multihash.
7976 *
8077 * @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
8279 */
83- exports . decode = function decode ( bytes ) {
80+ function decode ( bytes ) {
8481 if ( ! ( bytes instanceof Uint8Array ) ) {
8582 throw new Error ( 'multihash must be a Uint8Array' )
8683 }
@@ -90,7 +87,7 @@ exports.decode = function decode (bytes) {
9087 }
9188
9289 const code = varint . decode ( bytes )
93- if ( ! exports . isValidCode ( code ) ) {
90+ if ( ! isValidCode ( code ) ) {
9491 throw new Error ( `multihash unknown function code: 0x${ code . toString ( 16 ) } ` )
9592 }
9693 bytes = bytes . slice ( varint . decode . bytes )
@@ -114,22 +111,22 @@ exports.decode = function decode (bytes) {
114111}
115112
116113/**
117- * Encode a hash digest along with the specified function code.
114+ * Encode a hash digest along with the specified function code.
118115 *
119116 * > **Note:** the length is derived from the length of the digest itself.
120117 *
121118 * @param {Uint8Array } digest
122- * @param {string|number } code
119+ * @param {HashName | HashCode } code
123120 * @param {number } [length]
124121 * @returns {Uint8Array }
125122 */
126- exports . encode = function encode ( digest , code , length ) {
123+ function encode ( digest , code , length ) {
127124 if ( ! digest || code === undefined ) {
128125 throw new Error ( 'multihash encode requires at least two args: digest, code' )
129126 }
130127
131128 // ensure it's a hashfunction code.
132- const hashfn = exports . coerceCode ( code )
129+ const hashfn = coerceCode ( code )
133130
134131 if ( ! ( digest instanceof Uint8Array ) ) {
135132 throw new Error ( 'digest should be a Uint8Array' )
@@ -151,10 +148,11 @@ exports.encode = function encode (digest, code, length) {
151148/**
152149 * Converts a hash function name into the matching code.
153150 * 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
155153 * @returns {number }
156154 */
157- exports . coerceCode = function coerceCode ( name ) {
155+ function coerceCode ( name ) {
158156 let code = name
159157
160158 if ( typeof name === 'string' ) {
@@ -168,31 +166,31 @@ exports.coerceCode = function coerceCode (name) {
168166 throw new Error ( `Hash function code should be a number. Got: ${ code } ` )
169167 }
170168
171- if ( codes [ code ] === undefined && ! exports . isAppCode ( code ) ) {
169+ if ( codes [ code ] === undefined && ! isAppCode ( code ) ) {
172170 throw new Error ( `Unrecognized function code: ${ code } ` )
173171 }
174172
175173 return code
176174}
177175
178176/**
179- * Checks wether a code is part of the app range
177+ * Checks if a code is part of the app range
180178 *
181179 * @param {number } code
182180 * @returns {boolean }
183181 */
184- exports . isAppCode = function appCode ( code ) {
182+ function isAppCode ( code ) {
185183 return code > 0 && code < 0x10
186184}
187185
188186/**
189187 * Checks whether a multihash code is valid.
190188 *
191- * @param {number } code
189+ * @param {HashCode } code
192190 * @returns {boolean }
193191 */
194- exports . isValidCode = function validCode ( code ) {
195- if ( exports . isAppCode ( code ) ) {
192+ function isValidCode ( code ) {
193+ if ( isAppCode ( code ) ) {
196194 return true
197195 }
198196
@@ -211,9 +209,8 @@ exports.isValidCode = function validCode (code) {
211209 * @throws {Error }
212210 */
213211function validate ( multihash ) {
214- exports . decode ( multihash ) // throws if bad.
212+ decode ( multihash ) // throws if bad.
215213}
216- exports . validate = validate
217214
218215/**
219216 * Returns a prefix from a valid multihash. Throws an error if it is not valid.
@@ -222,8 +219,29 @@ exports.validate = validate
222219 * @returns {Uint8Array }
223220 * @throws {Error }
224221 */
225- exports . prefix = function prefix ( multihash ) {
222+ function prefix ( multihash ) {
226223 validate ( multihash )
227224
228225 return multihash . subarray ( 0 , 2 )
229226}
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