Constructor
# new Peer(store, host, config)
Peer creates a new IPFS Lite peer.
Parameters:
Name | Type | Description |
---|---|---|
store |
BlockStore | The underlying block store for locally caching immutable blocks of data. |
host |
Libp2p | The Libp2p host to use for interacting with the network. |
config |
PeerOptions | An optional set of configuration options. Currently only supports whether or not the peer should run in 'offline' mode. |
Classes
Members
Bitswap
# blockExchange
blockExchange is the "bitswap" instance that communicates with the network to retrieve blocks that are not in
the local store.
BlockService
# blockService
blockService is a content-addressable store for adding, deleting, and retrieving blocks of immutable data.
# dag
dag provides access to the underlying DAG service.
The dag API supports the creation and manipulation of dag-pb objects, as well as other IPLD formats
(i.e dag-cbor, ethereum-block, git, etc). Use it to put, get, and walk IPLD DAG objects.
BlockStore
# store
store is the underlying block store for locally caching immutable blocks of data.
Methods
# addFile(source, options)
addFile chunks and adds content to the DAG service.
The content is stored as a UnixFS DAG (default for IPFS). It returns the root ipld DAG node.
See https://github.com/ipfs/js-ipfs-unixfs-importer for further details.
Parameters:
Name | Type | Description |
---|---|---|
source |
An iterable that yields file objects with `path` and `content` keys. | |
options |
A set of options that control the layout and chunking mechanisms used by the underlying UnixFS library. |
Example
const peer = new Peer(...)
await peer.start()
const source = [
{
path: 'bar',
content: fs.createReadStream('bar.txt'),
},
]
const root = await peer.addFile(source)
console.log(root.cid.toString())
await peer.stop()
# getFile(cid)
getFile returns the content of a file as identified by its root CID.
The file must have been added as a UnixFS DAG (default for IPFS).
Parameters:
Name | Type | Description |
---|---|---|
cid |
The target content identifier. |
Example
const peer = new Peer(...)
await peer.start()
const buf = await peer.getFile('bafk...uny')
console.log(buf.toString())
await peer.stop()
# async hasBlock(cid)
hasBlock returns whether a given block is available locally. This method is shorthand for .store.has().
Parameters:
Name | Type | Description |
---|---|---|
cid |
CID | The target content identifier. |
Example
const peer = new Peer(...)
await peer.start()
const has = await peer.hasBlock('bafk...uny')
console.log(has)
await peer.stop()
# isOnline()
isOnline returns whether the peer has a valid block exchange and its p2p host has been started.
# async start()
start starts the underlying host and block exchange.
Example
const peer = new Peer(...)
await peer.start()
// do stuff with it...
await peer.stop()
# async stop()
stop stops the underlying block exchange and host.
Example
const peer = new Peer(...)
await peer.start()
// do stuff with it...
await peer.stop()