@pgxsinkit/client 0.1.32 → 0.1.36
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/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/index.d.ts +421 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7214 -2234
- package/dist/index.js.map +74 -44
- package/dist/lazy-guard.d.ts +58 -0
- package/dist/lazy-guard.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 +62 -0
- package/dist/mutation-state.d.ts.map +1 -0
- package/dist/mutation.d.ts +117 -4
- package/dist/mutation.d.ts.map +1 -1
- package/dist/schema.d.ts +49 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/shape-sync.d.ts +180 -0
- package/dist/shape-sync.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 +118 -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/worker/attach-sync-client.d.ts +75 -0
- package/dist/worker/attach-sync-client.d.ts.map +1 -0
- package/dist/worker/define-sync-worker.d.ts +75 -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 +260 -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 +15 -7
- package/skills/core/SKILL.md +197 -0
- package/skills/operating/SKILL.md +243 -0
- package/src/convergence.ts +197 -0
- package/src/debug.ts +151 -0
- package/src/index.ts +1104 -44
- package/src/lazy-guard.ts +121 -0
- package/src/local-store.ts +193 -0
- package/src/local-tables.ts +546 -0
- package/src/mutation-state.ts +90 -0
- package/src/mutation.ts +1868 -562
- package/src/schema.ts +352 -27
- package/src/shape-sync.ts +477 -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 +1076 -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 +132 -0
- package/src/sync-auth.ts +117 -0
- package/src/worker/attach-sync-client.ts +558 -0
- package/src/worker/define-sync-worker.ts +596 -0
- package/src/worker/live-diff.ts +140 -0
- package/src/worker/protocol.ts +325 -0
- package/src/worker/token-cache.ts +84 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { index, jsonb, pgSchema, primaryKey, text } from "drizzle-orm/pg-core";
|
|
2
|
+
|
|
3
|
+
import type { ShapeSubscriptionState } from "./subscription-state";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The two metadata-store relations the sync engine provisions at boot (ADR-0028 decision 4). The
|
|
7
|
+
* metadata schema name is dynamic (per-client config) but fixed at engine construction, so the tables
|
|
8
|
+
* are built per schema string and memoized.
|
|
9
|
+
*
|
|
10
|
+
* ADR-0029 D3: these pgTables are the SINGLE SOURCE for the relations. `migrateSubscriptionMetadataTables`
|
|
11
|
+
* (subscription-state.ts) renders their `CREATE TABLE`/`CREATE INDEX` DDL FROM these definitions via the
|
|
12
|
+
* in-house `renderCreateTableSql` (schema.ts), and the DML paths query them directly — so the DDL and the
|
|
13
|
+
* pgTable can no longer diverge, and the former drift guard in `tests/unit/sync-drizzle-executor.test.ts`
|
|
14
|
+
* is now a plain provisioning round-trip.
|
|
15
|
+
*
|
|
16
|
+
* Property keys are deliberately snake_case to mirror the row shapes the callers already consume
|
|
17
|
+
* (`shape_metadata`, `last_lsn`, `pk_json`, …), so a `select()` returns rows in the existing shape with
|
|
18
|
+
* no alias mapping.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export interface MetadataTables {
|
|
22
|
+
/** `<schema>.subscriptions_metadata` — one row per subscription key. */
|
|
23
|
+
readonly subscriptionsMetadata: ReturnType<typeof buildSubscriptionsMetadata>;
|
|
24
|
+
/** `<schema>.shape_row_tags` — the tagged-subquery reason-set store (ADR-0023). */
|
|
25
|
+
readonly shapeRowTags: ReturnType<typeof buildShapeRowTags>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function buildSubscriptionsMetadata(schema: ReturnType<typeof pgSchema>) {
|
|
29
|
+
return schema.table("subscriptions_metadata", {
|
|
30
|
+
key: text("key").primaryKey(),
|
|
31
|
+
// JSONB NOT NULL — round-trips as a parsed object (drizzle's jsonb codec), matching the raw path.
|
|
32
|
+
shape_metadata: jsonb("shape_metadata").$type<Record<string, ShapeSubscriptionState>>().notNull(),
|
|
33
|
+
// The LSN is persisted as TEXT (a bigint has no JSON/portable-column form); callers `BigInt(...)` it.
|
|
34
|
+
last_lsn: text("last_lsn").notNull(),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function buildShapeRowTags(schema: ReturnType<typeof pgSchema>) {
|
|
39
|
+
return schema.table(
|
|
40
|
+
"shape_row_tags",
|
|
41
|
+
{
|
|
42
|
+
shape_table: text("shape_table").notNull(),
|
|
43
|
+
pk_json: text("pk_json").notNull(),
|
|
44
|
+
tag: text("tag").notNull(),
|
|
45
|
+
},
|
|
46
|
+
(t) => [
|
|
47
|
+
primaryKey({ columns: [t.shape_table, t.pk_json, t.tag] }),
|
|
48
|
+
index("shape_row_tags_shape_tag_idx").on(t.shape_table, t.tag),
|
|
49
|
+
],
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const cache = new Map<string, MetadataTables>();
|
|
54
|
+
|
|
55
|
+
/** The (memoized) metadata-store `pgTable`s for a given metadata schema name. */
|
|
56
|
+
export function getMetadataTables(metadataSchema: string): MetadataTables {
|
|
57
|
+
let tables = cache.get(metadataSchema);
|
|
58
|
+
if (!tables) {
|
|
59
|
+
const schema = pgSchema(metadataSchema);
|
|
60
|
+
tables = {
|
|
61
|
+
subscriptionsMetadata: buildSubscriptionsMetadata(schema),
|
|
62
|
+
shapeRowTags: buildShapeRowTags(schema),
|
|
63
|
+
};
|
|
64
|
+
cache.set(metadataSchema, tables);
|
|
65
|
+
}
|
|
66
|
+
return tables;
|
|
67
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Live-tail sibling nudge (ADR-0031 live-tail completion). The engine commits a consistency group
|
|
2
|
+
// atomically at the group's lowest effective frontier (the MIN over per-shape `max(rawFrontier,
|
|
3
|
+
// commitFloor)`; see shape-inbox.ts). On the LIVE tail, when a change batch arrives on a busy shape at
|
|
4
|
+
// LSN L, a quiet sibling shape's frontier only advances when its parked long-poll returns — up to ~41s on
|
|
5
|
+
// Electric Cloud — so the busy shape's batch stays buffered, unapplied, for that whole hold. The nudge
|
|
6
|
+
// aborts those parked polls and forces an immediate NON-live catch-up on each lagging sibling, which
|
|
7
|
+
// returns a fresh `global_last_seen_lsn` in ~sub-second; the group frontier then reaches L and the batch
|
|
8
|
+
// commits. Commit atomicity is untouched — commits still fire only at the group min frontier — this only
|
|
9
|
+
// shortens how long a gated batch waits for its siblings' watermarks.
|
|
10
|
+
//
|
|
11
|
+
// This module holds the two pieces that are pure and side-effect-free (so a plain unit test can exercise
|
|
12
|
+
// them without dragging PGlite/Electric in through the engine module): the bounded-round constants and the
|
|
13
|
+
// one-shot cache-buster fetch wrapper. The stateful, per-group watchdog itself lives in the engine's
|
|
14
|
+
// per-group scope (index.ts), where it can read the inbox frontiers and reach the live shape streams.
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* How long a hold must PERSIST before the first nudge round fires. A cross-shape transaction's halves
|
|
18
|
+
* arrive milliseconds apart even on a healthy stack, so every grouped commit passes through a
|
|
19
|
+
* transient "held" instant — nudging on it immediately ABORTS the sibling's long-poll that was about
|
|
20
|
+
* to deliver the other half, turning a millisecond hold into an abort/refetch disruption loop
|
|
21
|
+
* (regression caught by the sync-e2e integration lane on local Electric). A hold that outlives this
|
|
22
|
+
* grace is the real thing: a quiet sibling parked in a multi-second poll. On Electric Cloud this adds
|
|
23
|
+
* ~1s to a path that saves ~40s; on local stacks it means the nudge simply never fires.
|
|
24
|
+
*/
|
|
25
|
+
export const NUDGE_HOLD_GRACE_MS = 1_000;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Max nudge rounds before a dead / genuinely-still-polling sibling degrades back to the old behavior
|
|
29
|
+
* (wait out its live long-poll) instead of being nudged forever — so a stuck sibling never becomes a
|
|
30
|
+
* refresh storm.
|
|
31
|
+
*/
|
|
32
|
+
export const NUDGE_MAX_ROUNDS = 3;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Polling step while waiting for a nudge round's forced catch-ups to advance the group frontier. The
|
|
36
|
+
* `forceDisconnectAndRefresh()` promise settles BEFORE the refreshed catch-up response lands (field
|
|
37
|
+
* measurement: round 2 fired ~750ms into round 1's still-in-flight requests when the wait keyed off that
|
|
38
|
+
* promise alone), so the watchdog instead polls the frontier at this step, up to
|
|
39
|
+
* {@link NUDGE_ROUND_WAIT_MS} per round, breaking out the moment the target is reached.
|
|
40
|
+
*/
|
|
41
|
+
export const NUDGE_ROUND_GRACE_MS = 100;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Max wait per nudge round for the forced catch-ups to return and advance the frontier. A nudged
|
|
45
|
+
* catch-up measures ~0.9–1.4s through a warm deployed edge proxy but >3.5s against a cold one — and
|
|
46
|
+
* a next round's refresh ABORTS a still-in-flight round (the abort-and-reconnect is the mechanism),
|
|
47
|
+
* so an early re-nudge throws away an almost-done response. The wait therefore covers the cold-edge
|
|
48
|
+
* tail; worst case to exhaustion stays NUDGE_MAX_ROUNDS × this ≈ 12s, still far under a ~41s hold.
|
|
49
|
+
*/
|
|
50
|
+
export const NUDGE_ROUND_WAIT_MS = 4_000;
|
|
51
|
+
|
|
52
|
+
/** The request URL of a `fetch` first argument, in any of its accepted forms; undefined if unreadable. */
|
|
53
|
+
function requestUrl(input: Parameters<typeof fetch>[0]): string | undefined {
|
|
54
|
+
if (typeof input === "string") return input;
|
|
55
|
+
if (input instanceof URL) return input.toString();
|
|
56
|
+
if (typeof input === "object" && input !== null && "url" in input) {
|
|
57
|
+
return (input as { url: string }).url;
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Wrap a `fetchClient` so a pending one-shot nudge token defeats the CDN on the nudged sibling's catch-up.
|
|
64
|
+
*
|
|
65
|
+
* A nudge's `forceDisconnectAndRefresh` issues a NON-live catch-up (Electric sets `canLongPoll: false`),
|
|
66
|
+
* which is CDN-cacheable — a HIT would return the SAME stale watermark that made the sibling lag, defeating
|
|
67
|
+
* the nudge. So the watchdog drops a per-shape token into `nudgeBusters` just before it nudges; this wrapper
|
|
68
|
+
* consumes that token on the sibling's next NON-live request, appending `cache-buster=<token>` to force a
|
|
69
|
+
* CDN miss. It is one-shot (the token is deleted on use) and skips LIVE (`live=true`) long-polls — the token
|
|
70
|
+
* is meant for the catch-up that follows the aborted poll, not the poll itself, which is left to be retained
|
|
71
|
+
* for that catch-up. An already-present `cache-buster` param is overwritten. When no token is pending this is
|
|
72
|
+
* a straight passthrough, so the off-path pays only this closure.
|
|
73
|
+
*
|
|
74
|
+
* Exported for unit testing; composed in the engine as the OUTER wrapper around `instrumentShapeFetch`, so
|
|
75
|
+
* the buster stamps the URL BEFORE instrumentation logs it (the debug rail then shows `BUSTED`).
|
|
76
|
+
*/
|
|
77
|
+
export function withNudgeBuster(
|
|
78
|
+
fetchClient: typeof fetch,
|
|
79
|
+
nudgeBusters: Map<string, string>,
|
|
80
|
+
shapeName: string,
|
|
81
|
+
): typeof fetch {
|
|
82
|
+
const wrapped = (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]): Promise<Response> => {
|
|
83
|
+
const token = nudgeBusters.get(shapeName);
|
|
84
|
+
if (token === undefined) {
|
|
85
|
+
return fetchClient(input, init);
|
|
86
|
+
}
|
|
87
|
+
const raw = requestUrl(input);
|
|
88
|
+
if (raw === undefined) {
|
|
89
|
+
return fetchClient(input, init);
|
|
90
|
+
}
|
|
91
|
+
const url = new URL(raw);
|
|
92
|
+
// A live long-poll is the parked request the nudge aborts, not the catch-up that carries the fresh
|
|
93
|
+
// watermark — leave it (and the token) untouched so the token lands on the following catch-up.
|
|
94
|
+
if (url.searchParams.get("live") === "true") {
|
|
95
|
+
return fetchClient(input, init);
|
|
96
|
+
}
|
|
97
|
+
// One-shot: consume the token and stamp this catch-up URL (set overwrites any existing param).
|
|
98
|
+
nudgeBusters.delete(shapeName);
|
|
99
|
+
url.searchParams.set("cache-buster", token);
|
|
100
|
+
const busted = url.toString();
|
|
101
|
+
if (typeof input === "string" || input instanceof URL) {
|
|
102
|
+
return fetchClient(busted, init);
|
|
103
|
+
}
|
|
104
|
+
// A Request carries method/headers/body that must survive the URL swap — rebuild from it.
|
|
105
|
+
return fetchClient(new Request(busted, input), init);
|
|
106
|
+
};
|
|
107
|
+
// `typeof fetch` (Bun's global) carries a `preconnect` member the wrapper neither has nor needs — the
|
|
108
|
+
// ShapeStream only ever invokes fetchClient as a plain fetch — so the shape-compatible cast is safe.
|
|
109
|
+
return wrapped as typeof fetch;
|
|
110
|
+
}
|