This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.ts
More file actions
502 lines (440 loc) · 17.7 KB
/
Copy pathmain.ts
File metadata and controls
502 lines (440 loc) · 17.7 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// main.ts - Deno Deploy script for multi-site IndexNow and Ping-O-Matic submission
// Import cron to schedule the script execution
import './cron.ts';
// Import decodeBase64 for basic auth
import { decodeBase64 } from "https://deno.land/std@0.224.0/encoding/base64.ts";
// --- Type Definitions ---
// Define interfaces for the structure of your configuration and post data
interface PingOMaticConfig {
title: string;
blogUrl: string;
rssUrl: string;
}
export interface SiteConfig {
id: string;
host: string;
feedUrl: string;
indexNowKeyEnv: string;
pingOMatic?: PingOMaticConfig;
webSubHubUrl?: string; // Optional: URL of the WebSub hub to notify
}
// Post defines a basic structure for JSON feed items (posts)
// Expand based on actual feed structure
interface Post {
url?: string; // Optional, there is a warning if it's missing
date_published?: string;
published?: string;
date?: string;
date_modified?: string;
updated_at?: string;
title?: string;
content_html?: string;
content_text?: string;
summary?: string;
[key: string]: unknown; // Allow other properties
}
interface JsonFeed {
version: string;
title: string;
home_page_url: string;
feed_url: string;
items: Post[];
}
// --- Constants ---
const TWENTY_FOUR_HOURS_IN_MS: number = 24 * 60 * 60 * 1000;
const LAST_CHECK_KEY_PREFIX: string = "last_check_"; // Prefix for last checked timestamps in Deno KV
const SITE_CONFIG_KV_KEY = ["site_configs"]; // Key for storing all site configs in Deno KV
// --- Deno KV (Key-Value Store) for persistence ---
// Deno.Kv will infer its type, but explicitly typing helps clarity
const kv: Deno.Kv = await Deno.openKv();
// --- KV Helper Functions for Site Config ---
async function getSiteConfigs(): Promise<SiteConfig[]> {
const result: Deno.KvEntryMaybe<SiteConfig[]> = await kv.get(SITE_CONFIG_KV_KEY);
return result.value || [];
}
async function setSiteConfigs(configs: SiteConfig[]): Promise<void> {
await kv.set(SITE_CONFIG_KV_KEY, configs);
}
// --- Helper Function to fetch JSON ---
async function fetchJsonFeed(url: string): Promise<JsonFeed | null> {
try {
const response: Response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Type assertion 'as JsonFeed' because fetch().json() returns Promise<any>
return (await response.json()) as JsonFeed;
} catch (error: unknown) { // Use 'unknown' for caught errors as they can be anything
console.error(`Error fetching JSON feed from ${url}:`, error);
return null;
}
}
// --- Helper Function to get last checked timestamp ---
async function getLastChecked(feedId: string): Promise<Date | null> {
const result: Deno.KvEntryMaybe<string> = await kv.get([LAST_CHECK_KEY_PREFIX + feedId]);
return result.value ? new Date(result.value) : null;
}
// --- Helper Function to set last checked timestamp ---
async function setLastChecked(feedId: string, timestamp: Date): Promise<void> {
await kv.set([LAST_CHECK_KEY_PREFIX + feedId], timestamp.toISOString());
}
// --- Helper Function to check if a post is new or updated ---
function isPostNewOrUpdated(post: Post, lastCheckedTime: Date | null): boolean {
// IMPORTANT: Adapt these date fields to match your JSON feed's structure.
// Using 'as string' to tell TypeScript these properties are expected to be strings
const publishedDate: Date = new Date(
(post.date_published || post.published || post.date) as string,
);
const updatedDate: Date = new Date(
(post.date_modified || post.updated_at || publishedDate.toISOString()) as string,
); // Fallback to publishedDate's ISO string
const currentTime: number = Date.now();
// If lastCheckedTime is null (first run), consider the post new if it's within the last 24 hours
if (!lastCheckedTime) {
return (currentTime - publishedDate.getTime()) <= TWENTY_FOUR_HOURS_IN_MS;
}
// Check if published or updated after the last check
return (publishedDate.getTime() > lastCheckedTime.getTime()) ||
(updatedDate.getTime() > lastCheckedTime.getTime());
}
// --- Function to ping IndexNow ---
async function pingIndexNow(host: string, apiKey: string, urls: string[]): Promise<void> {
if (urls.length === 0) {
console.log(`[${host}] No new URLs for IndexNow.`);
return;
}
const payload = {
host: host,
key: apiKey,
urlList: urls.map((url: string) => ({ loc: url })),
};
// --- START DEBUG ---
console.log(`[${host}] IndexNow Payload for debugging: ${JSON.stringify(payload, null, 2)}`);
// --- END DEBUG ---
try {
const response: Response = await fetch("https://api.indexnow.org/IndexNow", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (response.ok) {
console.log(`[${host}] Successfully pinged IndexNow with ${urls.length} URLs.`);
} else {
const errorText: string = await response.text();
console.error(
`[${host}] Failed to ping IndexNow. Status: ${response.status}, Response: ${errorText}`,
);
}
} catch (error: unknown) {
console.error(`[${host}] Error pinging IndexNow:`, error);
}
}
// --- Function to ping Ping-O-Matic ---
async function pingPingOMatic(siteConfig: SiteConfig): Promise<void> {
// Check if pingOMatic property exists and is not undefined
if (!siteConfig.pingOMatic) {
console.warn(`[${siteConfig.id}] Ping-O-Matic configuration is missing. Skipping.`);
return;
}
const { title, blogUrl, rssUrl } = siteConfig.pingOMatic;
if (!title || !blogUrl || !rssUrl) {
console.warn(
`[${siteConfig.id}] Missing Ping-O-Matic configuration (title, blogUrl, or rssUrl). Skipping.`,
);
return;
}
// Encode the parameters to ensure they are URL-safe
const encodedTitle: string = encodeURIComponent(title);
const encodedBlogUrl: string = encodeURIComponent(blogUrl);
const encodedRssUrl: string = encodeURIComponent(rssUrl);
const pingUrl: string =
`https://pingomatic.com/ping/?title=${encodedTitle}&blogurl=${encodedBlogUrl}&rssurl=${encodedRssUrl}&chk_blogs=on&chk_feedburner=on&chk_tailrank=on&chk_superfeedr=on`;
try {
const response: Response = await fetch(pingUrl, { method: "GET" });
if (response.ok) {
const responseText: string = await response.text();
console.log(
`[${siteConfig.id}] Successfully pinged Ping-O-Matic. Response: ${
responseText.substring(0, 100)
}...`,
);
} else {
const errorText: string = await response.text();
console.error(
`[${siteConfig.id}] Failed to ping Ping-O-Matic. Status: ${response.status}, Response: ${errorText}`,
);
}
} catch (error: unknown) {
console.error(`[${siteConfig.id}] Error pinging Ping-O-Matic:`, error);
}
}
// Function to notify Google's public websub hub
async function notifyWebSubHub(feedUrl: string, hubUrl: string = "https://pubsubhubbub.appspot.com/publish"): Promise<void> {
const params = new URLSearchParams({
'hub.mode': 'publish',
'hub.url': feedUrl
});
try {
const response = await fetch(hubUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params.toString(),
});
if (response.ok) {
console.log(`Successfully notified WebSub hub (${hubUrl}) for feed: ${feedUrl}`);
} else {
const errorText = await response.text();
console.error(`Failed to notify WebSub hub (${hubUrl}) for feed: ${feedUrl}. Status: ${response.status}, Response: ${errorText}`);
}
} catch (error: unknown) {
console.error(`Error notifying WebSub hub (${hubUrl}) for feed: ${feedUrl}:`, error);
}
}
// --- Main execution function for a single feed ---
async function processFeed(siteConfig: SiteConfig): Promise<void> {
console.log(`Processing feed for ${siteConfig.id} (${siteConfig.feedUrl})...`);
const lastCheckedTime: Date | null = await getLastChecked(siteConfig.id);
const currentRunTime: Date = new Date();
const feed: JsonFeed | null = await fetchJsonFeed(siteConfig.feedUrl);
if (!feed || !feed.items || feed.items.length === 0) {
console.log(`[${siteConfig.id}] Could not fetch feed or feed is empty.`);
return;
}
const urlsToIndexNow: string[] = [];
let hasUpdatedPostsForPingOMatic: boolean = false;
for (const post of feed.items) {
if (isPostNewOrUpdated(post, lastCheckedTime)) {
if (post.url) {
urlsToIndexNow.push(post.url);
hasUpdatedPostsForPingOMatic = true;
} else {
console.warn(
`[${siteConfig.id}] Post found without a 'url' field. Skipping for IndexNow/Ping-O-Matic:`,
post,
);
}
}
}
const indexNowApiKey: string | undefined = Deno.env.get(siteConfig.indexNowKeyEnv);
if (!indexNowApiKey) {
console.error(
`[${siteConfig.id}] IndexNow API key not found for environment variable: ${siteConfig.indexNowKeyEnv}. Skipping IndexNow ping.`,
);
} else {
await pingIndexNow(siteConfig.host, indexNowApiKey, urlsToIndexNow);
}
// After other pings, if there were updates
if (hasUpdatedPostsForPingOMatic) { // Reusing this flag to indicate *any* updates
if (siteConfig.pingOMatic) {
await pingPingOMatic(siteConfig);
}
// Notify WebSub hub if configured
if (siteConfig.webSubHubUrl) {
await notifyWebSubHub(siteConfig.feedUrl, siteConfig.webSubHubUrl);
}
} else {
console.log(`[${siteConfig.id}] No new or updated posts.`); // Updated message
}
await setLastChecked(siteConfig.id, currentRunTime);
console.log(`[${siteConfig.id}] Processing complete. Last checked timestamp updated.`);
}
// --- Basic Authentication Helper ---
function basicAuth(request: Request): Response | null {
const ADMIN_USERNAME = Deno.env.get("ADMIN_USERNAME");
const ADMIN_PASSWORD = Deno.env.get("ADMIN_PASSWORD");
if (!ADMIN_USERNAME || !ADMIN_PASSWORD) {
console.error("ADMIN_USERNAME or ADMIN_PASSWORD environment variables are not set for Basic Auth.");
return new Response("Server configuration error: Admin credentials not set.", { status: 500 });
}
const authHeader = request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Basic ")) {
return new Response("Unauthorized", {
status: 401,
headers: { "WWW-Authenticate": 'Basic realm="Admin"' },
});
}
const encoded = authHeader.substring(6); // "Basic ".length is 6
// CORRECTED USAGE: decodeBase64 instead of decode
const decoded = new TextDecoder().decode(decodeBase64(encoded));
const [username, password] = decoded.split(":");
if (username === ADMIN_USERNAME && password === ADMIN_PASSWORD) {
return null; // Authorized
} else {
return new Response("Unauthorized", {
status: 401,
headers: { "WWW-Authenticate": 'Basic realm="Admin"' },
});
}
}
// --- Admin UI Rendering ---
function renderAdminPage(configs: SiteConfig[]): Response {
const configJson = JSON.stringify(configs, null, 2); // Pretty print JSON
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deno Deploy Site Config Admin</title>
<style>
body { font-family: sans-serif; margin: 20px; background-color: #f4f4f4; color: #333; }
h1 { color: #0056b3; }
textarea {
width: 90%;
height: 400px;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: monospace;
white-space: pre;
overflow-wrap: normal;
overflow-x: auto;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover { background-color: #0056b3; }
.message {
margin-top: 15px;
padding: 10px;
border-radius: 4px;
font-weight: bold;
}
.success { background-color: #d4edda; color: #155724; border-color: #c3e6cb; }
.error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; }
</style>
</head>
<body>
<h1>Deno Deploy Site Configuration</h1>
<p>Edit the JSON below to manage your site configurations. Save changes to update Deno KV.</p>
<form id="configForm" method="POST" action="/update">
<textarea id="siteConfig" name="siteConfig">${configJson}</textarea>
<br>
<button type="submit">Save Configuration</button>
</form>
<div id="message" class="message"></div>
<script>
const form = document.getElementById('configForm');
const messageDiv = document.getElementById('message');
form.addEventListener('submit', async (e) => {
e.preventDefault();
messageDiv.textContent = '';
messageDiv.className = 'message';
try {
const textarea = document.getElementById('siteConfig');
const configData = textarea.value;
// Basic JSON validation before sending
try {
JSON.parse(configData);
} catch (jsonError) {
messageDiv.textContent = 'JSON Syntax Error: ' + jsonError.message;
messageDiv.classList.add('error');
return;
}
const response = await fetch('/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: configData
});
if (response.ok) {
const responseText = await response.text();
messageDiv.textContent = 'Configuration saved successfully! ' + responseText;
messageDiv.classList.add('success');
} else {
const errorText = await response.text();
messageDiv.textContent = 'Failed to save configuration: ' + response.status + ' ' + errorText;
messageDiv.classList.add('error');
}
} catch (error) {
messageDiv.textContent = 'An unexpected error occurred: ' + error.message;
messageDiv.classList.add('error');
}
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: { "Content-Type": "text/html" },
});
}
// --- Deno Deploy Entry Point ---
addEventListener("fetch", async (event: FetchEvent) => {
const request = event.request;
const url = new URL(request.url);
console.log(`[${new Date().toISOString()}] Request received: ${request.method} ${url.pathname}`);
// --- Admin UI Routes ---
if (url.pathname === "/admin") {
const authResponse = basicAuth(request);
if (authResponse) {
event.respondWith(authResponse);
return;
}
const currentConfigs = await getSiteConfigs();
event.respondWith(renderAdminPage(currentConfigs));
return;
}
if (url.pathname === "/update" && request.method === "POST") {
const authResponse = basicAuth(request);
if (authResponse) {
event.respondWith(authResponse);
return;
}
try {
const newConfigs: SiteConfig[] = await request.json(); // Expect JSON payload
if (!Array.isArray(newConfigs)) {
throw new Error("Invalid JSON: Expected an array of site configurations.");
}
await setSiteConfigs(newConfigs);
console.log(`[${new Date().toISOString()}] Site configurations updated successfully in Deno KV.`);
event.respondWith(new Response("Configuration saved successfully", { status: 200 }));
} catch (error: unknown) {
console.error(`[${new Date().toISOString()}] Error updating configurations:`, error);
event.respondWith(
new Response(`Error: ${(error as Error).message || "Invalid configuration format."}`, { status: 400 }),
);
}
return;
}
// --- Main Cron Job Execution (for '/') ---
if (url.pathname === "/") {
// Respond immediately for cron jobs, then run background task
event.respondWith(
new Response("Deno Deploy Multi-Site IndexNow/Ping-O-Matic checker running... (Check logs for details)", { status: 200 }),
);
console.log(`[${new Date().toISOString()}] Starting background processing for cron job.`);
try {
const siteConfigs: SiteConfig[] = await getSiteConfigs(); // Read from KV
if (!Array.isArray(siteConfigs) || siteConfigs.length === 0) {
console.warn(`[${new Date().toISOString()}] No site configurations found in Deno KV. Skipping cron job processing.`);
return;
}
console.log(`[${new Date().toISOString()}] Retrieved ${siteConfigs.length} site configurations from Deno KV.`);
const processingPromises: Promise<void>[] = siteConfigs.map(
(config: SiteConfig) => processFeed(config),
);
await Promise.all(processingPromises);
console.log(`[${new Date().toISOString()}] All site feeds processed.`);
} catch (error: unknown) {
console.error(`[${new Date().toISOString()}] Error in main cron execution loop:`, error);
if (error instanceof Error) {
console.error(error.stack);
}
}
return;
}
// Handle other unknown paths
event.respondWith(new Response("Not Found", { status: 404 }));
});