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

Commit aae2259

Browse files
code review fixes
1 parent 8df6248 commit aae2259

File tree

6 files changed

+46
-72
lines changed

6 files changed

+46
-72
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"homepage": "https://github.com/ipfs/js-ipfs#readme",
3939
"devDependencies": {
4040
"aegir": "^3.0.1",
41-
"async": "^1.5.2",
41+
"async": "^2.0.0-rc.4",
4242
"buffer-loader": "0.0.1",
4343
"chai": "^3.5.0",
4444
"expose-loader": "^0.7.1",
@@ -64,7 +64,7 @@
6464
"hapi": "^13.3.0",
6565
"ipfs-api": "^3.0.2",
6666
"ipfs-block": "^0.3.0",
67-
"ipfs-block-service": "^0.3.0",
67+
"ipfs-block-service": "^0.4.0",
6868
"ipfs-merkle-dag": "^0.5.0",
6969
"ipfs-multipart": "^0.1.0",
7070
"ipfs-repo": "^0.8.0",
@@ -79,7 +79,7 @@
7979
"peer-book": "0.1.0",
8080
"peer-id": "^0.6.6",
8181
"peer-info": "^0.6.2",
82-
"readable-stream": "^1.1.13",
82+
"readable-stream": "1.1.13",
8383
"ronin": "^0.3.11",
8484
"temp": "^0.8.3"
8585
},
@@ -115,4 +115,4 @@
115115
"kumavis <[email protected]>",
116116
"nginnever <[email protected]>"
117117
]
118-
}
118+
}

src/cli/commands/files/add.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function checkPath (inPath, recursive) {
3131
}
3232
if (inPath === '.' && recursive === true) {
3333
inPath = process.cwd()
34-
return inPath
3534
} else if (inPath === '.' && recursive === false) {
3635
s = fs.statSync(process.cwd())
3736
if (s.isDirectory()) {
@@ -76,15 +75,13 @@ module.exports = Command.extend({
7675
if (res.length !== 0) {
7776
const index = inPath.lastIndexOf('/')
7877
async.eachLimit(res, 10, (element, callback) => {
79-
const addPath = element.substring(index + 1, element.length)
80-
if (fs.statSync(element).isDirectory()) {
81-
callback()
82-
} else {
83-
rs = fs.createReadStream(element)
84-
filePair = {path: addPath, stream: rs}
85-
i.write(filePair)
86-
callback()
78+
if (!fs.statSync(element).isDirectory()) {
79+
i.write({
80+
path: element.substring(index + 1, element.length),
81+
stream: fs.createReadStream(element)
82+
})
8783
}
84+
callback()
8885
}, (err) => {
8986
if (err) {
9087
throw err

src/cli/commands/files/get.js

+28-44
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ log.error = debug('cli:files:error')
88
var fs = require('fs')
99
const path = require('path')
1010
const pathExists = require('path-exists')
11-
const async = require('async')
1211

1312
function checkArgs (hash, outPath) {
1413
if (!hash) {
@@ -30,60 +29,45 @@ function checkArgs (hash, outPath) {
3029
}
3130
}
3231

33-
function getFiles (result, dir) {
34-
var filePath
35-
result.on('file', (file) => {
32+
function ensureDir (dir, cb) {
33+
pathExists(dir)
34+
.then((exists) => {
35+
if (!exists) {
36+
fs.mkdir(dir, cb)
37+
} else {
38+
cb()
39+
}
40+
})
41+
.catch(cb)
42+
}
43+
44+
function fileHandler (result, dir) {
45+
return function onFile (file) {
3646
// Check to see if the result is in a directory
3747
if (file.path.lastIndexOf('/') === -1) {
38-
filePath = file.path
48+
const dirPath = path.join(dir, file.path)
3949
// Check to see if the result is a directory
4050
if (file.dir === false) {
41-
const ws = fs.createWriteStream(path.join(dir, file.path))
42-
file.stream.pipe(ws)
51+
file.stream.pipe(fs.createWriteStream(dirPath))
4352
} else {
44-
// Check to see if the directory has already been created
45-
pathExists(path.join(dir, file.path)).then(exists => {
46-
if (!exists) {
47-
fs.mkdir(path.join(dir, file.path), (err) => {
48-
if (err) {
49-
throw err
50-
}
51-
})
53+
ensureDir(dirPath, (err) => {
54+
if (err) {
55+
throw err
5256
}
5357
})
5458
}
5559
} else {
56-
// Check to see if the directory has already been created
57-
filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
58-
pathExists(path.join(dir, filePath)).then(exists => {
59-
// Create a directory for the incoming files
60-
if (!exists) {
61-
async.waterfall([
62-
(cb) => {
63-
fs.mkdir(path.join(dir, filePath), (err) => {
64-
if (err) {
65-
cb(err)
66-
}
67-
cb(null)
68-
})
69-
},
70-
(cb) => {
71-
const ws = fs.createWriteStream(path.join(dir, file.path))
72-
file.stream.pipe(ws)
73-
cb(null)
74-
}
75-
], (err) => {
76-
if (err) {
77-
throw err
78-
}
79-
})
60+
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
61+
const dirPath = path.join(dir, filePath)
62+
ensureDir(dirPath, (err) => {
63+
if (err) {
64+
throw err
8065
}
81-
// Just write the file
82-
const ws = fs.createWriteStream(path.join(dir, file.path))
83-
file.stream.pipe(ws)
66+
67+
file.stream.pipe(fs.createWriteStream(dirPath))
8468
})
8569
}
86-
})
70+
}
8771
}
8872

8973
module.exports = Command.extend({
@@ -100,7 +84,7 @@ module.exports = Command.extend({
10084
if (err) {
10185
throw err
10286
}
103-
getFiles(result, dir)
87+
result.on('file', fileHandler(result, dir))
10488
})
10589
})
10690
}

src/core/ipfs/init.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const glob = require('glob')
88
const async = require('async')
99
const Readable = require('stream').Readable
1010
const fs = require('fs')
11+
const Importer = require('ipfs-unixfs-engine').importer
1112

1213
module.exports = function init (self) {
1314
return (opts, callback) => {
@@ -71,7 +72,6 @@ module.exports = function init (self) {
7172
return doneImport(null)
7273
}
7374

74-
const Importer = require('ipfs-unixfs-engine').importer
7575
const blocks = new BlockService(self._repo)
7676
const dag = new DagService(blocks)
7777

@@ -87,23 +87,19 @@ module.exports = function init (self) {
8787
const index = __dirname.lastIndexOf('/')
8888
async.eachLimit(res, 10, (element, callback) => {
8989
const addPath = element.substring(index + 1, element.length)
90-
if (fs.statSync(element).isDirectory()) {
91-
callback()
92-
} else {
93-
const buffered = fs.readFileSync(element)
90+
if (!fs.statSync(element).isDirectory()) {
9491
const rs = new Readable()
95-
rs.push(buffered)
92+
rs.push(fs.readFileSync(element))
9693
rs.push(null)
9794
const filePair = {path: addPath, stream: rs}
9895
i.write(filePair)
99-
callback()
10096
}
97+
callback()
10198
}, (err) => {
10299
if (err) {
103100
throw err
104101
}
105102
i.end()
106-
return
107103
})
108104
})
109105

@@ -112,7 +108,9 @@ module.exports = function init (self) {
112108
})
113109

114110
function doneImport (err, stat) {
115-
if (err) { return callback(err) }
111+
if (err) {
112+
return callback(err)
113+
}
116114

117115
// All finished!
118116
callback(null, true)

src/core/ipfs/libp2p.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ module.exports = function libp2p (self) {
2020
})
2121
},
2222
stop: (callback) => {
23-
try {
24-
self._libp2pNode.swarm.close(callback)
25-
} catch (err) {
26-
console.log('It is fine :)')
27-
}
23+
self._libp2pNode.swarm.close(callback)
2824
},
2925
swarm: {
3026
peers: (callback) => {

test/cli-tests/test-block.js

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ describe('block', () => {
7171
describe('api running', () => {
7272
let httpAPI
7373
before((done) => {
74-
console.log('repoPath ->', repoPath)
7574
httpAPI = new HttpAPI(repoPath)
7675
httpAPI.start((err) => {
7776
expect(err).to.not.exist

0 commit comments

Comments
 (0)