@@ -14,10 +14,12 @@ export class DeviceIdService {
14
14
private deviceIdPromise : Promise < string > | undefined = undefined ;
15
15
private abortController : AbortController | undefined = undefined ;
16
16
private logger : LoggerBase ;
17
- private getMachineId : ( ) => Promise < string > ;
17
+ private readonly getMachineId : ( ) => Promise < string > ;
18
+ private timeout : number ;
18
19
19
- private constructor ( logger : LoggerBase ) {
20
+ private constructor ( logger : LoggerBase , timeout : number ) {
20
21
this . logger = logger ;
22
+ this . timeout = timeout ;
21
23
this . getMachineId = ( ) : Promise < string > => nodeMachineId . machineId ( true ) ;
22
24
// Start device ID calculation immediately
23
25
this . startDeviceIdCalculation ( ) ;
@@ -29,36 +31,33 @@ export class DeviceIdService {
29
31
* @param logger - The logger instance to use
30
32
* @returns The DeviceIdService instance
31
33
*/
32
- public static init ( logger : LoggerBase ) : DeviceIdService {
34
+ public static init ( logger : LoggerBase , timeout ?: number ) : DeviceIdService {
33
35
if ( DeviceIdService . instance ) {
34
36
return DeviceIdService . instance ;
35
37
}
36
- DeviceIdService . instance = new DeviceIdService ( logger ) ;
38
+ DeviceIdService . instance = new DeviceIdService ( logger , timeout ?? DEVICE_ID_TIMEOUT ) ;
37
39
return DeviceIdService . instance ;
38
40
}
39
41
42
+ /**
43
+ * Checks if the DeviceIdService is initialized.
44
+ * @returns True if the DeviceIdService is initialized, false otherwise
45
+ */
46
+ public static isInitialized ( ) : boolean {
47
+ return DeviceIdService . instance !== undefined ;
48
+ }
49
+
40
50
/**
41
51
* Gets the singleton instance of DeviceIdService.
42
52
* @returns The DeviceIdService instance
43
53
*/
44
54
public static getInstance ( ) : DeviceIdService {
45
55
if ( ! DeviceIdService . instance ) {
46
- throw Error ( "DeviceIdService not initialized" ) ;
56
+ throw new Error ( "DeviceIdService not initialized" ) ;
47
57
}
48
58
return DeviceIdService . instance ;
49
59
}
50
60
51
- /**
52
- * Resets the singleton instance (mainly for testing).
53
- */
54
- static resetInstance ( ) : void {
55
- // abort any ongoing calculation
56
- if ( DeviceIdService . instance ?. abortController ) {
57
- DeviceIdService . instance . abortController . abort ( ) ;
58
- }
59
- DeviceIdService . instance = undefined ;
60
- }
61
-
62
61
/**
63
62
* Starts the device ID calculation process.
64
63
* This method is called automatically in the constructor.
@@ -77,28 +76,27 @@ export class DeviceIdService {
77
76
* @returns Promise that resolves to the device ID string
78
77
*/
79
78
public async getDeviceId ( ) : Promise < string > {
80
- // Return cached value if available
81
79
if ( this . deviceId !== undefined ) {
82
80
return this . deviceId ;
83
81
}
84
82
85
- // If calculation is already in progress, wait for it
86
- if ( this . deviceIdPromise ) {
87
- return this . deviceIdPromise ;
83
+ if ( ! this . deviceIdPromise ) {
84
+ throw new Error ( "DeviceIdService calculation not started" ) ;
88
85
}
89
86
90
- // If somehow we don't have a promise, raise an error
91
- throw new Error ( "Failed to get device ID" ) ;
87
+ return this . deviceIdPromise ;
92
88
}
93
89
/**
94
90
* Aborts any ongoing device ID calculation.
95
91
*/
96
- abortCalculation ( ) : void {
92
+ public close ( ) : void {
97
93
if ( this . abortController ) {
98
94
this . abortController . abort ( ) ;
99
95
this . abortController = undefined ;
100
96
}
97
+ this . deviceId = undefined ;
101
98
this . deviceIdPromise = undefined ;
99
+ DeviceIdService . instance = undefined ;
102
100
}
103
101
104
102
/**
@@ -113,8 +111,9 @@ export class DeviceIdService {
113
111
const deviceId = await getDeviceId ( {
114
112
getMachineId : this . getMachineId ,
115
113
onError : ( reason , error ) => {
116
- this . handleDeviceIdError ( reason , error ) ;
114
+ this . handleDeviceIdError ( reason , String ( error ) ) ;
117
115
} ,
116
+ timeout : this . timeout ,
118
117
abortSignal : this . abortController . signal ,
119
118
} ) ;
120
119
@@ -128,7 +127,7 @@ export class DeviceIdService {
128
127
}
129
128
130
129
this . logger . debug ( {
131
- id : LogId . telemetryDeviceIdFailure ,
130
+ id : LogId . deviceIdResolutionError ,
132
131
context : "deviceId" ,
133
132
message : `Failed to get device ID: ${ String ( error ) } ` ,
134
133
} ) ;
@@ -146,20 +145,21 @@ export class DeviceIdService {
146
145
* @param reason - The reason for the error
147
146
* @param error - The error object
148
147
*/
149
- private handleDeviceIdError ( reason : string , error : Error ) : void {
148
+ private handleDeviceIdError ( reason : string , error : string ) : void {
150
149
switch ( reason ) {
151
150
case "resolutionError" :
152
151
this . logger . debug ( {
153
- id : LogId . telemetryDeviceIdFailure ,
152
+ id : LogId . deviceIdResolutionError ,
154
153
context : "deviceId" ,
155
- message : `Device ID resolution error: ${ String ( error ) } ` ,
154
+ message : `Resolution error: ${ String ( error ) } ` ,
156
155
} ) ;
157
156
break ;
158
157
case "timeout" :
159
158
this . logger . debug ( {
160
- id : LogId . telemetryDeviceIdTimeout ,
159
+ id : LogId . deviceIdTimeout ,
161
160
context : "deviceId" ,
162
161
message : "Device ID retrieval timed out" ,
162
+ noRedaction : true ,
163
163
} ) ;
164
164
break ;
165
165
case "abort" :
0 commit comments