Skip to content

Commit

Permalink
Fixed download timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Dec 1, 2023
1 parent d3cf414 commit 7a6d9c9
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 102 deletions.
33 changes: 26 additions & 7 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ import { RecoverableError } from "./task_manager.ts";
export type GID = [number, string];

export class Client {
cookies;
host;
ua;
cfg;
get cookies() {
return this.cfg.cookies;
}
get host() {
return this.cfg.ex ? "exhentai.org" : "e-hentai.org";
}
get ua() {
return this.cfg.ua ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36";
}
get timeout() {
return this.cfg.fetch_timeout;
}
signal;
constructor(cfg: Config, signal?: AbortSignal) {
this.cookies = cfg.cookies;
this.ua = cfg.ua ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36";
this.host = cfg.ex ? "exhentai.org" : "e-hentai.org";
this.cfg = cfg;
this.signal = signal;
}
get(
Expand Down Expand Up @@ -94,13 +102,24 @@ export class Client {
if (!d.signal && this.signal) {
d.signal = this.signal;
}
const osignal = d.signal;
const abortController = new AbortController();
const timeout = setTimeout(() => {
abortController.abort();
}, this.timeout);
osignal?.addEventListener("abort", () => {
abortController.abort(osignal?.reason);
});
d.signal = abortController.signal;
try {
return fetch(url, d);
} catch (e) {
if (e instanceof TypeError) {
throw new RecoverableError(e.message, { cause: e.cause });
}
throw e;
} finally {
clearTimeout(timeout);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type ConfigType = {
meili_hosts?: Record<string, string>;
cors_credentials_hosts: Array<string>;
flutter_frontend?: string;
fetch_timeout: number;
download_timeout: number;
};

export enum ThumbnailMethod {
Expand Down Expand Up @@ -164,6 +166,12 @@ export class Config {
get flutter_frontend() {
return this._return_string("flutter_frontend");
}
get fetch_timeout() {
return this._return_number("fetch_timeout") || 10000;
}
get download_timeout() {
return this._return_number("download_timeout") || 10000;
}
to_json(): ConfigType {
return {
cookies: typeof this.cookies === "string",
Expand All @@ -190,6 +198,8 @@ export class Config {
meili_hosts: this.meili_hosts,
cors_credentials_hosts: this.cors_credentials_hosts,
flutter_frontend: this.flutter_frontend,
fetch_timeout: this.fetch_timeout,
download_timeout: this.download_timeout,
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function gen_response<T>(
status = (d.status >= 400 && d.status < 600) ? d.status : 400;
}
const h = new Headers(headers);
h.set("Content-Type", "application/json");
h.set("Content-Type", "application/json; charset=UTF-8");
return new Response(JSON.stringify(d), { status, headers: h });
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export function gen_error<T = unknown>(
export function return_json<T = unknown>(data: T, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json; chatset=UTF-8" },
});
}

Expand Down
Loading

0 comments on commit 7a6d9c9

Please sign in to comment.