import dagJose from 'dag-jose' ; import { convert } from 'blockcodec-to-ipld-format' ; import IPFS from 'ipfs-core' ; import { IpfsApi } from '@ceramicnetwork/common' ; import tmp from 'tmp-promise' ; import getPort from 'get-port' ; param overrideConfig export async function createIPFS ( overrideConfig = { } ) { const format = convert ( dagJose ) ; const tmpFolder = await tmp . dir ( { unsafeCleanup : true } ) ; const port = await getPort ( ) ; const defaultConfig = { ipld : { formats : [ format ] } , repo : `${ tmpFolder . path }/ipfs${ port }/` , config : { Addresses : { Swarm : [ `/ip4/127.0.0.1/tcp/${ port }` ] } , Bootstrap : [ ] , } , } ; const config = { ... defaultConfig , ... overrideConfig } ; const instance = await IPFS . create ( config ) ; return new Proxy ( instance , { get ( target , p ) { if ( p === 'stop' ) { tmpFolder . cleanup ( ) ; } return target [ p ] ; } , } ) ; } param a param b export async function swarmConnect ( a , b ) { const addressB = ( await b . id ( ) ) . addresses [ 0 ] ; await a . swarm . connect ( addressB ) ; } param n param overrideConfig export function fleet ( n , overrideConfig = { } ) { return Promise . all ( Array . from ( { length : n } ) . map ( ( ) => createIPFS ( overrideConfig ) ) ) ; } param n param task export async function withFleet ( n , task ) { const instances = await fleet ( n ) ; try { await task ( instances ) ; } finally { instances . map ( ( instance ) => instance . stop ( ) ) ; } }