From b9613cbb2d6f70471cf723b21d49419816345b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20Ku=CC=88hnel?= Date: Sat, 14 Nov 2026 21:36:50 +0100 Subject: [PATCH 1/2] feat: optional timeout for requests to the bank HttpClient.timeoutMs: when set, every request gets an AbortSignal.timeout, so a bank that doesn't answer can't hang a dialog. Unset by default, so nothing changes for existing callers. --- src/httpClient.ts | 9 ++++++++ src/tests/httpClientTimeout.test.ts | 34 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/tests/httpClientTimeout.test.ts diff --git a/src/httpClient.ts b/src/httpClient.ts index 174c623..4c75e56 100644 --- a/src/httpClient.ts +++ b/src/httpClient.ts @@ -1,6 +1,13 @@ import { type CustomerMessage, type CustomerOrderMessage, Message } from './message.js'; export class HttpClient { + /** + * How long to wait for the bank's answer to one message, in milliseconds. Unset (the + * default) waits as long as the connection lasts; set it so that a bank that doesn't + * answer can't keep a dialog (and the caller) waiting forever. + */ + static timeoutMs: number | undefined = undefined; + constructor( public url: string, public debug = false, @@ -25,6 +32,8 @@ export class HttpClient { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body: requestBuffer.toString('base64'), + signal: + HttpClient.timeoutMs === undefined ? undefined : AbortSignal.timeout(HttpClient.timeoutMs), }); if (response.ok) { diff --git a/src/tests/httpClientTimeout.test.ts b/src/tests/httpClientTimeout.test.ts new file mode 100644 index 0000000..efd9c31 --- /dev/null +++ b/src/tests/httpClientTimeout.test.ts @@ -0,0 +1,34 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { HttpClient } from '../httpClient.js'; +import type { CustomerMessage } from '../message.js'; + +const message = { + encode: () => "HNHBK:1:3'", + toString: () => 'HNHBK', +} as unknown as CustomerMessage; + +describe('HttpClient.timeoutMs', () => { + afterEach(() => { + vi.unstubAllGlobals(); + HttpClient.timeoutMs = undefined; + }); + + it('waits without a timeout unless one is set', async () => { + const fetchMock = vi.fn().mockRejectedValue(new Error('offline')); + vi.stubGlobal('fetch', fetchMock); + await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow( + 'offline', + ); + expect(fetchMock.mock.calls[0][1].signal).toBeUndefined(); + }); + + it('gives every request the timeout that was set', async () => { + const fetchMock = vi.fn().mockRejectedValue(new DOMException('timed out', 'TimeoutError')); + vi.stubGlobal('fetch', fetchMock); + HttpClient.timeoutMs = 30_000; + await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow( + 'timed out', + ); + expect(fetchMock.mock.calls[0][1].signal).toBeInstanceOf(AbortSignal); + }); +}); From 190fcb7dc95e6bcc086ed1bcc054c21e05da0778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20Ku=CC=88hnel?= Date: Sat, 14 Nov 2026 21:41:20 +0100 Subject: [PATCH 2/2] docs: describe HttpClient.timeoutMs in the README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 9264dfb..5975d9e 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,18 @@ config.debugEnabled = true; This will print out all sent messages and received responses to the console in a structured format. +### Timeout for requests to the bank + +By default a request waits for the bank's answer as long as the connection lasts. To give up on a bank that doesn't answer, set a timeout in milliseconds; it applies to every request of every client: + +```typescript +import { HttpClient } from 'lib-fints'; + +HttpClient.timeoutMs = 30_000; +``` + +A request that takes longer is aborted with a `TimeoutError`. + ## Supported Transactions The following table shows all transactions supported by the FinTSClient interface: