katex_server: Fix implicit use of any.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-05-03 17:24:37 -07:00 committed by Tim Abbott
parent 7d3877f69a
commit 3c06c899c5
1 changed files with 13 additions and 10 deletions

View File

@ -73,36 +73,39 @@ app.use((ctx, _next) => {
ctx.status = 404; ctx.status = 404;
return; return;
} }
if (ctx.request.body === undefined) { const body: unknown = ctx.request.body;
if (typeof body !== "object" || body === null) {
ctx.status = 400; ctx.status = 400;
ctx.type = "text/plain"; ctx.type = "text/plain";
ctx.body = "Missing POST body"; ctx.body = "Missing POST body";
return; return;
} }
const given_secret = ctx.request.body.shared_secret; if (
if (typeof given_secret !== "string" || !compare_secret(given_secret)) { !("shared_secret" in body) ||
typeof body.shared_secret !== "string" ||
!compare_secret(body.shared_secret)
) {
ctx.status = 403; ctx.status = 403;
ctx.type = "text/plain"; ctx.type = "text/plain";
ctx.body = "Invalid 'shared_secret' argument"; ctx.body = "Invalid 'shared_secret' argument";
return; return;
} }
const content = ctx.request.body.content; const is_display = "is_display" in body && body.is_display === "true";
const is_display = ctx.request.body.is_display === "true";
if (typeof content !== "string") { if (!("content" in body) || typeof body.content !== "string") {
ctx.status = 400; ctx.status = 400;
ctx.type = "text/plain"; ctx.type = "text/plain";
ctx.body = "Invalid 'content' argument"; ctx.body = "Invalid 'content' argument";
return; return;
} }
const content = body.content;
httpRequestSizeBytes.labels(String(is_display)).observe(Buffer.byteLength(content, "utf8")); httpRequestSizeBytes.labels(String(is_display)).observe(Buffer.byteLength(content, "utf8"));
try { try {
ctx.body = katex.renderToString(content, {displayMode: is_display}); const output = katex.renderToString(content, {displayMode: is_display});
httpResponseSizeBytes ctx.body = output;
.labels(String(is_display)) httpResponseSizeBytes.labels(String(is_display)).observe(Buffer.byteLength(output, "utf8"));
.observe(Buffer.byteLength(ctx.body, "utf8"));
} catch (error) { } catch (error) {
if (error instanceof katex.ParseError) { if (error instanceof katex.ParseError) {
ctx.status = 400; ctx.status = 400;