Skip to content

Commit

Permalink
wip: refactor service-worker capture flow, to handle out-of-order events
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshah98 committed Nov 22, 2023
1 parent fa0f5f3 commit dbd4639
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 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 Down Expand Up @@ -49,7 +50,7 @@ export class Network {
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 Down Expand Up @@ -184,17 +186,16 @@ export class Network {
await this._handleRequest(session, { ...event, resourceType, interceptId });
this.#intercepts.delete(requestId);
} else {
let session;
await this._handleRequest(session, { ...event, resourceType: type, interceptId: requestId }, true);
this.#serviceWorkerRequests.set(requestId, request)

Check failure on line 189 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
}
}

// Called when a pending request is paused. Handles associating redirected requests with
// 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, serviceWorker = false) => {
let { request, requestId, interceptId, resourceType } = event;
_handleRequest = async (session, 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 @@ -209,11 +210,10 @@ export class Network {
request.requestId = requestId;
request.interceptId = interceptId;
request.redirectChain = redirectChain;
request.loaderId = loaderId;
this.#requests.set(requestId, request);

if (!serviceWorker) {
await sendResponseResource(this, request, session);
}
await sendResponseResource(this, request, session);
}

// Called when a response has been received for a specific request. Associates the response with
Expand All @@ -224,11 +224,24 @@ export class Network {
/* istanbul ignore if: race condition paranioa */
if (!request) return;

if(request.loaderId == ""){

Check failure on line 227 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected space(s) after "if"

Check failure on line 227 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected '===' and instead saw '=='

Check failure on line 227 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 227 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
// 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){

Check failure on line 230 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected space(s) after "if"

Check failure on line 230 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
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');
};

Check failure on line 243 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Block must not be padded by blank lines

}

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

0 comments on commit dbd4639

Please sign in to comment.