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

Commit 3f446d6

Browse files
author
Alan Shaw
committed
fix: support legacy links in cbor data (#2631)
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 4a28ea9 commit 3f446d6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"cid-tool": "~0.4.0",
8282
"cids": "~0.7.1",
8383
"class-is": "^1.1.0",
84-
"dag-cbor-links": "^1.3.0",
84+
"dag-cbor-links": "^1.3.2",
8585
"datastore-core": "~0.7.0",
8686
"datastore-pubsub": "^0.2.1",
8787
"debug": "^4.1.0",

src/cli/commands/dag/put.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const mh = require('multihashes')
44
const multibase = require('multibase')
55
const dagCBOR = require('ipld-dag-cbor')
66
const dagPB = require('ipld-dag-pb')
7+
const CID = require('cids')
78
const { cidToString } = require('../../../utils/cid')
89

910
const inputDecoders = {
@@ -109,6 +110,12 @@ module.exports = {
109110

110111
source = inputDecoders[inputEncoding](source)
111112

113+
// Support legacy { "/" : "<CID>" } format so dag put is actually useful
114+
// on the command line: https://github.com/ipld/js-ipld-dag-cbor/issues/84
115+
if (inputEncoding === 'json' && format === 'dag-cbor') {
116+
source = objectSlashToCID(source)
117+
}
118+
112119
const cid = await ipfs.dag.put(source, {
113120
format,
114121
hashAlg,
@@ -122,3 +129,26 @@ module.exports = {
122129
})())
123130
}
124131
}
132+
133+
function objectSlashToCID (obj) {
134+
if (Array.isArray(obj)) {
135+
return obj.map(objectSlashToCID)
136+
}
137+
138+
if (obj && typeof obj === 'object') {
139+
const keys = Object.keys(obj)
140+
if (keys.length === 1 && '/' in obj) {
141+
if (typeof obj['/'] !== 'string') {
142+
throw new Error('link should have been a string')
143+
}
144+
return new CID(obj['/']) // throws if not a CID - consistent with go-ipfs
145+
}
146+
147+
return keys.reduce((obj, key) => {
148+
obj[key] = objectSlashToCID(obj[key])
149+
return obj
150+
}, obj)
151+
}
152+
153+
return obj
154+
}

test/cli/dag.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,30 @@ describe('dag', () => runOnAndOff.off((thing) => {
135135
const out = await ipfs('pin ls')
136136
expect(out).to.include(cid)
137137
})
138+
139+
it('puts a cbor node with a legacy { "/": "<CID>" } links', async function () {
140+
this.timeout(20 * 1000)
141+
142+
const input = `dag api rulz ${Date.now()}`
143+
144+
const linkedCid = (await ipfs('dag put', {
145+
input: Buffer.from(`"${input}"`)
146+
})).trim()
147+
148+
const cid = (await ipfs('dag put', {
149+
input: Buffer.from(JSON.stringify({
150+
link: { '/': linkedCid },
151+
arrayLink: [{ '/': linkedCid }],
152+
data: { test: Date.now() },
153+
noData: null
154+
}))
155+
})).trim()
156+
157+
const out0 = (await ipfs(`dag get ${cid}/link`)).trim()
158+
expect(out0).to.equal(input)
159+
160+
const out1 = (await ipfs(`dag get ${cid}/arrayLink/0`)).trim()
161+
expect(out1).to.equal(input)
162+
})
138163
})
139164
}))

0 commit comments

Comments
 (0)