@pgxsinkit/client 0.1.32 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/boot-report.d.ts +134 -0
- package/dist/boot-report.d.ts.map +1 -0
- package/dist/convergence.d.ts +76 -0
- package/dist/convergence.d.ts.map +1 -0
- package/dist/debug.d.ts +34 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/export-data.d.ts +100 -0
- package/dist/export-data.d.ts.map +1 -0
- package/dist/export-dump.d.ts +92 -0
- package/dist/export-dump.d.ts.map +1 -0
- package/dist/export-store.d.ts +214 -0
- package/dist/export-store.d.ts.map +1 -0
- package/dist/index.d.ts +615 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8083 -2286
- package/dist/index.js.map +81 -44
- package/dist/lazy-guard.d.ts +59 -0
- package/dist/lazy-guard.d.ts.map +1 -0
- package/dist/lifecycle-slot.d.ts +31 -0
- package/dist/lifecycle-slot.d.ts.map +1 -0
- package/dist/live-rows-sql.d.ts +21 -0
- package/dist/live-rows-sql.d.ts.map +1 -0
- package/dist/local-store.d.ts +56 -0
- package/dist/local-store.d.ts.map +1 -0
- package/dist/local-tables.d.ts +707 -0
- package/dist/local-tables.d.ts.map +1 -0
- package/dist/mutation-state.d.ts +69 -0
- package/dist/mutation-state.d.ts.map +1 -0
- package/dist/mutation.d.ts +129 -4
- package/dist/mutation.d.ts.map +1 -1
- package/dist/schema.d.ts +82 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/shape-sync.d.ts +190 -0
- package/dist/shape-sync.d.ts.map +1 -0
- package/dist/store-path.d.ts +132 -0
- package/dist/store-path.d.ts.map +1 -0
- package/dist/sync/apply.d.ts +51 -0
- package/dist/sync/apply.d.ts.map +1 -0
- package/dist/sync/copy.d.ts +43 -0
- package/dist/sync/copy.d.ts.map +1 -0
- package/dist/sync/drizzle-executor.d.ts +5 -0
- package/dist/sync/drizzle-executor.d.ts.map +1 -0
- package/dist/sync/index.d.ts +32 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/metadata-tables.d.ts +131 -0
- package/dist/sync/metadata-tables.d.ts.map +1 -0
- package/dist/sync/nudge.d.ts +49 -0
- package/dist/sync/nudge.d.ts.map +1 -0
- package/dist/sync/shape-inbox.d.ts +215 -0
- package/dist/sync/shape-inbox.d.ts.map +1 -0
- package/dist/sync/subscription-state.d.ts +39 -0
- package/dist/sync/subscription-state.d.ts.map +1 -0
- package/dist/sync/tags.d.ts +130 -0
- package/dist/sync/tags.d.ts.map +1 -0
- package/dist/sync/types.d.ts +126 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/sync-auth.d.ts +53 -0
- package/dist/sync-auth.d.ts.map +1 -0
- package/dist/testing.d.ts +27 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +17 -0
- package/dist/testing.js.map +11 -0
- package/dist/worker/attach-sync-client.d.ts +96 -0
- package/dist/worker/attach-sync-client.d.ts.map +1 -0
- package/dist/worker/define-sync-worker.d.ts +76 -0
- package/dist/worker/define-sync-worker.d.ts.map +1 -0
- package/dist/worker/live-diff.d.ts +44 -0
- package/dist/worker/live-diff.d.ts.map +1 -0
- package/dist/worker/protocol.d.ts +341 -0
- package/dist/worker/protocol.d.ts.map +1 -0
- package/dist/worker/token-cache.d.ts +26 -0
- package/dist/worker/token-cache.d.ts.map +1 -0
- package/oxlint-plugin.js +143 -0
- package/package.json +20 -7
- package/skills/core/SKILL.md +210 -0
- package/skills/operating/SKILL.md +303 -0
- package/src/boot-report.ts +287 -0
- package/src/convergence.ts +197 -0
- package/src/debug.ts +151 -0
- package/src/export-data.ts +300 -0
- package/src/export-dump.ts +239 -0
- package/src/export-store.ts +309 -0
- package/src/index.ts +1652 -51
- package/src/lazy-guard.ts +135 -0
- package/src/lifecycle-slot.ts +66 -0
- package/src/live-rows-sql.ts +45 -0
- package/src/local-store.ts +193 -0
- package/src/local-tables.ts +546 -0
- package/src/mutation-state.ts +99 -0
- package/src/mutation.ts +1910 -562
- package/src/schema.ts +472 -28
- package/src/shape-sync.ts +498 -0
- package/src/store-path.ts +249 -0
- package/src/sync/apply.ts +530 -0
- package/src/sync/copy.ts +206 -0
- package/src/sync/drizzle-executor.ts +48 -0
- package/src/sync/index.ts +1087 -0
- package/src/sync/metadata-tables.ts +67 -0
- package/src/sync/nudge.ts +110 -0
- package/src/sync/shape-inbox.ts +566 -0
- package/src/sync/subscription-state.ts +135 -0
- package/src/sync/tags.ts +365 -0
- package/src/sync/types.ts +141 -0
- package/src/sync-auth.ts +117 -0
- package/src/testing.ts +53 -0
- package/src/worker/attach-sync-client.ts +653 -0
- package/src/worker/define-sync-worker.ts +762 -0
- package/src/worker/live-diff.ts +140 -0
- package/src/worker/protocol.ts +425 -0
- package/src/worker/token-cache.ts +84 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// Local store export (ADR-0035). Slice 1 ships the **store backup**: a full-fidelity, PGlite-restorable
|
|
2
|
+
// tarball of the whole local store, taken LIVE (no engine suspension). It is a `CHECKPOINT` — run through
|
|
3
|
+
// the engine's normal query serialisation so it orders behind in-flight engine work — followed by
|
|
4
|
+
// PGlite's `dumpDataDir`. Because the dump is of the datadir itself, the Mutation journal and Overlay
|
|
5
|
+
// travel INSIDE the artefact: a store backup is the lossless option, the only export an offline device
|
|
6
|
+
// with unflushed writes can take (CONTEXT.md → Store backup).
|
|
7
|
+
//
|
|
8
|
+
// Every export resolves to an {@link ExportReport} alongside the artefact, extending the ADR-0034
|
|
9
|
+
// observability rule (the BootReport house style) to exports: a versioned, allocation-light plain object
|
|
10
|
+
// (structured-clones across the worker bridge unchanged) carrying phase timings, a {@link MutationDiagnostics}
|
|
11
|
+
// snapshot, and a `startedAt` epoch anchor with every other value a monotonic offset/duration relative to it.
|
|
12
|
+
|
|
13
|
+
import type { MutationDiagnostics } from "@pgxsinkit/contracts";
|
|
14
|
+
|
|
15
|
+
import type { ClientPGlite } from "./index";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Monotonic clock (ms) — `performance.now()` where available, else `Date.now()` (mirrors BootReport).
|
|
19
|
+
* Exported so the sibling diagnostic-dump path ({@link file://./export-dump.ts}) times its extra phases
|
|
20
|
+
* (clone boot, pg_dump) on the SAME clock as this module's checkpoint/dump pair.
|
|
21
|
+
*/
|
|
22
|
+
export const nowMs = (): number => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
23
|
+
|
|
24
|
+
/** Options for {@link SyncClient.exportStore}. */
|
|
25
|
+
export interface StoreExportOptions {
|
|
26
|
+
/**
|
|
27
|
+
* How to compress the tarball, forwarded to PGlite's `dumpDataDir`. `"auto"` (the default) gzips when a
|
|
28
|
+
* `CompressionStream` is available and falls back to an uncompressed tar otherwise; `"gzip"` forces
|
|
29
|
+
* compression; `"none"` skips it. The report's `compression` records the compression that was actually
|
|
30
|
+
* applied (which, under `"auto"`, is resolved at runtime).
|
|
31
|
+
*/
|
|
32
|
+
compression?: "auto" | "gzip" | "none";
|
|
33
|
+
/**
|
|
34
|
+
* Override the generated artefact file name. When omitted, the name is
|
|
35
|
+
* `<storeId>-<timestamp>.pgdata.tar[.gz]`, where `storeId` is a filesystem-safe derivation of the store
|
|
36
|
+
* path and the extension reflects the applied compression.
|
|
37
|
+
*/
|
|
38
|
+
fileName?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The fields EVERY export report carries, whatever its kind — built in the BootReport house style
|
|
43
|
+
* (ADR-0034): `reportVersion` is a contract number (additive fields keep it, a breaking reshape bumps it);
|
|
44
|
+
* all `*Ms` are milliseconds; `startedAt` is the only wall-clock value and every `*AtMs` is a monotonic
|
|
45
|
+
* offset from it. The per-kind interfaces below add their `kind`/`scope`/`phases` discriminant on top.
|
|
46
|
+
*/
|
|
47
|
+
export interface ExportReportCommon {
|
|
48
|
+
reportVersion: 1;
|
|
49
|
+
/** Epoch anchor (`Date.now()`) at export start; every other duration/offset is monotonic relative to it. */
|
|
50
|
+
startedAt: number;
|
|
51
|
+
/** Export start → artefact ready. */
|
|
52
|
+
totalMs: number;
|
|
53
|
+
/** The artefact's byte length — the size the caller downloads / persists. */
|
|
54
|
+
byteLength: number;
|
|
55
|
+
/**
|
|
56
|
+
* The {@link MutationDiagnostics} snapshot at export time — the journal state captured alongside the
|
|
57
|
+
* artefact (for the store backup, the very journal that travels INSIDE the tarball; for the diagnostic
|
|
58
|
+
* dump, the live store's journal at dump time, whose rows the SQL also carries).
|
|
59
|
+
*/
|
|
60
|
+
diagnostics: MutationDiagnostics;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A live **store backup** (ADR-0035): the whole datadir as a PGlite-restorable tarball, journal and overlay
|
|
65
|
+
* included. `checkpoint`/`dump` are the only phases — a store backup is a `CHECKPOINT` + `dumpDataDir`, no
|
|
66
|
+
* clone. Slice-1 shape, kept unchanged (the union is additive).
|
|
67
|
+
*/
|
|
68
|
+
export interface StoreBackupReport extends ExportReportCommon {
|
|
69
|
+
/** Which export produced this artefact. */
|
|
70
|
+
kind: "store-backup";
|
|
71
|
+
/** What the artefact covers. A store backup is always the whole store, journal and overlay included. */
|
|
72
|
+
scope: "whole-store";
|
|
73
|
+
/** The compression actually applied to the tarball (resolved from `"auto"` at dump time). */
|
|
74
|
+
compression: "gzip" | "none";
|
|
75
|
+
phases: {
|
|
76
|
+
/** Offset from export start when the `CHECKPOINT` began. */
|
|
77
|
+
checkpointStartedAtMs: number;
|
|
78
|
+
/** `CHECKPOINT` wall — flushing dirty buffers to the datadir before it is tarred, serialised behind engine work. */
|
|
79
|
+
checkpointMs: number;
|
|
80
|
+
/** Offset from export start when `dumpDataDir` began. */
|
|
81
|
+
dumpStartedAtMs: number;
|
|
82
|
+
/** `dumpDataDir` wall — reading the datadir out and assembling (optionally compressing) the tarball. */
|
|
83
|
+
dumpMs: number;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A **diagnostic dump** (ADR-0035, via the throwaway clone of the addendum): human-readable SQL of
|
|
89
|
+
* EVERYTHING the store holds — synced tables, the `_overlay`/`_mutations` journal, the `pgxsinkit` metadata
|
|
90
|
+
* schema, the read-model views, and the reconcile functions/triggers. It is a live datadir dump (checkpoint
|
|
91
|
+
* + `dumpDataDir`) fed into a memory-backed throwaway PGlite via `loadDataDir`, against which `pg_dump` runs
|
|
92
|
+
* — so the live engine is never touched (the addendum's whole point). Its phases add the clone boot and the
|
|
93
|
+
* `pg_dump` walls to the shared checkpoint/dump pair.
|
|
94
|
+
*/
|
|
95
|
+
export interface DiagnosticDumpReport extends ExportReportCommon {
|
|
96
|
+
kind: "diagnostic-dump";
|
|
97
|
+
/** A diagnostic dump covers everything the store holds — synced data, journal, metadata, views, functions. */
|
|
98
|
+
scope: "everything";
|
|
99
|
+
phases: {
|
|
100
|
+
/** Offset from export start when the `CHECKPOINT` began. */
|
|
101
|
+
checkpointStartedAtMs: number;
|
|
102
|
+
/** `CHECKPOINT` wall — flushing dirty buffers before the internal datadir dump the clone consumes. */
|
|
103
|
+
checkpointMs: number;
|
|
104
|
+
/** Offset from export start when the internal `dumpDataDir` began. */
|
|
105
|
+
dumpStartedAtMs: number;
|
|
106
|
+
/** `dumpDataDir` wall — the uncompressed internal tarball the throwaway clone boots from (`compression: "none"`). */
|
|
107
|
+
dumpMs: number;
|
|
108
|
+
/** Offset from export start when the throwaway clone's `PGlite.create({ loadDataDir })` began. */
|
|
109
|
+
cloneBootStartedAtMs: number;
|
|
110
|
+
/** Clone boot wall — booting the memory-backed throwaway from the internal dump. */
|
|
111
|
+
cloneBootMs: number;
|
|
112
|
+
/** Offset from export start when `pg_dump` began running against the clone. */
|
|
113
|
+
pgDumpStartedAtMs: number;
|
|
114
|
+
/** `pg_dump` wall — the WASM `pg_dump` reading the clone out to SQL. */
|
|
115
|
+
pgDumpMs: number;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A **data export** (ADR-0035 decision 1, via the throwaway clone of the addendum): the PORTABLE artefact —
|
|
121
|
+
* the synced tables and the enum types they depend on, schema + data, nothing of pgxsinkit's machinery,
|
|
122
|
+
* loadable into a vanilla Postgres. It is a generated enum DDL header concatenated ahead of a `pg_dump -t`
|
|
123
|
+
* (per synced table) `--no-owner` run against the same memory-backed throwaway clone the diagnostic dump
|
|
124
|
+
* uses. Unlike the other two exports it GUARDS: it requires a drained Mutation journal (or the explicit
|
|
125
|
+
* `drainJournal: false` escape hatch), so its phases add the drain wall ahead of the clone pipeline, and it
|
|
126
|
+
* records its provenance (`tables` = the `-t` allowlist actually applied, `escapeHatch` = whether the drain
|
|
127
|
+
* was skipped).
|
|
128
|
+
*/
|
|
129
|
+
export interface DataExportReport extends ExportReportCommon {
|
|
130
|
+
kind: "data-export";
|
|
131
|
+
/** A data export covers the synced tables + their enum types only — never pgxsinkit's overlay/journal/metadata. */
|
|
132
|
+
scope: "synced-tables";
|
|
133
|
+
/**
|
|
134
|
+
* The schema-qualified physical synced tables the `-t` allowlist targeted, in registry order — the
|
|
135
|
+
* self-describing provenance of exactly what the artefact carries (ephemeral/read-projection entries
|
|
136
|
+
* excluded by construction). May be empty when the registry declares no owning persistent table.
|
|
137
|
+
*/
|
|
138
|
+
tables: string[];
|
|
139
|
+
/**
|
|
140
|
+
* `true` when `drainJournal: false` skipped the drain and the artefact reflects the SYNCED state as-is
|
|
141
|
+
* (unflushed local writes silently absent). `false` on the strict path (a drained or empty journal).
|
|
142
|
+
*/
|
|
143
|
+
escapeHatch: boolean;
|
|
144
|
+
phases: {
|
|
145
|
+
/** Offset from export start when the drain guard began (`0` when the escape hatch skipped it). */
|
|
146
|
+
drainStartedAtMs: number;
|
|
147
|
+
/** Drain wall — flushing + awaiting the journal reach fully-drained (`0` under the escape hatch). */
|
|
148
|
+
drainMs: number;
|
|
149
|
+
/** Offset from export start when the `CHECKPOINT` began. */
|
|
150
|
+
checkpointStartedAtMs: number;
|
|
151
|
+
/** `CHECKPOINT` wall — flushing dirty buffers before the internal datadir dump the clone consumes. */
|
|
152
|
+
checkpointMs: number;
|
|
153
|
+
/** Offset from export start when the internal `dumpDataDir` began. */
|
|
154
|
+
dumpStartedAtMs: number;
|
|
155
|
+
/** `dumpDataDir` wall — the uncompressed internal tarball the throwaway clone boots from (`compression: "none"`). */
|
|
156
|
+
dumpMs: number;
|
|
157
|
+
/** Offset from export start when the throwaway clone's `PGlite.create({ loadDataDir })` began. */
|
|
158
|
+
cloneBootStartedAtMs: number;
|
|
159
|
+
/** Clone boot wall — booting the memory-backed throwaway from the internal dump. */
|
|
160
|
+
cloneBootMs: number;
|
|
161
|
+
/** Offset from export start when `pg_dump -t` began running against the clone. */
|
|
162
|
+
pgDumpStartedAtMs: number;
|
|
163
|
+
/** `pg_dump -t` wall — the WASM `pg_dump` reading the allowlisted tables out to SQL. */
|
|
164
|
+
pgDumpMs: number;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A structured, versioned record of one local-store export (ADR-0035) — a discriminated union on `kind`.
|
|
170
|
+
* Every member shares {@link ExportReportCommon}; the `kind`/`scope`/`phases` discriminant tells the three
|
|
171
|
+
* exports apart. `exportStore` resolves a {@link StoreBackupReport}; `exportDiagnostics` a
|
|
172
|
+
* {@link DiagnosticDumpReport}; `exportData` a {@link DataExportReport}.
|
|
173
|
+
*/
|
|
174
|
+
export type ExportReport = StoreBackupReport | DiagnosticDumpReport | DataExportReport;
|
|
175
|
+
|
|
176
|
+
/** The artefact + its report — the resolved value of {@link SyncClient.exportStore}. */
|
|
177
|
+
export interface StoreExportResult {
|
|
178
|
+
/** The store-backup tarball as a named `File` (PGlite-restorable via `loadDataDir`, a later slice). */
|
|
179
|
+
file: File;
|
|
180
|
+
/** The structured record of the export (ADR-0035). */
|
|
181
|
+
report: StoreBackupReport;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Reduce a plain store PATH (ADR-0036) to a filesystem-safe token for the backup file name. Takes the
|
|
186
|
+
* path's LAST segment (as PGlite's own `dumpDataDir` does when it names the inner db), then keeps only
|
|
187
|
+
* `[A-Za-z0-9._-]`. No scheme stripping — the store path is a plain name, never a storage URL. Falls back
|
|
188
|
+
* to a fixed token for an empty path so the name is always well formed.
|
|
189
|
+
*/
|
|
190
|
+
export function deriveStoreId(storePath: string | undefined): string {
|
|
191
|
+
if (!storePath) return "pgxsinkit-store";
|
|
192
|
+
const lastSegment = storePath.split("/").filter(Boolean).pop() ?? storePath;
|
|
193
|
+
const safe = lastSegment.replace(/[^A-Za-z0-9._-]/g, "-").replace(/^-+|-+$/g, "");
|
|
194
|
+
return safe.length > 0 ? safe : "pgxsinkit-store";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* An ISO-ish, filesystem-safe timestamp (colons/dots → hyphens) for a default artefact file name. Exported
|
|
199
|
+
* so the diagnostic-dump path names its `.sql` artefact with the identical timestamp shape.
|
|
200
|
+
*/
|
|
201
|
+
export const compactTimestamp = (): string => new Date().toISOString().replace(/[:.]/g, "-");
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The datadir dump both exports share (ADR-0035): a `CHECKPOINT` through the store's normal query path,
|
|
205
|
+
* then `dumpDataDir`. The store backup keeps its bytes as the artefact; the diagnostic dump feeds them to a
|
|
206
|
+
* throwaway clone. Factored out so exactly one implementation of "flush + tar the live datadir" exists —
|
|
207
|
+
* `performStoreExport` and `performDiagnosticExport` cannot drift on the checkpoint ordering or the timing
|
|
208
|
+
* house style. Returns the raw `dumpDataDir` output plus the two phase walls, both offset from `startPerf`
|
|
209
|
+
* (the caller's export-start monotonic anchor) so the timings compose into either report.
|
|
210
|
+
*
|
|
211
|
+
* The `CHECKPOINT` is a utility statement — Drizzle has no builder for it, so a raw `exec` is the justified
|
|
212
|
+
* tier-③ form here. Running it via `pglite.exec` serialises it behind any in-flight engine work on PGlite's
|
|
213
|
+
* single connection, flushing dirty buffers to the datadir the dump then reads — so the tarball reflects
|
|
214
|
+
* committed state, not a torn mid-write datadir.
|
|
215
|
+
*/
|
|
216
|
+
export async function performDatadirDump(
|
|
217
|
+
pglite: Pick<ClientPGlite, "exec" | "dumpDataDir">,
|
|
218
|
+
compression: "auto" | "gzip" | "none",
|
|
219
|
+
startPerf: number,
|
|
220
|
+
): Promise<{
|
|
221
|
+
dumped: Awaited<ReturnType<ClientPGlite["dumpDataDir"]>>;
|
|
222
|
+
checkpointStartedAtMs: number;
|
|
223
|
+
checkpointMs: number;
|
|
224
|
+
dumpStartedAtMs: number;
|
|
225
|
+
dumpMs: number;
|
|
226
|
+
}> {
|
|
227
|
+
const checkpointStartedAtMs = nowMs() - startPerf;
|
|
228
|
+
const checkpointStartPerf = nowMs();
|
|
229
|
+
await pglite.exec("CHECKPOINT");
|
|
230
|
+
const checkpointMs = nowMs() - checkpointStartPerf;
|
|
231
|
+
|
|
232
|
+
const dumpStartedAtMs = nowMs() - startPerf;
|
|
233
|
+
const dumpStartPerf = nowMs();
|
|
234
|
+
const dumped = await pglite.dumpDataDir(compression);
|
|
235
|
+
const dumpMs = nowMs() - dumpStartPerf;
|
|
236
|
+
|
|
237
|
+
return { dumped, checkpointStartedAtMs, checkpointMs, dumpStartedAtMs, dumpMs };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The dependencies {@link performStoreExport} needs from the owning client — narrow, so it is unit-testable. */
|
|
241
|
+
export interface StoreExportDeps {
|
|
242
|
+
/** The live store to checkpoint and dump. */
|
|
243
|
+
pglite: Pick<ClientPGlite, "exec" | "dumpDataDir">;
|
|
244
|
+
/** The Mutation diagnostics seam (`client.diagnostics().mutation` / `readMutationStats`). */
|
|
245
|
+
readMutationStats: () => Promise<MutationDiagnostics>;
|
|
246
|
+
/**
|
|
247
|
+
* The store's configured plain store PATH (ADR-0036) — reduced to the `storeId` in the default backup
|
|
248
|
+
* file name (`deriveStoreId`). The resolved PGlite dataDir URL is deliberately NOT used here: it is
|
|
249
|
+
* internal plumbing and must not leak into an artefact name as something to imitate.
|
|
250
|
+
*/
|
|
251
|
+
storePath?: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Run a live store backup (ADR-0035): checkpoint → dump → snapshot diagnostics → assemble the report. The
|
|
256
|
+
* caller (`createSyncClient`) is responsible for awaiting engine-ready and entering the lifecycle slot
|
|
257
|
+
* BEFORE calling this — kept out of here so the helper stays a pure "do the dump" unit. No engine
|
|
258
|
+
* suspension: the backup is live by design.
|
|
259
|
+
*/
|
|
260
|
+
export async function performStoreExport(
|
|
261
|
+
deps: StoreExportDeps,
|
|
262
|
+
options: StoreExportOptions = {},
|
|
263
|
+
): Promise<StoreExportResult> {
|
|
264
|
+
const startedAt = Date.now();
|
|
265
|
+
const startPerf = nowMs();
|
|
266
|
+
const compression = options.compression ?? "auto";
|
|
267
|
+
|
|
268
|
+
// Live dump of the whole datadir (checkpoint → `dumpDataDir`, the shared core) — the journal and overlay
|
|
269
|
+
// ride inside it (the lossless backup, ADR-0035).
|
|
270
|
+
const { dumped, checkpointStartedAtMs, checkpointMs, dumpStartedAtMs, dumpMs } = await performDatadirDump(
|
|
271
|
+
deps.pglite,
|
|
272
|
+
compression,
|
|
273
|
+
startPerf,
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// PGlite names its dump `<db>.tar.gz` (type `application/x-gzip`) when it compressed, `<db>.tar`
|
|
277
|
+
// (`application/x-tar`) otherwise — so the RESULT, not the requested option, tells us what `"auto"`
|
|
278
|
+
// resolved to. Detect from whichever of name/type the runtime populated (its `File` is a polyfill in
|
|
279
|
+
// non-browser hosts, so trust both).
|
|
280
|
+
const dumpedName = dumped instanceof File ? dumped.name : "";
|
|
281
|
+
const gzipped = dumpedName.endsWith(".gz") || dumped.type.includes("gzip");
|
|
282
|
+
const appliedCompression: "gzip" | "none" = gzipped ? "gzip" : "none";
|
|
283
|
+
const extension = gzipped ? ".pgdata.tar.gz" : ".pgdata.tar";
|
|
284
|
+
const mimeType = gzipped ? "application/x-gzip" : "application/x-tar";
|
|
285
|
+
const storeId = deriveStoreId(deps.storePath);
|
|
286
|
+
const fileName = options.fileName ?? `${storeId}-${compactTimestamp()}${extension}`;
|
|
287
|
+
// Build the artefact `File` from the raw bytes, NOT by re-wrapping `dumped`: PGlite's dump is a File
|
|
288
|
+
// polyfill in non-browser hosts, and `new File([polyfillFile], name)` keeps the polyfill's own name
|
|
289
|
+
// rather than the one we pass. Extracting the bytes first guarantees the store-scoped name sticks.
|
|
290
|
+
const bytes = new Uint8Array(await dumped.arrayBuffer());
|
|
291
|
+
const file = new File([bytes], fileName, { type: mimeType });
|
|
292
|
+
|
|
293
|
+
// Snapshot diagnostics AFTER the dump so it describes the journal state the artefact actually holds.
|
|
294
|
+
const diagnostics = await deps.readMutationStats();
|
|
295
|
+
|
|
296
|
+
const report: StoreBackupReport = {
|
|
297
|
+
reportVersion: 1,
|
|
298
|
+
kind: "store-backup",
|
|
299
|
+
scope: "whole-store",
|
|
300
|
+
compression: appliedCompression,
|
|
301
|
+
startedAt,
|
|
302
|
+
totalMs: nowMs() - startPerf,
|
|
303
|
+
byteLength: file.size,
|
|
304
|
+
phases: { checkpointStartedAtMs, checkpointMs, dumpStartedAtMs, dumpMs },
|
|
305
|
+
diagnostics,
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
return { file, report };
|
|
309
|
+
}
|