Skip to content

Commit dfbaaa6

Browse files
committed
fix: Change timeout: undefined to fall back to Fetch API default timeout
1 parent e44e6e6 commit dfbaaa6

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/adapters/config/http-config.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ describe("Config", () => {
7676
expect(request.signal.aborted).toBe(true);
7777
});
7878

79+
it("falls back to default timeout if timeout = undefined (#1097)", () => {
80+
jest.spyOn(AbortSignal, "timeout");
81+
82+
const config = new HttpConfigImpl(
83+
{
84+
url: "https://mastodon.social",
85+
accessToken: "token",
86+
},
87+
new SerializerNativeImpl(),
88+
);
89+
90+
const requestInit = config.mergeRequestInitWithDefaults();
91+
const request = new Request("https://example.com", requestInit);
92+
93+
expect(request.signal.aborted).toBe(false);
94+
expect(AbortSignal.timeout).not.toHaveBeenCalled();
95+
96+
jest.restoreAllMocks();
97+
});
98+
7999
it("resolves HTTP path", () => {
80100
const config = new HttpConfigImpl(
81101
{

src/adapters/config/http-config.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { type HttpConfig, type Serializer } from "../../interfaces";
22
import { mergeAbortSignals } from "./merge-abort-signals";
33
import { mergeHeadersInit } from "./merge-headers-init";
44

5-
const DEFAULT_TIMEOUT_MS = 1000 * 300;
6-
75
export interface MastoHttpConfigProps {
86
/**
97
* REST API URL for your Mastodon instance.
@@ -18,7 +16,9 @@ export interface MastoHttpConfigProps {
1816
readonly accessToken?: string;
1917

2018
/**
21-
* Timeout in milliseconds.
19+
* Timeout configuration
20+
*
21+
* - A number sets the timeout in milliseconds.
2222
*
2323
* Defaults to 1000 * 300 = 300 seconds.
2424
*/
@@ -64,10 +64,6 @@ export class HttpConfigImpl implements HttpConfig {
6464
return url;
6565
}
6666

67-
private createTimeout(): AbortSignal {
68-
return AbortSignal.timeout(this.props.timeout ?? DEFAULT_TIMEOUT_MS);
69-
}
70-
7167
private mergeHeadersWithDefaults(override: HeadersInit = {}): Headers {
7268
const headersInit = mergeHeadersInit([
7369
this.props.requestInit?.headers ?? {},
@@ -85,8 +81,11 @@ export class HttpConfigImpl implements HttpConfig {
8581
private mergeAbortSignalWithDefaults(
8682
signal?: AbortSignal | null,
8783
): AbortSignal {
88-
const timeout = this.createTimeout();
89-
const signals: AbortSignal[] = [timeout];
84+
const signals: AbortSignal[] = [];
85+
86+
if (this.props.timeout != undefined) {
87+
signals.push(AbortSignal.timeout(this.props.timeout));
88+
}
9089

9190
if (this.props.requestInit?.signal) {
9291
signals.push(this.props.requestInit.signal);

0 commit comments

Comments
 (0)