diff --git a/src/is-online.ts b/src/is-online.ts index 612917405..e29d9a9af 100644 --- a/src/is-online.ts +++ b/src/is-online.ts @@ -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 + } } } diff --git a/test/is-online.spec.ts b/test/is-online.spec.ts new file mode 100644 index 000000000..ef84e9cc9 --- /dev/null +++ b/test/is-online.spec.ts @@ -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() + }) +})