@@ -53,40 +53,7 @@ import type {
53
53
MongoshLoggingAndTelemetryArguments ,
54
54
MongoshTrackingProperties ,
55
55
} 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' ;
90
57
91
58
export function setupLoggingAndTelemetry (
92
59
props : MongoshLoggingAndTelemetryArguments
@@ -125,11 +92,11 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
125
92
private isBufferingTelemetryEvents = false ;
126
93
127
94
private deviceId : string | undefined ;
128
- /** @internal */
95
+
96
+ /** @internal Used for awaiting the telemetry setup in tests. */
129
97
public setupTelemetryPromise : Promise < void > = Promise . resolve ( ) ;
130
98
131
- // eslint-disable-next-line @typescript-eslint/no-empty-function
132
- private resolveDeviceId : ( value : string ) => void = ( ) => { } ;
99
+ private readonly telemetrySetupAbort : AbortController = new AbortController ( ) ;
133
100
134
101
constructor ( {
135
102
bus,
@@ -160,26 +127,34 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
160
127
}
161
128
162
129
public flush ( ) : void {
163
- // Run any telemetry events even if device ID hasn't been resolved yet
164
- this . runAndClearPendingTelemetryEvents ( ) ;
165
-
166
130
// Run any other pending events with the set or dummy log for telemetry purposes.
167
131
this . runAndClearPendingBusEvents ( ) ;
168
132
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 ( ) ;
170
136
}
171
137
172
138
private async setupTelemetry ( ) : Promise < void > {
173
139
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
+ }
183
158
}
184
159
185
160
this . runAndClearPendingTelemetryEvents ( ) ;
0 commit comments