Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Keep a settings read that was under way during a save out of the cache

Claude3c994d7258d9
diff --git a/src/lib/settings.dbtest.ts b/src/lib/settings.dbtest.ts
index d36b8d5..69a9cd0 100644
--- a/src/lib/settings.dbtest.ts
+++ b/src/lib/settings.dbtest.ts
@@ -54,3 +54,52 @@ test("a read during an open save does not keep the old settings after commit", a
assert.equal((await getSettings()).requireTwoFactor, true);
});
+
+test("a read that finishes after a save commits does not cache what it read before", async () => {
+ await saveSettings({ requireTwoFactor: false, signupMode: "open" });
+ invalidateSettingsCache();
+
+ // Pages read through a pool that runs each query at once but hands the
+ // result back only when told: the read sees the row as it was, and
+ // returns after the save below has committed.
+ let release!: () => void;
+ const released = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ let queried!: () => void;
+ const ran = new Promise<void>((resolve) => {
+ queried = resolve;
+ });
+ const slowPool = new Proxy(database, {
+ get(target, property, receiver) {
+ if (property === "query") {
+ return async (...args: Parameters<pg.Pool["query"]>) => {
+ const result = await (
+ target.query as (...a: unknown[]) => Promise<unknown>
+ )(...args);
+ queried();
+ await released;
+ return result;
+ };
+ }
+
+ return Reflect.get(target, property, receiver);
+ },
+ });
+
+ replaceDbPoolForTests(slowPool);
+
+ try {
+ const oldRead = getSettings();
+
+ await ran;
+ await saveSettings({ requireTwoFactor: true }, database);
+ release();
+
+ assert.equal((await oldRead).requireTwoFactor, false);
+ } finally {
+ replaceDbPoolForTests(database);
+ }
+
+ assert.equal((await getSettings()).requireTwoFactor, true);
+});
diff --git a/src/lib/settings.ts b/src/lib/settings.ts
index 60d856a..801c135 100644
--- a/src/lib/settings.ts
+++ b/src/lib/settings.ts
@@ -184,6 +184,13 @@ const SETTINGS_CACHE_MS = 30_000;
const SETUP_COMPLETE_CACHE_MS = 60_000;
type SettingsCache = {
+ /**
+ * Counts the clears, so a read that was already under way when the
+ * settings changed does not put what it read back into the cache.
+ * Optional because a cache made by an earlier copy of this module in the
+ * same process (a dev reload) has none.
+ */
+ generation?: number;
settings: { expiresAt: number; value: AppSettings | null } | null;
setupCompleteUntil: number;
};
@@ -195,11 +202,13 @@ declare global {
// Kept on globalThis, like the rate limit store, so every copy of this
// module in one process shares it and a change clears it for all of them.
const cache: SettingsCache = (globalThis.__signudocSettingsCache ??= {
+ generation: 0,
settings: null,
setupCompleteUntil: 0,
});
export function invalidateSettingsCache() {
+ cache.generation = (cache.generation ?? 0) + 1;
cache.settings = null;
cache.setupCompleteUntil = 0;
}
@@ -296,9 +305,14 @@ async function getStoredSettings(): Promise<AppSettings | null> {
return cache.settings.value;
}
+ const generation = cache.generation ?? 0;
const value = await readStoredSettings();
- cache.settings = { expiresAt: now + SETTINGS_CACHE_MS, value };
+ // Settings saved while this read waited may be newer than what it read:
+ // use it for this request, but leave the cache to the next read.
+ if ((cache.generation ?? 0) === generation) {
+ cache.settings = { expiresAt: now + SETTINGS_CACHE_MS, value };
+ }
return value;
}