Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose bitfield #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions lib/sessions/hypercore.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ module.exports = class HypercoreSession {
})
}

async has ({ id, seq }) {
async has ({ id, seq, bitfield, length }) {
const core = this._sessionState.getCore(id)
return new Promise((resolve, reject) => {
core.ready(err => {
if (err) return reject(err)
return resolve({
has: core.has(seq)
has: core.has(seq),
bitfield: bitfield && core.bitfield.compress(seq, length)
})
})
})
Expand Down
34 changes: 34 additions & 0 deletions test/bitfield.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const test = require('tape')
const { createMany } = require('./helpers/create')

test('can get a bitfield', async t => {
const { clients, cleanup } = await createMany(2)

const client1 = clients[0]
const client2 = clients[1]
const corestore1 = client1.corestore()
const corestore2 = client2.corestore()

const core1 = corestore1.get()
await core1.ready()
await core1.append(Buffer.from('zero', 'utf8'))
await core1.append(Buffer.from('one', 'utf8'))
await core1.append(Buffer.from('two', 'utf8'))
await core1.append(Buffer.from('three', 'utf8'))
await client1.network.configure(core1.discoveryKey, { announce: true, lookup: true, flush: true })

const core2 = corestore2.get(core1.key)
await core2.ready()

await client2.network.configure(core2.discoveryKey, { announce: false, lookup: true })

await core2.get(1)
await core2.get(2)
const bitfield = await core2.getBitfield()
t.equal(bitfield.get(0), false)
t.equal(bitfield.get(1), true)
t.equal(bitfield.get(2), true)
t.equal(bitfield.get(4), false)
await cleanup()
t.end()
})