-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
196 lines (165 loc) · 7.29 KB
/
server.mjs
File metadata and controls
196 lines (165 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { createServer } from "http";
import { ImageResponse } from "@vercel/og";
import { readFileSync, existsSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { parse } from "node-html-parser";
import { resolveTemplate, resolveTemplateById, loadLayout, parseMeta, loadAssets, fetchRemoteAssets, listTemplates, isDomainAllowed } from "./lib/templates.mjs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const fonts = [
{ name: "Figtree", data: readFileSync(join(__dirname, "fonts/Figtree-Regular.ttf")), weight: 400, style: "normal" },
{ name: "Figtree", data: readFileSync(join(__dirname, "fonts/Figtree-SemiBold.ttf")), weight: 600, style: "normal" },
{ name: "Figtree", data: readFileSync(join(__dirname, "fonts/Figtree-Bold.ttf")), weight: 700, style: "normal" },
{ name: "Instrument Sans", data: readFileSync(join(__dirname, "fonts/InstrumentSans-Regular.ttf")), weight: 400, style: "normal" },
];
const SIZES = {
og: { width: 1200, height: 630 },
portrait: { width: 1080, height: 1350 },
square: { width: 1080, height: 1080 },
};
const formHTML = readFileSync(join(__dirname, "public/generate-opengraph/index.html"), "utf-8");
const server = createServer(async (req, res) => {
const url = new URL(req.url, "http://localhost");
if (url.pathname === "/generate-opengraph" || url.pathname === "/generate-opengraph/") {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(formHTML);
return;
}
if (url.pathname === "/generate-opengraph/templates") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(listTemplates()));
return;
}
if (url.pathname === "/generate-opengraph/debug") {
const targetUrl = url.searchParams.get("url");
if (!targetUrl) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Missing ?url= parameter" }));
return;
}
const debug = { inputUrl: targetUrl, steps: [] };
let finalUrl = targetUrl;
let meta = {};
try {
const pageRes = await fetch(targetUrl);
finalUrl = pageRes.url || targetUrl;
debug.finalUrl = finalUrl;
debug.redirected = finalUrl !== targetUrl;
if (debug.redirected) {
debug.steps.push(`Followed redirect: ${targetUrl} → ${finalUrl}`);
} else {
debug.steps.push(`No redirect — final URL is same as input`);
}
const html = await pageRes.text();
const site = parse(html);
meta = parseMeta(site);
debug.meta = meta;
debug.steps.push(`Parsed ${Object.keys(meta).length} meta tags from page`);
} catch (err) {
debug.error = err.message;
debug.steps.push(`Failed to fetch URL: ${err.message}`);
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify(debug));
return;
}
let parsedUrl;
try { parsedUrl = new URL(finalUrl); } catch { parsedUrl = null; }
const allTemplates = listTemplates();
debug.availableTemplates = allTemplates.map((t) => ({ id: t.id, domain: t.domain }));
const { templateDir, layoutName, config } = resolveTemplate(finalUrl);
const domains = Array.isArray(config.domain) ? config.domain : [config.domain];
const isWildcard = domains.length === 1 && domains[0] === "*";
if (isWildcard) {
debug.steps.push(`No template matched hostname "${parsedUrl?.hostname}" — using default template`);
} else {
debug.steps.push(`Hostname "${parsedUrl?.hostname}" matched template "${templateDir}" (domains: ${domains.join(", ")})`);
}
debug.matchedTemplate = templateDir;
const matchedRoute = config.routes.find((r) => {
if (r.path === "*") return true;
const regex = new RegExp("^" + r.path.replace(/\*/g, ".*") + "$");
return regex.test(parsedUrl?.pathname);
});
if (matchedRoute) {
if (matchedRoute.path === "*") {
debug.steps.push(`No specific route matched pathname "${parsedUrl?.pathname}" — using wildcard route → layout "${layoutName}"`);
} else {
debug.steps.push(`Pathname "${parsedUrl?.pathname}" matched route "${matchedRoute.path}" → layout "${layoutName}"`);
}
}
debug.matchedLayout = layoutName;
debug.allRoutes = config.routes;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(debug));
return;
}
if (url.pathname === "/opengraph") {
const targetUrl = url.searchParams.get("url");
if (!targetUrl) {
res.writeHead(400);
res.end("Missing ?url= parameter");
return;
}
if (!isDomainAllowed(targetUrl)) {
res.writeHead(403);
res.end("Domain not allowed");
return;
}
try {
const pageRes = await fetch(targetUrl);
const finalUrl = pageRes.url || targetUrl;
const html = await pageRes.text();
const site = parse(html);
const meta = parseMeta(site);
const { width, height } = SIZES.og;
const { templateDir, layoutName, config } = resolveTemplate(finalUrl);
const layout = await loadLayout(templateDir, layoutName);
const assets = await fetchRemoteAssets(loadAssets(templateDir));
const element = layout({ meta, site, config, assets, width, height });
const response = new ImageResponse(element, { width, height, fonts });
const buffer = Buffer.from(await response.arrayBuffer());
res.writeHead(200, {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=86400",
});
res.end(buffer);
} catch (err) {
res.writeHead(500);
res.end(`Failed to fetch URL: ${err.message}`);
}
return;
}
if (url.pathname === "/generate") {
const size = url.searchParams.get("size") || "og";
const templateId = url.searchParams.get("templateId") || "";
const layoutOverride = url.searchParams.get("layout") || "";
const templateUrl = url.searchParams.get("template") || "";
const { width, height } = SIZES[size] || SIZES.og;
// Pass all query params as meta so layouts can use whatever they need
const meta = Object.fromEntries(url.searchParams.entries());
const { templateDir, layoutName, config } = templateId
? resolveTemplateById(templateId, layoutOverride)
: resolveTemplate(templateUrl);
const layout = await loadLayout(templateDir, layoutName);
const assets = await fetchRemoteAssets(loadAssets(templateDir));
const element = layout({ meta, site: null, config, assets, width, height });
const response = new ImageResponse(element, { width, height, fonts });
const buffer = Buffer.from(await response.arrayBuffer());
res.writeHead(200, {
"Content-Type": "image/png",
"Cache-Control": "no-store",
});
res.end(buffer);
return;
}
// Serve static files from project root
const filePath = join(__dirname, url.pathname);
if (url.pathname.endsWith(".html") && existsSync(filePath)) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(readFileSync(filePath, "utf-8"));
return;
}
res.writeHead(404);
res.end("Not found");
});
server.listen(5050, () => console.log("Preview at http://localhost:5050"));