import * as cm from 'cache-manager'; import fastq from 'fastq'; import { Hono } from 'hono'; import { cors } from 'hono/cors'; import { marked } from 'marked'; import { poweredBy } from 'hono/powered-by'; import { prettyJSON } from 'hono/pretty-json'; import { serveStatic } from 'hono/serve-static'; import Quote from './core/index.js'; import getClientIP from './utils/getClientIp.js'; const app = new Hono(); const defaultTtl = 60 * 60 * 1000; const inflight = new Map(); const cache = cm.createCache({ ttl: defaultTtl }); const queue = fastq.promise(async ({ key, c, body }) => { try { const data = await Quote(body); const result = c.json(data); await cache.set(key, result); return result; } finally { inflight.delete(key); } }, 1); app.use('*', cors({ origin: '*', allowMethods: ['GET', 'POST', 'OPTIONS'], allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'], })); app.use(poweredBy({ serverName: 'Hisoka Labs' })); app.use(prettyJSON({ space: 3 })); app.use( serveStatic({ root: './public', getContent: async (file, c) => { const res = Bun.file(file); if (await res.exists()) { const data = await res.arrayBuffer(); const mime = res.type; return c.body(data, 200, { 'Content-Type': mime }); } }, }), ); app.use(async (c, next) => { var colorStatus = status => { switch ((status / 100) | 0) { case 5: return `\x1B[31m${status}\x1B[0m`; case 4: return `\x1B[33m${status}\x1B[0m`; case 3: return `\x1B[36m${status}\x1B[0m`; case 2: return `\x1B[32m${status}\x1B[0m`; } return `${status}`; }; const start = Date.now(); const ipClient = getClientIP(c); await next(); console.log( `${ipClient} ${c.req.method} ${c.req.path + new URL(c.req.url).search} ${colorStatus(c.res.status)} ${Date.now() - start}ms`, ); }); app.get('/', async c => { const res = Bun.file('README.md'); const text = await res.text(); const html = marked.parse(text); return c.html(`