-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
529 lines (437 loc) · 14.8 KB
/
Copy pathworker.js
File metadata and controls
529 lines (437 loc) · 14.8 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
* Optimized URL (HTML|JSON) → RSS generator (Cloudflare workers)
* - Static assets served via ASSETS binding (wrangler.toml)
* - Supported fields: required (title or link), optional (desc, date)
* - Regex-based filtering for item, title, link, desc (date unsupported)
* - Custom HTTP headers (RFC-style = newline-delimited; base64-encoded)
* - Caching via Cloudflare edge (can be disabled)
*/
export default {
async fetch(req, env, ctx) {
try {
return await handleFeed(req, ctx);
} catch (err) {
const status = err?.status || 500;
return new Response(err?.message || "Internal Error", { status });
}
},
};
const DEFAULT_LIMIT = 5; // items per feed
const MAX_LIMIT = 25; // max items per feed
const CACHE_TTL = 900; // in seconds; 15 minutes
const DISABLE_CACHE = false; // for testing
const DEBUG = false; // for debugging
if (!DEBUG) {
console.log = () => {};
}
async function handleFeed(req, ctx) {
const isJsonResponse = res => {
const type = res.headers.get('content-type') || '';
return type.includes('application/json');
};
const url = new URL(req.url);
const params = parseParams(url.searchParams);
let cacheKey;
// Caching; key includes params
if (!url.searchParams.get('nocache') && !DISABLE_CACHE) {
cacheKey = new Request(url.toString(), req);
const cached = await caches.default.match(cacheKey);
if (cached) return cached;
}
// NOTE: network wait not counted in CPU time
const upstream = await fetch(params.url, {
redirect: "follow", //
headers: {
'User-Agent': 'RSSible/1.0 (+https://rssible.hadid.dev/)', //
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', //
...params.headers // user-provided headers override defaults
},
}).catch((error) => {
throw http(502, `Page fetch error: ${error.message}`);
});
if (!upstream.ok) throw http(502, `Upstream ${upstream.status}`);
// Accept JSON; covert to HTML for parsing
const isJson = isJsonResponse(upstream);
const source = isJson ? await jsonToHtml(upstream, params) : upstream;
let res;
// For debugging: return the converted HTML from JSON
if (url.searchParams.get('mirror')) {
if (!isJson) {
// Don't use me as a proxy for arbitrary sites
throw http(501, 'The "mirror" option only supports JSON pages.');
}
res = source;
} else {
const items = await extractItems(source, params);
const rssXml = buildRss({ params, items });
res = new Response(rssXml, {
headers: {
"Content-Type": "application/rss+xml; charset=utf-8", //
"Cache-Control": `public, max-age=${CACHE_TTL}`,
},
});
}
if (!DISABLE_CACHE) {
ctx.waitUntil(caches.default.put(cacheKey, res.clone()));
}
return res;
}
function parseParams(query) {
const url = query.get("url")?.trim();
let headers = {};
// Base64-encoded headers (newline-delimited)
const headersB64 = query.get("headers")?.trim();
if (headersB64) {
try {
const raw = atob(headersB64);
// Converts to { name: value, ... }
headers = sanitizeHeaders(parseHeaders(raw));
} catch (e) {
throw http(400, "Invalid 'headers' parameter (base64-encoded).");
}
}
if (query.get("mirror")) {
return { url, headers };
}
const item = query.get("_item")?.trim();
if (!url || !item) {
throw http(400, "Query params 'url' and 'item' are required");
}
const title = query.get("title")?.trim();
const link = query.get("link")?.trim();
if (!title && !link) {
throw http(400, "Provide at least one selector: 'title' or 'link'.");
}
const desc = query.get("desc")?.trim();
const date = query.get("date")?.trim();
// Accept weird formats, like OxFF or 1e1
const limitRaw = Number(query.get("limit") || DEFAULT_LIMIT);
const limit = Math.min(isFinite(limitRaw) ? limitRaw : DEFAULT_LIMIT, MAX_LIMIT);
const streamRaw = (query.get("stream") || "on").toLowerCase();
const stream = !(streamRaw === "off");
// Compiles to array of { field, regex }
const filterRaw = query.get("filters")?.trim();
const filters = filterRaw ? parseFilters(filterRaw) : {};
return { url, item, title, link, desc, date, limit, stream, headers, filters };
}
// Read HTMLRewriter doc to figure out what the hell is going on here:
// https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/
async function extractItems(upstream, params) {
const normalizeText = str => {
if (!str) return "";
str = str.replace(/\s+/g, ' ').trim();
return decodeHTML(str);
};
const matchFilters = (item, filters) => {
if (filters.item && !filters.item.test(item._text)) return false;
if (filters.title && !filters.title.test(item.title)) return false;
if (filters.link && !filters.link.test(item.link)) return false;
return !(filters.desc && !filters.desc.test(item.desc));
};
const items = [];
let current;
const rewriter = new HTMLRewriter().on(params.item, {
element(elem) {
if (items.length >= params.limit) return;
current = { _text: "", title: "", desc: "" }; // reset for new item
elem.onEndTag(() => {
current.title = normalizeText(current.title);
// Strip leading numbering: 1., (1, and (1)
current.title = current.title.replace(/^\(?(\d+)[.)]\s+/, '')
current.desc = normalizeText(current.desc);
current.link = normalizeText(current.link);
current._text = normalizeText(current._text);
const match = matchFilters(current, params.filters);
if ((current.title || current.link) && match) {
items.push(current);
}
});
},
// Text within item element (for filtering)
// A text node may come in chunks/fragmented
// See https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#text-chunks
text(text) {
if (!params.filters.item) return;
if (items.length >= params.limit) return;
// Add space between text nodes; e.g., <p>one</p><p>two</p>
if (text.lastInTextNode) current._text += " "
const chunk = text.text?.trim();
if (chunk) current._text += chunk;
current._text = current._text?.trim();
},
});
if (params.title) {
// Include text of all matching nodes
rewriter.on(`${params.item} ${params.title}`, {
text(text) {
if (items.length >= params.limit) return;
// Add space between text nodes; e.g., <p>one</p><p>two</p>
if (text.lastInTextNode) current.title += " "
const chunk = text.text?.trim();
if (chunk) current.title = current.title + chunk;
current.title = current.title?.trim();
},
});
}
if (params.link) {
const isSelf = params.link === '@' || params.link === '.';
const linkSelector = isSelf ? params.item : `${params.item} ${params.link}`;
// First element with href attribute wins
rewriter.on(linkSelector, {
element(elem) {
if (items.length >= params.limit) return;
if (current.link) return; // already have a link
let href = elem.getAttribute("href");
if (href && href.startsWith('/')) {
href = new URL(params.url).origin + href;
}
if (href) current.link = href;
},
});
}
if (params.desc) {
// Include text of all matching nodes
rewriter.on(`${params.item} ${params.desc}`, {
text(text) {
if (items.length >= params.limit) return;
// Add space between text nodes; e.g., <p>one</p><p>two</p>
if (text.lastInTextNode) current.desc += " "
const chunk = text.text?.trim();
if (chunk) current.desc += chunk;
current.desc = current.desc?.trim();
},
});
}
if (params.date) {
// Parse all matches, first valid match wins
let nodeText = ""; // whole text node; chunks merged
rewriter.on(`${params.item} ${params.date}`, {
text(text) {
if (items.length >= params.limit) return;
if (current.pubDate) return; // already have a date
nodeText += text.text;
if (!text.lastInTextNode) return; // partial chunk
try {
current.pubDate = new Date(nodeText).toISOString();
} catch {
nodeText = ""; // reset for next match
}
},
});
}
const transformed = rewriter.transform(upstream);
if (params.stream) {
// Zero-copy streaming parsing
const reader = transformed.body.getReader();
while (true) {
// Pulls in chunks; controlled by producer
const { done } = await reader.read();
if (done || (items.length >= params.limit)) break;
}
} else {
// Read entire body once
await transformed.text();
}
return items;
}
function buildRss({ params, items }) {
const now = new Date().toUTCString();
const { origin, host } = new URL(params.url);
const indent = (str, n) => " ".repeat(n) + str;
let out = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>${esc(host)}</title>
<link>${esc(params.url)}</link>
<generator>RSSible</generator>
<ttl>${CACHE_TTL / 60}</ttl>
<image>
<url>${origin}/favicon.ico</url>
</image>
<lastBuildDate>${now}</lastBuildDate>`;
for (let i = 0; i < items.length; i++) {
const it = items[i];
out += "\n" + indent("<item>", 4);
if (it.title) out += "\n" + indent(`<title>${esc(it.title)}</title>`, 6);
if (it.link) out += "\n" + indent(`<link>${esc(it.link)}</link>`, 6);
if (it.desc) out += "\n" + indent(`<description><![CDATA[${it.desc}]]></description>`, 6);
if (it.pubDate) out += "\n" + indent(`<pubDate>${it.pubDate}</pubDate>`, 6);
if (it.link) out += "\n" + indent(`<guid isPermaLink="true">${esc(it.link)}</guid>`, 6);
out += "\n" + indent("</item>", 4);
}
out += "\n" + indent("</channel>", 2);
out += "\n</rss>";
return out;
}
// ¯\_(ツ)_/¯
const namedMap = {
'&': '&', //
'<': '<', //
'>': '>', //
'"': '"', //
'“': '“', //
'”': '”', //
'‘': '‘', //
''': "'", //
'’': '’', //
'(': '(', //
')': ')', //
'[': '[', //
']': ']', //
'{': '{', //
'}': '}', //
'*': '*', //
'\': '\\', //
'/': '/', //
' ': '\u00A0', //
'!': '!', //
'#': '#', //
'$': '$', //
'%': '%', //
'@': '@', //
'?': '?', //
';': ';', //
':': ':', //
'=': '=', //
'+': '+', //
'−': '-', //
'_': '_', //
'—': '—', //
'`': '`', //
'˜': '~', //
'|': '|',
};
function decodeHTML(str) {
if (!str) return str;
// &#...; (decimal)
str = str.replace(/&#(\d+);/g, (match, dec) => {
const code = Number(dec);
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
});
// &#x...; (hex)
str = str.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => {
const code = parseInt(hex, 16);
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
});
// Single regex to avoid multiple scans
return str.replace(/&[a-z]+;/gi, (m) => namedMap[m.toLowerCase()] || m);
}
// CPU-friendly escape
function esc(str) {
str = String(str);
// Early exit when no escapables present (avoids regex work)
if (str.indexOf("<") === -1 && str.indexOf(">") === -1 && str.indexOf("&") === -1) return str;
return str.replace(/[<>&]/g, (c) => (c === "<" ? "<" : c === ">" ? ">" : "&"));
}
function http(status, message) {
const err = new Error(message);
err.status = status;
return err;
}
function parseHeaders(block) {
const out = {};
// RFC-style lines: "Name: value"
for (const rawLine of block.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line) continue; // skip empty
const idx = line.indexOf(":");
if (idx <= 0) continue; // skip malformed
const name = line.slice(0, idx).trim();
const value = line.slice(idx + 1).trim();
if (!name) continue; // allow empty value
out[name] = value;
}
return out;
}
function sanitizeHeaders(headers) {
if (!headers) return undefined;
const drop = new Set([
"connection",
"proxy-connection",
"keep-alive",
"transfer-encoding",
"upgrade",
"te",
"host",
"content-length",
"content-encoding",
"proxy-authorization"
]);
const out = {};
for (const [k, v] of Object.entries(headers)) {
const lower = k.toLowerCase();
if (drop.has(lower)) continue;
out[k] = v;
}
return Object.keys(out).length ? out : {};
}
// Supported keys: item, title, link, desc (date unsupported)
// Block lines like: key=/pattern/flags
function parseFilters(block) {
const out = {};
for (const rawLine of block.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line) continue;
const eq = line.indexOf("=");
if (eq <= 0) continue;
const key = line.slice(0, eq).trim().toLowerCase();
if (!["item", "title", "link", "desc"].includes(key)) continue;
const regex = line.slice(eq + 1).trim();
if (!regex.startsWith("/")) continue;
const lastSlash = regex.lastIndexOf("/");
if (lastSlash <= 0) continue;
const pattern = regex.slice(1, lastSlash); // raw between slashes
if (!pattern) continue; // empty pattern is like not set
const flags = regex.slice(lastSlash + 1); // may be empty
try {
const pat = pattern
.replace('\/', '/')
.replace('\=', '=');
out[key] = new RegExp(pat, flags);
} catch {} // ignore invalid regex
}
return Object.keys(out).length ? out : {};
}
async function jsonToHtml(res) {
let data;
try {
data = JSON.parse(await res.text());
} catch {
throw http(502, "Invalid JSON from upstream.");
}
const _class = (key) => String(key ?? '')
.replace(/[^a-z0-9_\-$]/g, '-') // special char -> dash
.replace(/-+/g, '-'); // collapse dashes
function toHtml(key, value) {
const cls = _class(key);
if (value === null || value === undefined) {
return `<div class="${cls}"></div>`;
}
const type = typeof value;
if (type === 'number' || type === 'boolean') {
return `<div class="${cls}">${esc(String(value))}</div>`;
}
if (type === 'string') {
if (/^https?:\/\//i.test(value)) {
return `<a class="${cls}" href="${esc(value)}"></a>`;
} else {
return `<div class="${cls}">${esc(value)}</div>`;
}
}
if (Array.isArray(value)) {
return `<div class="${cls}">` + value.map(v => toHtml('_item', v)).join('') + `</div>`;
}
if (type === 'object') {
return `<div class="${cls}">` + Object.entries(value).map(([k, v]) => toHtml(k, v)).join('') + `</div>`;
}
return `<div class="${cls}">${esc(String(value))}</div>`;
}
const html = '<!doctype html><meta charset="utf-8">' + '<body>' + toHtml('_root', data) + '</body>';
// Using text/plain for easy debugging in browser
return new Response(html, {
headers: {
'Content-Type': 'text/plain; charset=utf-8', //
"Cache-Control": `public, max-age=${CACHE_TTL}`,
}
});
}