-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (138 loc) Β· 4.68 KB
/
Copy pathindex.js
File metadata and controls
163 lines (138 loc) Β· 4.68 KB
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 url = require('url');
var http = require('http');
var https = require('https');
var EventEmitter = require('events').EventEmitter;
var mixin = require('merge-descriptors');
var Timer = require('./timer');
//var agentBase = require('agent-base');
// TODO: Try to support these standards...
// PerformanceResourceTiming: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming
// HAR: http://www.softwareishard.com/blog/har-12-spec/#timings
function hookCallback (obj, member, pre_cb) {
var orig_fn = obj[member];
obj[member] = function () {
var args = Array.prototype.slice.call(arguments);
// FIXME: args.length or args.length-1?
var n = typeof args[args.length-1] === 'function' ? 1 : 0;
var orig_cb = args[args.length-n];
args[args.length-n] = () => {
pre_cb();
if (orig_cb)
return orig_cb.apply(this, arguments);
};
return orig_fn.apply(this, args);
};
}
exports.wrap = function createHttp(httpModule) {
var httpModule = httpModule;
var MeasureHttp = Object.create(httpModule);
mixin(MeasureHttp, EventEmitter.prototype);
MeasureHttp.request = request;
MeasureHttp.get = get;
//MeasureHttp.mixin = patchMethods;
//MeasureHttp.unmix = unpatchMethods;
return MeasureHttp;
function request (options, onResponse) {
var uri = getUriFromOptions(options);
var timer = new Timer();
timer.mark('start');
var req = httpModule.request(options, onResponse);
req.http_timer = timer;
//if(!usingPatched(httpModule.request)) {
setupTimerForRequest(timer, req, uri);
//}
return req;
}
function get (options, onResponse) {
var req = request(options, onResponse);
req.end();
return req;
}
function getUriFromOptions (options) {
var uri = options;
if(typeof uri === 'string') uri = url.parse(uri);
if(uri.uri) uri = uri.uri
return uri;
}
function getIdent(socket) {
return {
date: Date.now(),
//address: [socket.localAddress, socket.remoteAddress],
//port: [socket.localPort, socket.remotePort],
local: [socket.localAddress, socket.localPort],
remote: [socket.remoteAddress, socket.remotePort],
};
}
function setupTimerForRequest (timer, req, uri) {
hookCallback(req, 'end', timer.mark.bind(timer, 'sent'));
//req.socket.on('abort', timer.mark.bind(timer, 'abort')); // ?
req.on('socket', (socket) => {
timer.mark('socket');
//console.log('socket.agent');
//console.log(socket.address());
// Keep-Alive
var alreadyConnected = !(socket._connecting || socket.connecting);
if (!alreadyConnected) {
req.socket.on('lookup', timer.mark.bind(timer, 'lookup'));
req.socket.on('connect', timer.mark.bind(timer, 'connect'));
req.socket.on('secureConnect', timer.mark.bind(timer, 'ssl_connect'));
}
//req.socket.once('data', timer.mark.bind(timer, 'response'));
req.socket.on('error', () => {
console.log('ERR');
});
timer.ident = getIdent(socket);
req.socket.on('connect', () => {
// _sockname: { address: '192.168.1.83', family: 'IPv4', port: 53028 }
// _peername: { address: '172.217.17.78', family: 'IPv4', port: 443 } }
timer.ident = getIdent(socket);
MeasureHttp.emit('socket-connect', timer.ident);
});
/*
socket._handle.TLSWrap:
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
*/
if (0)
req.socket.on('secureConnect', () => {
console.log(req.socket);
var ps = req.socket.getPeerCertificate();
console.log(ps);
});
});
req.on('open', timer.mark.bind(timer, 'open'));
req.on('close', timer.mark.bind(timer, 'close'));
req.on('error', timer.mark.bind(timer, 'error'));
req.on('abort', timer.mark.bind(timer, 'abort')); // ?
req.on('abort', () => { console.log('ABORT'); }); // ?
req.on('response', function(response) {
timer.mark('response');
response.on('end', timer.mark.bind(timer, 'end'));
response.on('end', function() {
var stats = timer.stats();
stats.statusCode = response.statusCode;
stats.method = req.method || 'GET';
MeasureHttp.emit('stat', stats, uri);
});
});
}
};
/// http://www.softwareishard.com/blog/har-12-spec/#pageTimings
exports.toHAR = function (t) {
return {
blocked: t.socket - t.start,
dns: t.lookup - t.start,
connect: t.connect - t.socket,
ssl: t.ssl_connect - t.connect,
firstByte: t.response - t.connect,
receive: t.end - t.response,
total: t.end - t.start
};
}
exports.create = function () {
return {
http: exports.wrap(http),
https: exports.wrap(https)
};
};