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

Commit 8a2688e

Browse files
fix async methods everyhwere
1 parent 8a95179 commit 8a2688e

File tree

16 files changed

+286
-246
lines changed

16 files changed

+286
-246
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
"browser": {
1111
"libp2p-ipfs": "libp2p-ipfs-browser",
1212
"./src/core/default-repo.js": "./src/core/default-repo-browser.js",
13-
"./lib/core/default-repo.js": "./lib/core/default-repo-browser.js"
13+
"./lib/core/default-repo.js": "./lib/core/default-repo-browser.js",
14+
"./src/core/components/init-assets.js": false,
15+
"./lib/core/components/init-assets.js": false,
16+
"fs-pull-blob-store": false,
17+
"glob": false,
18+
"rimraf": false
1419
},
1520
"scripts": {
1621
"lint": "aegir-lint",
@@ -115,6 +120,7 @@
115120
"run-series": "^1.1.4",
116121
"run-waterfall": "^1.1.3",
117122
"stream-to-pull-stream": "^1.7.0",
123+
"tar-stream": "^1.5.2",
118124
"temp": "^0.8.3",
119125
"through2": "^2.0.1",
120126
"update-notifier": "^1.0.2",

src/cli/commands/object/get.js

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

3+
const waterfall = require('run-waterfall')
34
const utils = require('../../utils')
45
const debug = require('debug')
56
const log = debug('cli:object')
@@ -13,20 +14,17 @@ module.exports = {
1314
builder: {},
1415

1516
handler (argv) {
16-
utils.getIPFS((err, ipfs) => {
17+
waterfall([
18+
(cb) => utils.getIPFS(cb),
19+
(ipfs, cb) => ipfs.object.get(argv.key, {enc: 'base58'}, cb),
20+
(node, cb) => node.toJSON(cb)
21+
], (err, res) => {
1722
if (err) {
1823
throw err
1924
}
2025

21-
ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => {
22-
if (err) {
23-
throw err
24-
}
25-
26-
const res = node.toJSON()
27-
res.Data = res.Data ? res.Data.toString() : ''
28-
console.log(JSON.stringify(res))
29-
})
26+
res.Data = res.Data ? res.Data.toString() : ''
27+
console.log(JSON.stringify(res))
3028
})
3129
}
3230
}

src/cli/commands/object/new.js

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

3+
const waterfall = require('run-waterfall')
34
const utils = require('../../utils')
45
const debug = require('debug')
56
const log = debug('cli:object')
@@ -13,18 +14,16 @@ module.exports = {
1314
builder: {},
1415

1516
handler (argv) {
16-
utils.getIPFS((err, ipfs) => {
17+
waterfall([
18+
(cb) => utils.getIPFS(cb),
19+
(ipfs, cb) => ipfs.object.new(cb),
20+
(node, cb) => node.toJSON(cb)
21+
], (err, node) => {
1722
if (err) {
1823
throw err
1924
}
2025

21-
ipfs.object.new((err, node) => {
22-
if (err) {
23-
throw err
24-
}
25-
26-
console.log(node.toJSON().Hash)
27-
})
26+
console.log(node.Hash)
2827
})
2928
}
3029
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const utils = require('../../../utils')
44
const debug = require('debug')
55
const log = debug('cli:object')
66
const mDAG = require('ipfs-merkle-dag')
7+
const waterfall = require('run-waterfall')
8+
const parallel = require('run-parallel')
79
const DAGLink = mDAG.DAGLink
810
log.error = debug('cli:object:error')
911

@@ -15,23 +17,29 @@ module.exports = {
1517
builder: {},
1618

1719
handler (argv) {
18-
utils.getIPFS((err, ipfs) => {
20+
waterfall([
21+
(cb) => utils.getIPFS(cb),
22+
(ipfs, cb) => waterfall([
23+
(cb) => ipfs.object.get(argv.ref, {enc: 'base58'}, cb),
24+
(linkedObj, cb) => parallel([
25+
(cb) => linkedObj.size(cb),
26+
(cb) => linkedObj.multihash(cb)
27+
], cb)
28+
], (err, stats) => {
29+
if (err) {
30+
return cb(err)
31+
}
32+
33+
const link = new DAGLink(argv.name, stats[0], stats[1])
34+
ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}, cb)
35+
}),
36+
(node, cb) => node.toJSON(cb)
37+
], (err, node) => {
1938
if (err) {
2039
throw err
2140
}
2241

23-
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)
32-
}).catch((err) => {
33-
throw err
34-
})
42+
console.log(node.Hash)
3543
})
3644
}
3745
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
const utils = require('../../../utils')
44
const bl = require('bl')
55
const fs = require('fs')
6+
const waterfall = require('run-waterfall')
67
const debug = require('debug')
78
const log = debug('cli:object')
89
log.error = debug('cli:object:error')
910

1011
function appendData (key, data) {
11-
utils.getIPFS((err, ipfs) => {
12+
waterfall([
13+
(cb) => utils.getIPFS(cb),
14+
(ipfs, cb) => ipfs.object.patch.appendData(key, data, {enc: 'base58'}, cb),
15+
(node, cb) => node.toJSON(cb)
16+
], (err, node) => {
1217
if (err) {
1318
throw err
1419
}
1520

16-
ipfs.object.patch.appendData(key, data, {enc: 'base58'}, (err, node) => {
17-
if (err) {
18-
throw err
19-
}
20-
21-
console.log(node.toJSON().Hash)
22-
})
21+
console.log(node.Hash)
2322
})
2423
}
2524

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

3+
const waterfall = require('run-waterfall')
34
const DAGLink = require('ipfs-merkle-dag').DAGLink
45
const utils = require('../../../utils')
56
const debug = require('debug')
@@ -14,20 +15,18 @@ module.exports = {
1415
builder: {},
1516

1617
handler (argv) {
17-
utils.getIPFS((err, ipfs) => {
18+
const dLink = new DAGLink(argv.link)
19+
20+
waterfall([
21+
(cb) => utils.getIPFS(cb),
22+
(ipfs, cb) => ipfs.object.patch.rmLink(argv.root, dLink, {enc: 'base58'}, cb),
23+
(node, cb) => node.toJSON(cb)
24+
], (err, node) => {
1825
if (err) {
1926
throw err
2027
}
2128

22-
const dLink = new DAGLink(argv.link)
23-
24-
ipfs.object.patch.rmLink(argv.root, dLink, {enc: 'base58'}, (err, node) => {
25-
if (err) {
26-
throw err
27-
}
28-
29-
console.log(node.toJSON().Hash)
30-
})
29+
console.log(node.Hash)
3130
})
3231
}
3332
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
const utils = require('../../../utils')
44
const bl = require('bl')
55
const fs = require('fs')
6+
const waterfall = require('run-waterfall')
67
const debug = require('debug')
78
const log = debug('cli:object')
89
log.error = debug('cli:object:error')
910

1011
function parseAndAddNode (key, data) {
11-
utils.getIPFS((err, ipfs) => {
12+
waterfall([
13+
(cb) => utils.getIPFS(cb),
14+
(ipfs, cb) => ipfs.object.patch.setData(key, data, {enc: 'base58'}, cb),
15+
(node, cb) => node.toJSON(cb)
16+
], (err, node) => {
1217
if (err) {
1318
throw err
1419
}
1520

16-
ipfs.object.patch.setData(key, data, {enc: 'base58'}, (err, node) => {
17-
if (err) {
18-
throw err
19-
}
20-
21-
console.log(node.toJSON().Hash)
22-
})
21+
console.log(node.Hash)
2322
})
2423
}
2524

src/cli/commands/object/put.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
const utils = require('../../utils')
44
const bl = require('bl')
55
const fs = require('fs')
6+
const waterfall = require('run-waterfall')
67
const debug = require('debug')
78
const log = debug('cli:object')
89
log.error = debug('cli:object:error')
910

1011
function putNode (buf, enc) {
11-
utils.getIPFS((err, ipfs) => {
12+
waterfall([
13+
(cb) => utils.getIPFS(cb),
14+
(ipfs, cb) => ipfs.object.put(buf, {enc}, cb),
15+
(node, cb) => node.toJSON(cb)
16+
], (err, node) => {
1217
if (err) {
1318
throw err
1419
}
1520

16-
ipfs.object.put(buf, {enc}, (err, node) => {
17-
if (err) {
18-
throw err
19-
}
20-
21-
console.log('added', node.toJSON().Hash)
22-
})
21+
console.log('added', node.Hash)
2322
})
2423
}
2524

src/core/components/files.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const pull = require('pull-stream')
1111
const sort = require('pull-sort')
1212
const toStream = require('pull-stream-to-stream')
1313
const toPull = require('stream-to-pull-stream')
14+
const waterfall = require('run-waterfall')
1415

1516
module.exports = function files (self) {
1617
const createAddPullStream = () => {
@@ -96,15 +97,20 @@ module.exports = function files (self) {
9697

9798
function prepareFile (self, file, cb) {
9899
const bs58mh = multihashes.toB58String(file.multihash)
99-
self.object.get(file.multihash, (err, node) => {
100-
if (err) return cb(err)
101100

102-
cb(null, {
103-
path: file.path || bs58mh,
104-
hash: bs58mh,
105-
size: node.size()
101+
waterfall([
102+
(cb) => self.object.get(file.multihash, cb),
103+
(node, cb) => node.size((err, size) => {
104+
if (err) {
105+
return cb(err)
106+
}
107+
cb(null, {
108+
path: file.path || bs58mh,
109+
hash: bs58mh,
110+
size: size
111+
})
106112
})
107-
})
113+
], cb)
108114
}
109115

110116
function normalizeContent (content) {

src/core/components/init-assets.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict'
2+
3+
const BlockService = require('ipfs-block-service')
4+
const DagService = require('ipfs-merkle-dag').DAGService
5+
const path = require('path')
6+
const fs = require('fs')
7+
const glob = require('glob')
8+
const importer = require('ipfs-unixfs-engine').importer
9+
const pull = require('pull-stream')
10+
const file = require('pull-file')
11+
const mh = require('multihashes')
12+
13+
// Add the default assets to the repo.
14+
module.exports = function addDefaultAssets (self, log, callback) {
15+
const blocks = new BlockService(self._repo)
16+
const dag = new DagService(blocks)
17+
18+
const initDocsPath = path.join(__dirname, '../../init-files/init-docs')
19+
const index = __dirname.lastIndexOf('/')
20+
21+
pull(
22+
pull.values([initDocsPath]),
23+
pull.asyncMap((val, cb) => {
24+
glob(path.join(val, '/**/*'), cb)
25+
}),
26+
pull.flatten(),
27+
pull.map((element) => {
28+
const addPath = element.substring(index + 1, element.length)
29+
if (fs.statSync(element).isDirectory()) return
30+
31+
return {
32+
path: addPath,
33+
content: file(element)
34+
}
35+
}),
36+
pull.filter(Boolean),
37+
importer(dag),
38+
pull.through((el) => {
39+
if (el.path === 'files/init-docs/docs') {
40+
const hash = mh.toB58String(el.multihash)
41+
log('to get started, enter:')
42+
log()
43+
log(`\t jsipfs files cat /ipfs/${hash}/readme`)
44+
log()
45+
}
46+
}),
47+
pull.collect((err) => {
48+
if (err) {
49+
return callback(err)
50+
}
51+
52+
callback(null, true)
53+
})
54+
)
55+
}

0 commit comments

Comments
 (0)