Skip to content

Commit 7c790b0

Browse files
committed
fix: use REPO_PATH/gateway file for gateway address
1 parent d4cbc60 commit 7c790b0

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/kubo/daemon.ts

Lines changed: 2 additions & 8 deletions
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'
@@ -27,7 +27,6 @@ export default class KuboDaemon implements KuboNode {
2727
private readonly disposable: boolean
2828
private subprocess?: ResultPromise
2929
private _api?: KuboRPCClient
30-
private _gateway?: string
3130
private readonly repo: string
3231
private readonly stdout: Logger
3332
private readonly stderr: Logger
@@ -93,7 +92,7 @@ export default class KuboDaemon implements KuboNode {
9392
multiaddrs: (id?.addresses ?? []).map(ma => ma.toString()),
9493
api: checkForRunningApi(this.repo),
9594
repo: this.repo,
96-
gateway: this._gateway ?? ''
95+
gateway: getGatewayAddress(this.repo)
9796
}
9897
}
9998

@@ -204,16 +203,11 @@ export default class KuboDaemon implements KuboNode {
204203
const readyHandler = (data: Buffer): void => {
205204
output += data.toString()
206205
const apiMatch = output.trim().match(/API .*listening on:? (.*)/)
207-
const gwMatch = output.trim().match(/Gateway .*listening on:? (.*)/)
208206

209207
if ((apiMatch != null) && apiMatch.length > 0) {
210208
this._api = this.options.rpc(apiMatch[1])
211209
}
212210

213-
if ((gwMatch != null) && gwMatch.length > 0) {
214-
this._gateway = gwMatch[1]
215-
}
216-
217211
if (output.match(/(?:daemon is running|Daemon is ready)/) != null) {
218212
// we're good
219213
stdout.off('data', readyHandler)

src/kubo/utils.ts

Lines changed: 15 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('Node API', function () {
222222
expect(info).to.have.property('repo').that.is.a('string')
223223
expect(info).to.have.property('pid').that.is.a('number')
224224
expect(info).to.have.property('multiaddrs').that.is.an('array')
225-
expect(info).to.have.property('gateway').that.is.a('string').that.matches(/\/ip4\/127\.0\.0\.1\/tcp\/\d+/)
225+
expect(info).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/)
226226

227227
await node.stop()
228228
})

test/endpoint/routes.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('routes', function () {
110110
expect(res.result).to.have.property('repo').that.is.a('string')
111111
expect(res.result).to.have.property('multiaddrs').that.is.an('array')
112112
expect(res.result).to.have.property('peerId').that.is.a('string')
113-
expect(res.result).to.have.property('gateway').that.is.a('string')
113+
expect(res.result).to.have.property('gateway').that.is.a('string').that.matches(/http:\/\/127.0.0.1:\d+/)
114114
})
115115

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

0 commit comments

Comments
 (0)