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

fix: still load dag-pb, dag-cbor and raw when specifying custom formats #3132

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})