|  | 
|  | 1 | +import { getDeviceId } from "@mongodb-js/device-id"; | 
|  | 2 | +import nodeMachineId from "node-machine-id"; | 
|  | 3 | +import { LogId, LoggerBase } from "../common/logger.js"; | 
|  | 4 | + | 
|  | 5 | +export const DEVICE_ID_TIMEOUT = 3000; | 
|  | 6 | + | 
|  | 7 | +export class DeviceId { | 
|  | 8 | +    private deviceId: string | undefined = undefined; | 
|  | 9 | +    private deviceIdPromise: Promise<string> | undefined = undefined; | 
|  | 10 | +    private abortController: AbortController | undefined = undefined; | 
|  | 11 | +    private logger: LoggerBase; | 
|  | 12 | +    private readonly getMachineId: () => Promise<string>; | 
|  | 13 | +    private timeout: number; | 
|  | 14 | +    private static instance: DeviceId | undefined = undefined; | 
|  | 15 | + | 
|  | 16 | +    private constructor(logger: LoggerBase, timeout: number = DEVICE_ID_TIMEOUT) { | 
|  | 17 | +        this.logger = logger; | 
|  | 18 | +        this.timeout = timeout; | 
|  | 19 | +        this.getMachineId = (): Promise<string> => nodeMachineId.machineId(true); | 
|  | 20 | +    } | 
|  | 21 | + | 
|  | 22 | +    public static create(logger: LoggerBase, timeout?: number): DeviceId { | 
|  | 23 | +        if (this.instance) { | 
|  | 24 | +            throw new Error("DeviceId instance already exists, use get() to retrieve the device ID"); | 
|  | 25 | +        } | 
|  | 26 | + | 
|  | 27 | +        const instance = new DeviceId(logger, timeout ?? DEVICE_ID_TIMEOUT); | 
|  | 28 | +        instance.setup(); | 
|  | 29 | + | 
|  | 30 | +        this.instance = instance; | 
|  | 31 | + | 
|  | 32 | +        return instance; | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    private setup(): void { | 
|  | 36 | +        this.deviceIdPromise = this.calculateDeviceId(); | 
|  | 37 | +    } | 
|  | 38 | + | 
|  | 39 | +    /** | 
|  | 40 | +     * Closes the device ID calculation promise and abort controller. | 
|  | 41 | +     */ | 
|  | 42 | +    public close(): void { | 
|  | 43 | +        if (this.abortController) { | 
|  | 44 | +            this.abortController.abort(); | 
|  | 45 | +            this.abortController = undefined; | 
|  | 46 | +        } | 
|  | 47 | + | 
|  | 48 | +        this.deviceId = undefined; | 
|  | 49 | +        this.deviceIdPromise = undefined; | 
|  | 50 | +        DeviceId.instance = undefined; | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    /** | 
|  | 54 | +     * Gets the device ID, waiting for the calculation to complete if necessary. | 
|  | 55 | +     * @returns Promise that resolves to the device ID string | 
|  | 56 | +     */ | 
|  | 57 | +    public get(): Promise<string> { | 
|  | 58 | +        if (this.deviceId) { | 
|  | 59 | +            return Promise.resolve(this.deviceId); | 
|  | 60 | +        } | 
|  | 61 | + | 
|  | 62 | +        if (this.deviceIdPromise) { | 
|  | 63 | +            return this.deviceIdPromise; | 
|  | 64 | +        } | 
|  | 65 | + | 
|  | 66 | +        return this.calculateDeviceId(); | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    /** | 
|  | 70 | +     * Internal method that performs the actual device ID calculation. | 
|  | 71 | +     */ | 
|  | 72 | +    private async calculateDeviceId(): Promise<string> { | 
|  | 73 | +        if (!this.abortController) { | 
|  | 74 | +            this.abortController = new AbortController(); | 
|  | 75 | +        } | 
|  | 76 | + | 
|  | 77 | +        this.deviceIdPromise = getDeviceId({ | 
|  | 78 | +            getMachineId: this.getMachineId, | 
|  | 79 | +            onError: (reason, error) => { | 
|  | 80 | +                this.handleDeviceIdError(reason, String(error)); | 
|  | 81 | +            }, | 
|  | 82 | +            timeout: this.timeout, | 
|  | 83 | +            abortSignal: this.abortController.signal, | 
|  | 84 | +        }); | 
|  | 85 | + | 
|  | 86 | +        return this.deviceIdPromise; | 
|  | 87 | +    } | 
|  | 88 | + | 
|  | 89 | +    private handleDeviceIdError(reason: string, error: string): void { | 
|  | 90 | +        this.deviceIdPromise = Promise.resolve("unknown"); | 
|  | 91 | + | 
|  | 92 | +        switch (reason) { | 
|  | 93 | +            case "resolutionError": | 
|  | 94 | +                this.logger.debug({ | 
|  | 95 | +                    id: LogId.deviceIdResolutionError, | 
|  | 96 | +                    context: "deviceId", | 
|  | 97 | +                    message: `Resolution error: ${String(error)}`, | 
|  | 98 | +                }); | 
|  | 99 | +                break; | 
|  | 100 | +            case "timeout": | 
|  | 101 | +                this.logger.debug({ | 
|  | 102 | +                    id: LogId.deviceIdTimeout, | 
|  | 103 | +                    context: "deviceId", | 
|  | 104 | +                    message: "Device ID retrieval timed out", | 
|  | 105 | +                    noRedaction: true, | 
|  | 106 | +                }); | 
|  | 107 | +                break; | 
|  | 108 | +            case "abort": | 
|  | 109 | +                // No need to log in the case of 'abort' errors | 
|  | 110 | +                break; | 
|  | 111 | +        } | 
|  | 112 | +    } | 
|  | 113 | +} | 
0 commit comments