Skip to content

Commit 9c41eef

Browse files
committed
[chore] tweak get-device-id
Removes isNodeMachineId arguments and simplifies onTimeout
1 parent d643a66 commit 9c41eef

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

packages/device-id/src/get-device-id.spec.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe('getDeviceId', function () {
88

99
const deviceId = await getDeviceId({
1010
getMachineId,
11-
isNodeMachineId: false,
1211
}).value;
1312

1413
expect(deviceId).to.be.a('string');
@@ -22,12 +21,10 @@ describe('getDeviceId', function () {
2221

2322
const resultA = await getDeviceId({
2423
getMachineId,
25-
isNodeMachineId: true,
2624
}).value;
2725

2826
const resultB = await getDeviceId({
2927
getMachineId: () => Promise.resolve(mockMachineId.toUpperCase()),
30-
isNodeMachineId: true,
3128
}).value;
3229

3330
expect(resultA).to.equal(resultB);
@@ -39,7 +36,6 @@ describe('getDeviceId', function () {
3936

4037
const deviceId = await getDeviceId({
4138
getMachineId,
42-
isNodeMachineId: false,
4339
onError: (error) => {
4440
capturedError = error;
4541
},
@@ -56,7 +52,6 @@ describe('getDeviceId', function () {
5652

5753
const result = await getDeviceId({
5854
getMachineId,
59-
isNodeMachineId: false,
6055
onError: (err) => {
6156
capturedError = err;
6257
},
@@ -72,18 +67,16 @@ describe('getDeviceId', function () {
7267

7368
const resultA = await getDeviceId({
7469
getMachineId,
75-
isNodeMachineId: false,
7670
}).value;
7771

7872
const resultB = await getDeviceId({
7973
getMachineId,
80-
isNodeMachineId: false,
8174
}).value;
8275

8376
expect(resultA).to.equal(resultB);
8477
});
8578

86-
it('handles timeout when getting machine id', async function () {
79+
it('resolves timeout with "unknown" by default', async function () {
8780
let timeoutId: NodeJS.Timeout;
8881
const getMachineId = () =>
8982
new Promise<string>((resolve) => {
@@ -93,7 +86,6 @@ describe('getDeviceId', function () {
9386
let errorCalled = false;
9487
const result = await getDeviceId({
9588
getMachineId,
96-
isNodeMachineId: false,
9789
onError: () => {
9890
errorCalled = true;
9991
},
@@ -106,6 +98,57 @@ describe('getDeviceId', function () {
10698
expect(errorCalled).to.equal(false);
10799
});
108100

101+
it('resolves with result of onTimeout when successful', async function () {
102+
let timeoutId: NodeJS.Timeout;
103+
const getMachineId = () =>
104+
new Promise<string>((resolve) => {
105+
timeoutId = setTimeout(() => resolve('delayed-id'), 10_000);
106+
});
107+
108+
let errorCalled = false;
109+
const result = await getDeviceId({
110+
getMachineId,
111+
onError: () => {
112+
errorCalled = true;
113+
},
114+
timeout: 1,
115+
onTimeout: () => {
116+
return 'abc-123';
117+
},
118+
}).value;
119+
120+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
121+
clearTimeout(timeoutId!);
122+
expect(result).to.equal('abc-123');
123+
expect(errorCalled).to.equal(false);
124+
});
125+
126+
it('rejects with an error if onTimeout throws', async function () {
127+
let timeoutId: NodeJS.Timeout;
128+
const getMachineId = () =>
129+
new Promise<string>((resolve) => {
130+
timeoutId = setTimeout(() => resolve('delayed-id'), 10_000);
131+
});
132+
133+
let errorCalled = false;
134+
try {
135+
const result = await getDeviceId({
136+
getMachineId,
137+
onError: () => {
138+
errorCalled = true;
139+
},
140+
timeout: 1,
141+
onTimeout: () => {
142+
throw new Error('Operation timed out');
143+
},
144+
}).value;
145+
expect.fail('Expected promise to be rejected');
146+
} catch (error) {
147+
expect((error as Error).message).to.equal('Operation timed out');
148+
expect(errorCalled).to.equal(false);
149+
}
150+
});
151+
109152
it('handles external promise resolution', async function () {
110153
let timeoutId: NodeJS.Timeout;
111154
const getMachineId = () =>
@@ -115,7 +158,6 @@ describe('getDeviceId', function () {
115158

116159
const { resolve, value } = getDeviceId({
117160
getMachineId,
118-
isNodeMachineId: false,
119161
});
120162

121163
resolve('external-id');
@@ -140,7 +182,6 @@ describe('getDeviceId', function () {
140182

141183
const { reject, value } = getDeviceId({
142184
getMachineId,
143-
isNodeMachineId: false,
144185
});
145186

146187
reject(error);

packages/device-id/src/get-device-id.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createHmac } from 'crypto';
22

33
export function getDeviceId({
44
getMachineId,
5-
isNodeMachineId,
65
onError,
76
timeout = 3000,
87
onTimeout,
@@ -21,15 +20,14 @@ export function getDeviceId({
2120
const value = Promise.race([
2221
resolveMachineId({
2322
getMachineId,
24-
isNodeMachineId,
2523
onError,
2624
}),
2725
new Promise<string>((resolve, reject) => {
2826
timeoutId = setTimeout(() => {
29-
if (onTimeout) {
30-
onTimeout(resolve, reject);
31-
} else {
32-
resolve('unknown');
27+
try {
28+
resolve(onTimeout?.() ?? 'unknown');
29+
} catch (error) {
30+
reject(error);
3331
}
3432
}, timeout).unref?.();
3533

@@ -48,28 +46,20 @@ export function getDeviceId({
4846
export type GetDeviceIdOptions = {
4947
/** A function that returns a raw machine ID. */
5048
getMachineId: () => Promise<string | undefined>;
51-
/** When using node-machine-id, the ID is made uppercase to be consistent with other libraries. */
52-
isNodeMachineId: boolean;
5349
/** Runs when an error occurs while getting the machine ID. */
5450
onError?: (error: Error) => void;
5551
/** Timeout in milliseconds. Defaults to 3000ms. Set to `undefined` to disable. */
5652
timeout?: number | undefined;
5753
/** Runs when the timeout is reached. By default, resolves to "unknown". */
58-
onTimeout?: (
59-
resolve: (value: string) => void,
60-
reject: (err: Error) => void,
61-
) => void;
54+
onTimeout?: () => PromiseLike<string> | string;
6255
};
6356

6457
async function resolveMachineId({
6558
getMachineId,
66-
isNodeMachineId,
6759
onError,
6860
}: GetDeviceIdOptions): Promise<string> {
6961
try {
70-
const originalId = isNodeMachineId
71-
? (await getMachineId())?.toUpperCase()
72-
: await getMachineId();
62+
const originalId = (await getMachineId())?.toUpperCase();
7363

7464
if (!originalId) {
7565
onError?.(new Error('Failed to resolve machine ID'));
@@ -78,10 +68,7 @@ async function resolveMachineId({
7868

7969
// Create a hashed format from the machine ID
8070
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
81-
const hmac = createHmac(
82-
'sha256',
83-
isNodeMachineId ? originalId : originalId,
84-
);
71+
const hmac = createHmac('sha256', originalId);
8572

8673
/** This matches the message used to create the hashes in Atlas CLI */
8774
const DEVICE_ID_HASH_MESSAGE = 'atlascli';

0 commit comments

Comments
 (0)