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

Commit 1b0b22d

Browse files
committed
feat: migrate cli to use new async DAGNode interface
1 parent 01e56ec commit 1b0b22d

File tree

9 files changed

+76
-23
lines changed

9 files changed

+76
-23
lines changed

src/cli/commands/object/get.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ module.exports = {
2323
throw err
2424
}
2525

26-
const res = node.toJSON()
27-
res.Data = res.Data ? res.Data.toString() : ''
28-
console.log(JSON.stringify(res))
26+
node.toJSON((err, nodeJSON) => {
27+
if (err) {
28+
throw err
29+
}
30+
nodeJSON.Data = nodeJSON.Data ? nodeJSON.Data.toString() : ''
31+
console.log(JSON.stringify(nodeJSON))
32+
})
2933
})
3034
})
3135
}

src/cli/commands/object/new.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ module.exports = {
2323
throw err
2424
}
2525

26-
console.log(node.toJSON().Hash)
26+
node.toJSON((err, nodeJSON) => {
27+
if (err) {
28+
throw err
29+
}
30+
console.log(nodeJSON.Hash)
31+
})
2732
})
2833
})
2934
}

src/cli/commands/object/patch/add-link.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const utils = require('../../../utils')
44
const debug = require('debug')
55
const log = debug('cli:object')
6-
const mDAG = require('ipfs-merkle-dag')
7-
const DAGLink = mDAG.DAGLink
6+
const dagPB = require('ipld-dag-pb')
7+
const DAGLink = dagPB.DAGLink
88
log.error = debug('cli:object:error')
99

1010
module.exports = {
@@ -15,23 +15,41 @@ module.exports = {
1515
builder: {},
1616

1717
handler (argv) {
18-
utils.getIPFS((err, ipfs) => {
18+
utils.getIPFS(gotIPFS)
19+
20+
function gotIPFS (err, ipfs) {
1921
if (err) {
2022
throw err
2123
}
2224

2325
ipfs.object.get(argv.ref, {enc: 'base58'}).then((linkedObj) => {
24-
const link = new DAGLink(
25-
argv.name,
26-
linkedObj.size(),
27-
linkedObj.multihash()
28-
)
29-
return ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'})
30-
}).then((node) => {
31-
console.log(node.toJSON().Hash)
26+
linkedObj.size((err, size) => {
27+
if (err) {
28+
throw err
29+
}
30+
linkedObj.multihash((err, multihash) => {
31+
if (err) {
32+
throw err
33+
}
34+
35+
const link = new DAGLink(argv.name, size, multihash)
36+
37+
ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'})
38+
.then((node) => {
39+
node.toJSON(gotJSON)
40+
41+
function gotJSON (err, nodeJSON) {
42+
if (err) {
43+
throw err
44+
}
45+
console.log(nodeJSON.Hash)
46+
}
47+
})
48+
})
49+
})
3250
}).catch((err) => {
3351
throw err
3452
})
35-
})
53+
}
3654
}
3755
}

src/cli/commands/object/patch/append-data.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function appendData (key, data) {
1818
throw err
1919
}
2020

21-
console.log(node.toJSON().Hash)
21+
node.toJSON((err, nodeJSON) => {
22+
if (err) {
23+
throw err
24+
}
25+
26+
console.log(nodeJSON.Hash)
27+
})
2228
})
2329
})
2430
}

src/cli/commands/object/patch/rm-link.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const DAGLink = require('ipfs-merkle-dag').DAGLink
3+
const DAGLink = require('ipld-dag-pb').DAGLink
44
const utils = require('../../../utils')
55
const debug = require('debug')
66
const log = debug('cli:object')
@@ -26,7 +26,12 @@ module.exports = {
2626
throw err
2727
}
2828

29-
console.log(node.toJSON().Hash)
29+
node.toJSON((err, nodeJSON) => {
30+
if (err) {
31+
throw err
32+
}
33+
console.log(nodeJSON.Hash)
34+
})
3035
})
3136
})
3237
}

src/cli/commands/object/patch/set-data.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function parseAndAddNode (key, data) {
1818
throw err
1919
}
2020

21-
console.log(node.toJSON().Hash)
21+
node.toJSON((err, nodeJSON) => {
22+
if (err) {
23+
throw err
24+
}
25+
26+
console.log(nodeJSON.Hash)
27+
})
2228
})
2329
})
2430
}

src/cli/commands/object/put.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function putNode (buf, enc) {
1818
throw err
1919
}
2020

21-
console.log('added', node.toJSON().Hash)
21+
node.toJSON((err, nodeJSON) => {
22+
if (err) {
23+
throw err
24+
}
25+
26+
console.log('added', nodeJSON.Hash)
27+
})
2228
})
2329
})
2430
}

src/http-api/resources/object.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,17 @@ exports.patchAddLink = {
443443
}).code(500)
444444
}
445445

446-
node.toJSON((err, nodeJSON) => {
446+
node.toJSON(gotJSON)
447+
448+
function gotJSON (err, nodeJSON) {
447449
if (err) {
448450
return reply({
449451
Message: 'Failed to get object: ' + err,
450452
Code: 0
451453
}).code(500)
452454
}
453455
return reply(nodeJSON)
454-
})
456+
}
455457
})
456458
})
457459
})

test/http-api/ipfs-api/test-object.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
23
'use strict'
34

45
const expect = require('chai').expect

0 commit comments

Comments
 (0)