Proof-of-concept "sipper" minimal pages for web crawlers

This commit is contained in:
Mononaut
2023-08-24 17:49:27 +09:00
parent c51159d275
commit eeefaa6374
9 changed files with 661 additions and 44 deletions

View File

@@ -1,9 +1,19 @@
import fetch from 'node-fetch-commonjs';
import config from './config';
interface Match {
render: boolean;
title: string;
fallbackImg: string;
staticImg?: string;
networkMode: string;
params?: string[];
sip?: SipTemplate;
}
interface SipTemplate {
template: string;
getData: Function;
}
const routes = {
@@ -19,18 +29,37 @@ const routes = {
title: "Mempool Accelerator",
fallbackImg: '/resources/previews/accelerator.jpg',
},
address: {
render: true,
params: 1,
getTitle(path) {
return `Address: ${path[0]}`;
}
},
block: {
render: true,
params: 1,
getTitle(path) {
return `Block: ${path[0]}`;
},
sip: {
template: 'block',
async getData (params: string[]) {
if (params?.length) {
let blockId = params[0];
if (blockId.length !== 64) {
blockId = await (await fetch(config.API.ESPLORA + `/block-height/${blockId}`)).text();
}
const [block, transactions] = await Promise.all([
(await fetch(config.API.MEMPOOL + `/block/${blockId}`)).json(),
(await fetch(config.API.ESPLORA + `/block/${blockId}/txids`)).json()
])
return {
block,
transactions,
};
}
}
}
},
address: {
render: true,
params: 1,
getTitle(path) {
return `Address: ${path[0]}`;
}
},
blocks: {
@@ -162,7 +191,7 @@ const networks = {
}
};
export function matchRoute(network: string, path: string): Match {
export function matchRoute(network: string, path: string, matchFor: string = 'render'): Match {
const match: Match = {
render: false,
title: '',
@@ -183,7 +212,7 @@ export function matchRoute(network: string, path: string): Match {
match.fallbackImg = route.fallbackImg;
// traverse the route tree until we run out of route or tree, or hit a renderable match
while (route.routes && parts.length && route.routes[parts[0]]) {
while (!route[matchFor] && route.routes && parts.length && route.routes[parts[0]]) {
route = route.routes[parts[0]];
parts.shift();
if (route.fallbackImg) {
@@ -192,8 +221,10 @@ export function matchRoute(network: string, path: string): Match {
}
// enough route parts left for title & rendering
if (route.render && parts.length >= route.params) {
match.render = true;
if (route[matchFor] && parts.length >= route.params) {
match.render = route.render;
match.sip = route.sip;
match.params = parts;
}
// only use set a static image for exact matches
if (!parts.length && route.staticImg) {