-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
163 lines (148 loc) · 4.65 KB
/
index.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
var Chrome = require('chrome-remote-interface')
var prettyBytes = require('pretty-bytes')
var evaluate = require('./helpers/evaluate.js')
var Plan = require('./helpers/plan')
var find = require('./helpers/find')
function Run(opts) {
this.opts = opts
this.p = new Plan(4)
this.stats = {}
this.stats.requests = []
this.stats.network = {}
this.stats.network.total = 0
this.stats.network.typesUsage = {
"Document": { count: 0, weight: 0},
"Fetch": { count: 0, weight: 0},
"Font": { count: 0, weight: 0},
"Image": { count: 0, weight: 0},
"Other": { count: 0, weight: 0},
"Script": { count: 0, weight: 0},
"Stylesheet": { count: 0, weight: 0},
"WebSocket": { count: 0, weight: 0},
"XHR": { count: 0, weight: 0}
}
this.stats.unused = {}
// Find unused elements
this.p.on("done::els", this._onElsDone.bind(this))
// Count types usage and total page weight
this.p.on('done::requests', this._onRequestsDone.bind(this))
// all done
this.p.on('done', this._onDone.bind(this))
}
Run.prototype.init = function() {
var self = this
Chrome(function (chrome) {
self.chrome = chrome
evaluate.setChrome(chrome)
self.chrome.Page.enable()
self.emulate()
})
}
Run.prototype.emulate = function() {
this.chrome.send('Page.setDeviceMetricsOverride', {
mobile: this.opts.mobile,
width: parseInt(this.opts.width),
height: parseInt(this.opts.height),
emulateViewport: this.opts.emulateViewport || true,
fitWindow: true,
deviceScaleFactor: parseInt(this.opts.deviceScaleFactor),
scale: parseInt(this.opts.scale)
}, this._handleEmulate.bind(this))
}
Run.prototype._handleEmulate = function(err) {
if ( err ) {
this.chrome.close()
return this.opts.cb(new Error("Issue in emulate"))
}
var self = this
this.chrome.Network.enable()
if ( !this.opts.userAgent ) {
this.chrome.Page.navigate({'url': this.opts.link})
// Collect requests
this.collectReqs()
} else {
this.setUa()
}
this._handleOnload()
}
Run.prototype.setUa = function() {
var self = this
this.chrome.send('Network.setUserAgentOverride', {
'userAgent': this.opts.userAgent
}, function(err) {
if ( err ) {
self.chrome.close()
return self.opts.cb(new Error("Issue in overriding user-agent"))
}
self.chrome.Page.navigate({'url': self.opts.link})
// Collect requests
self.collectReqs()
})
}
Run.prototype.collectReqs = function() {
var self = this
this.chrome.on('Network.responseReceived', function (req) {
if ( req.response.url.substr(0, 6) == "chrome" ) return
var contentLength = parseInt(req.response.headers['Content-Length']) || 0
self.stats.requests.push(
{
url: req.response.url,
type: req.type,
contentLength: contentLength,
humanWeight: prettyBytes(contentLength)
}
)
})
}
Run.prototype._handleOnload = function() {
var self = this
this.chrome.on('Page.loadEventFired', function() {
self.chrome.Runtime.enable()
self.p.done('requests', self.stats.requests)
// Collect unused elements
evaluate.eval(__dirname + '/unusedElements.js', function(err, data) {
if ( err ) {
self.chrome.close()
return self.opts.cb(new Error("Issue in finding unused elements."))
}
self.stats.unused.elements = JSON.parse(data.result.value)
self.p.done("els", self.stats.unused.elements)
})
})
}
Run.prototype._onElsDone = function() {
this.stats.unused.bytes = { total: 0, list: [] }
var bytes = this.stats.unused.bytes
var unusedEls = this.stats.unused.elements
for (var i = 0; i < unusedEls.length; i++) {
obj = find(unusedEls[i].images, this.stats.requests, "url")
if ( obj ) {
bytes.list.push(obj)
bytes.total += parseInt(obj.contentLength)
obj.humanWeight = prettyBytes(parseInt(obj.contentLength))
}
}
bytes.humanWeight = prettyBytes(bytes.total)
this.p.done()
}
Run.prototype._onRequestsDone = function(requests) {
// aggregate network stats!
for (var i = 0; i < requests.length; i++) {
if ( requests[i].contentLength ) {
this.stats.network.total += parseInt(requests[i].contentLength)
this.stats.network.typesUsage[requests[i].type].weight += parseInt(requests[i].contentLength)
}
this.stats.network.typesUsage[requests[i].type].count++
}
this.stats.network.humanTotal = prettyBytes(this.stats.network.total)
for (var k in this.stats.network.typesUsage) {
var type = this.stats.network.typesUsage[k]
type.humanWeight = prettyBytes(type.weight)
}
this.p.done()
}
Run.prototype._onDone = function() {
this.chrome.close()
this.opts.cb(false, this.stats)
}
module.exports = Run