Skip to content

Commit

Permalink
fix: return gateway href in .info response (#850)
Browse files Browse the repository at this point in the history
* fix: return gateway maddr in .info response

* fix: use REPO_PATH/gateway file for gateway address
  • Loading branch information
SgtPooki authored Nov 21, 2024
1 parent 3574412 commit c9dafd0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/kubo/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/kubo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface KuboInfo {
multiaddrs: string[]
api?: string
repo: string
gateway: string
}

export interface KuboNode extends Node<KuboRPCClient, KuboOptions, KuboInfo, KuboInitOptions, KuboStartOptions, KuboStopOptions> {
Expand Down
15 changes: 15 additions & 0 deletions src/kubo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
}
Expand Down
21 changes: 21 additions & 0 deletions test/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
})
})
})
2 changes: 2 additions & 0 deletions test/endpoint/routes.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit c9dafd0

Please sign in to comment.