Node.js implementation of the Bitswap 'data exchange' protocol used by IPFS
> npm install ipfs-bitswap --save
const Bitswap = require('ipfs-bitswap')
The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.
const Bitswap = require('ipfs-bitswap')
Loading this module through a script tag will make the IpfsBitswap
object available in the global namespace.
<script src="https://unpkg.com/ipfs-bitswap/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/ipfs-bitswap/dist/index.js"></script>
For the documentation see API.md.
libp2p: Libp2p
, instance of the local network stack.blockstore: Blockstore
, instance of the local database (IpfsRepo.blockstore
)
Create a new instance.
cid: CID|Array
Returns a source pull-stream
. Values emitted are the received blocks.
Example:
// Single block
pull(
bitswap.getStream(cid),
pull.collect((err, blocks) => {
// blocks === [block]
})
)
// Many blocks
pull(
bitswap.getStream([cid1, cid2, cid3]),
pull.collect((err, blocks) => {
// blocks === [block1, block2, block3]
})
)
Note: This is safe guarded so that the network is not asked for blocks that are in the local
datastore
.
cids: CID|[]CID
Cancel previously requested cids, forcefully. That means they are removed from the
wantlist independent of how many other resources requested these cids. Callbacks
attached to getBlock
are errored with Error('manual unwant: cid)
.
cid: CID|[]CID
Cancel previously requested cids.
Returns a duplex pull-stream
that emits an object {cid: CID}
for every written block when it was stored.
Objects passed into here should be of the form {data: Buffer, cid: CID}
blockAndCid: {data: Buffer, cid: CID}
callback: Function
Announce that the current node now has the block containing data
. This will store it
in the local database and attempt to serve it to all peers that are known
to have requested it. The callback is called when we are sure that the block
is stored.
peerId: PeerId
Get the wantlist for a given peer.
Get stats about about the current state of the bitswap instance.
» tree src
src
├── components
│ ├── decision
│ │ ├── engine.js
│ │ ├── index.js
│ │ └── ledger.js
│ ├── network # Handles peerSet and open new conns
│ │ └── index.js
│ └── want-manager # Keeps track of all blocks the peer wants (not the others which it is connected)
│ ├── index.js
│ └── msg-queue.js # Messages to send queue, one per peer
├── constants.js
├── index.js
└── types
├── message # (Type) message that is put in the wire
│ ├── entry.js
│ ├── index.js
│ └── message.proto.js
└── wantlist # (Type) track wanted blocks
├── entry.js
└── index.js
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.