Skip to content

Commit

Permalink
fix: Change timeout: undefined to fall back to Fetch API default ti…
Browse files Browse the repository at this point in the history
…meout
  • Loading branch information
neet committed Apr 27, 2024
1 parent e44e6e6 commit dfbaaa6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/adapters/config/http-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ describe("Config", () => {
expect(request.signal.aborted).toBe(true);
});

it("falls back to default timeout if timeout = undefined (#1097)", () => {
jest.spyOn(AbortSignal, "timeout");

const config = new HttpConfigImpl(
{
url: "https://mastodon.social",
accessToken: "token",
},
new SerializerNativeImpl(),
);

const requestInit = config.mergeRequestInitWithDefaults();
const request = new Request("https://example.com", requestInit);

expect(request.signal.aborted).toBe(false);
expect(AbortSignal.timeout).not.toHaveBeenCalled();

jest.restoreAllMocks();
});

it("resolves HTTP path", () => {
const config = new HttpConfigImpl(
{
Expand Down
17 changes: 8 additions & 9 deletions src/adapters/config/http-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { type HttpConfig, type Serializer } from "../../interfaces";
import { mergeAbortSignals } from "./merge-abort-signals";
import { mergeHeadersInit } from "./merge-headers-init";

const DEFAULT_TIMEOUT_MS = 1000 * 300;

export interface MastoHttpConfigProps {
/**
* REST API URL for your Mastodon instance.
Expand All @@ -18,7 +16,9 @@ export interface MastoHttpConfigProps {
readonly accessToken?: string;

/**
* Timeout in milliseconds.
* Timeout configuration
*
* - A number sets the timeout in milliseconds.
*
* Defaults to 1000 * 300 = 300 seconds.
*/
Expand Down Expand Up @@ -64,10 +64,6 @@ export class HttpConfigImpl implements HttpConfig {
return url;
}

private createTimeout(): AbortSignal {
return AbortSignal.timeout(this.props.timeout ?? DEFAULT_TIMEOUT_MS);
}

private mergeHeadersWithDefaults(override: HeadersInit = {}): Headers {
const headersInit = mergeHeadersInit([
this.props.requestInit?.headers ?? {},
Expand All @@ -85,8 +81,11 @@ export class HttpConfigImpl implements HttpConfig {
private mergeAbortSignalWithDefaults(
signal?: AbortSignal | null,
): AbortSignal {
const timeout = this.createTimeout();
const signals: AbortSignal[] = [timeout];
const signals: AbortSignal[] = [];

if (this.props.timeout != undefined) {
signals.push(AbortSignal.timeout(this.props.timeout));
}

if (this.props.requestInit?.signal) {
signals.push(this.props.requestInit.signal);
Expand Down

0 comments on commit dfbaaa6

Please sign in to comment.