Skip to content

Commit

Permalink
PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanthemanuel committed Dec 18, 2023
1 parent 04617eb commit 74c14f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
17 changes: 16 additions & 1 deletion packages/proxy/lib/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ import type { ResourceTypeAndCredentialManager } from '@packages/server/lib/util
import type { ProtocolManagerShape } from '@packages/types'
import type Protocol from 'devtools-protocol'
import { ServiceWorkerManager } from './util/service-worker-manager'
import { hasServiceWorkerHeader } from './util/headers'

function getRandomColorFn () {
return chalk.hex(`#${Number(
Math.floor(Math.random() * 0xFFFFFF),
).toString(16).padStart(6, 'F').toUpperCase()}`)
}

const hasServiceWorkerHeader = (headers: Record<string, string | string[] | undefined>) => {
return headers?.['service-worker'] === 'script' || headers?.['Service-Worker'] === 'script'
}

export const isVerboseTelemetry = true

const isVerbose = isVerboseTelemetry
Expand Down Expand Up @@ -336,6 +339,18 @@ export class Http {
getAUTUrl: this.getAUTUrl,
setAUTUrl: this.setAUTUrl,
getPreRequest: (cb) => {
// The initial request that loads the service worker does not always get sent to CDP. Thus, we need to explicitly ignore it. We determine
// it's the service worker request via the `service-worker` header
if (hasServiceWorkerHeader(req.headers)) {
ctx.debug('Ignoring service worker script since we are not guaranteed to receive it', req.proxiedUrl)

cb({
noPreRequestExpected: true,
})

return
}

return this.preRequests.get(ctx.req, ctx.debug, cb)
},
addPendingUrlWithoutPreRequest: (url) => {
Expand Down
3 changes: 0 additions & 3 deletions packages/proxy/lib/http/util/headers.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/proxy/lib/http/util/prerequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
} from '@packages/proxy'
import type { ProtocolManagerShape } from '@packages/types'
import Debug from 'debug'
import { hasServiceWorkerHeader } from './headers'

const debug = Debug('cypress:proxy:http:util:prerequests')
const debugVerbose = Debug('cypress-verbose:proxy:http:util:prerequests')
Expand Down Expand Up @@ -232,18 +231,6 @@ export class PreRequests {
}

get (req: CypressIncomingRequest, ctxDebug, callback: GetPreRequestCb) {
// The initial request that loads the service worker does not always get sent to CDP. Thus, we need to explicitly ignore it. We determine
// it's the service worker request via the `service-worker` header
if (hasServiceWorkerHeader(req.headers)) {
ctxDebug('Ignoring service worker script since we are not guaranteed to receive it', req.proxiedUrl)

callback({
noPreRequestExpected: true,
})

return
}

const proxyRequestReceivedTimestamp = performance.now() + performance.timeOrigin

metrics.proxyRequestsReceived++
Expand Down
9 changes: 0 additions & 9 deletions packages/proxy/test/unit/http/util/prerequests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,6 @@ describe('http/util/prerequests', () => {
expectPendingCounts(0, 2)
})

it('immediately handles a request from a service worker loading', () => {
const cbServiceWorker = sinon.stub()

preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: { 'service-worker': 'script' } } as any, () => {}, cbServiceWorker)

expect(cbServiceWorker).to.be.calledOnce
expect(cbServiceWorker).to.be.calledWith()
})

it('removes a pending request', () => {
const cb = sinon.stub()

Expand Down

0 comments on commit 74c14f0

Please sign in to comment.