@@ -13,7 +13,7 @@ const readonly = ({ enumerable = true, configurable = false } = {}) => ({
13
13
* @template T
14
14
* @param {T } source
15
15
* @param {Array<string|number> } base
16
- * @returns {Iterable<[string, CID ]> }
16
+ * @returns {Iterable<[string, API.CIDView ]> }
17
17
*/
18
18
const links = function * ( source , base ) {
19
19
if ( source == null ) return
@@ -74,6 +74,7 @@ const tree = function * (source, base) {
74
74
* @template T
75
75
* @param {T } source
76
76
* @param {string[] } path
77
+ * @return {API.BlockCursorView }
77
78
*/
78
79
const get = ( source , path ) => {
79
80
/** @type {Record<string, any> } */
@@ -92,19 +93,23 @@ const get = (source, path) => {
92
93
}
93
94
94
95
/**
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>}
96
101
*/
97
102
class Block {
98
103
/**
99
104
* @param {Object } options
100
- * @param {API.CID } options.cid
105
+ * @param {API.CIDView<C, A, V> } options.cid
101
106
* @param {API.ByteView<T> } options.bytes
102
107
* @param {T } options.value
103
108
*/
104
109
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' ) }
106
111
107
- this . cid = /** @type { CID } */ ( cid )
112
+ this . cid = cid
108
113
this . bytes = bytes
109
114
this . value = value
110
115
this . asBlock = this
@@ -127,43 +132,47 @@ class Block {
127
132
}
128
133
129
134
/**
130
- * @param {string } [path]
131
- */
135
+ * @param {string } [path]
136
+ */
132
137
get ( path = '/' ) {
133
138
return get ( this . value , path . split ( '/' ) . filter ( Boolean ) )
134
139
}
135
140
}
136
141
137
142
/**
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.
141
146
* @param {Object } options
142
147
* @param {T } options.value
143
148
* @param {API.BlockEncoder<Code, T> } options.codec
144
149
* @param {API.MultihashHasher<Alg> } options.hasher
145
- * @returns {Promise<Block<T >> }
150
+ * @returns {Promise<API.BlockView<T, Code, Alg >> }
146
151
*/
147
152
const encode = async ( { value, codec, hasher } ) => {
148
153
if ( typeof value === 'undefined' ) throw new Error ( 'Missing required argument "value"' )
149
154
if ( ! codec || ! hasher ) throw new Error ( 'Missing required argument: codec or hasher' )
150
155
151
156
const bytes = codec . encode ( value )
152
157
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
+ )
154
163
155
164
return new Block ( { value, bytes, cid } )
156
165
}
157
166
158
167
/**
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.
162
171
* @param {Object } options
163
172
* @param {API.ByteView<T> } options.bytes
164
173
* @param {API.BlockDecoder<Code, T> } options.codec
165
174
* @param {API.MultihashHasher<Alg> } options.hasher
166
- * @returns {Promise<Block<T >> }
175
+ * @returns {Promise<API.BlockView<T, Code, Alg >> }
167
176
*/
168
177
const decode = async ( { bytes, codec, hasher } ) => {
169
178
if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
@@ -182,10 +191,12 @@ const decode = async ({ bytes, codec, hasher }) => {
182
191
*/
183
192
184
193
/**
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> }
189
200
*/
190
201
const createUnsafe = ( { bytes, cid, value : maybeValue , codec } ) => {
191
202
const value = maybeValue !== undefined
@@ -194,19 +205,24 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => {
194
205
195
206
if ( value === undefined ) throw new Error ( 'Missing required argument, must either provide "value" or "codec"' )
196
207
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
+ } )
198
213
}
199
214
200
215
/**
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
204
220
* @param {Object } options
205
- * @param {API.CID<Code, Alg> } options.cid
221
+ * @param {API.CID<Code, Alg, V > } options.cid
206
222
* @param {API.ByteView<T> } options.bytes
207
223
* @param {API.BlockDecoder<Code, T> } options.codec
208
224
* @param {API.MultihashHasher<Alg> } options.hasher
209
- * @returns {Promise<Block<T >> }
225
+ * @returns {Promise<API.BlockView<T, Code, Alg, V >> }
210
226
*/
211
227
const create = async ( { bytes, cid, hasher, codec } ) => {
212
228
if ( ! bytes ) throw new Error ( 'Missing required argument "bytes"' )
@@ -217,7 +233,12 @@ const create = async ({ bytes, cid, hasher, codec }) => {
217
233
throw new Error ( 'CID hash does not match bytes' )
218
234
}
219
235
220
- return createUnsafe ( { bytes, cid, value, codec } )
236
+ return createUnsafe ( {
237
+ bytes,
238
+ cid,
239
+ value,
240
+ codec
241
+ } )
221
242
}
222
243
223
244
export { encode , decode , create , createUnsafe , Block }
0 commit comments