Rename `range.downloaded()` to `range.done()`
MODIFY
README.md
README.md
@@ -203,7 +203,7 @@ Download a range of data.
You can await when the range has been fully downloaded by doing:
```js
-await range.downloaded()
+await range.done()
```
A range can have the following properties:

MODIFY
index.js
index.js
@@ -12,6 +12,7 @@ const Replicator = require('./lib/replicator')
const Core = require('./lib/core')
const BlockEncryption = require('./lib/block-encryption')
const Info = require('./lib/info')
+const Download = require('./lib/download')
const { ReadStream, WriteStream } = require('./lib/streams')
const { BAD_ARGUMENT, SESSION_CLOSED, SESSION_NOT_WRITABLE, SNAPSHOT_NOT_AVAILABLE } = require('./lib/errors')
@@ -628,7 +629,7 @@ module.exports = class Hypercore extends EventEmitter {
const end = this.core.tree.length
const contig = this.contiguousLength
- await this.download({ start, end, ifAvailable: true }).downloaded()
+ await this.download({ start, end, ifAvailable: true }).done()
if (!upgraded) upgraded = this.contiguousLength !== contig
}
@@ -739,21 +740,12 @@ module.exports = class Hypercore extends EventEmitter {
}
download (range) {
- const reqP = this._download(range)
+ const req = this._download(range)
// do not crash in the background...
- reqP.catch(noop)
+ req.catch(noop)
- // TODO: turn this into an actual object...
- return {
- async downloaded () {
- const req = await reqP
- return req.promise
- },
- destroy () {
- reqP.then(req => req.context && req.context.detach(req), noop)
- }
- }
+ return new Download(req)
}
async _download (range) {

ADD
lib/download.js
+module.exports = class Download {
+ constructor (req) {
+ this.req = req
+ }
+
+ async done () {
+ return (await this.req).promise
+ }
+
+ /**
+ * Deprecated. Use `range.done()`.
+ */
+ downloaded () {
+ return this.done()
+ }
+
+ destroy () {
+ this.req.then(req => req.context && req.context.detach(req), noop)
+ }
+}
+
+function noop () {}

MODIFY
test/auth.js
test/auth.js
@@ -50,7 +50,7 @@ test('multisig hypercore', async function (t) {
replicate(a, b, t)
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(b.length, 5)
t.end()
@@ -156,7 +156,7 @@ test('multisig hypercore with instance and extension', async function (t) {
t.is(a.length, 1)
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(a.length, 1)
@@ -214,7 +214,7 @@ test('proof-of-work hypercore', async function (t) {
replicate(a, b, t)
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(b.length, 5)
})
@@ -244,7 +244,7 @@ test('core using custom sign fn', async function (t) {
replicate(a, b, t)
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(b.length, 5)
t.end()

MODIFY
test/clear.js
test/clear.js
@@ -24,7 +24,7 @@ test('clear + replication', async function (t) {
replicate(a, b, t)
await a.append(['a', 'b', 'c'])
- await b.download({ start: 0, end: 3 }).downloaded()
+ await b.download({ start: 0, end: 3 }).done()
await a.clear(1)
@@ -43,7 +43,7 @@ test('clear + replication, gossip', async function (t) {
replicate(b, c, t)
await a.append(['a', 'b', 'c'])
- await b.download({ start: 0, end: 3 }).downloaded()
+ await b.download({ start: 0, end: 3 }).done()
await c.update()
await b.clear(1)

MODIFY
test/encryption.js
test/encryption.js
@@ -50,7 +50,7 @@ test('encrypted replication', async function (t) {
await t.test('through direct download', async function (t) {
const r = b.download({ start: 0, length: a.length })
- await r.downloaded()
+ await r.done()
for (let i = 0; i < 5; i++) {
t.alike(await b.get(i), await a.get(i))
@@ -75,7 +75,7 @@ test('encrypted replication', async function (t) {
await t.test('through direct download', async function (t) {
const r = b.download({ start: 0, length: a.length })
- await r.downloaded()
+ await r.done()
for (let i = 0; i < 5; i++) {
t.alike(await b.get(i), await a.core.blocks.get(i))

MODIFY
test/replicate.js
test/replicate.js
@@ -17,7 +17,7 @@ test('basic replication', async function (t) {
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(d, 5)
})
@@ -40,7 +40,7 @@ test('basic replication from fork', async function (t) {
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(d, 5)
t.is(a.fork, b.fork)
@@ -64,7 +64,7 @@ test('eager replication from bigger fork', async function (t) {
})
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(d, a.length)
t.is(a.fork, b.fork)
@@ -102,7 +102,7 @@ test('bigger download range', async function (t) {
})
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
t.is(b.length, a.length, 'same length')
t.is(downloaded.size, a.length, 'downloaded all')
@@ -120,7 +120,7 @@ test('high latency reorg', async function (t) {
{
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
}
s[0].destroy()
@@ -134,7 +134,7 @@ test('high latency reorg', async function (t) {
{
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
}
let same = 0
@@ -385,7 +385,7 @@ test('replicate discrete range', async function (t) {
replicate(a, b, t)
const r = b.download({ blocks: [0, 2, 3] })
- await r.downloaded()
+ await r.done()
t.is(d, 3)
t.alike(await b.get(0), Buffer.from('a'))
@@ -407,7 +407,7 @@ test('replicate discrete empty range', async function (t) {
const r = b.download({ blocks: [] })
- await r.downloaded()
+ await r.done()
t.is(d, 0)
})
@@ -506,7 +506,7 @@ test.skip('can disable downloading from a peer', async function (t) {
{
const r = c.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
}
const aPeer = b.peers[0].stream.rawStream === aStream
@@ -527,7 +527,7 @@ test.skip('can disable downloading from a peer', async function (t) {
{
const r = b.download({ start: 0, end: a.length })
- await r.downloaded()
+ await r.done()
}
t.is(aUploads, 0)
@@ -545,13 +545,13 @@ test('contiguous length', async function (t) {
replicate(a, b, t)
- await b.download({ blocks: [0, 2, 4] }).downloaded()
+ await b.download({ blocks: [0, 2, 4] }).done()
t.is(b.contiguousLength, 1, 'b has 0 through 1')
- await b.download({ blocks: [1] }).downloaded()
+ await b.download({ blocks: [1] }).done()
t.is(b.contiguousLength, 3, 'b has 0 through 2')
- await b.download({ blocks: [3] }).downloaded()
+ await b.download({ blocks: [3] }).done()
t.is(b.contiguousLength, 5, 'b has all blocks')
})
@@ -571,7 +571,7 @@ test('contiguous length after fork', async function (t) {
replicate(a, b, t)
- await b.download({ start: 0, end: a.length }).downloaded()
+ await b.download({ start: 0, end: a.length }).done()
t.is(b.contiguousLength, 3, 'b has all blocks after fork')
})
@@ -652,7 +652,7 @@ test('download blocks if available', async function (t) {
b.on('download', () => d++)
const r = b.download({ blocks: [1, 3, 6], ifAvailable: true })
- await r.downloaded()
+ await r.done()
t.is(d, 2)
})
@@ -669,7 +669,7 @@ test('download range if available', async function (t) {
b.on('download', () => d++)
const r = b.download({ start: 2, end: 6, ifAvailable: true })
- await r.downloaded()
+ await r.done()
t.is(d, 3)
})
@@ -688,7 +688,7 @@ test('download blocks if available, destroy midway', async function (t) {
})
const r = b.download({ blocks: [1, 3, 6], ifAvailable: true })
- await r.downloaded()
+ await r.done()
t.pass('range resolved')
})
@@ -707,7 +707,7 @@ test('download blocks available from when only a partial set is available', asyn
await b.get(3)
const r = c.download({ start: 0, end: -1, ifAvailable: true })
- await r.downloaded()
+ await r.done()
t.ok(!(await c.has(0)))
t.ok(!(await c.has(1)))
@@ -723,7 +723,7 @@ test('download range resolves immediately if no peers', async function (t) {
// no replication
const r = b.download({ start: 0, end: 5, ifAvailable: true })
- await r.downloaded()
+ await r.done()
t.pass('range resolved')
})
@@ -767,7 +767,7 @@ test('non-sparse snapshot during partial replication', async function (t) {
await a.append(['a', 'b', 'c', 'd', 'e'])
- await b.download({ start: 0, end: 3 }).downloaded()
+ await b.download({ start: 0, end: 3 }).done()
await c.update()
const s = c.snapshot()
@@ -776,7 +776,7 @@ test('non-sparse snapshot during partial replication', async function (t) {
await s.update()
t.is(s.contiguousLength, s.length)
- await b.download({ start: 3, end: 5 }).downloaded()
+ await b.download({ start: 3, end: 5 }).done()
t.is(s.contiguousLength, s.length)
await s.update()
@@ -794,13 +794,13 @@ test('sparse replication without gossiping', async function (t) {
let s
s = replicate(a, b)
- await b.download({ start: 0, end: 3 }).downloaded()
+ await b.download({ start: 0, end: 3 }).done()
await unreplicate(s)
await a.append(['d', 'e', 'f', 'd'])
s = replicate(a, b)
- await b.download({ start: 4, end: 7 }).downloaded()
+ await b.download({ start: 4, end: 7 }).done()
await unreplicate(s)
await t.test('block', async function (t) {
@@ -818,7 +818,7 @@ test('sparse replication without gossiping', async function (t) {
s = replicate(b, c)
t.teardown(() => unreplicate(s))
- await c.download({ start: 4, end: 6 }).downloaded()
+ await c.download({ start: 4, end: 6 }).done()
t.pass('resolved')
})
@@ -828,7 +828,7 @@ test('sparse replication without gossiping', async function (t) {
s = replicate(b, c)
t.teardown(() => unreplicate(s))
- await c.download({ blocks: [4, 6] }).downloaded()
+ await c.download({ blocks: [4, 6] }).done()
t.pass('resolved')
})

MODIFY
test/snapshots.js
test/snapshots.js
@@ -43,10 +43,10 @@ test('implicit snapshot - gets are snapshotted at call time', async function (t)
t.is(await clone.get(2), 'block #2.1')
- await range1.downloaded()
+ await range1.done()
t.pass('local range finished')
- await range2.downloaded()
+ await range2.done()
t.pass('remote range finished')
})

Rename `range.downloaded()` to `range.done()` (#303)