Update SocMed Image

This commit is contained in:
2026-09-01 20:12:59 -03:00
parent 5f20e5d2b7
commit 1385efec70
9 changed files with 162 additions and 24 deletions
+21 -16
View File
@@ -67,8 +67,9 @@ const server = http.createServer(async (req, res) => {
const url = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
withSecurityHeaders(res, { contentSecurityPolicy: !isDiscoveryRoute(req, url) });
const baseUrl = publicBaseUrl(req);
const isPublicRead = isReadRequest(req);
if (req.method === 'GET' && url.pathname === '/') {
if (isPublicRead && url.pathname === '/') {
const nonce = crypto.randomBytes(16).toString('base64');
withSecurityHeaders(res, { scriptNonce: nonce });
return sendHtml(res, 200, renderIndex({
@@ -79,7 +80,7 @@ const server = http.createServer(async (req, res) => {
}));
}
if (req.method === 'GET' && (url.pathname === '/lore' || url.pathname === '/lore/')) {
if (isPublicRead && (url.pathname === '/lore' || url.pathname === '/lore/')) {
const nonce = crypto.randomBytes(16).toString('base64');
withSecurityHeaders(res, { scriptNonce: nonce });
return sendHtml(res, 200, renderIndex({
@@ -92,7 +93,7 @@ const server = http.createServer(async (req, res) => {
}
const memePageMatch = url.pathname.match(/^\/meme\/([a-f0-9]{64})\/?$/);
if (req.method === 'GET' && memePageMatch) {
if (isPublicRead && memePageMatch) {
const meme = store.get(memePageMatch[1]);
if (!meme || meme.status !== 'approved') {
return sendNotFoundPage(res, { baseUrl, pathname: url.pathname });
@@ -108,37 +109,37 @@ const server = http.createServer(async (req, res) => {
}));
}
if (req.method === 'GET' && url.pathname === '/openapi.json') {
if (isPublicRead && url.pathname === '/openapi.json') {
res.setHeader('Cache-Control', 'public, max-age=300');
return sendJson(res, 200, openApiSpecFor(baseUrl));
}
if (req.method === 'GET' && url.pathname === '/meme-api.skill.md') {
if (isPublicRead && url.pathname === '/meme-api.skill.md') {
res.setHeader('Cache-Control', 'public, max-age=300');
return sendText(res, 200, renderMemeApiSkill(baseUrl), 'text/markdown; charset=utf-8');
}
if (req.method === 'GET' && url.pathname === '/robots.txt') {
if (isPublicRead && url.pathname === '/robots.txt') {
return sendText(res, 200, robotsTxt(baseUrl));
}
if (req.method === 'GET' && url.pathname === '/llms.txt') {
if (isPublicRead && url.pathname === '/llms.txt') {
return sendText(res, 200, llmsTxt(baseUrl));
}
if (req.method === 'GET' && url.pathname === '/site.webmanifest') {
if (isPublicRead && url.pathname === '/site.webmanifest') {
return sendJson(res, 200, manifest(baseUrl));
}
if (req.method === 'GET' && url.pathname === '/favicon.ico') {
if (isPublicRead && url.pathname === '/favicon.ico') {
return sendFile(res, './public/assets/favicon/favicon.ico');
}
if (req.method === 'GET' && url.pathname === '/feed.json') {
if (isPublicRead && url.pathname === '/feed.json') {
return sendJson(res, 200, feedJson(baseUrl, store.listForReview({ status: 'approved' }).memes));
}
if (req.method === 'GET' && url.pathname === '/sitemap.xml') {
if (isPublicRead && url.pathname === '/sitemap.xml') {
const body = sitemapXml(baseUrl, store.listForReview({ status: 'approved' }).memes);
res.writeHead(200, {
'Content-Type': 'application/xml; charset=utf-8',
@@ -154,7 +155,7 @@ const server = http.createServer(async (req, res) => {
return sendFile(res, './public/admin.html');
}
if (req.method === 'GET' && url.pathname.startsWith('/assets/')) {
if (isPublicRead && url.pathname.startsWith('/assets/')) {
const isVersioned = url.searchParams.has('v') || FINGERPRINTED_ASSET.test(url.pathname);
res.setHeader('Cache-Control', isVersioned ? IMMUTABLE_CACHE_CONTROL : REVALIDATING_CACHE_CONTROL);
return sendFile(res, `./public${url.pathname}`);
@@ -256,7 +257,7 @@ const server = http.createServer(async (req, res) => {
}
const mediaMatch = url.pathname.match(/^\/media\/([a-f0-9]{64})$/);
if (req.method === 'GET' && mediaMatch) {
if (isPublicRead && mediaMatch) {
checkReadLimit(req, res, mediaReadLimiter);
const meme = store.get(mediaMatch[1]);
if (!meme || meme.status !== 'approved') return sendText(res, 404, 'Not found');
@@ -295,7 +296,7 @@ const server = http.createServer(async (req, res) => {
return sendFile(res, store.absolutePath(meme.storageKey), { absolute: true });
}
if (req.method === 'GET' && url.pathname === '/healthz') {
if (isPublicRead && url.pathname === '/healthz') {
noIndex(res);
return sendJson(res, 200, { ok: true });
}
@@ -330,12 +331,12 @@ function positiveInt(value, fallback) {
}
function isDiscoveryRoute(req, url) {
if (req.method !== 'GET') return false;
if (!isReadRequest(req)) return false;
return DISCOVERY_ROUTES.has(url.pathname);
}
function isBrowserPageRequest(req, url) {
if (req.method !== 'GET') return false;
if (!isReadRequest(req)) return false;
return ![
'/api',
'/assets',
@@ -346,6 +347,10 @@ function isBrowserPageRequest(req, url) {
].some((prefix) => url.pathname === prefix || url.pathname.startsWith(`${prefix}/`));
}
function isReadRequest(req) {
return req.method === 'GET' || req.method === 'HEAD';
}
function sendNotFoundPage(res, { baseUrl, pathname }) {
noIndex(res);
return sendHtml(res, 404, renderNotFound({