Class

Peer

Peer(store, host, config)

Peer is an IPFS Lite peer. It provides a DAG service that can fetch and put blocks from/to the IPFS network. It casually satisfies the original IPFS Lite interface: https://github.com/hsanjuan/ipfs-lite.

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.

View Source core/peer.ts, line 19

Classes

Peer

Members

Bitswap

# blockExchange

blockExchange is the "bitswap" instance that communicates with the network to retrieve blocks that are not in the local store.

View Source core/peer.ts, line 126

BlockService

# blockService

blockService is a content-addressable store for adding, deleting, and retrieving blocks of immutable data.

View Source core/peer.ts, line 132

PeerOptions

# config

config is a set of options to configure the IPFS Lite peer.

View Source core/peer.ts, line 107

# 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.

View Source core/peer.ts, line 81

Libp2p

# host

host is the Libp2p host used to interact with the network.

View Source core/peer.ts, line 113

BlockStore

# store

store is the underlying block store for locally caching immutable blocks of data.

View Source core/peer.ts, line 119

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.

View Source files/index.ts, line 30

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.

View Source files/index.ts, line 50

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.

View Source core/peer.ts, line 66

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.

View Source core/peer.ts, line 72

# async start()

start starts the underlying host and block exchange.

View Source core/peer.ts, line 38

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.

View Source core/peer.ts, line 51

Example
const peer = new Peer(...)
await peer.start()
// do stuff with it...
await peer.stop()