Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

demand-coverage-session.mjs

35 lines1.5 KB

1// @ts-nocheck
2/**
3 * Durable Object side of the persisted demand coverage witness
4 * (`e2e/cloudflare-persisted-demand-coverage.e2e.test.ts`). The worker runs
5 * one session per request, against the Durable Object's own storage, through
6 * the real `createCloudflareDOSQLitePersistence`. The test restarts wrangler
7 * between session 1 and session 2, so session 2 reads what workerd persisted.
8 * The histories and the session runners are the receiving contract's
9 * (`packages/db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract.ts`),
10 * imported here into the worker bundle. This module records what it observes;
11 * the test owns the judgment, through the shared judge once it is published.
12 */
13import {
14 receivingHistory,
15 recordSession1,
16 recordSession2,
17} from '../../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract.ts'
18import { createCloudflareDOSQLitePersistence } from '../../dist/esm/index.js'
19​
20// Runs one session. `phase` is `session1` (online, takes the history's steps)
21// or `session2` (offline, returns the record). Errors reject to the caller.
22export async function runDemandCoverageSession(storage, body) {
23 const persistence = createCloudflareDOSQLitePersistence({ storage })
24 const history = receivingHistory(body.history)
25 if (body.phase === `session1`) {
26 return {
27 phase: `session1`,
28 record: await recordSession1(persistence, history),
29 }
30 }
31 return {
32 phase: `session2`,
33 record: await recordSession2(persistence, history),
34 }