mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import { defaultIfEmpty, filter, firstValueFrom, fromEvent, map, Subject, takeUntil } from "rxjs";
|
|
import { Jsonify } from "type-fest";
|
|
|
|
import { Utils } from "../../../platform/misc/utils";
|
|
import { Decryptable } from "../../interfaces/decryptable.interface";
|
|
import { InitializerMetadata } from "../../interfaces/initializer-metadata.interface";
|
|
import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";
|
|
|
|
import { EncryptServiceImplementation } from "./encrypt.service.implementation";
|
|
import { getClassInitializer } from "./get-class-initializer";
|
|
|
|
// TTL (time to live) is not strictly required but avoids tying up memory resources if inactive
|
|
const workerTTL = 3 * 60000; // 3 minutes
|
|
|
|
/**
|
|
* @deprecated Replaced by BulkEncryptionService (PM-4154)
|
|
*/
|
|
export class MultithreadEncryptServiceImplementation extends EncryptServiceImplementation {
|
|
private worker: Worker;
|
|
private timeout: any;
|
|
|
|
private clear$ = new Subject<void>();
|
|
|
|
/**
|
|
* 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 (items == null || items.length < 1) {
|
|
return [];
|
|
}
|
|
|
|
this.logService.info("Starting decryption using multithreading");
|
|
|
|
this.worker ??= new Worker(
|
|
new URL(
|
|
/* webpackChunkName: 'encrypt-worker' */
|
|
"@bitwarden/common/platform/services/cryptography/encrypt.worker.ts",
|
|
import.meta.url,
|
|
),
|
|
);
|
|
|
|
this.restartTimeout();
|
|
|
|
const request = {
|
|
id: Utils.newGuid(),
|
|
items: items,
|
|
key: key,
|
|
};
|
|
|
|
this.worker.postMessage(JSON.stringify(request));
|
|
|
|
return await firstValueFrom(
|
|
fromEvent(this.worker, "message").pipe(
|
|
filter((response: MessageEvent) => response.data?.id === request.id),
|
|
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([]),
|
|
),
|
|
);
|
|
}
|
|
|
|
private clear() {
|
|
this.clear$.next();
|
|
this.worker?.terminate();
|
|
this.worker = null;
|
|
this.clearTimeout();
|
|
}
|
|
|
|
private restartTimeout() {
|
|
this.clearTimeout();
|
|
this.timeout = setTimeout(() => this.clear(), workerTTL);
|
|
}
|
|
|
|
private clearTimeout() {
|
|
if (this.timeout != null) {
|
|
clearTimeout(this.timeout);
|
|
}
|
|
}
|
|
}
|