Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 2b4fbf5

Browse files
author
Pedro Santos
committed
chore: code review changes
1 parent c1172c2 commit 2b4fbf5

27 files changed

+328
-471
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
3838
"dependencies": {
3939
"async": "^2.6.2",
40-
"bl": "^3.0.0",
4140
"bs58": "^4.0.1",
4241
"chai": "^4.2.0",
4342
"chai-as-promised": "^7.1.1",
@@ -46,6 +45,7 @@
4645
"delay": "^4.3.0",
4746
"dirty-chai": "^2.0.1",
4847
"es6-promisify": "^6.0.2",
48+
"get-stream": "^5.1.0",
4949
"hat": "0.0.3",
5050
"ipfs-block": "~0.8.0",
5151
"ipfs-unixfs": "~0.1.16",
@@ -68,6 +68,7 @@
6868
"peer-id": "~0.12.0",
6969
"peer-info": "~0.15.0",
7070
"pull-stream": "^3.6.14",
71+
"pull-to-promise": "^1.0.1",
7172
"pump": "^3.0.0",
7273
"readable-stream": "^3.1.1",
7374
"streaming-iterables": "^4.1.0",

src/block/get.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ module.exports = (common, options) => {
8383
})
8484

8585
it('should return an error for an invalid CID', () => {
86-
return expect(ipfs.block.get('invalid')).to.eventually.be.rejected.and.be.an.instanceOf(Error)
86+
return expect(ipfs.block.get('invalid')).to.eventually.be.rejected
87+
.and.be.an.instanceOf(Error)
88+
.and.have.property('message')
89+
.that.include('Non-base58 character')
8790
})
8891
})
8992
}

src/files-mfs/ls-pull-stream.js

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
const hat = require('hat')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const pull = require('pull-stream/pull')
7-
const onEnd = require('pull-stream/sinks/on-end')
8-
const collect = require('pull-stream/sinks/collect')
6+
const pullToPromise = require('pull-to-promise')
97

108
/** @typedef { import("ipfsd-ctl").TestsInterface } TestsInterface */
119
/**
@@ -28,16 +26,10 @@ module.exports = (common, options) => {
2826
it('should not ls not found file/dir, expect error', () => {
2927
const testDir = `/test-${hat()}`
3028

31-
return new Promise((resolve) => {
32-
pull(
33-
ipfs.files.lsPullStream(`${testDir}/404`),
34-
onEnd((err) => {
35-
expect(err).to.exist()
36-
expect(err.message).to.include('does not exist')
37-
resolve()
38-
})
39-
)
40-
})
29+
return expect(pullToPromise.any(ipfs.files.lsPullStream(`${testDir}/404`))).to.eventually.be.rejected
30+
.and.be.an.instanceOf(Error)
31+
.and.have.property('message')
32+
.that.include('does not exist')
4133
})
4234

4335
it('should ls directory', async () => {
@@ -46,19 +38,12 @@ module.exports = (common, options) => {
4638
await ipfs.files.mkdir(`${testDir}/lv1`, { p: true })
4739
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })
4840

49-
await new Promise((resolve) => {
50-
pull(
51-
ipfs.files.lsPullStream(testDir),
52-
collect((err, entries) => {
53-
expect(err).to.not.exist()
54-
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
55-
{ name: 'b', type: 0, size: 0, hash: '' },
56-
{ name: 'lv1', type: 0, size: 0, hash: '' }
57-
])
58-
resolve()
59-
})
60-
)
61-
})
41+
const entries = await pullToPromise.any(ipfs.files.lsPullStream(testDir))
42+
43+
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
44+
{ name: 'b', type: 0, size: 0, hash: '' },
45+
{ name: 'lv1', type: 0, size: 0, hash: '' }
46+
])
6247
})
6348

6449
it('should ls directory with long option', async () => {
@@ -67,29 +52,22 @@ module.exports = (common, options) => {
6752
await ipfs.files.mkdir(`${testDir}/lv1`, { p: true })
6853
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })
6954

70-
await new Promise((resolve) => {
71-
pull(
72-
ipfs.files.lsPullStream(testDir, { long: true }),
73-
collect((err, entries) => {
74-
expect(err).to.not.exist()
75-
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
76-
{
77-
name: 'b',
78-
type: 0,
79-
size: 13,
80-
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T'
81-
},
82-
{
83-
name: 'lv1',
84-
type: 1,
85-
size: 0,
86-
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
87-
}
88-
])
89-
resolve()
90-
})
91-
)
92-
})
55+
const entries = await pullToPromise.any(ipfs.files.lsPullStream(testDir, { long: true }))
56+
57+
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
58+
{
59+
name: 'b',
60+
type: 0,
61+
size: 13,
62+
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T'
63+
},
64+
{
65+
name: 'lv1',
66+
type: 1,
67+
size: 0,
68+
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
69+
}
70+
])
9371
})
9472
})
9573
}

src/files-mfs/ls-readable-stream.js

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const hat = require('hat')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
const getStream = require('get-stream')
67

78
/** @typedef { import("ipfsd-ctl").TestsInterface } TestsInterface */
89
/**
@@ -24,16 +25,12 @@ module.exports = (common, options) => {
2425

2526
it('should not ls not found file/dir, expect error', () => {
2627
const testDir = `/test-${hat()}`
27-
2828
const stream = ipfs.files.lsReadableStream(`${testDir}/404`)
2929

30-
return new Promise((resolve) => {
31-
stream.once('error', (err) => {
32-
expect(err).to.exist()
33-
expect(err.message).to.include('does not exist')
34-
resolve()
35-
})
36-
})
30+
return expect(getStream(stream)).to.eventually.be.rejected
31+
.and.be.an.instanceOf(Error)
32+
.and.have.property('message')
33+
.that.include('does not exist')
3734
})
3835

3936
it('should ls directory', async () => {
@@ -43,19 +40,13 @@ module.exports = (common, options) => {
4340
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })
4441

4542
const stream = ipfs.files.lsReadableStream(testDir)
46-
const entries = []
47-
48-
stream.on('data', entry => entries.push(entry))
49-
50-
await new Promise((resolve) => {
51-
stream.once('end', () => {
52-
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
53-
{ name: 'b', type: 0, size: 0, hash: '' },
54-
{ name: 'lv1', type: 0, size: 0, hash: '' }
55-
])
56-
resolve()
57-
})
58-
})
43+
44+
const entries = await getStream.array(stream)
45+
46+
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
47+
{ name: 'b', type: 0, size: 0, hash: '' },
48+
{ name: 'lv1', type: 0, size: 0, hash: '' }
49+
])
5950
})
6051

6152
it('should ls directory with long option', async () => {
@@ -65,29 +56,22 @@ module.exports = (common, options) => {
6556
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })
6657

6758
const stream = ipfs.files.lsReadableStream(testDir, { long: true })
68-
const entries = []
69-
70-
stream.on('data', entry => entries.push(entry))
71-
72-
await new Promise((resolve) => {
73-
stream.once('end', () => {
74-
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
75-
{
76-
name: 'b',
77-
type: 0,
78-
size: 13,
79-
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T'
80-
},
81-
{
82-
name: 'lv1',
83-
type: 1,
84-
size: 0,
85-
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
86-
}
87-
])
88-
resolve()
89-
})
90-
})
59+
const entries = await getStream(stream)
60+
61+
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
62+
{
63+
name: 'b',
64+
type: 0,
65+
size: 13,
66+
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T'
67+
},
68+
{
69+
name: 'lv1',
70+
type: 1,
71+
size: 0,
72+
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
73+
}
74+
])
9175
})
9276
})
9377
}

src/files-mfs/read-pull-stream.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
const hat = require('hat')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const pull = require('pull-stream/pull')
7-
const collect = require('pull-stream/sinks/collect')
6+
const pullToPromise = require('pull-to-promise')
87

98
/** @typedef { import("ipfsd-ctl").TestsInterface } TestsInterface */
109
/**
@@ -27,16 +26,10 @@ module.exports = (common, options) => {
2726
it('should not read not found, expect error', () => {
2827
const testDir = `/test-${hat()}`
2928

30-
return new Promise((resolve) => {
31-
pull(
32-
ipfs.files.readPullStream(`${testDir}/404`),
33-
collect((err) => {
34-
expect(err).to.exist()
35-
expect(err.message).to.contain('does not exist')
36-
resolve()
37-
})
38-
)
39-
})
29+
return expect(pullToPromise.any(ipfs.files.readPullStream(`${testDir}/404`))).to.eventually.be.rejected
30+
.and.be.an.instanceOf(Error)
31+
.and.have.property('message')
32+
.that.include('does not exist')
4033
})
4134

4235
it('should read file', async () => {
@@ -45,16 +38,9 @@ module.exports = (common, options) => {
4538
await ipfs.files.mkdir(testDir)
4639
await ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true })
4740

48-
await new Promise((resolve, reject) => {
49-
pull(
50-
ipfs.files.readPullStream(`${testDir}/a`),
51-
collect((err, bufs) => {
52-
expect(err).to.not.exist()
53-
expect(bufs).to.eql([Buffer.from('Hello, world!')])
54-
resolve()
55-
})
56-
)
57-
})
41+
const bufs = await pullToPromise.any(ipfs.files.readPullStream(`${testDir}/a`))
42+
43+
expect(bufs).to.eql([Buffer.from('Hello, world!')])
5844
})
5945
})
6046
}

src/files-mfs/read-readable-stream.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const hat = require('hat')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const bl = require('bl')
6+
const getStream = require('get-stream')
77

88
/** @typedef { import("ipfsd-ctl").TestsInterface } TestsInterface */
99
/**
@@ -25,17 +25,12 @@ module.exports = (common, options) => {
2525

2626
it('should not read not found, expect error', () => {
2727
const testDir = `/test-${hat()}`
28-
2928
const stream = ipfs.files.readReadableStream(`${testDir}/404`)
30-
stream.on('data', () => {})
31-
32-
return new Promise((resolve) => {
33-
stream.once('error', (err) => {
34-
expect(err).to.exist()
35-
expect(err.message).to.contain('does not exist')
36-
resolve()
37-
})
38-
})
29+
30+
return expect(getStream(stream)).to.eventually.be.rejected
31+
.and.be.an.instanceOf(Error)
32+
.and.have.property('message')
33+
.that.include('does not exist')
3934
})
4035

4136
it('should read file', async () => {
@@ -46,13 +41,8 @@ module.exports = (common, options) => {
4641

4742
const stream = ipfs.files.readReadableStream(`${testDir}/a`)
4843

49-
await new Promise((resolve, reject) => {
50-
stream.pipe(bl((err, buf) => {
51-
expect(err).to.not.exist()
52-
expect(buf).to.eql(Buffer.from('Hello, world!'))
53-
resolve()
54-
}))
55-
})
44+
const buf = await getStream(stream)
45+
expect(buf).to.eql('Hello, world!')
5646
})
5747
})
5848
}

0 commit comments

Comments
 (0)