import { API } from '../core/api' ; import { readableNodeToWeb } from '../helpers/handlers' ; import { MobileQueryEvent } from '../models' ; import { ReadableStream } from 'web-streams-polyfill/ponyfill' ; import KSUID from '@therebel/ksuid' ; extends API export default class IPFS extends API { returns async id ( ) { const response = await this . sendGet ( `ipfs/id` ) ; return response . text ( ) as Promise < string > ; } param verbose param latency param streams param direction async peers ( verbose ? , latency ? , streams ? , direction ? ) { const response = await this . sendGet ( 'ipfs/swarm/peers' , undefined , { verbose : ( ! ! verbose ) . toString ( ) , latency : ( ! ! latency ) . toString ( ) , streams : ( ! ! streams ) . toString ( ) , direction : ( ! ! direction ) . toString ( ) } ) ; return response . json ( ) ; } param path param key returns async cat ( path , key ? ) { const response = await this . sendGet ( `ipfs/cat/${ path }` , undefined , { key : key || '' } ) ; return response . blob ( ) ; } param addr returns async connect ( addr ) { const response = await this . sendPost ( `ipfs/swarm/connect` , [ addr ] ) ; return response . status === 200 ; } param topic param data returns async pubsubPub ( topic , data ) { const response = await this . sendPost ( `ipfs/pubsub/pub/${ topic }` , undefined , undefined , data , undefined , typeof data === 'string' ) ; return response . status === 200 ; } param topic returns async pubsubSubGet ( topic , queryId ) { const response = await this . sendGet ( `ipfs/pubsub/sub/${ topic }` , undefined , { queryId } ) ; if ( ! response . body ) { throw Error ( 'Empty response stream' ) ; } return readableNodeToWeb < ArrayBuffer > ( response . body as ReadableStream ) ; } param topic param useEventSource returns async pubsubSub ( topic , useEventSource ? ) { const ksuidFromAsync = await KSUID . random ( ) ; const queryId = ksuidFromAsync . string ; const queryHandle = useEventSource ? this . sendEventSource ( `ipfs/pubsub/sub/${ topic }` , undefined , { events : true , queryId } ) : await this . pubsubSubGet ( topic , queryId ) ; return { queryId , queryHandle } ; } }