-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
40 lines (35 loc) · 1.09 KB
/
app.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
const etag = require('etag');
const { parse } = require('url');
const { getBlob } = require('./blob');
const { getInt } = require('./validator');
async function getApp(req, res) {
try {
const { query = {} } = parse(req.url, true);
const {
seed = Math.random(),
extraPoints = 4,
randomness = 6,
size = 256,
fill,
stroke = 'none',
strokeWidth = 0,
} = query;
const ep = getInt(extraPoints);
const rand = getInt(randomness);
const sz = getInt(size);
const stw = getInt(strokeWidth);
const blob = await getBlob(seed, ep, rand, sz, fill, stroke, stw);
res.statusCode = 200;
res.setHeader('Content-Disposition', `filename="blob.svg"`);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate');
res.setHeader('ETag', etag(blob));
res.send(blob);
} catch (e) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Failed to generate a blob SVG image.');
console.error(e.message);
}
}
module.exports = { getApp };