mirror of
https://github.com/FlipsideCrypto/og-image.git
synced 2026-02-06 10:46:43 +00:00
- Upgrade Node 10 to Node 12 - Bump dependencies to the latest version - Fix a small typescript issue @Snazzyham mentioned #105 - Fix perf by using `setContent()` instead of writing a file each request per @ChristopherBiscardi, [suggested on twitter](https://twitter.com/chrisbiscardi/status/1239686555972718593)
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { IncomingMessage, ServerResponse } from 'http';
|
|
import { parseRequest } from './_lib/parser';
|
|
import { getScreenshot } from './_lib/chromium';
|
|
import { getHtml } from './_lib/template';
|
|
|
|
const isDev = process.env.NOW_REGION === 'dev1';
|
|
const isHtmlDebug = process.env.OG_HTML_DEBUG === '1';
|
|
|
|
export default async function handler(req: IncomingMessage, res: ServerResponse) {
|
|
try {
|
|
const parsedReq = parseRequest(req);
|
|
const html = getHtml(parsedReq);
|
|
if (isHtmlDebug) {
|
|
res.setHeader('Content-Type', 'text/html');
|
|
res.end(html);
|
|
return;
|
|
}
|
|
const { fileType } = parsedReq;
|
|
const file = await getScreenshot(html, fileType, isDev);
|
|
res.statusCode = 200;
|
|
res.setHeader('Content-Type', `image/${fileType}`);
|
|
res.setHeader('Cache-Control', `public, immutable, no-transform, s-maxage=31536000, max-age=31536000`);
|
|
res.end(file);
|
|
} catch (e) {
|
|
res.statusCode = 500;
|
|
res.setHeader('Content-Type', 'text/html');
|
|
res.end('<h1>Internal Error</h1><p>Sorry, there was a problem</p>');
|
|
console.error(e);
|
|
}
|
|
}
|