[MM-44004] Optionally turn off UTF-8 encoding for winreg if the system doesn't support it (#2128)

* [MM-44004] Optionally turn off UTF-8 encoding for winreg if the system doesn't support it

* Fix tests

* REVERT ME: Enable msi installer

* REVERT ME: Wait for msi

* Toggle between winreg and winreg-utf8 if the latter doesn't work

* Revert MSI stuff

* Added logging for the catch at the end to see what error is occurring there

* Catch the error on registry creation as well

* Update logging a bit further

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Devin Binnie 2023-01-20 12:11:37 -05:00 committed by GitHub
parent 94120a0315
commit d312bfca24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View file

@ -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'));
});
});
});

View file

@ -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<string | WindowsRegistry.RegistryItem[] | undefined>((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});
}
}