-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (55 loc) · 1.67 KB
/
index.js
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
const crypto = require('crypto');
const fs = require('fs');
const { XMLParser, XMLBuilder } = require('fast-xml-parser');
const express = require('express');
const _xml_opt = { ignoreAttributes: false, attributeNamePrefix: '@_' };
const xml = new XMLParser(_xml_opt);
const xmlb = new XMLBuilder(_xml_opt);
const app = express();
const base = fs.readFileSync('assets/base.svg').toString();
function getPatterns() {
return fs.readdirSync('assets/patterns/');
}
function getTodaysPattern() {
let int = parseInt(crypto.createHash('md5').update(Date.now().toString()).digest('hex'), 16);
let patterns = getPatterns();
let ind = int % patterns.length;
if (!(ind >= 0)) ind = 0;
let ptrn = fs.readFileSync(`assets/patterns/${patterns[ind]}`).toString();
return ptrn;
}
app.get('/favicon', (req, res) => {
let src = getTodaysPattern();
let components = {
mouthptrn: false,
headptrn: false,
lowerheadptrn: false,
eyeleftptrn: false,
outereyeleftptrn: false,
noseptrn: false,
eyerightptrn: false,
outereyerightptrn: false
};
let ckeys = Object.keys(components);
let { pattern } = xml.parse(src);
let basexml = xml.parse(base.replace('{{mainptrn}}', src));
if (!Array.isArray(pattern)) pattern = [pattern];
for (const p of pattern) {
if (ckeys.includes(p['@_id'])) {
components[p['@_id']] = true;
}
}
for (let i = 0; i < ckeys.length; i++) {
if (components[ckeys[i]]) {
basexml.svg.path[i]['@_fill'] = `url(#${ckeys[i]})`;
}
}
let final = xmlb.build(basexml);
res.set('content-type', 'image/svg+xml');
res.end(final);
});
if (!process.env.DETA_RUNTIME) {
app.listen(8080, console.log.bind(globalThis, '[i] Ran at 8080'));
} else {
module.exports = app;
}