1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

Revert "[PM-7810] Handle Multithread Decryption Through Offscreen API (#8978)" (#9221)

This reverts commit f51042f813.
This commit is contained in:
Matt Gibson
2024-05-16 15:48:03 -04:00
committed by GitHub
parent bed8239c92
commit 4afe5de87b
7 changed files with 21 additions and 312 deletions

View File

@@ -19,36 +19,17 @@ export class MultithreadEncryptServiceImplementation extends EncryptServiceImple
private clear$ = new Subject<void>();
/**
* Decrypts items using a web worker if the environment supports it.
* Will fall back to the main thread if the window object is not available.
* Sends items to a web worker to decrypt them.
* This utilises multithreading to decrypt items faster without interrupting other operations (e.g. updating UI).
*/
async decryptItems<T extends InitializerMetadata>(
items: Decryptable<T>[],
key: SymmetricCryptoKey,
): Promise<T[]> {
if (typeof window === "undefined") {
return super.decryptItems(items, key);
}
if (items == null || items.length < 1) {
return [];
}
const decryptedItems = await this.getDecryptedItemsFromWorker(items, key);
const parsedItems = JSON.parse(decryptedItems);
return this.initializeItems(parsedItems);
}
/**
* Sends items to a web worker to decrypt them. This utilizes multithreading to decrypt items
* faster without interrupting other operations (e.g. updating UI). This method returns values
* prior to deserialization to support forwarding results to another party
*/
async getDecryptedItemsFromWorker<T extends InitializerMetadata>(
items: Decryptable<T>[],
key: SymmetricCryptoKey,
): Promise<string> {
this.logService.info("Starting decryption using multithreading");
this.worker ??= new Worker(
@@ -72,20 +53,19 @@ export class MultithreadEncryptServiceImplementation extends EncryptServiceImple
return await firstValueFrom(
fromEvent(this.worker, "message").pipe(
filter((response: MessageEvent) => response.data?.id === request.id),
map((response) => response.data.items),
map((response) => JSON.parse(response.data.items)),
map((items) =>
items.map((jsonItem: Jsonify<T>) => {
const initializer = getClassInitializer<T>(jsonItem.initializerKey);
return initializer(jsonItem);
}),
),
takeUntil(this.clear$),
defaultIfEmpty("[]"),
defaultIfEmpty([]),
),
);
}
protected initializeItems<T extends InitializerMetadata>(items: Jsonify<T>[]): T[] {
return items.map((jsonItem: Jsonify<T>) => {
const initializer = getClassInitializer<T>(jsonItem.initializerKey);
return initializer(jsonItem);
});
}
private clear() {
this.clear$.next();
this.worker?.terminate();