From c9dafd0192c60f54a8308c46a601a627919d6c71 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 21 Nov 2024 10:50:49 -0600 Subject: [PATCH] fix: return gateway href in .info response (#850) * fix: return gateway maddr in .info response * fix: use REPO_PATH/gateway file for gateway address --- src/kubo/daemon.ts | 5 +++-- src/kubo/index.ts | 1 + src/kubo/utils.ts | 15 +++++++++++++++ test/controller.spec.ts | 21 +++++++++++++++++++++ test/endpoint/routes.node.ts | 2 ++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/kubo/daemon.ts b/src/kubo/daemon.ts index 4239e23e..62c380d3 100644 --- a/src/kubo/daemon.ts +++ b/src/kubo/daemon.ts @@ -4,7 +4,7 @@ import { execa, type ResultPromise } from 'execa' import mergeOptions from 'merge-options' import pDefer from 'p-defer' import waitFor from 'p-wait-for' -import { checkForRunningApi, tmpDir, buildStartArgs, repoExists, buildInitArgs } from './utils.js' +import { checkForRunningApi, tmpDir, buildStartArgs, repoExists, buildInitArgs, getGatewayAddress } from './utils.js' import type { KuboNode, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions, KuboStopOptions } from './index.js' import type { Logger } from '@libp2p/interface' import type { KuboRPCClient } from 'kubo-rpc-client' @@ -91,7 +91,8 @@ export default class KuboDaemon implements KuboNode { peerId: id?.id.toString(), multiaddrs: (id?.addresses ?? []).map(ma => ma.toString()), api: checkForRunningApi(this.repo), - repo: this.repo + repo: this.repo, + gateway: getGatewayAddress(this.repo) } } diff --git a/src/kubo/index.ts b/src/kubo/index.ts index fc9b8dfb..705acb4e 100644 --- a/src/kubo/index.ts +++ b/src/kubo/index.ts @@ -75,6 +75,7 @@ export interface KuboInfo { multiaddrs: string[] api?: string repo: string + gateway: string } export interface KuboNode extends Node { diff --git a/src/kubo/utils.ts b/src/kubo/utils.ts index e8406a51..fff81833 100644 --- a/src/kubo/utils.ts +++ b/src/kubo/utils.ts @@ -32,6 +32,21 @@ export const checkForRunningApi = (repoPath = ''): string | undefined => { return (api != null) ? api.toString() : undefined } +export const getGatewayAddress = (repoPath = ''): string => { + let gatewayAddress = '' + try { + /** + * Note that this file is only created by Kubo versions >=v0.15.0, which came out in 2022 + * + * @see https://github.com/ipfs/kubo/blob/720663d7c8f9971d34f85bd4c02a256da2d56a25/docs/changelogs/v0.15.md?plain=1#L56 + */ + gatewayAddress = fs.readFileSync(path.join(repoPath, 'gateway'))?.toString() + } catch (err: any) { + log('Unable to open gateway file') + } + return gatewayAddress +} + export const tmpDir = (type = ''): string => { return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`) } diff --git a/test/controller.spec.ts b/test/controller.spec.ts index 3df630f8..f13034c8 100644 --- a/test/controller.spec.ts +++ b/test/controller.spec.ts @@ -208,4 +208,25 @@ describe('Node API', function () { } }) }) + + describe('info', () => { + describe('should return the node info', () => { + for (const opts of types) { + it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => { + const node = await factory.spawn(opts) + const info = await node.info() + + expect(info).to.have.property('version').that.is.a('string') + expect(info).to.have.property('api').that.is.a('string') + expect(info).to.have.property('peerId').that.is.a('string') + expect(info).to.have.property('repo').that.is.a('string') + expect(info).to.have.property('pid').that.is.a('number') + expect(info).to.have.property('multiaddrs').that.is.an('array') + expect(info).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/) + + await node.stop() + }) + } + }) + }) }) diff --git a/test/endpoint/routes.node.ts b/test/endpoint/routes.node.ts index f73212be..a8307500 100644 --- a/test/endpoint/routes.node.ts +++ b/test/endpoint/routes.node.ts @@ -109,6 +109,8 @@ describe('routes', function () { expect(res.result).to.have.property('api').that.is.a('string') expect(res.result).to.have.property('repo').that.is.a('string') expect(res.result).to.have.property('multiaddrs').that.is.an('array') + expect(res.result).to.have.property('peerId').that.is.a('string') + expect(res.result).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/) }) it('should return 400', async () => {