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

Feat capture service worker resources #1434

Closed
wants to merge 4 commits into from
Closed
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
26 changes: 22 additions & 4 deletions packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Network {
#intercepts = new Map();
#authentications = new Set();
#aborted = new Set();
#serviceWorkerRequests = new Map();

constructor(page, options) {
this.page = page;
Expand All @@ -43,13 +44,13 @@ export class Network {

let commands = [
session.send('Network.enable'),
session.send('Network.setBypassServiceWorker', { bypass: true }),
session.send('Network.setBypassServiceWorker', { bypass: false }),
session.send('Network.setCacheDisabled', { cacheDisabled: true }),
session.send('Network.setUserAgentOverride', { userAgent: this.userAgent }),
session.send('Network.setExtraHTTPHeaders', { headers: this.requestHeaders })
];

if (this.intercept && session.isDocument) {
if (this.intercept) {
session.on('Fetch.requestPaused', this._handleRequestPaused.bind(this, session));
session.on('Fetch.authRequired', this._handleAuthRequired.bind(this, session));

Expand Down Expand Up @@ -155,6 +156,7 @@ export class Network {
let { networkId: requestId, requestId: interceptId, resourceType } = event;
let pending = this.#pending.get(requestId);
this.#pending.delete(requestId);
this.#serviceWorkerRequests.delete(requestId);

// guard against redirects with the same requestId
if (pending?.request.url === event.request.url &&
Expand All @@ -169,7 +171,7 @@ export class Network {
// Called when a request will be sent. If the request has already been intercepted, handle it;
// otherwise set it to be pending until it is paused.
_handleRequestWillBeSent = async event => {
let { requestId, request } = event;
let { requestId, request, type } = event;

// do not handle data urls
if (request.url.startsWith('data:')) return;
Expand All @@ -183,6 +185,8 @@ export class Network {
let { session, requestId: interceptId, resourceType } = intercept;
await this._handleRequest(session, { ...event, resourceType, interceptId });
this.#intercepts.delete(requestId);
} else {
this.#serviceWorkerRequests.set(requestId, request)
}
}
}
Expand All @@ -191,7 +195,7 @@ export class Network {
// responses and calls this.onrequest with request info and callbacks to continue, respond,
// or abort a request. One of the callbacks is required to be called and only one.
_handleRequest = async (session, event) => {
let { request, requestId, interceptId, resourceType } = event;
let { request, requestId, interceptId, resourceType, loaderId } = event;
let redirectChain = [];

// if handling a redirected request, associate the response and add to its redirect chain
Expand All @@ -206,6 +210,7 @@ export class Network {
request.requestId = requestId;
request.interceptId = interceptId;
request.redirectChain = redirectChain;
request.loaderId = loaderId;
this.#requests.set(requestId, request);

await sendResponseResource(this, request, session);
Expand All @@ -219,11 +224,24 @@ export class Network {
/* istanbul ignore if: race condition paranioa */
if (!request) return;

if(request.loaderId == ""){
// get original request's requestId and use that instead
for (const [rqId, rq] of Object.entries(this.#serviceWorkerRequests)) {
if(request.url === rq.url && request.method === rq.method){
requestId = rqId;
request.type = rq.type;
this.#serviceWorkerRequests.delete(rqId);
break;
}
}
}

request.response = response;
request.response.buffer = async () => {
let result = await this.send(session, 'Network.getResponseBody', { requestId });
return Buffer.from(result.body, result.base64Encoded ? 'base64' : 'utf-8');
};

}

// Called when a request streams events. These types of requests break asset discovery because
Expand Down