@@ -13,7 +13,7 @@ const readonly = ({ enumerable = true, configurable = false } = {}) => ({
1313 * @template T
1414 * @param {T } source
1515 * @param {Array<string|number> } base
16- * @returns {Iterable<[string, CID ]> }
16+ * @returns {Iterable<[string, API.CIDView ]> }
1717 */
1818const links = function * ( source , base ) {
1919 if ( source == null ) return
@@ -74,6 +74,7 @@ const tree = function * (source, base) {
7474 * @template T
7575 * @param {T } source
7676 * @param {string[] } path
77+ * @return {API.BlockCursorView }
7778 */
7879const get = ( source , path ) => {
7980 /** @type {Record<string, any> } */
@@ -92,19 +93,23 @@ const get = (source, path) => {
9293}
9394
9495/**
95- * @template T
96+ * @template {unknown} T - Logical type of the data encoded in the block
97+ * @template {number} C - multicodec code corresponding to codec used to encode the block
98+ * @template {number} A - multicodec code corresponding to the hashing algorithm used in CID creation.
99+ * @template {API.CIDVersion} V - CID version
100+ * @implements {API.BlockView<T, C, A, V>}
96101 */
97102class Block {
98103 /**
99104 * @param {Object } options
100- * @param {API.CID } options.cid
105+ * @param {API.CIDView<C, A, V> } options.cid
101106 * @param {API.ByteView<T> } options.bytes
102107 * @param {T } options.value
103108 */
104109 constructor ( { cid, bytes, value } ) {
105- if ( ! cid || ! bytes || typeof value === 'undefined' ) throw new Error ( 'Missing required argument' )
110+ if ( ! cid || ! bytes || typeof value === 'undefined' ) { throw new Error ( 'Missing required argument' ) }
106111
107- this . cid = /** @type { CID } */ ( cid )
112+ this . cid = cid
108113 this . bytes = bytes
109114 this . value = value
110115 this . asBlock = this
@@ -127,43 +132,47 @@ class Block {
127132 }
128133
129134 /**
130- * @param {string } [path]
131- */
135+ * @param {string } [path]
136+ */
132137 get ( path = '/' ) {
133138 return get ( this . value , path . split ( '/' ) . filter ( Boolean ) )
134139 }
135140}
136141
137142/**
138- * @template T
139- * @template {number} Code
140- * @template {number} Alg
143+ * @template {unknown} T - Logical type of the data encoded in the block
144+ * @template {number} Code - multicodec code corresponding to codec used to encode the block
145+ * @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
141146 * @param {Object } options
142147 * @param {T } options.value
143148 * @param {API.BlockEncoder<Code, T> } options.codec
144149 * @param {API.MultihashHasher<Alg> } options.hasher
145- * @returns {Promise<Block<T >> }
150+ * @returns {Promise<API.BlockView<T, Code, Alg >> }
146151 */
147152const encode = async ( { value, codec, hasher } ) => {
148153 if ( typeof value === 'undefined' ) throw new Error ( 'Missing required argument "value"' )
149154 if ( ! codec || ! hasher ) throw new Error ( 'Missing required argument: codec or hasher' )
150155
151156 const bytes = codec . encode ( value )
152157 const hash = await hasher . digest ( bytes )
153- const cid = CID . create ( 1 , codec . code , hash )
158+ const cid = CID . create (
159+ 1 ,
160+ codec . code ,
161+ hash
162+ )
154163
155164 return new Block ( { value, bytes, cid } )
156165}
157166
158167/**
159- * @template T
160- * @template {number} Code
161- * @template {number} Alg
168+ * @template {unknown} T - Logical type of the data encoded in the block
169+ * @template {number} Code - multicodec code corresponding to codec used to encode the block
170+ * @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
162171 * @param {Object } options
163172 * @param {API.ByteView<T> } options.bytes
164173 * @param {API.BlockDecoder<Code, T> } options.codec
165174 * @param {API.MultihashHasher<Alg> } options.hasher
166- * @returns {Promise<Block<T >> }
175+ * @returns {Promise<API.BlockView<T, Code, Alg >> }
167176 */
168177const decode = async ( { bytes, codec, hasher } ) => {
169178 if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
@@ -182,10 +191,12 @@ const decode = async ({ bytes, codec, hasher }) => {
182191 */
183192
184193/**
185- * @template T
186- * @template {number} Code
187- * @param {{ cid: API.CID, value:T, codec?: API.BlockDecoder<Code, T>, bytes: API.ByteView<T> }|{cid:API.CID, bytes:API.ByteView<T>, value?:void, codec:API.BlockDecoder<Code, T>} } options
188- * @returns {Block<T> }
194+ * @template {unknown} T - Logical type of the data encoded in the block
195+ * @template {number} Code - multicodec code corresponding to codec used to encode the block
196+ * @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
197+ * @template {API.CIDVersion} V - CID version
198+ * @param {{ cid: API.Link<T, Code, Alg, V>, value:T, codec?: API.BlockDecoder<Code, T>, bytes: API.ByteView<T> }|{cid:API.Link<T, Code, Alg, V>, bytes:API.ByteView<T>, value?:void, codec:API.BlockDecoder<Code, T>} } options
199+ * @returns {API.BlockView<T, Code, Alg, V> }
189200 */
190201const createUnsafe = ( { bytes, cid, value : maybeValue , codec } ) => {
191202 const value = maybeValue !== undefined
@@ -194,19 +205,24 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
194205
195206 if ( value === undefined ) throw new Error ( 'Missing required argument, must either provide "value" or "codec"' )
196207
197- return new Block ( { cid, bytes, value } )
208+ return new Block ( {
209+ cid : /** @type {API.CIDView<Code, Alg, V> } */ ( cid ) ,
210+ bytes,
211+ value
212+ } )
198213}
199214
200215/**
201- * @template T
202- * @template {number} Code
203- * @template {number} Alg
216+ * @template {unknown} T - Logical type of the data encoded in the block
217+ * @template {number} Code - multicodec code corresponding to codec used to encode the block
218+ * @template {number} Alg - multicodec code corresponding to the hashing algorithm used in CID creation.
219+ * @template {API.CIDVersion} V - CID version
204220 * @param {Object } options
205- * @param {API.CID<Code, Alg> } options.cid
221+ * @param {API.CID<Code, Alg, V > } options.cid
206222 * @param {API.ByteView<T> } options.bytes
207223 * @param {API.BlockDecoder<Code, T> } options.codec
208224 * @param {API.MultihashHasher<Alg> } options.hasher
209- * @returns {Promise<Block<T >> }
225+ * @returns {Promise<API.BlockView<T, Code, Alg, V >> }
210226 */
211227const create = async ( { bytes, cid, hasher, codec } ) => {
212228 if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
@@ -217,7 +233,12 @@ const create = async ({ bytes, cid, hasher, codec }) => {
217233 throw new Error ( 'CID hash does not match bytes' )
218234 }
219235
220- return createUnsafe ( { bytes, cid, value, codec } )
236+ return createUnsafe ( {
237+ bytes,
238+ cid,
239+ value,
240+ codec
241+ } )
221242}
222243
223244export { encode , decode , create , createUnsafe , Block }
0 commit comments