|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {UncaughtError} from './PageCollector.js'; |
| 8 | +import type { |
| 9 | + ConsoleMessage, |
| 10 | + WebWorker, |
| 11 | + Target, |
| 12 | + CDPSession, |
| 13 | + Protocol, |
| 14 | + Browser, |
| 15 | +} from './third_party/index.js'; |
| 16 | +import type {ExtensionServiceWorker} from './types.js'; |
| 17 | +import type {WithSymbolId} from './utils/id.js'; |
| 18 | +import {createIdGenerator, stableIdSymbol} from './utils/id.js'; |
| 19 | + |
| 20 | +const CHROME_EXTENSION_PREFIX = 'chrome-extension://'; |
| 21 | + |
| 22 | +export class ServiceWorkerSubscriber { |
| 23 | + #target: Target; |
| 24 | + #callback: (item: ConsoleMessage | UncaughtError) => void; |
| 25 | + #session?: CDPSession; |
| 26 | + #worker?: WebWorker; |
| 27 | + |
| 28 | + constructor( |
| 29 | + target: Target, |
| 30 | + callback: (item: ConsoleMessage | UncaughtError) => void, |
| 31 | + ) { |
| 32 | + this.#target = target; |
| 33 | + this.#callback = callback; |
| 34 | + } |
| 35 | + |
| 36 | + async subscribe() { |
| 37 | + this.#session = await this.#target.createCDPSession(); |
| 38 | + await this.#session.send('Runtime.enable'); |
| 39 | + this.#session.on('Runtime.exceptionThrown', this.#onExceptionThrown); |
| 40 | + |
| 41 | + this.#worker = (await this.#target.worker()) ?? undefined; |
| 42 | + if (this.#worker) { |
| 43 | + this.#worker.on('console', this.#onConsole); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async unsubscribe() { |
| 48 | + if (this.#worker) { |
| 49 | + this.#worker.off('console', this.#onConsole); |
| 50 | + } |
| 51 | + if (this.#session) { |
| 52 | + this.#session.off('Runtime.exceptionThrown', this.#onExceptionThrown); |
| 53 | + await this.#session.send('Runtime.disable'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #onConsole = (message: ConsoleMessage) => { |
| 58 | + this.#callback(message); |
| 59 | + }; |
| 60 | + |
| 61 | + #onExceptionThrown = (event: Protocol.Runtime.ExceptionThrownEvent) => { |
| 62 | + const url = this.#target.url(); |
| 63 | + |
| 64 | + const extensionId = extractExtensionId(url); |
| 65 | + |
| 66 | + if (extensionId) { |
| 67 | + this.#callback(new UncaughtError(event.exceptionDetails, extensionId)); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export class ServiceWorkerConsoleCollector { |
| 73 | + #storage = new Map< |
| 74 | + string, |
| 75 | + Array<WithSymbolId<ConsoleMessage | UncaughtError>> |
| 76 | + >(); |
| 77 | + #maxLogs: number; |
| 78 | + #browser?: Browser; |
| 79 | + #serviceWorkerSubscribers = new Map<Target, ServiceWorkerSubscriber>(); |
| 80 | + #idGenerator = createIdGenerator(); |
| 81 | + |
| 82 | + constructor(browser?: Browser, maxLogs = 1000) { |
| 83 | + this.#browser = browser; |
| 84 | + this.#maxLogs = maxLogs; |
| 85 | + } |
| 86 | + |
| 87 | + async init(workers: ExtensionServiceWorker[]) { |
| 88 | + if (!this.#browser) { |
| 89 | + return; |
| 90 | + } |
| 91 | + this.#browser.on('targetcreated', this.#onTargetCreated); |
| 92 | + this.#browser.on('targetdestroyed', this.#onTargetDestroyed); |
| 93 | + |
| 94 | + for (const worker of workers) { |
| 95 | + void this.#onTargetCreated(worker.target); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + dispose() { |
| 100 | + if (!this.#browser) { |
| 101 | + return; |
| 102 | + } |
| 103 | + this.#browser.off('targetcreated', this.#onTargetCreated); |
| 104 | + this.#browser.off('targetdestroyed', this.#onTargetDestroyed); |
| 105 | + for (const subscriber of this.#serviceWorkerSubscribers.values()) { |
| 106 | + void subscriber.unsubscribe(); |
| 107 | + } |
| 108 | + this.#serviceWorkerSubscribers.clear(); |
| 109 | + } |
| 110 | + |
| 111 | + #onTargetCreated = async (target: Target) => { |
| 112 | + if (this.#serviceWorkerSubscribers.has(target)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + const origin = target.url(); |
| 116 | + if (target.type() === 'service_worker' && isExtensionOrigin(origin)) { |
| 117 | + const extensionId = extractExtensionId(origin); |
| 118 | + |
| 119 | + if (!extensionId) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const subscriber = new ServiceWorkerSubscriber(target, item => { |
| 124 | + this.addLog(extensionId, item); |
| 125 | + }); |
| 126 | + void subscriber.subscribe(); |
| 127 | + this.#serviceWorkerSubscribers.set(target, subscriber); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + #onTargetDestroyed = async (target: Target) => { |
| 132 | + const subscriber = this.#serviceWorkerSubscribers.get(target); |
| 133 | + if (subscriber) { |
| 134 | + void subscriber.unsubscribe(); |
| 135 | + this.#serviceWorkerSubscribers.delete(target); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + addLog(extensionId: string, log: ConsoleMessage | UncaughtError) { |
| 140 | + const logs = this.#storage.get(extensionId) ?? []; |
| 141 | + const withId = log as WithSymbolId<ConsoleMessage | UncaughtError>; |
| 142 | + withId[stableIdSymbol] = this.#idGenerator(); |
| 143 | + logs.push(withId); |
| 144 | + if (logs.length > this.#maxLogs) { |
| 145 | + logs.shift(); |
| 146 | + } |
| 147 | + this.#storage.set(extensionId, logs); |
| 148 | + } |
| 149 | + |
| 150 | + getData( |
| 151 | + extensionId: string, |
| 152 | + ): Array<WithSymbolId<ConsoleMessage | UncaughtError>> { |
| 153 | + return this.#storage.get(extensionId) ?? []; |
| 154 | + } |
| 155 | + |
| 156 | + getById( |
| 157 | + extensionId: string, |
| 158 | + stableId: number, |
| 159 | + ): WithSymbolId<ConsoleMessage | UncaughtError> { |
| 160 | + const logs = this.#storage.get(extensionId); |
| 161 | + if (!logs) { |
| 162 | + throw new Error('No logs found for selected extension'); |
| 163 | + } |
| 164 | + const item = logs.find(item => item[stableIdSymbol] === stableId); |
| 165 | + if (item) { |
| 166 | + return item; |
| 167 | + } |
| 168 | + throw new Error('Log not found for selected extension'); |
| 169 | + } |
| 170 | + |
| 171 | + find( |
| 172 | + extensionId: string, |
| 173 | + filter: (item: WithSymbolId<ConsoleMessage | UncaughtError>) => boolean, |
| 174 | + ): WithSymbolId<ConsoleMessage | UncaughtError> | undefined { |
| 175 | + const logs = this.#storage.get(extensionId); |
| 176 | + if (!logs) { |
| 177 | + return; |
| 178 | + } |
| 179 | + return logs.find(filter); |
| 180 | + } |
| 181 | + |
| 182 | + clearLogs(extensionId: string) { |
| 183 | + this.#storage.delete(extensionId); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +function extractExtensionId(origin: string): string | null { |
| 188 | + if (!origin || !isExtensionOrigin(origin)) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + const pathPart = origin.substring(CHROME_EXTENSION_PREFIX.length); |
| 193 | + const slashIndex = pathPart.indexOf('/'); |
| 194 | + |
| 195 | + // if there's no / it means that pathPart is now the extensionId, otherwise |
| 196 | + // we take everything until the first / |
| 197 | + return slashIndex === -1 ? pathPart : pathPart.substring(0, slashIndex); |
| 198 | +} |
| 199 | + |
| 200 | +function isExtensionOrigin(origin: string) { |
| 201 | + return origin.startsWith(CHROME_EXTENSION_PREFIX); |
| 202 | +} |
0 commit comments