-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.js
More file actions
52 lines (44 loc) · 1.16 KB
/
handler.js
File metadata and controls
52 lines (44 loc) · 1.16 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
const fetch = require('node-fetch');
const FormData = require('form-data');
const fetchStyles = require('./src/fetchStyles');
const getCritical = require('./src/getCritical');
const minify = require('./src/minify');
module.exports.processor = async ({
url,
key,
styles,
hash,
return_url: returnURL,
secret,
}) => {
const stylesheets = await fetchStyles(styles);
const critical = await getCritical(url, stylesheets);
const minified = minify(critical);
const body = new FormData();
const data = {
key,
hash,
stylesheet: minified,
secret,
url,
};
Object.keys(data)
.forEach(key => {
try {
body.append(key, data[key]);
} catch {}
});
const response = await fetch(returnURL, {
method: 'POST',
body,
insecureHTTPParser: true,
});
if (!response.ok) {
throw new Error(response.statusText);
}
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
return response.status;
}
return response.json();
};