@@ -55,6 +55,39 @@ import type {
5555} from './types' ;
5656import { createHmac } from 'crypto' ;
5757
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+ }
90+
5891export function setupLoggingAndTelemetry (
5992 props : MongoshLoggingAndTelemetryArguments
6093) : MongoshLoggingAndTelemetry {
@@ -136,39 +169,13 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
136169 this . resolveDeviceId ( 'unknown' ) ;
137170 }
138171
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-
168172 private async setupTelemetry ( ) : Promise < void > {
169173 if ( ! this . deviceId ) {
170174 this . deviceId = await Promise . race ( [
171- this . getDeviceId ( ) ,
175+ getDeviceId ( {
176+ onError : ( error ) =>
177+ this . bus . emit ( 'mongosh:error' , error , 'telemetry' ) ,
178+ } ) ,
172179 new Promise < string > ( ( resolve ) => {
173180 this . resolveDeviceId = resolve ;
174181 } ) ,
0 commit comments