UNPKG

852 BJavaScriptView Raw
1'use strict';
2
3var format = require('util').format,
4 MAX_LENGTH = 4 * 1024;
5
6function rule(analyzer) {
7 // @see http://stackoverflow.com/a/11335500
8 var re = /data:.+\/(.+);base64,(.*)\)/;
9 analyzer.setMetric('base64Length');
10
11 analyzer.on('declaration', function(rule, property, value) {
12 var base64,
13 buf,
14 matches;
15
16 if (re.test(value)) {
17 // parse data URI
18 matches = value.match(re);
19 base64 = matches[2];
20 buf = new Buffer(base64, 'base64');
21
22 analyzer.incrMetric('base64Length', base64.length);
23
24 if (base64.length > MAX_LENGTH) {
25 analyzer.addOffender('base64Length', format('%s { %s: ... } // base64: %s kB, raw: %s kB', rule.selectors.join(', '), property, (base64.length / 1024).toFixed(2), (buf.length / 1024).toFixed(2)));
26 }
27 }
28 });
29}
30
31rule.description = 'Reports on base64-encoded images';
32module.exports = rule;