native-machine-id 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +192 -0
- package/README.md +85 -0
- package/binding.cc +248 -0
- package/binding.gyp +29 -0
- package/dist/.esm-wrapper.mjs +5 -0
- package/dist/bin/machine-id.d.ts +3 -0
- package/dist/bin/machine-id.d.ts.map +1 -0
- package/dist/bin/machine-id.js +7 -0
- package/dist/bin/machine-id.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2024 MongoDB Inc.
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
|
192
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# native-machine-id
|
|
2
|
+
|
|
3
|
+
> Native retrieval of a unique desktop machine ID without admin privileges or child processes. Faster and more reliable alternative to node-machine-id.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install native-machine-id
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or use it directly in the CLI
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npx native-machine-id
|
|
15
|
+
npx native-machine-id --raw
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### As a module
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import { getMachineId } from 'native-machine-id';
|
|
24
|
+
|
|
25
|
+
// Get the machine ID
|
|
26
|
+
const hashedId = await getMachineId();
|
|
27
|
+
console.log('SHA-256 Hashed Machine ID:', id);
|
|
28
|
+
const id = await getMachineId({ raw: true });
|
|
29
|
+
console.log('Raw Machine ID:', id);
|
|
30
|
+
|
|
31
|
+
// Or synchronously
|
|
32
|
+
import { getMachineIdSync } from 'native-machine-id';
|
|
33
|
+
const id = getMachineIdSync();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Supported Platforms
|
|
37
|
+
|
|
38
|
+
- **macOS**: Uses the `IOPlatformUUID` from the `IOKit` framework (Supported on macOS 12.0 and later).
|
|
39
|
+
- **Linux**: Uses the `/var/lib/dbus/machine-id` file to retrieve the machine ID. If this file does not exist, it falls back to `/etc/machine-id`.
|
|
40
|
+
- **Windows**: Uses the `MachineGuid` from the `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography` registry.
|
|
41
|
+
|
|
42
|
+
## Comparison with `node-machine-id`
|
|
43
|
+
|
|
44
|
+
This module provides similar functionality to [node-machine-id](https://www.npmjs.com/package/node-machine-id), but **using native access to system APIs without the need for child processes**, making it much faster and reliable.
|
|
45
|
+
|
|
46
|
+
Here's a table of performance comparisons between the two libraries, based on the average runtime from 1000 iterations of the `getMachineIdSync` and `machineIdSync` functions, from `scripts/benchmark.ts`:
|
|
47
|
+
|
|
48
|
+
| Test | node-machine-id | native-machine-id | Improvement |
|
|
49
|
+
| ----------- | --------------- | ----------------- | ----------- |
|
|
50
|
+
| **Mac** |
|
|
51
|
+
| Raw | 10.71ms | 0.0072ms | 1494x |
|
|
52
|
+
| Hashed | 12.42ms | 0.0176ms | 707x |
|
|
53
|
+
| **Linux** |
|
|
54
|
+
| Raw | 3.26ms | 0.0059ms | 557x |
|
|
55
|
+
| Hashed | 3.25ms | 0.0088ms | 368x |
|
|
56
|
+
| **Windows** |
|
|
57
|
+
| Raw | 45.36ms\* | 0.0122ms | 3704x |
|
|
58
|
+
| Hashed | 28.66ms\* | 0.0272ms | 1053x |
|
|
59
|
+
|
|
60
|
+
\* - Windows tests may be inaccurate due to potential caching.
|
|
61
|
+
|
|
62
|
+
### Migrating from `node-machine-id`
|
|
63
|
+
|
|
64
|
+
If you were previously using `node-machine-id`, you can use the following mapping to get a result with the following hashing transformation. This is not guaranteed always to 1:1 match the output of `node-machine-id` for all cases. For example on Linux, it falls back to `/etc/machine-id` if `/var/lib/dbus/machine-id` is not available.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { createHash } from 'crypto';
|
|
68
|
+
import { getMachineIdSync } from 'native-machine-id';
|
|
69
|
+
|
|
70
|
+
function machineIdSync(original: boolean): string | undefined {
|
|
71
|
+
const rawMachineId = getMachineIdSync({ raw: true }).toLowerCase();
|
|
72
|
+
|
|
73
|
+
return original
|
|
74
|
+
? rawMachineId
|
|
75
|
+
: createHash('sha256').update(rawMachineId).digest('hex');
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Credits
|
|
80
|
+
|
|
81
|
+
Influenced by the work from [denisbrodbeck/machineid](https://github.com/denisbrodbeck/machineid) and [automation-stack/node-machine-id](https://github.com/automation-stack/node-machine-id).
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
Apache-2.0
|
package/binding.cc
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <string>
|
|
3
|
+
|
|
4
|
+
#ifdef __APPLE__
|
|
5
|
+
#include <CoreFoundation/CoreFoundation.h>
|
|
6
|
+
#include <IOKit/IOKitLib.h>
|
|
7
|
+
#elif defined(__linux__)
|
|
8
|
+
#include <fstream>
|
|
9
|
+
#include <algorithm>
|
|
10
|
+
#elif defined(_WIN32)
|
|
11
|
+
#include <windows.h>
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
using namespace Napi;
|
|
15
|
+
|
|
16
|
+
namespace
|
|
17
|
+
{
|
|
18
|
+
|
|
19
|
+
#ifdef __APPLE__
|
|
20
|
+
// Get macOS machine ID using IOKit framework directly
|
|
21
|
+
std::string getMachineId() noexcept
|
|
22
|
+
{
|
|
23
|
+
std::string uuid;
|
|
24
|
+
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMainPortDefault, "IOService:/");
|
|
25
|
+
|
|
26
|
+
if (ioRegistryRoot == MACH_PORT_NULL)
|
|
27
|
+
{
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
CFStringRef uuidKey = CFSTR("IOPlatformUUID");
|
|
32
|
+
CFTypeRef uuidProperty = IORegistryEntryCreateCFProperty(ioRegistryRoot, uuidKey, kCFAllocatorDefault, 0);
|
|
33
|
+
|
|
34
|
+
if (uuidProperty)
|
|
35
|
+
{
|
|
36
|
+
char buffer[128];
|
|
37
|
+
if (!CFStringGetCString((CFStringRef)uuidProperty, buffer, sizeof(buffer), kCFStringEncodingUTF8))
|
|
38
|
+
{
|
|
39
|
+
CFRelease(uuidProperty);
|
|
40
|
+
IOObjectRelease(ioRegistryRoot);
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
uuid = buffer;
|
|
45
|
+
CFRelease(uuidProperty);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
IOObjectRelease(ioRegistryRoot);
|
|
49
|
+
return uuid;
|
|
50
|
+
}
|
|
51
|
+
#elif defined(__linux__)
|
|
52
|
+
// Linux machine ID paths
|
|
53
|
+
const char *DBUS_PATH = "/var/lib/dbus/machine-id";
|
|
54
|
+
const char *DBUS_PATH_ETC = "/etc/machine-id";
|
|
55
|
+
|
|
56
|
+
// Trim whitespace and newlines from a string
|
|
57
|
+
std::string trim(const std::string &str)
|
|
58
|
+
{
|
|
59
|
+
if (str.empty())
|
|
60
|
+
{
|
|
61
|
+
return str;
|
|
62
|
+
}
|
|
63
|
+
std::string result = str;
|
|
64
|
+
size_t from_right = result.find_last_not_of(" \n\r\t");
|
|
65
|
+
if (from_right != std::string::npos)
|
|
66
|
+
{
|
|
67
|
+
result.erase(from_right + 1);
|
|
68
|
+
}
|
|
69
|
+
result.erase(0, result.find_first_not_of(" \n\r\t"));
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Read file contents
|
|
74
|
+
std::string readFile(const char *path)
|
|
75
|
+
{
|
|
76
|
+
try
|
|
77
|
+
{
|
|
78
|
+
std::ifstream file(path);
|
|
79
|
+
std::string content;
|
|
80
|
+
|
|
81
|
+
if (file.is_open())
|
|
82
|
+
{
|
|
83
|
+
std::string line;
|
|
84
|
+
|
|
85
|
+
if (!file.fail() && std::getline(file, line))
|
|
86
|
+
{
|
|
87
|
+
content = line;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
file.close();
|
|
91
|
+
}
|
|
92
|
+
return content;
|
|
93
|
+
}
|
|
94
|
+
catch (const std::exception &)
|
|
95
|
+
{
|
|
96
|
+
return "";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Get Linux machine ID by reading from system files
|
|
101
|
+
std::string getMachineId() noexcept
|
|
102
|
+
{
|
|
103
|
+
std::string uuid = readFile(DBUS_PATH);
|
|
104
|
+
|
|
105
|
+
// Try fallback path if the first path fails
|
|
106
|
+
if (uuid.empty())
|
|
107
|
+
{
|
|
108
|
+
uuid = readFile(DBUS_PATH_ETC);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return trim(uuid);
|
|
112
|
+
}
|
|
113
|
+
#elif defined(_WIN32)
|
|
114
|
+
// Get Windows machine ID from registry
|
|
115
|
+
std::string getMachineId() noexcept
|
|
116
|
+
{
|
|
117
|
+
std::string uuid;
|
|
118
|
+
HKEY hKey;
|
|
119
|
+
LONG result = RegOpenKeyExA(
|
|
120
|
+
HKEY_LOCAL_MACHINE,
|
|
121
|
+
"SOFTWARE\\Microsoft\\Cryptography",
|
|
122
|
+
0,
|
|
123
|
+
KEY_QUERY_VALUE | KEY_WOW64_64KEY,
|
|
124
|
+
&hKey);
|
|
125
|
+
|
|
126
|
+
if (result != ERROR_SUCCESS)
|
|
127
|
+
{
|
|
128
|
+
return "";
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
char value[128] = {0};
|
|
132
|
+
DWORD valueSize = sizeof(value);
|
|
133
|
+
DWORD valueType;
|
|
134
|
+
|
|
135
|
+
result = RegQueryValueExA(
|
|
136
|
+
hKey,
|
|
137
|
+
"MachineGuid",
|
|
138
|
+
NULL,
|
|
139
|
+
&valueType,
|
|
140
|
+
reinterpret_cast<LPBYTE>(value),
|
|
141
|
+
&valueSize);
|
|
142
|
+
|
|
143
|
+
if (result == ERROR_SUCCESS && valueType == REG_SZ && valueSize > 0)
|
|
144
|
+
{
|
|
145
|
+
// Create string with explicit length based on returned valueSize
|
|
146
|
+
uuid = std::string(value, valueSize - (value[valueSize - 1] == '\0' ? 1 : 0));
|
|
147
|
+
}
|
|
148
|
+
else
|
|
149
|
+
{
|
|
150
|
+
RegCloseKey(hKey);
|
|
151
|
+
return "";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
RegCloseKey(hKey);
|
|
155
|
+
return uuid;
|
|
156
|
+
}
|
|
157
|
+
#endif
|
|
158
|
+
|
|
159
|
+
// Function to get the machine ID (synchronous version)
|
|
160
|
+
Value GetMachineIdSync(const CallbackInfo &args)
|
|
161
|
+
{
|
|
162
|
+
Env env = args.Env();
|
|
163
|
+
|
|
164
|
+
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
|
|
165
|
+
std::string id = getMachineId();
|
|
166
|
+
if (!id.empty())
|
|
167
|
+
{
|
|
168
|
+
return String::New(env, id);
|
|
169
|
+
}
|
|
170
|
+
#endif
|
|
171
|
+
|
|
172
|
+
// If we couldn't get a machine ID or platform not supported, return undefined.
|
|
173
|
+
return env.Undefined();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Async worker class for getting machine ID
|
|
177
|
+
class GetMachineIdWorker : public AsyncWorker
|
|
178
|
+
{
|
|
179
|
+
private:
|
|
180
|
+
std::string id;
|
|
181
|
+
|
|
182
|
+
public:
|
|
183
|
+
GetMachineIdWorker(Function &callback)
|
|
184
|
+
: AsyncWorker(callback) {}
|
|
185
|
+
|
|
186
|
+
// This runs on a worker thread
|
|
187
|
+
void Execute() override
|
|
188
|
+
{
|
|
189
|
+
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
|
|
190
|
+
id = getMachineId();
|
|
191
|
+
#endif
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// This runs on the main thread after Execute completes
|
|
195
|
+
void OnOK() override
|
|
196
|
+
{
|
|
197
|
+
Napi::Env env = Env();
|
|
198
|
+
Napi::HandleScope scope(env);
|
|
199
|
+
|
|
200
|
+
if (!id.empty())
|
|
201
|
+
{
|
|
202
|
+
Callback().Call({env.Null(), String::New(env, id)});
|
|
203
|
+
}
|
|
204
|
+
else
|
|
205
|
+
{
|
|
206
|
+
Callback().Call({env.Null(), env.Undefined()});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
void OnError(const Error &e) override
|
|
211
|
+
{
|
|
212
|
+
Napi::Env env = Env();
|
|
213
|
+
Napi::HandleScope scope(env);
|
|
214
|
+
Callback().Call({e.Value(), env.Undefined()});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Function to get the machine ID asynchronously
|
|
219
|
+
Value GetMachineIdAsync(const CallbackInfo &args)
|
|
220
|
+
{
|
|
221
|
+
Env env = args.Env();
|
|
222
|
+
|
|
223
|
+
// Check if a callback was provided
|
|
224
|
+
if (args.Length() < 1 || !args[0].IsFunction())
|
|
225
|
+
{
|
|
226
|
+
TypeError::New(env, "Callback function expected").ThrowAsJavaScriptException();
|
|
227
|
+
return env.Undefined();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Get the callback function
|
|
231
|
+
Function callback = args[0].As<Function>();
|
|
232
|
+
|
|
233
|
+
// Create and queue the async worker
|
|
234
|
+
GetMachineIdWorker *worker = new GetMachineIdWorker(callback);
|
|
235
|
+
worker->Queue();
|
|
236
|
+
|
|
237
|
+
return env.Undefined();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static Object InitModule(Env env, Object exports)
|
|
242
|
+
{
|
|
243
|
+
exports["getMachineIdSync"] = Function::New(env, GetMachineIdSync);
|
|
244
|
+
exports["getMachineIdAsync"] = Function::New(env, GetMachineIdAsync);
|
|
245
|
+
return exports;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
NODE_API_MODULE(machine_id, InitModule)
|
package/binding.gyp
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
'targets': [{
|
|
3
|
+
'target_name': 'machine_id',
|
|
4
|
+
'sources': [ 'binding.cc' ],
|
|
5
|
+
'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
|
|
6
|
+
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
|
|
7
|
+
'defines': ['NAPI_VERSION=6'],
|
|
8
|
+
'cflags!': [ '-fno-exceptions' ],
|
|
9
|
+
'cflags_cc!': [ '-fno-exceptions' ],
|
|
10
|
+
'xcode_settings': {
|
|
11
|
+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
|
12
|
+
'CLANG_CXX_LIBRARY': 'libc++',
|
|
13
|
+
'MACOSX_DEPLOYMENT_TARGET': '12.0',
|
|
14
|
+
},
|
|
15
|
+
'msvs_settings': {
|
|
16
|
+
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
|
|
17
|
+
},
|
|
18
|
+
'conditions': [
|
|
19
|
+
['OS=="mac"', {
|
|
20
|
+
'link_settings': {
|
|
21
|
+
'libraries': [
|
|
22
|
+
'-framework CoreFoundation',
|
|
23
|
+
'-framework IOKit'
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}]
|
|
27
|
+
]
|
|
28
|
+
}]
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"machine-id.d.ts","sourceRoot":"","sources":["../../src/bin/machine-id.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const id = (0, __1.getMachineIdSync)({ raw: process.argv.includes('--raw') }) || '';
|
|
6
|
+
console.log(id);
|
|
7
|
+
//# sourceMappingURL=machine-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"machine-id.js","sourceRoot":"","sources":["../../src/bin/machine-id.ts"],"names":[],"mappings":";;;AACA,0BAAsC;AAEtC,MAAM,EAAE,GAAG,IAAA,oBAAgB,EAAC,EAAE,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;AAG3E,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type GetMachineIdOptions = {
|
|
2
|
+
raw?: boolean;
|
|
3
|
+
};
|
|
4
|
+
export declare function getMachineId({ raw, }?: GetMachineIdOptions): Promise<string | undefined>;
|
|
5
|
+
export declare function getMachineIdSync({ raw }?: GetMachineIdOptions): string | undefined;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA2BA,MAAM,MAAM,mBAAmB,GAAG;IAEhC,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,CAAC;AAMF,wBAAsB,YAAY,CAAC,EACjC,GAAW,GACZ,GAAE,mBAAwB,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAQxD;AAMD,wBAAgB,gBAAgB,CAAC,EAAE,GAAW,EAAE,GAAE,mBAAwB,GACtE,MAAM,GACN,SAAS,CAQZ"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMachineIdSync = exports.getMachineId = void 0;
|
|
7
|
+
const bindings_1 = __importDefault(require("bindings"));
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const util_1 = require("util");
|
|
10
|
+
const binding = (0, bindings_1.default)('machine_id');
|
|
11
|
+
function getMachineIdFromBindingSync() {
|
|
12
|
+
try {
|
|
13
|
+
return binding.getMachineIdSync() || undefined;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function getMachineIdFromBindingAsync() {
|
|
20
|
+
try {
|
|
21
|
+
const deviceId = (await (0, util_1.promisify)(binding.getMachineIdAsync)()) || undefined;
|
|
22
|
+
return deviceId;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function getMachineId({ raw = false, } = {}) {
|
|
29
|
+
const machineId = await getMachineIdFromBindingAsync();
|
|
30
|
+
if (!machineId || raw === true) {
|
|
31
|
+
return machineId;
|
|
32
|
+
}
|
|
33
|
+
return (0, crypto_1.createHash)('sha256').update(machineId).digest('hex');
|
|
34
|
+
}
|
|
35
|
+
exports.getMachineId = getMachineId;
|
|
36
|
+
function getMachineIdSync({ raw = false } = {}) {
|
|
37
|
+
const machineId = getMachineIdFromBindingSync();
|
|
38
|
+
if (!machineId || raw === true) {
|
|
39
|
+
return machineId;
|
|
40
|
+
}
|
|
41
|
+
return (0, crypto_1.createHash)('sha256').update(machineId).digest('hex');
|
|
42
|
+
}
|
|
43
|
+
exports.getMachineIdSync = getMachineIdSync;
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,wDAAgC;AAChC,mCAAoC;AACpC,+BAAiC;AAEjC,MAAM,OAAO,GAAG,IAAA,kBAAQ,EAAC,YAAY,CAAC,CAAC;AAEvC,SAAS,2BAA2B;IAClC,IAAI;QACF,OAAO,OAAO,CAAC,gBAAgB,EAAE,IAAI,SAAS,CAAC;KAChD;IAAC,MAAM;QAEN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC;AAED,KAAK,UAAU,4BAA4B;IACzC,IAAI;QACF,MAAM,QAAQ,GACZ,CAAC,MAAM,IAAA,gBAAS,EAAC,OAAO,CAAC,iBAAiB,CAAC,EAAE,CAAC,IAAI,SAAS,CAAC;QAE9D,OAAO,QAAQ,CAAC;KACjB;IAAC,MAAM;QAEN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC;AAWM,KAAK,UAAU,YAAY,CAAC,EACjC,GAAG,GAAG,KAAK,MACY,EAAE;IACzB,MAAM,SAAS,GAAG,MAAM,4BAA4B,EAAE,CAAC;IAEvD,IAAI,CAAC,SAAS,IAAI,GAAG,KAAK,IAAI,EAAE;QAC9B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC9D,CAAC;AAVD,oCAUC;AAMD,SAAgB,gBAAgB,CAAC,EAAE,GAAG,GAAG,KAAK,KAA0B,EAAE;IAGxE,MAAM,SAAS,GAAG,2BAA2B,EAAE,CAAC;IAEhD,IAAI,CAAC,SAAS,IAAI,GAAG,KAAK,IAAI,EAAE;QAC9B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC9D,CAAC;AAVD,4CAUC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "native-machine-id",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Native retrieval of a unique desktop machine ID without admin privileges or child processes. Faster and more reliable alternative to node-machine-id.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"compile": "tsc -p tsconfig.json && node-gyp rebuild && gen-esm-wrapper . ./dist/.esm-wrapper.mjs",
|
|
9
|
+
"bootstrap": "npm run compile",
|
|
10
|
+
"pretest": "npm run compile",
|
|
11
|
+
"test": "mocha",
|
|
12
|
+
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
|
|
13
|
+
"test-ci": "npm run test-cov",
|
|
14
|
+
"lint": "eslint . && prettier --check .",
|
|
15
|
+
"check": "npm run lint && npm run test",
|
|
16
|
+
"prepublishOnly": "npm run compile",
|
|
17
|
+
"benchmark": "ts-node scripts/benchmark.ts"
|
|
18
|
+
},
|
|
19
|
+
"author": "Compass Team <compass@mongodb.com>",
|
|
20
|
+
"gypfile": true,
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"bindings": "^1.5.0",
|
|
23
|
+
"node-addon-api": "^8.0.0"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"exports": {
|
|
27
|
+
"require": "./dist/index.js",
|
|
28
|
+
"import": "./dist/.esm-wrapper.mjs"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/mongodb-js/devtools-shared",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/mongodb-js/devtools-shared.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": "https://jira.mongodb.org/projects/COMPASS/issues",
|
|
36
|
+
"bin": {
|
|
37
|
+
"native-machine-id": "dist/bin/machine-id.js"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"binding.cc",
|
|
41
|
+
"binding.gyp",
|
|
42
|
+
"dist",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@mongodb-js/eslint-config-devtools": "0.9.11",
|
|
47
|
+
"@mongodb-js/mocha-config-devtools": "^1.0.5",
|
|
48
|
+
"@mongodb-js/prettier-config-devtools": "^1.0.2",
|
|
49
|
+
"@mongodb-js/tsconfig-devtools": "^1.0.3",
|
|
50
|
+
"@types/chai": "^4.2.21",
|
|
51
|
+
"@types/mocha": "^9.1.1",
|
|
52
|
+
"@types/sinon-chai": "^3.2.5",
|
|
53
|
+
"@types/node": "^17.0.35",
|
|
54
|
+
"eslint": "^7.25.0",
|
|
55
|
+
"gen-esm-wrapper": "^1.1.1",
|
|
56
|
+
"mocha": "^8.4.0",
|
|
57
|
+
"chai": "^4.5.0",
|
|
58
|
+
"node-machine-id": "^1.1.12",
|
|
59
|
+
"typescript": "^5.0.4",
|
|
60
|
+
"ts-node": "^10.9.2"
|
|
61
|
+
}
|
|
62
|
+
}
|