Skip to content

Commit

Permalink
fix: isOnline should return false when node is offline (#222)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain authored Apr 19, 2024
1 parent 7266128 commit e9436ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/is-online.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ export function createIsOnline (client: HTTPRPCClient): KuboRPCClient['isOnline'
const id = createId(client)

return async function isOnline (options = {}) {
const res = await id(options)
try {
const res = await id(options)

return Boolean(res?.addresses?.length)
return Boolean(res?.addresses?.length)
} catch {
return false
}
}
}
28 changes: 28 additions & 0 deletions test/is-online.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { factory } from './utils/factory.js'
import type { KuboRPCClient } from '../src/index.js'

const f = factory()

describe('.isOnline', function () {
this.timeout(20 * 1000)

let ipfs: KuboRPCClient

before(async function () {
ipfs = (await f.spawn()).api
})

after(async function () { return f.clean() })

it('should return true when the node is online', async function () {
await expect(ipfs.isOnline()).to.eventually.be.true()
})

it('should return false when the node is offline', async function () {
await f.controllers[0].stop()
await expect(ipfs.isOnline()).to.eventually.be.false()
})
})

0 comments on commit e9436ca

Please sign in to comment.