fix "Can't set headers after they are sent to the client" error

This commit is contained in:
hexboy 2024-10-03 07:14:02 +03:30
parent d22e677683
commit 93bbb45d48
2 changed files with 14 additions and 4 deletions
src
downloader
index.ts

View file

@ -79,10 +79,14 @@ export class GotDownloader {
.head(srv.url + url, this.getOptions(srv, 'head'))
.then((r) => {
res.set(r.headers);
res.sendStatus(r.statusCode);
if (!res.headersSent) {
res.sendStatus(r.statusCode);
}
})
.catch((r: { statusCode?: number }) => {
res.sendStatus(r?.statusCode ?? 404);
if (!res.headersSent) {
res.sendStatus(r?.statusCode ?? 404);
}
});
};

View file

@ -57,10 +57,16 @@ const cacheRequestHandler: RequestHandler = (req, res, next) => {
downloader.download(url, srv, res);
}
} else {
res.sendStatus(403);
if (!res.headersSent) {
res.sendStatus(403);
}
}
})
.catch(() => res.sendStatus(403));
.catch(() => {
if (!res.headersSent) {
res.sendStatus(403);
}
});
};
// init cache dir