@webex/internal-plugin-metrics 3.11.0 → 3.12.0-auth-prejoin-fetch.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/README.md +36 -0
- package/dist/automated-user.js +19 -0
- package/dist/automated-user.js.map +1 -0
- package/dist/batcher.js +3 -0
- package/dist/batcher.js.map +1 -1
- package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +639 -65
- package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -1
- package/dist/call-diagnostic/call-diagnostic-metrics.js +168 -76
- package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -1
- package/dist/call-diagnostic/call-diagnostic-metrics.util.js +18 -4
- package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -1
- package/dist/call-diagnostic/config.js +17 -3
- package/dist/call-diagnostic/config.js.map +1 -1
- package/dist/config.js +4 -0
- package/dist/config.js.map +1 -1
- package/dist/generic-metrics.js +8 -6
- package/dist/generic-metrics.js.map +1 -1
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -1
- package/dist/metrics.js +1 -1
- package/dist/metrics.types.js +4 -0
- package/dist/metrics.types.js.map +1 -1
- package/dist/new-metrics.js +108 -26
- package/dist/new-metrics.js.map +1 -1
- package/dist/prelogin-metrics.js +106 -0
- package/dist/prelogin-metrics.js.map +1 -0
- package/dist/privacy-and-security-permission-enricher/constants.js +18 -0
- package/dist/privacy-and-security-permission-enricher/constants.js.map +1 -0
- package/dist/privacy-and-security-permission-enricher/index.js +143 -0
- package/dist/privacy-and-security-permission-enricher/index.js.map +1 -0
- package/dist/privacy-and-security-permission-enricher/types.js +7 -0
- package/dist/privacy-and-security-permission-enricher/types.js.map +1 -0
- package/dist/privacy-and-security-permission-enricher/utils.js +71 -0
- package/dist/privacy-and-security-permission-enricher/utils.js.map +1 -0
- package/dist/types/automated-user.d.ts +2 -0
- package/dist/types/call-diagnostic/call-diagnostic-metrics-latencies.d.ts +208 -5
- package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +79 -15
- package/dist/types/call-diagnostic/call-diagnostic-metrics.util.d.ts +5 -0
- package/dist/types/call-diagnostic/config.d.ts +5 -0
- package/dist/types/config.d.ts +4 -0
- package/dist/types/index.d.ts +6 -3
- package/dist/types/metrics.types.d.ts +9 -4
- package/dist/types/new-metrics.d.ts +29 -1
- package/dist/types/prelogin-metrics.d.ts +47 -0
- package/dist/types/privacy-and-security-permission-enricher/constants.d.ts +6 -0
- package/dist/types/privacy-and-security-permission-enricher/index.d.ts +40 -0
- package/dist/types/privacy-and-security-permission-enricher/types.d.ts +14 -0
- package/dist/types/privacy-and-security-permission-enricher/utils.d.ts +5 -0
- package/dist/types/unhandled-exception-telemetry/index.d.ts +60 -0
- package/dist/types/unhandled-exception-telemetry/utils.d.ts +6 -0
- package/dist/unhandled-exception-telemetry/index.js +330 -0
- package/dist/unhandled-exception-telemetry/index.js.map +1 -0
- package/dist/unhandled-exception-telemetry/utils.js +105 -0
- package/dist/unhandled-exception-telemetry/utils.js.map +1 -0
- package/package.json +12 -11
- package/src/automated-user.ts +16 -0
- package/src/batcher.js +4 -0
- package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +800 -75
- package/src/call-diagnostic/call-diagnostic-metrics.ts +102 -15
- package/src/call-diagnostic/call-diagnostic-metrics.util.ts +22 -2
- package/src/call-diagnostic/config.ts +15 -0
- package/src/config.js +4 -0
- package/src/generic-metrics.ts +6 -6
- package/src/index.ts +9 -0
- package/src/metrics.types.ts +27 -5
- package/src/new-metrics.ts +98 -6
- package/src/prelogin-metrics.ts +94 -0
- package/src/privacy-and-security-permission-enricher/constants.ts +33 -0
- package/src/privacy-and-security-permission-enricher/index.ts +142 -0
- package/src/privacy-and-security-permission-enricher/types.ts +21 -0
- package/src/privacy-and-security-permission-enricher/utils.ts +82 -0
- package/src/unhandled-exception-telemetry/index.ts +403 -0
- package/src/unhandled-exception-telemetry/utils.ts +101 -0
- package/test/unit/spec/automated-user.ts +43 -0
- package/test/unit/spec/batcher.js +43 -0
- package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +47 -11
- package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +1550 -416
- package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +887 -159
- package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +61 -3
- package/test/unit/spec/new-metrics.ts +203 -50
- package/test/unit/spec/prelogin-metrics-batcher.ts +71 -36
- package/test/unit/spec/prelogin-metrics.ts +132 -0
- package/test/unit/spec/privacy-and-security-permission-enricher.ts +318 -0
- package/test/unit/spec/unhandled-exception-telemetry/utils.ts +109 -0
- package/test/unit/spec/unhandled-exception-telemetry.ts +688 -0
package/src/new-metrics.ts
CHANGED
|
@@ -9,6 +9,8 @@ import CallDiagnosticMetrics from './call-diagnostic/call-diagnostic-metrics';
|
|
|
9
9
|
import BehavioralMetrics from './behavioral-metrics';
|
|
10
10
|
import OperationalMetrics from './operational-metrics';
|
|
11
11
|
import BusinessMetrics from './business-metrics';
|
|
12
|
+
import PreLoginMetrics from './prelogin-metrics';
|
|
13
|
+
import PreLoginMetricsBatcher from './prelogin-metrics-batcher';
|
|
12
14
|
import {
|
|
13
15
|
RecursivePartial,
|
|
14
16
|
MetricEventProduct,
|
|
@@ -17,17 +19,20 @@ import {
|
|
|
17
19
|
ClientEvent,
|
|
18
20
|
FeatureEvent,
|
|
19
21
|
EventPayload,
|
|
20
|
-
OperationalEvent,
|
|
21
22
|
MediaQualityEvent,
|
|
22
23
|
InternalEvent,
|
|
23
24
|
SubmitClientEventOptions,
|
|
24
25
|
Table,
|
|
25
26
|
DelayedClientEvent,
|
|
26
27
|
DelayedClientFeatureEvent,
|
|
28
|
+
PrivacyAndSecurityPermission,
|
|
27
29
|
} from './metrics.types';
|
|
28
30
|
import CallDiagnosticLatencies from './call-diagnostic/call-diagnostic-metrics-latencies';
|
|
29
31
|
import {setMetricTimings} from './call-diagnostic/call-diagnostic-metrics.util';
|
|
30
32
|
import {generateCommonErrorMetadata} from './utils';
|
|
33
|
+
import {isAutomatedUser as detectAutomatedUser} from './automated-user';
|
|
34
|
+
import PrivacyAndSecurityPermissionEnricher from './privacy-and-security-permission-enricher';
|
|
35
|
+
import {startUnhandledExceptionTelemetry} from './unhandled-exception-telemetry';
|
|
31
36
|
|
|
32
37
|
/**
|
|
33
38
|
* Metrics plugin to centralize all types of metrics.
|
|
@@ -45,6 +50,7 @@ class Metrics extends WebexPlugin {
|
|
|
45
50
|
behavioralMetrics: BehavioralMetrics;
|
|
46
51
|
operationalMetrics: OperationalMetrics;
|
|
47
52
|
businessMetrics: BusinessMetrics;
|
|
53
|
+
preLoginMetrics: PreLoginMetrics;
|
|
48
54
|
isReady = false;
|
|
49
55
|
|
|
50
56
|
/**
|
|
@@ -64,6 +70,8 @@ class Metrics extends WebexPlugin {
|
|
|
64
70
|
|
|
65
71
|
delayedClientFeatureEventsOverrides: Partial<DelayedClientFeatureEvent['options']> = {};
|
|
66
72
|
|
|
73
|
+
private privacyAndSecurityPermissionEnricher: PrivacyAndSecurityPermissionEnricher;
|
|
74
|
+
|
|
67
75
|
/**
|
|
68
76
|
* Constructor
|
|
69
77
|
* @param args
|
|
@@ -74,8 +82,19 @@ class Metrics extends WebexPlugin {
|
|
|
74
82
|
constructor(...args) {
|
|
75
83
|
super(...args);
|
|
76
84
|
|
|
85
|
+
this.privacyAndSecurityPermissionEnricher = new PrivacyAndSecurityPermissionEnricher(
|
|
86
|
+
(error) => {
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
this.webex.logger.error(
|
|
89
|
+
'NewMetrics: @submitClientEvent. Privacy and security permission enrichment failed.',
|
|
90
|
+
error
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
);
|
|
77
94
|
// @ts-ignore
|
|
78
95
|
this.callDiagnosticLatencies = new CallDiagnosticLatencies({}, {parent: this.webex});
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
this.callDiagnosticMetrics = new CallDiagnosticMetrics({}, {parent: this.webex});
|
|
79
98
|
this.onReady();
|
|
80
99
|
}
|
|
81
100
|
|
|
@@ -85,13 +104,20 @@ class Metrics extends WebexPlugin {
|
|
|
85
104
|
private onReady() {
|
|
86
105
|
// @ts-ignore
|
|
87
106
|
this.webex.once('ready', () => {
|
|
88
|
-
|
|
89
|
-
|
|
107
|
+
this.preLoginMetrics = new PreLoginMetrics(
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
new PreLoginMetricsBatcher({}, {parent: this.webex}),
|
|
110
|
+
{},
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
{parent: this.webex}
|
|
113
|
+
);
|
|
90
114
|
this.isReady = true;
|
|
91
115
|
this.setDelaySubmitClientEvents({
|
|
92
116
|
shouldDelay: this.delaySubmitClientEvents,
|
|
93
117
|
overrides: this.delayedClientEventsOverrides,
|
|
94
118
|
});
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
startUnhandledExceptionTelemetry(this.webex);
|
|
95
121
|
});
|
|
96
122
|
}
|
|
97
123
|
|
|
@@ -172,6 +198,13 @@ class Metrics extends WebexPlugin {
|
|
|
172
198
|
return this.businessMetrics?.isReadyToSubmitEvents() ?? false;
|
|
173
199
|
}
|
|
174
200
|
|
|
201
|
+
/**
|
|
202
|
+
* @returns whether the current user agent belongs to an automated user
|
|
203
|
+
*/
|
|
204
|
+
isAutomatedUser() {
|
|
205
|
+
return detectAutomatedUser();
|
|
206
|
+
}
|
|
207
|
+
|
|
175
208
|
/**
|
|
176
209
|
* Behavioral event
|
|
177
210
|
* @param args
|
|
@@ -251,6 +284,38 @@ class Metrics extends WebexPlugin {
|
|
|
251
284
|
return this.businessMetrics.submitBusinessEvent({name, payload, table, metadata});
|
|
252
285
|
}
|
|
253
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Call Analyzer: Pre-Login Event
|
|
289
|
+
* @param args
|
|
290
|
+
*/
|
|
291
|
+
submitPreLoginEvent({
|
|
292
|
+
name,
|
|
293
|
+
preLoginId,
|
|
294
|
+
payload,
|
|
295
|
+
metadata,
|
|
296
|
+
}: {
|
|
297
|
+
name: string;
|
|
298
|
+
preLoginId: string;
|
|
299
|
+
payload: EventPayload;
|
|
300
|
+
metadata?: EventPayload;
|
|
301
|
+
}): Promise<void> {
|
|
302
|
+
if (!this.isReady) {
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
this.webex.logger.log(
|
|
305
|
+
`NewMetrics: @submitPreLoginEvent. Attempted to submit before webex.ready: ${name}`
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
return Promise.resolve();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return this.preLoginMetrics.submitPreLoginEvent({
|
|
312
|
+
name,
|
|
313
|
+
preLoginId,
|
|
314
|
+
payload,
|
|
315
|
+
metadata,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
254
319
|
/**
|
|
255
320
|
* Call Analyzer: Media Quality Event
|
|
256
321
|
* @param args
|
|
@@ -283,7 +348,7 @@ class Metrics extends WebexPlugin {
|
|
|
283
348
|
payload?: RecursivePartial<FeatureEvent['payload']>;
|
|
284
349
|
options: any;
|
|
285
350
|
}) {
|
|
286
|
-
if (!this.
|
|
351
|
+
if (!this.isReady) {
|
|
287
352
|
// @ts-ignore
|
|
288
353
|
this.webex.logger.log(
|
|
289
354
|
`NewMetrics: @submitFeatureEvent. Attempted to submit before webex.ready. Event name: ${name}`
|
|
@@ -318,7 +383,7 @@ class Metrics extends WebexPlugin {
|
|
|
318
383
|
payload?: RecursivePartial<ClientEvent['payload']>;
|
|
319
384
|
options?: SubmitClientEventOptions;
|
|
320
385
|
}): Promise<any> {
|
|
321
|
-
if (!this.
|
|
386
|
+
if (!this.isReady) {
|
|
322
387
|
// @ts-ignore
|
|
323
388
|
this.webex.logger.log(
|
|
324
389
|
`NewMetrics: @submitClientEvent. Attempted to submit before webex.ready. Event name: ${name}`
|
|
@@ -331,14 +396,41 @@ class Metrics extends WebexPlugin {
|
|
|
331
396
|
options: {meetingId: options?.meetingId},
|
|
332
397
|
});
|
|
333
398
|
|
|
334
|
-
|
|
399
|
+
const enrichedPayload = this.privacyAndSecurityPermissionEnricher.enrich({
|
|
335
400
|
name,
|
|
336
401
|
payload,
|
|
402
|
+
scope: this.getPermissionScope(options),
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
return this.callDiagnosticMetrics.submitClientEvent({
|
|
406
|
+
name,
|
|
407
|
+
payload: enrichedPayload,
|
|
337
408
|
options,
|
|
338
409
|
delaySubmitEvent: this.delaySubmitClientEvents,
|
|
339
410
|
});
|
|
340
411
|
}
|
|
341
412
|
|
|
413
|
+
/**
|
|
414
|
+
* Stores the latest browser permission state supplied by the client.
|
|
415
|
+
* @param permission latest normalized permission snapshot
|
|
416
|
+
*/
|
|
417
|
+
public setPrivacyAndSecurityPermission(permission: PrivacyAndSecurityPermission): void {
|
|
418
|
+
this.privacyAndSecurityPermissionEnricher.setPermission(permission);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Resolves event options to the call identity used by Call Diagnostic where possible.
|
|
423
|
+
* @param options client event options
|
|
424
|
+
* @returns the permission history scope
|
|
425
|
+
*/
|
|
426
|
+
private getPermissionScope(options?: SubmitClientEventOptions): string {
|
|
427
|
+
const meeting = options?.meetingId
|
|
428
|
+
? (this as any).webex.meetings?.getBasicMeetingInformation?.(options.meetingId)
|
|
429
|
+
: undefined;
|
|
430
|
+
|
|
431
|
+
return options?.correlationId ?? meeting?.correlationId ?? 'default';
|
|
432
|
+
}
|
|
433
|
+
|
|
342
434
|
/**
|
|
343
435
|
* Issue request to alias a user's pre-login ID with their CI UUID
|
|
344
436
|
* @param {string} preLoginId
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import GenericMetrics from './generic-metrics';
|
|
2
|
+
import {BusinessEvent, EventPayload} from './metrics.types';
|
|
3
|
+
import PreLoginMetricsBatcher from './prelogin-metrics-batcher';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description Util class to handle PreLogin Metrics
|
|
7
|
+
* @export
|
|
8
|
+
* @class PreLoginMetrics
|
|
9
|
+
*/
|
|
10
|
+
export default class PreLoginMetrics extends GenericMetrics {
|
|
11
|
+
private preLoginMetricsBatcher: typeof PreLoginMetricsBatcher;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Constructor
|
|
15
|
+
* @param {PreLoginMetricsBatcher} preLoginMetricsBatcher - Pre-login metrics batcher
|
|
16
|
+
* @param {any} attrs - Attributes
|
|
17
|
+
* @param {any} options - Options
|
|
18
|
+
* @constructor
|
|
19
|
+
*/
|
|
20
|
+
constructor(
|
|
21
|
+
preLoginMetricsBatcher: typeof PreLoginMetricsBatcher,
|
|
22
|
+
attrs: any = {},
|
|
23
|
+
options: {parent?: any} = {}
|
|
24
|
+
) {
|
|
25
|
+
super(attrs, options);
|
|
26
|
+
this.preLoginMetricsBatcher = preLoginMetricsBatcher;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Submit a business metric to our metrics endpoint.
|
|
31
|
+
* Routes to the correct table with the correct schema payload by table.
|
|
32
|
+
* @see https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Business+metrics++-%3E+ROMA
|
|
33
|
+
* @param {Object} options - The options object
|
|
34
|
+
* @param {string} options.name - Name of the metric
|
|
35
|
+
* @param {string} options.preLoginId - ID to identify pre-login user
|
|
36
|
+
* @param {EventPayload} options.payload - User payload of the metric
|
|
37
|
+
* @param {EventPayload} [options.metadata] - Optional metadata to include outside of eventPayload.value
|
|
38
|
+
* @returns {Promise<void>} Promise that resolves when the metric is submitted
|
|
39
|
+
*/
|
|
40
|
+
public submitPreLoginEvent({
|
|
41
|
+
name,
|
|
42
|
+
preLoginId,
|
|
43
|
+
payload,
|
|
44
|
+
metadata,
|
|
45
|
+
}: {
|
|
46
|
+
name: string;
|
|
47
|
+
preLoginId: string;
|
|
48
|
+
payload: EventPayload;
|
|
49
|
+
metadata?: EventPayload;
|
|
50
|
+
}): Promise<void> {
|
|
51
|
+
if (!metadata) {
|
|
52
|
+
metadata = {};
|
|
53
|
+
}
|
|
54
|
+
if (!metadata.appType) {
|
|
55
|
+
metadata.appType = 'Web Client';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const finalEvent = this.buildEvent(name, preLoginId, payload, metadata);
|
|
59
|
+
|
|
60
|
+
this.preLoginMetricsBatcher.savePreLoginId(preLoginId);
|
|
61
|
+
|
|
62
|
+
return this.preLoginMetricsBatcher.request(finalEvent);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Builds a formatted event object for metrics submission.
|
|
67
|
+
* @param {string} metricName - Metric name
|
|
68
|
+
* @param {string} preLoginId - Pre-login user identifier
|
|
69
|
+
* @param {EventPayload} payload - Metric payload data
|
|
70
|
+
* @param {EventPayload} metadata - Additional metadata to include in the event
|
|
71
|
+
* @returns {object} Formatted metrics event object with type, eventPayload, and timestamp
|
|
72
|
+
*/
|
|
73
|
+
private buildEvent(
|
|
74
|
+
metricName: string,
|
|
75
|
+
preLoginId: string,
|
|
76
|
+
payload: EventPayload,
|
|
77
|
+
metadata: EventPayload
|
|
78
|
+
): BusinessEvent {
|
|
79
|
+
return {
|
|
80
|
+
type: ['business'],
|
|
81
|
+
eventPayload: {
|
|
82
|
+
metricName,
|
|
83
|
+
browserDetails: this.getBrowserDetails(),
|
|
84
|
+
context: this.getContext(),
|
|
85
|
+
timestamp: new Date().getTime(),
|
|
86
|
+
value: {
|
|
87
|
+
preLoginId,
|
|
88
|
+
...metadata,
|
|
89
|
+
...payload,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type {ClientEvent} from '../metrics.types';
|
|
2
|
+
import type {PermissionEnrichmentPolicy} from './types';
|
|
3
|
+
|
|
4
|
+
export const CAMERA_AND_MICROPHONE_PERMISSION_EVENTS = new Set<ClientEvent['name']>([
|
|
5
|
+
'client.call.initiated',
|
|
6
|
+
'client.media.capabilities',
|
|
7
|
+
'client.ice.end',
|
|
8
|
+
'client.locus.join.request',
|
|
9
|
+
'client.locus.join.response',
|
|
10
|
+
'client.media-engine.ready',
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export const MEDIA_TX_PERMISSION_EVENTS = new Set<ClientEvent['name']>([
|
|
14
|
+
'client.media.tx.start',
|
|
15
|
+
'client.media.tx.stop',
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const CONTENT_SHARE_PERMISSION_EVENTS = new Set<ClientEvent['name']>([
|
|
19
|
+
'client.share.initiated',
|
|
20
|
+
'client.share.floor-grant.request',
|
|
21
|
+
'client.share.floor-granted.local',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
export const FINAL_PERMISSION_EVENTS = new Set<ClientEvent['name']>([
|
|
25
|
+
'client.call.leave',
|
|
26
|
+
'client.call.remote-ended',
|
|
27
|
+
'client.call.aborted',
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
export const NO_PERMISSION_ENRICHMENT: PermissionEnrichmentPolicy = {
|
|
31
|
+
resources: [],
|
|
32
|
+
terminal: false,
|
|
33
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {ClientEventPayload, PrivacyAndSecurityPermission} from '../metrics.types';
|
|
2
|
+
import {
|
|
3
|
+
CAMERA_AND_MICROPHONE_PERMISSION_EVENTS,
|
|
4
|
+
CONTENT_SHARE_PERMISSION_EVENTS,
|
|
5
|
+
FINAL_PERMISSION_EVENTS,
|
|
6
|
+
MEDIA_TX_PERMISSION_EVENTS,
|
|
7
|
+
NO_PERMISSION_ENRICHMENT,
|
|
8
|
+
} from './constants';
|
|
9
|
+
import type {
|
|
10
|
+
PermissionEnrichmentContext,
|
|
11
|
+
PermissionEnrichmentPolicy,
|
|
12
|
+
PermissionEnrichmentRule,
|
|
13
|
+
} from './types';
|
|
14
|
+
import {
|
|
15
|
+
getChangedPermission,
|
|
16
|
+
projectPrivacyAndSecurityPermission,
|
|
17
|
+
resolveContentShareResources,
|
|
18
|
+
resolveMediaResources,
|
|
19
|
+
} from './utils';
|
|
20
|
+
|
|
21
|
+
export const PERMISSION_ENRICHMENT_RULES = [
|
|
22
|
+
{
|
|
23
|
+
events: CAMERA_AND_MICROPHONE_PERMISSION_EVENTS,
|
|
24
|
+
resolve: () => ({resources: ['camera', 'microphone'], terminal: false}),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
events: MEDIA_TX_PERMISSION_EVENTS,
|
|
28
|
+
resolve: (payload) => ({resources: resolveMediaResources(payload), terminal: false}),
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
events: CONTENT_SHARE_PERMISSION_EVENTS,
|
|
32
|
+
resolve: (payload) => ({
|
|
33
|
+
resources: resolveContentShareResources(payload),
|
|
34
|
+
terminal: false,
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
events: FINAL_PERMISSION_EVENTS,
|
|
39
|
+
resolve: () => ({resources: ['camera', 'microphone', 'contentShare'], terminal: true}),
|
|
40
|
+
},
|
|
41
|
+
] satisfies readonly PermissionEnrichmentRule[];
|
|
42
|
+
|
|
43
|
+
const resolvePermissionEnrichmentPolicy = (
|
|
44
|
+
name: PermissionEnrichmentContext['name'],
|
|
45
|
+
payload?: ClientEventPayload
|
|
46
|
+
): PermissionEnrichmentPolicy =>
|
|
47
|
+
PERMISSION_ENRICHMENT_RULES.find(({events}) => events.has(name))?.resolve(payload) ??
|
|
48
|
+
NO_PERMISSION_ENRICHMENT;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Enriches eligible client events with relevant browser permission changes.
|
|
52
|
+
*/
|
|
53
|
+
export default class PrivacyAndSecurityPermissionEnricher {
|
|
54
|
+
private permission?: PrivacyAndSecurityPermission;
|
|
55
|
+
|
|
56
|
+
private lastReported = new Map<string, PrivacyAndSecurityPermission>();
|
|
57
|
+
|
|
58
|
+
private readonly onEnrichmentError: (error: unknown) => void;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a permission enricher.
|
|
62
|
+
* @param {Function} onEnrichmentError permission enrichment error handler
|
|
63
|
+
*/
|
|
64
|
+
constructor(onEnrichmentError: (error: unknown) => void) {
|
|
65
|
+
this.onEnrichmentError = onEnrichmentError;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Stores the latest normalized browser permission state.
|
|
70
|
+
* @param {PrivacyAndSecurityPermission} permission permission snapshot
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
public setPermission(permission: PrivacyAndSecurityPermission): void {
|
|
74
|
+
this.permission = projectPrivacyAndSecurityPermission(permission, [
|
|
75
|
+
'camera',
|
|
76
|
+
'microphone',
|
|
77
|
+
'contentShare',
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns the original payload or a copy enriched with relevant permission changes.
|
|
83
|
+
* @param {PermissionEnrichmentContext} context event context and permission history scope
|
|
84
|
+
* @returns {ClientEventPayload | undefined}
|
|
85
|
+
*/
|
|
86
|
+
public enrich({
|
|
87
|
+
name,
|
|
88
|
+
payload,
|
|
89
|
+
scope,
|
|
90
|
+
}: PermissionEnrichmentContext): ClientEventPayload | undefined {
|
|
91
|
+
const policy = resolvePermissionEnrichmentPolicy(name, payload);
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
if (payload?.privacyAndSecurityPermission !== undefined) {
|
|
95
|
+
// An explicitly supplied permission payload is authoritative for this event.
|
|
96
|
+
if (!policy.terminal) {
|
|
97
|
+
this.lastReported.set(scope, {
|
|
98
|
+
...this.lastReported.get(scope),
|
|
99
|
+
...(payload.privacyAndSecurityPermission as PrivacyAndSecurityPermission),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return payload;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (policy.resources.length === 0) {
|
|
107
|
+
return payload;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const projectedPermission = this.permission
|
|
111
|
+
? projectPrivacyAndSecurityPermission(this.permission, policy.resources)
|
|
112
|
+
: undefined;
|
|
113
|
+
|
|
114
|
+
if (!projectedPermission) {
|
|
115
|
+
return payload;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (policy.terminal) {
|
|
119
|
+
return {...payload, privacyAndSecurityPermission: projectedPermission};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const lastReported = this.lastReported.get(scope) ?? {};
|
|
123
|
+
const changedPermission = getChangedPermission(projectedPermission, lastReported);
|
|
124
|
+
|
|
125
|
+
if (!changedPermission) {
|
|
126
|
+
return payload;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.lastReported.set(scope, {...lastReported, ...changedPermission});
|
|
130
|
+
|
|
131
|
+
return {...payload, privacyAndSecurityPermission: changedPermission};
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.onEnrichmentError(error);
|
|
134
|
+
|
|
135
|
+
return payload;
|
|
136
|
+
} finally {
|
|
137
|
+
if (policy.terminal) {
|
|
138
|
+
this.lastReported.delete(scope);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientEvent,
|
|
3
|
+
ClientEventPayload,
|
|
4
|
+
PrivacyAndSecurityPermissionResource,
|
|
5
|
+
} from '../metrics.types';
|
|
6
|
+
|
|
7
|
+
export type PermissionEnrichmentPolicy = {
|
|
8
|
+
resources: readonly PrivacyAndSecurityPermissionResource[];
|
|
9
|
+
terminal: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type PermissionEnrichmentRule = {
|
|
13
|
+
events: ReadonlySet<ClientEvent['name']>;
|
|
14
|
+
resolve: (payload?: ClientEventPayload) => PermissionEnrichmentPolicy;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type PermissionEnrichmentContext = {
|
|
18
|
+
name: ClientEvent['name'];
|
|
19
|
+
payload?: ClientEventPayload;
|
|
20
|
+
scope: string;
|
|
21
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {isEqual} from 'lodash';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ClientEventPayload,
|
|
5
|
+
PrivacyAndSecurityPermission,
|
|
6
|
+
PrivacyAndSecurityPermissionResource,
|
|
7
|
+
} from '../metrics.types';
|
|
8
|
+
|
|
9
|
+
export const resolveMediaResources = (
|
|
10
|
+
payload?: ClientEventPayload
|
|
11
|
+
): PrivacyAndSecurityPermissionResource[] => {
|
|
12
|
+
switch (payload?.mediaType) {
|
|
13
|
+
case 'audio':
|
|
14
|
+
return ['microphone'];
|
|
15
|
+
case 'video':
|
|
16
|
+
return ['camera'];
|
|
17
|
+
case 'share':
|
|
18
|
+
return ['contentShare'];
|
|
19
|
+
default:
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const resolveContentShareResources = (
|
|
25
|
+
payload?: ClientEventPayload
|
|
26
|
+
): PrivacyAndSecurityPermissionResource[] => {
|
|
27
|
+
if (payload?.mediaType !== 'share') {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return ['contentShare'];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const copyPermissionState = (
|
|
35
|
+
source: PrivacyAndSecurityPermission,
|
|
36
|
+
target: PrivacyAndSecurityPermission,
|
|
37
|
+
resource: PrivacyAndSecurityPermissionResource
|
|
38
|
+
): void => {
|
|
39
|
+
switch (resource) {
|
|
40
|
+
case 'camera':
|
|
41
|
+
if (source.camera) {
|
|
42
|
+
target.camera = {...source.camera};
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
case 'microphone':
|
|
46
|
+
if (source.microphone) {
|
|
47
|
+
target.microphone = {...source.microphone};
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
case 'contentShare':
|
|
51
|
+
if (source.contentShare) {
|
|
52
|
+
target.contentShare = {...source.contentShare};
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const projectPrivacyAndSecurityPermission = (
|
|
61
|
+
permission: PrivacyAndSecurityPermission,
|
|
62
|
+
resources: readonly PrivacyAndSecurityPermissionResource[]
|
|
63
|
+
): PrivacyAndSecurityPermission | undefined => {
|
|
64
|
+
const projectedPermission: PrivacyAndSecurityPermission = {};
|
|
65
|
+
|
|
66
|
+
resources.forEach((resource) => {
|
|
67
|
+
copyPermissionState(permission, projectedPermission, resource);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return Object.keys(projectedPermission).length > 0 ? projectedPermission : undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const getChangedPermission = (
|
|
74
|
+
current: PrivacyAndSecurityPermission,
|
|
75
|
+
previous: PrivacyAndSecurityPermission
|
|
76
|
+
): PrivacyAndSecurityPermission | undefined => {
|
|
77
|
+
const changedResources = (Object.keys(current) as PrivacyAndSecurityPermissionResource[]).filter(
|
|
78
|
+
(resource) => !isEqual(current[resource], previous[resource])
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return projectPrivacyAndSecurityPermission(current, changedResources);
|
|
82
|
+
};
|