-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module HAR #263
Module HAR #263
Changes from 12 commits
803131e
d854956
c8ed0bb
f14204d
ddc7819
f3748eb
c632fcb
d3088b1
0036a36
4b49d6d
7f45e2c
8e6daa7
2ddef09
d06f915
09891a4
713bbcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/** | ||
* Log requests for build HAR output | ||
* | ||
* @see: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html | ||
*/ | ||
|
||
var fs = require('fs'); | ||
|
||
/** | ||
* From netsniff.js with small workarounds | ||
* | ||
* @see: https://github.com/ariya/phantomjs/blob/master/examples/netsniff.js | ||
*/ | ||
|
||
function createHAR(address, title, startTime, endTime, resources, creator) | ||
{ | ||
var entries = []; | ||
|
||
resources.forEach(function (resource) { | ||
var request = resource.request, | ||
startReply = resource.startReply, | ||
endReply = resource.endReply; | ||
|
||
if (!request || !startReply || !endReply) { | ||
return; | ||
} | ||
|
||
// Exclude Data URI from HAR file because | ||
// they aren't included in specification | ||
if (request.url.match(/(^data:image\/.*)/i)) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation? |
||
|
||
entries.push({ | ||
startedDateTime: request.time.toISOString(), | ||
time: endReply.time - request.time, | ||
request: { | ||
method: request.method, | ||
url: request.url, | ||
httpVersion: "HTTP/1.1", | ||
cookies: [], | ||
headers: request.headers, | ||
queryString: [], | ||
headersSize: -1, | ||
bodySize: -1 | ||
}, | ||
response: { | ||
status: endReply.status, | ||
statusText: endReply.statusText, | ||
httpVersion: "HTTP/1.1", | ||
cookies: [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about cookies? |
||
headers: endReply.headers, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about |
||
redirectURL: "", | ||
headersSize: -1, | ||
bodySize: startReply.bodySize, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not accurate but that's phantomJS' fault. In a project of mine, I actually request each resource separately to get the accurate number of bytes transferred (and the content of each response). It's a pain. |
||
content: { | ||
size: startReply.bodySize, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know there's an open phantomJS bug open, but it'd be nice if we could optionally add the content |
||
mimeType: endReply.contentType === null ? "" : endReply.contentType | ||
} | ||
}, | ||
cache: {}, | ||
timings: { | ||
blocked: 0, | ||
dns: -1, | ||
connect: -1, | ||
send: 0, | ||
wait: startReply.time - request.time, | ||
receive: endReply.time - startReply.time, | ||
ssl: -1 | ||
}, | ||
pageref: address | ||
}); | ||
}); | ||
|
||
return { | ||
log: { | ||
version: '1.2', | ||
creator: creator, | ||
pages: [{ | ||
startedDateTime: startTime.toISOString(), | ||
id: address, | ||
title: title, | ||
pageTimings: { | ||
onLoad: endTime.getTime() - startTime.getTime() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html#sec-object-types-pageTimings HAR spec mentions the optional |
||
} | ||
}], | ||
entries: entries | ||
} | ||
}; | ||
} | ||
/** End **/ | ||
|
||
exports.module = function(phantomas) { | ||
|
||
var param = phantomas.getParam('har'), | ||
path = ''; | ||
|
||
var creator = { | ||
name: "Phantomas - har", | ||
version: phantomas.getVersion() | ||
}; | ||
|
||
if (typeof param === 'undefined') { | ||
phantomas.log('No HAR path specified, use --har <path>'); | ||
return; | ||
} | ||
|
||
// --har | ||
if (param === true) { | ||
// defaults to "2013-12-07T20:15:01.521Z.har" | ||
path = (new Date()).toJSON() + '.har'; | ||
} | ||
// --har [file name] | ||
else { | ||
path = param; | ||
} | ||
|
||
phantomas.log('HAR path: %s', path); | ||
|
||
var resources = []; | ||
var startTime; | ||
var endTime; | ||
var page; | ||
|
||
phantomas.on('pageBeforeOpen', function(p) { | ||
page = p; | ||
}); | ||
|
||
phantomas.on('pageOpen', function() { | ||
startTime = new Date(); | ||
}); | ||
|
||
phantomas.on('loadFinished', function() { | ||
endTime = new Date(); | ||
}); | ||
|
||
phantomas.on('onResourceRequested', function(res, req) { | ||
resources[res.id] = { | ||
request: res, | ||
startReply: null, | ||
endReply: null | ||
}; | ||
|
||
}); | ||
|
||
phantomas.on('onResourceReceived', function(res) { | ||
if (res.stage === 'start') | ||
resources[res.id].startReply = res; | ||
|
||
if (res.stage === 'end') | ||
resources[res.id].endReply = res; | ||
}); | ||
|
||
phantomas.on('report', function() { | ||
var address = page.url; | ||
var title = page.title; | ||
|
||
// Warning page was not finished correctly | ||
if (! endTime) | ||
endTime = new Date(); | ||
|
||
phantomas.log('Create HAR'); | ||
var har = createHAR(address, title, startTime, endTime, resources, creator); | ||
|
||
phantomas.log('Convert HAR to JSON'); | ||
var dump = JSON.stringify(har); | ||
|
||
phantomas.log('Write HAR in \'%s\'', path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrap with double quotes so you don't have to escape single quotes |
||
fs.write(path, dump); | ||
|
||
phantomas.log('HAR Done !'); | ||
}); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a newline at EOF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just check if the first five chars are
data: