import IPFS from 'ipfs' ; import { Event } from '../lib/Event' ; const IpfsConfig = { repo : '/ipfs/apps/forever' , EXPERIMENTAL : { pubsub : true , } , config : { Addresses : { Swarm : [ '/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star' , '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star' , ] } , } } ; export default class Node extends Event { readonly ipfs : any ; constructor ( repo ? ) { super ( ) ; IpfsConfig . repo = repo ? repo : IpfsConfig . repo ; this . ipfs = new IPFS ( IpfsConfig ) ; this . ipfs . on ( 'ready' , ( ) => this . trigger ( 'ready' ) ) ; this . ipfs . on ( 'init' , ( ) => this . trigger ( 'init' ) ) ; this . ipfs . on ( 'start' , ( ) => this . trigger ( 'start' ) ) ; this . ipfs . on ( 'stop' , ( ) => this . trigger ( 'stop' ) ) ; this . ipfs . on ( 'error' , ( ) => this . trigger ( 'error' ) ) ; } onReady ( callback ) { super . register ( 'ready' , callback ) ; } onInit ( callback ) { super . register ( 'init' , callback ) ; } onStart ( callback ) { super . register ( 'start' , callback ) ; } onStop ( callback ) { super . register ( 'stop' , callback ) ; } onError ( callback ) { super . register ( 'error' , callback ) ; } async publish ( topic , data ) { return await this . ipfs . pubsub . publish ( topic , data ) ; } async subscribe ( topic , handler ) { return await this . ipfs . pubsub . subscribe ( topic , handler ) ; } unsubscribe ( topic , handlerToRemove ) { this . ipfs . pubsub . unsubscribe ( topic , handlerToRemove ) ; } }