@@ -53,40 +53,7 @@ import type {
5353 MongoshLoggingAndTelemetryArguments ,
5454 MongoshTrackingProperties ,
5555} from './types' ;
56- import { createHmac } from 'crypto' ;
57-
58- /**
59- * @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
60- */
61- export async function getDeviceId ( {
62- onError,
63- } : {
64- onError ?: ( error : Error ) => void ;
65- } = { } ) : Promise < string | 'unknown' > {
66- try {
67- // Create a hashed format from the all uppercase version of the machine ID
68- // to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
69- const originalId : string =
70- // eslint-disable-next-line @typescript-eslint/no-var-requires
71- await require ( 'native-machine-id' ) . getMachineId ( {
72- raw : true ,
73- } ) ;
74-
75- if ( ! originalId ) {
76- return 'unknown' ;
77- }
78- const hmac = createHmac ( 'sha256' , originalId ) ;
79-
80- /** This matches the message used to create the hashes in Atlas CLI */
81- const DEVICE_ID_HASH_MESSAGE = 'atlascli' ;
82-
83- hmac . update ( DEVICE_ID_HASH_MESSAGE ) ;
84- return hmac . digest ( 'hex' ) ;
85- } catch ( error ) {
86- onError ?.( error as Error ) ;
87- return 'unknown' ;
88- }
89- }
56+ import { getDeviceId } from '@mongodb-js/device-id' ;
9057
9158export function setupLoggingAndTelemetry (
9259 props : MongoshLoggingAndTelemetryArguments
@@ -125,11 +92,11 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
12592 private isBufferingTelemetryEvents = false ;
12693
12794 private deviceId : string | undefined ;
128- /** @internal */
95+
96+ /** @internal Used for awaiting the telemetry setup in tests. */
12997 public setupTelemetryPromise : Promise < void > = Promise . resolve ( ) ;
13098
131- // eslint-disable-next-line @typescript-eslint/no-empty-function
132- private resolveDeviceId : ( value : string ) => void = ( ) => { } ;
99+ private readonly telemetrySetupAbort : AbortController = new AbortController ( ) ;
133100
134101 constructor ( {
135102 bus,
@@ -160,26 +127,34 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
160127 }
161128
162129 public flush ( ) : void {
163- // Run any telemetry events even if device ID hasn't been resolved yet
164- this . runAndClearPendingTelemetryEvents ( ) ;
165-
166130 // Run any other pending events with the set or dummy log for telemetry purposes.
167131 this . runAndClearPendingBusEvents ( ) ;
168132
169- this . resolveDeviceId ( 'unknown' ) ;
133+ // Abort setup, which will cause the device ID to be set to 'unknown'
134+ // and run any remaining telemetry events
135+ this . telemetrySetupAbort . abort ( ) ;
170136 }
171137
172138 private async setupTelemetry ( ) : Promise < void > {
173139 if ( ! this . deviceId ) {
174- this . deviceId = await Promise . race ( [
175- getDeviceId ( {
176- onError : ( error ) =>
177- this . bus . emit ( 'mongosh:error' , error , 'telemetry' ) ,
178- } ) ,
179- new Promise < string > ( ( resolve ) => {
180- this . resolveDeviceId = resolve ;
181- } ) ,
182- ] ) ;
140+ try {
141+ // eslint-disable-next-line @typescript-eslint/no-var-requires
142+ const getMachineId = require ( 'native-machine-id' ) . getMachineId ;
143+ this . deviceId = await getDeviceId ( {
144+ getMachineId : ( ) => getMachineId ( { raw : true } ) ,
145+ onError : ( reason , error ) => {
146+ if ( reason === 'abort' ) {
147+ return ;
148+ }
149+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
150+ this . bus . emit ( 'mongosh:error' , error , 'telemetry' ) ;
151+ } ,
152+ abortSignal : this . telemetrySetupAbort . signal ,
153+ } ) ;
154+ } catch ( error ) {
155+ this . deviceId = 'unknown' ;
156+ this . bus . emit ( 'mongosh:error' , error as Error , 'telemetry' ) ;
157+ }
183158 }
184159
185160 this . runAndClearPendingTelemetryEvents ( ) ;
0 commit comments