Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit a423d7f

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
chore: update ipld formats (#1010)
BREAKING CHANGE: The default string encoding for version 1 CIDs has changed to `base32`. IPLD formats have been updated to the latest versions. IPLD nodes returned by `ipfs.dag` and `ipfs.object` commands have significant breaking changes. If you are using these commands in your application you are likely to encounter the following changes to `dag-pb` nodes (the default node type that IPFS creates): * `DAGNode` properties have been renamed as follows: * `data` => `Data` * `links` => `Links` * `size` => `size` (Note: no change) * `DAGLink` properties have been renamed as follows: * `cid` => `Hash` * `name` => `Name` * `size` => `Tsize` See CHANGELOGs for each IPLD format for it's respective changes, you can read more about the [`dag-pb` changes in the CHANGELOG](https://github.com/ipld/js-ipld-dag-pb/blob/master) License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 961fc4a commit a423d7f

File tree

10 files changed

+86
-82
lines changed

10 files changed

+86
-82
lines changed

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
"bl": "^3.0.0",
3232
"bs58": "^4.0.1",
3333
"buffer": "^5.2.1",
34-
"cids": "~0.5.8",
34+
"cids": "~0.7.1",
3535
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
3636
"debug": "^4.1.0",
3737
"detect-node": "^2.0.4",
3838
"end-of-stream": "^1.4.1",
3939
"err-code": "^1.1.2",
4040
"flatmap": "0.0.3",
4141
"glob": "^7.1.3",
42-
"ipfs-block": "~0.8.0",
43-
"ipld-dag-cbor": "~0.13.1",
44-
"ipld-dag-pb": "~0.15.3",
45-
"is-ipfs": "~0.6.0",
42+
"ipfs-block": "~0.8.1",
43+
"ipld-dag-cbor": "~0.15.0",
44+
"ipld-dag-pb": "~0.17.3",
45+
"is-ipfs": "~0.6.1",
4646
"is-pull-stream": "0.0.0",
4747
"is-stream": "^2.0.0",
4848
"iso-stream-http": "~0.1.2",
@@ -54,7 +54,7 @@
5454
"lru-cache": "^5.1.1",
5555
"multiaddr": "^6.0.6",
5656
"multibase": "~0.6.0",
57-
"multicodec": "~0.5.0",
57+
"multicodec": "~0.5.1",
5858
"multihashes": "~0.4.14",
5959
"ndjson": "github:hugomrdias/ndjson#feat/readable-stream3",
6060
"once": "^1.4.0",
@@ -86,7 +86,7 @@
8686
"cross-env": "^5.2.0",
8787
"dirty-chai": "^2.0.1",
8888
"go-ipfs-dep": "0.4.19",
89-
"interface-ipfs-core": "~0.102.0",
89+
"interface-ipfs-core": "~0.103.0",
9090
"ipfsd-ctl": "~0.42.0",
9191
"nock": "^10.0.2",
9292
"stream-equal": "^1.1.1"

src/dag/get.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ module.exports = (send) => {
4646
},
4747
(ipfsBlock, path, cb) => {
4848
const dagResolver = resolvers[ipfsBlock.cid.codec]
49+
4950
if (!dagResolver) {
5051
const error = new Error('ipfs-http-client is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
5152
error.missingMulticodec = ipfsBlock.cid.codec
52-
cb(error)
53-
return
53+
return cb(error)
54+
}
55+
56+
let res
57+
try {
58+
res = dagResolver.resolve(ipfsBlock.data, path)
59+
} catch (err) {
60+
return cb(err)
5461
}
55-
dagResolver.resolve(ipfsBlock.data, path, cb)
62+
cb(null, res)
5663
}
5764
], callback)
5865
})

src/dag/put.js

+29-25
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,38 @@ module.exports = (send) => {
5050

5151
options = Object.assign(optionDefaults, options)
5252

53-
if (options.format === 'dag-cbor') {
54-
dagCBOR.util.serialize(dagNode, finalize)
55-
} else if (options.format === 'dag-pb') {
56-
dagPB.util.serialize(dagNode, finalize)
57-
} else {
58-
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
59-
finalize(null, dagNode)
53+
let serialized
54+
55+
try {
56+
if (options.format === 'dag-cbor') {
57+
serialized = dagCBOR.util.serialize(dagNode)
58+
} else if (options.format === 'dag-pb') {
59+
serialized = dagPB.util.serialize(dagNode)
60+
} else {
61+
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
62+
serialized = dagNode
63+
}
64+
} catch (err) {
65+
return callback(err)
6066
}
6167

62-
function finalize (err, serialized) {
63-
if (err) { return callback(err) }
64-
const sendOptions = {
65-
qs: {
66-
hash: options.hashAlg,
67-
format: options.format,
68-
'input-enc': options.inputEnc
69-
}
68+
const sendOptions = {
69+
qs: {
70+
hash: options.hashAlg,
71+
format: options.format,
72+
'input-enc': options.inputEnc
7073
}
71-
sendOneFile(serialized, sendOptions, (err, result) => {
72-
if (err) {
73-
return callback(err)
74-
}
75-
if (result['Cid']) {
76-
return callback(null, new CID(result['Cid']['/']))
77-
} else {
78-
return callback(result)
79-
}
80-
})
8174
}
75+
76+
sendOneFile(serialized, sendOptions, (err, result) => {
77+
if (err) {
78+
return callback(err)
79+
}
80+
if (result['Cid']) {
81+
return callback(null, new CID(result['Cid']['/']))
82+
} else {
83+
return callback(result)
84+
}
85+
})
8286
})
8387
}

src/object/addLink.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ module.exports = (send) => {
1919
return callback(err)
2020
}
2121

22-
send({
23-
path: 'object/patch/add-link',
24-
args: [
25-
cid.toString(),
26-
dLink.name,
27-
dLink.cid.toString()
28-
]
29-
}, (err, result) => {
22+
const args = [
23+
cid.toString(),
24+
dLink.Name || dLink.name || null,
25+
(dLink.Hash || dLink.cid || '').toString() || null
26+
]
27+
28+
send({ path: 'object/patch/add-link', args }, (err, result) => {
3029
if (err) {
3130
return callback(err)
3231
}

src/object/get.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const dagPB = require('ipld-dag-pb')
5-
const DAGNode = dagPB.DAGNode
6-
const DAGLink = dagPB.DAGLink
4+
const { DAGNode, DAGLink } = require('ipld-dag-pb')
75
const CID = require('cids')
86
const LRU = require('lru-cache')
97
const lruOptions = {
@@ -49,19 +47,11 @@ module.exports = (send) => {
4947
return callback(err)
5048
}
5149

52-
result.Data = Buffer.from(result.Data, 'base64')
50+
const links = result.Links.map(l => new DAGLink(l.Name, l.Size, l.Hash))
51+
const node = DAGNode.create(Buffer.from(result.Data, 'base64'), links)
5352

54-
const links = result.Links.map((l) => {
55-
return new DAGLink(l.Name, l.Size, l.Hash)
56-
})
57-
58-
DAGNode.create(result.Data, links, (err, node) => {
59-
if (err) {
60-
return callback(err)
61-
}
62-
cache.set(cidB58Str, node)
63-
callback(null, node)
64-
})
53+
cache.set(cidB58Str, node)
54+
callback(null, node)
6555
})
6656
})
6757
}

src/object/links.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const dagPB = require('ipld-dag-pb')
5-
const DAGLink = dagPB.DAGLink
4+
const { DAGLink } = require('ipld-dag-pb')
65
const CID = require('cids')
76
const LRU = require('lru-cache')
87
const lruOptions = {
@@ -44,7 +43,9 @@ module.exports = (send) => {
4443
let links = []
4544

4645
if (result.Links) {
47-
links = result.Links.map((l) => new DAGLink(l.Name, l.Size, l.Hash))
46+
links = result.Links.map((l) => {
47+
return new DAGLink(l.Name, l.Size, l.Hash)
48+
})
4849
}
4950
callback(null, links)
5051
})

src/object/put.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ module.exports = (send) => {
3636
}
3737
} else if (DAGNode.isDAGNode(obj)) {
3838
tmpObj = {
39-
Data: obj.data.toString(),
40-
Links: obj.links.map((l) => {
41-
const link = l.toJSON()
42-
link.hash = link.cid
43-
return link
44-
})
39+
Data: obj.Data.toString(),
40+
Links: obj.Links.map(l => ({
41+
Name: l.Name,
42+
Hash: l.Hash.toString(),
43+
Size: l.Tsize
44+
}))
4545
}
4646
} else if (typeof obj === 'object') {
4747
tmpObj.Data = obj.Data.toString()

src/object/rmLink.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (send) => {
2323
path: 'object/patch/rm-link',
2424
args: [
2525
cid.toString(),
26-
dLink.name
26+
dLink.Name || dLink.name || null
2727
]
2828
}, (err, result) => {
2929
if (err) {

test/dag.spec.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const dirtyChai = require('dirty-chai')
88
const expect = chai.expect
99
chai.use(dirtyChai)
1010
const series = require('async/series')
11-
const dagPB = require('ipld-dag-pb')
12-
const DAGNode = dagPB.DAGNode
11+
const { DAGNode } = require('ipld-dag-pb')
1312
const CID = require('cids')
1413
const ipfsClient = require('../src')
1514
const f = require('./utils/factory')
@@ -37,20 +36,19 @@ describe('.dag', function () {
3736

3837
it('should be able to put and get a DAG node with format dag-pb', (done) => {
3938
const data = Buffer.from('some data')
40-
DAGNode.create(data, (err, node) => {
39+
const node = DAGNode.create(data)
40+
41+
ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
4142
expect(err).to.not.exist()
42-
ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
43+
cid = cid.toV0()
44+
expect(cid.codec).to.equal('dag-pb')
45+
cid = cid.toBaseEncodedString('base58btc')
46+
// expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u')
47+
expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
48+
ipfs.dag.get(cid, (err, result) => {
4349
expect(err).to.not.exist()
44-
cid = cid.toV0()
45-
expect(cid.codec).to.equal('dag-pb')
46-
cid = cid.toBaseEncodedString('base58btc')
47-
// expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u')
48-
expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
49-
ipfs.dag.get(cid, (err, result) => {
50-
expect(err).to.not.exist()
51-
expect(result.value.data).to.deep.equal(data)
52-
done()
53-
})
50+
expect(result.value.Data).to.deep.equal(data)
51+
done()
5452
})
5553
})
5654
})

test/interface.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ describe('interface-ipfs-core tests', () => {
173173
isNode ? null : {
174174
name: 'should readable stream ls with a base58 encoded CID',
175175
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
176+
},
177+
// .refs
178+
{
179+
name: 'dag refs test',
180+
reason: 'FIXME unskip when 0.4.21 is released'
176181
}
177182
]
178183
})

0 commit comments

Comments
 (0)