mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 18:23:31 +00:00
feat: duplicate error behavior in fake storage service
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
|||||||
} from "../src/platform/abstractions/storage.service";
|
} from "../src/platform/abstractions/storage.service";
|
||||||
import { StorageOptions } from "../src/platform/models/domain/storage-options";
|
import { StorageOptions } from "../src/platform/models/domain/storage-options";
|
||||||
|
|
||||||
|
const INTERNAL_KEY = "__internal__";
|
||||||
|
|
||||||
export class FakeStorageService implements AbstractStorageService, ObservableStorageService {
|
export class FakeStorageService implements AbstractStorageService, ObservableStorageService {
|
||||||
private store: Record<string, unknown>;
|
private store: Record<string, unknown>;
|
||||||
private updatesSubject = new Subject<StorageUpdate>();
|
private updatesSubject = new Subject<StorageUpdate>();
|
||||||
@@ -63,13 +65,28 @@ export class FakeStorageService implements AbstractStorageService, ObservableSto
|
|||||||
this.mock.has(key, options);
|
this.mock.has(key, options);
|
||||||
return Promise.resolve(this.store[key] != null);
|
return Promise.resolve(this.store[key] != null);
|
||||||
}
|
}
|
||||||
save<T>(key: string, obj: T, options?: StorageOptions): Promise<void> {
|
async save<T>(key: string, obj: T, options?: StorageOptions): Promise<void> {
|
||||||
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
// These exceptions are copied from https://github.com/sindresorhus/conf/blob/608adb0c46fb1680ddbd9833043478367a64c120/source/index.ts#L193-L203
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
// which is a library that is used by `ElectronStorageService`. We add them here to ensure that the behavior in our testing mirrors the real world.
|
||||||
this.mock.save(key, obj, options);
|
if (typeof key !== "string" && typeof key !== "object") {
|
||||||
|
throw new TypeError(
|
||||||
|
`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof key !== "object" && obj === undefined) {
|
||||||
|
throw new TypeError("Use `delete()` to clear values");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._containsReservedKey(key)) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Please don't use the ${INTERNAL_KEY} key, as it's used to manage this module internal operations.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.mock.save(key, obj, options);
|
||||||
this.store[key] = obj;
|
this.store[key] = obj;
|
||||||
this.updatesSubject.next({ key: key, updateType: "save" });
|
this.updatesSubject.next({ key: key, updateType: "save" });
|
||||||
return Promise.resolve();
|
|
||||||
}
|
}
|
||||||
remove(key: string, options?: StorageOptions): Promise<void> {
|
remove(key: string, options?: StorageOptions): Promise<void> {
|
||||||
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
||||||
@@ -79,4 +96,20 @@ export class FakeStorageService implements AbstractStorageService, ObservableSto
|
|||||||
this.updatesSubject.next({ key: key, updateType: "remove" });
|
this.updatesSubject.next({ key: key, updateType: "remove" });
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _containsReservedKey(key: string | Partial<unknown>): boolean {
|
||||||
|
if (typeof key === "object") {
|
||||||
|
const firsKey = Object.keys(key)[0];
|
||||||
|
|
||||||
|
if (firsKey === INTERNAL_KEY) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof key !== "string") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user