mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 07:43:35 +00:00
This reverts commit f51042f813.
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user