Files
clean-architecture-frontend…/tests/helpers/memory-indexeddb.ts
T
DongHyeonkaandClaude Opus 5.5 3e37fbf0ed fix(storage): delete a partition's IndexedDB dataset when an authorized purge cannot open it (05-03)
LOGOUT and ACCOUNT_DELETION purged row by row and so needed an open that a
corrupt budget or binding row always rejected, leaving personal rows on the
device. After authority approval, a purge that finds the dataset rejected by
admission now deletes that partition's own database and reports it; healthy
datasets and other partitions are untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 22:24:55 +09:00

1163 lines
33 KiB
TypeScript

type MutableRequest<Value> = {
result: Value;
error: DOMException | null;
onsuccess: ((event: Event) => unknown) | null;
onerror: ((event: Event) => unknown) | null;
};
type MutableOpenRequest = MutableRequest<IDBDatabase> & {
transaction: IDBTransaction | null;
onblocked: ((event: IDBVersionChangeEvent) => unknown) | null;
onupgradeneeded: ((event: IDBVersionChangeEvent) => unknown) | null;
};
type IndexState = {
keyPath: string | string[];
unique: boolean;
multiEntry: boolean;
};
type StoreState = {
keyPath: string;
autoIncrement: boolean;
data: Map<string, unknown>;
indexes: Map<string, IndexState>;
};
/** One named database: its schema, rows and version. */
type DatabaseState = {
stores: Map<string, StoreState>;
version: number;
latest: FakeDatabase | null;
/** Connections handed out by a successful open, closed or not. */
connections: FakeDatabase[];
/** Transactions that have not finished, in creation order. */
transactions: FakeTransaction[];
/** A `deleteDatabase` waiting for the open connections to close. */
pendingDelete: (() => void) | null;
};
function storeOf(state: DatabaseState, name: string): StoreState {
const store = state.stores.get(name);
if (!store) {
throw new DOMException("Store does not exist.", "NotFoundError");
}
return store;
}
// The fake decides when a transaction's task has ended with a real macrotask,
// so a test that fakes timers cannot keep a transaction alive by accident.
const realSetTimeout = globalThis.setTimeout.bind(globalThis);
type FakeKeyRange =
| Readonly<{
fakeKind: "ONLY";
boundary: IDBValidKey;
}>
| Readonly<{
fakeKind: "LOWER";
boundary: IDBValidKey;
open: boolean;
}>
| Readonly<{
fakeKind: "UPPER";
boundary: IDBValidKey;
open: boolean;
}>
| Readonly<{
fakeKind: "BOUND";
lower: IDBValidKey;
upper: IDBValidKey;
lowerOpen: boolean;
upperOpen: boolean;
}>;
function createRequest<Value>(): MutableRequest<Value> {
return {
result: undefined as Value,
error: null,
onsuccess: null,
onerror: null,
};
}
function cloneStore(state: StoreState): StoreState {
return {
keyPath: state.keyPath,
autoIncrement: state.autoIncrement,
data: new Map(
Array.from(state.data, ([key, value]) => [
key,
structuredClone(value),
]),
),
indexes: new Map(
Array.from(state.indexes, ([name, index]) => [
name,
{
...index,
keyPath: Array.isArray(index.keyPath)
? [...index.keyPath]
: index.keyPath,
},
]),
),
};
}
function cloneStores(
stores: ReadonlyMap<string, StoreState>,
): Map<string, StoreState> {
return new Map(
Array.from(stores, ([name, state]) => [name, cloneStore(state)]),
);
}
function asException(error: unknown): DOMException {
return error instanceof DOMException
? error
: new DOMException("Fake IndexedDB failure.", "UnknownError");
}
function stringList(values: Iterable<string>): DOMStringList {
const items = [...values];
return {
contains: (value: string) => items.includes(value),
item: (index: number) => items[index] ?? null,
get length() {
return items.length;
},
[Symbol.iterator]: () => items[Symbol.iterator](),
} as DOMStringList;
}
function readPath(value: unknown, keyPath: string): unknown {
let current = value;
for (const segment of keyPath.split(".")) {
if (!current || typeof current !== "object") return undefined;
current = (current as Record<string, unknown>)[segment];
}
return current;
}
function primaryKey(state: StoreState, value: unknown): string {
const key = readPath(value, state.keyPath);
if (
(typeof key !== "string" && typeof key !== "number") ||
String(key).length === 0
) {
throw new DOMException("Invalid fake key.", "DataError");
}
return String(key);
}
function compareKeys(first: IDBValidKey, second: IDBValidKey): number {
const left =
first instanceof Date
? first.getTime()
: typeof first === "number" || typeof first === "string"
? first
: JSON.stringify(first);
const right =
second instanceof Date
? second.getTime()
: typeof second === "number" || typeof second === "string"
? second
: JSON.stringify(second);
if (left === right) return 0;
if (typeof left === "number" && typeof right === "number") {
return left < right ? -1 : 1;
}
return String(left) < String(right) ? -1 : 1;
}
/**
* A unique index rejects a second row whose index key already belongs to
* another primary key, which is how a browser reports it: a request-level
* `ConstraintError` that then aborts the transaction.
*
* The fake enforced primary keys only, so the one `unique: true` index in the
* repository — `indexeddb-opfs-journal.ts`'s logical-key index, whose store is
* keyed by `transactionId` instead — could never fail here the way it fails in
* a browser. Every other index in `src/adapters` is non-unique, so this check
* is a no-op for them.
*/
function assertUniqueIndexes(
state: StoreState,
key: string,
value: unknown,
): void {
for (const [name, index] of state.indexes) {
if (!index.unique || typeof index.keyPath !== "string") continue;
const indexKey = readPath(value, index.keyPath);
// An absent index key keeps the row out of the index entirely, so it
// cannot collide with anything.
if (indexKey === undefined) continue;
for (const [otherKey, otherValue] of state.data) {
// Replacing a row never collides with the row it replaces.
if (otherKey === key) continue;
const otherIndexKey = readPath(otherValue, index.keyPath);
if (otherIndexKey === undefined) continue;
if (
compareKeys(
indexKey as IDBValidKey,
otherIndexKey as IDBValidKey,
) === 0
) {
throw new DOMException(
`Unique index ${name} already holds this key.`,
"ConstraintError",
);
}
}
}
}
class FakeUpgradeTransaction {
aborted = false;
constructor(private readonly database: FakeDatabase) {}
abort(): void {
this.aborted = true;
}
objectStore(name: string): IDBObjectStore {
return this.database.upgradeObjectStore(name, this);
}
request<Value>(operation: () => Value): IDBRequest<Value> {
const request = createRequest<Value>();
try {
request.result = structuredClone(operation());
queueMicrotask(() => request.onsuccess?.(new Event("success")));
} catch (error) {
request.error = asException(error);
this.aborted = true;
queueMicrotask(() => request.onerror?.(new Event("error")));
}
return request as unknown as IDBRequest<Value>;
}
}
class FakeDatabase {
version = 0;
closed = false;
onversionchange: ((event: IDBVersionChangeEvent) => unknown) | null = null;
onclose: ((event: Event) => unknown) | null = null;
constructor(
private readonly owner: MemoryIndexedDbFactory,
readonly state: DatabaseState,
) {}
get objectStoreNames(): DOMStringList {
return stringList(this.state.stores.keys());
}
createObjectStore(
name: string,
options?: IDBObjectStoreParameters,
): IDBObjectStore {
if (this.state.stores.has(name)) {
throw new DOMException("Store already exists.", "ConstraintError");
}
if (typeof options?.keyPath !== "string") {
throw new DOMException("The fake requires a string key path.", "DataError");
}
this.state.stores.set(name, {
keyPath: options.keyPath,
autoIncrement: options.autoIncrement ?? false,
data: new Map(),
indexes: new Map(),
});
return this.schemaObjectStore(name);
}
deleteObjectStore(name: string): void {
if (!this.state.stores.delete(name)) {
throw new DOMException("Store does not exist.", "NotFoundError");
}
}
schemaObjectStore(name: string): IDBObjectStore {
const state = storeOf(this.state, name);
return new FakeObjectStore(null, state) as unknown as IDBObjectStore;
}
upgradeObjectStore(
name: string,
transaction: FakeUpgradeTransaction,
): IDBObjectStore {
const state = storeOf(this.state, name);
return new FakeObjectStore(
null,
state,
transaction,
) as unknown as IDBObjectStore;
}
transaction(
storeNames: string | Iterable<string>,
mode: IDBTransactionMode = "readonly",
): IDBTransaction {
if (this.closed) {
throw new DOMException("Connection is closed.", "InvalidStateError");
}
const names =
typeof storeNames === "string" ? [storeNames] : [...storeNames];
const transaction = new FakeTransaction(
this.owner,
this.state,
names,
mode,
);
this.owner.noteTransaction(transaction);
return transaction as unknown as IDBTransaction;
}
close(): void {
this.closed = true;
this.state.pendingDelete?.();
}
fireVersionChange(newVersion: number | null): void {
this.onversionchange?.({
oldVersion: this.version,
newVersion,
} as IDBVersionChangeEvent);
}
fireForcedClose(): void {
this.closed = true;
this.onclose?.(new Event("close"));
}
}
class FakeTransaction {
oncomplete: ((event: Event) => unknown) | null = null;
onerror: ((event: Event) => unknown) | null = null;
onabort: ((event: Event) => unknown) | null = null;
error: DOMException | null = null;
readonly mode: IDBTransactionMode;
private active = true;
/**
* Native "active" state: a request may be placed only in the task that
* created the transaction or while one of its request events (and the
* microtasks that event queued) is being handled.
*/
private accepting = true;
/** Held back until every earlier overlapping transaction finishes. */
private started = false;
private readonly deferred: Array<() => void> = [];
private completionQueued = false;
private pending = 0;
private readonly workingStores: Map<string, StoreState>;
constructor(
private readonly owner: MemoryIndexedDbFactory,
private readonly database: DatabaseState,
private readonly names: readonly string[],
mode: IDBTransactionMode,
) {
this.mode = mode;
this.workingStores = new Map(
names.map((name) => [name, cloneStore(storeOf(database, name))]),
);
database.transactions.push(this);
this.startIfUnblocked();
this.deactivateAfterTask();
}
/**
* Native IndexedDB runs transactions with overlapping scopes in creation
* order unless both are readonly. A later one starts from the rows the
* earlier one committed.
*/
startIfUnblocked(): void {
if (this.started || !this.active) return;
const earlier = this.database.transactions.slice(
0,
this.database.transactions.indexOf(this),
);
if (earlier.some((transaction) => transaction.blocks(this))) return;
this.started = true;
for (const name of this.names) {
const fresh = cloneStore(storeOf(this.database, name));
const working = this.workingStores.get(name)!;
working.data = fresh.data;
working.indexes = fresh.indexes;
}
for (const callback of this.deferred.splice(0)) {
this.owner.scheduleTransaction(callback, this.mode);
}
if (!this.accepting) this.commitIfIdle();
}
private blocks(later: FakeTransaction): boolean {
return (
this.active &&
(this.mode === "readwrite" || later.mode === "readwrite") &&
this.names.some((name) => later.names.includes(name))
);
}
private schedule(callback: () => void): void {
if (this.started) {
this.owner.scheduleTransaction(callback, this.mode);
} else {
this.deferred.push(callback);
}
}
private deactivateAfterTask(): void {
realSetTimeout(() => {
this.accepting = false;
this.commitIfIdle();
}, 0);
}
/** A native transaction with no request left commits once inactive. */
private commitIfIdle(): void {
if (this.started && this.pending === 0) this.queueCompletion();
}
private assertAccepting(): void {
if (!this.active || !this.accepting) {
throw new DOMException(
"The transaction is not active.",
"TransactionInactiveError",
);
}
}
private dispatch(handler: () => void): void {
this.accepting = true;
handler();
this.deactivateAfterTask();
}
private finish(): void {
const index = this.database.transactions.indexOf(this);
if (index >= 0) this.database.transactions.splice(index, 1);
for (const transaction of [...this.database.transactions]) {
transaction.startIfUnblocked();
}
}
objectStore(name: string): IDBObjectStore {
if (!this.names.includes(name)) {
throw new DOMException("Store is outside transaction.", "NotFoundError");
}
const state = this.workingStores.get(name);
if (!state) {
throw new DOMException("Store does not exist.", "NotFoundError");
}
return new FakeObjectStore(this, state) as unknown as IDBObjectStore;
}
abort(): void {
if (!this.active) {
throw new DOMException("Transaction is inactive.", "InvalidStateError");
}
this.abortWith(new DOMException("Transaction aborted.", "AbortError"));
}
request<Value>(operation: () => Value): IDBRequest<Value> {
this.assertAccepting();
const request = createRequest<Value>();
this.pending += 1;
this.schedule(() => {
if (!this.active) return;
try {
request.result = structuredClone(operation());
this.dispatch(() => request.onsuccess?.(new Event("success")));
} catch (error) {
request.error = asException(error);
this.dispatch(() => request.onerror?.(new Event("error")));
this.abortWith(request.error);
}
if (!this.active) return;
this.pending -= 1;
this.queueCompletion();
});
return request as unknown as IDBRequest<Value>;
}
cursor(
rows: readonly Readonly<{
key: IDBValidKey;
primaryKey: IDBValidKey;
value: unknown;
}>[],
direction: IDBCursorDirection,
): IDBRequest<IDBCursorWithValue | null> {
this.assertAccepting();
const request = createRequest<IDBCursorWithValue | null>();
let position = 0;
this.pending += 1;
const emit = () => {
this.schedule(() => {
if (!this.active) return;
let continued = false;
let eventReturned = false;
const row = rows[position];
if (!row) {
request.result = null;
} else {
request.result = {
key: structuredClone(row.key),
primaryKey: structuredClone(row.primaryKey),
value: structuredClone(row.value),
continue: (key?: IDBValidKey) => {
if (continued) {
throw new DOMException(
"Cursor already continued.",
"InvalidStateError",
);
}
continued = true;
if (eventReturned) this.pending += 1;
if (key === undefined) {
position += 1;
} else {
const nextPosition = rows.findIndex(
(candidate, candidateIndex) =>
candidateIndex > position &&
(direction === "prev" ||
direction === "prevunique"
? compareKeys(candidate.key, key) <= 0
: compareKeys(candidate.key, key) >= 0),
);
position =
nextPosition < 0 ? rows.length : nextPosition;
}
emit();
},
continuePrimaryKey: (
key: IDBValidKey,
primaryKey: IDBValidKey,
) => {
if (continued) {
throw new DOMException(
"Cursor already continued.",
"InvalidStateError",
);
}
continued = true;
if (eventReturned) this.pending += 1;
const nextPosition = rows.findIndex(
(candidate, candidateIndex) => {
if (candidateIndex <= position) return false;
const keyOrder = compareKeys(candidate.key, key);
const tupleOrder =
keyOrder === 0
? compareKeys(
candidate.primaryKey,
primaryKey,
)
: keyOrder;
return direction === "prev" ||
direction === "prevunique"
? tupleOrder <= 0
: tupleOrder >= 0;
},
);
position =
nextPosition < 0 ? rows.length : nextPosition;
emit();
},
} as unknown as IDBCursorWithValue;
}
this.dispatch(() => request.onsuccess?.(new Event("success")));
eventReturned = true;
if (continued || !this.active) return;
this.pending -= 1;
this.queueCompletion();
});
};
emit();
return request as unknown as IDBRequest<IDBCursorWithValue | null>;
}
assertWritable(): void {
if (this.mode !== "readwrite") {
throw new DOMException("Transaction is read-only.", "ReadOnlyError");
}
}
private queueCompletion(): void {
if (
!this.active ||
this.pending !== 0 ||
this.completionQueued
) {
return;
}
this.completionQueued = true;
this.schedule(() => {
this.completionQueued = false;
if (!this.active || this.pending !== 0) return;
const commitFailure =
this.mode === "readwrite"
? this.owner.consumeCommitFailure()
: null;
if (commitFailure) {
this.abortWith(commitFailure);
return;
}
if (this.mode === "readwrite") {
for (const [name, state] of this.workingStores) {
this.database.stores.set(name, cloneStore(state));
}
}
this.active = false;
this.finish();
this.oncomplete?.(new Event("complete"));
});
}
private abortWith(error: DOMException): void {
if (!this.active) return;
this.active = false;
this.error = error;
this.finish();
this.owner.scheduleLifecycle(() => {
this.onerror?.(new Event("error"));
this.onabort?.(new Event("abort"));
});
}
}
class FakeObjectStore {
constructor(
private readonly transaction: FakeTransaction | null,
private readonly state: StoreState,
private readonly upgradeTransaction: FakeUpgradeTransaction | null = null,
) {}
get indexNames(): DOMStringList {
return stringList(this.state.indexes.keys());
}
get(query: IDBValidKey | IDBKeyRange): IDBRequest<unknown> {
return this.request(() =>
this.state.data.get(String(query)),
);
}
put(value: unknown): IDBRequest<IDBValidKey> {
this.assertWritable();
return this.request(() => {
const key = primaryKey(this.state, value);
assertUniqueIndexes(this.state, key, value);
this.state.data.set(key, structuredClone(value));
return key as IDBValidKey;
});
}
add(value: unknown): IDBRequest<IDBValidKey> {
this.assertWritable();
return this.request(() => {
const key = primaryKey(this.state, value);
if (this.state.data.has(key)) {
throw new DOMException("Key already exists.", "ConstraintError");
}
assertUniqueIndexes(this.state, key, value);
this.state.data.set(key, structuredClone(value));
return key as IDBValidKey;
});
}
delete(query: IDBValidKey | IDBKeyRange): IDBRequest<undefined> {
this.assertWritable();
return this.request(() => {
this.state.data.delete(String(query));
return undefined;
});
}
openCursor(
query?: IDBValidKey | IDBKeyRange | null,
direction: IDBCursorDirection = "next",
): IDBRequest<IDBCursorWithValue | null> {
return this.requireTransaction().cursor(
this.rows(undefined, direction, query),
direction,
);
}
index(name: string): IDBIndex {
if (!this.state.indexes.has(name)) {
throw new DOMException("Index does not exist.", "NotFoundError");
}
return new FakeIndex(
this.requireTransaction(),
this.state,
name,
) as unknown as IDBIndex;
}
createIndex(
name: string,
keyPath: string | string[],
options?: IDBIndexParameters,
): IDBIndex {
if (this.state.indexes.has(name)) {
throw new DOMException("Index already exists.", "ConstraintError");
}
this.state.indexes.set(name, {
keyPath: Array.isArray(keyPath) ? [...keyPath] : keyPath,
unique: options?.unique ?? false,
multiEntry: options?.multiEntry ?? false,
});
return {} as IDBIndex;
}
deleteIndex(name: string): void {
if (!this.state.indexes.delete(name)) {
throw new DOMException("Index does not exist.", "NotFoundError");
}
}
rows(
indexName: string | undefined,
direction: IDBCursorDirection,
query?: IDBValidKey | IDBKeyRange | null,
): readonly Readonly<{
key: IDBValidKey;
primaryKey: IDBValidKey;
value: unknown;
}>[] {
const index = indexName
? this.state.indexes.get(indexName)
: undefined;
let rows = Array.from(this.state.data, ([key, value]) => {
const indexKey =
index && typeof index.keyPath === "string"
? readPath(value, index.keyPath)
: key;
if (index && indexKey === undefined) return null;
return {
key: indexKey as IDBValidKey,
primaryKey: key,
value,
};
}).filter(
(
row,
): row is {
key: IDBValidKey;
primaryKey: string;
value: unknown;
} => row !== null,
);
if (
query &&
typeof query === "object" &&
"fakeKind" in query
) {
const range = query as unknown as FakeKeyRange;
rows = rows.filter((row) => {
if (range.fakeKind === "BOUND") {
const lower = compareKeys(row.key, range.lower);
const upper = compareKeys(row.key, range.upper);
return (
(range.lowerOpen ? lower > 0 : lower >= 0) &&
(range.upperOpen ? upper < 0 : upper <= 0)
);
}
const comparison = compareKeys(row.key, range.boundary);
if (range.fakeKind === "ONLY") return comparison === 0;
if (range.fakeKind === "LOWER") {
return range.open
? comparison > 0
: comparison >= 0;
}
return range.open
? comparison < 0
: comparison <= 0;
});
}
rows.sort((left, right) => {
const indexOrder = compareKeys(left.key, right.key);
return indexOrder === 0
? compareKeys(left.primaryKey, right.primaryKey)
: indexOrder;
});
if (direction === "prev" || direction === "prevunique") {
rows.reverse();
}
return rows;
}
private requireTransaction(): FakeTransaction {
if (!this.transaction) {
throw new DOMException(
"Schema object store has no data transaction.",
"TransactionInactiveError",
);
}
return this.transaction;
}
private request<Value>(operation: () => Value): IDBRequest<Value> {
if (this.transaction) return this.transaction.request(operation);
if (this.upgradeTransaction) {
return this.upgradeTransaction.request(operation);
}
throw new DOMException(
"Schema object store has no data transaction.",
"TransactionInactiveError",
);
}
private assertWritable(): void {
if (this.transaction) {
this.transaction.assertWritable();
return;
}
if (!this.upgradeTransaction) {
throw new DOMException(
"Schema object store has no data transaction.",
"TransactionInactiveError",
);
}
}
}
class FakeIndex {
constructor(
private readonly transaction: FakeTransaction,
private readonly state: StoreState,
private readonly name: string,
) {}
openCursor(
query?: IDBValidKey | IDBKeyRange | null,
direction: IDBCursorDirection = "next",
): IDBRequest<IDBCursorWithValue | null> {
const store = new FakeObjectStore(this.transaction, this.state);
return this.transaction.cursor(
store.rows(this.name, direction, query),
direction,
);
}
}
/**
* Minimal deterministic IndexedDB model for adapter contract tests. It is not
* intended to polyfill IndexedDB for application code.
*
* Modeled like a browser:
* - request ordering and transaction-level commit/rollback;
* - one database per name (`open(name)`), so two names never share rows;
* - transactions with overlapping scopes run one after another unless both
* are readonly, and a later one sees what the earlier one committed;
* - a request placed after the transaction's task ended, outside a request
* event and its microtasks, throws `TransactionInactiveError`, and a
* transaction with nothing left to do commits on its own;
* - upgrade blocking, through `blockNextOpen`/`releaseBlockedOpen`.
*
* Not modeled (05-08) — use a browser capability spec for these:
* - key comparison is by `String`/JSON, and a compound (array) index key path
* is not read, so such an index orders by primary key;
* - opening a higher version does not fire `versionchange` on open
* connections, and `onblocked` fires only for `blockNextOpen`; tests drive
* `triggerVersionChange` by hand;
* - the upgrade transaction ends synchronously, so an abort requested from an
* asynchronous step inside `onupgradeneeded` is not rolled back;
* - quota: writes never fail for lack of space unless a test injects it with
* `failNextWriteCommit`.
*
* `seed`, `readRaw` and the lifecycle triggers act on the database opened
* most recently.
*/
export class MemoryIndexedDbFactory {
private readonly databases = new Map<string, DatabaseState>();
private current: DatabaseState = this.databaseNamed("");
private blockedRequest: Readonly<{
request: MutableOpenRequest;
targetVersion: number;
database: FakeDatabase;
}> | null = null;
private transactionQueue: Array<() => void> = [];
private transactionsPaused = false;
private nextCommitFailure: DOMException | null = null;
private shouldBlockNextOpen = false;
lastTransaction: FakeTransaction | null = null;
readonly factory: IDBFactory = {
open: (name: string, version?: number) =>
this.open(name, version ?? 1) as unknown as IDBOpenDBRequest,
deleteDatabase: (name: string) =>
this.deleteDatabase(name) as unknown as IDBOpenDBRequest,
cmp: compareKeys,
} as IDBFactory;
readonly keyRange: Pick<
typeof IDBKeyRange,
"only" | "lowerBound" | "upperBound" | "bound"
> = {
only: (value: IDBValidKey) =>
({
fakeKind: "ONLY",
boundary: structuredClone(value),
}) as unknown as IDBKeyRange,
lowerBound: (bound: IDBValidKey, open = false) =>
({
fakeKind: "LOWER",
boundary: structuredClone(bound),
open,
}) as unknown as IDBKeyRange,
upperBound: (bound: IDBValidKey, open = false) =>
({
fakeKind: "UPPER",
boundary: structuredClone(bound),
open,
}) as unknown as IDBKeyRange,
bound: (
lower: IDBValidKey,
upper: IDBValidKey,
lowerOpen = false,
upperOpen = false,
) =>
({
fakeKind: "BOUND",
lower: structuredClone(lower),
upper: structuredClone(upper),
lowerOpen,
upperOpen,
}) as unknown as IDBKeyRange,
};
open(name: string, version: number): MutableOpenRequest {
const state = this.databaseNamed(name);
const database = new FakeDatabase(this, state);
database.version = state.version;
state.latest = database;
this.current = state;
const request: MutableOpenRequest = {
...createRequest<IDBDatabase>(),
result: database as unknown as IDBDatabase,
transaction: null,
onblocked: null,
onupgradeneeded: null,
};
queueMicrotask(() => {
if (this.shouldBlockNextOpen) {
this.shouldBlockNextOpen = false;
this.blockedRequest = {
request,
targetVersion: version,
database,
};
request.onblocked?.({
oldVersion: state.version,
newVersion: version,
} as IDBVersionChangeEvent);
return;
}
this.finishOpen(request, version, database);
});
return request;
}
/**
* Like a browser: open connections get `versionchange` (new version
* `null`); if any stays open the request reports `blocked` and completes
* once the last one closes. The name then holds an empty version-0
* database, so `hasStore` answers false.
*/
deleteDatabase(name: string): MutableOpenRequest {
const request: MutableOpenRequest = {
...createRequest<IDBDatabase>(),
transaction: null,
onblocked: null,
onupgradeneeded: null,
};
queueMicrotask(() => {
const state = this.databaseNamed(name);
const oldVersion = state.version;
const finish = () => {
if (state.connections.some((connection) => !connection.closed)) {
return;
}
state.pendingDelete = null;
state.stores = new Map();
state.version = 0;
state.connections = [];
request.onsuccess?.({
oldVersion,
newVersion: null,
} as unknown as Event);
};
for (const connection of state.connections) {
if (!connection.closed) connection.fireVersionChange(null);
}
if (state.connections.some((connection) => !connection.closed)) {
state.pendingDelete = finish;
request.onblocked?.({
oldVersion,
newVersion: null,
} as IDBVersionChangeEvent);
return;
}
finish();
});
return request;
}
blockNextOpen(): void {
this.shouldBlockNextOpen = true;
}
releaseBlockedOpen(): void {
const blocked = this.blockedRequest;
this.blockedRequest = null;
if (blocked) {
this.finishOpen(
blocked.request,
blocked.targetVersion,
blocked.database,
);
}
}
pauseTransactions(): void {
this.transactionsPaused = true;
}
clearLastTransaction(): void {
this.lastTransaction = null;
}
resumeTransactions(): void {
this.transactionsPaused = false;
const queued = this.transactionQueue;
this.transactionQueue = [];
for (const callback of queued) queueMicrotask(callback);
}
failNextWriteCommit(error: DOMException): void {
this.nextCommitFailure = error;
}
triggerVersionChange(newVersion: number): void {
this.current.latest?.fireVersionChange(newVersion);
}
triggerForcedClose(): void {
this.current.latest?.fireForcedClose();
}
isConnectionClosed(): boolean {
return this.current.latest?.closed ?? true;
}
seed(storeName: string, value: unknown): void {
const state = storeOf(this.current, storeName);
const key = primaryKey(state, value);
state.data.set(key, structuredClone(value));
}
readRaw(storeName: string, key: string): unknown {
const value = storeOf(this.current, storeName).data.get(key);
return value === undefined ? undefined : structuredClone(value);
}
scheduleTransaction(
callback: () => void,
mode?: IDBTransactionMode,
): void {
if (this.transactionsPaused && mode === "readwrite") {
this.transactionQueue.push(callback);
return;
}
queueMicrotask(callback);
}
scheduleLifecycle(callback: () => void): void {
queueMicrotask(callback);
}
consumeCommitFailure(): DOMException | null {
const failure = this.nextCommitFailure;
this.nextCommitFailure = null;
return failure;
}
noteTransaction(transaction: FakeTransaction): void {
this.lastTransaction = transaction;
}
storeNames(): Iterable<string> {
return this.current.stores.keys();
}
hasStore(name: string): boolean {
return this.current.stores.has(name);
}
private databaseNamed(name: string): DatabaseState {
let state = this.databases.get(name);
if (!state) {
state = {
stores: new Map(),
version: 0,
latest: null,
connections: [],
transactions: [],
pendingDelete: null,
};
this.databases.set(name, state);
}
return state;
}
private finishOpen(
request: MutableOpenRequest,
targetVersion: number,
database: FakeDatabase,
): void {
const state = database.state;
if (targetVersion < state.version) {
request.error = new DOMException(
"Requested version is older.",
"VersionError",
);
request.onerror?.(new Event("error"));
return;
}
if (targetVersion > state.version) {
const before = cloneStores(state.stores);
const oldVersion = state.version;
const upgrade = new FakeUpgradeTransaction(database);
request.transaction = upgrade as unknown as IDBTransaction;
database.version = targetVersion;
request.onupgradeneeded?.({
oldVersion,
newVersion: targetVersion,
} as IDBVersionChangeEvent);
request.transaction = null;
if (upgrade.aborted) {
state.stores = before;
database.version = oldVersion;
request.error = new DOMException(
"Upgrade transaction aborted.",
"AbortError",
);
request.onerror?.(new Event("error"));
return;
}
state.version = targetVersion;
}
database.version = state.version;
state.connections.push(database);
request.result = database as unknown as IDBDatabase;
request.onsuccess?.(new Event("success"));
}
}