|
| 1 | +import Debug from 'debug' |
| 2 | +import type { BrowserPreRequest } from '../../types' |
| 3 | +import type Protocol from 'devtools-protocol' |
| 4 | + |
| 5 | +const debug = Debug('cypress:proxy:service-worker-manager') |
| 6 | + |
| 7 | +type ServiceWorkerRegistration = { |
| 8 | + registrationId: string |
| 9 | + scopeURL: string |
| 10 | + activatedServiceWorker?: ServiceWorker |
| 11 | +} |
| 12 | + |
| 13 | +type ServiceWorker = { |
| 14 | + registrationId: string |
| 15 | + scriptURL: string |
| 16 | + initiatorURL?: string |
| 17 | + controlledURLs: Set<string> |
| 18 | +} |
| 19 | + |
| 20 | +type RegisterServiceWorkerOptions = { |
| 21 | + registrationId: string |
| 22 | + scopeURL: string |
| 23 | +} |
| 24 | + |
| 25 | +type UnregisterServiceWorkerOptions = { |
| 26 | + registrationId: string |
| 27 | +} |
| 28 | + |
| 29 | +type AddActivatedServiceWorkerOptions = { |
| 30 | + registrationId: string |
| 31 | + scriptURL: string |
| 32 | +} |
| 33 | + |
| 34 | +type AddInitiatorToServiceWorkerOptions = { |
| 35 | + scriptURL: string |
| 36 | + initiatorURL: string |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Manages service worker registrations and their controlled URLs. |
| 41 | + * |
| 42 | + * The basic lifecycle is as follows: |
| 43 | + * |
| 44 | + * 1. A service worker is registered via `registerServiceWorker`. |
| 45 | + * 2. The service worker is activated via `addActivatedServiceWorker`. |
| 46 | + * |
| 47 | + * At some point while 1 and 2 are happening: |
| 48 | + * |
| 49 | + * 3. We receive a message from the browser that a service worker has been initiated with the `addInitiatorToServiceWorker` method. |
| 50 | + * |
| 51 | + * At this point, when the manager tries to process a browser pre-request, it will check if the request is controlled by a service worker. |
| 52 | + * It determines it is controlled by a service worker if: |
| 53 | + * |
| 54 | + * 1. The document URL for the browser pre-request matches the initiator URL for the service worker. |
| 55 | + * 2. The request URL is within the scope of the service worker or the request URL's initiator is controlled by the service worker. |
| 56 | + */ |
| 57 | +export class ServiceWorkerManager { |
| 58 | + private serviceWorkerRegistrations: Map<string, ServiceWorkerRegistration> = new Map<string, ServiceWorkerRegistration>() |
| 59 | + private pendingInitiators: Map<string, string> = new Map<string, string>() |
| 60 | + |
| 61 | + /** |
| 62 | + * Goes through the list of service worker registrations and adds or removes them from the manager. |
| 63 | + */ |
| 64 | + updateServiceWorkerRegistrations (data: Protocol.ServiceWorker.WorkerRegistrationUpdatedEvent) { |
| 65 | + data.registrations.forEach((registration) => { |
| 66 | + if (registration.isDeleted) { |
| 67 | + this.unregisterServiceWorker({ registrationId: registration.registrationId }) |
| 68 | + } else { |
| 69 | + this.registerServiceWorker({ registrationId: registration.registrationId, scopeURL: registration.scopeURL }) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Goes through the list of service worker versions and adds any that are activated to the manager. |
| 76 | + */ |
| 77 | + updateServiceWorkerVersions (data: Protocol.ServiceWorker.WorkerVersionUpdatedEvent) { |
| 78 | + data.versions.forEach((version) => { |
| 79 | + if (version.status === 'activated') { |
| 80 | + this.addActivatedServiceWorker({ registrationId: version.registrationId, scriptURL: version.scriptURL }) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Adds an initiator URL to a service worker. If the service worker has not yet been activated, the initiator URL is added to a pending list and will |
| 87 | + * be added to the service worker when it is activated. |
| 88 | + */ |
| 89 | + addInitiatorToServiceWorker ({ scriptURL, initiatorURL }: AddInitiatorToServiceWorkerOptions) { |
| 90 | + let initiatorAdded = false |
| 91 | + |
| 92 | + for (const registration of this.serviceWorkerRegistrations.values()) { |
| 93 | + if (registration.activatedServiceWorker?.scriptURL === scriptURL) { |
| 94 | + registration.activatedServiceWorker.initiatorURL = initiatorURL |
| 95 | + |
| 96 | + initiatorAdded = true |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (!initiatorAdded) { |
| 102 | + this.pendingInitiators.set(scriptURL, initiatorURL) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Processes a browser pre-request to determine if it is controlled by a service worker. If it is, the service worker's controlled URLs are updated with the given request URL. |
| 108 | + * |
| 109 | + * @param browserPreRequest The browser pre-request to process. |
| 110 | + * @returns `true` if the request is controlled by a service worker, `false` otherwise. |
| 111 | + */ |
| 112 | + processBrowserPreRequest (browserPreRequest: BrowserPreRequest) { |
| 113 | + if (browserPreRequest.initiator?.type === 'preload') { |
| 114 | + return false |
| 115 | + } |
| 116 | + |
| 117 | + let requestControlledByServiceWorker = false |
| 118 | + |
| 119 | + this.serviceWorkerRegistrations.forEach((registration) => { |
| 120 | + const activatedServiceWorker = registration.activatedServiceWorker |
| 121 | + const paramlessDocumentURL = browserPreRequest.documentURL.split('?')[0] |
| 122 | + |
| 123 | + if (!activatedServiceWorker || activatedServiceWorker.initiatorURL !== paramlessDocumentURL) { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + const paramlessURL = browserPreRequest.url.split('?')[0] |
| 128 | + const paramlessInitiatorURL = browserPreRequest.initiator?.url?.split('?')[0] |
| 129 | + const paramlessCallStackURL = browserPreRequest.initiator?.stack?.callFrames[0]?.url?.split('?')[0] |
| 130 | + const urlIsControlled = paramlessURL.startsWith(registration.scopeURL) |
| 131 | + const initiatorUrlIsControlled = paramlessInitiatorURL && activatedServiceWorker.controlledURLs?.has(paramlessInitiatorURL) |
| 132 | + const topStackUrlIsControlled = paramlessCallStackURL && activatedServiceWorker.controlledURLs?.has(paramlessCallStackURL) |
| 133 | + |
| 134 | + if (urlIsControlled || initiatorUrlIsControlled || topStackUrlIsControlled) { |
| 135 | + activatedServiceWorker.controlledURLs.add(paramlessURL) |
| 136 | + requestControlledByServiceWorker = true |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + return requestControlledByServiceWorker |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Registers the given service worker with the given scope. Will not overwrite an existing registration. |
| 145 | + */ |
| 146 | + private registerServiceWorker ({ registrationId, scopeURL }: RegisterServiceWorkerOptions) { |
| 147 | + // Only register service workers if they haven't already been registered |
| 148 | + if (this.serviceWorkerRegistrations.get(registrationId)?.scopeURL === scopeURL) { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + this.serviceWorkerRegistrations.set(registrationId, { |
| 153 | + registrationId, |
| 154 | + scopeURL, |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Unregisters the service worker with the given registration ID. |
| 160 | + */ |
| 161 | + private unregisterServiceWorker ({ registrationId }: UnregisterServiceWorkerOptions) { |
| 162 | + this.serviceWorkerRegistrations.delete(registrationId) |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Adds an activated service worker to the manager. |
| 167 | + */ |
| 168 | + private addActivatedServiceWorker ({ registrationId, scriptURL }: AddActivatedServiceWorkerOptions) { |
| 169 | + const registration = this.serviceWorkerRegistrations.get(registrationId) |
| 170 | + |
| 171 | + if (registration) { |
| 172 | + const initiatorURL = this.pendingInitiators.get(scriptURL) |
| 173 | + |
| 174 | + registration.activatedServiceWorker = { |
| 175 | + registrationId, |
| 176 | + scriptURL, |
| 177 | + controlledURLs: new Set<string>(), |
| 178 | + initiatorURL: initiatorURL || registration.activatedServiceWorker?.initiatorURL, |
| 179 | + } |
| 180 | + |
| 181 | + this.pendingInitiators.delete(scriptURL) |
| 182 | + } else { |
| 183 | + debug('Could not find service worker registration for registration ID %s', registrationId) |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments