Open History to see the last commit
1/**
2 * Real Electron main process for the persisted demand coverage witness
3 * (`tests/persisted-demand-coverage.electron.e2e.test.ts`). It serves the real
4 * `exposeElectronSQLitePersistence` handler over the real `ipcMain`, with the
5 * real Node persistence over a `node:sqlite` connection. Electron's ABI cannot
6 * load better-sqlite3's native module, so a small adapter gives `node:sqlite`
7 * the better-sqlite3 surface the Node driver uses (the same adapter the reload
8 * class fixture uses). The renderer asks for one database file per history
9 * (`demand:session`), so each history reads and writes its own file.
10 */
11import { DatabaseSync } from 'node:sqlite'
12import { BrowserWindow, app, ipcMain } from 'electron'
13import { createNodeSQLitePersistence } from '../../../../node-db-sqlite-persistence/src'
14import { exposeElectronSQLitePersistence } from '../../../src/main'
15import type { BetterSqlite3Database } from '../../../../node-db-sqlite-persistence/src'
16
17export const RESULT_PREFIX = `__TANSTACK_DB_DEMAND_COVERAGE__:`
18
19function binding(value: unknown): string | number | bigint | null {
20 if (value == null) return null
21 if (typeof value === `boolean`) return value ? 1 : 0
22 if (
23 typeof value === `string` ||
24 typeof value === `number` ||
25 typeof value === `bigint`
26 )
27 return value
28 return String(value)
29}
30
31// The better-sqlite3 surface that BetterSqlite3SQLiteDriver uses.
32function betterSqliteSurface(database: DatabaseSync): BetterSqlite3Database {
33 return {
34 prepare: (sql: string) => {
35 const statement = database.prepare(sql)
36 return {
37 all: (...params: Array<unknown>) =>
38 statement.all(...params.map(binding)).map((row) => ({ ...row })),
39 run: (...params: Array<unknown>) =>
40 statement.run(...params.map(binding)),
41 }
42 },
43 exec: (sql: string) => {
44 database.exec(sql)
45 },
46 close: () => database.close(),
47 } as unknown as BetterSqlite3Database
48}
49
50let window: BrowserWindow | undefined
51let session: { close: () => void } | undefined
52
53// One session per history file: the previous session closes first, so the
54// renderer's next session is a fresh main-process persistence over that file.
55ipcMain.handle(`demand:session`, (_event, filename: string) => {
56 session?.close()
57 const database = new DatabaseSync(filename)
58 const dispose = exposeElectronSQLitePersistence({
59 ipcMain,
60 persistence: createNodeSQLitePersistence({
61 database: betterSqliteSurface(database),
62 }),
63 })
64 session = {
65 close: () => {
66 dispose()
67 database.close()
68 },
69 }
70})
71ipcMain.handle(`demand:close-session`, () => {
72 session?.close()
73 session = undefined
74})
75// Quit only after stdout has flushed: a result can be larger than the pipe's
76// buffer, and an early quit truncates it.
77ipcMain.handle(`demand:result`, (_event, result: string) => {
78 process.stdout.write(`${RESULT_PREFIX}${result}\n`, () => app.quit())
79})
80
81void app.whenReady().then(() => {
82 window = new BrowserWindow({
83 show: false,
84 webPreferences: {
85 preload: process.env.DEMAND_COVERAGE_PRELOAD,
86 contextIsolation: true,
87 sandbox: false,
88 },
89 })
90 void window.loadFile(process.env.DEMAND_COVERAGE_PAGE!, {
91 query: {
92 phase: process.env.DEMAND_COVERAGE_PHASE!,
93 dir: process.env.DEMAND_COVERAGE_DIR!,
94 },
95 })
96})