Skip to content

Commit c9dafd0

Browse files
authored
fix: return gateway href in .info response (#850)
* fix: return gateway maddr in .info response * fix: use REPO_PATH/gateway file for gateway address
1 parent 3574412 commit c9dafd0

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

src/kubo/daemon.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { execa, type ResultPromise } from 'execa'
44
import mergeOptions from 'merge-options'
55
import pDefer from 'p-defer'
66
import waitFor from 'p-wait-for'
7-
import { checkForRunningApi, tmpDir, buildStartArgs, repoExists, buildInitArgs } from './utils.js'
7+
import { checkForRunningApi, tmpDir, buildStartArgs, repoExists, buildInitArgs, getGatewayAddress } from './utils.js'
88
import type { KuboNode, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions, KuboStopOptions } from './index.js'
99
import type { Logger } from '@libp2p/interface'
1010
import type { KuboRPCClient } from 'kubo-rpc-client'
@@ -91,7 +91,8 @@ export default class KuboDaemon implements KuboNode {
9191
peerId: id?.id.toString(),
9292
multiaddrs: (id?.addresses ?? []).map(ma => ma.toString()),
9393
api: checkForRunningApi(this.repo),
94-
repo: this.repo
94+
repo: this.repo,
95+
gateway: getGatewayAddress(this.repo)
9596
}
9697
}
9798

src/kubo/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export interface KuboInfo {
7575
multiaddrs: string[]
7676
api?: string
7777
repo: string
78+
gateway: string
7879
}
7980

8081
export interface KuboNode extends Node<KuboRPCClient, KuboOptions, KuboInfo, KuboInitOptions, KuboStartOptions, KuboStopOptions> {

src/kubo/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ export const checkForRunningApi = (repoPath = ''): string | undefined => {
3232
return (api != null) ? api.toString() : undefined
3333
}
3434

35+
export const getGatewayAddress = (repoPath = ''): string => {
36+
let gatewayAddress = ''
37+
try {
38+
/**
39+
* Note that this file is only created by Kubo versions >=v0.15.0, which came out in 2022
40+
*
41+
* @see https://github.com/ipfs/kubo/blob/720663d7c8f9971d34f85bd4c02a256da2d56a25/docs/changelogs/v0.15.md?plain=1#L56
42+
*/
43+
gatewayAddress = fs.readFileSync(path.join(repoPath, 'gateway'))?.toString()
44+
} catch (err: any) {
45+
log('Unable to open gateway file')
46+
}
47+
return gatewayAddress
48+
}
49+
3550
export const tmpDir = (type = ''): string => {
3651
return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`)
3752
}

test/controller.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,25 @@ describe('Node API', function () {
208208
}
209209
})
210210
})
211+
212+
describe('info', () => {
213+
describe('should return the node info', () => {
214+
for (const opts of types) {
215+
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
216+
const node = await factory.spawn(opts)
217+
const info = await node.info()
218+
219+
expect(info).to.have.property('version').that.is.a('string')
220+
expect(info).to.have.property('api').that.is.a('string')
221+
expect(info).to.have.property('peerId').that.is.a('string')
222+
expect(info).to.have.property('repo').that.is.a('string')
223+
expect(info).to.have.property('pid').that.is.a('number')
224+
expect(info).to.have.property('multiaddrs').that.is.an('array')
225+
expect(info).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/)
226+
227+
await node.stop()
228+
})
229+
}
230+
})
231+
})
211232
})

test/endpoint/routes.node.ts

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ describe('routes', function () {
109109
expect(res.result).to.have.property('api').that.is.a('string')
110110
expect(res.result).to.have.property('repo').that.is.a('string')
111111
expect(res.result).to.have.property('multiaddrs').that.is.an('array')
112+
expect(res.result).to.have.property('peerId').that.is.a('string')
113+
expect(res.result).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/)
112114
})
113115

114116
it('should return 400', async () => {

0 commit comments

Comments
 (0)