Update server.js
This commit is contained in:
16
server.js
16
server.js
@@ -71,9 +71,7 @@ function isAborted(req, res) {
|
|||||||
|
|
||||||
function sendError(res, status, code, message, requestId) {
|
function sendError(res, status, code, message, requestId) {
|
||||||
if (res.headersSent) {
|
if (res.headersSent) {
|
||||||
try {
|
try { res.end(); } catch {}
|
||||||
res.end();
|
|
||||||
} catch {}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.status(status).json({ error: code, message, requestId });
|
res.status(status).json({ error: code, message, requestId });
|
||||||
@@ -213,7 +211,6 @@ async function heicToJpeg(input, opts) {
|
|||||||
|
|
||||||
const { width, height, rgba } = await heifDisplayToRGBA(imgs[0]);
|
const { width, height, rgba } = await heifDisplayToRGBA(imgs[0]);
|
||||||
|
|
||||||
// IMPORTANT: feed Sharp raw pixel metadata so it doesn't treat the buffer as an encoded image
|
|
||||||
return normalizeForVision(Buffer.from(rgba), {
|
return normalizeForVision(Buffer.from(rgba), {
|
||||||
...opts,
|
...opts,
|
||||||
raw: { width, height, channels: 4 },
|
raw: { width, height, channels: 4 },
|
||||||
@@ -231,13 +228,11 @@ async function pdfFirstPageToJpeg(input, opts) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.writeFile(pdf, input);
|
await fs.writeFile(pdf, input);
|
||||||
|
|
||||||
await execFilePromise(
|
await execFilePromise(
|
||||||
"pdftoppm",
|
"pdftoppm",
|
||||||
["-jpeg", "-singlefile", "-r", String(opts.pdfDpi), pdf, `/tmp/${id}`],
|
["-jpeg", "-singlefile", "-r", String(opts.pdfDpi), pdf, `/tmp/${id}`],
|
||||||
DEFAULT_REQ_TIMEOUT_PDF_MS
|
DEFAULT_REQ_TIMEOUT_PDF_MS
|
||||||
);
|
);
|
||||||
|
|
||||||
const buf = await fs.readFile(out);
|
const buf = await fs.readFile(out);
|
||||||
return normalizeForVision(buf, opts);
|
return normalizeForVision(buf, opts);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -255,9 +250,10 @@ let convertInflight = 0;
|
|||||||
|
|
||||||
async function withConvertSingleFlight(req, res, fn) {
|
async function withConvertSingleFlight(req, res, fn) {
|
||||||
if (convertInflight >= MAX_CONVERT_INFLIGHT) {
|
if (convertInflight >= MAX_CONVERT_INFLIGHT) {
|
||||||
|
res.setHeader("Retry-After", "1");
|
||||||
return sendError(
|
return sendError(
|
||||||
res,
|
res,
|
||||||
503,
|
429,
|
||||||
"busy",
|
"busy",
|
||||||
"Converter busy; retry shortly",
|
"Converter busy; retry shortly",
|
||||||
req.requestId
|
req.requestId
|
||||||
@@ -276,7 +272,7 @@ async function withConvertSingleFlight(req, res, fn) {
|
|||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
app.post("/convert", async (req, res) => {
|
app.post("/convert", async (req, res) => {
|
||||||
// Encourage quick socket turnover; Fly will still manage concurrency.
|
// Encourage quick socket turnover
|
||||||
res.setHeader("Connection", "close");
|
res.setHeader("Connection", "close");
|
||||||
|
|
||||||
return withConvertSingleFlight(req, res, async () => {
|
return withConvertSingleFlight(req, res, async () => {
|
||||||
@@ -361,9 +357,7 @@ function clampInt(v, min, max, fallback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function safeUnlink(p) {
|
async function safeUnlink(p) {
|
||||||
try {
|
try { await fs.unlink(p); } catch {}
|
||||||
await fs.unlink(p);
|
|
||||||
} catch {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|||||||
Reference in New Issue
Block a user