forked from vweevers/win-detect-browsers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.js
More file actions
65 lines (50 loc) · 1.51 KB
/
registry.js
File metadata and controls
65 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Note: registry-js internally has a `process.platform === 'win32'` check so it
// won't load its native addon on other platforms (good)
import * as registry from 'registry-js'
const HKEY = registry.HKEY
const SHORT_HIVES = new Map([
['HKLM', HKEY.HKEY_LOCAL_MACHINE],
['HKCU', HKEY.HKEY_CURRENT_USER],
['HKCR', HKEY.HKEY_CLASSES_ROOT],
['HKCC', HKEY.HKEY_CURRENT_CONFIG],
['HKU', HKEY.HKEY_USERS]
])
// TODO: refactor
const api = {}
api.value = function (hive, key, valueName, callback) {
if (typeof valueName === 'function') {
// Get default value
callback = valueName
valueName = null
}
let shortHive
if (SHORT_HIVES.has(hive)) {
shortHive = hive
hive = SHORT_HIVES.get(hive)
}
key = Array.isArray(key) ? key.join('\\') : key
const result = registry.enumerateValuesSafe(hive, key)
const expectedName = valueName || ''
const fqk = [shortHive || hive, key].join('\\')
for (const item of result) {
if (!item) continue
if (item.name === expectedName && item.type === 'REG_SZ') {
return process.nextTick(callback, null, item.data, fqk)
}
}
const err = new Error('not found')
err.notFound = true
process.nextTick(callback, err)
}
api.values = function (hive, key, callback) {
if (SHORT_HIVES.has(hive)) {
hive = SHORT_HIVES.get(hive)
}
const result = registry.enumerateValuesSafe(hive, key)
const obj = {}
for (const item of result) {
if (item) obj[item.name] = item.data
}
process.nextTick(callback, null, obj)
}
export default api