Skip to content

Commit 8485965

Browse files
authored
fix(native-machine-id): replace bindings dependency with explicit paths (#534)
- Use explicit paths for requiring as that works better with bundlers - No need to use `bindings` as the alternative is simpler Signed-off-by: Sebastian Malton <[email protected]> Co-authored-by: Anna Henningsen <[email protected]> Fixes: #533
1 parent d643a66 commit 8485965

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

package-lock.json

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/native-machine-id/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"author": "Compass Team <[email protected]>",
2121
"gypfile": true,
2222
"dependencies": {
23-
"bindings": "^1.5.0",
2423
"node-addon-api": "^8.0.0"
2524
},
2625
"license": "Apache-2.0",
@@ -53,15 +52,15 @@
5352
"@mongodb-js/tsconfig-devtools": "^1.0.3",
5453
"@types/chai": "^4.2.21",
5554
"@types/mocha": "^9.1.1",
56-
"@types/sinon-chai": "^3.2.5",
5755
"@types/node": "^17.0.35",
56+
"@types/sinon-chai": "^3.2.5",
57+
"chai": "^4.5.0",
5858
"eslint": "^7.25.0",
5959
"gen-esm-wrapper": "^1.1.1",
6060
"mocha": "^8.4.0",
61-
"chai": "^4.5.0",
6261
"node-machine-id": "^1.1.12",
63-
"typescript": "^5.0.4",
64-
"ts-node": "^10.9.2"
62+
"ts-node": "^10.9.2",
63+
"typescript": "^5.0.4"
6564
},
6665
"keywords": [
6766
"machine id",

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import bindings from 'bindings';
21
import { createHash } from 'crypto';
32
import { promisify } from 'util';
43

5-
const binding = bindings('native_machine_id');
4+
type NativeMachineIdModule = {
5+
getMachineIdSync: () => string;
6+
getMachineIdAsync: (cb: (err: unknown, id: string) => void) => void;
7+
};
8+
9+
const binding = (() => {
10+
try {
11+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12+
return require('../build/Release/native_machine_id.node') as NativeMachineIdModule;
13+
} catch (outerError) {
14+
try {
15+
// eslint-disable-next-line @typescript-eslint/no-var-requires
16+
return require('../build/Debug/native_machine_id.node') as NativeMachineIdModule;
17+
} catch {
18+
throw outerError;
19+
}
20+
}
21+
})();
622

723
function getMachineIdFromBindingSync(): string | undefined {
824
try {

0 commit comments

Comments
 (0)