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

Commit

Permalink
chore: update ipld formats (#1010)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
achingbrain authored and Alan Shaw committed May 21, 2019
1 parent 961fc4a commit a423d7f
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 82 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
"bl": "^3.0.0",
"bs58": "^4.0.1",
"buffer": "^5.2.1",
"cids": "~0.5.8",
"cids": "~0.7.1",
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
"debug": "^4.1.0",
"detect-node": "^2.0.4",
"end-of-stream": "^1.4.1",
"err-code": "^1.1.2",
"flatmap": "0.0.3",
"glob": "^7.1.3",
"ipfs-block": "~0.8.0",
"ipld-dag-cbor": "~0.13.1",
"ipld-dag-pb": "~0.15.3",
"is-ipfs": "~0.6.0",
"ipfs-block": "~0.8.1",
"ipld-dag-cbor": "~0.15.0",
"ipld-dag-pb": "~0.17.3",
"is-ipfs": "~0.6.1",
"is-pull-stream": "0.0.0",
"is-stream": "^2.0.0",
"iso-stream-http": "~0.1.2",
Expand All @@ -54,7 +54,7 @@
"lru-cache": "^5.1.1",
"multiaddr": "^6.0.6",
"multibase": "~0.6.0",
"multicodec": "~0.5.0",
"multicodec": "~0.5.1",
"multihashes": "~0.4.14",
"ndjson": "github:hugomrdias/ndjson#feat/readable-stream3",
"once": "^1.4.0",
Expand Down Expand Up @@ -86,7 +86,7 @@
"cross-env": "^5.2.0",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "0.4.19",
"interface-ipfs-core": "~0.102.0",
"interface-ipfs-core": "~0.103.0",
"ipfsd-ctl": "~0.42.0",
"nock": "^10.0.2",
"stream-equal": "^1.1.1"
Expand Down
13 changes: 10 additions & 3 deletions src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ module.exports = (send) => {
},
(ipfsBlock, path, cb) => {
const dagResolver = resolvers[ipfsBlock.cid.codec]

if (!dagResolver) {
const error = new Error('ipfs-http-client is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
error.missingMulticodec = ipfsBlock.cid.codec
cb(error)
return
return cb(error)
}

let res
try {
res = dagResolver.resolve(ipfsBlock.data, path)
} catch (err) {
return cb(err)
}
dagResolver.resolve(ipfsBlock.data, path, cb)
cb(null, res)
}
], callback)
})
Expand Down
54 changes: 29 additions & 25 deletions src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,38 @@ module.exports = (send) => {

options = Object.assign(optionDefaults, options)

if (options.format === 'dag-cbor') {
dagCBOR.util.serialize(dagNode, finalize)
} else if (options.format === 'dag-pb') {
dagPB.util.serialize(dagNode, finalize)
} else {
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
finalize(null, dagNode)
let serialized

try {
if (options.format === 'dag-cbor') {
serialized = dagCBOR.util.serialize(dagNode)
} else if (options.format === 'dag-pb') {
serialized = dagPB.util.serialize(dagNode)
} else {
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
serialized = dagNode
}
} catch (err) {
return callback(err)
}

function finalize (err, serialized) {
if (err) { return callback(err) }
const sendOptions = {
qs: {
hash: options.hashAlg,
format: options.format,
'input-enc': options.inputEnc
}
const sendOptions = {
qs: {
hash: options.hashAlg,
format: options.format,
'input-enc': options.inputEnc
}
sendOneFile(serialized, sendOptions, (err, result) => {
if (err) {
return callback(err)
}
if (result['Cid']) {
return callback(null, new CID(result['Cid']['/']))
} else {
return callback(result)
}
})
}

sendOneFile(serialized, sendOptions, (err, result) => {
if (err) {
return callback(err)
}
if (result['Cid']) {
return callback(null, new CID(result['Cid']['/']))
} else {
return callback(result)
}
})
})
}
15 changes: 7 additions & 8 deletions src/object/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ module.exports = (send) => {
return callback(err)
}

send({
path: 'object/patch/add-link',
args: [
cid.toString(),
dLink.name,
dLink.cid.toString()
]
}, (err, result) => {
const args = [
cid.toString(),
dLink.Name || dLink.name || null,
(dLink.Hash || dLink.cid || '').toString() || null
]

send({ path: 'object/patch/add-link', args }, (err, result) => {
if (err) {
return callback(err)
}
Expand Down
20 changes: 5 additions & 15 deletions src/object/get.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const { DAGNode, DAGLink } = require('ipld-dag-pb')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
Expand Down Expand Up @@ -49,19 +47,11 @@ module.exports = (send) => {
return callback(err)
}

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

const links = result.Links.map((l) => {
return new DAGLink(l.Name, l.Size, l.Hash)
})

DAGNode.create(result.Data, links, (err, node) => {
if (err) {
return callback(err)
}
cache.set(cidB58Str, node)
callback(null, node)
})
cache.set(cidB58Str, node)
callback(null, node)
})
})
}
7 changes: 4 additions & 3 deletions src/object/links.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
const { DAGLink } = require('ipld-dag-pb')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
Expand Down Expand Up @@ -44,7 +43,9 @@ module.exports = (send) => {
let links = []

if (result.Links) {
links = result.Links.map((l) => new DAGLink(l.Name, l.Size, l.Hash))
links = result.Links.map((l) => {
return new DAGLink(l.Name, l.Size, l.Hash)
})
}
callback(null, links)
})
Expand Down
12 changes: 6 additions & 6 deletions src/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ module.exports = (send) => {
}
} else if (DAGNode.isDAGNode(obj)) {
tmpObj = {
Data: obj.data.toString(),
Links: obj.links.map((l) => {
const link = l.toJSON()
link.hash = link.cid
return link
})
Data: obj.Data.toString(),
Links: obj.Links.map(l => ({
Name: l.Name,
Hash: l.Hash.toString(),
Size: l.Tsize
}))
}
} else if (typeof obj === 'object') {
tmpObj.Data = obj.Data.toString()
Expand Down
2 changes: 1 addition & 1 deletion src/object/rmLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = (send) => {
path: 'object/patch/rm-link',
args: [
cid.toString(),
dLink.name
dLink.Name || dLink.name || null
]
}, (err, result) => {
if (err) {
Expand Down
26 changes: 12 additions & 14 deletions test/dag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const series = require('async/series')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const { DAGNode } = require('ipld-dag-pb')
const CID = require('cids')
const ipfsClient = require('../src')
const f = require('./utils/factory')
Expand Down Expand Up @@ -37,20 +36,19 @@ describe('.dag', function () {

it('should be able to put and get a DAG node with format dag-pb', (done) => {
const data = Buffer.from('some data')
DAGNode.create(data, (err, node) => {
const node = DAGNode.create(data)

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
expect(err).to.not.exist()
ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
cid = cid.toV0()
expect(cid.codec).to.equal('dag-pb')
cid = cid.toBaseEncodedString('base58btc')
// expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u')
expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
ipfs.dag.get(cid, (err, result) => {
expect(err).to.not.exist()
cid = cid.toV0()
expect(cid.codec).to.equal('dag-pb')
cid = cid.toBaseEncodedString('base58btc')
// expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u')
expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
ipfs.dag.get(cid, (err, result) => {
expect(err).to.not.exist()
expect(result.value.data).to.deep.equal(data)
done()
})
expect(result.value.Data).to.deep.equal(data)
done()
})
})
})
Expand Down
5 changes: 5 additions & 0 deletions test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ describe('interface-ipfs-core tests', () => {
isNode ? null : {
name: 'should readable stream ls with a base58 encoded CID',
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
},
// .refs
{
name: 'dag refs test',
reason: 'FIXME unskip when 0.4.21 is released'
}
]
})
Expand Down

0 comments on commit a423d7f

Please sign in to comment.