Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch timeout for external requests to small #881

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/api/src/router/location.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fetchWithTimeout } from "@homarr/common";
import type { z } from "@homarr/validation";
import { validation } from "@homarr/validation";

Expand All @@ -8,7 +9,7 @@ export const locationRouter = createTRPCRouter({
.input(validation.location.searchCity.input)
.output(validation.location.searchCity.output)
.query(async ({ input }) => {
const res = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`);
const res = await fetchWithTimeout(`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`);
return (await res.json()) as z.infer<typeof validation.location.searchCity.output>;
}),
});
3 changes: 2 additions & 1 deletion packages/api/src/router/widgets/weather.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { fetchWithTimeout } from "@homarr/common";
import { validation } from "@homarr/validation";

import { createTRPCRouter, publicProcedure } from "../../trpc";

export const weatherRouter = createTRPCRouter({
atLocation: publicProcedure.input(validation.widget.weather.atLocationInput).query(async ({ input }) => {
const res = await fetch(
const res = await fetchWithTimeout(
`https://api.open-meteo.com/v1/forecast?latitude=${input.latitude}&longitude=${input.longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min&current_weather=true&timezone=auto`,
);
const json: unknown = await res.json();
Expand Down
17 changes: 17 additions & 0 deletions packages/common/src/fetch-with-timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Same as fetch, but with a timeout of 10 seconds.
* https://stackoverflow.com/questions/46946380/fetch-api-request-timeout
* @param param0 fetch arguments
* @returns fetch response
*/
export const fetchWithTimeout = (...[url, requestInit]: Parameters<typeof fetch>) => {
const controller = new AbortController();

// 10 seconds timeout:
const timeoutId = setTimeout(() => controller.abort(), 10000);

return fetch(url, { signal: controller.signal, ...requestInit }).then((response) => {
clearTimeout(timeoutId);
Meierschlumpf marked this conversation as resolved.
Show resolved Hide resolved
return response;
});
};
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./url";
export * from "./number";
export * from "./error";
export * from "./encryption";
export * from "./fetch-with-timeout";
3 changes: 2 additions & 1 deletion packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@homarr/log": "workspace:^0.1.0"
"@homarr/log": "workspace:^0.1.0",
"@homarr/common": "workspace:^0.1.0"
},
"devDependencies": {
"@homarr/eslint-config": "workspace:^0.2.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/icons/src/repositories/github.icon-repository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchWithTimeout } from "@homarr/common";

import type { IconRepositoryLicense } from "../types/icon-repository-license";
import type { RepositoryIconGroup } from "../types/repository-icon-group";
import { IconRepository } from "./icon-repository";
Expand All @@ -19,7 +21,7 @@ export class GitHubIconRepository extends IconRepository {
throw new Error("Repository URLs are required for this repository");
}

const response = await fetch(this.repositoryIndexingUrl);
const response = await fetchWithTimeout(this.repositoryIndexingUrl);
const listOfFiles = (await response.json()) as GitHubApiResponse;

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchWithTimeout } from "@homarr/common";

import type { IconRepositoryLicense } from "../types/icon-repository-license";
import type { RepositoryIconGroup } from "../types/repository-icon-group";
import { IconRepository } from "./icon-repository";
Expand All @@ -15,7 +17,7 @@ export class JsdelivrIconRepository extends IconRepository {
}

protected async getAllIconsInternalAsync(): Promise<RepositoryIconGroup> {
const response = await fetch(this.repositoryIndexingUrl);
const response = await fetchWithTimeout(this.repositoryIndexingUrl);
const listOfFiles = (await response.json()) as JsdelivrApiResponse;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/ping/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { extractErrorMessage } from "@homarr/common";
import { extractErrorMessage, fetchWithTimeout } from "@homarr/common";
import { logger } from "@homarr/log";

export const sendPingRequestAsync = async (url: string) => {
try {
return await fetch(url).then((response) => ({ statusCode: response.status }));
return await fetchWithTimeout(url).then((response) => ({ statusCode: response.status }));
} catch (error) {
logger.error("packages/ping/src/index.ts:", error);
return {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading