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;
|
2022-08-03 01:11:54 +00:00
|
|
|
network: string;
|
|
|
|
defaultImageUrl: string;
|
2022-07-24 21:16:57 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.app = express();
|
|
|
|
this.mempoolHost = config.MEMPOOL.HTTP_HOST + (config.MEMPOOL.HTTP_PORT ? ':' + config.MEMPOOL.HTTP_PORT : '');
|
2022-08-03 01:11:54 +00:00
|
|
|
this.network = config.MEMPOOL.NETWORK || 'bitcoin';
|
|
|
|
this.defaultImageUrl = this.getDefaultImageUrl();
|
2022-07-24 21:16:57 +00:00
|
|
|
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 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 00:37:54 +00:00
|
|
|
async clusterTask({ page, data: { url, path, action } }) {
|
|
|
|
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-03 01:11:54 +00:00
|
|
|
const waitForReady = await page.$('meta[property="og:preview:loading"]');
|
2022-08-12 16:34:41 +00:00
|
|
|
let success = true;
|
2022-08-03 01:11:54 +00:00
|
|
|
if (waitForReady != null) {
|
2022-08-12 16:34:41 +00:00
|
|
|
success = await Promise.race([
|
|
|
|
page.waitForSelector('meta[property="og:preview:ready"]', { timeout: config.PUPPETEER.RENDER_TIMEOUT || 3000 }).then(() => true),
|
|
|
|
page.waitForSelector('meta[property="og:preview:fail"]', { timeout: config.PUPPETEER.RENDER_TIMEOUT || 3000 }).then(() => false)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
return screenshot;
|
|
|
|
} else {
|
|
|
|
console.log(`failed to render page preview for ${action} due to client-side error. probably requested an invalid ID`);
|
|
|
|
page.repairRequested = true;
|
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) {
|
2022-08-12 16:34:41 +00:00
|
|
|
res.status(500).send('failed to render page preview');
|
|
|
|
} else {
|
|
|
|
res.contentType('image/png');
|
|
|
|
res.send(img);
|
2022-08-02 00:37:54 +00:00
|
|
|
}
|
2022-07-24 21:16:57 +00:00
|
|
|
} 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
|
2022-08-03 01:11:54 +00:00
|
|
|
const rawPath = req.params[0];
|
|
|
|
const match = rawPath.match(/\.[\w]+$/);
|
2022-07-27 00:55:17 +00:00
|
|
|
if (match?.length && match[0] !== '.html') {
|
|
|
|
res.status(404).send();
|
2022-08-03 01:11:54 +00:00
|
|
|
return;
|
2022-07-27 00:55:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 01:11:54 +00:00
|
|
|
let previewSupported = true;
|
|
|
|
let mode = 'mainnet'
|
|
|
|
let ogImageUrl = this.defaultImageUrl;
|
|
|
|
let ogTitle;
|
|
|
|
const { lang, path } = parseLanguageUrl(rawPath);
|
|
|
|
const parts = path.slice(1).split('/');
|
|
|
|
|
|
|
|
// handle network mode modifiers
|
|
|
|
if (['testnet', 'signet'].includes(parts[0])) {
|
|
|
|
mode = parts.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle supported preview routes
|
2022-08-11 17:19:12 +00:00
|
|
|
switch (parts[0]) {
|
|
|
|
case 'block':
|
|
|
|
ogTitle = `Block: ${parts[1]}`;
|
|
|
|
break;
|
|
|
|
case 'address':
|
|
|
|
ogTitle = `Address: ${parts[1]}`;
|
|
|
|
break;
|
2022-08-15 23:14:34 +00:00
|
|
|
case 'tx':
|
|
|
|
ogTitle = `Transaction: ${parts[1]}`;
|
|
|
|
break;
|
2022-08-11 17:19:12 +00:00
|
|
|
case 'lightning':
|
|
|
|
switch (parts[1]) {
|
|
|
|
case 'node':
|
|
|
|
ogTitle = `Lightning Node: ${parts[2]}`;
|
|
|
|
break;
|
|
|
|
case 'channel':
|
|
|
|
ogTitle = `Lightning Channel: ${parts[2]}`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
previewSupported = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
previewSupported = false;
|
2022-08-03 01:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (previewSupported) {
|
2022-08-11 17:42:29 +00:00
|
|
|
ogImageUrl = `${config.SERVER.HOST}/render/${lang || 'en'}/preview${path}`;
|
2022-08-03 01:11:54 +00:00
|
|
|
ogTitle = `${this.network ? capitalize(this.network) + ' ' : ''}${mode !== 'mainnet' ? capitalize(mode) + ' ' : ''}${ogTitle}`;
|
|
|
|
} else {
|
|
|
|
ogTitle = 'The Mempool Open Source Project™';
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(`
|
|
|
|
<!doctype html>
|
|
|
|
<html lang="en-US" dir="ltr">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>${ogTitle}</title>
|
|
|
|
<meta name="description" content="The Mempool Open Source Project™ - our self-hosted explorer for the ${capitalize(this.network)} community."/>
|
|
|
|
<meta property="og:image" content="${ogImageUrl}"/>
|
|
|
|
<meta property="og:image:type" content="image/png"/>
|
|
|
|
<meta property="og:image:width" content="${previewSupported ? 1200 : 1000}"/>
|
|
|
|
<meta property="og:image:height" content="${previewSupported ? 600 : 500}"/>
|
|
|
|
<meta property="og:title" content="${ogTitle}">
|
|
|
|
<meta property="twitter:card" content="summary_large_image">
|
|
|
|
<meta property="twitter:site" content="@mempool">
|
|
|
|
<meta property="twitter:creator" content="@mempool">
|
|
|
|
<meta property="twitter:title" content="${ogTitle}">
|
|
|
|
<meta property="twitter:description" content="Our self-hosted mempool explorer for the ${capitalize(this.network)} community."/>
|
|
|
|
<meta property="twitter:image:src" content="${ogImageUrl}"/>
|
|
|
|
<meta property="twitter:domain" content="mempool.space">
|
|
|
|
<body></body>
|
|
|
|
</html>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultImageUrl() {
|
|
|
|
switch (this.network) {
|
|
|
|
case 'liquid':
|
2022-08-03 16:43:47 +00:00
|
|
|
return this.mempoolHost + '/resources/liquid/liquid-network-preview.png';
|
2022-08-03 01:11:54 +00:00
|
|
|
case 'bisq':
|
2022-08-03 16:43:47 +00:00
|
|
|
return this.mempoolHost + '/resources/bisq/bisq-markets-preview.png';
|
2022-08-03 01:11:54 +00:00
|
|
|
default:
|
2022-08-03 16:43:47 +00:00
|
|
|
return this.mempoolHost + '/resources/mempool-space-preview.png';
|
2022-07-26 20:47:03 +00:00
|
|
|
}
|
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);
|
|
|
|
});
|
2022-08-03 01:11:54 +00:00
|
|
|
|
|
|
|
function capitalize(str) {
|
|
|
|
if (str && str.length) {
|
|
|
|
return str[0].toUpperCase() + str.slice(1);
|
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|