From 21bbb47dfac55a1af91cef3828484c90f605102e Mon Sep 17 00:00:00 2001 From: nilshah98 Date: Mon, 27 Nov 2023 16:01:00 +0530 Subject: [PATCH] feat: use captureServiceWorker config to capture original requests --- packages/core/src/discovery.js | 1 + packages/core/src/network.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/core/src/discovery.js b/packages/core/src/discovery.js index 8856003be..6d88e104a 100644 --- a/packages/core/src/discovery.js +++ b/packages/core/src/discovery.js @@ -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 diff --git a/packages/core/src/network.js b/packages/core/src/network.js index a6f9ad00a..e3397d020 100644 --- a/packages/core/src/network.js +++ b/packages/core/src/network.js @@ -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', ''); @@ -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}), session.send('Network.setCacheDisabled', { cacheDisabled: true }), session.send('Network.setUserAgentOverride', { userAgent: this.userAgent }), session.send('Network.setExtraHTTPHeaders', { headers: this.requestHeaders }) @@ -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); + } } // release request // note: we are releasing this, even if intercept is not set for network.js @@ -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 = []; @@ -213,7 +217,9 @@ export class Network { request.redirectChain = redirectChain; this.#requests.set(requestId, request); - await sendResponseResource(this, request, session); + if(!serviceWorker){ + await sendResponseResource(this, request, session); + } } // Called when a response has been received for a specific request. Associates the response with