cardserver
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -12,9 +12,25 @@ #!/usr/bin/env node | ||
let chain = Promise.resolve() | ||
;(async () => { | ||
const app = express() | ||
const card = async (page, req, res) => { | ||
const host = req.hostname.replace('cards.', '') | ||
const url = `${req.protocol}://${host}/${req.params[0]}` | ||
const browser = await puppeteer.launch({ | ||
args: [ | ||
'--disable-accelerated-2d-canvas', | ||
'--disable-dev-shm-usage', | ||
'--disable-gpu', | ||
'--disable-setuid-sandbox', | ||
'--no-first-run', | ||
'--no-sandbox', | ||
'--no-zygote', | ||
'--single-process', | ||
], | ||
headless: true, | ||
}) | ||
chain = chain.then(async () => { | ||
app.enable('trust proxy') | ||
app.get(/^\/(.*)\.png$/, async (req, res) => { | ||
const host = req.hostname.replace('cards.', '') | ||
const url = `${req.protocol}://${host}/${req.params[0]}` | ||
const imagePath = `/tmp/cards/${host}/${req.params[0].replace('cards/', '')}.png` | ||
@@ -25,47 +41,38 @@ const imageExpired = !fs.existsSync(imagePath) || Date.parse(fs.statSync(imagePath).mtime) < new Date - maxage * 1000 | ||
if (!imageExpired) { | ||
return serve(fs.readFileSync(imagePath), res) | ||
} | ||
let image | ||
if (!fs.existsSync(path.dirname(imagePath))) { | ||
fs.mkdirSync(path.dirname(imagePath), { recursive: true }) | ||
} | ||
if (imageExpired) { | ||
const page = await browser.newPage() | ||
try { | ||
const result = await page.goto(url) | ||
await page.setViewport({ width, height }) | ||
await page.evaluateHandle('document.fonts.ready') | ||
try { | ||
const result = await page.goto(url) | ||
if (!result.ok()) { | ||
await page.evaluateHandle('document.fonts.ready') | ||
if (result.ok()) { | ||
fs.mkdirSync(path.dirname(imagePath), { recursive: true }) | ||
image = await page.screenshot({ path: imagePath }) | ||
} | ||
res.status(result.status()) | ||
return res.send() | ||
} catch (err) { | ||
res.status(502) | ||
} | ||
const screenshot = await page.screenshot({ path: imagePath }) | ||
page.close() | ||
} else { | ||
image = fs.readFileSync(imagePath) | ||
} | ||
serve(screenshot, res) | ||
} catch (err) { | ||
res.status(502) | ||
return res.send() | ||
if (image) { | ||
res.header('Cache-Control', 'public, max-age=' + maxage) | ||
res.header('Content-Type', 'image/png') | ||
res.header('Expires', new Date(Date.now() + maxage * 1000).toUTCString()) | ||
} | ||
return res.send(image) | ||
}) | ||
} | ||
const serve = (image, res) => { | ||
res.header('Cache-Control', 'public, max-age=' + maxage) | ||
res.header('Content-Type', 'image/png') | ||
res.header('Expires', new Date(Date.now() + maxage * 1000).toUTCString()) | ||
res.send(image) | ||
} | ||
(async () => { | ||
const app = express() | ||
const browser = await puppeteer.launch({ args: [ '--no-sandbox', `--window-size=${width},${height}` ] }) | ||
const page = await browser.newPage() | ||
await page.setViewport({ width: 1200, height: 630 }) | ||
app.enable('trust proxy') | ||
app.get(/^\/(.*)\.png$/, (req, res) => card(page, req, res)) | ||
if (app.listen(port)) { | ||
@@ -72,0 +79,0 @@ console.log('Card server started on port ' + port) |
{ | ||
"name": "cardserver", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "A simple social media card renderer written using Puppeteer – screenshot card-like webpages on-the-fly", | ||
@@ -15,3 +15,6 @@ "main": "cardserver.js", | ||
"puppeteer": "^5.3.0" | ||
}, | ||
"scripts": { | ||
"start": "node cardserver.js" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7981
66