Skip to content

Commit 4ce0ba2

Browse files
committed
add cli fix, cat cli fix, alias, interface-core tests
1 parent 761f2c9 commit 4ce0ba2

File tree

6 files changed

+67
-101
lines changed

6 files changed

+67
-101
lines changed

src/cli/commands/files/add.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,41 @@ module.exports = Command.extend({
5959
if (err) {
6060
throw err
6161
}
62-
const i = ipfs.files.add()
63-
var filePair
64-
i.on('data', (file) => {
65-
console.log('added', bs58.encode(file.multihash).toString(), file.path)
66-
})
67-
i.once('end', () => {
68-
return
69-
})
70-
if (res.length !== 0) {
71-
const index = inPath.lastIndexOf('/')
72-
parallelLimit(res.map((element) => (callback) => {
73-
if (!fs.statSync(element).isDirectory()) {
74-
i.write({
75-
path: element.substring(index + 1, element.length),
76-
stream: fs.createReadStream(element)
77-
})
78-
}
79-
callback()
80-
}), 10, (err) => {
81-
if (err) {
82-
throw err
83-
}
84-
i.end()
62+
ipfs.files.add((err, i) => {
63+
if (err) {
64+
throw err
65+
}
66+
var filePair
67+
i.on('data', (file) => {
68+
console.log('added', bs58.encode(file.multihash).toString(), file.path)
8569
})
86-
} else {
87-
rs = fs.createReadStream(inPath)
88-
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
89-
filePair = {path: inPath, stream: rs}
90-
i.write(filePair)
91-
i.end()
92-
}
70+
i.once('end', () => {
71+
return
72+
})
73+
if (res.length !== 0) {
74+
const index = inPath.lastIndexOf('/')
75+
parallelLimit(res.map((element) => (callback) => {
76+
if (!fs.statSync(element).isDirectory()) {
77+
i.write({
78+
path: element.substring(index + 1, element.length),
79+
stream: fs.createReadStream(element)
80+
})
81+
}
82+
callback()
83+
}), 10, (err) => {
84+
if (err) {
85+
throw err
86+
}
87+
i.end()
88+
})
89+
} else {
90+
rs = fs.createReadStream(inPath)
91+
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
92+
filePair = {path: inPath, stream: rs}
93+
i.write(filePair)
94+
i.end()
95+
}
96+
})
9397
})
9498
})
9599
}

src/cli/commands/files/cat.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ module.exports = Command.extend({
3131
})
3232
return
3333
}
34-
ipfs.files.cat(path, (err, res) => {
34+
ipfs.files.cat(path, (err, file) => {
3535
if (err) {
3636
throw (err)
3737
}
38-
res.on('data', (data) => {
39-
data.stream.pipe(process.stdout)
40-
})
38+
file.pipe(process.stdout)
4139
})
4240
})
4341
}

src/core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ function IPFS (repoInstance) {
5757
this.object = object(this)
5858
this.libp2p = libp2p(this)
5959
this.files = files(this)
60+
this.cat = files(this).cat // Alias for js-ipfs-api cat
6061
this.bitswap = bitswap(this)
6162
}

src/core/ipfs/files.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
const Importer = require('ipfs-unixfs-engine').importer
44
const Exporter = require('ipfs-unixfs-engine').exporter
55
const UnixFS = require('ipfs-unixfs')
6+
const promisify = require('promisify-es6')
67

78
module.exports = function files (self) {
89
return {
9-
add: (arr, callback) => {
10+
add: promisify((arr, callback) => {
1011
if (typeof arr === 'function') {
1112
callback = arr
1213
arr = undefined
@@ -15,7 +16,7 @@ module.exports = function files (self) {
1516
callback = function noop () {}
1617
}
1718
if (arr === undefined) {
18-
return new Importer(self._dagS)
19+
callback(null, new Importer(self._dagS))
1920
}
2021

2122
const i = new Importer(self._dagS)
@@ -34,8 +35,13 @@ module.exports = function files (self) {
3435
})
3536

3637
i.end()
37-
},
38-
cat: (hash, callback) => {
38+
}),
39+
40+
cat: promisify((hash, callback) => {
41+
if (typeof hash === 'function') {
42+
callback = hash
43+
return callback('You must supply a multihash', null)
44+
}
3945
self._dagS.get(hash, (err, fetchedNode) => {
4046
if (err) {
4147
return callback(err, null)
@@ -45,13 +51,16 @@ module.exports = function files (self) {
4551
callback('This dag node is a directory', null)
4652
} else {
4753
const exportStream = Exporter(hash, self._dagS)
48-
callback(null, exportStream)
54+
exportStream.once('data', (object) => {
55+
callback(null, object.stream)
56+
})
4957
}
5058
})
51-
},
52-
get: (hash, callback) => {
59+
}),
60+
61+
get: promisify((hash, callback) => {
5362
var exportFile = Exporter(hash, self._dagS)
5463
callback(null, exportFile)
55-
}
64+
})
5665
}
5766
}

src/http-api/resources/files.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ exports.cat = {
4242
Code: 0
4343
}).code(500)
4444
}
45-
stream.on('data', (data) => {
46-
return reply(data.stream)
47-
})
45+
return reply(stream)
4846
})
4947
}
5048
}

test/core-tests/test-files.js

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,20 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const bl = require('bl')
5-
const expect = require('chai').expect
6-
const Readable = require('stream').Readable
7-
const bs58 = require('bs58')
4+
const test = require('interface-ipfs-core')
85

96
const IPFS = require('../../src/core')
107

11-
describe('files', () => {
12-
let ipfs
13-
14-
before((done) => {
15-
ipfs = new IPFS(require('./repo-path'))
16-
ipfs.load(done)
17-
})
18-
19-
it('add', (done) => {
20-
const buffered = new Buffer('some data')
21-
const rs = new Readable()
22-
rs.push(buffered)
23-
rs.push(null)
24-
const arr = []
25-
const filePair = {path: 'data.txt', stream: rs}
26-
arr.push(filePair)
27-
ipfs.files.add(arr, (err, res) => {
28-
expect(err).to.not.exist
29-
expect(res[0].path).to.equal('data.txt')
30-
expect(res[0].size).to.equal(17)
31-
expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS')
32-
done()
8+
const common = {
9+
setup: function (cb) {
10+
const ipfs = new IPFS(require('./repo-path'))
11+
ipfs.load(() => {
12+
cb(null, ipfs)
3313
})
34-
})
14+
},
15+
teardown: function (cb) {
16+
cb()
17+
}
18+
}
3519

36-
it('cat', (done) => {
37-
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
38-
ipfs.files.cat(hash, (err, res) => {
39-
expect(err).to.not.exist
40-
res.on('data', (data) => {
41-
data.stream.pipe(bl((err, bldata) => {
42-
expect(err).to.not.exist
43-
expect(bldata.toString()).to.equal('hello world\n')
44-
done()
45-
}))
46-
})
47-
})
48-
})
49-
50-
it('get', (done) => {
51-
// TODO create non-trival get test
52-
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
53-
ipfs.files.get(hash, (err, res) => {
54-
expect(err).to.not.exist
55-
res.on('data', (data) => {
56-
data.stream.pipe(bl((err, bldata) => {
57-
expect(err).to.not.exist
58-
expect(bldata.toString()).to.equal('hello world\n')
59-
done()
60-
}))
61-
})
62-
})
63-
})
64-
})
20+
test.files(common)

0 commit comments

Comments
 (0)