@pgxsinkit/client 0.1.31 → 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 +19 -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
package/src/debug.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Opt-in runtime instrumentation for diagnosing convergence / sync latency in a live app (e.g. the
|
|
2
|
+
// board demo). It is OFF by default and adds nothing to a normal run: every call early-returns unless
|
|
3
|
+
// `globalThis.__pgxsinkitDebug` is truthy, so it is safe to leave the call sites in shipping code.
|
|
4
|
+
//
|
|
5
|
+
// Enable it from the browser console (`globalThis.__pgxsinkitDebug = true`) — or set it before the
|
|
6
|
+
// client boots; the board's dev build turns it on automatically. Each line is stamped with a monotonic
|
|
7
|
+
// millisecond clock so the gaps between phases (enqueue → flush → server ack → Electric echo →
|
|
8
|
+
// overlay clear → live-query render) can be read straight off the console.
|
|
9
|
+
|
|
10
|
+
interface DebugGlobal {
|
|
11
|
+
__pgxsinkitDebug?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const isEnabled = (): boolean => (globalThis as DebugGlobal).__pgxsinkitDebug === true;
|
|
15
|
+
|
|
16
|
+
const now = (): number => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A sink that receives every `syncDebug` line (event, the worker's monotonic stamp, optional data),
|
|
20
|
+
* INDEPENDENT of the local `__pgxsinkitDebug` console gate. This is the seam `defineSyncWorker` installs
|
|
21
|
+
* to forward the debug rail to tabs (ADR-0032 decision 7): a SharedWorker's own console is invisible
|
|
22
|
+
* (`chrome://inspect`), so lines must cross the bridge and each tab re-prints them gated on ITS OWN flag.
|
|
23
|
+
* Unset by default → no forwarding and zero cost on the in-process path.
|
|
24
|
+
*/
|
|
25
|
+
type SyncDebugSink = (event: string, stamp: number, data?: Record<string, unknown>) => void;
|
|
26
|
+
let syncDebugSink: SyncDebugSink | undefined;
|
|
27
|
+
|
|
28
|
+
/** Install (or clear, with `undefined`) the debug-rail sink. Idempotent; `defineSyncWorker` owns it. */
|
|
29
|
+
export function setSyncDebugSink(sink: SyncDebugSink | undefined): void {
|
|
30
|
+
syncDebugSink = sink;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Log one timestamped event. Prints to the console only when `globalThis.__pgxsinkitDebug` is on, but ALSO
|
|
35
|
+
* feeds any installed {@link setSyncDebugSink} sink (so the worker can forward the rail to tabs even when the
|
|
36
|
+
* worker's own console gate is off). No sink AND not enabled → an early return, so the off-path pays nothing.
|
|
37
|
+
*/
|
|
38
|
+
export function syncDebug(event: string, data?: Record<string, unknown>): void {
|
|
39
|
+
const enabled = isEnabled();
|
|
40
|
+
if (!enabled && syncDebugSink === undefined) return;
|
|
41
|
+
const stamp = now();
|
|
42
|
+
if (enabled) {
|
|
43
|
+
const prefix = `[pgxsinkit ${stamp.toFixed(0)}ms]`;
|
|
44
|
+
if (data) {
|
|
45
|
+
console.debug(`${prefix} ${event}`, data);
|
|
46
|
+
} else {
|
|
47
|
+
console.debug(`${prefix} ${event}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
syncDebugSink?.(event, stamp, data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The request URL of a `fetch` first argument, in any of its accepted forms; undefined if unreadable. */
|
|
54
|
+
function fetchInputUrl(input: Parameters<typeof fetch>[0]): string | undefined {
|
|
55
|
+
if (typeof input === "string") return input;
|
|
56
|
+
if (input instanceof URL) return input.toString();
|
|
57
|
+
if (typeof input === "object" && input !== null && "url" in input) {
|
|
58
|
+
return (input as { url: string }).url;
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Wrap a `fetch` so every ShapeStream request (the Electric read-path long-poll/catch-up) emits a start
|
|
65
|
+
* line (shape/table, offset, live — derived from the request URL params) and a completion line (status,
|
|
66
|
+
* ms, and whether the response is up-to-date). Passthrough when instrumentation is off: the early return
|
|
67
|
+
* runs before any URL parsing, so the off-path pays nothing per request beyond this closure itself.
|
|
68
|
+
*
|
|
69
|
+
* Inject it as a ShapeStream `fetchClient`; pass the stream's existing `fetchClient` (if any) as the base
|
|
70
|
+
* so the instrumentation composes rather than replaces.
|
|
71
|
+
*/
|
|
72
|
+
export function instrumentShapeFetch(baseFetch: typeof fetch = fetch): typeof fetch {
|
|
73
|
+
const wrapped = (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]): Promise<Response> => {
|
|
74
|
+
if (!isEnabled()) return baseFetch(input, init);
|
|
75
|
+
const url = fetchInputUrl(input);
|
|
76
|
+
const params = url ? new URL(url).searchParams : undefined;
|
|
77
|
+
// Derive the shape identity ONCE and name it on BOTH the start and done lines, so a start/done pair
|
|
78
|
+
// is readable without cross-referencing timing (multiple shapes' requests interleave on the rail).
|
|
79
|
+
const shape = params?.get("table") ?? undefined;
|
|
80
|
+
const live = params?.get("live") === "true";
|
|
81
|
+
const offset = params?.get("offset") ?? undefined;
|
|
82
|
+
// The handle chain is the backlog-0001 forensic: WHICH handle each request carried, which one the
|
|
83
|
+
// response minted, and whether the stale-cache ladder's params were on the wire — so a
|
|
84
|
+
// dead-handle-served-N-times storm reads straight off the rail (same responseHandle across busted
|
|
85
|
+
// attempts) instead of needing a network-tab capture at the moment of recurrence.
|
|
86
|
+
const requestHandle = params?.get("handle") ?? undefined;
|
|
87
|
+
const cacheBuster = params?.has("cache-buster") ?? false;
|
|
88
|
+
const expiredHandle = params?.get("expired_handle") ?? undefined;
|
|
89
|
+
// The whole request identity rides in ONE compact string field: Chrome's console preview truncates
|
|
90
|
+
// object previews at ~5 properties, and a captured log (the only artifact of a live recurrence) keeps
|
|
91
|
+
// just the preview — separate fields silently vanish exactly when they matter (backlog 0001).
|
|
92
|
+
const cursor = params?.get("cursor") ?? undefined;
|
|
93
|
+
const req = [
|
|
94
|
+
`${shape ?? "?"}@${offset ?? "?"}`,
|
|
95
|
+
live ? "live" : "catchup",
|
|
96
|
+
requestHandle ? `h=${requestHandle}` : "no-handle",
|
|
97
|
+
// The live-cycle cursor is Electric's CDN-collapse defense — its absence or non-advancement on
|
|
98
|
+
// live polls is itself a finding (backlog 0001).
|
|
99
|
+
cursor ? `c=${cursor}` : live ? "NO-CURSOR" : "",
|
|
100
|
+
cacheBuster ? "BUSTED" : "",
|
|
101
|
+
expiredHandle ? `expired=${expiredHandle}` : "",
|
|
102
|
+
]
|
|
103
|
+
.filter(Boolean)
|
|
104
|
+
.join(" ");
|
|
105
|
+
syncDebug("shape request start", { req });
|
|
106
|
+
const startedAt = now();
|
|
107
|
+
return baseFetch(input, init).then((response) => {
|
|
108
|
+
// Same single-string discipline for the response half: status/ms stay first-class (they are what
|
|
109
|
+
// rail readers scan), the forensic chain (up-to-date, minted handle, end offset) folds into `resp`.
|
|
110
|
+
const responseHandle = response.headers.get("electric-handle");
|
|
111
|
+
const responseOffset = response.headers.get("electric-offset");
|
|
112
|
+
const responseCursor = response.headers.get("electric-cursor");
|
|
113
|
+
const age = response.headers.get("age");
|
|
114
|
+
const resp = [
|
|
115
|
+
response.headers.has("electric-up-to-date") ? "up-to-date" : "",
|
|
116
|
+
responseHandle && responseHandle !== requestHandle ? `minted h=${responseHandle}` : "",
|
|
117
|
+
responseOffset ? `end=${responseOffset}` : "",
|
|
118
|
+
responseCursor ? `c=${responseCursor}` : "",
|
|
119
|
+
age ? `age=${age}` : "",
|
|
120
|
+
]
|
|
121
|
+
.filter(Boolean)
|
|
122
|
+
.join(" ");
|
|
123
|
+
syncDebug("shape request done", {
|
|
124
|
+
req,
|
|
125
|
+
status: response.status,
|
|
126
|
+
ms: Math.round(now() - startedAt),
|
|
127
|
+
...(resp ? { resp } : {}),
|
|
128
|
+
});
|
|
129
|
+
return response;
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
// `typeof fetch` (Bun's global) carries a `preconnect` member the wrapper neither has nor needs — the
|
|
133
|
+
// ShapeStream only ever invokes fetchClient as a plain fetch — so the shape-compatible cast is safe.
|
|
134
|
+
return wrapped as typeof fetch;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Run `fn`, logging `<event> done` with its wall-clock duration (and any extra `data`). When
|
|
139
|
+
* instrumentation is off this is a thin pass-through with no logging and no timing overhead beyond the
|
|
140
|
+
* call itself. Returns whatever `fn` returns.
|
|
141
|
+
*/
|
|
142
|
+
export async function timeAsync<T>(event: string, fn: () => Promise<T>, data?: Record<string, unknown>): Promise<T> {
|
|
143
|
+
if (!isEnabled()) return fn();
|
|
144
|
+
const startedAt = now();
|
|
145
|
+
syncDebug(`${event} start`, data);
|
|
146
|
+
try {
|
|
147
|
+
return await fn();
|
|
148
|
+
} finally {
|
|
149
|
+
syncDebug(`${event} done`, { ms: Math.round(now() - startedAt) });
|
|
150
|
+
}
|
|
151
|
+
}
|