@pgxsinkit/client 0.2.7 → 0.3.0

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.
Files changed (94) hide show
  1. package/README.md +2 -1
  2. package/dist/boot-report.d.ts +5 -2
  3. package/dist/boot-report.d.ts.map +1 -1
  4. package/dist/circuits/envelope-to-change.d.ts +35 -0
  5. package/dist/circuits/envelope-to-change.d.ts.map +1 -0
  6. package/dist/circuits/group-sync.d.ts +123 -0
  7. package/dist/circuits/group-sync.d.ts.map +1 -0
  8. package/dist/circuits/shape-group.d.ts +77 -0
  9. package/dist/circuits/shape-group.d.ts.map +1 -0
  10. package/dist/circuits/stream-inbox.d.ts +126 -0
  11. package/dist/circuits/stream-inbox.d.ts.map +1 -0
  12. package/dist/circuits/stream-source.d.ts +90 -0
  13. package/dist/circuits/stream-source.d.ts.map +1 -0
  14. package/dist/circuits/subscription-client.d.ts +143 -0
  15. package/dist/circuits/subscription-client.d.ts.map +1 -0
  16. package/dist/circuits/sync-engine.d.ts +131 -0
  17. package/dist/circuits/sync-engine.d.ts.map +1 -0
  18. package/dist/debug.d.ts +0 -10
  19. package/dist/debug.d.ts.map +1 -1
  20. package/dist/export-dump.d.ts.map +1 -1
  21. package/dist/index.d.ts +47 -19
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +6258 -6443
  24. package/dist/index.js.map +26 -26
  25. package/dist/local-tables.d.ts +1 -1
  26. package/dist/local-tables.d.ts.map +1 -1
  27. package/dist/schema.d.ts.map +1 -1
  28. package/dist/sync/apply.d.ts +13 -21
  29. package/dist/sync/apply.d.ts.map +1 -1
  30. package/dist/sync/copy.d.ts +8 -8
  31. package/dist/sync/fold.d.ts +40 -0
  32. package/dist/sync/fold.d.ts.map +1 -0
  33. package/dist/sync/metadata-tables.d.ts +25 -1
  34. package/dist/sync/metadata-tables.d.ts.map +1 -1
  35. package/dist/sync/subscription-state.d.ts +21 -2
  36. package/dist/sync/subscription-state.d.ts.map +1 -1
  37. package/dist/sync/types.d.ts +9 -129
  38. package/dist/sync/types.d.ts.map +1 -1
  39. package/dist/testing.js +3 -3
  40. package/dist/testing.js.map +2 -2
  41. package/dist/worker/attach-sync-client.d.ts +3 -1
  42. package/dist/worker/attach-sync-client.d.ts.map +1 -1
  43. package/dist/worker/define-sync-worker.d.ts +17 -1
  44. package/dist/worker/define-sync-worker.d.ts.map +1 -1
  45. package/dist/worker/engine-control.d.ts +53 -0
  46. package/dist/worker/engine-control.d.ts.map +1 -1
  47. package/package.json +4 -5
  48. package/skills/core/SKILL.md +72 -55
  49. package/skills/operating/SKILL.md +120 -120
  50. package/src/boot-report.ts +4 -3
  51. package/src/circuits/envelope-to-change.ts +102 -0
  52. package/src/circuits/group-sync.ts +1047 -0
  53. package/src/circuits/shape-group.ts +158 -0
  54. package/src/circuits/stream-inbox.ts +225 -0
  55. package/src/circuits/stream-source.ts +165 -0
  56. package/src/circuits/subscription-client.ts +353 -0
  57. package/src/circuits/sync-engine.ts +641 -0
  58. package/src/debug.ts +1 -85
  59. package/src/export-dump.ts +3 -2
  60. package/src/index.ts +290 -273
  61. package/src/lazy-guard.ts +1 -1
  62. package/src/local-tables.ts +9 -2
  63. package/src/schema.ts +2 -11
  64. package/src/sync/apply.ts +72 -110
  65. package/src/sync/copy.ts +15 -15
  66. package/src/sync/fold.ts +94 -0
  67. package/src/sync/metadata-tables.ts +42 -1
  68. package/src/sync/subscription-state.ts +21 -3
  69. package/src/sync/types.ts +13 -141
  70. package/src/testing.ts +2 -2
  71. package/src/worker/attach-sync-client.ts +8 -1
  72. package/src/worker/define-sync-worker.ts +122 -13
  73. package/src/worker/engine-control.ts +89 -0
  74. package/dist/read-stream-stall.d.ts +0 -54
  75. package/dist/read-stream-stall.d.ts.map +0 -1
  76. package/dist/shape-sync.d.ts +0 -207
  77. package/dist/shape-sync.d.ts.map +0 -1
  78. package/dist/sync/index.d.ts +0 -32
  79. package/dist/sync/index.d.ts.map +0 -1
  80. package/dist/sync/nudge.d.ts +0 -49
  81. package/dist/sync/nudge.d.ts.map +0 -1
  82. package/dist/sync/shape-inbox.d.ts +0 -215
  83. package/dist/sync/shape-inbox.d.ts.map +0 -1
  84. package/dist/sync/tags.d.ts +0 -142
  85. package/dist/sync/tags.d.ts.map +0 -1
  86. package/dist/sync-auth.d.ts +0 -53
  87. package/dist/sync-auth.d.ts.map +0 -1
  88. package/src/read-stream-stall.ts +0 -102
  89. package/src/shape-sync.ts +0 -533
  90. package/src/sync/index.ts +0 -1124
  91. package/src/sync/nudge.ts +0 -110
  92. package/src/sync/shape-inbox.ts +0 -566
  93. package/src/sync/tags.ts +0 -390
  94. package/src/sync-auth.ts +0 -117
package/src/debug.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  //
5
5
  // Enable it from the browser console (`globalThis.__pgxsinkitDebug = true`) — or set it before the
6
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 →
7
+ // millisecond clock so the gaps between phases (enqueue → flush → server ack → stream echo →
8
8
  // overlay clear → live-query render) can be read straight off the console.
9
9
 
10
10
  interface DebugGlobal {
@@ -50,90 +50,6 @@ export function syncDebug(event: string, data?: Record<string, unknown>): void {
50
50
  syncDebugSink?.(event, stamp, data);
51
51
  }
52
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
53
  /**
138
54
  * Run `fn`, logging `<event> done` with its wall-clock duration (and any extra `data`). When
139
55
  * instrumentation is off this is a thin pass-through with no logging and no timing overhead beyond the
@@ -143,8 +143,9 @@ export async function runThrowawayCloneDump(
143
143
 
144
144
  // Step 2 — boot the throwaway. Memory-backed via the resolution module's SCHEME selection (ADR-0036
145
145
  // decision 5: NEVER PGlite's explicit `fs: new MemoryFS()` — on 0.5.4 a `dumpDataDir` from an explicit-fs
146
- // instance silently omits post-initdb relation files). No extensions, no `electric` engine, no `live`:
147
- // the clone exists only to be read out by `pg_dump`, and `DEALLOCATE ALL` has nothing here to corrupt.
146
+ // instance silently omits post-initdb relation files). No extensions at all — not even `live` — and no
147
+ // sync runtime: the clone exists only to be read out by `pg_dump`, and `DEALLOCATE ALL` has nothing
148
+ // here to corrupt.
148
149
  const cloneDataDir = resolveStoreDataDir(nextCloneStorePath(), "memory");
149
150
  const cloneBootStartedAtMs = nowMs() - startPerf;
150
151
  const cloneBootStartPerf = nowMs();