Skip to content

Expose bitfields #3

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

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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ See the [Hypercore docs](https://github.com/hypercore-protocol/hypercore) for mo

Append a block or array of blocks to the hypercore

#### `await feed.getBitfield([start], [length])

Get a bitfield of downloaded blocks. Optionally restrict to the range from `start` for `length` blocks. Resolves to a [bitfield](https://github.com/fb55/bitfield) instance.

#### `feed.peers`

A list of peers this feed is connected to.
Expand Down
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { EventEmitter } = require('events')
const rle = require('bitfield-rle')
const Bitfield = require('bitfield')
const maybe = require('call-me-maybe')
const codecs = require('codecs')
const hypercoreCrypto = require('hypercore-crypto')
Expand Down Expand Up @@ -545,6 +547,17 @@ class RemoteHypercore extends Nanoresource {
return rsp.has
}

async _getBitfield (start, length) {
const rsp = await this._client.hypercore.has({
seq: start || 0,
bitfield: true,
length,
id: this._id
})
if (!rsp.bitfield) throw new Error('Did not receive a bitfield')
return new Bitfield(rle.decode(rsp.bitfield))
}

async _download (range, resourceId) {
if (!this.opened) await this.open()
if (this.closed) throw new Error('Feed is closed')
Expand Down Expand Up @@ -611,6 +624,10 @@ class RemoteHypercore extends Nanoresource {
return maybe(cb, this._has(seq))
}

getBitfield (start, length, cb) {
return maybe(cb, this._getBitfield(start, length))
}

cancel (get) {
if (typeof get.resourceId !== 'number') throw new Error('Must pass a get return value')
return this._cancel(get.resourceId)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"homepage": "https://github.com/hyperspace-org/hyperspace#readme",
"dependencies": {
"@hyperspace/rpc": "^1.0.0",
"bitfield": "^3.0.0",
"bitfield-rle": "^2.2.1",
"call-me-maybe": "^1.0.1",
"codecs": "^2.1.0",
"freemap": "^1.0.0",
Expand Down