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

chore: async await refactor #562

Merged
merged 45 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
2fb718c
chore: bitswap async/await refactor
Nov 20, 2019
14aa336
chore: block async/await refactor
Nov 20, 2019
a22f248
chore: bootstrap async/await refactor
Nov 20, 2019
70f8bc7
chore: config async/await refactor
Nov 20, 2019
fd72f40
chore: dag async/await refactor
Nov 20, 2019
a2c6b12
chore: dht async/await refactor
Nov 20, 2019
8bc5fc9
chore: files-mfs async/await refactor
Nov 20, 2019
2154ebe
chore: files-regular async/await refactor
Nov 20, 2019
7e4eabc
chore: key async/await refactor
Nov 20, 2019
1f810f9
chore: miscellaneous async/await refactor
Nov 20, 2019
7ba7a2d
chore: name-pubsub async/await refactor
Nov 20, 2019
6a4c1bb
chore: name async/await refactor
Nov 20, 2019
3910b68
chore: object async/await refactor
Nov 20, 2019
f8caf5c
chore: pin async/await refactor
Nov 20, 2019
b49666b
chore: ping async/await refactor
Nov 20, 2019
3c9610c
chore: pubsub async/await refactor
Nov 20, 2019
08c455d
chore: repo async/await refactor
Nov 20, 2019
143a2c4
chore: stats async/await refactor
Nov 20, 2019
6d4d5bb
chore: swarm async/await refactor
Nov 20, 2019
abe108d
chore: remove unnecessary util file
Nov 20, 2019
e242e09
chore: update dependencies
Nov 20, 2019
3fd2a8b
refactor: remove async dep and unnecessary utils
Nov 22, 2019
79e4e6b
refactor: bitswap before and after methods to async syntax
Nov 22, 2019
571b35b
refactor: block before and after methods to async syntax
Nov 22, 2019
20e842b
refactor: bootstrap before and after methods to async syntax
Nov 22, 2019
444c6a5
refactor: config before and after methods to async syntax
Nov 22, 2019
ba18c86
refactor: dag before and after methods to async syntax
Nov 22, 2019
bc7dcef
refactor: dht before and after methods to async syntax
Nov 22, 2019
50eabf6
refactor: files-mfs before and after methods to async syntax
Nov 22, 2019
4172720
refactor: files-regular before and after methods to async syntax
Nov 22, 2019
dd446e8
refactor: key before and after methods to async syntax
Nov 22, 2019
1cb16c8
refactor: miscellaneous before and after methods to async syntax
Nov 22, 2019
3f95aab
refactor: name before and after methods to async syntax
Nov 22, 2019
03b5d6f
refactor: name-pubsub before and after methods to async syntax
Nov 22, 2019
187c89e
refactor: object before and after methods to async syntax
Nov 22, 2019
cb07f73
refactor: pin before and after methods to async syntax
Nov 22, 2019
9953c7f
refactor: ping before and after methods to async syntax
Nov 22, 2019
4479df9
refactor: pubsub before and after methods to async syntax
Nov 22, 2019
25a99bf
refactor: repo before and after methods to async syntax
Nov 22, 2019
f00b03d
refactor: stats before and after methods to async syntax
Nov 22, 2019
514ffed
chore: remove 'only' from files-mfs/cp
Nov 22, 2019
a0c1e6a
chore: uncomment bootstrap rm test assertion
Nov 22, 2019
f3a8224
refactor: name resolve test
Nov 22, 2019
2720e57
fix: increase dht findProvs test timeout
Nov 24, 2019
2aaff5e
fix: miscellaneous stop test
Nov 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.1",
"get-stream": "^5.1.0",
"hat": "0.0.3",
"ipfs-block": "~0.8.0",
"ipfs-unixfs": "~0.1.16",
Expand All @@ -61,9 +62,15 @@
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.6.0",
"peer-id": "~0.12.0",
"p-each-series": "^2.1.0",
"p-map-series": "^2.1.0",
"p-timeout": "^3.2.0",
"p-times": "^2.1.0",
"p-whilst": "^2.1.0",
"peer-id": "~0.13.5",
"peer-info": "~0.15.0",
"pull-stream": "^3.6.11",
"pull-to-promise": "^1.0.1",
"pump": "^3.0.0",
"readable-stream": "^3.1.1",
"streaming-iterables": "^4.1.0",
Expand Down
16 changes: 4 additions & 12 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,9 @@ module.exports = (createCommon, options) => {

after((done) => common.teardown(done))

it('should get bitswap stats', (done) => {
ipfs.bitswap.stat((err, res) => {
expectIsBitswap(err, res)
done()
})
})

it('should get bitswap stats (promised)', () => {
return ipfs.bitswap.stat().then((res) => {
expectIsBitswap(null, res)
})
it('should get bitswap stats', async () => {
const res = await ipfs.bitswap.stat()
expectIsBitswap(null, res)
})

it('should not get bitswap stats when offline', function (done) {
Expand All @@ -52,7 +44,7 @@ module.exports = (createCommon, options) => {
(node, cb) => node.stop((err) => cb(err, node))
], (err, node) => {
expect(err).to.not.exist()
node.bitswap.wantlist((err) => {
node.bitswap.stat((err) => {
expect(err).to.exist()
done()
})
Expand Down
35 changes: 12 additions & 23 deletions src/bitswap/utils.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
'use strict'

const until = require('async/until')
const pWhilst = require('p-whilst')

function waitForWantlistKey (ipfs, key, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}

opts = opts || {}
opts.timeout = opts.timeout || 1000
function waitForWantlistKey (ipfs, key, opts = {}) {
opts.timeout = opts.timeout || 10000

let list = { Keys: [] }
let timedOut = false

setTimeout(() => { timedOut = true }, opts.timeout)
const start = Date.now()
const test = () => !list.Keys.some(k => k['/'] === key)

const iteratee = async () => {
if (Date.now() - start > opts.timeout) {
throw new Error(`Timed out waiting for ${key} in wantlist`)
}

const test = () => timedOut ? true : list.Keys.some(k => k['/'] === key)
const iteratee = (cb) => {
ipfs.bitswap.wantlist(opts.peerId, (err, nextList) => {
if (err) return cb(err)
list = nextList
cb()
})
list = await ipfs.bitswap.wantlist(opts.peerId)
}

until(test, iteratee, (err) => {
if (err) return cb(err)
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`))
cb()
})
return pWhilst(test, iteratee)
}

module.exports.waitForWantlistKey = waitForWantlistKey
11 changes: 4 additions & 7 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@ module.exports = (createCommon, options) => {
common.teardown(done)
})

it('should get the wantlist', (done) => {
waitForWantlistKey(ipfsB, key, done)
it('should get the wantlist', () => {
return waitForWantlistKey(ipfsB, key)
})

it('should get the wantlist by peer ID for a diffreent node', (done) => {
ipfsB.id((err, info) => {
expect(err).to.not.exist()
waitForWantlistKey(ipfsA, key, { peerId: info.id }, done)
})
it('should get the wantlist by peer ID for a diffreent node', () => {
return waitForWantlistKey(ipfsA, key, { peerId: ipfsB.peerId.id })
})

it('should not get the wantlist when offline', function (done) {
Expand Down
86 changes: 33 additions & 53 deletions src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,86 +34,66 @@ module.exports = (createCommon, options) => {

after((done) => common.teardown(done))

it('should get by CID object', (done) => {
it('should get by CID object', async () => {
const cid = new CID(hash)
const block = await ipfs.block.get(cid)

ipfs.block.get(cid, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(cid.multihash)
done()
})
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(cid.multihash)
})

it('should get by CID in string', (done) => {
ipfs.block.get(multihash.toB58String(hash), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(hash)
done()
})
it('should get by CID in string', async () => {
const block = await ipfs.block.get(multihash.toB58String(hash))

expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(hash)
})

it('should get an empty block', (done) => {
ipfs.block.put(Buffer.alloc(0), {
it('should get an empty block', async () => {
const res = await ipfs.block.put(Buffer.alloc(0), {
format: 'dag-pb',
mhtype: 'sha2-256',
version: 0
}, (err, block) => {
expect(err).to.not.exist()

ipfs.block.get(block.cid, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.alloc(0))
done()
})
})

const block = await ipfs.block.get(res.cid)

expect(block.data).to.eql(Buffer.alloc(0))
})

it('should get a block added as CIDv0 with a CIDv1', done => {
it('should get a block added as CIDv0 with a CIDv1', async () => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 0 }, (err, res) => {
expect(err).to.not.exist()
const res = await ipfs.block.put(input, { version: 0 })

const cidv0 = res.cid
expect(cidv0.version).to.equal(0)
const cidv0 = res.cid
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()
const cidv1 = cidv0.toV1()

ipfs.block.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
const block = await ipfs.block.get(cidv1)
expect(block.data).to.eql(input)
})

it('should get a block added as CIDv1 with a CIDv0', done => {
it('should get a block added as CIDv1 with a CIDv0', async () => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 1 }, (err, res) => {
expect(err).to.not.exist()
const res = await ipfs.block.put(input, { version: 1 })

const cidv1 = res.cid
expect(cidv1.version).to.equal(1)
const cidv1 = res.cid
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()
const cidv0 = cidv1.toV0()

ipfs.block.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
const block = await ipfs.block.get(cidv0)
expect(block.data).to.eql(input)
})

it('should return an error for an invalid CID', () => {
return ipfs.block.get('invalid')
.then(
() => expect.fail('should have returned an error for invalid argument'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.get('invalid')).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
.and.have.property('message')
.that.include('Non-base58 character')
})
})
}
59 changes: 24 additions & 35 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,68 +31,57 @@ module.exports = (createCommon, options) => {

after((done) => common.teardown(done))

it('should put a buffer, using defaults', (done) => {
it('should put a buffer, using defaults', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = Buffer.from('blorb')

ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(blob)

expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should put a buffer, using CID', (done) => {
it('should put a buffer, using CID', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = Buffer.from('blorb')

ipfs.block.put(blob, { cid: cid }, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(blob, { cid: cid })

expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should put a buffer, using options', (done) => {
it('should put a buffer, using options', async () => {
const blob = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(blob, {
const block = await ipfs.block.put(blob, {
format: 'raw',
mhtype: 'sha2-512',
version: 1
}, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.version).to.equal(1)
expect(block.cid.codec).to.equal('raw')
expect(multihash.decode(block.cid.multihash).name).to.equal('sha2-512')
done()
})

expect(block.data).to.be.eql(blob)
expect(block.cid.version).to.equal(1)
expect(block.cid.codec).to.equal('raw')
expect(multihash.decode(block.cid.multihash).name).to.equal('sha2-512')
})

it('should put a Block instance', (done) => {
it('should put a Block instance', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const b = new Block(Buffer.from('blorb'), cid)

ipfs.block.put(b, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
done()
})
const block = await ipfs.block.put(b)

expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
})

it('should error with array of blocks', (done) => {
it('should error with array of blocks', () => {
const blob = Buffer.from('blorb')

ipfs.block.put([blob, blob], (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
return expect(ipfs.block.put([blob, blob])).to.eventually.be.rejected.and.be.an.instanceOf(Error)
})
})
}
26 changes: 9 additions & 17 deletions src/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,23 @@ module.exports = (createCommon, options) => {

after((done) => common.teardown(done))

it('should stat by CID', (done) => {
it('should stat by CID', async () => {
const cid = new CID(hash)

ipfs.block.stat(cid, (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
done()
})
const stats = await ipfs.block.stat(cid)

expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
})

it('should return error for missing argument', () => {
return ipfs.block.stat(null)
.then(
() => expect.fail('should have thrown for missing parameter'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.stat(null)).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
})

it('should return error for invalid argument', () => {
return ipfs.block.stat('invalid')
.then(
() => expect.fail('should have thrown for invalid parameter'),
(err) => expect(err).to.be.an.instanceof(Error)
)
return expect(ipfs.block.stat('invalid')).to.eventually.be.rejected
.and.be.an.instanceOf(Error)
})
})
}
Loading