import { Hono } from 'hono' const app = new Hono() const joypixels = require("emoji-toolkit"); import { mkdirSync, writeFileSync, existsSync, exists, readFileSync } from 'fs' import { $ } from 'bun'; app.post('/render', async (c) => { const auth = c.req.header('Authorization') if (auth !== process.env.PASSWORD) { return c.text('Unauthorized', 401) } const body = await c.req.json().catch(() => null) if (!body?.emoji) { return c.text('Missing emoji in body', 400) } let extraArgs = "-b" if(body?.extraArgs) { if (typeof body.extraArgs !== 'string') { return c.text('extraArgs must be a string', 400) } extraArgs = body.extraArgs; } const emojiHtml = joypixels.toImage(body.emoji) const urlMatch = emojiHtml.match(/src="([^"]*)"/) if (!urlMatch) { return c.text('Could not extract emoji URL', 500) } const emojiUrl = urlMatch[1] const filename = "cache/" + emojiUrl.split('/').pop() ; if(!existsSync(filename)) { // Download the image const res = await fetch(emojiUrl) if (!res.ok) { return c.text('Failed to download emoji image', 500) } const arrayBuffer = await res.arrayBuffer() const buffer = Buffer.from(arrayBuffer) // Ensure cache directory exists if (!existsSync('cache')) { mkdirSync('cache') } // Get filename from URL writeFileSync(filename, buffer) } if(!existsSync(filename+".bimg")) { await $`sanjuuni --disable-opencl ${extraArgs} -i ${filename} -o ${filename+".bimg"}` } const bimg = readFileSync(filename + ".bimg", "utf8"); return c.text(bimg) }) export default { port: 7427, host: "127.0.0.1", fetch: app.fetch, }