From d4cbc601da0d7014f64aded7123df12ce941e3db Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:15:07 -0600 Subject: [PATCH 1/2] fix: return gateway maddr in .info response --- src/kubo/daemon.ts | 9 ++++++++- src/kubo/index.ts | 1 + test/controller.spec.ts | 21 +++++++++++++++++++++ test/endpoint/routes.node.ts | 2 ++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/kubo/daemon.ts b/src/kubo/daemon.ts index 4239e23e..73fc2394 100644 --- a/src/kubo/daemon.ts +++ b/src/kubo/daemon.ts @@ -27,6 +27,7 @@ export default class KuboDaemon implements KuboNode { private readonly disposable: boolean private subprocess?: ResultPromise private _api?: KuboRPCClient + private _gateway?: string private readonly repo: string private readonly stdout: Logger private readonly stderr: Logger @@ -91,7 +92,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: this._gateway ?? '' } } @@ -202,11 +204,16 @@ export default class KuboDaemon implements KuboNode { const readyHandler = (data: Buffer): void => { output += data.toString() const apiMatch = output.trim().match(/API .*listening on:? (.*)/) + const gwMatch = output.trim().match(/Gateway .*listening on:? (.*)/) if ((apiMatch != null) && apiMatch.length > 0) { this._api = this.options.rpc(apiMatch[1]) } + if ((gwMatch != null) && gwMatch.length > 0) { + this._gateway = gwMatch[1] + } + if (output.match(/(?:daemon is running|Daemon is ready)/) != null) { // we're good stdout.off('data', readyHandler) 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/test/controller.spec.ts b/test/controller.spec.ts index 3df630f8..36fb58c2 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(/\/ip4\/127\.0\.0\.1\/tcp\/\d+/) + + await node.stop() + }) + } + }) + }) }) diff --git a/test/endpoint/routes.node.ts b/test/endpoint/routes.node.ts index f73212be..281306e0 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') }) it('should return 400', async () => { From 7c790b016fd3c2920605739b5bb78a43e5fd000e Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:31:58 -0600 Subject: [PATCH 2/2] fix: use REPO_PATH/gateway file for gateway address --- src/kubo/daemon.ts | 10 ++-------- src/kubo/utils.ts | 15 +++++++++++++++ test/controller.spec.ts | 2 +- test/endpoint/routes.node.ts | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/kubo/daemon.ts b/src/kubo/daemon.ts index 73fc2394..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' @@ -27,7 +27,6 @@ export default class KuboDaemon implements KuboNode { private readonly disposable: boolean private subprocess?: ResultPromise private _api?: KuboRPCClient - private _gateway?: string private readonly repo: string private readonly stdout: Logger private readonly stderr: Logger @@ -93,7 +92,7 @@ export default class KuboDaemon implements KuboNode { multiaddrs: (id?.addresses ?? []).map(ma => ma.toString()), api: checkForRunningApi(this.repo), repo: this.repo, - gateway: this._gateway ?? '' + gateway: getGatewayAddress(this.repo) } } @@ -204,16 +203,11 @@ export default class KuboDaemon implements KuboNode { const readyHandler = (data: Buffer): void => { output += data.toString() const apiMatch = output.trim().match(/API .*listening on:? (.*)/) - const gwMatch = output.trim().match(/Gateway .*listening on:? (.*)/) if ((apiMatch != null) && apiMatch.length > 0) { this._api = this.options.rpc(apiMatch[1]) } - if ((gwMatch != null) && gwMatch.length > 0) { - this._gateway = gwMatch[1] - } - if (output.match(/(?:daemon is running|Daemon is ready)/) != null) { // we're good stdout.off('data', readyHandler) 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 36fb58c2..f13034c8 100644 --- a/test/controller.spec.ts +++ b/test/controller.spec.ts @@ -222,7 +222,7 @@ describe('Node API', function () { 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(/\/ip4\/127\.0\.0\.1\/tcp\/\d+/) + 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 281306e0..a8307500 100644 --- a/test/endpoint/routes.node.ts +++ b/test/endpoint/routes.node.ts @@ -110,7 +110,7 @@ describe('routes', function () { 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') + expect(res.result).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/) }) it('should return 400', async () => {