Skip to content

Commit 3d53343

Browse files
committed
chore: add jsdoc comments
1 parent 8942075 commit 3d53343

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

index.js

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import createCID from 'multiformats/cid.js'
33
import * as bytes from 'multiformats/bytes.js'
44

55
const cache = new Map()
6+
7+
/**
8+
* @typedef {Object} Varint
9+
* @property {function(Uint8Array):[number, number]} decode
10+
* @property {function(number):Uint8Array} encode
11+
*/
12+
13+
/**
14+
* @type {Varint}
15+
*/
616
const varint = {
717
decode: data => {
818
const code = varints.decode(data)
@@ -16,15 +26,50 @@ const varint = {
1626
}
1727
}
1828

19-
const createMultihash = multiformats => {
20-
const { get, has, parse, add } = multiformats
29+
/**
30+
* @template Raw,Encoded
31+
* @typedef {(value:Raw) => Encoded} Encode
32+
*/
33+
34+
/**
35+
* @template Raw,Encoded
36+
* @typedef {Object} Codec
37+
* @property {string} name
38+
* @property {number} code
39+
* @property {Encode<Raw, Encoded>} encode
40+
* @property {Encode<Encoded, Raw>} decode
41+
*/
42+
43+
/**
44+
* @typedef {Codec<Uint8Array, Uint8Array>} MultihashCodec
45+
* @typedef {(bytes:Uint8Array) => {name:string, code:number, length:number, digest:Uint8Array}} Multihash$decode
46+
* @typedef {(byte:Uint8Array, base:string|name) => Uint8Array} Multihash$encode
47+
* @typedef {(bytes:Uint8Array, key:string) => Promise<Uint8Array>} Multihash$hash
48+
* @typedef {Object} Multihash
49+
* @property {Multihash$encode} encode
50+
* @property {Multihash$decode} decode
51+
* @property {Multihash$hash} hash
52+
* @property {function(number|string):boolean} has
53+
* @property {function(number|string):void|MultihashCodec} get
54+
* @property {function(MultihashCodec):void} add
55+
* @property {function(Uint8Array, Uint8Array):Promise<true>} validate
56+
*/
57+
58+
/**
59+
* @param {MultiformatsUtil & Multicodec} multiformats
60+
* @returns {Multihash}
61+
*/
62+
const createMultihash = ({ get, has, parse, add }) => {
63+
/** @type {Multihash$decode} */
2164
const decode = digest => {
2265
const [info, len] = parse(digest)
2366
digest = digest.slice(len)
2467
const [length, len2] = varint.decode(digest)
2568
digest = digest.slice(len2)
2669
return { code: info.code, name: info.name, length, digest }
2770
}
71+
72+
/** @type {Multihash$encode} */
2873
const encode = (digest, id) => {
2974
let info
3075
if (typeof id === 'number') {
@@ -36,6 +81,8 @@ const createMultihash = multiformats => {
3681
const length = varint.encode(digest.length)
3782
return Uint8Array.from([...code, ...length, ...digest])
3883
}
84+
85+
/** @type {Multihash$hash} */
3986
const hash = async (buff, key) => {
4087
buff = bytes.coerce(buff)
4188
const info = get(key)
@@ -44,6 +91,12 @@ const createMultihash = multiformats => {
4491
/* c8 ignore next */
4592
return encode(await info.encode(buff), key)
4693
}
94+
95+
/**
96+
* @param {Uint8Array} _hash
97+
* @param {Uint8Array} buff
98+
* @returns {Promise<true>}
99+
*/
47100
const validate = async (_hash, buff) => {
48101
_hash = bytes.coerce(_hash)
49102
const { length, digest, code } = decode(_hash)
@@ -57,9 +110,29 @@ const createMultihash = multiformats => {
57110
/* c8 ignore next */
58111
return true
59112
}
113+
60114
return { encode, has, decode, hash, validate, add, get }
61115
}
62116

117+
/**
118+
* @typedef {Encode<string, Uint8Array>} MultibaseDecode
119+
* @typedef {Encode<Uint8Array, string>} MultibaseEncode
120+
* @typedef {Object} MultibaseCodec
121+
* @property {string} prefix
122+
* @property {string} name
123+
* @property {MultibaseEncode} encode
124+
* @property {MultibaseDecode} decode
125+
* @typedef {Object} Multibase
126+
* @property {(codec:MultibaseCodec|MultibaseCodec[]) => void} add
127+
* @property {(prefex:string) => MultibaseCodec} get
128+
* @property {(prefex:string) => boolean} has
129+
* @property {(bytes:Uint8Array, prefix:string) => string} encode
130+
* @property {MultibaseDecode} decode
131+
* @property {(text:string) => MultibaseCodec} encoding
132+
*
133+
*
134+
* @returns {Multibase}
135+
*/
63136
const createMultibase = () => {
64137
const prefixMap = new Map()
65138
const nameMap = new Map()
@@ -75,6 +148,11 @@ const createMultibase = () => {
75148
_add(prefix, name, encode, decode)
76149
}
77150
}
151+
152+
/**
153+
* @param {string} id
154+
* @returns {MultibaseCodec}
155+
*/
78156
const get = id => {
79157
if (id.length === 1) {
80158
if (!prefixMap.has(id)) throw new Error(`Missing multibase implementation for "${id}"`)
@@ -105,11 +183,37 @@ const createMultibase = () => {
105183
const { decode } = get(prefix)
106184
return Uint8Array.from(decode(string))
107185
}
186+
/**
187+
* @param {string} string
188+
* @returns {MultibaseCodec}
189+
*/
108190
const encoding = string => get(string[0])
109191
return { add, has, get, encode, decode, encoding }
110192
}
111193

194+
/**
195+
* @typedef {Object} MultiformatsUtil
196+
* @property {Varint} varint
197+
* @property {function(Uint8Array):[MultihashCodec, number]} parse
198+
*
199+
* @typedef {Object} Multicodec
200+
* @property {function(MultihashCodec):void} add
201+
* @property {function(string|number|Uint8Array):MultihashCodec} get
202+
* @property {function(string):boolean} has
203+
*
204+
* @typedef {Object} MultiformatsExt
205+
* @property {Multicodec} multicodec
206+
* @property {Multibase} multibase
207+
* @property {Multihash} multihash
208+
*
209+
* @typedef {MultiformatsUtil & Multicodec & MultiformatsExt} Multiformats
210+
211+
* @param {Array<[number, string, Function, Function]>} [table]
212+
* @returns {Multiformats}
213+
*/
112214
const create = (table = []) => {
215+
/** @type {Map<number, [string, Encode<Uint8Array, Uint8Array>, Encode<Uint8Array, Uint8Array>]>}
216+
*/
113217
const intMap = new Map()
114218
const nameMap = new Map()
115219
const _add = (code, name, encode, decode) => {
@@ -131,6 +235,12 @@ const create = (table = []) => {
131235
for (const [code, name, encode, decode] of table) {
132236
_add(code, name, encode, decode)
133237
}
238+
239+
/**
240+
*
241+
* @param {Uint8Array} buff
242+
* @returns {[MultihashCodec, number]}
243+
*/
134244
const parse = buff => {
135245
buff = bytes.coerce(buff)
136246
const [code, len] = varint.decode(buff)
@@ -140,6 +250,7 @@ const create = (table = []) => {
140250
}
141251
return [{ code, name, encode, decode }, len]
142252
}
253+
143254
const get = obj => {
144255
if (typeof obj === 'string') {
145256
if (nameMap.has(obj)) {
@@ -190,10 +301,12 @@ const create = (table = []) => {
190301
}
191302

192303
const multiformats = { parse, add, get, has, encode, decode, varint, bytes }
304+
/** @type {Multicodec} */
193305
multiformats.multicodec = { add, get, has, encode, decode }
194306
multiformats.multibase = createMultibase()
195307
multiformats.multihash = createMultihash(multiformats)
196308
multiformats.CID = createCID(multiformats)
309+
197310
return multiformats
198311
}
199312
export { create, bytes, varint }

0 commit comments

Comments
 (0)