Skip to content
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

Wait for loadingFinished before requesting response body #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions lib/PuppeteerHar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PuppeteerHar {
cleanUp() {
this.network_events = [];
this.page_events = [];
this.response_body_promises = [];
this.response_body_promises = {};
}

/**
Expand Down Expand Up @@ -68,7 +68,24 @@ class PuppeteerHar {
return;
}
this.network_events.push({ method, params });

if (method=='Network.loadingFinished' && params.requestId in this.response_body_promises ) {
const {response,resolve} = this.response_body_promises[params.requestId]
const {requestId} = params
this.client.send(
'Network.getResponseBody', { requestId },
).then((responseBody) => {
// Set the response so `chrome-har` can add it to the HAR
response.body = new Buffer.from(
responseBody.body,
responseBody.base64Encoded ? 'base64' : undefined,
).toString();
}, (reason) => {
// Resources (i.e. response bodies) are flushed after page commits
// navigation and we are no longer able to retrieve them. In this
// case, fail soft so we still add the rest of the response to the
// HAR. Possible option would be force wait before navigation...
}).then(resolve)
}
if (this.saveResponse && method == 'Network.responseReceived') {
const response = params.response;
const requestId = params.requestId;
Expand All @@ -78,21 +95,17 @@ class PuppeteerHar {
response.headers.location == null &&
this.captureMimeTypes.includes(response.mimeType)
) {
const promise = this.client.send(
'Network.getResponseBody', { requestId },
).then((responseBody) => {
// Set the response so `chrome-har` can add it to the HAR
params.response.body = new Buffer.from(
responseBody.body,
responseBody.base64Encoded ? 'base64' : undefined,
).toString();
}, (reason) => {
// Resources (i.e. response bodies) are flushed after page commits
// navigation and we are no longer able to retrieve them. In this
// case, fail soft so we still add the rest of the response to the
// HAR. Possible option would be force wait before navigation...
});
this.response_body_promises.push(promise);
let resolve
let reject
const promise = new Promise((res,rej)=>{
resolve=res
reject =rej
//Some requests may never "finish".
// TODO This timeout wait should be an option.
// TODO When a response "times out", we should call Network.getResponseBody and at least get the truncated data
setTimeout(res,10000)
})
this.response_body_promises[requestId] = {response,promise,resolve,reject};
}
}
});
Expand All @@ -104,7 +117,7 @@ class PuppeteerHar {
*/
async stop() {
this.inProgress = false;
await Promise.all(this.response_body_promises);
await Promise.all(Object.values(this.response_body_promises).map(({promise})=>promise));
await this.client.detach();
const har = harFromMessages(
this.page_events.concat(this.network_events),
Expand Down