diff --git a/src/common/config/RegistryConfig.test.js b/src/common/config/RegistryConfig.test.js index 691bad02..3f162e73 100644 --- a/src/common/config/RegistryConfig.test.js +++ b/src/common/config/RegistryConfig.test.js @@ -3,7 +3,7 @@ import RegistryConfig from 'common/config/RegistryConfig'; -jest.mock('winreg-utf8', () => { +jest.mock('winreg', () => { return jest.fn().mockImplementation(({hive, key}) => { return { values: (fn) => { @@ -58,7 +58,7 @@ describe('common/config/RegistryConfig', () => { const registryConfig = new RegistryConfig(); const originalFn = registryConfig.getRegistryEntryValues; - registryConfig.getRegistryEntryValues = (hive, key, name) => originalFn('mattermost-hive', key, name); + registryConfig.getRegistryEntryValues = (hive, key, name) => originalFn.apply(registryConfig, ['mattermost-hive', key, name, false]); await registryConfig.init(); Object.defineProperty(process, 'platform', { @@ -77,7 +77,7 @@ describe('common/config/RegistryConfig', () => { const registryConfig = new RegistryConfig(); it('should return correct values', () => { - expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key')).resolves.toStrictEqual([ + expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key', null, false)).resolves.toStrictEqual([ { name: 'correct-key-name-1', value: 'correct-key-value-1', @@ -90,19 +90,19 @@ describe('common/config/RegistryConfig', () => { }); it('should return correct value by name', () => { - expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key', 'correct-key-name-1')).resolves.toBe('correct-key-value-1'); + expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key', 'correct-key-name-1', false)).resolves.toBe('correct-key-value-1'); }); it('should return undefined with wrong name', () => { - expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key', 'wrong-key-name-1')).resolves.toBe(undefined); + expect(registryConfig.getRegistryEntryValues('correct-hive', 'correct-key', 'wrong-key-name-1', false)).resolves.toBe(undefined); }); it('should return undefined with bad hive', () => { - expect(registryConfig.getRegistryEntryValues('bad-hive', 'correct-key')).resolves.toBe(undefined); + expect(registryConfig.getRegistryEntryValues('bad-hive', 'correct-key', null, false)).resolves.toBe(undefined); }); it('should call reject when an error occurs', () => { - expect(registryConfig.getRegistryEntryValues('really-bad-hive', 'correct-key')).rejects.toThrow(new Error('This is an error')); + expect(registryConfig.getRegistryEntryValues('really-bad-hive', 'correct-key', null, false)).rejects.toThrow(new Error('This is an error')); }); }); }); diff --git a/src/common/config/RegistryConfig.ts b/src/common/config/RegistryConfig.ts index d4ca23d1..eb18c40a 100644 --- a/src/common/config/RegistryConfig.ts +++ b/src/common/config/RegistryConfig.ts @@ -4,7 +4,8 @@ import {EventEmitter} from 'events'; import log from 'electron-log'; -import WindowsRegistry from 'winreg-utf8'; +import WindowsRegistry from 'winreg'; +import WindowsRegistryUTF8 from 'winreg-utf8'; import {RegistryConfig as RegistryConfigType, Team} from 'types/config'; @@ -126,12 +127,18 @@ export default class RegistryConfig extends EventEmitter { * @param {WindowsRegistry} regKey A configured instance of the WindowsRegistry class * @param {string} name Name of the specific entry to retrieve (optional) */ - getRegistryEntryValues(hive: string, key: string, name?: string) { - const registry = new WindowsRegistry({hive, key, utf8: true}); + getRegistryEntryValues(hive: string, key: string, name?: string, utf8 = true) { return new Promise((resolve, reject) => { try { + const registry = this.createRegistry(hive, key, utf8); registry.values((error: Error, results: WindowsRegistry.RegistryItem[]) => { - if (error || !results || results.length === 0) { + if (error) { + this.handleRegistryEntryError(error, hive, key, name, utf8).then((result) => { + resolve(result); + }); + return; + } + if (!results || results.length === 0) { resolve(undefined); return; } @@ -143,9 +150,31 @@ export default class RegistryConfig extends EventEmitter { } }); } catch (e) { - log.error(`There was an error accessing the registry for ${key}`); - reject(e); + this.handleRegistryEntryError(e as Error, hive, key, name, utf8).then((result) => { + if (result) { + resolve(result); + } + reject(e); + }); } }); } + + handleRegistryEntryError(e: Error, hive: string, key: string, name?: string, utf8?: boolean) { + log.verbose('There was an error accessing the registry for', {hive, key, name, utf8}, e); + if (utf8) { + log.verbose('Trying without UTF-8...', {hive, key, name}); + return this.getRegistryEntryValues(hive, key, name, false); + } + + return Promise.resolve(undefined); + } + + createRegistry(hive: string, key: string, utf8 = true) { + if (utf8) { + return new WindowsRegistryUTF8({hive, key, utf8}); + } + + return new WindowsRegistry({hive, key}); + } }