2022-08-04 21:27:02 +00:00
|
|
|
import { IncomingMessage, ServerResponse } from 'http'
|
|
|
|
import { parseRequest } from './_lib/parser'
|
|
|
|
import { getScreenshot } from './_lib/chromium'
|
|
|
|
import { getHtml } from './_lib/template'
|
|
|
|
import { getChallengeHtml } from './_lib/challenge-template'
|
2022-01-07 20:07:38 +00:00
|
|
|
|
2022-08-04 21:27:02 +00:00
|
|
|
const isDev = !process.env.AWS_REGION
|
|
|
|
const isHtmlDebug = process.env.OG_HTML_DEBUG === '1'
|
2022-01-07 20:07:38 +00:00
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: IncomingMessage,
|
|
|
|
res: ServerResponse
|
|
|
|
) {
|
|
|
|
try {
|
2022-08-04 21:27:02 +00:00
|
|
|
const parsedReq = parseRequest(req)
|
|
|
|
let html = getHtml(parsedReq)
|
|
|
|
if (parsedReq.challengerOutcome) html = getChallengeHtml(parsedReq)
|
2022-01-07 20:07:38 +00:00
|
|
|
if (isHtmlDebug) {
|
2022-08-04 21:27:02 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html')
|
|
|
|
res.end(html)
|
|
|
|
return
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
2022-08-04 21:27:02 +00:00
|
|
|
const { fileType } = parsedReq
|
|
|
|
const file = await getScreenshot(html, fileType, isDev)
|
|
|
|
res.statusCode = 200
|
|
|
|
res.setHeader('Content-Type', `image/${fileType}`)
|
2022-01-07 20:07:38 +00:00
|
|
|
res.setHeader(
|
2022-08-04 21:27:02 +00:00
|
|
|
'Cache-Control',
|
2022-01-07 20:07:38 +00:00
|
|
|
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`
|
2022-08-04 21:27:02 +00:00
|
|
|
)
|
|
|
|
res.end(file)
|
2022-01-07 20:07:38 +00:00
|
|
|
} catch (e) {
|
2022-08-04 21:27:02 +00:00
|
|
|
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)
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
}
|