-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathclearcut-sender.ts
More file actions
59 lines (49 loc) · 1.5 KB
/
clearcut-sender.ts
File metadata and controls
59 lines (49 loc) · 1.5 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
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import crypto from 'node:crypto';
import {logger} from '../../logger.js';
import type {ChromeDevToolsMcpExtension, OsType} from '../types.js';
const SESSION_ROTATION_INTERVAL_MS = 24 * 60 * 60 * 1000;
export class ClearcutSender {
#appVersion: string;
#osType: OsType;
#sessionId: string;
#sessionCreated: number;
constructor(appVersion: string, osType: OsType) {
this.#appVersion = appVersion;
this.#osType = osType;
this.#sessionId = crypto.randomUUID();
this.#sessionCreated = Date.now();
}
async send(event: ChromeDevToolsMcpExtension): Promise<void> {
this.#rotateSessionIfNeeded();
const enrichedEvent = this.#enrichEvent(event);
this.transport(enrichedEvent);
}
transport(event: ChromeDevToolsMcpExtension): void {
logger('Telemetry event', JSON.stringify(event, null, 2));
}
async sendShutdownEvent(): Promise<void> {
const shutdownEvent: ChromeDevToolsMcpExtension = {
server_shutdown: {},
};
await this.send(shutdownEvent);
}
#rotateSessionIfNeeded(): void {
if (Date.now() - this.#sessionCreated > SESSION_ROTATION_INTERVAL_MS) {
this.#sessionId = crypto.randomUUID();
this.#sessionCreated = Date.now();
}
}
#enrichEvent(event: ChromeDevToolsMcpExtension): ChromeDevToolsMcpExtension {
return {
...event,
session_id: this.#sessionId,
app_version: this.#appVersion,
os_type: this.#osType,
};
}
}