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

opfs-request-order.opfs.ts

101 lines3.3 KB

1/**
2 * Chromium driver for the OPFS request-order oracle in
3 * `tests/opfs-request-order-oracle.ts`. It runs every fixed example through
4 * the public connection, the real worker, and real OPFS. It takes the access
5 * handle with OPFSCoopSyncVFS's own handoff: notify the handle's
6 * BroadcastChannel, then hold its Web Lock. Chromium's lock inventory shows
7 * when the connection's request waits for that lock.
8 */
9import { openBrowserWASQLiteOPFSDatabase } from '../src/index'
10import {
11 findRequestOrderViolation,
12 observeRequestOrder,
13 requestOrderExamples,
14} from '../tests/opfs-request-order-oracle'
15import type {
16 AccessHandleControl,
17 RequestOrderHistory,
18 RequestOrderObservation,
19} from '../tests/opfs-request-order-oracle'
20​
21export type OPFSRequestOrderResult =
22 | {
23 status: `complete`
24 results: Array<{
25 history: RequestOrderHistory
26 observation: RequestOrderObservation
27 violation: string | undefined
28 }>
29 }
30 | { status: `failed-before-checkpoint`; primaryFailure: string }
31​
32declare global {
33 interface Window {
34 __tanstackOPFSRequestOrderOracle?: OPFSRequestOrderResult
35 }
37​
38function chromiumHandleControl(databaseName: string): AccessHandleControl {
39 const lockName = `ahp:/${databaseName}`
40 let release: (() => void) | undefined
41 return {
42 take: () =>
43 new Promise<void>((acquired) => {
44 new BroadcastChannel(lockName).postMessage(null)
45 void navigator.locks.request(lockName, () => {
46 acquired()
47 return new Promise<void>((resolve) => {
48 release = resolve
49 })
50 })
51 }),
52 untilWaiting: async () => {
53 const deadline = Date.now() + 10_000
54 while (Date.now() < deadline) {
55 const { pending = [] } = await navigator.locks.query()
56 if (pending.some((lock) => lock.name === lockName)) return true
57 await new Promise((resolve) => setTimeout(resolve, 10))
58 }
59 return false
60 },
61 // A Worker message crosses threads; this bounds the delivery wait.
62 untilDelivered: () => new Promise((resolve) => setTimeout(resolve, 100)),
63 give: () => {
64 release?.()
65 release = undefined
66 return Promise.resolve()
67 },
68 }
70​
71async function run(): Promise<void> {
72 const status = document.querySelector<HTMLOutputElement>(`#oracle-status`)
73 try {
74 const results = []
75 for (const [index, history] of requestOrderExamples.entries()) {
76 // OPFS database names near 63 characters fail to open; keep them short.
77 const databaseName = `ro-${index}-${crypto.randomUUID().slice(0, 8)}.sqlite`
78 const observation = await observeRequestOrder(
79 (name) => openBrowserWASQLiteOPFSDatabase({ databaseName: name }),
80 chromiumHandleControl(databaseName),
81 history,
82 databaseName,
83 )
84 results.push({
85 history,
86 observation,
87 violation: findRequestOrderViolation(history, observation),
88 })
89 }
90 window.__tanstackOPFSRequestOrderOracle = { status: `complete`, results }
91 if (status) status.value = `complete`
92 } catch (error) {
93 window.__tanstackOPFSRequestOrderOracle = {
94 status: `failed-before-checkpoint`,
95 primaryFailure: error instanceof Error ? error.message : String(error),
96 }
97 if (status) status.value = `failed-before-checkpoint`
98 }
100​
101void run()