Skip to content

Commit 79d0768

Browse files
authored
fix: strip unknown preferences when loading COMPASS-7026 (#4654)
strip unknown preferences when loading
1 parent 9000511 commit 79d0768

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/compass-preferences-model/src/storage.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { mkdtempSync, rmdirSync } from 'fs';
22
import path from 'path';
33
import os from 'os';
4-
import { UserStorage, type User } from './storage';
4+
import { UserStorage, StoragePreferences, type User } from './storage';
55
import { expect } from 'chai';
6+
import type { UserPreferences } from './preferences';
67

78
describe('storage', function () {
89
let tmpDir: string;
@@ -41,4 +42,28 @@ describe('storage', function () {
4142
});
4243
});
4344
});
45+
46+
describe('StoragePreferences', function () {
47+
before(function () {
48+
tmpDir = mkdtempSync(path.join(os.tmpdir()));
49+
});
50+
51+
after(function () {
52+
rmdirSync(tmpDir, { recursive: true });
53+
});
54+
55+
it('will strip unknown prefs', async function () {
56+
const prefsStorage = new StoragePreferences(
57+
{ showedNetworkOptIn: true, foo: true } as unknown as UserPreferences,
58+
tmpDir
59+
);
60+
await prefsStorage.setup();
61+
62+
// roundabout way to make it load because setup doesn't fill prefsStorage.preferences if it writes the file
63+
await prefsStorage.updatePreferences({});
64+
65+
const prefs = prefsStorage.getPreferences();
66+
expect(prefs).to.deep.equal({ showedNetworkOptIn: true });
67+
});
68+
});
4469
});

packages/compass-preferences-model/src/storage.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { UUID } from 'bson';
12
import { promises as fs } from 'fs';
3+
import { pick } from 'lodash';
24
import { join } from 'path';
35
import type { UserPreferences } from './preferences';
4-
import { UUID } from 'bson';
6+
import { allPreferencesProps } from './preferences';
57

68
export abstract class BasePreferencesStorage {
79
abstract setup(): Promise<void>;
@@ -71,7 +73,19 @@ export class StoragePreferences extends BasePreferencesStorage {
7173
}
7274

7375
private async readPreferences(): Promise<UserPreferences> {
74-
return JSON.parse(await fs.readFile(this.getFilePath(), 'utf8'));
76+
const preferences = JSON.parse(
77+
await fs.readFile(this.getFilePath(), 'utf8')
78+
);
79+
80+
// Exclude old and renamed preferences so we don't try and save those back.
81+
// (This is still technically a lie because we could only have a subset of
82+
// UserPreferences. ie. imagine a JSON file that has an empty object. Or
83+
// that was saved with the previous version of compass where we had fewer
84+
// settings.)
85+
return pick(
86+
preferences,
87+
Object.keys(allPreferencesProps)
88+
) as UserPreferences;
7589
}
7690

7791
getPreferences() {

0 commit comments

Comments
 (0)