Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 78c8769

Browse files
achingbrainvmx
authored andcommitted
feat: expose getFormat function
It's useful to be able to get the format implementations out of ipld just before `ipld.put` operations in the HTTP API server of IPFS. This is because we've serialized the node on the client for transmission over HTTP so need to deserialize it before passing it into `ipld.put` for it to be serialized (and as such, verified) again. We can duplicate the list of resolvers and logic behind the addition of new resolvers from ipld or we can just expose the `getFormat` function which we do here to reduce any code duplication.
1 parent 5bdfe5c commit 78c8769

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ the new strategy in [Issue #260](https://github.com/ipld/js-ipld/issues/260)
5151
- [`.removeMany(cids, options)`](#removemanycids-options)
5252
- [`.tree(cid, [path], [options])`](#treecid-path-options)
5353
- [`.addFormat(ipldFormatImplementation)`](#addformatipldformatimplementation)
54+
- [`.getFormat(codec)`](#getformatcodec)
5455
- [`.removeFormat(codec)`](#removeformatcodec)
5556
- [Properties](#properties)
5657
- [`defaultOptions`](#defaultoptions)
@@ -284,6 +285,13 @@ Returns an async iterator of all the paths (as Strings) you could resolve into.
284285

285286
Returns the IPLD instance. This way you can chain `addFormat()` calls.
286287

288+
### `.getFormat(codec)`
289+
290+
> Return the implementation for an IPLD Format
291+
292+
- `codec` (`multicodec`, required): the codec of the IPLD Format to return the implementation from.
293+
294+
If the implementation is not present in the current list of resolvers, the `loadFormat` function passed as an option to the constructor of this module will be invoked and it's output added to the list of available resolvers.
287295

288296
### `.removeFormat(codec)`
289297

src/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class IPLDResolver {
9191
const generator = async function * () {
9292
// End iteration if there isn't a CID to follow anymore
9393
while (cid !== null) {
94-
const format = await this._getFormat(cid.codec)
94+
const format = await this.getFormat(cid.codec)
9595

9696
// get block
9797
// use local resolver
@@ -133,7 +133,7 @@ class IPLDResolver {
133133
*/
134134
async get (cid, options) {
135135
const block = await this.bs.get(cid, options)
136-
const format = await this._getFormat(block.cid.codec)
136+
const format = await this.getFormat(block.cid.codec)
137137
const node = format.util.deserialize(block.data)
138138

139139
return node
@@ -182,7 +182,7 @@ class IPLDResolver {
182182
throw new Error('`format` parameter must be number (multicodec)')
183183
}
184184

185-
const formatImpl = await this._getFormat(format)
185+
const formatImpl = await this.getFormat(format)
186186
const defaultOptions = {
187187
hashAlg: formatImpl.defaultHashAlg,
188188
cidVersion: 1,
@@ -239,7 +239,7 @@ class IPLDResolver {
239239
// when we hit the first iteration. This way the constructor can be
240240
// a synchronous function.
241241
if (options === undefined) {
242-
formatImpl = await this._getFormat(format)
242+
formatImpl = await this.getFormat(format)
243243
const defaultOptions = {
244244
hashAlg: formatImpl.defaultHashAlg,
245245
cidVersion: 1,
@@ -318,7 +318,7 @@ class IPLDResolver {
318318
// If a path is a link then follow it and return its CID
319319
const maybeRecurse = async (block, treePath) => {
320320
// A treepath we might want to follow recursively
321-
const format = await this._getFormat(block.cid.codec)
321+
const format = await this.getFormat(block.cid.codec)
322322
const result = format.resolver.resolve(block.data, treePath)
323323
// Something to follow recusively, hence push it into the queue
324324
if (CID.isCID(result.value)) {
@@ -347,7 +347,7 @@ class IPLDResolver {
347347
// There aren't any paths left, get them from the given CID
348348
if (treePaths.length === 0 && queue.length > 0) {
349349
({ cid, basePath } = queue.shift())
350-
const format = await this._getFormat(cid.codec)
350+
const format = await this.getFormat(cid.codec)
351351
block = await this.bs.get(cid, options)
352352

353353
const paths = format.resolver.tree(block.data)
@@ -381,10 +381,7 @@ class IPLDResolver {
381381
return extendIterator(generator())
382382
}
383383

384-
/* */
385-
/* internals */
386-
/* */
387-
async _getFormat (codec) {
384+
async getFormat (codec) {
388385
// TODO vmx 2019-01-24: Once all CIDs support accessing the codec code
389386
// instead of the name, remove this part
390387
if (typeof codec === 'string') {

0 commit comments

Comments
 (0)