import { expect } from "chai" ; import { isBrowser } from "browser-or-node" ; import { MemoryDatastore } from "interface-datastore" ; import multiformats , { CID } from "multiformats/basics.js" ; import { Buffer } from "buffer" ; import { Peer , BlockStore } from ".." ; import { setupLibP2PHost } from "../setup" ; const { util : { serialize , deserialize } , } = require ( "ipld-dag-pb" ) ; const dagpb = { encode : serialize , decode : ( buffer ) => deserialize ( Buffer . from ( buffer ) ) , code : 0x70 , name : "dag-pb" , } ; multiformats . multicodec . add ( dagpb ) ; const sleep = ( ms ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ; let lite ; ( isBrowser ? describe . skip : describe ) ( "fetching IPLD dag from network" , function ( ) { this . timeout ( 10000 ) ; before ( async function ( ) { const bs = new BlockStore ( new MemoryDatastore ( ) ) ; const host = await setupLibP2PHost ( undefined , undefined , [ "/ip4/0.0.0.0/tcp/0" , ] ) ; lite = new Peer ( bs , host ) ; await lite . start ( ) ; await sleep ( 500 ) ; } ) ; after ( async function ( ) { await lite . stop ( ) ; } ) ; it ( "request, fetch, and decode" , async function ( ) { const cid = new CID ( "QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u" ) ; const block = await lite . get ( cid ) ; if ( block && block . Data ) { const msg = block . Data . toString ( ) . replace ( /[^0-9a-zA-Z_\s]/gi , "" ) . trim ( ) ; expect ( msg ) . to . eql ( "Hello World" ) ; } else { throw Error ( "Expected block to have data" ) ; } } ) ; } ) ;