mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(config): transact per-group configuration
Whole-table writes and cached snapshots can overwrite other groups or resurrect removed configuration. Use per-group guarded writes, one-time identity migration and explicit unreadable-row failures.
This commit is contained in:
1 parent
ceb906d479
commit
c853f2910f
7 files changed
+1764
-587
No files matched your search
+72
-51
@@ -1,19 +1,31 @@
|
||||
import type { Env, Config, Route } from "./types";
|
||||
import { log } from "./lib/log";
|
||||
import { migrateRoutes, validateRoutes } from "./config/schema";
|
||||
import { d1ConfigStore, type ConfigStore } from "./storage/config-store";
|
||||
import type { Env, Config, Group, Route } from "./types";
|
||||
import {
|
||||
createConfigStore,
|
||||
kvConfigStore,
|
||||
type ConfigStore,
|
||||
type GuardedWrite,
|
||||
type LoadOptions,
|
||||
} from "./storage/config-store";
|
||||
import { canUseD1 } from "./storage/d1";
|
||||
|
||||
const CONFIG_CACHE_TTL = 300_000;
|
||||
const ROUTES_KEY = "config:routes";
|
||||
let configCache: { config: Config; expiresAt: number } | null = null;
|
||||
const configStores = new WeakMap<KVNamespace, ConfigStore>();
|
||||
|
||||
export function getConfigStore(kv: KVNamespace): ConfigStore | null {
|
||||
return configStores.get(kv) ?? null;
|
||||
/**
|
||||
* The configuration store bound to this KV namespace. Every request, queue
|
||||
* batch and scheduled task registers the D1-backed store first (see
|
||||
* `initConfigStore`); a namespace that was never registered — a test, or a
|
||||
* deployment without D1 — reads and writes the KV-only store.
|
||||
*/
|
||||
export function getConfigStore(kv: KVNamespace): ConfigStore {
|
||||
return configStores.get(kv) ?? kvConfigStore(kv);
|
||||
}
|
||||
|
||||
export function initConfigStore(env: Env): ConfigStore {
|
||||
return ensureStore(env);
|
||||
const existing = configStores.get(env.KV);
|
||||
if (existing && (existing.kind === "d1" || !canUseD1(env.DB))) return existing;
|
||||
const store = createConfigStore(env.DB, env.KV);
|
||||
configStores.set(env.KV, store);
|
||||
return store;
|
||||
}
|
||||
|
||||
export function resetConfigStore(kv?: KVNamespace): void {
|
||||
@@ -23,52 +35,64 @@ export function resetConfigStore(kv?: KVNamespace): void {
|
||||
}
|
||||
}
|
||||
|
||||
function ensureStore(env: Env): ConfigStore {
|
||||
let store = configStores.get(env.KV);
|
||||
if (!store) {
|
||||
store = d1ConfigStore(env.DB, env.KV);
|
||||
configStores.set(env.KV, store);
|
||||
}
|
||||
return store;
|
||||
}
|
||||
|
||||
export async function loadRoutes(kv: KVNamespace): Promise<Route[]> {
|
||||
const store = configStores.get(kv);
|
||||
if (store) return store.loadRoutes();
|
||||
|
||||
try {
|
||||
const stored = await kv.get<Route[]>(ROUTES_KEY, "json");
|
||||
if (stored) return validateRoutes(migrateRoutes(stored));
|
||||
} catch (err) {
|
||||
log.warn({ err }, "Failed to load routes from KV");
|
||||
}
|
||||
return [];
|
||||
export async function loadRoutes(kv: KVNamespace, opts?: LoadOptions): Promise<Route[]> {
|
||||
return getConfigStore(kv).loadRoutes(opts);
|
||||
}
|
||||
|
||||
export async function saveRoutes(kv: KVNamespace, routes: Route[]): Promise<void> {
|
||||
const store = configStores.get(kv);
|
||||
if (store) {
|
||||
await store.saveRoutes(routes);
|
||||
configCache = null;
|
||||
return;
|
||||
}
|
||||
await kv.put(ROUTES_KEY, JSON.stringify(routes));
|
||||
configCache = null;
|
||||
await getConfigStore(kv).saveRoutes(routes);
|
||||
}
|
||||
|
||||
export function invalidateConfigCache(): void {
|
||||
configCache = null;
|
||||
export async function saveGroupRoutes(
|
||||
kv: KVNamespace,
|
||||
groupId: string,
|
||||
routes: Route[],
|
||||
opts?: GuardedWrite<Route[]>,
|
||||
): Promise<Route[]> {
|
||||
return getConfigStore(kv).saveGroupRoutes(groupId, routes, opts);
|
||||
}
|
||||
|
||||
/** Drop every cached configuration snapshot held by this isolate. */
|
||||
export function invalidateConfigCache(kv?: KVNamespace): void {
|
||||
if (kv) configStores.get(kv)?.invalidateCache();
|
||||
}
|
||||
|
||||
/** One group, read fresh (its row alone): see `ConfigStore.loadGroup`. */
|
||||
export function loadGroupFresh(
|
||||
env: Env,
|
||||
id: string,
|
||||
): Promise<{ group: Group | null; unreadable: boolean }> {
|
||||
return initConfigStore(env).loadGroup(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration and groups of one consistent moment, for a dispatch that
|
||||
* needs both (routes of a renamed group must never meet its old id, nor new
|
||||
* targets meet stale source restrictions).
|
||||
*/
|
||||
export async function loadConfigSnapshot(
|
||||
env: Env,
|
||||
opts?: LoadOptions,
|
||||
): Promise<{ config: Config; groups: Group[] }> {
|
||||
const store = initConfigStore(env);
|
||||
const { routes, groups, unreadable } = await store.loadSnapshot(opts);
|
||||
const config = buildConfig(env, routes);
|
||||
if (unreadable?.length) config.unreadableGroups = unreadable;
|
||||
return { config, groups };
|
||||
}
|
||||
|
||||
/**
|
||||
* The configuration for one dispatch. Each call returns a fresh object with
|
||||
* its own `routes` array, so a caller narrowing the routes (tenant scoping)
|
||||
* can never leak that change into another request served by this isolate.
|
||||
*/
|
||||
export async function loadConfig(env: Env): Promise<Config> {
|
||||
if (configCache && Date.now() < configCache.expiresAt) {
|
||||
return configCache.config;
|
||||
}
|
||||
initConfigStore(env);
|
||||
return buildConfig(env, await loadRoutes(env.KV));
|
||||
}
|
||||
|
||||
ensureStore(env);
|
||||
const routes = await loadRoutes(env.KV);
|
||||
|
||||
const config: Config = {
|
||||
function buildConfig(env: Env, routes: Route[]): Config {
|
||||
return {
|
||||
baseUrl: env.BASE_URL ?? "https://webhooker.example.workers.dev",
|
||||
github: {
|
||||
webhookSecret: env.GITHUB_WEBHOOK_SECRET,
|
||||
@@ -80,9 +104,6 @@ export async function loadConfig(env: Env): Promise<Config> {
|
||||
discord: {
|
||||
token: env.DISCORD_TOKEN ?? "",
|
||||
},
|
||||
routes,
|
||||
routes: [...routes],
|
||||
};
|
||||
|
||||
configCache = { config, expiresAt: Date.now() + CONFIG_CACHE_TTL };
|
||||
return config;
|
||||
}
|
||||
@@ -61,6 +61,7 @@ export const routeSchema = v.object({
|
||||
export const groupMemberSchema = v.object({
|
||||
login: v.string(),
|
||||
role: v.picklist(["owner", "admin", "viewer"]),
|
||||
userId: v.optional(v.string()),
|
||||
});
|
||||
|
||||
export const forgeSourceSchema = v.object({
|
||||
@@ -77,6 +78,11 @@ export const groupSchema = v.object({
|
||||
owners: v.optional(v.array(v.string())),
|
||||
providers: v.optional(v.array(v.picklist(["github", "gitea", "gitlab", "custom"]))),
|
||||
installationId: v.optional(v.number()),
|
||||
isolated: v.optional(v.boolean()),
|
||||
instance: v.optional(v.string()),
|
||||
instanceBackfilled: v.optional(v.boolean()),
|
||||
legacyId: v.optional(v.string()),
|
||||
webhookSecret: v.optional(v.nullable(v.string())),
|
||||
emoji: v.optional(v.boolean()),
|
||||
forgeSources: v.optional(v.array(forgeSourceSchema)),
|
||||
lang: v.optional(v.string()),
|
||||
|
||||
+1202
-277
File diff suppressed because it is too large.
Load diff
@@ -51,6 +51,12 @@ export interface Config {
|
||||
token: string;
|
||||
};
|
||||
routes: Route[];
|
||||
/**
|
||||
* Groups with a stored route that could not be read. Their events are
|
||||
* reported as transient failures instead of passing for delivered while a
|
||||
* route that might have matched was silently left out.
|
||||
*/
|
||||
unreadableGroups?: string[];
|
||||
}
|
||||
|
||||
export interface RouteTarget {
|
||||
@@ -138,6 +144,46 @@ export interface Group {
|
||||
* `owners` list. Empty/omitted = no installation restriction.
|
||||
*/
|
||||
installationId?: number;
|
||||
/**
|
||||
* Isolated groups (self-service personal groups) only receive events from
|
||||
* their own `/webhook/{groupId}` ingress; the global endpoint dispatches to
|
||||
* them only once a super admin binds a source (`installationId` or
|
||||
* `owners`). Only super admins may edit this field.
|
||||
*/
|
||||
isolated?: boolean;
|
||||
/**
|
||||
* Immutable identity of this group, assigned by the server when the group is
|
||||
* created and kept across renames. A group re-created under a former id gets
|
||||
* a new one, so invites and delivery ledgers of the old group never apply to
|
||||
* it. Clients cannot set it.
|
||||
*/
|
||||
instance?: string;
|
||||
/**
|
||||
* Set when the group got its `instance` from the one-time backfill, i.e. it
|
||||
* existed before instances: only such a group may adopt state stored under
|
||||
* its id (a pre-instance webhook secret or invite). Server-owned.
|
||||
*/
|
||||
instanceBackfilled?: boolean;
|
||||
/**
|
||||
* For a backfilled group: the id it had when it got its instance. State
|
||||
* stored under a group id before instances (a webhook secret, an invite, a
|
||||
* queued delivery) belongs to the group with that `legacyId` only — not to
|
||||
* whichever group holds the id now (another one may have been renamed to
|
||||
* it). Server-owned, kept across renames.
|
||||
*/
|
||||
legacyId?: string;
|
||||
/**
|
||||
* The group's webhook secret (per-group ingress). Server-owned, stored with
|
||||
* the group so it changes only through a compare-and-swap that re-checks the
|
||||
* writer's role; never sent to clients. `null` in a mutation removes it.
|
||||
*/
|
||||
webhookSecret?: string | null;
|
||||
/**
|
||||
* Opaque revision of the stored group, set on read by the admin API and
|
||||
* echoed back on update so a stale edit is rejected instead of overwriting a
|
||||
* newer one. Never persisted.
|
||||
*/
|
||||
revision?: string;
|
||||
/**
|
||||
* Whether to include emoji in messages sent through this group's routes.
|
||||
* Defaults to true when omitted.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { Env } from "../lib/types";
|
||||
import { initConfigStore } from "../lib/config";
|
||||
|
||||
/**
|
||||
* Bind the D1-backed configuration store before any handler runs, so no
|
||||
* request path (invite acceptance, OAuth callbacks, bot commands, ...) can
|
||||
* fall back to the KV-only store and read or write a configuration that
|
||||
* diverges from D1.
|
||||
*/
|
||||
export default defineEventHandler((event) => {
|
||||
const env = (event.context.cloudflare as { env?: Env } | undefined)?.env;
|
||||
if (env?.KV) initConfigStore(env);
|
||||
});
|
||||
@@ -1,8 +1,10 @@
|
||||
import type { Env } from "../lib/types";
|
||||
import { handleQueueBatch } from "../lib/queue/consumer";
|
||||
import { initConfigStore } from "../lib/config";
|
||||
|
||||
export default defineNitroPlugin((nitroApp) => {
|
||||
nitroApp.hooks.hook("cloudflare:queue", async ({ batch, env }) => {
|
||||
initConfigStore(env as Env);
|
||||
await handleQueueBatch(batch, env as Env);
|
||||
});
|
||||
});
|
||||
+423
-259
@@ -1,33 +1,20 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { d1ConfigStore, type ConfigStore } from "../server/lib/storage/config-store";
|
||||
import {
|
||||
d1ConfigStore,
|
||||
kvConfigStore,
|
||||
ConfigConflictError,
|
||||
UNGROUPED_ID,
|
||||
} from "../server/lib/storage/config-store";
|
||||
import type { Route, Group } from "../server/lib/types";
|
||||
|
||||
interface Stmt {
|
||||
sql: string;
|
||||
bound: unknown[];
|
||||
bind: (...args: unknown[]) => Stmt;
|
||||
all: () => Promise<{ results: Array<Record<string, unknown>> }>;
|
||||
}
|
||||
|
||||
interface FakeDB {
|
||||
prepare: (sql: string) => Stmt;
|
||||
batch: (stmts: Stmt[]) => Promise<unknown[]>;
|
||||
}
|
||||
|
||||
interface FakeDBOptions {
|
||||
/** Whether `d1_routes` has the `position` column (migration 0012). */
|
||||
positionColumn?: boolean;
|
||||
/** How many batches fail with a transient (non-schema) error first. */
|
||||
batchFailures?: number;
|
||||
}
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
|
||||
function route(id: string, groupId: string, overrides: Partial<Route> = {}): Route {
|
||||
return {
|
||||
id,
|
||||
name: `route-${id}`,
|
||||
enabled: true,
|
||||
filters: [],
|
||||
targets: [],
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "c1" }],
|
||||
groupId,
|
||||
stop: false,
|
||||
fallback: false,
|
||||
@@ -35,259 +22,436 @@ function route(id: string, groupId: string, overrides: Partial<Route> = {}): Rou
|
||||
};
|
||||
}
|
||||
|
||||
function group(id: string): Group {
|
||||
return { id, name: `group-${id}`, adminIds: [] };
|
||||
function group(id: string, overrides: Partial<Group> = {}): Group {
|
||||
return { id, name: `group-${id}`, adminIds: [], ...overrides };
|
||||
}
|
||||
|
||||
function createDB(options: FakeDBOptions = {}): {
|
||||
db: FakeDB & D1Database;
|
||||
routesTable: Route[];
|
||||
groupsTable: Group[];
|
||||
queries: string[];
|
||||
batches: Stmt[][];
|
||||
function setup(options: { position?: boolean } = {}): {
|
||||
db: D1Database;
|
||||
sqlite: ReturnType<typeof createSqliteD1>["sqlite"];
|
||||
kv: KVNamespace;
|
||||
kvStore: Map<string, string>;
|
||||
cfg: ReturnType<typeof d1ConfigStore>;
|
||||
} {
|
||||
const routesTable: Route[] = [];
|
||||
const groupsTable: Group[] = [];
|
||||
const queries: string[] = [];
|
||||
const batches: Stmt[][] = [];
|
||||
let batchFailures = options.batchFailures ?? 0;
|
||||
const db: FakeDB = {
|
||||
prepare(sql: string): Stmt {
|
||||
const stmt: Stmt = {
|
||||
sql,
|
||||
bound: [],
|
||||
bind(...args: unknown[]): Stmt {
|
||||
stmt.bound = args;
|
||||
return stmt;
|
||||
},
|
||||
async all(): Promise<{ results: Array<Record<string, unknown>> }> {
|
||||
queries.push(sql);
|
||||
if (sql.includes("position") && !options.positionColumn) {
|
||||
throw new Error("D1_ERROR: no such column: position at offset 33: SQLITE_ERROR");
|
||||
}
|
||||
if (sql.includes("FROM d1_routes")) {
|
||||
return {
|
||||
results: routesTable.map((r) => ({
|
||||
id: r.id,
|
||||
group_id: r.groupId ?? "",
|
||||
name: r.name,
|
||||
enabled: r.enabled ? 1 : 0,
|
||||
filters: JSON.stringify(r.filters),
|
||||
targets: JSON.stringify(r.targets),
|
||||
stop: r.stop ? 1 : 0,
|
||||
fallback: r.fallback ? 1 : 0,
|
||||
discord_role_ids: r.discordRoleIds ? JSON.stringify(r.discordRoleIds) : null,
|
||||
ast: r.ast ? JSON.stringify(r.ast) : null,
|
||||
})),
|
||||
};
|
||||
}
|
||||
if (sql.includes("FROM d1_groups")) {
|
||||
return {
|
||||
results: groupsTable.map((g) => ({
|
||||
id: g.id,
|
||||
name: g.name,
|
||||
data: JSON.stringify(g),
|
||||
version: 1,
|
||||
})),
|
||||
};
|
||||
}
|
||||
return { results: [] };
|
||||
},
|
||||
};
|
||||
return stmt;
|
||||
},
|
||||
async batch(stmts: Stmt[]): Promise<unknown[]> {
|
||||
batches.push(stmts);
|
||||
if (batchFailures > 0) {
|
||||
batchFailures--;
|
||||
throw new Error("D1_ERROR: network connection lost");
|
||||
}
|
||||
if (!options.positionColumn && stmts.some((st) => st.sql.includes("position,"))) {
|
||||
throw new Error("D1_ERROR: table d1_routes has no column named position: SQLITE_ERROR");
|
||||
}
|
||||
return [];
|
||||
},
|
||||
};
|
||||
return { db: db as FakeDB & D1Database, routesTable, groupsTable, queries, batches };
|
||||
}
|
||||
|
||||
/** Bound `(id, position)` pairs of every route upsert in a batch. */
|
||||
function upsertPositions(stmts: Stmt[]): Array<[unknown, unknown]> {
|
||||
return stmts
|
||||
.filter((s) => s.sql.includes("INSERT INTO d1_routes"))
|
||||
.map((s) => [s.bound[0], s.sql.includes("position") ? s.bound[10] : undefined]);
|
||||
}
|
||||
|
||||
function createKV(): { kv: KVNamespace; store: Map<string, string> } {
|
||||
const store = new Map<string, string>();
|
||||
const kv = {
|
||||
store,
|
||||
get: async <T>(key: string, type?: string): Promise<T | null> => {
|
||||
const v = store.get(key);
|
||||
if (v == null) return null;
|
||||
if (type === "json") return JSON.parse(v) as T;
|
||||
return v as unknown as T;
|
||||
},
|
||||
put: async (key: string, value: string): Promise<void> => {
|
||||
store.set(key, value);
|
||||
},
|
||||
delete: async (key: string): Promise<void> => {
|
||||
store.delete(key);
|
||||
},
|
||||
list: async (): Promise<{ keys: unknown[] }> => ({ keys: [] }),
|
||||
};
|
||||
return { kv: kv as unknown as KVNamespace, store };
|
||||
const { db, sqlite } = createSqliteD1({
|
||||
migrations: (name) => options.position !== false || !name.startsWith("0012"),
|
||||
});
|
||||
const { kv, store } = createMemoryKV();
|
||||
return { db, sqlite, kv, kvStore: store, cfg: d1ConfigStore(db, kv) };
|
||||
}
|
||||
|
||||
describe("d1ConfigStore", () => {
|
||||
it("loads empty when D1 and KV are empty", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv } = createKV();
|
||||
const store: ConfigStore = d1ConfigStore(db, kv);
|
||||
expect(await store.loadRoutes()).toEqual([]);
|
||||
expect(await store.loadGroups()).toEqual([]);
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.loadRoutes()).toEqual([]);
|
||||
expect(await cfg.loadGroups()).toEqual([]);
|
||||
});
|
||||
|
||||
it("seeds routes from KV into memory when D1 is empty and caches in KV", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const existing = [route("r1", "g1")];
|
||||
store.set("config:routes", JSON.stringify(existing));
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0].id).toBe("r1");
|
||||
});
|
||||
|
||||
it("reads routes directly from D1 when populated", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
routesTable.push(route("r1", "g1"));
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0].id).toBe("r1");
|
||||
expect(loaded[0].groupId).toBe("g1");
|
||||
});
|
||||
|
||||
it("round-trips routes through D1 + KV cache on save", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const routes = [route("r1", "g1"), route("r2", "g2")];
|
||||
await cfg.saveRoutes(routes);
|
||||
expect(routesTable).toHaveLength(0);
|
||||
const cached = store.get("config:routes");
|
||||
expect(cached).toBe(JSON.stringify(routes));
|
||||
const reloaded = await cfg.loadRoutes();
|
||||
expect(reloaded).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("round-trips groups through D1 + KV cache on save", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const groups = [group("g1"), group("g2")];
|
||||
await cfg.saveGroups(groups);
|
||||
expect(store.get("config:groups")).toBe(JSON.stringify(groups));
|
||||
const reloaded = await cfg.loadGroups();
|
||||
expect(reloaded).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("invalidateCache forces a reload", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
routesTable.push(route("r1", "g1"));
|
||||
const first = await cfg.loadRoutes();
|
||||
expect(first).toHaveLength(1);
|
||||
routesTable.push(route("r2", "g2"));
|
||||
const cached = await cfg.loadRoutes();
|
||||
expect(cached).toHaveLength(1);
|
||||
cfg.invalidateCache();
|
||||
const second = await cfg.loadRoutes();
|
||||
expect(second).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("persists the per-group route order when the position column exists", async () => {
|
||||
const { db, batches } = createDB({ positionColumn: true });
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.saveRoutes([route("b", "g1"), route("x", "g2"), route("a", "g1")]);
|
||||
expect(upsertPositions(batches[0]!)).toEqual([
|
||||
["b", 0],
|
||||
["x", 0],
|
||||
["a", 1],
|
||||
]);
|
||||
});
|
||||
|
||||
it("loads routes ordered by group and position when the column exists", async () => {
|
||||
const { db, queries } = createDB({ positionColumn: true });
|
||||
const { kv } = createKV();
|
||||
await d1ConfigStore(db, kv).loadRoutes();
|
||||
expect(queries.some((q) => q.includes("FROM d1_routes ORDER BY group_id, position, id"))).toBe(
|
||||
true,
|
||||
it("seeds D1 once from the legacy KV configuration", async () => {
|
||||
const { cfg, kvStore, sqlite } = setup();
|
||||
kvStore.set("config:groups", JSON.stringify([group("g1")]));
|
||||
kvStore.set(
|
||||
"config:routes",
|
||||
JSON.stringify([
|
||||
route("r1", "g1"),
|
||||
// Legacy shape: a single `target` and no group.
|
||||
{ ...route("r2", ""), groupId: undefined, targets: undefined, target: { channelId: "x" } },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to the legacy id order and inserts on an unmigrated database", async () => {
|
||||
const { db, queries, batches } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("FROM d1_routes ORDER BY id");
|
||||
await cfg.saveRoutes([route("r1", "g1")]);
|
||||
expect(upsertPositions(batches.at(-1)!)).toEqual([["r1", undefined]]);
|
||||
});
|
||||
|
||||
it("picks up the position column once the database is migrated", async () => {
|
||||
const options: FakeDBOptions = {};
|
||||
const { db, queries } = createDB(options);
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("ORDER BY id");
|
||||
options.positionColumn = true;
|
||||
cfg.invalidateCache();
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("ORDER BY group_id, position, id");
|
||||
});
|
||||
|
||||
it("never retries without positions when D1 fails for another reason", async () => {
|
||||
const { db, batches } = createDB({ positionColumn: true, batchFailures: 1 });
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const routes = [route("b", "g1"), route("a", "g1")];
|
||||
await cfg.saveRoutes(routes);
|
||||
// Only the positioned batch was attempted; the save took the existing
|
||||
// "D1 unavailable" path instead of writing rows that lose the order.
|
||||
expect(batches).toHaveLength(1);
|
||||
expect(upsertPositions(batches[0]!)).toEqual([
|
||||
["b", 0],
|
||||
["a", 1],
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded.map((r) => [r.id, r.groupId])).toEqual([
|
||||
["r1", "g1"],
|
||||
["r2", UNGROUPED_ID],
|
||||
]);
|
||||
expect(store.get("config:routes")).toBe(JSON.stringify(routes));
|
||||
// The legacy single target was migrated before it reached D1.
|
||||
expect(loaded[1]!.targets).toEqual([{ channelId: "x" }]);
|
||||
expect((await cfg.loadGroups()).map((g) => g.id)).toEqual(["g1", UNGROUPED_ID]);
|
||||
expect(kvStore.get("config:d1-seeded")).toBe("1");
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_routes").get()).toEqual({ n: 2 });
|
||||
});
|
||||
|
||||
it("updating groups does not cascade-delete routes", async () => {
|
||||
const { db, routesTable, groupsTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
it("never resurrects a configuration that was emptied on purpose", async () => {
|
||||
const { cfg, kvStore } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
// A stale legacy copy is still in KV.
|
||||
kvStore.set("config:routes", JSON.stringify([route("old", "g1")]));
|
||||
await cfg.saveGroupRoutes("g1", []);
|
||||
await cfg.deleteGroup("g1");
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toEqual([]);
|
||||
expect(await cfg.loadGroups({ fresh: true })).toEqual([]);
|
||||
});
|
||||
|
||||
// Setup initial state with groups and routes
|
||||
groupsTable.push(group("g1"), group("g2"));
|
||||
routesTable.push(route("r1", "g1"), route("r2", "g2"));
|
||||
it("saving one group's routes leaves every other group untouched", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("b1", "b"), route("b2", "b")]);
|
||||
// An admin of "a" saves from a snapshot taken before "b" changed.
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a", { name: "renamed" })]);
|
||||
const all = await cfg.loadRoutes({ fresh: true });
|
||||
expect(all.map((r) => `${r.groupId}/${r.id}/${r.name}`)).toEqual([
|
||||
"a/a1/renamed",
|
||||
"b/b1/route-b1",
|
||||
"b/b2/route-b2",
|
||||
]);
|
||||
});
|
||||
|
||||
// Simulate updating groups (e.g., changing a group's name)
|
||||
const updatedGroups = [{ ...group("g1"), name: "Updated Group 1" }, group("g2")];
|
||||
it("saveRoutes rewrites only the groups whose list changed", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("b1", "b")]);
|
||||
sqlite.exec("UPDATE d1_routes SET updated_at = 1 WHERE group_id = 'b'");
|
||||
await cfg.saveRoutes([route("a1", "a", { enabled: false }), route("b1", "b")]);
|
||||
expect(sqlite.query("SELECT updated_at FROM d1_routes WHERE group_id = 'b'").get()).toEqual({
|
||||
updated_at: 1,
|
||||
});
|
||||
const a = (await cfg.loadRoutes({ fresh: true })).find((r) => r.id === "a1");
|
||||
expect(a!.enabled).toBe(false);
|
||||
});
|
||||
|
||||
// This should not delete routes
|
||||
await cfg.saveGroups(updatedGroups);
|
||||
it("persists and loads the per-group route order", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("b", "g1"), route("c", "g1"), route("a", "g1")]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["b", "c", "a"]);
|
||||
});
|
||||
|
||||
// Routes should still exist in the table
|
||||
// Note: In the fake DB, batch() doesn't actually execute the statements,
|
||||
// so we can't verify the actual deletion behavior here.
|
||||
// This test mainly ensures saveGroups doesn't throw an error.
|
||||
expect(routesTable).toHaveLength(2);
|
||||
it("works on a database without the position column", async () => {
|
||||
const { cfg } = setup({ position: false });
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("b", "g1"), route("a", "g1")]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("serves cached routes until invalidated, fresh reads bypass the cache", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
expect(await cfg.loadRoutes()).toHaveLength(1);
|
||||
sqlite.exec("DELETE FROM d1_routes");
|
||||
expect(await cfg.loadRoutes()).toHaveLength(1);
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toHaveLength(0);
|
||||
cfg.invalidateCache();
|
||||
expect(await cfg.loadRoutes()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("refuses to create or rename onto an id retired after the caller checked it", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
// A concurrent delete/rename retired "x" between the check and the write.
|
||||
sqlite.exec(
|
||||
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES ('group-retired:x', 0, ${Date.now() + 60_000})`,
|
||||
);
|
||||
expect(await cfg.createGroup(group("x"))).toBe(false);
|
||||
await expect(cfg.renameGroup("a", "x")).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a"]);
|
||||
});
|
||||
|
||||
it("counts a group whose own row is unreadable — and has routes — as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.createGroup(group("g2"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
sqlite.exec("UPDATE d1_groups SET data = 'null' WHERE id IN ('g1', 'g2')");
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("counts a stored regex the engine cannot run as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
// Valid before the switch to RE2: lookahead now fails to compile.
|
||||
sqlite.exec(
|
||||
`UPDATE d1_routes SET filters = '[{"type":"repo","match":"/^secret(?=/)/","exclude":true}]' WHERE id = 'r1'`,
|
||||
);
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("keeps consulting KV state written before D1 when seeding from it", async () => {
|
||||
const { cfg, kvStore } = setup();
|
||||
kvStore.set("config:groups", JSON.stringify([group("g1")]));
|
||||
await cfg.loadGroups({ fresh: true });
|
||||
expect(kvStore.has("storage:kv-fallback")).toBe(true);
|
||||
});
|
||||
|
||||
it("counts a route with a filter of the wrong shape as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
sqlite.exec(`UPDATE d1_routes SET filters = '[{"type":"event","match":{}}]' WHERE id = 'r1'`);
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.routes).toEqual([]);
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("refuses to replace a group's routes while one of its rows is unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1"), route("r2", "g1")]);
|
||||
sqlite.exec("UPDATE d1_routes SET targets = '{broken' WHERE id = 'r2'");
|
||||
// Reads skip it, but a save would silently delete it: refused instead.
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["r1"]);
|
||||
await expect(cfg.saveGroupRoutes("g1", [route("r1", "g1")])).rejects.toThrow(/unreadable/);
|
||||
await expect(cfg.saveRoutes([route("r1", "g1", { name: "renamed" })])).rejects.toThrow(
|
||||
/unreadable/,
|
||||
);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_routes").get()).toEqual({ n: 2 });
|
||||
});
|
||||
|
||||
it("renames a group with its routes and fragments in one transaction", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("old", { name: "Team" }));
|
||||
await cfg.saveGroupRoutes("old", [route("r1", "old"), route("r2", "old")]);
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f1', 'old', 'F', '{}', 1, 1)",
|
||||
);
|
||||
await cfg.renameGroup("old", "new");
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => [g.id, g.name])).toEqual([
|
||||
["new", "Team"],
|
||||
]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => `${r.groupId}/${r.id}`)).toEqual([
|
||||
"new/r1",
|
||||
"new/r2",
|
||||
]);
|
||||
expect(sqlite.query("SELECT group_id FROM d1_fragments").all()).toEqual([{ group_id: "new" }]);
|
||||
});
|
||||
|
||||
it("a failed rename changes nothing", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await expect(cfg.renameGroup("a", "b")).rejects.toThrow();
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.groupId)).toEqual(["a"]);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("deleting a group removes its routes and fragments only", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("r1", "b")]);
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f1', 'a', 'F', '{}', 1, 1)",
|
||||
);
|
||||
await cfg.deleteGroup("a");
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.groupId)).toEqual(["b"]);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_fragments").get()).toEqual({ n: 0 });
|
||||
});
|
||||
|
||||
it("saveGroups writes only changed groups and deletes the missing ones", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
sqlite.exec("UPDATE d1_groups SET updated_at = 1");
|
||||
await cfg.saveGroups([group("a"), group("c")]);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a", "c"]);
|
||||
expect(sqlite.query("SELECT updated_at FROM d1_groups WHERE id = 'a'").get()).toEqual({
|
||||
updated_at: 1,
|
||||
});
|
||||
// Updating a group never touches its routes.
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("createGroup never overwrites an existing group", async () => {
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.createGroup(group("g", { name: "first" }))).toBe(true);
|
||||
expect(await cfg.createGroup(group("g", { name: "second" }))).toBe(false);
|
||||
expect((await cfg.loadGroups({ fresh: true }))[0]!.name).toBe("first");
|
||||
});
|
||||
|
||||
it("updateGroup is a compare-and-swap that retries on contention", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g", { name: "v0" }));
|
||||
let calls = 0;
|
||||
const updated = await cfg.updateGroup("g", (g) => {
|
||||
calls++;
|
||||
// A concurrent writer bumps the row between our read and our write once.
|
||||
if (calls === 1)
|
||||
sqlite.exec(
|
||||
"UPDATE d1_groups SET version = version + 1, data = json_set(data, '$.name', 'other')",
|
||||
);
|
||||
return { ...g, name: `${g.name}+mine` };
|
||||
});
|
||||
expect(calls).toBe(2);
|
||||
expect(updated!.name).toBe("other+mine");
|
||||
expect((await cfg.loadGroups({ fresh: true }))[0]!.name).toBe("other+mine");
|
||||
});
|
||||
|
||||
it("updateGroup gives up with a conflict when contention never settles", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await expect(
|
||||
cfg.updateGroup("g", (g) => {
|
||||
sqlite.exec("UPDATE d1_groups SET version = version + 1");
|
||||
return { ...g, name: "x" };
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
});
|
||||
|
||||
it("updateGroup returns null for a missing group and keeps a group on null", async () => {
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.updateGroup("missing", (g) => g)).toBeNull();
|
||||
await cfg.createGroup(group("g"));
|
||||
expect((await cfg.updateGroup("g", () => null))!.id).toBe("g");
|
||||
});
|
||||
|
||||
it("never stores a client revision", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup({ ...group("g"), revision: "abc" });
|
||||
const row = sqlite.query("SELECT data FROM d1_groups").get() as { data: string };
|
||||
expect(JSON.parse(row.data).revision).toBeUndefined();
|
||||
});
|
||||
|
||||
it("propagates D1 write failures instead of pretending to save", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
await expect(cfg.saveGroupRoutes("g", [route("r1", "g")])).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("propagates D1 read failures instead of serving any stale copy", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await cfg.saveGroupRoutes("g", [route("r1", "g")]);
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
cfg.invalidateCache();
|
||||
await expect(cfg.loadRoutes()).rejects.toThrow();
|
||||
await expect(cfg.loadGroups()).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("gives every group an immutable instance id that survives renames", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
sqlite.exec(
|
||||
'INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (\'legacy\', \'L\', \'{"id":"legacy","name":"L","adminIds":[]}\', 1, 0, 0)',
|
||||
);
|
||||
await cfg.createGroup({ ...group("fresh"), instance: "client-chosen" });
|
||||
const loaded = await cfg.loadGroups({ fresh: true });
|
||||
const legacy = loaded.find((g) => g.id === "legacy");
|
||||
const fresh = loaded.find((g) => g.id === "fresh");
|
||||
expect(legacy!.instance).toMatch(/^[0-9a-f]{32}$/);
|
||||
expect(fresh!.instance).not.toBe("client-chosen");
|
||||
await cfg.updateGroup("fresh", (g) => ({ ...g, instance: "tampered" }));
|
||||
const renamed = await cfg.renameGroup("fresh", "moved");
|
||||
expect(renamed.instance).toBe(fresh!.instance);
|
||||
await cfg.deleteGroup("moved");
|
||||
await cfg.createGroup(group("moved"));
|
||||
const again = (await cfg.loadGroups({ fresh: true })).find((g) => g.id === "moved")!;
|
||||
expect(again.instance).not.toBe(fresh!.instance);
|
||||
});
|
||||
|
||||
it("refuses a delete when the group changed after it was checked", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await expect(
|
||||
cfg.deleteGroup("g", {
|
||||
expect: async () => {
|
||||
await cfg.updateGroup("g", (x) => ({ ...x, name: "changed" }));
|
||||
},
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.name)).toEqual(["changed"]);
|
||||
});
|
||||
|
||||
it("refuses routes for a group that does not exist", async () => {
|
||||
const { cfg } = setup();
|
||||
await expect(cfg.saveGroupRoutes("ghost", [route("r1", "ghost")])).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("kvConfigStore", () => {
|
||||
it("keeps groups and routes in permanent KV keys", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const cfg = kvConfigStore(kv);
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await cfg.renameGroup("a", "b");
|
||||
expect((await cfg.loadGroups()).map((g) => g.id)).toEqual(["b"]);
|
||||
expect((await cfg.loadRoutes()).map((r) => r.groupId)).toEqual(["b"]);
|
||||
await cfg.deleteGroup("b");
|
||||
expect(JSON.parse(store.get("config:routes")!)).toEqual([]);
|
||||
});
|
||||
|
||||
it("backfills the same instance whichever cold start wins the race", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([group("legacy")]));
|
||||
// Two isolates read the legacy list before either wrote it back.
|
||||
const [a, b] = await Promise.all([
|
||||
kvConfigStore(kv).loadGroups(),
|
||||
kvConfigStore(kv).loadGroups(),
|
||||
]);
|
||||
expect(a[0]!.instance).toBeTruthy();
|
||||
expect(a[0]!.instance).toBe(b[0]!.instance);
|
||||
expect(a[0]!.instanceBackfilled).toBe(true);
|
||||
// A rename keeps the identity.
|
||||
const cfg = kvConfigStore(kv);
|
||||
const moved = await cfg.renameGroup("legacy", "renamed", { transform: (g) => ({ ...g }) });
|
||||
expect(moved.instance).toBe(a[0]!.instance);
|
||||
expect(moved.instanceBackfilled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("d1ConfigStore guarded writes", () => {
|
||||
it("lets only one of two writes from the same base commit", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((r) => (release = r));
|
||||
const slow = cfg.saveGroupRoutes("g", [route("a", "g")], { expect: () => gate });
|
||||
// A second write sneaks in while the first waits between check and write.
|
||||
await cfg.saveGroupRoutes("g", [route("b", "g")]);
|
||||
release();
|
||||
await expect(slow).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["b"]);
|
||||
});
|
||||
|
||||
it("refuses a rename whose source changed after it was read", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("old", { name: "v1" }));
|
||||
await expect(
|
||||
cfg.renameGroup("old", "new", {
|
||||
expect: async () => {
|
||||
await cfg.updateGroup("old", (g) => ({ ...g, name: "v2" }));
|
||||
},
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => [g.id, g.name])).toEqual([
|
||||
["old", "v2"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not seed (nor mark seeded) when the legacy KV read fails", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:routes", JSON.stringify([route("r1", "g1")]));
|
||||
const failing = {
|
||||
...kv,
|
||||
get: async (key: string, type?: string) => {
|
||||
if (key === "config:groups") throw new Error("KV down");
|
||||
return kv.get(key, type as never);
|
||||
},
|
||||
} as unknown as KVNamespace;
|
||||
const cfg = d1ConfigStore(db, failing);
|
||||
await expect(cfg.loadRoutes({ fresh: true })).rejects.toThrow("KV down");
|
||||
expect(store.get("config:d1-seeded")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("fails instead of serving an empty configuration when D1 and the copy are missing", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const { kv } = createMemoryKV();
|
||||
const cfg = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
cfg.invalidateCache();
|
||||
await kv.delete("config:cache:routes");
|
||||
await expect(cfg.loadRoutes()).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user