Skip to content

Commit

Permalink
adding timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile committed Apr 23, 2024
1 parent 7c15d36 commit 48d2525
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/proxy/lib/http/util/service-worker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ export class ServiceWorkerManager {

if (activatedServiceWorker) {
if (requestPotentiallyControlledByServiceWorker) {
isControlled = await this.isURLControlledByServiceWorker(browserPreRequest)
try {
isControlled = await this.isURLControlledByServiceWorker(browserPreRequest)
} catch (e) {
debug('timed out checking if pre-request is controlled by service worker: %o', { url: browserPreRequest.url, requestId: browserPreRequest.requestId })
}

if (isControlled) {
debug('Request is controlled by service worker: %o', { url: browserPreRequest.url, requestId: browserPreRequest.requestId })
Expand Down Expand Up @@ -377,7 +381,14 @@ export class ServiceWorkerManager {
promises.push(deferred)
debug('adding pending controlled request promise: %o', { url, requestId: browserPreRequest.requestId })

return deferred.promise
let timer
// race the deferred promise with a timeout to prevent the pre-request from hanging indefinitely
const racingPromises = Promise.race([
deferred.promise,
new Promise<boolean>((_resolve, reject) => timer = setTimeout(reject, 250)),
]).finally(() => clearTimeout(timer))

return racingPromises
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ describe('lib/http/util/service-worker-manager', () => {
hasRedirectResponse: true,
}))).to.be.false
})

it('supports a pre-request that times out when it does not receive a fetch event', async () => {
expect(await manager.processBrowserPreRequest(createBrowserPreRequest({
url: 'http://localhost:8080/foo.js',
}))).to.be.false
})
})

context('without any controlled urls', () => {
Expand Down

0 comments on commit 48d2525

Please sign in to comment.