From e9436ca5989741b253b502d356fabade884ca9e1 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 19 Apr 2024 12:29:02 +0100 Subject: [PATCH] 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. --- src/is-online.ts | 8 ++++++-- test/is-online.spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/is-online.spec.ts 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() + }) +})