Intro

Installable via npm install --save ipfs-bitswap, it can also be used directly in the browser.

Download

The source is available for download from GitHub. Alternatively, you can install using npm:

$ npm install --save ipfs-bitswap

You can then require() ipfs-bitswap as normal:

const ipfsBitswap = require('ipfs-bitswap')

In the Browser

ipfs-bitswap should work in any ECMAScript 2018 environment out of the box.

Usage:

<script type="text/javascript" src="index.js"></script>

The portable versions of ipfs-bitswap, including index.js and index.min.js, are included in the /dist folder. ipfs-bitswap can also be found on unpkg.com under

JavaScript implementation of the Bitswap 'data exchange' protocol used by IPFS.

new Bitswap(libp2p: Libp2p, blockstore: Blockstore, options: Object)
Parameters
libp2p (Libp2p)
blockstore (Blockstore)
options (Object)
Instance Members
enableStats()
disableStats()
wantlistForPeer(peerId)
ledgerForPeer(peerId)
get(cid, options)
getMany(cids, options)
unwant(cids)
cancelWants(cids)
put(block)
putMany(blocks)
_sendHaveBlockNotifications(block)
getWantlist()
peers()
stat()
start()
stop()

findProviders

src/network.js

Find providers given a cid.

findProviders(cid: CID, maxProviders: number, options: Object): AsyncIterable<PeerInfo>
Parameters
cid (CID)
maxProviders (number)
options (Object = {})
Name Description
options.abortSignal AbortSignal
Returns
AsyncIterable<PeerInfo>:

findAndConnect

src/network.js

Find the providers of a given cid and connect to them.

findAndConnect(cid: CID, options: Object): void
Parameters
cid (CID)
options (Object)
Name Description
options.abortSignal AbortSignal
Returns
void:

Tell the network we can provide content for the passed CID

provide(cid: CID, options: Object): Promise<void>
Parameters
cid (CID)
options (Object)
Name Description
options.abortSignal AbortSignal
Returns
Promise<void>:

Connects to another peer

connectTo(peer: (PeerId | Multiaddr), options: Object): Promise<Connection>
Parameters
peer ((PeerId | Multiaddr))
options (Object)
Name Description
options.abortSignal AbortSignal
Returns
Promise<Connection>:
Task

Type: Object

Properties
topic (string) : a name for the Task (like an id but not necessarily unique)
priority (number) : tasks are ordered by priority per peer
size (number) : the size of the task, eg the number of bytes in a block
TaskMerger

Type: Object

Properties
hasNewInfo (function (task, tasksWithTopic)) : given the existing tasks with the same topic, does the task add some new information? Used to decide whether to merge the task or ignore it.
merge (function (task, existingTask)) : merge the information from the given task into the existing task (with the same topic)
Static Members
hasNewInfo(task, tasksWithTopic)
merge(newTask, existingTask)

The task merger that is used by default. Assumes that new tasks do not add any information over existing tasks, and doesn't try to merge.

DefaultTaskMerger

Queue of requests to be processed by the engine. The requests from each peer are added to the peer's queue, sorted by priority. Tasks are popped in priority order from the best peer - see popTasks() for more details.

new RequestQueue(taskMerger: TaskMerger)
Parameters
taskMerger (TaskMerger)
Instance Members
pushTasks(peerId, tasks)
popTasks(targetMinBytes)
remove(topic, peerId)
tasksDone(peerId, tasks)

Queue of tasks for a particular peer, sorted by priority.

new PeerTasks(peerId: PeerId, taskMerger: TaskMerger)
Parameters
peerId (PeerId)
taskMerger (TaskMerger)
Instance Members
pushTasks(tasks)
popTasks(targetMinBytes)
taskDone(task)
remove(topic)
isIdle()

Queue of pending tasks for a particular peer, sorted by priority.

new PendingTasks()

SortedMap is a Map whose iterator order can be defined by the user

new SortedMap(entries: Array<k, v>?, cmp: function (a, b)?)

Extends Map

Parameters
entries (Array<k, v>?)
cmp (function (a, b)?) compares [k1, v1] to [k2, v2]
Instance Members
update(i)

Is equal for Maps of BitswapMessageEntry or Blocks

isMapEqual
Parameters
a (Map)
b (Map)
Returns
boolean: