ipfs / js-ipfsd-ctl

Control an IPFS daemon (go-ipfs or js-ipfs) using JavaScript!
https://ipfs.github.io/js-ipfsd-ctl
Other
152 stars 67 forks source link

feat: add back `controller.api.gatewayHost` and `controller.api.gatewayPort` or similar #831

Open SgtPooki opened 2 months ago

SgtPooki commented 2 months ago

With the latest update from #823 we no longer have the ability to get the listening ipfs gateway host and port from the instantiated kubo node.

this forces consumers to do handle passing around port/host config passed to createNode or parsing config options from the created KuboNode using https://github.com/multiformats/js-multiaddr-to-uri

We shouldn't need to do this on the consumer side because ipfsd-ctl and kubo-rpc-client should both have the gateway URL readily accessible.

We should expose this on the await node.info() result.

SgtPooki commented 1 month ago

we should call .id and it should return the listening multiaddrs

SgtPooki commented 1 month ago

it doesn't seem like controller.api.id returns the same gateway endpoint we used to get with controller.api.gatewayHost/port

create code:

import { createNode, type KuboNode } from 'ipfsd-ctl'
import { path as kuboPath } from 'kubo'
import { create } from 'kubo-rpc-client'

export async function createKuboNode (listenPort?: number, repoPath?: string): Promise<{node: KuboNode, gatewayUrl: string}> {
  console.log('listening on port:', listenPort)
  const controller = await createNode({
    type: 'kubo',
    rpc: create,
    bin: kuboPath(),
    test: true,
    repo: repoPath,
    init: {
      config: {
        Addresses: {
          Swarm: [
            '/ip4/0.0.0.0/tcp/4001',
            '/ip4/0.0.0.0/tcp/4002/ws'
          ],
          Gateway: `/ip4/0.0.0.0/tcp/${listenPort ?? 0}`
        },
        Gateway: {
          NoFetch: true,
          ExposeRoutingAPI: true,
          HTTPHeaders: {
            'Access-Control-Allow-Origin': ['*'],
            'Access-Control-Allow-Methods': ['GET', 'POST', 'PUT', 'OPTIONS']
          }
        }
      }
    },
    args: ['--enable-pubsub-experiment', '--enable-namesys-pubsub']
  })

  console.log('id output: ', await controller.api.id())

  return {
    node: controller,
    gatewayUrl: `http://localhost:${listenPort ?? 0}`
  }
}

and the output:

listening on port: 3440
id output:  {
  id: PeerId(12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
  publicKey: 'CAESIIB/5yS1MRszt7rKlJLmo7ulFBVdmonJotEXrw2GJxyB',
  addresses: [
    Multiaddr(/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/127.0.0.1/udp/4001/quic-v1/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEiBGXKLzwcwO4htzNfqPCXkUAr6EwfNcwxO6kQSI23ysYw/certhash/uEiBgrXo_HUamK5EdTepvz4UPm1SOcOF4NSpaedG-qrryXA/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/192.168.1.227/tcp/4001/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/192.168.1.227/udp/4001/quic-v1/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/192.168.1.227/udp/4001/quic-v1/webtransport/certhash/uEiBGXKLzwcwO4htzNfqPCXkUAr6EwfNcwxO6kQSI23ysYw/certhash/uEiBgrXo_HUamK5EdTepvz4UPm1SOcOF4NSpaedG-qrryXA/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/97.126.29.103/udp/4001/quic-v1/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip4/97.126.29.103/udp/4001/quic-v1/webtransport/certhash/uEiBGXKLzwcwO4htzNfqPCXkUAr6EwfNcwxO6kQSI23ysYw/certhash/uEiBgrXo_HUamK5EdTepvz4UPm1SOcOF4NSpaedG-qrryXA/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip6/::1/tcp/4001/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip6/::1/udp/4001/quic-v1/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU),
    Multiaddr(/ip6/::1/udp/4001/quic-v1/webtransport/certhash/uEiBGXKLzwcwO4htzNfqPCXkUAr6EwfNcwxO6kQSI23ysYw/certhash/uEiBgrXo_HUamK5EdTepvz4UPm1SOcOF4NSpaedG-qrryXA/p2p/12D3KooWJTyULQ6sHpQuQMPbm3Bu2fAwoB2k6DC5JxGPKn5WPzeU)
  ],
  agentVersion: 'kubo/0.27.0/',
  protocols: [
    '/ipfs/bitswap',
    '/ipfs/bitswap/1.0.0',
    '/ipfs/bitswap/1.1.0',
    '/ipfs/bitswap/1.2.0',
    '/ipfs/id/1.0.0',
    '/ipfs/id/push/1.0.0',
    '/ipfs/kad/1.0.0',
    '/ipfs/lan/kad/1.0.0',
    '/ipfs/ping/1.0.0',
    '/libp2p/autonat/1.0.0',
    '/libp2p/circuit/relay/0.2.0/hop',
    '/libp2p/circuit/relay/0.2.0/stop',
    '/libp2p/dcutr',
    '/x/'
  ]
}
http://localhost:3440