Skip to content

Commit 98d5c1f

Browse files
committed
refactor: fixes from feedback
1 parent fb33312 commit 98d5c1f

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

packages/machine-id/src/index.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createHash } from 'crypto';
66
import sinonChai from 'sinon-chai';
77
import sinon from 'sinon';
88
import bindings from 'bindings';
9+
import assert from 'assert';
910

1011
chai.use(sinonChai);
1112

@@ -16,7 +17,9 @@ describe('machine-id', function () {
1617
let id: string;
1718

1819
beforeEach(function () {
19-
id = getMachineId({ raw: true }) || '';
20+
const deviceId = getMachineId();
21+
assert(deviceId);
22+
id = deviceId;
2023
});
2124

2225
it('returns a valid UUID format machine ID', function () {
@@ -50,7 +53,9 @@ describe('machine-id', function () {
5053
let id: string;
5154

5255
beforeEach(function () {
53-
id = getMachineId() || '';
56+
const deviceId = getMachineId();
57+
assert(deviceId);
58+
id = deviceId;
5459
});
5560

5661
it('returns a valid SHA256 hash format machine ID', function () {
@@ -59,11 +64,10 @@ describe('machine-id', function () {
5964

6065
expect(hashRegex.test(id));
6166

62-
expect(id).equals(
63-
createHash('sha256')
64-
.update(getMachineId({ raw: true }) || '')
65-
.digest('hex'),
66-
);
67+
const hashId = getMachineId({ raw: true });
68+
assert(hashId);
69+
70+
expect(id).equals(createHash('sha256').update(hashId).digest('hex'));
6771
});
6872
});
6973

packages/machine-id/src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@ export type GetMachineIdOptions = {
88
raw?: boolean;
99
};
1010

11+
function getMachineIdFromBinding(): string | undefined {
12+
try {
13+
return binding.getMachineId() || undefined;
14+
} catch {
15+
// If the binding fails, we can assume the machine ID is not available.
16+
return undefined;
17+
}
18+
}
19+
1120
/**
1221
* Get the machine ID for the current system
1322
* @returns The machine ID (UUID) or undefined if not available
1423
*/
1524
export function getMachineId({ raw = false }: GetMachineIdOptions = {}):
1625
| string
1726
| undefined {
18-
let machineId: string | undefined;
19-
20-
try {
21-
machineId = binding.getMachineId();
22-
} catch {
23-
// If the binding fails, we can assume the machine ID is not available.
24-
return undefined;
25-
}
26-
27-
if (!machineId) {
28-
return undefined;
29-
}
27+
const machineId: string | undefined = getMachineIdFromBinding();
3028

31-
if (raw === true) {
29+
if (!machineId || raw === true) {
3230
return machineId;
3331
}
3432

0 commit comments

Comments
 (0)