-
Notifications
You must be signed in to change notification settings - Fork 0
/
yslow.js
355 lines (309 loc) · 12 KB
/
yslow.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
var fs = require('fs');
var yslowSource;
// yslow source ready
function initReadYslowSource(dirname){
var sources = [];
var basePath = dirname + "/yslow/";
"yslow.js \
version.js \
componentSet.js \
component.js \
component-ph.js \
controller.js \
util.js \
doc.js \
rules.js \
rulesets/sample.js \
resultset.js \
view.js \
context.js \
renderers.js \
peeler.js \
peeler-bm-ch-ph.js"
.split(" ")
.forEach(function(file){
var path = basePath + file.trim();
var contents = fs.read(path)
sources.push(contents);
});
return yslowSource = sources.join("\n");
}
exports.run = function (options, done){
if(!yslowSource) {
initReadYslowSource(options.dirname);
}
var yslowArgs = {
info: 'all',
format: 'json',
ruleset: 'ydefault',
beacon: false,
ua: false,
viewport: false,
headers: false,
console: 0,
threshold: 80,
// set yslow unary args
dict: false,
verbose: false
};
var url = options.url;
var page = require('webpage').create();
page.resources = {};
// allow x-domain requests, used to retrieve components content
page.settings.webSecurityEnabled = false;
// request
page.onResourceRequested = function (req) {
page.resources[req.url] = {
request: req
};
};
// response
page.onResourceReceived = function (res) {
var info,
resp = page.resources[res.url].response;
if (!resp) {
page.resources[res.url].response = res;
} else {
for (info in res) {
if (res.hasOwnProperty(info)) {
resp[info] = res[info];
}
}
}
};
// enable console output, useful for debugging
yslowArgs.console = parseInt(yslowArgs.console, 10) || 0;
if (yslowArgs.console) {
if (yslowArgs.console === 1) {
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.onError = function (msg) {
console.error(msg);
};
} else {
page.onConsoleMessage = function (msg, line, source) {
console.log(JSON.stringify({
message: msg,
lineNumber: line,
source: source
}, null, 4));
};
page.onError = function (msg, trace) {
console.error(JSON.stringify({
message: msg,
stacktrace: trace
}));
};
}
} else {
page.onError = function () {
// catch uncaught error from the page
};
}
// set user agent string
if (yslowArgs.ua) {
page.settings.userAgent = yslowArgs.ua;
}
// set page viewport
if (yslowArgs.viewport) {
var viewport = yslowArgs.viewport.toLowerCase();
page.viewportSize = {
width: parseInt(viewport.slice(0, viewport.indexOf('x')), 10) ||
page.viewportSize.width,
height: parseInt(viewport.slice(viewport.indexOf('x') + 1), 10) ||
page.viewportSize.height
};
}
// set custom headers
if (yslowArgs.headers) {
try {
page.customHeaders = JSON.parse(yslowArgs.headers);
} catch (err) {
console.log('Invalid custom headers: ' + err);
}
}
// open page
page.startTime = new Date();
page.open(url, function (status) {
var yslow, ysphantomjs, controller, evalFunc, loadTime, url, resp,
startTime = page.startTime,
resources = page.resources;
if (status !== 'success') {
console.log('FAIL to load ' + url);
} else {
// page load time
loadTime = new Date() - startTime;
// set resources response time
for (url in resources) {
if (resources.hasOwnProperty(url)) {
resp = resources[url].response;
if (resp) {
resp.time = new Date(resp.time) - startTime;
}
}
}
// yslow wrapper to be evaluated by page
// yslow = function () {
//YSLOW HERE
// };
// serialize YSlow phantomjs object
// resources, yslow args and page load time
ysphantomjs = 'YSLOW.phantomjs = {' +
'resources: ' + JSON.stringify(resources) + ',' +
'args: ' + JSON.stringify(yslowArgs) + ',' +
'loadTime: ' + JSON.stringify(loadTime) + '};';
// YSlow phantomjs controller
controller = function () {
YSLOW.phantomjs.run = function () {
try {
var results, xhr, output, threshold,
doc = document,
ys = YSLOW,
yscontext = new ys.context(doc),
yspeeler = ys.peeler,
comps = yspeeler.peel(doc),
baseHref = yspeeler.getBaseHref(doc),
cset = new ys.ComponentSet(doc),
ysphantomjs = ys.phantomjs,
resources = ysphantomjs.resources,
args = ysphantomjs.args,
ysutil = ys.util,
// format out with appropriate content type
formatOutput = function (content, format) {
format = format || (args.format || '').toLowerCase();
var harness = {
'tap': {
func: ysutil.formatAsTAP,
contentType: 'text/plain'
},
'junit': {
func: ysutil.formatAsJUnit,
contentType: 'text/xml'
}
};
switch (format) {
case 'xml':
return {
content: ysutil.objToXML(content),
contentType: 'text/xml'
};
case 'plain':
return {
content: ysutil.prettyPrintResults(
content
),
contentType: 'text/plain'
};
// test formats
case 'tap':
case 'junit':
try {
threshold = JSON.parse(args.threshold);
} catch (err) {
threshold = args.threshold;
}
return {
content: harness[format].func(
ysutil.testResults(
content,
threshold
)
),
contentType: harness[format].contentType
};
default:
return {
content: JSON.stringify(content),
contentType: 'application/json'
};
}
},
// format raw headers into object
formatHeaders = function (headers) {
var reHeader = /^([^:]+):\s*([\s\S]+)$/,
reLineBreak = /[\n\r]/g,
header = {};
headers.split('\n').forEach(function (h) {
var m = reHeader.exec(
h.replace(reLineBreak, '')
);
if (m) {
header[m[1]] = m[2];
}
});
return header;
};
comps.forEach(function (comp) {
var res = resources[comp.href] || {};
cset.addComponent(
comp.href,
comp.type,
comp.base || baseHref,
{
obj: comp.obj,
request: res.request,
response: res.response
}
);
});
// refinement
cset.inline = ysutil.getInlineTags(doc);
cset.domElementsCount = ysutil.countDOMElements(doc);
cset.cookies = cset.doc_comp.cookie;
cset.components = ysutil.setInjected(doc,
cset.components, cset.doc_comp.body);
// run analysis
yscontext.component_set = cset;
ys.controller.lint(doc, yscontext, args.ruleset);
yscontext.result_set.url = baseHref;
yscontext.PAGE.t_done = ysphantomjs.loadTime;
yscontext.collectStats();
results = ysutil.getResults(yscontext, args.info);
// prepare output results
if (args.dict && args.format !== 'plain') {
results.dictionary = ysutil.getDict(args.info,
args.ruleset);
}
// output = {};
// ['json', 'tap'].forEach(function(format){
// output[format] = formatOutput(results, format);
// })
output = formatOutput(results);
// return JSON.stringify(output);
return output.content;
} catch (err) {
return err;
}
};
return YSLOW.phantomjs.run();
};
// serialize then combine:
// YSlow + page resources + args + loadtime + controller
// yslow = yslow.toString();
// yslow = yslow.slice(13, yslow.length - 1);
yslow = yslowSource;
// minification removes last ';'
if (yslow.slice(yslow.length - 1) !== ';') {
yslow += ';';
}
controller = controller.toString();
controller = controller.slice(13, controller.length - 1);
evalFunc = new Function(yslow + ysphantomjs + controller);
// evaluate script and log results
var res = page.evaluate(evalFunc);
if(typeof res === 'string'){
res = JSON.parse(res)
}
var yslowJSONReport = './yslow/'+ encodeURIComponent(cliConfig.url) +'.json';
if(fs.exists(yslowJSONReport)){
fs.remove(yslowJSONReport);
}
var f = fs.open(yslowJSONReport, "w");
f.writeLine(JSON.stringify(res, undefined, 4));
f.close();
}
// finish yslow
done(res);
});
};