Skip to content

Commit

Permalink
feat: use captureServiceWorker config to capture original requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshah98 committed Nov 27, 2023
1 parent 0cc0a39 commit 21bbb47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/src/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export function createDiscoveryQueue(percy) {
requestHeaders: snapshot.discovery.requestHeaders,
authorization: snapshot.discovery.authorization,
userAgent: snapshot.discovery.userAgent,
captureServiceWorker: snapshot.discovery.captureServiceWorker,
meta: snapshot.meta,

// enable network inteception
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Network {
this.timeout = options.networkIdleTimeout ?? 100;
this.authorization = options.authorization;
this.requestHeaders = options.requestHeaders ?? {};
this.captureServiceWorker = options.captureServiceWorker ?? false;
this.userAgent = options.userAgent ??
// by default, emulate a non-headless browser
page.session.browser.version.userAgent.replace('Headless', '');
Expand All @@ -54,7 +55,7 @@ export class Network {

let commands = [
session.send('Network.enable'),
session.send('Network.setBypassServiceWorker', { bypass: true }),
session.send('Network.setBypassServiceWorker', { bypass: !this.captureServiceWorker}),

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

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
session.send('Network.setCacheDisabled', { cacheDisabled: true }),
session.send('Network.setUserAgentOverride', { userAgent: this.userAgent }),
session.send('Network.setExtraHTTPHeaders', { headers: this.requestHeaders })
Expand Down Expand Up @@ -178,13 +179,16 @@ 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;

if (this.intercept) {
this.#pending.set(requestId, event);
if (this.captureServiceWorker) {
await this._handleRequest(undefined, {...event, resourceType: type, interceptId: requestId}, true);

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

View workflow job for this annotation

GitHub Actions / Lint

A space is required after '{'

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

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
}
}
// release request
// note: we are releasing this, even if intercept is not set for network.js
Expand All @@ -195,7 +199,7 @@ export class Network {
// 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) => {
_handleRequest = async (session, event, serviceWorker = false) => {
let { request, requestId, interceptId, resourceType } = event;
let redirectChain = [];

Expand All @@ -213,7 +217,9 @@ export class Network {
request.redirectChain = redirectChain;
this.#requests.set(requestId, request);

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

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

View workflow job for this annotation

GitHub Actions / Lint

Expected space(s) after "if"

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

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
await sendResponseResource(this, request, session);
}
}

// Called when a response has been received for a specific request. Associates the response with
Expand Down

0 comments on commit 21bbb47

Please sign in to comment.