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

Commit a96e3bc

Browse files
authored
fix: still load dag-pb, dag-cbor and raw when specifying custom formats (#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
1 parent 343bd45 commit a96e3bc

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

packages/ipfs-http-client/test/dag.spec.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
const { Buffer } = require('buffer')
77
const { expect } = require('interface-ipfs-core/src/utils/mocha')
8-
const { DAGNode } = require('ipld-dag-pb')
8+
const ipldDagPb = require('ipld-dag-pb')
9+
const { DAGNode } = ipldDagPb
910
const CID = require('cids')
1011
const f = require('./utils/factory')()
1112
const ipfsHttpClient = require('../src')
@@ -98,4 +99,32 @@ describe('.dag', function () {
9899

99100
expect(askedToLoadFormat).to.be.true()
100101
})
102+
103+
it('should allow formats to be specified without overwriting others', async () => {
104+
const ipfs2 = ipfsHttpClient({
105+
url: `http://${ipfs.apiHost}:${ipfs.apiPort}`,
106+
ipld: {
107+
formats: [
108+
ipldDagPb
109+
]
110+
}
111+
})
112+
113+
const dagCborNode = {
114+
hello: 'world'
115+
}
116+
const cid1 = await ipfs2.dag.put(dagCborNode, {
117+
format: 'dag-cbor',
118+
hashAlg: 'sha2-256'
119+
})
120+
121+
const dagPbNode = new DAGNode(Buffer.alloc(0), [], 0)
122+
const cid2 = await ipfs2.dag.put(dagPbNode, {
123+
format: 'dag-pb',
124+
hashAlg: 'sha2-256'
125+
})
126+
127+
await expect(ipfs2.dag.get(cid1)).to.eventually.have.property('value').that.deep.equals(dagCborNode)
128+
await expect(ipfs2.dag.get(cid2)).to.eventually.have.property('value').that.deep.equals(dagPbNode)
129+
})
101130
})

packages/ipfs/src/core/runtime/ipld-nodejs.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ const multicodec = require('multicodec')
44

55
// All known (non-default) IPLD formats
66
const IpldFormats = {
7+
get [multicodec.DAG_PB] () {
8+
return require('ipld-dag-pb')
9+
},
10+
get [multicodec.DAG_CBOR] () {
11+
return require('ipld-dag-cbor')
12+
},
13+
get [multicodec.RAW] () {
14+
return require('ipld-raw')
15+
},
716
get [multicodec.BITCOIN_BLOCK] () {
817
return require('ipld-bitcoin')
918
},

packages/ipfs/test/core/ipld.spec.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { expect } = require('interface-ipfs-core/src/utils/mocha')
5+
const factory = require('../utils/factory')
6+
const ipldDagPb = require('ipld-dag-pb')
7+
8+
describe('ipld', function () {
9+
this.timeout(10 * 1000)
10+
const df = factory()
11+
12+
after(() => df.clean())
13+
14+
it('should allow formats to be specified without overwriting others', async () => {
15+
const ipfs = (await df.spawn({
16+
type: 'proc',
17+
ipfsOptions: {
18+
ipld: {
19+
formats: [
20+
require('ipld-dag-pb')
21+
]
22+
}
23+
}
24+
})).api
25+
26+
const dagCborNode = {
27+
hello: 'world'
28+
}
29+
const cid1 = await ipfs.dag.put(dagCborNode, {
30+
format: 'dag-cbor',
31+
hashAlg: 'sha2-256'
32+
})
33+
34+
const dagPbNode = new ipldDagPb.DAGNode(Buffer.alloc(0), [], 0)
35+
const cid2 = await ipfs.dag.put(dagPbNode, {
36+
format: 'dag-pb',
37+
hashAlg: 'sha2-256'
38+
})
39+
40+
await expect(ipfs.dag.get(cid1)).to.eventually.have.property('value').that.deep.equals(dagCborNode)
41+
await expect(ipfs.dag.get(cid2)).to.eventually.have.property('value').that.deep.equals(dagPbNode)
42+
})
43+
})

0 commit comments

Comments
 (0)