@libo11y/webext 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +225 -2
- package/dist/chrome.d.ts +57 -0
- package/dist/chrome.js +16 -0
- package/dist/consent-sync.d.ts +37 -0
- package/dist/consent-sync.js +97 -0
- package/dist/content.d.ts +55 -0
- package/dist/content.js +170 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +11 -0
- package/dist/messaging-transport.d.ts +13 -0
- package/dist/messaging-transport.js +47 -0
- package/dist/messaging.d.ts +20 -0
- package/dist/messaging.js +91 -0
- package/dist/options.d.ts +66 -0
- package/dist/options.js +1 -0
- package/dist/protocol.d.ts +54 -0
- package/dist/protocol.js +44 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/dist/worker.d.ts +62 -0
- package/dist/worker.js +353 -0
- package/package.json +39 -4
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WP5 (webext). setupWorker for a Manifest V3 service worker.
|
|
3
|
+
*/
|
|
4
|
+
import { type Platform } from "@libo11y/core";
|
|
5
|
+
import type { BrowserExportOptions, HttpInstrumentationOptions } from "@libo11y/web";
|
|
6
|
+
import { type ConsentController, type ConsentControllerOptions, type Delivery, type DeliveryOptions, type HttpTransportOptions, type PersistentQueue, type QueueOptions, type SignalEndpoints, type Transport, type WebPlatformOptions } from "@libo11y/web/internal";
|
|
7
|
+
import { type ChromeLike } from "./chrome.ts";
|
|
8
|
+
import type { WorkerSetupOptions, WorkerTelemetry } from "./options.ts";
|
|
9
|
+
/** OTEL_* defaults for the worker, under the caller's env: short delays, because the worker can stop at any time. */
|
|
10
|
+
export declare const WORKER_ENV_DEFAULTS: Readonly<Record<string, string>>;
|
|
11
|
+
/**
|
|
12
|
+
* What setupWorkerWith builds on. Internal: the default is the real modules and getChrome(); tests
|
|
13
|
+
* replace any of them.
|
|
14
|
+
*/
|
|
15
|
+
export interface WorkerDeps {
|
|
16
|
+
readonly chrome: ChromeLike | undefined;
|
|
17
|
+
readonly createConsentController: (opts: ConsentControllerOptions) => ConsentController;
|
|
18
|
+
readonly openQueue: (opts: QueueOptions) => Promise<PersistentQueue>;
|
|
19
|
+
readonly resolveEndpoints: (opts: BrowserExportOptions, hermetic: boolean) => SignalEndpoints;
|
|
20
|
+
readonly createHttpTransport: (opts: HttpTransportOptions) => Transport;
|
|
21
|
+
readonly createDelivery: (opts: DeliveryOptions) => Delivery;
|
|
22
|
+
readonly webPlatform: (opts: WebPlatformOptions) => Platform;
|
|
23
|
+
readonly installFetchInstrumentation: (opts: HttpInstrumentationOptions) => () => void;
|
|
24
|
+
}
|
|
25
|
+
/** The real dependencies. */
|
|
26
|
+
export declare function defaultWorkerDeps(): WorkerDeps;
|
|
27
|
+
/**
|
|
28
|
+
* Builds and installs the worker's telemetry. Call it at the top level of the worker script on
|
|
29
|
+
* every wake. In order: the consent controller with the stored state beating options.consent
|
|
30
|
+
* (readStoredConsent) and syncConsent; openQueue (IndexedDB at the extension origin), releasing
|
|
31
|
+
* every lease an earlier wake left (it is the queue's only user, so none of those sends is still in
|
|
32
|
+
* flight); the HTTP
|
|
33
|
+
* transport with sendBeacon undefined; a delivery with persistFirst true, mode always "normal" and a
|
|
34
|
+
* lease of three times transport.timeoutMillis when that is longer than the default;
|
|
35
|
+
* webPlatform with name "webext" and WORKER_ENV_DEFAULTS; setupWith; serviceVersion defaulting to
|
|
36
|
+
* the manifest version. Then: the runtime.onMessage listener for ExportMessages from this
|
|
37
|
+
* extension's own contexts (sender.id === runtime.id), which puts each accepted batch in the queue,
|
|
38
|
+
* replies, and drains; the worker's fetch instrumentation; and a drain of whatever the last wake
|
|
39
|
+
* left. Rejects like setup on a bad configuration.
|
|
40
|
+
*
|
|
41
|
+
* The export listener is registered before the first await, so the message that woke the worker
|
|
42
|
+
* reaches it; batches that arrive before the telemetry is ready are answered once it is. Traced
|
|
43
|
+
* messages that arrive before the application can add its listener are held, and replayed one task
|
|
44
|
+
* after this resolves to the listeners added through the handle's onMessage by then, so add them
|
|
45
|
+
* in the first callback on the returned promise. A service worker cannot use a top-level await, so
|
|
46
|
+
* the pattern is:
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* setupWorker(options).then((t) => t.onMessage(handle));
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* Each batch re-reads the stored consent before it is accepted, so a grant
|
|
53
|
+
* or revoke in another context counts before its storage.onChanged arrives. When no state is
|
|
54
|
+
* stored yet, a granted or denied initial state is stored for the content scripts to read. The
|
|
55
|
+
* worker's consent options decide the privacy signals for the whole extension: a stored grant
|
|
56
|
+
* that a respected DNT or GPC signal refuses, from a content script or an earlier wake, is
|
|
57
|
+
* replaced in storage by "denied", so every context denies (syncConsent). The
|
|
58
|
+
* console sink defaults to off, as on the web.
|
|
59
|
+
*/
|
|
60
|
+
export declare function setupWorker(options?: WorkerSetupOptions): Promise<WorkerTelemetry>;
|
|
61
|
+
/** setupWorker over deps. Internal: the seam the unit tests use. */
|
|
62
|
+
export declare function setupWorkerWith(options: WorkerSetupOptions, deps: WorkerDeps): Promise<WorkerTelemetry>;
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WP5 (webext). setupWorker for a Manifest V3 service worker.
|
|
3
|
+
*/
|
|
4
|
+
import { setupWith } from "@libo11y/core";
|
|
5
|
+
import { activeConsent, createConsentController, createDelivery, createHttpTransport, installFetchInstrumentation, OTLP_SIGNALS, openQueue, resolveEndpoints, setActiveConsent, webPlatform, } from "@libo11y/web/internal";
|
|
6
|
+
import { diag } from "@opentelemetry/api";
|
|
7
|
+
import { getChrome } from "./chrome.js";
|
|
8
|
+
import { CONSENT_STORAGE_KEY, readStoredConsent, syncConsent } from "./consent-sync.js";
|
|
9
|
+
import { createTracedMessaging, sendTracedTabMessage, tracedListener } from "./messaging.js";
|
|
10
|
+
import { isExportMessage, isTracedMessage, PROTOCOL_VERSION } from "./protocol.js";
|
|
11
|
+
/** OTEL_* defaults for the worker, under the caller's env: short delays, because the worker can stop at any time. */
|
|
12
|
+
export const WORKER_ENV_DEFAULTS = {
|
|
13
|
+
OTEL_BSP_SCHEDULE_DELAY: "1000",
|
|
14
|
+
OTEL_BLRP_SCHEDULE_DELAY: "1000",
|
|
15
|
+
OTEL_METRIC_EXPORT_INTERVAL: "15000",
|
|
16
|
+
};
|
|
17
|
+
/** The real dependencies. */
|
|
18
|
+
export function defaultWorkerDeps() {
|
|
19
|
+
return {
|
|
20
|
+
chrome: getChrome(),
|
|
21
|
+
createConsentController,
|
|
22
|
+
openQueue,
|
|
23
|
+
resolveEndpoints: (opts, hermetic) => resolveEndpoints(opts, hermetic),
|
|
24
|
+
createHttpTransport,
|
|
25
|
+
createDelivery,
|
|
26
|
+
webPlatform,
|
|
27
|
+
installFetchInstrumentation,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Builds and installs the worker's telemetry. Call it at the top level of the worker script on
|
|
32
|
+
* every wake. In order: the consent controller with the stored state beating options.consent
|
|
33
|
+
* (readStoredConsent) and syncConsent; openQueue (IndexedDB at the extension origin), releasing
|
|
34
|
+
* every lease an earlier wake left (it is the queue's only user, so none of those sends is still in
|
|
35
|
+
* flight); the HTTP
|
|
36
|
+
* transport with sendBeacon undefined; a delivery with persistFirst true, mode always "normal" and a
|
|
37
|
+
* lease of three times transport.timeoutMillis when that is longer than the default;
|
|
38
|
+
* webPlatform with name "webext" and WORKER_ENV_DEFAULTS; setupWith; serviceVersion defaulting to
|
|
39
|
+
* the manifest version. Then: the runtime.onMessage listener for ExportMessages from this
|
|
40
|
+
* extension's own contexts (sender.id === runtime.id), which puts each accepted batch in the queue,
|
|
41
|
+
* replies, and drains; the worker's fetch instrumentation; and a drain of whatever the last wake
|
|
42
|
+
* left. Rejects like setup on a bad configuration.
|
|
43
|
+
*
|
|
44
|
+
* The export listener is registered before the first await, so the message that woke the worker
|
|
45
|
+
* reaches it; batches that arrive before the telemetry is ready are answered once it is. Traced
|
|
46
|
+
* messages that arrive before the application can add its listener are held, and replayed one task
|
|
47
|
+
* after this resolves to the listeners added through the handle's onMessage by then, so add them
|
|
48
|
+
* in the first callback on the returned promise. A service worker cannot use a top-level await, so
|
|
49
|
+
* the pattern is:
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* setupWorker(options).then((t) => t.onMessage(handle));
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Each batch re-reads the stored consent before it is accepted, so a grant
|
|
56
|
+
* or revoke in another context counts before its storage.onChanged arrives. When no state is
|
|
57
|
+
* stored yet, a granted or denied initial state is stored for the content scripts to read. The
|
|
58
|
+
* worker's consent options decide the privacy signals for the whole extension: a stored grant
|
|
59
|
+
* that a respected DNT or GPC signal refuses, from a content script or an earlier wake, is
|
|
60
|
+
* replaced in storage by "denied", so every context denies (syncConsent). The
|
|
61
|
+
* console sink defaults to off, as on the web.
|
|
62
|
+
*/
|
|
63
|
+
export function setupWorker(options = {}) {
|
|
64
|
+
return setupWorkerWith(options, defaultWorkerDeps());
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The delivery lease for a transport timeout: three times it, never less than the delivery's
|
|
68
|
+
* 30-second default. As leaseForTimeout in @libo11y/web, whose internal entry point does not
|
|
69
|
+
* export it.
|
|
70
|
+
*/
|
|
71
|
+
function leaseForTimeout(timeoutMillis) {
|
|
72
|
+
return Math.max(30_000, 3 * timeoutMillis);
|
|
73
|
+
}
|
|
74
|
+
function refused(reason) {
|
|
75
|
+
return { libo11y: PROTOCOL_VERSION, type: "export-reply", ok: false, reason };
|
|
76
|
+
}
|
|
77
|
+
const ACCEPTED = { libo11y: PROTOCOL_VERSION, type: "export-reply", ok: true };
|
|
78
|
+
const CONSENT_CLOSED = "consent is not granted";
|
|
79
|
+
// isExportFrame is true for anything that claims to be an ExportMessage, well-formed or not, so a
|
|
80
|
+
// malformed one gets a refusal rather than silence.
|
|
81
|
+
function isExportFrame(m) {
|
|
82
|
+
return (typeof m === "object" &&
|
|
83
|
+
m !== null &&
|
|
84
|
+
m.libo11y === PROTOCOL_VERSION &&
|
|
85
|
+
m.type === "export");
|
|
86
|
+
}
|
|
87
|
+
async function accept(message, target) {
|
|
88
|
+
if (target === undefined)
|
|
89
|
+
return refused("the worker's telemetry is not set up");
|
|
90
|
+
if (!isExportMessage(message))
|
|
91
|
+
return refused("malformed export message");
|
|
92
|
+
await target.refreshConsent();
|
|
93
|
+
if (!target.gate.recording())
|
|
94
|
+
return refused(CONSENT_CLOSED);
|
|
95
|
+
const entry = await target.queue.put({ signal: message.signal, body: message.body });
|
|
96
|
+
if (entry === undefined)
|
|
97
|
+
return refused("the queue refused the batch");
|
|
98
|
+
if (!target.gate.recording()) {
|
|
99
|
+
// Revoked while the batch was being stored: the revoke's clear may have run before the put.
|
|
100
|
+
await target.queue.remove([entry.id]);
|
|
101
|
+
return refused(CONSENT_CLOSED);
|
|
102
|
+
}
|
|
103
|
+
return ACCEPTED;
|
|
104
|
+
}
|
|
105
|
+
// answer accepts message, replies, and drains on acceptance. It never rejects: a failure is a
|
|
106
|
+
// refusal, and a reply that cannot be sent (the sender went away) still leaves the batch drained.
|
|
107
|
+
async function answer(message, target, sendResponse) {
|
|
108
|
+
let reply;
|
|
109
|
+
try {
|
|
110
|
+
reply = await accept(message, target);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
diag.warn("o11y: webext worker failed to accept a batch", err);
|
|
114
|
+
reply = refused(`the worker failed to store the batch: ${err instanceof Error ? err.message : String(err)}`);
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
sendResponse(reply);
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
diag.debug("o11y: webext worker could not answer an export", err);
|
|
121
|
+
}
|
|
122
|
+
if (reply.ok)
|
|
123
|
+
void target?.delivery.drain();
|
|
124
|
+
}
|
|
125
|
+
/** How many early traced messages the worker holds at most; more are left to other contexts. */
|
|
126
|
+
const MAX_HELD_TRACED = 100;
|
|
127
|
+
async function cleanup(steps) {
|
|
128
|
+
for (const step of steps) {
|
|
129
|
+
try {
|
|
130
|
+
await step();
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
diag.warn("o11y: webext worker shutdown step failed", err);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function queueOptions(options) {
|
|
138
|
+
const t = options.transport ?? {};
|
|
139
|
+
return {
|
|
140
|
+
...(t.databaseName === undefined ? {} : { databaseName: t.databaseName }),
|
|
141
|
+
...(t.maxQueueBytes === undefined ? {} : { maxBytes: t.maxQueueBytes }),
|
|
142
|
+
...(t.maxQueueAgeMillis === undefined ? {} : { maxAgeMillis: t.maxQueueAgeMillis }),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** setupWorker over deps. Internal: the seam the unit tests use. */
|
|
146
|
+
export async function setupWorkerWith(options, deps) {
|
|
147
|
+
const chrome = deps.chrome;
|
|
148
|
+
if (chrome === undefined) {
|
|
149
|
+
throw new Error("o11y: setupWorker needs the extension API (chrome.runtime) of a service worker");
|
|
150
|
+
}
|
|
151
|
+
// Registered synchronously, before any await, so the event that woke the worker is delivered.
|
|
152
|
+
let settle = () => { };
|
|
153
|
+
const ready = new Promise((resolve) => {
|
|
154
|
+
settle = resolve;
|
|
155
|
+
});
|
|
156
|
+
const onExport = (message, sender, sendResponse) => {
|
|
157
|
+
if (!isExportFrame(message) || sender.id !== chrome.runtime.id)
|
|
158
|
+
return undefined;
|
|
159
|
+
void ready.then((target) => answer(message, target, sendResponse));
|
|
160
|
+
return true;
|
|
161
|
+
};
|
|
162
|
+
const acceptForwarded = options.acceptForwarded !== false;
|
|
163
|
+
if (acceptForwarded)
|
|
164
|
+
chrome.runtime.onMessage.addListener(onExport);
|
|
165
|
+
const removeExportListener = () => {
|
|
166
|
+
if (acceptForwarded)
|
|
167
|
+
chrome.runtime.onMessage.removeListener(onExport);
|
|
168
|
+
};
|
|
169
|
+
// The application can add its traced listeners only once this resolves, after the browser has
|
|
170
|
+
// dispatched the message that woke the worker. So traced messages that no traced listener can
|
|
171
|
+
// take yet are held, and replayed one task after setup resolves to the listeners added by then.
|
|
172
|
+
// Held messages nobody takes are left unanswered, for another context's listener to answer.
|
|
173
|
+
const tracedListeners = [];
|
|
174
|
+
const held = [];
|
|
175
|
+
let holding = true;
|
|
176
|
+
const onEarlyTraced = (message, sender, sendResponse) => {
|
|
177
|
+
if (!holding || tracedListeners.length > 0 || held.length >= MAX_HELD_TRACED)
|
|
178
|
+
return undefined;
|
|
179
|
+
if (!isTracedMessage(message) || sender.id !== chrome.runtime.id)
|
|
180
|
+
return undefined;
|
|
181
|
+
held.push({ message, sender, sendResponse });
|
|
182
|
+
return true;
|
|
183
|
+
};
|
|
184
|
+
chrome.runtime.onMessage.addListener(onEarlyTraced);
|
|
185
|
+
const releaseHeld = () => {
|
|
186
|
+
if (!holding)
|
|
187
|
+
return;
|
|
188
|
+
holding = false;
|
|
189
|
+
chrome.runtime.onMessage.removeListener(onEarlyTraced);
|
|
190
|
+
for (const h of held.splice(0)) {
|
|
191
|
+
let answered = false;
|
|
192
|
+
const once = (response) => {
|
|
193
|
+
if (answered)
|
|
194
|
+
return;
|
|
195
|
+
answered = true;
|
|
196
|
+
try {
|
|
197
|
+
h.sendResponse(response);
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
diag.debug("o11y: webext worker could not answer a held traced message", err);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
for (const listener of [...tracedListeners]) {
|
|
204
|
+
try {
|
|
205
|
+
listener(h.message, h.sender, once);
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
diag.warn("o11y: webext traced listener failed on a held message", err);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
const steps = [];
|
|
214
|
+
let built;
|
|
215
|
+
let core;
|
|
216
|
+
let sync;
|
|
217
|
+
try {
|
|
218
|
+
const stored = await readStoredConsent(chrome);
|
|
219
|
+
const initial = stored ?? options.consent?.initial;
|
|
220
|
+
const controller = deps.createConsentController({
|
|
221
|
+
...options.consent,
|
|
222
|
+
...(initial === undefined ? {} : { initial }),
|
|
223
|
+
});
|
|
224
|
+
const initialState = controller.consent.state();
|
|
225
|
+
if (stored === undefined && initialState !== "pending" && chrome.storage !== undefined) {
|
|
226
|
+
chrome.storage.local.set({ [CONSENT_STORAGE_KEY]: initialState }).catch((err) => {
|
|
227
|
+
diag.warn("o11y: storing the initial consent state", err);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
sync = syncConsent(controller, chrome);
|
|
231
|
+
steps.push(sync.stop);
|
|
232
|
+
const queue = await deps.openQueue(queueOptions(options));
|
|
233
|
+
steps.push(() => queue.close());
|
|
234
|
+
// The worker is the only context that uses its queue, so a lease held now is one the browser
|
|
235
|
+
// cut off when it stopped an earlier wake mid-send. Released before the delivery exists, so no
|
|
236
|
+
// send of this wake is in flight yet, and the first drain sends those entries at once.
|
|
237
|
+
await queue.releaseLeases();
|
|
238
|
+
const endpoints = deps.resolveEndpoints(options, options.hermetic === true);
|
|
239
|
+
const t = options.transport ?? {};
|
|
240
|
+
const transport = deps.createHttpTransport({
|
|
241
|
+
endpoints,
|
|
242
|
+
sendBeacon: undefined,
|
|
243
|
+
...(options.headers === undefined ? {} : { headers: options.headers }),
|
|
244
|
+
...(t.compression === undefined ? {} : { compression: t.compression }),
|
|
245
|
+
...(t.timeoutMillis === undefined ? {} : { timeoutMillis: t.timeoutMillis }),
|
|
246
|
+
});
|
|
247
|
+
const delivery = deps.createDelivery({
|
|
248
|
+
transport,
|
|
249
|
+
queue,
|
|
250
|
+
gate: controller,
|
|
251
|
+
persistFirst: true,
|
|
252
|
+
mode: () => "normal",
|
|
253
|
+
...(t.initialBackoffMillis === undefined ? {} : { initialBackoffMillis: t.initialBackoffMillis }),
|
|
254
|
+
...(t.maxBackoffMillis === undefined ? {} : { maxBackoffMillis: t.maxBackoffMillis }),
|
|
255
|
+
// A lease shorter than a send could let the next drain claim the entry mid-send and resend it.
|
|
256
|
+
...(t.timeoutMillis === undefined ? {} : { leaseMillis: leaseForTimeout(t.timeoutMillis) }),
|
|
257
|
+
});
|
|
258
|
+
steps.unshift(() => delivery.shutdown());
|
|
259
|
+
const platform = deps.webPlatform({
|
|
260
|
+
name: "webext",
|
|
261
|
+
exportOptions: options,
|
|
262
|
+
endpoints,
|
|
263
|
+
delivery,
|
|
264
|
+
gate: controller,
|
|
265
|
+
mode: () => "normal",
|
|
266
|
+
envDefaults: WORKER_ENV_DEFAULTS,
|
|
267
|
+
...(options.contextManager === undefined ? {} : { contextManager: options.contextManager }),
|
|
268
|
+
});
|
|
269
|
+
const coreOptions = {
|
|
270
|
+
...options,
|
|
271
|
+
console: options.console ?? false,
|
|
272
|
+
serviceVersion: options.serviceVersion ?? chrome.runtime.getManifest().version,
|
|
273
|
+
};
|
|
274
|
+
core = await setupWith(coreOptions, platform);
|
|
275
|
+
built = { controller, queue, delivery, endpoints };
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
removeExportListener();
|
|
279
|
+
releaseHeld();
|
|
280
|
+
settle(undefined);
|
|
281
|
+
await cleanup(steps);
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
const { controller, queue, delivery, endpoints } = built;
|
|
285
|
+
const telemetry = core;
|
|
286
|
+
const unregisterRevoke = controller.onRevoke(async () => {
|
|
287
|
+
await telemetry.forceFlush();
|
|
288
|
+
await delivery.clear();
|
|
289
|
+
});
|
|
290
|
+
const messaging = createTracedMessaging(chrome, telemetry.propagator);
|
|
291
|
+
const uninstalls = [];
|
|
292
|
+
// Core's handle extended in place, so identity-keyed lookups such as exemplarStoreOf still work.
|
|
293
|
+
const handle = telemetry;
|
|
294
|
+
Object.defineProperty(handle, "consent", { value: controller.consent, enumerable: true });
|
|
295
|
+
Object.assign(handle, {
|
|
296
|
+
onMessage: (listener) => {
|
|
297
|
+
const onRuntimeMessage = tracedListener(telemetry.propagator, listener);
|
|
298
|
+
tracedListeners.push(onRuntimeMessage);
|
|
299
|
+
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
|
300
|
+
return () => {
|
|
301
|
+
const i = tracedListeners.indexOf(onRuntimeMessage);
|
|
302
|
+
if (i >= 0)
|
|
303
|
+
tracedListeners.splice(i, 1);
|
|
304
|
+
chrome.runtime.onMessage.removeListener(onRuntimeMessage);
|
|
305
|
+
};
|
|
306
|
+
},
|
|
307
|
+
sendMessage: messaging.sendMessage,
|
|
308
|
+
sendTabMessage: (tabId, payload) => sendTracedTabMessage(chrome, telemetry.propagator, tabId, payload),
|
|
309
|
+
});
|
|
310
|
+
const coreShutdown = telemetry.shutdown.bind(telemetry);
|
|
311
|
+
let shutdownPromise;
|
|
312
|
+
handle.shutdown = (opts) => {
|
|
313
|
+
shutdownPromise ??= (async () => {
|
|
314
|
+
removeExportListener();
|
|
315
|
+
releaseHeld();
|
|
316
|
+
const report = await coreShutdown(opts);
|
|
317
|
+
await cleanup([
|
|
318
|
+
...uninstalls,
|
|
319
|
+
unregisterRevoke,
|
|
320
|
+
() => {
|
|
321
|
+
if (activeConsent() === controller.consent)
|
|
322
|
+
setActiveConsent(undefined);
|
|
323
|
+
},
|
|
324
|
+
...steps,
|
|
325
|
+
]);
|
|
326
|
+
return report;
|
|
327
|
+
})();
|
|
328
|
+
return shutdownPromise;
|
|
329
|
+
};
|
|
330
|
+
try {
|
|
331
|
+
setActiveConsent(controller.consent);
|
|
332
|
+
if (options.fetch !== false) {
|
|
333
|
+
const f = typeof options.fetch === "object" ? options.fetch : {};
|
|
334
|
+
uninstalls.push(deps.installFetchInstrumentation({
|
|
335
|
+
telemetry: handle,
|
|
336
|
+
propagateTo: f.propagateTo ?? [],
|
|
337
|
+
ignoreUrls: [
|
|
338
|
+
...(f.ignoreUrls ?? []),
|
|
339
|
+
...OTLP_SIGNALS.flatMap((s) => (endpoints[s] === undefined ? [] : [endpoints[s]])),
|
|
340
|
+
],
|
|
341
|
+
}));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
catch (err) {
|
|
345
|
+
settle(undefined);
|
|
346
|
+
await handle.shutdown();
|
|
347
|
+
throw err;
|
|
348
|
+
}
|
|
349
|
+
settle({ gate: controller, queue, delivery, refreshConsent: sync?.refresh ?? (async () => { }) });
|
|
350
|
+
setTimeout(releaseHeld, 0);
|
|
351
|
+
void delivery.drain();
|
|
352
|
+
return handle;
|
|
353
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libo11y/webext",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "The libo11y Manifest V3 extension platform for TypeScript: setup for the service worker with a queue persisted to IndexedDB on every batch, setup for content scripts and extension pages that forward their records to the worker by runtime messaging, and trace context that crosses with every message.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/jamesainslie/libo11y.git",
|
|
17
|
+
"directory": "ts/packages/webext"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@libo11y/core": "0.2.1",
|
|
21
|
+
"@libo11y/spec": "0.2.1",
|
|
22
|
+
"@libo11y/web": "0.2.1",
|
|
23
|
+
"@opentelemetry/api": "1.10.0",
|
|
24
|
+
"@opentelemetry/sdk-logs": "3.1.1",
|
|
25
|
+
"@opentelemetry/sdk-metrics": "3.1.1",
|
|
26
|
+
"@opentelemetry/sdk-trace": "3.1.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@libo11y/test": "0.2.1",
|
|
30
|
+
"fake-indexeddb": "6.2.7"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"provenance": true
|
|
40
|
+
}
|
|
41
|
+
}
|