Skip to content
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format follows Keep a Changelog and the package uses semantic versioning.

## Unreleased

- Coordinated custom-persistent namespace wrappers sharing the same immediate
delegate object and encoded namespace within one loaded module. Registry
updates no longer lose settled peer writes; complete same-key mutations and
namespace cleanup preserve order. Failed cleanup drains started deletes
before releasing its barrier. Idle bookkeeping is removed. Registry
contention, queued references and cleanup-error delay are costs. Distinct
delegate identities, module copies, tabs, direct writes, reads and shared
memory remain uncoordinated. Public options, formats and the default
IndexedDB path are unchanged.

- Guarded failed-decryption cleanup against deleting a changed committed
persistent-cache row. Eligible concrete built-in reads compare known
metadata and encrypted bytes in the same transaction as any delete.
Expand Down
311 changes: 311 additions & 0 deletions benchmarks/shared-persistent-namespace-ordering/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vitest/config";

const root = fileURLToPath(new URL("../../", import.meta.url));
const target = fileURLToPath(new URL("../../src/browser/cache.ts", import.meta.url)).replaceAll("\\", "/");
const { baseline } = JSON.parse(readFileSync(new URL("./protocol.json", import.meta.url), "utf8"));
const source = execFileSync("git", ["--no-pager", "show", `${baseline}:src/browser/cache.ts`], { cwd: root, encoding: "utf8" });
export default defineConfig({
root,
plugins: [{
name: "exact-baseline-shared-persistent-namespace", enforce: "pre",
load(id) { return id.replaceAll("\\", "/").split("?")[0] === target ? source : null; },
}],
test: { include: ["tests/browser-persistent-namespace.test.ts"] },
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createThimbleConnection } from "../../src/browser/connect.js";
import type { CachedJsonObject, PersistentObjectCache } from "../../src/browser/cache.js";
import { entry, equal, MapBackend, type Backend, type Policy } from "./fixtures.js";

export async function runFactoryCase(policy: Policy, backend: Backend = new MapBackend()) {
const calls: Array<{ phase: string; kind: string; key: string }> = [];
let phase = "seed", clones = 0;
const previousClone = globalThis.structuredClone;
const shared: PersistentObjectCache = {
get(key) { calls.push({ phase, kind: "get", key }); return Promise.resolve(backend.read(key)); },
set(value) { calls.push({ phase, kind: "set", key: value.key }); return Promise.resolve(backend.write(value)); },
delete(key) { calls.push({ phase, kind: "delete", key }); return Promise.resolve(backend.remove(key)); },
async clear() { throw new Error("Factory must not clear the whole custom delegate"); },
async destroy() { throw new Error("Factory must not destroy the whole custom delegate"); },
};
const connections: Awaited<ReturnType<typeof createThimbleConnection>>[] = [];
const channel = `p16-factory-${crypto.randomUUID()}`;
const open = async (scope = "p16-owner") => {
const connection = await createThimbleConnection({
configurationUrl: "https://p16.example.test/api/config",
persistentCache: shared,
channelName: channel,
fetchImplementation: () => Promise.resolve(Response.json({
name: "ThimbleDB", provider: "local", readBaseUrl: "/api/objects",
headTtlMs: 10_000, cachePolicy: policy, collectionLayouts: { notes: "snapshot" },
layoutGeneration: "generation-1", csrfToken: "fixture",
user: { id: "fixture", provider: "dev", roles: [], tenants: [], identities: [] },
scope: { id: scope, encrypted: false, keyId: null, keyEndpoint: null },
})),
});
connections.push(connection);
return connection;
};
try {
const first = await open(), second = await open(), foreign = await open("p16-owner:other");
const foreignEntry = entry("head", 90, "foreign/HEAD.json");
await foreign.cache.set(foreignEntry);
globalThis.structuredClone = ((value: unknown, options?: StructuredSerializeOptions) => {
const result = previousClone(value, options);
if (phase === "sequential") clones++;
return result;
}) as typeof structuredClone;
phase = "sequential";
const sequentialEntry = entry("head", 1, "sequential/HEAD.json");
await first.cache.set(sequentialEntry);
first.cache.clearMemory();
const sequentialReturn = await first.cache.get(sequentialEntry.key);
await first.cache.delete(sequentialEntry.key);
const sequential = {
counts: Object.fromEntries(["get", "set", "delete"].map(kind => [kind,
calls.filter(call => call.phase === "sequential" && call.kind === kind).length])),
clones, returned: sequentialReturn, expected: sequentialEntry,
correct: equal(sequentialReturn, sequentialEntry),
};
phase = "reset"; await first.cache.clearAll();
phase = "concurrent";
const values: CachedJsonObject[] = [entry("head", 2, "first/HEAD.json"), entry("head", 3, "second/HEAD.json")];
await Promise.all([first.cache.set(values[0]!), second.cache.set(values[1]!)]);
const afterSets = await backend.snapshot();
phase = "clear"; await first.cache.clearAll();
const afterClear = await backend.snapshot();
phase = "cold";
const cold = await open();
const returned = await Promise.all(values.map(value => cold.cache.get(value.key)));
foreign.cache.clearMemory();
const foreignReturned = await foreign.cache.get(foreignEntry.key);
const correct = sequential.correct && afterSets.length === 5 && afterClear.length === 2 &&
returned.every(value => value === null) && equal(foreignReturned, foreignEntry) &&
connections.every(connection => connection.cache.metrics().persistentErrors === 0);
return { policy, correct, sequential, calls, afterSets, afterClear, returned, foreignReturned, foreignExpected: foreignEntry };
} finally {
globalThis.structuredClone = previousClone;
for (const connection of connections) connection.client.close();
}
}
Loading