import type LibP2P from "libp2p" ; import Bitswap from "ipfs-bitswap" ; import type { Blockstore } from "ipfs-repo" ; import { CID } from "multiformats/basics.js" ; import { BlockService } from "./blockservice" ; import { BlockStore } from "./blockstore" ; import { DAGService } from "./dagservice" ; /**
 * PeerOptions wraps configuration options for the IPFS Lite peer.
 */ export interface PeerOptions { /**
     * offline controls whether or not the block service will announce or retrieve blocks from the network.
     */ offline : boolean ; } /**
 * 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.
 */ export class Peer extends DAGService { /**
     * config is a set of options to configure the IPFS Lite peer.
     */ public config : PeerOptions ; /**
     * host is the Libp2p host used to interact with the network.
     */ public host : LibP2P ; /**
     * store is the underlying block store for locally caching immutable blocks of data.
     */ public store : BlockStore ; /**
     * blockExchange is the "bitswap" instance that communicates with the network to retrieve blocks that are not in
     * the local store.
     */ public blockExchange : Bitswap ; /**
     * blockService is a content-addressable store for adding, deleting, and retrieving blocks of immutable data.
     */ public blockService : BlockService ; param { BlockStore } store param { Libp2p } host param { PeerOptions } config constructor ( store , host , config = { offline : false } ) { const blockExchange = new Bitswap ( host , ( store as unknown ) as Blockstore ) ; const blockService = new BlockService ( store , config . offline ? undefined : blockExchange ) ; super ( { blockService } ) ; this . host = host ; this . store = store ; this . config = config ; this . blockExchange = blockExchange ; this . blockService = blockService ; } example async start ( ) { await this . host . start ( ) ; this . blockExchange . start ( ) ; } example async stop ( ) { this . blockExchange . stop ( ) ; await this . host . stop ( ) ; } param { CID } cid example async hasBlock ( cid ) { return this . store . has ( cid ) ; } /**
     * isOnline returns whether the peer has a valid block exchange and its p2p host has been started.
     */ isOnline ( ) { return this . blockExchange && this . host && this . host . isStarted ( ) ; } /**
     * 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.
     */ get dag ( ) : this { return this ; } }