Fix sending timeout

This commit is contained in:
2022-03-12 15:55:15 +03:00
parent 1fad9f9525
commit dd4959ff85

View File

@@ -67,9 +67,12 @@ export async function downloadFromCache(bookId: number, fileType: string): Promi
}); });
return new Promise<DownloadedFile | null>((resolve, reject) => { return new Promise<DownloadedFile | null>((resolve, reject) => {
readStream.on("response", async (response: Response) => { let timeout: NodeJS.Timeout | null = null;
const resolver = async (response: Response) => {
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
resolve(null); resolve(null);
if (timeout) clearTimeout(timeout);
return return
} }
@@ -77,11 +80,20 @@ export async function downloadFromCache(bookId: number, fileType: string): Promi
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?'); if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
if (timeout) clearTimeout(timeout);
return resolve({ return resolve({
source: readStream, source: readStream,
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1], filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
caption: decode(captionData), caption: decode(captionData),
}) })
}); }
timeout = setTimeout(() => {
readStream.off("response", resolver);
resolve(null);
}, 60_000);
readStream.on("response", resolver);
}); });
} }