mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 17:23:37 +00:00
[PM-8210] Discourage Active User in CryptoService (#9364)
* Add Helper For Preparing a Record For Use in `forkJoin` * Update & Test CryptoService Changes * Delete Unused Code * Update DeviceTrustService * Update CipherService * Make `userPublicKey$` Public * Rename convertValues File * Update libs/common/src/platform/abstractions/crypto.service.ts Co-authored-by: Andreas Coroiu <acoroiu@bitwarden.com> * Add `convertValues` Tests * Add Doc Comments * Convert to `function`'s Co-authored-by: Andreas Coroiu <acoroiu@bitwarden.com> * Fix Test Typos * Add param doc * Update Test Name * Add `@throws` Docs --------- Co-authored-by: Andreas Coroiu <acoroiu@bitwarden.com>
This commit is contained in:
74
libs/common/src/platform/misc/convert-values.spec.ts
Normal file
74
libs/common/src/platform/misc/convert-values.spec.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { forkJoin, lastValueFrom, of, switchMap } from "rxjs";
|
||||
|
||||
import { convertValues } from "./convert-values";
|
||||
|
||||
describe("convertValues", () => {
|
||||
it("returns null if given null", async () => {
|
||||
const output = await lastValueFrom(
|
||||
of<Record<string, number>>(null).pipe(convertValues((k, v) => of(v + 1))),
|
||||
);
|
||||
|
||||
expect(output).toEqual(null);
|
||||
});
|
||||
|
||||
it("returns empty record if given empty record", async () => {
|
||||
const output = await lastValueFrom(
|
||||
of<Record<string, number>>({}).pipe(convertValues((k, v) => of(v + 1))),
|
||||
);
|
||||
|
||||
expect(output).toEqual({});
|
||||
});
|
||||
|
||||
const cases: { it: string; input: Record<string, number>; output: Record<string, number> }[] = [
|
||||
{
|
||||
it: "converts single entry to observable",
|
||||
input: {
|
||||
one: 1,
|
||||
},
|
||||
output: {
|
||||
one: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
it: "converts multiple entries to observable",
|
||||
input: {
|
||||
one: 1,
|
||||
two: 2,
|
||||
three: 3,
|
||||
},
|
||||
output: {
|
||||
one: 2,
|
||||
two: 3,
|
||||
three: 4,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
it.each(cases)("$it", async ({ input, output: expectedOutput }) => {
|
||||
const output = await lastValueFrom(
|
||||
of(input).pipe(
|
||||
convertValues((key, value) => of(value + 1)),
|
||||
switchMap((values) => forkJoin(values)),
|
||||
),
|
||||
);
|
||||
|
||||
expect(output).toEqual(expectedOutput);
|
||||
});
|
||||
|
||||
it("converts async functions to observable", async () => {
|
||||
const output = await lastValueFrom(
|
||||
of({
|
||||
one: 1,
|
||||
two: 2,
|
||||
}).pipe(
|
||||
convertValues(async (key, value) => await Promise.resolve(value + 1)),
|
||||
switchMap((values) => forkJoin(values)),
|
||||
),
|
||||
);
|
||||
|
||||
expect(output).toEqual({
|
||||
one: 2,
|
||||
two: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
23
libs/common/src/platform/misc/convert-values.ts
Normal file
23
libs/common/src/platform/misc/convert-values.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ObservableInput, OperatorFunction, map } from "rxjs";
|
||||
|
||||
/**
|
||||
* Converts a record of keys and values into a record preserving the original key and converting each value into an {@link ObservableInput}.
|
||||
* @param project A function to project a given key and value pair into an {@link ObservableInput}
|
||||
*/
|
||||
export function convertValues<TKey extends PropertyKey, TInput, TOutput>(
|
||||
project: (key: TKey, value: TInput) => ObservableInput<TOutput>,
|
||||
): OperatorFunction<Record<TKey, TInput>, Record<TKey, ObservableInput<TOutput>>> {
|
||||
return map((inputRecord) => {
|
||||
if (inputRecord == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Can't use TKey in here, have to use `PropertyKey`
|
||||
const result: Record<PropertyKey, ObservableInput<TOutput>> = {};
|
||||
for (const [key, value] of Object.entries(inputRecord) as [TKey, TInput][]) {
|
||||
result[key] = project(key, value);
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user