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