cohost/serverside/prngnouns.ts

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-04-14 00:02:29 -04:00
// TODO: randomly pick color
2023-11-04 14:11:47 -04:00
import { contentType } from "https://deno.land/std@0.202.0/media_types/mod.ts";
import {
Image,
TextLayout,
} from "https://deno.land/x/imagescript@1.2.15/ImageScript.js";
const font = await fetch("https://static.pyrope.net/courier-std-bold.otf")
.then((r) => r.arrayBuffer())
.then((b) => new Uint8Array(b));
2024-03-24 20:34:58 -04:00
function make_image(
{ text, color = 0, outline_color = 0xff_ff_ff_ff }: {
text: string;
color?: number;
outline_color?: number;
},
): Image {
2023-11-04 14:11:47 -04:00
let image = Image.renderText(
font,
64,
2024-03-24 20:34:58 -04:00
text,
2023-11-04 14:11:47 -04:00
0,
new TextLayout({ verticalAlign: "center", horizontalAlign: "middle" }),
);
const outline = Image.renderText(
font,
64,
2024-03-24 20:34:58 -04:00
text,
outline_color,
2023-11-04 14:11:47 -04:00
new TextLayout({ verticalAlign: "center", horizontalAlign: "middle" }),
);
for (const hshift of [-2, 0, 2]) {
for (const vshift of [-2, 0, 2]) {
image = image.composite(outline, hshift, vshift);
}
}
2024-03-24 20:34:58 -04:00
const text_img = Image.renderText(
2023-11-04 14:11:47 -04:00
font,
64,
2024-03-24 20:34:58 -04:00
text,
color,
2023-11-04 14:11:47 -04:00
new TextLayout({ verticalAlign: "center", horizontalAlign: "middle" }),
);
2024-03-24 20:34:58 -04:00
const final = image.composite(text_img);
2023-11-04 14:11:47 -04:00
return final.crop(0, 0, final.width, final.height - 22);
}
// the whole cache thing def sketches me out, but people probably won't be *that* malicious
2024-02-24 22:39:17 -05:00
const MAX_PRN_LENGTH = 128;
2023-11-04 14:11:47 -04:00
// shouldn't really be a problem
2024-02-24 20:40:21 -05:00
const MAX_PRN_CHOICES = 256;
2023-11-04 14:11:47 -04:00
2024-03-24 20:34:58 -04:00
// let's stick to hex even though BigInt() can handle prefixes (prefices?)
const hex_string_to_color = (s: string): number | undefined => {
const parsed = parseInt(s, 16);
if (Number.isNaN(parsed)) return;
// JANK! SORRY
return Number((BigInt(parsed) << 8n) | 0xffn);
};
2023-11-04 14:11:47 -04:00
const image_response = (data: Uint8Array): Response =>
new Response(data, {
headers: { "Content-Type": contentType("png") },
});
Deno.serve({ port: 61265 }, async (req) => {
2024-03-24 20:34:58 -04:00
const url = new URL(req.url);
const params = url.searchParams;
const prns = params.getAll("p");
2023-11-04 14:11:47 -04:00
if (prns.some((p) => p.length > MAX_PRN_LENGTH)) {
return new Response(`MAX_PRN_LENGTH = ${MAX_PRN_LENGTH}`, { status: 413 });
}
if (prns.length > MAX_PRN_CHOICES) {
return new Response(`MAX_PRN_CHOICES = ${MAX_PRN_CHOICES}`, {
status: 413,
});
}
const prn = prns[Math.floor(Math.random() * prns.length)] ??
"NONE, APPARENTLY";
2024-03-24 20:34:58 -04:00
const fg_str = params.get("fg");
const outline_str = params.get("outline");
const sentence_id = params.get("sentence_id");
const sentence_index = params.get("sentence_index");
console.log(
`${sentence_id}#${sentence_index}: chose ${prn} from ${prns}. ${fg_str} on ${outline_str}`,
);
const fg = hex_string_to_color(fg_str ?? "0");
const outline = hex_string_to_color(outline_str ?? "ffffff");
const resp = image_response(
await make_image({ text: prn, color: fg, outline_color: outline }).encode(),
);
2023-11-04 14:11:47 -04:00
return resp;
});