forked from spoopy-link/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_cache.js
More file actions
47 lines (40 loc) · 1.63 KB
/
web_cache.js
File metadata and controls
47 lines (40 loc) · 1.63 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
const crypto = require('crypto');
const request = require('snekfetch');
const { PAGES, GH_ROOT, SERVER_404_MESSAGE } = require('./Constants');
const TimedCache = require('./util/TimedCache');
const { JSDOM } = require('jsdom');
const log = require('./util/logger');
const WEB_ROOT = process.env.GH_ROOT || GH_ROOT;
const cache = new TimedCache(9e5, (item) => request.get(`${WEB_ROOT}${item.startsWith('/') ? item : PAGES[item]}`)
.then(async(res) => {
if (!res.text.startsWith('<!DOCTYPE html>')) return res.text;
const dom = new JSDOM(res.text);
const document = dom.window.document;
const scripts = document.querySelectorAll('script');
const styles = document.querySelectorAll('link[rel=stylesheet]');
for (const node of [...scripts, ...styles]) {
if (!node.src && !node.href) continue;
node.setAttribute('crossorigin', 'anonymous');
let src = (node.src || node.href).replace(/^\//, '');
src = /^https?:\/\//.test(src) ? src : `https://spoopy.link/${src}`;
await request.get(src).then((r) => {
node.setAttribute('integrity', `sha384-${crypto.createHash('sha384').update(r.text).digest('base64')}`);
})
.catch(log);
}
const search = document.createElement('LINK');
search.setAttribute('type', 'application/opensearchdescription+xml');
search.setAttribute('rel', 'search');
search.setAttribute('href', '/search.xml');
document.head.appendChild(search);
return dom.serialize();
})
.catch((err) => {
log('FETCH', err);
return SERVER_404_MESSAGE;
})
);
cache.on('get', (state, item) => {
log('CACHE ITEM', state, item);
});
module.exports = cache;