-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutils.js
366 lines (305 loc) · 11 KB
/
utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (C) 2016 The EcoMeter authors (https://gitlab.com/ecoconceptionweb/ecometer)
* Copyright (C) 2019 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const DEBUG = true;
requirejs.config({
//By default load any module IDs from script
baseUrl: 'script/externalLibs',
});
// Load module require.js
requirejs(['esprima'],
(esprima) => console.log("Load esprima module"));
const compressibleImage = [
/^image\/bmp(;|$)/i,
/^image\/svg\+xml(;|$)/i,
/^image\/vnd\.microsoft\.icon(;|$)/i,
/^image\/x-icon(;|$)/i,
];
const image = [
/^image\/gif(;|$)/i,
/^image\/jpeg(;|$)/i,
/^image\/png(;|$)/i,
/^image\/tiff(;|$)/i,
].concat(compressibleImage);
const css = [
/^text\/css(;|$)/i,
];
const javascript = [
/^text\/javascript(;|$)/i,
/^application\/javascript(;|$)/i,
/^application\/x-javascript(;|$)/i,
];
const compressibleFont = [
/^font\/eot(;|$)/i,
/^font\/opentype(;|$)/i,
];
const font = [
/^application\/x-font-ttf(;|$)/i,
/^application\/x-font-opentype(;|$)/i,
/^application\/font-woff(;|$)/i,
/^application\/x-font-woff(;|$)/i,
/^application\/font-woff2(;|$)/i,
/^application\/vnd.ms-fontobject(;|$)/i,
/^application\/font-sfnt(;|$)/i,
/^font\/woff2(;|$)/i,
].concat(compressibleFont);
const manifest = [
/^text\/cache-manifest(;|$)/i,
/^application\/x-web-app-manifest\+json(;|$)/i,
/^application\/manifest\+json(;|$)/i,
];
// Mime types from H5B project recommendations
// See https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess#L741
const compressible = [
/^text\/html(;|$)/i,
/^text\/plain(;|$)/i,
/^text\/xml(;|$)/i,
/^application\/json(;|$)/i,
/^application\/atom\+xml(;|$)/i,
/^application\/ld\+json(;|$)/i,
/^application\/rdf\+xml(;|$)/i,
/^application\/rss\+xml(;|$)/i,
/^application\/schema\+json(;|$)/i,
/^application\/vnd\.geo\+json(;|$)/i,
/^application\/vnd\.ms-fontobject(;|$)/i,
/^application\/xhtml\+xml(;|$)/i,
/^application\/xml(;|$)/i,
/^text\/vcard(;|$)/i,
/^text\/vnd\.rim\.location\.xloc(;|$)/i,
/^text\/vtt(;|$)/i,
/^text\/x-component(;|$)/i,
/^text\/x-cross-domain-policy(;|$)/i,
].concat(javascript, css, compressibleImage, compressibleFont, manifest);
const audio = [
/^audio\/mpeg(;|$)/i,
/^audio\/x-ms-wma(;|$)/i,
/^audio\/vnd.rn-realaudio(;|$)/i,
/^audio\/x-wav(;|$)/i,
/^application\/ogg(;|$)/i,
];
const video = [
/^video\/mpeg(;|$)/i,
/^video\/mp4(;|$)/i,
/^video\/quicktime(;|$)/i,
/^video\/x-ms-wmv(;|$)/i,
/^video\/x-msvideo(;|$)/i,
/^video\/x-flv(;|$)/i,
/^video\/webm(;|$)/i,
];
const others = [
/^application\/x-shockwave-flash(;|$)/i,
/^application\/octet-stream(;|$)/i,
/^application\/pdf(;|$)/i,
/^application\/zip(;|$)/i,
];
const staticResources = [].concat(image, javascript, font, css, audio, video, manifest, others);
const httpCompressionTokens = [
'br',
'compress',
'deflate',
'gzip',
'pack200-gzip',
];
const httpRedirectCodes = [301, 302, 303, 307];
// utils for cache rule
function isStaticRessource(resource) {
const contentType = getResponseHeaderFromResource(resource, "content-type");
return staticResources.some(value => value.test(contentType));
}
function isFontResource(resource) {
const contentType = getResponseHeaderFromResource(resource, "content-type");
if (font.some(value => value.test(contentType))) return true;
// if not check url , because sometimes content-type is set to text/plain
if (contentType === "text/plain" || contentType==="" || contentType =="application/octet-stream") {
const url = resource.request.url;
if (url.endsWith(".woff")) return true;
if (url.endsWith(".woff2")) return true;
if (url.includes(".woff?")) return true;
if (url.includes(".woff2?")) return true;
if (url.includes(".woff2.json")) return true;
}
return false;
}
function getHeaderWithName(headers, headerName) {
let headerValue = "";
headers.forEach(header => {
if (header.name.toLowerCase() === headerName.toLowerCase()) headerValue = header.value;
});
return headerValue;
}
function getResponseHeaderFromResource(resource, headerName) {
return getHeaderWithName(resource.response.headers, headerName);
}
function getCookiesLength(resource) {
let cookies = getHeaderWithName(resource.request.headers, "cookie");
if (cookies) return cookies.length;
else return 0;
}
function hasValidCacheHeaders(resource) {
const headers = resource.response.headers;
let cache = {};
let isValid = false;
headers.forEach(header => {
if (header.name.toLowerCase() === 'cache-control') cache.CacheControl = header.value;
if (header.name.toLowerCase() === 'expires') cache.Expires = header.value;
if (header.name.toLowerCase() === 'date') cache.Date = header.value;
});
// debug(() => `Cache headers gathered: ${JSON.stringify(cache)}`);
if (cache.CacheControl) {
if (!(/(no-cache)|(no-store)|(max-age\s*=\s*0)/i).test(cache.CacheControl)) isValid = true;
}
if (cache.Expires) {
let now = cache.Date ? new Date(cache.Date) : new Date();
let expires = new Date(cache.Expires);
// Expires is in the past
if (expires < now) {
//debug(() => `Expires header is in the past ! ${now.toString()} < ${expires.toString()}`);
isValid = false;
}
}
return isValid;
}
// utils for compress rule
function isCompressibleResource(resource) {
if (resource.response.content.size <= 150) return false;
const contentType = getResponseHeaderFromResource(resource, "content-type");
return compressible.some(value => value.test(contentType));
}
function isResourceCompressed(resource) {
const contentEncoding = getResponseHeaderFromResource(resource, "content-encoding");
return ((contentEncoding.length > 0) && (httpCompressionTokens.indexOf(contentEncoding.toLocaleLowerCase()) !== -1));
}
// utils for ETags rule
function isRessourceUsingETag(resource) {
const eTag = getResponseHeaderFromResource(resource, "ETag");
if (eTag === "") return false;
return true;
}
function getDomainFromUrl(url) {
var elements = url.split("//");
if (elements[1] === undefined) return "";
else {
elements = elements[1].split('/'); // get domain with port
elements = elements[0].split(':'); // get domain without port
}
return elements[0];
}
/**
* Count character occurences in the given string
*/
function countChar(char, str) {
let total = 0;
str.split("").forEach(curr => {
if (curr === char) total++;
});
return total;
}
/**
* Detect minification for Javascript and CSS files
*/
function isMinified(scriptContent) {
if (!scriptContent) return true;
if (scriptContent.length === 0) return true;
const total = scriptContent.length - 1;
const semicolons = countChar(';', scriptContent);
const linebreaks = countChar('\n', scriptContent);
if (linebreaks < 2) return true;
// Empiric method to detect minified files
//
// javascript code is minified if, on average:
// - there is more than one semicolon by line
// - and there are more than 100 characters by line
return semicolons / linebreaks > 1 && linebreaks / total < 0.01;
}
/**
* Detect network resources (data urls embedded in page is not network resource)
* Test with request.url as request.httpVersion === "data" does not work with old chrome version (example v55)
*/
function isNetworkResource(harEntry) {
return !(harEntry.request.url.startsWith("data"));
}
/**
* Detect non-network resources (data urls embedded in page)
* Test with request.url as request.httpVersion === "data" does not work with old chrome version (example v55)
*/
function isDataResource(harEntry) {
return (harEntry.request.url.startsWith("data"));
}
function computeNumberOfErrorsInJSCode(code, url) {
let errorNumber = 0;
try {
const syntax = require("esprima").parse(code, { tolerant: true, sourceType: 'script', loc: true });
if (syntax.errors) {
if (syntax.errors.length > 0) {
errorNumber += syntax.errors.length;
debug(() => `url ${url} : ${Syntax.errors.length} errors`);
}
}
} catch (err) {
errorNumber++;
debug(() => `url ${url} : ${err} `);
}
return errorNumber;
}
function isHttpRedirectCode(code) {
return httpRedirectCodes.some(value => value === code);
}
function getImageTypeFromResource(resource) {
const contentType = getResponseHeaderFromResource(resource, "content-type");
if (contentType === "image/png") return "png";
if (contentType === "image/jpeg") return "jpeg";
if (contentType === "image/gif") return "gif";
if (contentType === "image/bmp") return "bmp";
if (contentType === "image/tiff") return "tiff";
return "";
}
function getMinOptimisationGainsForImage(pixelsNumber, imageSize, imageType) {
// difficult to get good compression when image is small , images less than 10Kb are considered optimized
if (imageSize < 10000) return 0;
// image png or gif < 50Kb are considered optimized (used for transparency not supported in jpeg format)
if ((imageSize < 50000) && ((imageType === 'png') || (imageType === 'gif'))) return 0;
let imgMaxSize = Math.max(pixelsNumber / 5, 10000); // difficult to get under 10Kb
// image > 500Kb are too big for web site , there are considered never optimized
if (imageSize > 500000) return Math.max(imageSize - 500000, imageSize - imgMaxSize);
return Math.max(0, imageSize - imgMaxSize);
}
function isSvgUrl(url) {
if (url.endsWith(".svg")) return true;
if (url.includes(".svg?")) return true;
return false;
}
function isSvgOptimized(svgImage) {
if (svgImage.length < 1000) return true; // do not consider image < 1KB
if (svgImage.search(" <") === -1) return true;
return false;
}
function getOfficialSocialButtonFormUrl(url)
{
if (url.includes("platform.twitter.com/widgets.js")) return "tweeter";
if (url.includes("platform.linkedin.com/in.js")) return "linkedin";
if (url.includes("assets.pinterest.com/js/pinit.js")) return "pinterest";
if (url.includes("connect.facebook.net") && url.includes("sdk.js")) return "facebook";
if (url.includes("platform-api.sharethis.com/js/sharethis.js")) return "sharethis.com (mutliple social network) ";
if (url.includes("s7.addthis.com/js/300/addthis_widget.js")) return "addthis.com (mutliple social network) ";
if (url.includes("static.addtoany.com/menu/page.js")) return "addtoany.com (mutliple social network) ";
return "";
}
function debug(lazyString) {
if (!DEBUG) return;
const message = typeof lazyString === 'function' ? lazyString() : lazyString;
console.log(`GreenIT-Analysis [DEBUG] ${message}\n`);
}