Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: still load dag-pb, dag-cbor and raw when specifying custom forma…
Browse files Browse the repository at this point in the history
…ts (#3132)

If we specify a `formats` array as part of the ipld options in in-proc
nodes, it replaces the default list of dag-pb, dag-cbor and raw.

This change allows the `loadFormat` function to still resolve those
formats even if the user has passed a `format` array that does not
contain them.

Fixes #3129
  • Loading branch information
achingbrain committed Jul 1, 2020
1 parent 343bd45 commit a96e3bc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/ipfs-http-client/test/dag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

const { Buffer } = require('buffer')
const { expect } = require('interface-ipfs-core/src/utils/mocha')
const { DAGNode } = require('ipld-dag-pb')
const ipldDagPb = require('ipld-dag-pb')
const { DAGNode } = ipldDagPb
const CID = require('cids')
const f = require('./utils/factory')()
const ipfsHttpClient = require('../src')
Expand Down Expand Up @@ -98,4 +99,32 @@ describe('.dag', function () {

expect(askedToLoadFormat).to.be.true()
})

it('should allow formats to be specified without overwriting others', async () => {
const ipfs2 = ipfsHttpClient({
url: `http://${ipfs.apiHost}:${ipfs.apiPort}`,
ipld: {
formats: [
ipldDagPb
]
}
})

const dagCborNode = {
hello: 'world'
}
const cid1 = await ipfs2.dag.put(dagCborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
})

const dagPbNode = new DAGNode(Buffer.alloc(0), [], 0)
const cid2 = await ipfs2.dag.put(dagPbNode, {
format: 'dag-pb',
hashAlg: 'sha2-256'
})

await expect(ipfs2.dag.get(cid1)).to.eventually.have.property('value').that.deep.equals(dagCborNode)
await expect(ipfs2.dag.get(cid2)).to.eventually.have.property('value').that.deep.equals(dagPbNode)
})
})
9 changes: 9 additions & 0 deletions packages/ipfs/src/core/runtime/ipld-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ const multicodec = require('multicodec')

// All known (non-default) IPLD formats
const IpldFormats = {
get [multicodec.DAG_PB] () {
return require('ipld-dag-pb')
},
get [multicodec.DAG_CBOR] () {
return require('ipld-dag-cbor')
},
get [multicodec.RAW] () {
return require('ipld-raw')
},
get [multicodec.BITCOIN_BLOCK] () {
return require('ipld-bitcoin')
},
Expand Down
43 changes: 43 additions & 0 deletions packages/ipfs/test/core/ipld.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('interface-ipfs-core/src/utils/mocha')
const factory = require('../utils/factory')
const ipldDagPb = require('ipld-dag-pb')

describe('ipld', function () {
this.timeout(10 * 1000)
const df = factory()

after(() => df.clean())

it('should allow formats to be specified without overwriting others', async () => {
const ipfs = (await df.spawn({
type: 'proc',
ipfsOptions: {
ipld: {
formats: [
require('ipld-dag-pb')
]
}
}
})).api

const dagCborNode = {
hello: 'world'
}
const cid1 = await ipfs.dag.put(dagCborNode, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
})

const dagPbNode = new ipldDagPb.DAGNode(Buffer.alloc(0), [], 0)
const cid2 = await ipfs.dag.put(dagPbNode, {
format: 'dag-pb',
hashAlg: 'sha2-256'
})

await expect(ipfs.dag.get(cid1)).to.eventually.have.property('value').that.deep.equals(dagCborNode)
await expect(ipfs.dag.get(cid2)).to.eventually.have.property('value').that.deep.equals(dagPbNode)
})
})

0 comments on commit a96e3bc

Please sign in to comment.