@pgxsinkit/client 0.2.7 → 0.5.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 (153) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE-APACHE-2.0 +202 -0
  3. package/NOTICE +81 -0
  4. package/README.md +11 -3
  5. package/dist/boot-report.d.ts +11 -8
  6. package/dist/boot-report.d.ts.map +1 -1
  7. package/dist/circuits/envelope-to-change.d.ts +39 -0
  8. package/dist/circuits/envelope-to-change.d.ts.map +1 -0
  9. package/dist/circuits/group-sync.d.ts +123 -0
  10. package/dist/circuits/group-sync.d.ts.map +1 -0
  11. package/dist/circuits/shape-group.d.ts +77 -0
  12. package/dist/circuits/shape-group.d.ts.map +1 -0
  13. package/dist/circuits/stream-inbox.d.ts +126 -0
  14. package/dist/circuits/stream-inbox.d.ts.map +1 -0
  15. package/dist/circuits/stream-source.d.ts +96 -0
  16. package/dist/circuits/stream-source.d.ts.map +1 -0
  17. package/dist/circuits/subscription-client.d.ts +143 -0
  18. package/dist/circuits/subscription-client.d.ts.map +1 -0
  19. package/dist/circuits/sync-engine.d.ts +131 -0
  20. package/dist/circuits/sync-engine.d.ts.map +1 -0
  21. package/dist/debug.d.ts +0 -10
  22. package/dist/debug.d.ts.map +1 -1
  23. package/dist/event-lane.d.ts +32 -1
  24. package/dist/event-lane.d.ts.map +1 -1
  25. package/dist/export-data.d.ts +3 -3
  26. package/dist/export-dump.d.ts +9 -9
  27. package/dist/export-dump.d.ts.map +1 -1
  28. package/dist/export-store.d.ts +13 -13
  29. package/dist/index.d.ts +237 -113
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +7037 -6665
  32. package/dist/index.js.map +46 -43
  33. package/dist/local-store.d.ts +1 -1
  34. package/dist/local-store.d.ts.map +1 -1
  35. package/dist/local-tables.d.ts +59 -1
  36. package/dist/local-tables.d.ts.map +1 -1
  37. package/dist/mutation.d.ts +2 -2
  38. package/dist/mutation.d.ts.map +1 -1
  39. package/dist/mutations-api.d.ts.map +1 -1
  40. package/dist/opfs-effects.d.ts +30 -2
  41. package/dist/opfs-effects.d.ts.map +1 -1
  42. package/dist/schema.d.ts +12 -1
  43. package/dist/schema.d.ts.map +1 -1
  44. package/dist/store-boot.d.ts +30 -2
  45. package/dist/store-boot.d.ts.map +1 -1
  46. package/dist/store-build.d.ts +19 -0
  47. package/dist/store-build.d.ts.map +1 -0
  48. package/dist/store-engine.d.ts +22 -0
  49. package/dist/store-engine.d.ts.map +1 -0
  50. package/dist/store-meta.d.ts +29 -3
  51. package/dist/store-meta.d.ts.map +1 -1
  52. package/dist/store-ownership-lock.d.ts +33 -0
  53. package/dist/store-ownership-lock.d.ts.map +1 -0
  54. package/dist/store-path.d.ts +40 -28
  55. package/dist/store-path.d.ts.map +1 -1
  56. package/dist/sync/apply.d.ts +17 -25
  57. package/dist/sync/apply.d.ts.map +1 -1
  58. package/dist/sync/copy.d.ts +84 -8
  59. package/dist/sync/copy.d.ts.map +1 -1
  60. package/dist/sync/drizzle-executor.d.ts +4 -4
  61. package/dist/sync/drizzle-executor.d.ts.map +1 -1
  62. package/dist/sync/fold.d.ts +40 -0
  63. package/dist/sync/fold.d.ts.map +1 -0
  64. package/dist/sync/metadata-tables.d.ts +25 -1
  65. package/dist/sync/metadata-tables.d.ts.map +1 -1
  66. package/dist/sync/subscription-state.d.ts +26 -7
  67. package/dist/sync/subscription-state.d.ts.map +1 -1
  68. package/dist/sync/types.d.ts +9 -129
  69. package/dist/sync/types.d.ts.map +1 -1
  70. package/dist/testing.d.ts +1 -1
  71. package/dist/testing.js +4 -3
  72. package/dist/testing.js.map +4 -4
  73. package/dist/worker/attach-sync-client.d.ts +27 -14
  74. package/dist/worker/attach-sync-client.d.ts.map +1 -1
  75. package/dist/worker/define-sync-worker.d.ts +52 -19
  76. package/dist/worker/define-sync-worker.d.ts.map +1 -1
  77. package/dist/worker/engine-control.d.ts +53 -0
  78. package/dist/worker/engine-control.d.ts.map +1 -1
  79. package/dist/worker/live-query-manager.d.ts +3 -3
  80. package/dist/worker/live-query-manager.d.ts.map +1 -1
  81. package/dist/worker/live-query-params-guard.d.ts +5 -5
  82. package/dist/worker/protocol.d.ts +26 -7
  83. package/dist/worker/protocol.d.ts.map +1 -1
  84. package/package.json +11 -9
  85. package/skills/core/SKILL.md +145 -63
  86. package/skills/operating/SKILL.md +221 -221
  87. package/skills/operating/references/postgres-builds.md +48 -0
  88. package/src/boot-report.ts +13 -12
  89. package/src/circuits/envelope-to-change.ts +211 -0
  90. package/src/circuits/group-sync.ts +1047 -0
  91. package/src/circuits/shape-group.ts +158 -0
  92. package/src/circuits/stream-inbox.ts +225 -0
  93. package/src/circuits/stream-source.ts +191 -0
  94. package/src/circuits/subscription-client.ts +353 -0
  95. package/src/circuits/sync-engine.ts +643 -0
  96. package/src/debug.ts +1 -85
  97. package/src/event-lane.ts +132 -14
  98. package/src/export-data.ts +4 -4
  99. package/src/export-dump.ts +24 -28
  100. package/src/export-store.ts +17 -17
  101. package/src/index.ts +797 -544
  102. package/src/lazy-guard.ts +1 -1
  103. package/src/live-rows-sql.ts +1 -1
  104. package/src/local-store.ts +2 -2
  105. package/src/local-tables.ts +76 -5
  106. package/src/mutation.ts +9 -8
  107. package/src/mutations-api.ts +2 -2
  108. package/src/opfs-effects.ts +74 -10
  109. package/src/schema.ts +126 -29
  110. package/src/store-boot.ts +71 -7
  111. package/src/store-build.ts +37 -0
  112. package/src/store-engine.ts +94 -0
  113. package/src/store-lifecycle.ts +1 -1
  114. package/src/store-meta.ts +155 -4
  115. package/src/store-ownership-lock.ts +109 -0
  116. package/src/store-path.ts +56 -40
  117. package/src/sync/apply.ts +104 -156
  118. package/src/sync/copy.ts +116 -21
  119. package/src/sync/drizzle-executor.ts +13 -14
  120. package/src/sync/fold.ts +94 -0
  121. package/src/sync/metadata-tables.ts +43 -2
  122. package/src/sync/subscription-state.ts +28 -10
  123. package/src/sync/types.ts +13 -141
  124. package/src/testing.ts +4 -4
  125. package/src/worker/attach-sync-client.ts +150 -57
  126. package/src/worker/define-sync-worker.ts +325 -87
  127. package/src/worker/engine-control.ts +89 -0
  128. package/src/worker/engine-router.ts +1 -1
  129. package/src/worker/live-query-fingerprint.ts +4 -4
  130. package/src/worker/live-query-manager.ts +12 -12
  131. package/src/worker/live-query-params-guard.ts +10 -10
  132. package/src/worker/protocol.ts +38 -7
  133. package/dist/read-stream-stall.d.ts +0 -54
  134. package/dist/read-stream-stall.d.ts.map +0 -1
  135. package/dist/shape-sync.d.ts +0 -207
  136. package/dist/shape-sync.d.ts.map +0 -1
  137. package/dist/sync/index.d.ts +0 -32
  138. package/dist/sync/index.d.ts.map +0 -1
  139. package/dist/sync/nudge.d.ts +0 -49
  140. package/dist/sync/nudge.d.ts.map +0 -1
  141. package/dist/sync/shape-inbox.d.ts +0 -215
  142. package/dist/sync/shape-inbox.d.ts.map +0 -1
  143. package/dist/sync/tags.d.ts +0 -142
  144. package/dist/sync/tags.d.ts.map +0 -1
  145. package/dist/sync-auth.d.ts +0 -53
  146. package/dist/sync-auth.d.ts.map +0 -1
  147. package/src/read-stream-stall.ts +0 -102
  148. package/src/shape-sync.ts +0 -533
  149. package/src/sync/index.ts +0 -1124
  150. package/src/sync/nudge.ts +0 -110
  151. package/src/sync/shape-inbox.ts +0 -566
  152. package/src/sync/tags.ts +0 -390
  153. package/src/sync-auth.ts +0 -117
package/src/sync/nudge.ts DELETED
@@ -1,110 +0,0 @@
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
- }