@@ -64,28 +64,6 @@ export function setupLoggingAndTelemetry(
6464 return loggingAndTelemetry ;
6565}
6666
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-
8967/** @internal */
9068export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
9169 private static dummyLogger = new MongoLogWriter (
@@ -158,19 +136,43 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
158136 this . resolveDeviceId ( 'unknown' ) ;
159137 }
160138
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+
161168 private async setupTelemetry ( ) : Promise < void > {
162169 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+ ] ) ;
174176 }
175177
176178 this . runAndClearPendingTelemetryEvents ( ) ;
0 commit comments