-
Notifications
You must be signed in to change notification settings - Fork 9
/
cache_checker.js
161 lines (145 loc) · 4.68 KB
/
cache_checker.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
const puppeteer = require('puppeteer');
const https = require('https');
const http = require('http');
// constants
const URL_HAS_PROTOCOL_REGEX = /^https?:\/\//i;
/*
*This function loads the webpage first time so i can be cached
*@note only if website using cache
*
*/
first_load = async (URL, flag) => {
//Create browser instance
const browser = await puppeteer.launch({
userDataDir: '/tmp/user-data-dir', //<--- to save user data like cache
headless: true, //<-- false for headful mode
args: ['--no-sandbox'],
});
const page = await browser.newPage();
const response = await page.goto(URL);
if (flag == '--server' || flag == '--s') {
const extendedServerDetails = {
domainName: URL,
...response.headers(),
...response.remoteAddress(),
};
console.log(extendedServerDetails); //initially used tables, but they got out of view for large headers
}
browser.close();
return true;
};
/*
* Main Method
*This function loads the webpage and check if we have cached data
*
*
*/
is_cached = async (URL, flag) => {
console.log('Cache Checker is running on ' + URL);
console.log('Please wait.....');
const updatedURL = URL.match(URL_HAS_PROTOCOL_REGEX) ? URL : `https://${URL}`;
await first_load(updatedURL, flag); //<-- This function loads the webpage first time so i can be cached
//Create browser instance
const browser = await puppeteer.launch({
userDataDir: '/tmp/user-data-dir', //<--- to save user data like cache
headless: true, //<-- false for headful mode
args: ['--no-sandbox'],
});
const page = await browser.newPage();
// Data Map to store the results
let dataHashMap = {
css: [],
html: [],
image: [],
js: [],
font: [],
compression: [],
count: {
cssCount: 0,
imageCount: 0,
jsCount: 0,
fontCount: 0,
htmlCount: 0,
otherCount: 0,
},
other: [],
};
is_leverage_cache = '';
page.on('requestfinished', (request) => {
let response = request.response();
//check for compression headers in request
if (response.headers()['content-encoding'] !== undefined)
dataHashMap.compression.push([
request.url(),
response.headers()['content-encoding'],
]);
if (request.resourceType() === 'stylesheet') {
dataHashMap.css.push([request.url(), response._fromDiskCache]);
dataHashMap.count.cssCount++;
} else if (request.resourceType() === 'script') {
dataHashMap.js.push([request.url(), response._fromDiskCache]);
dataHashMap.count.jsCount++;
} else if (request.resourceType() === 'image') {
dataHashMap.image.push([request.url(), response._fromDiskCache]);
dataHashMap.count.imageCount++;
} else if (request.resourceType() === 'font') {
dataHashMap.font.push([request.url(), response._fromDiskCache]);
dataHashMap.count.fontCount++;
} else if (request.resourceType() === 'document') {
let cache = response._fromDiskCache;
if (cache === false) {
if (response._status === 304) {
//<--The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources.
//It is an implicit redirection to a cached resource.
cache = true;
}
} else {
cache = response._fromDiskCache;
}
if (cache === true) {
is_leverage_cache = `${URL} is leveraging browser cache`;
} else {
is_leverage_cache = `${URL} does not leverage browser cache`;
}
dataHashMap.html.push([request.url(), cache]);
dataHashMap.count.htmlCount++;
} else {
dataHashMap.other.push([request.url(), response._fromDiskCache]);
dataHashMap.count.otherCount++;
}
});
await page.goto(updatedURL);
await page.waitFor(5000);
//Print the Results
console.log(dataHashMap);
console.log(
'Total Request Made:',
dataHashMap.count.cssCount +
dataHashMap.count.imageCount +
dataHashMap.count.fontCount +
dataHashMap.count.jsCount +
dataHashMap.count.htmlCount +
dataHashMap.count.otherCount
);
console.log(is_leverage_cache);
browser.close();
console.log('Completed!');
/* To check if the URL has SSL certificate */
const options = updatedURL;
var client = http; // To check if the http has a SSL certificate
if (updatedURL.indexOf('https') === 0) {
client = https;
}
var req = client.request(options, function (res) {
if (res.socket.authorized) {
console.log('The website uses SSL Certificate!!');
} else {
console.log('The website does not use SSL Certificate!!');
}
});
req.on('error', (e) => {
console.error(e);
});
req.end();
};
module.exports = { is_cached };