Skip to content

Commit

Permalink
Parse extra info events (#71)
Browse files Browse the repository at this point in the history
* Include extra info in request
* Include extra info in response. Filter blocked cookies.
  • Loading branch information
Michael Dijkstra authored Jul 29, 2020
1 parent 231f47f commit 4586d29
Show file tree
Hide file tree
Showing 7 changed files with 7,120 additions and 11 deletions.
84 changes: 83 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { v1 } = require('uuid');
const dayjs = require('dayjs');
const debug = require('debug')(name);
const ignoredEvents = require('./lib/ignoredEvents');
const { parseRequestCookies } = require('./lib/cookies');
const { parseRequestCookies, formatCookie } = require('./lib/cookies');
const { getHeaderValue, parseHeaders } = require('./lib/headers');
const {
formatMillis,
Expand Down Expand Up @@ -262,6 +262,87 @@ module.exports = {
}
break;

case 'Network.requestWillBeSentExtraInfo':
{
if (ignoredRequests.has(params.requestId)) {
continue;
}

const entry = entries.find(
entry => entry._requestId === params.requestId
);
if (!entry) {
debug(
`Extra info sent for requestId ${
params.requestId
} with no matching request.`
);
continue;
}

if (params.headers) {
entry.request.headers = entry.request.headers.concat(
parseHeaders(params.headers)
);
}

if (params.associatedCookies) {
entry.request.cookies = (entry.request.cookies || []).concat(
params.associatedCookies
.filter(({ blockedReasons }) => !blockedReasons.length)
.map(({ cookie }) => formatCookie(cookie))
);
}
}
break;

case 'Network.responseReceivedExtraInfo':
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
}

if (ignoredRequests.has(params.requestId)) {
continue;
}

let entry = entries.find(
entry => entry._requestId === params.requestId
);

if (!entry) {
entry = entriesWithoutPage.find(
entry => entry._requestId === params.requestId
);
}

if (!entry) {
debug(
`Received response extra info for requestId ${
params.requestId
} with no matching request.`
);
continue;
}

if (!entry.response) {
// Extra info received before response
entry.extraResponseInfo = {
headers: parseHeaders(params.headers),
blockedCookies: params.blockedCookies
};
continue;
}

if (params.headers) {
entry.response.headers = entry.response.headers.concat(
parseHeaders(params.headers)
);
}
}
break;

case 'Network.responseReceived':
{
if (pages.length < 1) {
Expand All @@ -283,6 +364,7 @@ module.exports = {
entry => entry._requestId === params.requestId
);
}

if (!entry) {
debug(
`Received network response for requestId ${
Expand Down
21 changes: 13 additions & 8 deletions lib/cookies.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const Cookie = require('tough-cookie').Cookie;
const dayjs = require('dayjs');

function parseCookie(cookieString) {
let cookie = Cookie.parse(cookieString);
if (!cookie) {
return undefined;
}

function formatCookie(cookie) {
return {
name: cookie.key,
name: cookie.key || cookie.name,
value: cookie.value,
path: cookie.path || undefined, // must be undefined, not null, to exclude empty path
domain: cookie.domain || undefined, // must be undefined, not null, to exclude empty domain
Expand All @@ -21,6 +16,15 @@ function parseCookie(cookieString) {
};
}

function parseCookie(cookieString) {
let cookie = Cookie.parse(cookieString);
if (!cookie) {
return undefined;
}

return formatCookie(cookie);
}

function splitAndParse(header, divider) {
return header
.split(divider)
Expand All @@ -35,5 +39,6 @@ module.exports = {
},
parseResponseCookies(cookieHeader) {
return splitAndParse(cookieHeader, '\n');
}
},
formatCookie
};
29 changes: 27 additions & 2 deletions lib/entryFromResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ module.exports = function(entry, response, page, options) {
const responseHeaders = response.headers;
const cookieHeader = getHeaderValue(responseHeaders, 'Set-Cookie');

let cookies = parseResponseCookies(cookieHeader);
let headers = parseHeaders(responseHeaders);

if (entry.extraResponseInfo) {
if (entry.extraResponseInfo.headers) {
headers = entry.extraResponseInfo.headers.concat(
headers.filter(
({ name }) => !headers.find(header => header.name === name)
)
);
}

if (entry.extraResponseInfo.blockedCookies) {
cookies = cookies.filter(
({ name }) =>
!entry.extraResponseInfo.blockedCookies.find(
({ cookie }) => cookie.name === name
)
);
}

// Remove extra info once it has been added to the response
delete entry.extraResponseInfo;
}

// response.body must be set by the library user, by either calling
// Network.getResponseBody or Network.getResponseBodyForInterception as it is
// not part of the Chrome DevTools Protocol specification.
Expand All @@ -51,8 +76,8 @@ module.exports = function(entry, response, page, options) {
},
headersSize: -1,
bodySize: -1,
cookies: parseResponseCookies(cookieHeader),
headers: parseHeaders(responseHeaders),
cookies,
headers,
_transferSize: response.encodedDataLength
};

Expand Down
Loading

0 comments on commit 4586d29

Please sign in to comment.