@@ -127,42 +127,94 @@ class IPLDResolver {
127
127
return extendIterator ( generator ( ) )
128
128
}
129
129
130
+ /**
131
+ * Get a node by CID.
132
+ *
133
+ * @param {CID } cid - The CID of the IPLD Node that should be retrieved.
134
+ * @returns {Promise.<Object> } - Returns a Promise with the IPLD Node that correspond to the given `cid`.
135
+ */
136
+ async get ( cid ) {
137
+ const block = await promisify ( this . bs . get . bind ( this . bs ) ) ( cid )
138
+ const format = await this . _getFormat ( block . cid . codec )
139
+ const node = await promisify ( format . util . deserialize ) ( block . data )
140
+
141
+ return node
142
+ }
143
+
130
144
/**
131
145
* Get multiple nodes back from an array of CIDs.
132
146
*
133
147
* @param {Iterable.<CID> } cids - The CIDs of the IPLD Nodes that should be retrieved.
134
148
* @returns {Iterable.<Promise.<Object>> } - Returns an async iterator with the IPLD Nodes that correspond to the given `cids`.
135
149
*/
136
- get ( cids ) {
150
+ getMany ( cids ) {
137
151
if ( ! typical . isIterable ( cids ) || typical . isString ( cids ) ||
138
152
Buffer . isBuffer ( cids ) ) {
139
153
throw new Error ( '`cids` must be an iterable of CIDs' )
140
154
}
141
155
142
156
const generator = async function * ( ) {
143
157
for await ( const cid of cids ) {
144
- const block = await promisify ( this . bs . get . bind ( this . bs ) ) ( cid )
145
- const format = await this . _getFormat ( block . cid . codec )
146
- const node = await promisify ( format . util . deserialize ) ( block . data )
147
- yield node
158
+ yield this . get ( cid )
148
159
}
149
160
} . bind ( this )
150
161
151
162
return extendIterator ( generator ( ) )
152
163
}
153
164
165
+ /**
166
+ * Stores the given IPLD Node of a recognized IPLD Format.
167
+ *
168
+ * @param {Object } node - The deserialized IPLD node that should be inserted.
169
+ * @param {number } format - The multicodec of the format that IPLD Node should be encoded in.
170
+ * @param {Object } [userOptions] - Options is an object with the following properties.
171
+ * @param {number } [userOtions.hashAlg=hash algorithm of the given multicodec] - The hashing algorithm that is used to calculate the CID.
172
+ * @param {number } [userOptions.cidVersion=1] - The CID version to use.
173
+ * @param {boolean } [userOptions.onlyHash=false] - If true the serialized form of the IPLD Node will not be passed to the underlying block store.
174
+ * @returns {Promise.<CID> } - Returns the CID of the serialized IPLD Nodes.
175
+ */
176
+ async put ( node , format , userOptions ) {
177
+ if ( format === undefined ) {
178
+ throw new Error ( '`put` requires a format' )
179
+ }
180
+ if ( typeof format !== 'number' ) {
181
+ throw new Error ( '`format` parameter must be number (multicodec)' )
182
+ }
183
+
184
+ const formatImpl = await this . _getFormat ( format )
185
+ const defaultOptions = {
186
+ hashAlg : formatImpl . defaultHashAlg ,
187
+ cidVersion : 1 ,
188
+ onlyHash : false
189
+ }
190
+ const options = mergeOptions ( defaultOptions , userOptions )
191
+
192
+ const cidOptions = {
193
+ version : options . cidVersion ,
194
+ hashAlg : options . hashAlg ,
195
+ onlyHash : options . onlyHash
196
+ }
197
+ const cid = await promisify ( formatImpl . util . cid ) ( node , cidOptions )
198
+
199
+ if ( ! options . onlyHash ) {
200
+ await this . _store ( cid , node )
201
+ }
202
+
203
+ return cid
204
+ }
205
+
154
206
/**
155
207
* Stores the given IPLD Nodes of a recognized IPLD Format.
156
208
*
157
209
* @param {Iterable.<Object> } nodes - Deserialized IPLD nodes that should be inserted.
158
210
* @param {number } format - The multicodec of the format that IPLD Node should be encoded in.
159
211
* @param {Object } [userOptions] - Options are applied to any of the `nodes` and is an object with the following properties.
160
212
* @param {number } [userOtions.hashAlg=hash algorithm of the given multicodec] - The hashing algorithm that is used to calculate the CID.
161
- * @param {number } [userOptions.cidVersion=1]` - The CID version to use.
213
+ * @param {number } [userOptions.cidVersion=1] - The CID version to use.
162
214
* @param {boolean } [userOptions.onlyHash=false] - If true the serialized form of the IPLD Node will not be passed to the underlying block store.
163
215
* @returns {Iterable.<Promise.<CID>> } - Returns an async iterator with the CIDs of the serialized IPLD Nodes.
164
216
*/
165
- put ( nodes , format , userOptions ) {
217
+ putMany ( nodes , format , userOptions ) {
166
218
if ( ! typical . isIterable ( nodes ) || typical . isString ( nodes ) ||
167
219
Buffer . isBuffer ( nodes ) ) {
168
220
throw new Error ( '`nodes` must be an iterable' )
@@ -192,41 +244,41 @@ class IPLDResolver {
192
244
options = mergeOptions ( defaultOptions , userOptions )
193
245
}
194
246
195
- const cidOptions = {
196
- version : options . cidVersion ,
197
- hashAlg : options . hashAlg ,
198
- onlyHash : options . onlyHash
199
- }
200
- const cid = await promisify ( formatImpl . util . cid ) ( node , cidOptions )
201
- if ( ! options . onlyHash ) {
202
- await this . _store ( cid , node )
203
- }
204
- yield cid
247
+ yield this . put ( node , format , options )
205
248
}
206
249
} . bind ( this )
207
250
208
251
return extendIterator ( generator ( ) )
209
252
}
210
253
254
+ /**
255
+ * Remove an IPLD Node by the given CID.
256
+ *
257
+ * @param {CID } cid - The CID of the IPLD Node that should be removed.
258
+ * @return {Promise.<CID> } The CID of the removed IPLD Node.
259
+ */
260
+ async remove ( cid ) {
261
+ return promisify ( this . bs . delete . bind ( this . bs ) ) ( cid )
262
+ }
263
+
211
264
/**
212
265
* Remove IPLD Nodes by the given CIDs.
213
266
*
214
267
* Throws an error if any of the Blocks can’t be removed. This operation is
215
268
* *not* atomic, some Blocks might have already been removed.
216
269
*
217
- * @param {Iterable.<CID> } cids - The CIDs of the IPLD Nodes that should be removed
218
- * @return {void }
270
+ * @param {Iterable.<CID> } cids - The CIDs of the IPLD Nodes that should be removed.
271
+ * @return {Iterable.<Promise.<CID>> } Returns an async iterator with the CIDs of the removed IPLD Nodes.
219
272
*/
220
- remove ( cids ) {
273
+ removeMany ( cids ) {
221
274
if ( ! typical . isIterable ( cids ) || typical . isString ( cids ) ||
222
275
Buffer . isBuffer ( cids ) ) {
223
276
throw new Error ( '`cids` must be an iterable of CIDs' )
224
277
}
225
278
226
279
const generator = async function * ( ) {
227
280
for await ( const cid of cids ) {
228
- await promisify ( this . bs . delete . bind ( this . bs ) ) ( cid )
229
- yield cid
281
+ yield this . remove ( cid )
230
282
}
231
283
} . bind ( this )
232
284
0 commit comments