Skip to content

Commit 35f703a

Browse files
committed
more worker init error handling
1 parent 40a2d71 commit 35f703a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/idb-cache/src/encryptionWorkerFn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface PostMessageCapable {
1010
export function encryptionWorkerFunction() {
1111
if (typeof crypto === "undefined" || !crypto?.subtle) {
1212
const errorMessage =
13-
"Web Crypto API is not supported in a Web Worker environment.";
13+
"Web Crypto API is not supported in this Web Worker environment.";
1414
console.error(`Worker: ${errorMessage}`);
1515

1616
if ("SharedWorkerGlobalScope" in self && "onconnect" in self) {

packages/idb-cache/src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class IDBCache implements IDBCacheInterface {
116116
ExtendedPendingRequest<EncryptedChunk | string>
117117
>;
118118
private workerReadyPromise: Promise<void> | null = null;
119+
private workerInitializationFailed = false;
119120
private maxAge: number;
120121
private cleanupIntervalId: number | undefined;
121122

@@ -200,11 +201,18 @@ export class IDBCache implements IDBCacheInterface {
200201
cacheKey?: string,
201202
cacheBuster?: string
202203
): Promise<void> {
204+
if (this.workerInitializationFailed) {
205+
throw new WorkerInitializationError(
206+
"Worker initialization previously failed"
207+
);
208+
}
209+
203210
if (this.workerReadyPromise) {
204211
return this.workerReadyPromise;
205212
}
206213
this.workerReadyPromise = new Promise<void>((resolve, reject) => {
207214
const rejectAll = (errorMessage: string) => {
215+
this.workerInitializationFailed = true;
208216
reject(new WorkerInitializationError(errorMessage));
209217
rejectAllPendingRequests(this.pendingRequests, errorMessage);
210218
};
@@ -401,6 +409,12 @@ export class IDBCache implements IDBCacheInterface {
401409
}
402410

403411
private async ensureWorkerInitialized() {
412+
if (this.workerInitializationFailed) {
413+
throw new WorkerInitializationError(
414+
"Worker initialization previously failed"
415+
);
416+
}
417+
404418
if (!this.workerReadyPromise) {
405419
throw new WorkerInitializationError("Worker is not initialized.");
406420
}
@@ -423,6 +437,10 @@ export class IDBCache implements IDBCacheInterface {
423437
* @throws {WorkerInitializationError} If the worker is not initialized properly.
424438
*/
425439
public async getItem(itemKey: string): Promise<string | null> {
440+
if (this.workerInitializationFailed) {
441+
return Promise.resolve(null);
442+
}
443+
426444
try {
427445
const startTime = Date.now();
428446

@@ -562,6 +580,10 @@ export class IDBCache implements IDBCacheInterface {
562580
* @throws {EncryptionError} If encryption fails.
563581
*/
564582
public async setItem(itemKey: string, value: string): Promise<void> {
583+
if (this.workerInitializationFailed) {
584+
return Promise.resolve();
585+
}
586+
565587
try {
566588
const startTime = Date.now();
567589

@@ -716,6 +738,10 @@ export class IDBCache implements IDBCacheInterface {
716738
* @throws {DatabaseError} If there is an issue accessing the database.
717739
*/
718740
public async removeItem(itemKey: string): Promise<void> {
741+
if (this.workerInitializationFailed) {
742+
return Promise.resolve();
743+
}
744+
719745
try {
720746
const db = await this.dbReadyPromise;
721747
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
@@ -754,6 +780,12 @@ export class IDBCache implements IDBCacheInterface {
754780
* @throws {DatabaseError} If there is an issue accessing the database.
755781
*/
756782
public async count(): Promise<number> {
783+
if (this.workerInitializationFailed) {
784+
throw new WorkerInitializationError(
785+
"Worker initialization previously failed"
786+
);
787+
}
788+
757789
try {
758790
const db = await this.dbReadyPromise;
759791
const transaction = db.transaction(this.storeName, "readonly");
@@ -782,6 +814,10 @@ export class IDBCache implements IDBCacheInterface {
782814
* @throws {DatabaseError} If there is an issue accessing the database.
783815
*/
784816
public async clear(): Promise<void> {
817+
if (this.workerInitializationFailed) {
818+
return;
819+
}
820+
785821
try {
786822
const db = await this.dbReadyPromise;
787823
const transaction = db.transaction(this.storeName, "readwrite");

0 commit comments

Comments
 (0)