Skip to content

Commit 10b5c61

Browse files
authored
add live download support (#9)
1 parent a925831 commit 10b5c61

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/sessions/hypercore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ module.exports = class HypercoreSession {
9595
})
9696
}
9797

98-
async download ({ id, resourceId, start, end, blocks, linear }) {
98+
async download ({ id, resourceId, start, end, blocks, linear, live }) {
9999
const core = this._sessionState.getCore(id)
100-
const opts = { start, end, blocks: blocks.length ? blocks : null, linear }
100+
const opts = { start, end: live ? -1 : end, blocks: blocks.length ? blocks : null, linear }
101101
return new Promise((resolve, reject) => {
102102
let downloaded = false
103103
const d = core.download(opts, (err) => {

test/networked.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,42 @@ test('can watch downloads and appends', async t => {
511511
t.end()
512512
})
513513

514+
test('download all', async t => {
515+
const { clients, cleanup } = await createMany(2)
516+
517+
const client1 = clients[0]
518+
const client2 = clients[1]
519+
const corestore1 = client1.corestore()
520+
const corestore2 = client2.corestore()
521+
522+
const core1 = corestore1.get()
523+
await core1.ready()
524+
await core1.append(Buffer.from('zero', 'utf8'))
525+
await core1.append(Buffer.from('one', 'utf8'))
526+
await core1.append(Buffer.from('two', 'utf8'))
527+
await client1.network.configure(core1.discoveryKey, { announce: true, lookup: true, flush: true })
528+
529+
const core2 = corestore2.get(core1.key)
530+
await core2.ready()
531+
532+
let downloads = 0
533+
const p = new Promise((resolve) => {
534+
core2.on('download', () => {
535+
downloads++
536+
if (downloads === 3) resolve()
537+
})
538+
})
539+
540+
await client2.network.configure(core1.discoveryKey, { announce: false, lookup: true })
541+
542+
core2.download() // download all
543+
544+
await p
545+
t.same(downloads, 3, '3 downloads')
546+
await cleanup()
547+
t.end()
548+
})
549+
514550
function watchDownloadPromise (core, expectedSeq) {
515551
return new Promise((resolve, reject) => {
516552
core.once('download', seq => {

0 commit comments

Comments
 (0)