@@ -64,28 +64,6 @@ export function setupLoggingAndTelemetry(
64
64
return loggingAndTelemetry ;
65
65
}
66
66
67
- /**
68
- * @returns A hashed, unique identifier for the running device or `undefined` if not known.
69
- */
70
- export async function getDeviceId ( ) : Promise < string | 'unknown' > {
71
- // Create a hashed format from the all uppercase version of the machine ID
72
- // to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
73
- const originalId : string = (
74
- await require ( 'native-machine-id' ) . getMachineId ( { raw : true } )
75
- ) ?. toUpperCase ( ) ;
76
-
77
- if ( ! originalId ) {
78
- return 'unknown' ;
79
- }
80
- const hmac = createHmac ( 'sha256' , originalId ) ;
81
-
82
- /** This matches the message used to create the hashes in Atlas CLI */
83
- const DEVICE_ID_HASH_MESSAGE = 'atlascli' ;
84
-
85
- hmac . update ( DEVICE_ID_HASH_MESSAGE ) ;
86
- return hmac . digest ( 'hex' ) ;
87
- }
88
-
89
67
/** @internal */
90
68
export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
91
69
private static dummyLogger = new MongoLogWriter (
@@ -158,19 +136,43 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
158
136
this . resolveDeviceId ( 'unknown' ) ;
159
137
}
160
138
139
+ /**
140
+ * @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
141
+ */
142
+ private async getDeviceId ( ) : Promise < string | 'unknown' > {
143
+ try {
144
+ // Create a hashed format from the all uppercase version of the machine ID
145
+ // to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
146
+ const originalId : string =
147
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
148
+ await require ( 'native-machine-id' ) . getMachineId ( {
149
+ raw : true ,
150
+ } ) ;
151
+
152
+ if ( ! originalId ) {
153
+ return 'unknown' ;
154
+ }
155
+ const hmac = createHmac ( 'sha256' , originalId ) ;
156
+
157
+ /** This matches the message used to create the hashes in Atlas CLI */
158
+ const DEVICE_ID_HASH_MESSAGE = 'atlascli' ;
159
+
160
+ hmac . update ( DEVICE_ID_HASH_MESSAGE ) ;
161
+ return hmac . digest ( 'hex' ) ;
162
+ } catch ( error ) {
163
+ this . bus . emit ( 'mongosh:error' , error as Error , 'telemetry' ) ;
164
+ return 'unknown' ;
165
+ }
166
+ }
167
+
161
168
private async setupTelemetry ( ) : Promise < void > {
162
169
if ( ! this . deviceId ) {
163
- try {
164
- this . deviceId ??= await Promise . race ( [
165
- getDeviceId ( ) ,
166
- new Promise < string > ( ( resolve ) => {
167
- this . resolveDeviceId = resolve ;
168
- } ) ,
169
- ] ) ;
170
- } catch ( error ) {
171
- this . bus . emit ( 'mongosh:error' , error as Error , 'telemetry' ) ;
172
- this . deviceId = 'unknown' ;
173
- }
170
+ this . deviceId = await Promise . race ( [
171
+ this . getDeviceId ( ) ,
172
+ new Promise < string > ( ( resolve ) => {
173
+ this . resolveDeviceId = resolve ;
174
+ } ) ,
175
+ ] ) ;
174
176
}
175
177
176
178
this . runAndClearPendingTelemetryEvents ( ) ;
0 commit comments