Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions sdk/js/src/platform/state.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,59 @@ for (const outcome of ["commit", "abort", "abort without a reason"] as const) {
});
}

// forget() deletes the credential with remove(): a delete whose transaction
// aborted leaves the credential stored, so remove() must not report it gone.
for (const outcome of ["commit", "abort", "abort without a reason"] as const) {
test(`browser state remove waits for transaction ${outcome}`, { timeout: 5000 }, async (t) => {
const opened = { result: { transaction: () => transaction }, onsuccess: () => {} };
const deleted = { onsuccess: () => {} };
let signalDelete!: () => void;
const queued = new Promise<void>((resolve) => { signalDelete = resolve; });
const failure = new DOMException("commit failed", "UnknownError");
const transaction = {
objectStore: () => ({
delete: () => {
signalDelete();
return deleted;
},
}),
error: outcome === "abort" ? failure : null,
oncomplete: () => {},
onabort: () => {},
};
Object.defineProperty(globalThis, "indexedDB", {
configurable: true,
value: { open: () => opened },
});
t.after(() => Reflect.deleteProperty(globalThis, "indexedDB"));

let settled = false;
const result = openState("test").remove("credential").then(
() => { settled = true; },
(error: unknown) => {
settled = true;
return error;
},
);
opened.onsuccess();
await queued;
deleted.onsuccess();
await new Promise<void>((resolve) => setImmediate(resolve));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

setImmediate is a non-standard, Node.js-specific global and is not supported in standard browser environments. Since this is a browser-targeted state store test, using setImmediate can cause runtime errors if the tests are executed in a real browser environment (e.g., via Karma, Web Test Runner, or Playwright) without a polyfill. Consider using setTimeout(resolve, 0) for cross-platform compatibility.

Suggested change
await new Promise<void>((resolve) => setImmediate(resolve));
await new Promise<void>((resolve) => setTimeout(resolve, 0));

assert.equal(settled, false, "remove settled before the transaction finished");
if (outcome === "commit") {
transaction.oncomplete();
} else {
transaction.onabort();
}
const error = await result;
if (outcome === "abort without a reason") {
assert.match(String(error), /IndexedDB delete transaction aborted/);
} else {
assert.equal(error, outcome === "abort" ? failure : undefined);
}
});
}

test("browser state retries an unsuccessful database open", async (t) => {
const failure = new DOMException("database temporarily unavailable", "UnknownError");
const data = Uint8Array.of(5);
Expand Down
6 changes: 5 additions & 1 deletion sdk/js/src/platform/state.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export function openState(location: string): StateStore {
},
async remove(name) {
const tx = (await open()).transaction(STORE, "readwrite");
await request(tx.objectStore(STORE).delete(name));
await new Promise<void>((resolve, reject) => {
tx.oncomplete = () => resolve();
tx.onabort = () => reject(tx.error ?? new Error("IndexedDB delete transaction aborted"));
tx.objectStore(STORE).delete(name);
});
Comment on lines +61 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In IndexedDB, if a request or transaction error is not caught and event.preventDefault() is not called, the error bubbles up to the global scope (triggering window.onerror or global error events). This can cause false positives in error monitoring tools (like Sentry) or cause some test runners to fail. Consider adding a tx.onerror handler that calls event.preventDefault() to prevent the error from bubbling up globally.

      await new Promise<void>((resolve, reject) => {
        tx.oncomplete = () => resolve();
        tx.onabort = () => reject(tx.error ?? new Error("IndexedDB delete transaction aborted"));
        tx.onerror = (event) => event.preventDefault();
        tx.objectStore(STORE).delete(name);
      });

},
};
}
Expand Down
Loading