Skip to content

Commit bb97041

Browse files
committed
keep getMachineId under deviceId
1 parent 13a0349 commit bb97041

File tree

6 files changed

+33
-46
lines changed

6 files changed

+33
-46
lines changed

src/helpers/deviceId.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getDeviceId } from "@mongodb-js/device-id";
2+
import nodeMachineId from "node-machine-id";
23
import { LogId, LoggerBase } from "../common/logger.js";
34

45
export const DEVICE_ID_TIMEOUT = 3000;
@@ -15,9 +16,9 @@ export class DeviceIdService {
1516
private logger: LoggerBase;
1617
private getMachineId: () => Promise<string>;
1718

18-
private constructor(logger: LoggerBase, getMachineId: () => Promise<string>) {
19+
private constructor(logger: LoggerBase) {
1920
this.logger = logger;
20-
this.getMachineId = getMachineId;
21+
this.getMachineId = (): Promise<string> => nodeMachineId.machineId(true);
2122
// Start device ID calculation immediately
2223
this.startDeviceIdCalculation();
2324
}
@@ -28,11 +29,11 @@ export class DeviceIdService {
2829
* @param logger - The logger instance to use
2930
* @returns The DeviceIdService instance
3031
*/
31-
public static init(logger: LoggerBase, getMachineId: () => Promise<string>): DeviceIdService {
32+
public static init(logger: LoggerBase): DeviceIdService {
3233
if (DeviceIdService.instance) {
3334
return DeviceIdService.instance;
3435
}
35-
DeviceIdService.instance = new DeviceIdService(logger, getMachineId);
36+
DeviceIdService.instance = new DeviceIdService(logger);
3637
return DeviceIdService.instance;
3738
}
3839

@@ -51,6 +52,10 @@ export class DeviceIdService {
5152
* Resets the singleton instance (mainly for testing).
5253
*/
5354
static resetInstance(): void {
55+
// abort any ongoing calculation
56+
if (DeviceIdService.instance?.abortController) {
57+
DeviceIdService.instance.abortController.abort();
58+
}
5459
DeviceIdService.instance = undefined;
5560
}
5661

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import { StdioRunner } from "./transports/stdio.js";
4343
import { StreamableHttpRunner } from "./transports/streamableHttp.js";
4444
import { systemCA } from "@mongodb-js/devtools-proxy-support";
4545
import { DeviceIdService } from "./helpers/deviceId.js";
46-
import nodeMachineId from "node-machine-id";
4746

4847
async function main(): Promise<void> {
4948
systemCA().catch(() => undefined); // load system CA asynchronously as in mongosh
@@ -52,7 +51,7 @@ async function main(): Promise<void> {
5251
assertVersionMode();
5352

5453
const transportRunner = config.transport === "stdio" ? new StdioRunner(config) : new StreamableHttpRunner(config);
55-
const deviceId = DeviceIdService.init(transportRunner.logger, () => nodeMachineId.machineId(true));
54+
const deviceId = DeviceIdService.init(transportRunner.logger);
5655

5756
const shutdown = (): void => {
5857
transportRunner.logger.info({

tests/integration/helpers.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
import { CompositeLogger } from "../../src/common/logger.js";
2+
import { ExportsManager } from "../../src/common/exportsManager.js";
3+
import { Session } from "../../src/common/session.js";
4+
import { Server } from "../../src/server.js";
5+
import { Telemetry } from "../../src/telemetry/telemetry.js";
6+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
17
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
28
import { InMemoryTransport } from "./inMemoryTransport.js";
3-
import { Server } from "../../src/server.js";
49
import { UserConfig } from "../../src/common/config.js";
10+
import { DeviceIdService } from "../../src/helpers/deviceId.js";
511
import { McpError, ResourceUpdatedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
6-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
7-
import { Session } from "../../src/common/session.js";
8-
import { Telemetry } from "../../src/telemetry/telemetry.js";
912
import { config } from "../../src/common/config.js";
1013
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
1114
import { ConnectionManager } from "../../src/common/connectionManager.js";
12-
import { CompositeLogger } from "../../src/common/logger.js";
13-
import { ExportsManager } from "../../src/common/exportsManager.js";
14-
import { DeviceIdService } from "../../src/helpers/deviceId.js";
15-
import nodeMachineId from "node-machine-id";
1615

1716
interface ParameterInfo {
1817
name: string;
@@ -41,6 +40,7 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
4140
const userConfig = getUserConfig();
4241
const clientTransport = new InMemoryTransport();
4342
const serverTransport = new InMemoryTransport();
43+
const logger = new CompositeLogger();
4444

4545
await serverTransport.start();
4646
await clientTransport.start();
@@ -58,11 +58,10 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
5858
}
5959
);
6060

61-
const logger = new CompositeLogger();
6261
const exportsManager = ExportsManager.init(userConfig, logger);
6362

6463
// Initialize DeviceIdService for tests
65-
DeviceIdService.init(logger, () => nodeMachineId.machineId(true));
64+
DeviceIdService.init(logger);
6665

6766
const connectionManager = new ConnectionManager();
6867

tests/integration/telemetry.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import { describe, expect, it } from "vitest";
66
import { CompositeLogger } from "../../src/common/logger.js";
77
import { ConnectionManager } from "../../src/common/connectionManager.js";
88
import { ExportsManager } from "../../src/common/exportsManager.js";
9-
import nodeMachineId from "node-machine-id";
109

1110
describe("Telemetry", () => {
1211
it("should resolve the actual device ID", async () => {
1312
const logger = new CompositeLogger();
1413

1514
// Initialize DeviceIdService like the main application does
16-
DeviceIdService.init(logger, () => nodeMachineId.machineId(true));
17-
15+
DeviceIdService.init(logger);
1816
const actualDeviceId = await DeviceIdService.getInstance().getDeviceId();
1917

2018
const telemetry = Telemetry.create(

tests/integration/tools/mongodb/connect/connect.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import {
77
} from "../../../helpers.js";
88
import { config } from "../../../../../src/common/config.js";
99
import { defaultTestConfig, setupIntegrationTest } from "../../../helpers.js";
10-
import { beforeEach, describe, expect, it, vi } from "vitest";
11-
12-
// Mock the deviceId utility for consistent testing
13-
vi.mock("../../../../../src/helpers/deviceId.js", () => ({
14-
getDeviceIdForConnection: vi.fn().mockResolvedValue("test-device-id"),
15-
}));
10+
import { beforeEach, describe, expect, it } from "vitest";
1611

1712
describeWithMongoDB(
1813
"SwitchConnection tool",

tests/unit/helpers/deviceId.test.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { CompositeLogger } from "../../../src/common/logger.js";
77
// Mock the dependencies
88
vi.mock("@mongodb-js/device-id");
99
vi.mock("node-machine-id");
10-
1110
const MockGetDeviceId = vi.mocked(getDeviceId);
1211

1312
describe("Device ID Helper", () => {
@@ -18,9 +17,6 @@ describe("Device ID Helper", () => {
1817
vi.clearAllMocks();
1918
testLogger = new CompositeLogger();
2019

21-
// Create a mock getMachineId function
22-
mockGetMachineId = vi.fn().mockResolvedValue("test-machine-id");
23-
2420
// Reset singleton between tests
2521
DeviceIdService.resetInstance();
2622
});
@@ -33,7 +29,7 @@ describe("Device ID Helper", () => {
3329
describe("DeviceIdService Singleton", () => {
3430
it("should return the same instance for multiple getInstance calls", () => {
3531
// Initialize first
36-
DeviceIdService.init(testLogger, mockGetMachineId);
32+
DeviceIdService.init(testLogger);
3733

3834
const instance1 = DeviceIdService.getInstance();
3935
const instance2 = DeviceIdService.getInstance();
@@ -50,25 +46,20 @@ describe("Device ID Helper", () => {
5046
MockGetDeviceId.mockResolvedValue(mockDeviceId);
5147

5248
// Initialize after mocking
53-
DeviceIdService.init(testLogger, mockGetMachineId);
49+
DeviceIdService.init(testLogger);
5450

5551
const deviceId = DeviceIdService.getInstance();
5652
const result = await deviceId.getDeviceId();
5753

5854
expect(result).toBe(mockDeviceId);
59-
expect(MockGetDeviceId).toHaveBeenCalledWith({
60-
getMachineId: mockGetMachineId,
61-
onError: expect.any(Function),
62-
abortSignal: expect.any(AbortSignal),
63-
});
6455
});
6556

6657
it("should cache device ID after first retrieval", async () => {
6758
const mockDeviceId = "test-device-id-123";
6859
MockGetDeviceId.mockResolvedValue(mockDeviceId);
6960

7061
// Initialize after mocking
71-
DeviceIdService.init(testLogger, mockGetMachineId);
62+
DeviceIdService.init(testLogger);
7263

7364
const deviceId = DeviceIdService.getInstance();
7465

@@ -88,7 +79,7 @@ describe("Device ID Helper", () => {
8879
MockGetDeviceId.mockResolvedValue(mockDeviceId);
8980

9081
// Initialize after mocking
91-
DeviceIdService.init(testLogger, mockGetMachineId);
82+
DeviceIdService.init(testLogger);
9283

9384
const deviceId = DeviceIdService.getInstance();
9485

@@ -115,7 +106,7 @@ describe("Device ID Helper", () => {
115106
});
116107

117108
// Initialize after mocking
118-
DeviceIdService.init(testLogger, mockGetMachineId);
109+
DeviceIdService.init(testLogger);
119110

120111
const deviceId = DeviceIdService.getInstance();
121112

@@ -133,7 +124,7 @@ describe("Device ID Helper", () => {
133124
MockGetDeviceId.mockRejectedValue(new Error("Device ID resolution failed"));
134125

135126
// Initialize after mocking
136-
DeviceIdService.init(testLogger, mockGetMachineId);
127+
DeviceIdService.init(testLogger);
137128

138129
const deviceId = DeviceIdService.getInstance();
139130
const result = await deviceId.getDeviceId();
@@ -150,7 +141,7 @@ describe("Device ID Helper", () => {
150141
});
151142

152143
// Initialize after mocking
153-
DeviceIdService.init(testLogger, mockGetMachineId);
144+
DeviceIdService.init(testLogger);
154145

155146
const deviceId = DeviceIdService.getInstance();
156147
const result = await deviceId.getDeviceId();
@@ -167,7 +158,7 @@ describe("Device ID Helper", () => {
167158
});
168159

169160
// Initialize after mocking
170-
DeviceIdService.init(testLogger, mockGetMachineId);
161+
DeviceIdService.init(testLogger);
171162

172163
const deviceId = DeviceIdService.getInstance();
173164
const result = await deviceId.getDeviceId();
@@ -184,7 +175,7 @@ describe("Device ID Helper", () => {
184175
});
185176

186177
// Initialize after mocking
187-
DeviceIdService.init(testLogger, mockGetMachineId);
178+
DeviceIdService.init(testLogger);
188179

189180
const deviceId = DeviceIdService.getInstance();
190181
const result = await deviceId.getDeviceId();
@@ -199,7 +190,7 @@ describe("Device ID Helper", () => {
199190
MockGetDeviceId.mockRejectedValue(new Error("Device ID failed"));
200191

201192
// Initialize after mocking
202-
DeviceIdService.init(testLogger, mockGetMachineId);
193+
DeviceIdService.init(testLogger);
203194

204195
const deviceId = DeviceIdService.getInstance();
205196
const result = await deviceId.getDeviceId();
@@ -211,7 +202,7 @@ describe("Device ID Helper", () => {
211202
MockGetDeviceId.mockResolvedValue("device-id");
212203

213204
// Initialize after mocking
214-
DeviceIdService.init(testLogger, mockGetMachineId);
205+
DeviceIdService.init(testLogger);
215206

216207
const deviceId = DeviceIdService.getInstance();
217208
await deviceId.getDeviceId();
@@ -226,7 +217,7 @@ describe("Device ID Helper", () => {
226217
MockGetDeviceId.mockRejectedValue("String error");
227218

228219
// Initialize after mocking
229-
DeviceIdService.init(testLogger, mockGetMachineId);
220+
DeviceIdService.init(testLogger);
230221

231222
const deviceId = DeviceIdService.getInstance();
232223
const result = await deviceId.getDeviceId();

0 commit comments

Comments
 (0)