Skip to content

Commit e9436ca

Browse files
authored
fix: isOnline should return false when node is offline (#222)
If the node is offline, trying to invoke the api client results in an error being thrown. If this happens the node is not online so return false instead.
1 parent 7266128 commit e9436ca

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/is-online.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ export function createIsOnline (client: HTTPRPCClient): KuboRPCClient['isOnline'
66
const id = createId(client)
77

88
return async function isOnline (options = {}) {
9-
const res = await id(options)
9+
try {
10+
const res = await id(options)
1011

11-
return Boolean(res?.addresses?.length)
12+
return Boolean(res?.addresses?.length)
13+
} catch {
14+
return false
15+
}
1216
}
1317
}

test/is-online.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-env mocha */
2+
3+
import { expect } from 'aegir/chai'
4+
import { factory } from './utils/factory.js'
5+
import type { KuboRPCClient } from '../src/index.js'
6+
7+
const f = factory()
8+
9+
describe('.isOnline', function () {
10+
this.timeout(20 * 1000)
11+
12+
let ipfs: KuboRPCClient
13+
14+
before(async function () {
15+
ipfs = (await f.spawn()).api
16+
})
17+
18+
after(async function () { return f.clean() })
19+
20+
it('should return true when the node is online', async function () {
21+
await expect(ipfs.isOnline()).to.eventually.be.true()
22+
})
23+
24+
it('should return false when the node is offline', async function () {
25+
await f.controllers[0].stop()
26+
await expect(ipfs.isOnline()).to.eventually.be.false()
27+
})
28+
})

0 commit comments

Comments
 (0)