Skip to content

Commit

Permalink
fix: support legacy links in cbor data (ipfs#2631)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Nov 27, 2019
1 parent f08758e commit f98023b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"cid-tool": "~0.4.0",
"cids": "~0.7.1",
"class-is": "^1.1.0",
"dag-cbor-links": "^1.3.0",
"dag-cbor-links": "^1.3.2",
"datastore-core": "~0.7.0",
"datastore-pubsub": "^0.2.1",
"debug": "^4.1.0",
Expand Down
30 changes: 30 additions & 0 deletions src/cli/commands/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const mh = require('multihashes')
const multibase = require('multibase')
const dagCBOR = require('ipld-dag-cbor')
const dagPB = require('ipld-dag-pb')
const CID = require('cids')
const { cidToString } = require('../../../utils/cid')

const inputDecoders = {
Expand Down Expand Up @@ -109,6 +110,12 @@ module.exports = {

source = inputDecoders[inputEncoding](source)

// Support legacy { "/" : "<CID>" } format so dag put is actually useful
// on the command line: https://github.com/ipld/js-ipld-dag-cbor/issues/84
if (inputEncoding === 'json' && format === 'dag-cbor') {
source = objectSlashToCID(source)
}

const cid = await ipfs.dag.put(source, {
format,
hashAlg,
Expand All @@ -122,3 +129,26 @@ module.exports = {
})())
}
}

function objectSlashToCID (obj) {
if (Array.isArray(obj)) {
return obj.map(objectSlashToCID)
}

if (obj && typeof obj === 'object') {
const keys = Object.keys(obj)
if (keys.length === 1 && '/' in obj) {
if (typeof obj['/'] !== 'string') {
throw new Error('link should have been a string')
}
return new CID(obj['/']) // throws if not a CID - consistent with go-ipfs
}

return keys.reduce((obj, key) => {
obj[key] = objectSlashToCID(obj[key])
return obj
}, obj)
}

return obj
}
25 changes: 25 additions & 0 deletions test/cli/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,30 @@ describe('dag', () => runOnAndOff.off((thing) => {
const out = await ipfs('pin ls')
expect(out).to.include(cid)
})

it('puts a cbor node with a legacy { "/": "<CID>" } links', async function () {
this.timeout(20 * 1000)

const input = `dag api rulz ${Date.now()}`

const linkedCid = (await ipfs('dag put', {
input: Buffer.from(`"${input}"`)
})).trim()

const cid = (await ipfs('dag put', {
input: Buffer.from(JSON.stringify({
link: { '/': linkedCid },
arrayLink: [{ '/': linkedCid }],
data: { test: Date.now() },
noData: null
}))
})).trim()

const out0 = (await ipfs(`dag get ${cid}/link`)).trim()
expect(out0).to.equal(input)

const out1 = (await ipfs(`dag get ${cid}/arrayLink/0`)).trim()
expect(out1).to.equal(input)
})
})
}))

0 comments on commit f98023b

Please sign in to comment.