2022-07-24 21:16:57 +00:00
|
|
|
import express from "express";
|
|
|
|
import { Application, Request, Response, NextFunction } from 'express';
|
|
|
|
import * as http from 'http';
|
|
|
|
import config from './config';
|
|
|
|
import { Cluster } from 'puppeteer-cluster';
|
2022-08-02 00:37:54 +00:00
|
|
|
import ReusablePage from './concurrency/ReusablePage';
|
2022-08-02 21:02:33 +00:00
|
|
|
import { parseLanguageUrl } from './language/lang';
|
2022-07-24 21:16:57 +00:00
|
|
|
const puppeteerConfig = require('../puppeteer.config.json');
|
|
|
|
|
|
|
|
if (config.PUPPETEER.EXEC_PATH) {
|
|
|
|
puppeteerConfig.executablePath = config.PUPPETEER.EXEC_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Server {
|
|
|
|
private server: http.Server | undefined;
|
|
|
|
private app: Application;
|
|
|
|
cluster?: Cluster;
|
|
|
|
mempoolHost: string;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.app = express();
|
|
|
|
this.mempoolHost = config.MEMPOOL.HTTP_HOST + (config.MEMPOOL.HTTP_PORT ? ':' + config.MEMPOOL.HTTP_PORT : '');
|
|
|
|
this.startServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
async startServer() {
|
|
|
|
this.app
|
|
|
|
.use((req: Request, res: Response, next: NextFunction) => {
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
.use(express.urlencoded({ extended: true }))
|
|
|
|
.use(express.text())
|
|
|
|
;
|
|
|
|
|
|
|
|
this.cluster = await Cluster.launch({
|
2022-08-02 00:37:54 +00:00
|
|
|
concurrency: ReusablePage,
|
2022-07-24 21:16:57 +00:00
|
|
|
maxConcurrency: config.PUPPETEER.CLUSTER_SIZE,
|
|
|
|
puppeteerOptions: puppeteerConfig,
|
|
|
|
});
|
2022-07-26 20:47:03 +00:00
|
|
|
await this.cluster?.task(async (args) => { return this.clusterTask(args) });
|
2022-07-24 21:16:57 +00:00
|
|
|
|
|
|
|
this.setUpRoutes();
|
|
|
|
|
|
|
|
this.server = http.createServer(this.app);
|
|
|
|
|
|
|
|
this.server.listen(config.SERVER.HTTP_PORT, () => {
|
|
|
|
console.log(`Mempool Unfurl Server is running on port ${config.SERVER.HTTP_PORT}`);
|
|
|
|
});
|
2022-08-02 21:02:33 +00:00
|
|
|
|
|
|
|
this.initClusterPages();
|
2022-07-24 21:16:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 01:46:23 +00:00
|
|
|
async stopServer() {
|
|
|
|
if (this.cluster) {
|
|
|
|
await this.cluster.idle();
|
|
|
|
await this.cluster.close();
|
|
|
|
}
|
|
|
|
if (this.server) {
|
|
|
|
await this.server.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 21:16:57 +00:00
|
|
|
setUpRoutes() {
|
|
|
|
this.app.get('/render*', async (req, res) => { return this.renderPreview(req, res) })
|
|
|
|
this.app.get('*', (req, res) => { return this.renderHTML(req, res) })
|
|
|
|
}
|
|
|
|
|
2022-08-02 21:02:33 +00:00
|
|
|
async initClusterPages() {
|
|
|
|
for (let i = 0; i < config.PUPPETEER.CLUSTER_SIZE; i++) {
|
|
|
|
this.cluster?.execute({ action: 'init' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 00:37:54 +00:00
|
|
|
async clusterTask({ page, data: { url, path, action } }) {
|
2022-08-02 21:02:33 +00:00
|
|
|
if (action === 'init') {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-02 00:37:54 +00:00
|
|
|
try {
|
2022-08-02 21:02:33 +00:00
|
|
|
const urlParts = parseLanguageUrl(path);
|
|
|
|
if (page.language !== urlParts.lang) {
|
|
|
|
// switch language
|
|
|
|
page.language = urlParts.lang;
|
|
|
|
const localizedUrl = urlParts.lang ? `${this.mempoolHost}/${urlParts.lang}${urlParts.path}` : `${this.mempoolHost}${urlParts.path}` ;
|
|
|
|
await page.goto(localizedUrl, { waitUntil: "load" });
|
|
|
|
} else {
|
2022-08-02 00:37:54 +00:00
|
|
|
const loaded = await page.evaluate(async (path) => {
|
|
|
|
if (window['ogService']) {
|
|
|
|
window['ogService'].loadPage(path);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-07-27 18:13:37 +00:00
|
|
|
}
|
2022-08-02 21:02:33 +00:00
|
|
|
}, urlParts.path);
|
2022-08-02 00:37:54 +00:00
|
|
|
if (!loaded) {
|
|
|
|
throw new Error('failed to access open graph service');
|
2022-07-27 18:13:37 +00:00
|
|
|
}
|
2022-08-02 21:02:33 +00:00
|
|
|
}
|
2022-08-02 00:37:54 +00:00
|
|
|
|
2022-08-02 21:02:33 +00:00
|
|
|
if (action === 'screenshot') {
|
|
|
|
const waitForReady = await page.$('meta[property="og:preview:loading"]');
|
|
|
|
if (waitForReady != null) {
|
|
|
|
await page.waitForSelector('meta[property="og:preview:ready"]', { timeout: 3000 });
|
2022-07-27 00:55:17 +00:00
|
|
|
}
|
2022-08-02 21:02:33 +00:00
|
|
|
return page.screenshot();
|
|
|
|
} else if (action === 'html') {
|
|
|
|
await page.waitForSelector('meta[property="og:meta:ready"]', { timeout: 3000 });
|
|
|
|
return page.content();
|
2022-07-26 20:47:03 +00:00
|
|
|
}
|
2022-08-02 00:37:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`failed to render page for ${action}`, e instanceof Error ? e.message : e);
|
|
|
|
page.repairRequested = true;
|
2022-07-26 20:47:03 +00:00
|
|
|
}
|
2022-07-24 21:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async renderPreview(req, res) {
|
|
|
|
try {
|
2022-08-02 21:02:33 +00:00
|
|
|
const path = req.params[0]
|
2022-08-02 00:37:54 +00:00
|
|
|
const img = await this.cluster?.execute({ url: this.mempoolHost + path, path: path, action: 'screenshot' });
|
2022-07-24 21:16:57 +00:00
|
|
|
|
2022-08-02 00:37:54 +00:00
|
|
|
if (!img) {
|
|
|
|
throw new Error('failed to render preview image');
|
|
|
|
}
|
2022-08-02 21:02:33 +00:00
|
|
|
|
2022-07-24 21:16:57 +00:00
|
|
|
res.contentType('image/png');
|
|
|
|
res.send(img);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:47:03 +00:00
|
|
|
async renderHTML(req, res) {
|
2022-07-27 00:55:17 +00:00
|
|
|
// drop requests for static files
|
|
|
|
const path = req.params[0];
|
|
|
|
const match = path.match(/\.[\w]+$/);
|
|
|
|
if (match?.length && match[0] !== '.html') {
|
|
|
|
res.status(404).send();
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:47:03 +00:00
|
|
|
try {
|
2022-08-02 21:02:33 +00:00
|
|
|
let html = await this.cluster?.execute({ url: this.mempoolHost + path, path: path, action: 'html' });
|
2022-08-02 00:37:54 +00:00
|
|
|
if (!html) {
|
|
|
|
throw new Error('failed to render preview image');
|
|
|
|
}
|
|
|
|
res.send(html);
|
2022-07-26 20:47:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
2022-07-24 21:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = new Server();
|
2022-08-02 01:46:23 +00:00
|
|
|
|
|
|
|
process.on('SIGTERM', async () => {
|
|
|
|
console.info('Shutting down Mempool Unfurl Server');
|
|
|
|
await server.stopServer();
|
|
|
|
process.exit(0);
|
|
|
|
});
|