-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
612 lines (549 loc) · 17.3 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
const { URL } = require("url");
const http = require("http");
const https = require("https");
const querystring = require("querystring");
const generateId = require("nanoid");
const cookie = require("cookie");
const setCookie = require("set-cookie-parser");
const {
name: packageName,
version: packageVersion
} = require("./package.json");
const headerName = "x-har-request-id";
const harEntryMap = new Map();
function getDuration(a, b) {
const seconds = b[0] - a[0];
const nanoseconds = b[1] - a[1];
return seconds * 1000 + nanoseconds / 1e6;
}
function handleRequest(request, options) {
if (!options || typeof options !== "object") {
return;
}
const headers = options.headers || {};
const requestId = headers[headerName] ? headers[headerName][0] : null;
if (!requestId) {
return;
}
// Redirects! Fetch follows them (in `follow`) mode and uses the same request
// headers. So we'll see multiple requests with the same ID. We should remove
// any previous entry from `harEntryMap` and attach it has a "parent" to this
// one.
const parentEntry = harEntryMap.get(requestId);
if (parentEntry) {
harEntryMap.delete(requestId);
}
const now = Date.now();
const startTime = process.hrtime();
const url = new URL(options.url || options.href); // Depends on Node version?
const entry = {
_parent: parentEntry,
_timestamps: {
start: startTime
},
_resourceType: "fetch",
startedDateTime: new Date(now).toISOString(),
cache: {
beforeRequest: null,
afterRequest: null
},
timings: {
blocked: -1,
dns: -1,
connect: -1,
send: 0,
wait: 0,
receive: 0,
ssl: -1
},
request: {
method: request.method,
url: url.href,
cookies: buildRequestCookies(headers),
headers: buildHeaders(headers),
queryString: [...url.searchParams].map(([name, value]) => ({
name,
value
})),
headersSize: -1,
bodySize: -1
}
};
// Some versions of `node-fetch` will put `body` in the `options` received by
// this function and others exclude it. Instead we have to capture writes to
// the `ClientRequest` stream. There might be some official way to do this
// with streams, but the events and piping I tried didn't work. FIXME?
const _write = request.write;
const _end = request.end;
let requestBody;
const concatBody = chunk => {
// Assume the writer will be consistent such that we wouldn't get Buffers in
// some writes and strings in others.
if (typeof chunk === "string") {
if (requestBody == null) {
requestBody = chunk;
} else {
requestBody += chunk;
}
} else if (Buffer.isBuffer(chunk)) {
if (requestBody == null) {
requestBody = chunk;
} else {
requestBody = Buffer.concat([requestBody, chunk]);
}
}
};
request.write = function(...args) {
concatBody(...args);
return _write.call(this, ...args);
};
request.end = function(...args) {
concatBody(...args);
if (requestBody != null) {
// Works for both buffers and strings.
entry.request.bodySize = Buffer.byteLength(requestBody);
let mimeType;
for (const name in headers) {
if (name.toLowerCase() === "content-type") {
mimeType = headers[name][0];
break;
}
}
if (mimeType) {
const bodyString = requestBody.toString(); // FIXME: Assumes encoding?
if (mimeType === "application/x-www-form-urlencoded") {
entry.request.postData = {
mimeType,
params: buildParams(bodyString)
};
} else {
entry.request.postData = { mimeType, text: bodyString };
}
}
}
return _end.call(this, ...args);
};
let removeSocketListeners;
request.on("socket", socket => {
entry._timestamps.socket = process.hrtime();
const onLookup = () => {
entry._timestamps.lookup = process.hrtime();
};
const onConnect = () => {
entry._timestamps.connect = process.hrtime();
};
const onSecureConnect = () => {
entry._timestamps.secureConnect = process.hrtime();
};
socket.once("lookup", onLookup);
socket.once("connect", onConnect);
socket.once("secureConnect", onSecureConnect);
removeSocketListeners = () => {
socket.removeListener("lookup", onLookup);
socket.removeListener("connect", onConnect);
socket.removeListener("secureConnect", onSecureConnect);
};
});
request.on("finish", () => {
entry._timestamps.sent = process.hrtime();
removeSocketListeners();
});
request.on("response", response => {
entry._timestamps.firstByte = process.hrtime();
harEntryMap.set(requestId, entry);
// Now we know whether `lookup` or `connect` happened. It's possible they
// were skipped if the hostname was already resolved (or we were given an
// IP directly), or if a connection was already open (e.g. due to
// `keep-alive`).
if (!entry._timestamps.lookup) {
entry._timestamps.lookup = entry._timestamps.socket;
}
if (!entry._timestamps.connect) {
entry._timestamps.connect = entry._timestamps.lookup;
}
// Populate request info that isn't available until now.
const httpVersion = `HTTP/${response.httpVersion}`;
entry.request.httpVersion = httpVersion;
entry.response = {
status: response.statusCode,
statusText: response.statusMessage,
httpVersion,
cookies: buildResponseCookies(response.headers),
headers: buildHeaders(response.rawHeaders),
content: {
size: -1,
mimeType: response.headers["content-type"]
},
redirectURL: response.headers.location || "",
headersSize: -1,
bodySize: -1
};
// Detect supported compression encodings.
const compressed = /^(gzip|compress|deflate|br)$/.test(
response.headers["content-encoding"]
);
if (compressed) {
entry._compressed = true;
response.on("data", chunk => {
if (entry.response.bodySize === -1) {
entry.response.bodySize = 0;
}
entry.response.bodySize += Buffer.byteLength(chunk);
});
}
});
}
/**
* Support the three possible header formats we'd get from a request or
* response:
*
* - A flat array with both names and values: [name, value, name, value, ...]
* - An object with array values: { name: [value, value] }
* - An object with string values: { name: value }
*/
function buildHeaders(headers) {
const list = [];
if (Array.isArray(headers)) {
for (let i = 0; i < headers.length; i += 2) {
list.push({
name: headers[i],
value: headers[i + 1]
});
}
} else {
Object.keys(headers).forEach(name => {
const values = Array.isArray(headers[name])
? headers[name]
: [headers[name]];
values.forEach(value => {
list.push({ name, value });
});
});
}
return list;
}
function buildRequestCookies(headers) {
const cookies = [];
for (const header in headers) {
if (header.toLowerCase() === "cookie") {
headers[header].forEach(headerValue => {
const parsed = cookie.parse(headerValue);
for (const name in parsed) {
const value = parsed[name];
cookies.push({ name, value });
}
});
}
}
return cookies;
}
function buildParams(paramString) {
const params = [];
const parsed = querystring.parse(paramString);
for (const name in parsed) {
const value = parsed[name];
if (Array.isArray(value)) {
value.forEach(item => {
params.push({ name, value: item });
});
} else {
params.push({ name, value });
}
}
return params;
}
function buildResponseCookies(headers) {
const cookies = [];
const setCookies = headers["set-cookie"];
if (setCookies) {
setCookies.forEach(headerValue => {
let parsed;
try {
parsed = setCookie.parse(headerValue);
} catch (err) {
return;
}
parsed.forEach(cookie => {
const { name, value, path, domain, expires, httpOnly, secure } = cookie;
const harCookie = {
name,
value,
httpOnly: httpOnly || false,
secure: secure || false
};
if (path) {
harCookie.path = path;
}
if (domain) {
harCookie.domain = domain;
}
if (expires) {
harCookie.expires = expires.toISOString();
}
cookies.push(harCookie);
});
});
}
return cookies;
}
/**
* Instrument an existing Agent instance. This overrides the instance's
* `addRequest` method. It should be fine to continue using for requests made
* without `withHar` - if the request doesn't have our `x-har-request-id`
* header, it won't do anything extra.
*/
function instrumentAgentInstance(agent) {
const { addRequest: originalAddRequest } = agent;
if (!originalAddRequest.isHarEnabled) {
agent.addRequest = function addRequest(request, ...args) {
handleRequest(request, ...args);
return originalAddRequest.call(this, request, ...args);
};
agent.addRequest.isHarEnabled = true;
}
}
function createAgentClass(BaseAgent) {
class HarAgent extends BaseAgent {
constructor(...args) {
super(...args);
this.addRequest.isHarEnabled = true;
}
// This method is undocumented in the Node.js Agent docs. But every custom
// agent implementation out there uses it, so...
addRequest(request, ...args) {
handleRequest(request, ...args);
return super.addRequest(request, ...args);
}
}
return HarAgent;
}
const HarHttpAgent = createAgentClass(http.Agent);
const HarHttpsAgent = createAgentClass(https.Agent);
// Shared agent instances.
let globalHttpAgent;
let globalHttpsAgent;
function getInputUrl(input) {
// Support URL or Request object.
const url = typeof input === "string" ? input : input.url;
return new URL(url);
}
function addHeaders(oldHeaders, newHeaders) {
if (!oldHeaders) {
return newHeaders;
} else if (
typeof oldHeaders.set === "function" &&
typeof oldHeaders.constructor === "function"
) {
const Headers = oldHeaders.constructor;
const headers = new Headers(oldHeaders);
for (const name in newHeaders) {
headers.set(name, newHeaders[name]);
}
return headers;
} else {
return Object.assign({}, oldHeaders, newHeaders);
}
}
function getAgent(input, options) {
if (options.agent) {
if (typeof options.agent === "function") {
return function(...args) {
const agent = options.agent.call(this, ...args);
if (agent) {
instrumentAgentInstance(agent);
return agent;
}
return getGlobalAgent(input);
};
}
instrumentAgentInstance(options.agent);
return options.agent;
}
return getGlobalAgent(input);
}
function getGlobalAgent(input) {
const url = getInputUrl(input);
if (url.protocol === "http:") {
if (!globalHttpAgent) {
globalHttpAgent = new HarHttpAgent();
}
return globalHttpAgent;
}
if (!globalHttpsAgent) {
globalHttpsAgent = new HarHttpsAgent();
}
return globalHttpsAgent;
}
function withHar(baseFetch, defaults = {}) {
return function fetch(input, options = {}) {
const {
har = defaults.har,
harPageRef = defaults.harPageRef,
onHarEntry = defaults.onHarEntry
} = options;
if (har === false) {
return baseFetch(input, options);
}
// Ideally we could just attach the generated entry data to the request
// directly, like via a header. An ideal place would be in a header, but the
// headers are already processed by the time the response is finished, so we
// can't add it there.
//
// We could also give each request its own Agent instance that knows how to
// populate an entry for each given request, but it seems expensive to
// create new one for every single request.
//
// So instead, we generate an ID for each request and attach it to a request
// header. The agent then adds the entry data to `harEntryMap` using the ID
// as a key.
const requestId = generateId();
options = Object.assign({}, options, {
headers: addHeaders(options.headers, { [headerName]: requestId }),
// node-fetch 2.x supports a function here, but 1.x does not. So parse
// the URL and implement protocol-switching ourselves.
agent: getAgent(input, options)
});
return baseFetch(input, options).then(
async response => {
const entry = harEntryMap.get(requestId);
harEntryMap.delete(requestId);
if (!entry) {
return response;
}
// We need to consume the decoded response in order to populate the
// `response.content` field.
const text = await response.text();
const { _timestamps: time } = entry;
time.received = process.hrtime();
const parents = [];
let child = entry;
do {
let parent = child._parent;
// Remove linked parent references as they're flattened.
delete child._parent;
if (parent) {
parents.unshift(parent);
}
child = parent;
} while (child);
// In some versions of `node-fetch`, the returned `response` is actually
// an instance of `Body`, not `Response`, and the `Body` class does not
// set a `headers` property when constructed. So instead of using
// `response.constructor`, try to get `Response` from other places, like
// on the given Fetch instance or the global scope (like `isomorphic-fetch`
// sets). If all else fails, you can override the class used via the
// `Response` option to `withHar`.
const Response =
defaults.Response ||
baseFetch.Response ||
global.Response ||
response.constructor;
// `clone()` is broken in `node-fetch` and results in a stalled Promise
// for responses above a certain size threshold. So construct a similar
// clone ourselves...
const responseCopy = new Response(text, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
// These are not spec-compliant `Response` options, but `node-fetch`
// has them.
ok: response.ok,
size: response.size,
url: response.url
});
// Allow grouping by pages.
entry.pageref = harPageRef || "page_1";
parents.forEach(parent => {
parent.pageref = entry.pageref;
});
// Response content info.
const bodySize = Buffer.byteLength(text);
entry.response.content.text = text;
entry.response.content.size = bodySize;
if (entry._compressed) {
if (entry.response.bodySize !== -1) {
entry.response.content.compression =
entry.response.content.size - entry.response.bodySize;
}
} else {
entry.response.bodySize = bodySize;
}
// Finalize timing info.
// Chrome's HAR viewer (the Network panel) is broken and doesn't honor
// the HAR spec. If `blocked` is not a positive number, it shows the
// `wait` time as stalled instead of the time waiting for the response.
entry.timings.blocked = Math.max(
getDuration(time.start, time.socket),
0.01 // Minimum value, see above.
);
entry.timings.dns = getDuration(time.socket, time.lookup);
entry.timings.connect = getDuration(
time.lookup,
// For backwards compatibility with HAR 1.1, the `connect` timing
// includes `ssl` instead of being mutually exclusive.
time.secureConnect || time.connect
);
if (time.secureConnect) {
entry.timings.ssl = getDuration(time.connect, time.secureConnect);
}
entry.timings.send = getDuration(
time.secureConnect || time.connect,
time.sent
);
entry.timings.wait = Math.max(
// Seems like it might be possible to receive a response before the
// request fires its `finish` event. This is just a hunch and it would
// be worthwhile to disprove.
getDuration(time.sent, time.firstByte),
0
);
entry.timings.receive = getDuration(time.firstByte, time.received);
entry.time = getDuration(time.start, time.received);
responseCopy.harEntry = entry;
if (har && typeof har === "object") {
har.log.entries.push(...parents, entry);
}
if (onHarEntry) {
parents.forEach(parent => {
onHarEntry(parent);
});
onHarEntry(entry);
}
return responseCopy;
},
err => {
harEntryMap.delete(requestId);
throw err;
}
);
};
}
withHar.harEntryMap = harEntryMap;
function createHarLog(entries = [], pageInfo = {}) {
return {
log: {
version: "1.2",
creator: {
name: packageName,
version: packageVersion
},
pages: [
Object.assign(
{
startedDateTime: new Date().toISOString(),
id: "page_1",
title: "Page",
pageTimings: {
onContentLoad: -1,
onLoad: -1
}
},
pageInfo
)
],
entries
}
};
}
exports.withHar = withHar;
exports.createHarLog = createHarLog;