cursedbelt-server 1.1.0 → 2.1.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 (81) hide show
  1. package/dist/server/bench/assert.d.ts +61 -0
  2. package/dist/server/bench/assert.js +117 -0
  3. package/dist/server/bench/budget.d.ts +130 -0
  4. package/dist/server/bench/budget.js +131 -0
  5. package/dist/server/bench/cpuBudget.d.ts +45 -0
  6. package/dist/server/bench/cpuBudget.js +34 -0
  7. package/dist/server/bench/cpuClock.d.ts +65 -0
  8. package/dist/server/bench/cpuClock.js +100 -0
  9. package/dist/server/bench/index.d.ts +40 -0
  10. package/dist/server/bench/index.js +40 -0
  11. package/dist/server/bench/recorder.d.ts +70 -0
  12. package/dist/server/bench/recorder.js +95 -0
  13. package/dist/server/bench/runBench.d.ts +61 -0
  14. package/dist/server/bench/runBench.js +61 -0
  15. package/dist/server/d1/backup.d.ts +110 -0
  16. package/dist/server/d1/backup.js +128 -0
  17. package/dist/server/d1/fakeD1.d.ts +41 -0
  18. package/dist/server/d1/fakeD1.js +185 -0
  19. package/dist/server/d1/index.d.ts +24 -0
  20. package/dist/server/d1/index.js +24 -0
  21. package/dist/server/d1/kysely.d.ts +56 -0
  22. package/dist/server/d1/kysely.js +138 -0
  23. package/dist/server/d1/limits.d.ts +56 -0
  24. package/dist/server/d1/limits.js +96 -0
  25. package/dist/server/d1/local.d.ts +31 -0
  26. package/dist/server/d1/local.js +135 -0
  27. package/dist/server/d1/remote.d.ts +59 -0
  28. package/dist/server/d1/remote.js +124 -0
  29. package/dist/server/d1/scheduling.d.ts +113 -0
  30. package/dist/server/d1/scheduling.js +164 -0
  31. package/dist/server/d1/types.d.ts +143 -0
  32. package/dist/server/d1/types.js +80 -0
  33. package/dist/server/d1/values.d.ts +50 -0
  34. package/dist/server/d1/values.js +124 -0
  35. package/dist/server/sync/http.d.ts +20 -3
  36. package/dist/server/sync/http.js +20 -14
  37. package/dist/server/sync/index.d.ts +10 -2
  38. package/dist/server/sync/index.js +9 -1
  39. package/dist/server/sync/planner.d.ts +38 -8
  40. package/dist/server/sync/planner.js +32 -8
  41. package/dist/server/sync/signal.d.ts +161 -0
  42. package/dist/server/sync/signal.js +348 -0
  43. package/dist/server/sync/timer.d.ts +63 -19
  44. package/dist/server/sync/timer.js +104 -45
  45. package/dist/server/sync/types.d.ts +0 -2
  46. package/package.json +21 -3
  47. package/src/leafSubpathsImportNothing.spec.ts +15 -3
  48. package/src/noTimerDialsAPeer.spec.ts +469 -0
  49. package/src/server/bench/assert.ts +192 -0
  50. package/src/server/bench/budget.spec.ts +126 -0
  51. package/src/server/bench/budget.ts +207 -0
  52. package/src/server/bench/cpuBudget.spec.ts +302 -0
  53. package/src/server/bench/cpuBudget.ts +81 -0
  54. package/src/server/bench/cpuClock.ts +119 -0
  55. package/src/server/bench/index.ts +81 -0
  56. package/src/server/bench/recorder.ts +163 -0
  57. package/src/server/bench/runBench.ts +110 -0
  58. package/src/server/d1/backup.spec.ts +121 -0
  59. package/src/server/d1/backup.ts +186 -0
  60. package/src/server/d1/fakeD1.ts +193 -0
  61. package/src/server/d1/index.ts +62 -0
  62. package/src/server/d1/kysely.spec.ts +145 -0
  63. package/src/server/d1/kysely.ts +169 -0
  64. package/src/server/d1/limits.spec.ts +90 -0
  65. package/src/server/d1/limits.ts +123 -0
  66. package/src/server/d1/local.ts +173 -0
  67. package/src/server/d1/remote.ts +182 -0
  68. package/src/server/d1/sameShape.spec.ts +279 -0
  69. package/src/server/d1/scheduling.spec.ts +120 -0
  70. package/src/server/d1/scheduling.ts +210 -0
  71. package/src/server/d1/types.ts +163 -0
  72. package/src/server/d1/values.ts +138 -0
  73. package/src/server/sync/http.ts +31 -16
  74. package/src/server/sync/index.ts +23 -1
  75. package/src/server/sync/planner.spec.ts +33 -16
  76. package/src/server/sync/planner.ts +48 -11
  77. package/src/server/sync/signal.spec.ts +306 -0
  78. package/src/server/sync/signal.ts +422 -0
  79. package/src/server/sync/timer.spec.ts +97 -16
  80. package/src/server/sync/timer.ts +124 -47
  81. package/src/server/sync/types.ts +0 -2
@@ -34,8 +34,16 @@ export { createOpLog, type OpLog } from "./opLog";
34
34
  export { createTokenStore, type DeviceToken, type TokenStore } from "./tokens";
35
35
  export { createSyncApplier, pushToRemote, pullFromRemote, runSync, type EngineOptions, type ExportPolicy, type PeerRefusal, } from "./engine";
36
36
  export { capPageBytes, createHttpRemote, createSyncReceiver, type ReceiverHooks, type ReceiverOptions, } from "./http";
37
- export { DEFAULT_LOOP, isUnreachable, planNextSync, type SyncLoopConfig, type SyncLoopState, } from "./planner";
37
+ export { DEFAULT_LOOP, isUnreachable, planNextSync, type SyncLoopConfig, type SyncLoopState, type SyncPlan, } from "./planner";
38
+ export { SIGNAL_SSE_HEADERS, connectSyncSignal, createSyncSignalHub, encodeNewsFrame, parseNewsFrame, signalResponse, signalStream, type SignalClient, type SignalClientOptions, type SignalStreamOptions, type SyncNews, type SyncSignalHub, } from "./signal";
38
39
  export { OFFLINE_STATUS, PEER_CONTACT_WRITE_MS, createSyncStatusReporter, type SyncStatus, type SyncStatusReporter, type SyncStatusStore, } from "./status";
39
- export { REQUEST_POLL_MS, startSyncTimer, type SyncTimerDeps, type SyncTimerHandle } from "./timer";
40
+ /**
41
+ * 🔴 `REQUEST_POLL_MS` was exported from here until 2026-09-15 and is GONE — that
42
+ * removal is why this package went to 2.0.0. It was a 20-second dial at a configured
43
+ * peer, and being *public API* is what made it dangerous: one more consumer and
44
+ * deleting it would have been a breaking change rather than an edit. `./timer`'s
45
+ * header has the ruling and the measurement; `./signal` is what replaced it.
46
+ */
47
+ export { startSyncTimer, type SyncTimerDeps, type SyncTimerHandle, type WakeReason } from "./timer";
40
48
  export { createSyncAlarm, type AlarmOutcome, type SyncAlarm, type SyncAlarmOptions } from "./alarm";
41
49
  export { DEFAULT_HOLD_MS, createCommandQueue, createCommandReceiver, createCommandRemote, startCommandWorker, type CommandQueue, type CommandRemote, type CommandRoutesOptions, type CommandStatus, type CommandWorkerDeps, type CommandWorkerHandle, type SyncCommand, } from "./commands";
@@ -35,7 +35,15 @@ export { createTokenStore } from "./tokens";
35
35
  export { createSyncApplier, pushToRemote, pullFromRemote, runSync, } from "./engine";
36
36
  export { capPageBytes, createHttpRemote, createSyncReceiver, } from "./http";
37
37
  export { DEFAULT_LOOP, isUnreachable, planNextSync, } from "./planner";
38
+ export { SIGNAL_SSE_HEADERS, connectSyncSignal, createSyncSignalHub, encodeNewsFrame, parseNewsFrame, signalResponse, signalStream, } from "./signal";
38
39
  export { OFFLINE_STATUS, PEER_CONTACT_WRITE_MS, createSyncStatusReporter, } from "./status";
39
- export { REQUEST_POLL_MS, startSyncTimer } from "./timer";
40
+ /**
41
+ * 🔴 `REQUEST_POLL_MS` was exported from here until 2026-09-15 and is GONE — that
42
+ * removal is why this package went to 2.0.0. It was a 20-second dial at a configured
43
+ * peer, and being *public API* is what made it dangerous: one more consumer and
44
+ * deleting it would have been a breaking change rather than an edit. `./timer`'s
45
+ * header has the ruling and the measurement; `./signal` is what replaced it.
46
+ */
47
+ export { startSyncTimer } from "./timer";
40
48
  export { createSyncAlarm } from "./alarm";
41
49
  export { DEFAULT_HOLD_MS, createCommandQueue, createCommandReceiver, createCommandRemote, startCommandWorker, } from "./commands";
@@ -1,11 +1,30 @@
1
1
  /**
2
- * The pure loop planner — vault's `planNextSync`, unchanged in spirit: back off after
3
- * a failure, else honor a debounced dirty flag, else the steady interval. Pure so
4
- * the timer is a thin wrapper and this is fake-clock testable.
2
+ * The pure loop planner: back off after a failure, else honor a debounced dirty
3
+ * flag, else **do not wake at all**. Pure so the timer is a thin wrapper and this
4
+ * is fake-clock testable.
5
+ *
6
+ * ── 🔴 There is no steady interval, and that is the whole point ──────────────────
7
+ *
8
+ * This used to end with `else: the steady interval` — `intervalMs: 5 * 60_000`, a
9
+ * dial at a configured peer every five minutes whether or not either side had
10
+ * anything to say. The owner ruled that out on 2026-09-15: *"There should be no
11
+ * polling in cb unless you can make some good case for it that beats the api
12
+ * option."* No such case was found, so the field is gone rather than defaulted to
13
+ * zero — a config key that reintroduces a poll is a config key somebody sets.
14
+ *
15
+ * What is left are the two wake-ups that have a REASON, and neither is a poll:
16
+ *
17
+ * · **debounce** — there are local ops to push. The side that has news says so;
18
+ * the wait only coalesces a burst.
19
+ * · **backoff** — the last attempt failed. This runs *only while disconnected*,
20
+ * never against a healthy peer, which is exactly the carve-out the ruling
21
+ * names for a reconnect timer.
22
+ *
23
+ * A clean, healthy loop returns `waitMs: null` — "nothing to do, do not book a
24
+ * timer" — and the process goes quiet until something happens. News from the far
25
+ * side arrives over `./signal`, not by asking.
5
26
  */
6
27
  export interface SyncLoopConfig {
7
- /** Steady poll cadence when reachable and idle. */
8
- intervalMs: number;
9
28
  /** How long after a local write to wait before syncing, so a burst coalesces. */
10
29
  debounceMs: number;
11
30
  /** First backoff step after a failure. */
@@ -22,10 +41,21 @@ export interface SyncLoopState {
22
41
  /** When the oldest un-synced local write happened, or null if clean. */
23
42
  dirtySince: number | null;
24
43
  }
25
- export declare function planNextSync(state: SyncLoopState, now: number, cfg?: SyncLoopConfig): {
44
+ /**
45
+ * What the loop should do next.
46
+ *
47
+ * 🔴 `waitMs: null` means **do not schedule anything** — not "wait zero" and not
48
+ * "wait forever". It is the idle state, and a caller that turns it into a number
49
+ * has put the poll back.
50
+ */
51
+ export interface SyncPlan {
26
52
  runNow: boolean;
27
- waitMs: number;
28
- };
53
+ /** Milliseconds until the next wake-up, or `null` when there is no reason to wake. */
54
+ waitMs: number | null;
55
+ /** Why the loop will wake — `null` alongside `waitMs: null`. */
56
+ reason: "retry" | "dirty" | null;
57
+ }
58
+ export declare function planNextSync(state: SyncLoopState, now: number, cfg?: SyncLoopConfig): SyncPlan;
29
59
  /** A peer that is asleep (the nightly EC2 window) or an internet-less Mac is NORMAL,
30
60
  * not an error. It still feeds the backoff. */
31
61
  export declare function isUnreachable(err: unknown): boolean;
@@ -1,10 +1,30 @@
1
1
  /**
2
- * The pure loop planner — vault's `planNextSync`, unchanged in spirit: back off after
3
- * a failure, else honor a debounced dirty flag, else the steady interval. Pure so
4
- * the timer is a thin wrapper and this is fake-clock testable.
2
+ * The pure loop planner: back off after a failure, else honor a debounced dirty
3
+ * flag, else **do not wake at all**. Pure so the timer is a thin wrapper and this
4
+ * is fake-clock testable.
5
+ *
6
+ * ── 🔴 There is no steady interval, and that is the whole point ──────────────────
7
+ *
8
+ * This used to end with `else: the steady interval` — `intervalMs: 5 * 60_000`, a
9
+ * dial at a configured peer every five minutes whether or not either side had
10
+ * anything to say. The owner ruled that out on 2026-09-15: *"There should be no
11
+ * polling in cb unless you can make some good case for it that beats the api
12
+ * option."* No such case was found, so the field is gone rather than defaulted to
13
+ * zero — a config key that reintroduces a poll is a config key somebody sets.
14
+ *
15
+ * What is left are the two wake-ups that have a REASON, and neither is a poll:
16
+ *
17
+ * · **debounce** — there are local ops to push. The side that has news says so;
18
+ * the wait only coalesces a burst.
19
+ * · **backoff** — the last attempt failed. This runs *only while disconnected*,
20
+ * never against a healthy peer, which is exactly the carve-out the ruling
21
+ * names for a reconnect timer.
22
+ *
23
+ * A clean, healthy loop returns `waitMs: null` — "nothing to do, do not book a
24
+ * timer" — and the process goes quiet until something happens. News from the far
25
+ * side arrives over `./signal`, not by asking.
5
26
  */
6
27
  export const DEFAULT_LOOP = {
7
- intervalMs: 5 * 60_000,
8
28
  debounceMs: 3_000,
9
29
  backoffBaseMs: 30_000,
10
30
  backoffMaxMs: 30 * 60_000,
@@ -13,14 +33,18 @@ export function planNextSync(state, now, cfg = DEFAULT_LOOP) {
13
33
  if (state.consecutiveFailures > 0) {
14
34
  const step = Math.min(cfg.backoffMaxMs, cfg.backoffBaseMs * 2 ** (state.consecutiveFailures - 1));
15
35
  const due = state.lastAttempt + step;
16
- return now >= due ? { runNow: true, waitMs: 0 } : { runNow: false, waitMs: due - now };
36
+ return now >= due
37
+ ? { runNow: true, waitMs: 0, reason: "retry" }
38
+ : { runNow: false, waitMs: due - now, reason: "retry" };
17
39
  }
18
40
  if (state.dirtySince !== null) {
19
41
  const due = state.dirtySince + cfg.debounceMs;
20
- return now >= due ? { runNow: true, waitMs: 0 } : { runNow: false, waitMs: due - now };
42
+ return now >= due
43
+ ? { runNow: true, waitMs: 0, reason: "dirty" }
44
+ : { runNow: false, waitMs: due - now, reason: "dirty" };
21
45
  }
22
- const due = state.lastAttempt + cfg.intervalMs;
23
- return now >= due ? { runNow: true, waitMs: 0 } : { runNow: false, waitMs: due - now };
46
+ // Clean and healthy: nothing to push, nothing to retry. Stay silent.
47
+ return { runNow: false, waitMs: null, reason: null };
24
48
  }
25
49
  /** A peer that is asleep (the nightly EC2 window) or an internet-less Mac is NORMAL,
26
50
  * not an error. It still feeds the backoff. */
@@ -0,0 +1,161 @@
1
+ /**
2
+ * **The peer says it has news. Nobody asks.**
3
+ *
4
+ * This is the half of the replication story that deleting the dialer (`./timer`,
5
+ * `./http`'s old `GET /requested`) would otherwise have left open, and it is the
6
+ * reason that deletion costs nothing.
7
+ *
8
+ * ## The three ways, and why this is the one
9
+ *
10
+ * Push-from-the-Mac answers Mac → peer completely: the side that has news calls
11
+ * `markDirty()` and pushes. It does not answer **peer → Mac**, and something must:
12
+ *
13
+ * | | |
14
+ * |---|---|
15
+ * | the Mac polls an outbox | ❌ polling. Ruled out 2026-09-15 |
16
+ * | the peer dials the Mac | ❌ needs the Mac reachable — a tunnel, an open port, a laptop that cannot sleep. That is the thing we are trying to delete |
17
+ * | 🟢 **the Mac holds a stream open and the peer pushes down it** | ✅ the Mac is still purely a client: it dials **out**, listens on nothing, and no timer fires |
18
+ *
19
+ * So: one long-lived outbound connection, opened by the dialing half, carrying a
20
+ * `news` frame whenever the receiver's op log grows. The Mac's reaction is
21
+ * `syncNow()` — the push it would have made anyway, at the moment there is a reason
22
+ * for it instead of 4,320 times a day on the chance.
23
+ *
24
+ * ## 🔴 Two honest caveats, neither of which is a poll
25
+ *
26
+ * · **A sleeping Mac drops the connection.** On wake it reconnects and catches up
27
+ * from `since` — the same catch-up a push model needs anyway, so it costs
28
+ * nothing extra. `since` is the receiver's head as the client last saw it.
29
+ * · **The reconnect backoff is a timer.** It runs *only while disconnected*, never
30
+ * against a healthy peer, and it is written below as an `await sleep()` in a
31
+ * loop rather than a `setTimeout` that calls `fetch` — the shape matters,
32
+ * because the second one is indistinguishable from the poll this file exists to
33
+ * replace. A connected client issues **exactly one** outbound request for as
34
+ * long as it stays connected, and `signal.spec.ts` measures that over 24
35
+ * simulated hours.
36
+ *
37
+ * ## Why this is not `cursedbelt-cc`'s `serverPush.ts`
38
+ *
39
+ * `libs/cursedbelt-cc/src/server/stream/serverPush.ts` is the real tenant-scoped
40
+ * push tier — heartbeats, backpressure, SSE — and on shape it is exactly this.
41
+ * It is not imported here for a reason that survives the preference:
42
+ * `cursedbelt-server/sync` is a **leaf** whose only runtime dependency is `hono`
43
+ * (see `./index.ts`), and `cursedbelt-cc` is a separate published package with zero
44
+ * consumers today, parked whole. Importing it would make every app that wants an
45
+ * op log install the cc platform to get one — the precise trade `./index.ts` was
46
+ * split to refuse, and the one `leafSubpathsImportNothing.spec.ts` now gates.
47
+ *
48
+ * The hub below is therefore the same model at a tenth the surface: this channel
49
+ * carries one frame kind, to one peer, with no tenant dimension. If `cursedbelt-cc`
50
+ * ever gains consumers and the two packages can depend on each other, {@link
51
+ * SyncSignalHub} is a strict subset of `ServerPushHub` and swaps in without
52
+ * touching a producer.
53
+ *
54
+ * ## The Workers path, named and not required yet
55
+ *
56
+ * On Cloudflare the durable form of "the Mac holds a connection all day" is a
57
+ * **Durable Object with WebSocket Hibernation**: the socket stays open while the
58
+ * object is evicted from memory, so an idle connection is not billed for duration.
59
+ * That is what keeps this free at rest, and it is the shape task `195` (the Mac is
60
+ * a client, never an origin) lands on. Nothing here needs it today — SSE over the
61
+ * existing receiver works on Bun and on Workers alike — but the frame format below
62
+ * is deliberately transport-free so the move is a transport swap, not a protocol
63
+ * change.
64
+ */
65
+ /** SSE frame headers. `x-accel-buffering` keeps nginx from holding the stream in a
66
+ * proxy buffer, which turns a live channel into a silent one — the same trap that
67
+ * makes a cached `200` sit over a dead origin elsewhere in this fleet. */
68
+ export declare const SIGNAL_SSE_HEADERS: Readonly<Record<string, string>>;
69
+ /** What the receiver tells the dialing half. One kind, deliberately. */
70
+ export interface SyncNews {
71
+ /** The receiver's op-log head at the moment it announced. The client's `since`. */
72
+ head: number;
73
+ /** The announcing instance, so a client linked to several can tell them apart. */
74
+ instanceId: string;
75
+ }
76
+ export interface SyncSignalHub {
77
+ /** Tell every listener the log grew. Never throws, never blocks on a socket. */
78
+ announce(news: SyncNews): void;
79
+ /** Attach a listener. The returned handle is safe to call more than once. */
80
+ subscribe(listener: (news: SyncNews) => void): () => void;
81
+ /** Live subscriber count — the reconnect tests assert against this. */
82
+ readonly subscriberCount: number;
83
+ /** Drop every subscriber (server shutdown). */
84
+ close(): void;
85
+ }
86
+ export declare function createSyncSignalHub(): SyncSignalHub;
87
+ export interface SignalStreamOptions {
88
+ /** Overridden only in tests, so the heartbeat can be driven by hand. */
89
+ setIntervalFn?: (fn: () => void, ms: number) => unknown;
90
+ clearIntervalFn?: (handle: unknown) => void;
91
+ heartbeatMs?: number;
92
+ /** Cancels the stream (the request's abort signal). */
93
+ signal?: AbortSignal;
94
+ /**
95
+ * Sent as the first frame so a client that reconnects after a sleep learns the
96
+ * current head without a round trip. This is the `since` catch-up.
97
+ */
98
+ initial?: SyncNews;
99
+ }
100
+ /**
101
+ * Bridge a hub subscription onto a `ReadableStream` of SSE bytes.
102
+ *
103
+ * The stream ends when the client disconnects (`signal`) or the hub closes; both
104
+ * paths run the same teardown, so a dropped connection cannot leave a subscription
105
+ * — or an interval — behind.
106
+ */
107
+ export declare function signalStream(hub: SyncSignalHub, options?: SignalStreamOptions): ReadableStream<Uint8Array>;
108
+ /** Serialize one announcement as an SSE frame. */
109
+ export declare const encodeNewsFrame: (news: SyncNews) => string;
110
+ /** A ready-to-return SSE `Response` — what the receiver's `GET /signal` answers. */
111
+ export declare const signalResponse: (hub: SyncSignalHub, options?: SignalStreamOptions) => Response;
112
+ export interface SignalClientOptions {
113
+ /** Where the peer mounted its receiver, e.g. `https://peer.example/api/sync`. */
114
+ basePath: string;
115
+ /** The same bearer the rest of the sync surface uses. */
116
+ token: string;
117
+ /** This instance's id, echoed as `x-sync-peer` so the far side can label us. */
118
+ selfId: string;
119
+ /**
120
+ * The peer has news. Wire this to {@link import("./timer").SyncTimerHandle.syncNow}
121
+ * — the whole point is that a reconciliation now happens for a REASON.
122
+ */
123
+ onNews: (news: SyncNews) => void;
124
+ fetchImpl?: typeof fetch;
125
+ /** First reconnect step. Doubles to {@link backoffMaxMs}. Default 1s. */
126
+ backoffBaseMs?: number;
127
+ /** Reconnect ceiling — an offline laptop settles here. Default 5 min. */
128
+ backoffMaxMs?: number;
129
+ /** Swapped in tests. Resolves after `ms`; never issues a request. */
130
+ sleepFn?: (ms: number) => Promise<void>;
131
+ /** Jitter in [0,1). Real randomness by default — see the note on the stampede. */
132
+ jitterFn?: () => number;
133
+ log?: (msg: string) => void;
134
+ }
135
+ export interface SignalClient {
136
+ /** Close the stream and stop reconnecting. Idempotent. */
137
+ stop(): void;
138
+ /** Whether a stream is open right now — the tests assert against this. */
139
+ readonly connected: boolean;
140
+ /** Outbound requests issued since start. 🔴 The number the ruling is about. */
141
+ readonly requestCount: number;
142
+ }
143
+ /**
144
+ * Hold one stream open against the peer, calling {@link SignalClientOptions.onNews}
145
+ * on every `news` frame, and reconnect with jittered backoff **while disconnected**.
146
+ *
147
+ * 🔴 Read the loop shape as load-bearing. The reconnect is `await sleep(ms)` inside
148
+ * an async loop, NOT a `setTimeout` whose callback calls `fetch`. The second shape
149
+ * is byte-for-byte what a poll looks like, and a reviewer — or a checker — cannot
150
+ * tell the two apart once it is written that way. Here, a connected client is
151
+ * parked on `reader.read()` and issues nothing; `requestCount` stops moving the
152
+ * moment a connection succeeds, and that is what `signal.spec.ts` asserts over 24
153
+ * simulated hours.
154
+ *
155
+ * The jitter is not decoration either: a fixed backoff across N clients recovering
156
+ * from the same outage is a synchronized stampede at the instant the peer comes
157
+ * back up — the worst possible moment to send it a thundering herd.
158
+ */
159
+ export declare function connectSyncSignal(options: SignalClientOptions): SignalClient;
160
+ /** `event: news\ndata: {...}` → {@link SyncNews}, or null for a heartbeat/unknown. */
161
+ export declare function parseNewsFrame(frame: string): SyncNews | null;
@@ -0,0 +1,348 @@
1
+ /**
2
+ * **The peer says it has news. Nobody asks.**
3
+ *
4
+ * This is the half of the replication story that deleting the dialer (`./timer`,
5
+ * `./http`'s old `GET /requested`) would otherwise have left open, and it is the
6
+ * reason that deletion costs nothing.
7
+ *
8
+ * ## The three ways, and why this is the one
9
+ *
10
+ * Push-from-the-Mac answers Mac → peer completely: the side that has news calls
11
+ * `markDirty()` and pushes. It does not answer **peer → Mac**, and something must:
12
+ *
13
+ * | | |
14
+ * |---|---|
15
+ * | the Mac polls an outbox | ❌ polling. Ruled out 2026-09-15 |
16
+ * | the peer dials the Mac | ❌ needs the Mac reachable — a tunnel, an open port, a laptop that cannot sleep. That is the thing we are trying to delete |
17
+ * | 🟢 **the Mac holds a stream open and the peer pushes down it** | ✅ the Mac is still purely a client: it dials **out**, listens on nothing, and no timer fires |
18
+ *
19
+ * So: one long-lived outbound connection, opened by the dialing half, carrying a
20
+ * `news` frame whenever the receiver's op log grows. The Mac's reaction is
21
+ * `syncNow()` — the push it would have made anyway, at the moment there is a reason
22
+ * for it instead of 4,320 times a day on the chance.
23
+ *
24
+ * ## 🔴 Two honest caveats, neither of which is a poll
25
+ *
26
+ * · **A sleeping Mac drops the connection.** On wake it reconnects and catches up
27
+ * from `since` — the same catch-up a push model needs anyway, so it costs
28
+ * nothing extra. `since` is the receiver's head as the client last saw it.
29
+ * · **The reconnect backoff is a timer.** It runs *only while disconnected*, never
30
+ * against a healthy peer, and it is written below as an `await sleep()` in a
31
+ * loop rather than a `setTimeout` that calls `fetch` — the shape matters,
32
+ * because the second one is indistinguishable from the poll this file exists to
33
+ * replace. A connected client issues **exactly one** outbound request for as
34
+ * long as it stays connected, and `signal.spec.ts` measures that over 24
35
+ * simulated hours.
36
+ *
37
+ * ## Why this is not `cursedbelt-cc`'s `serverPush.ts`
38
+ *
39
+ * `libs/cursedbelt-cc/src/server/stream/serverPush.ts` is the real tenant-scoped
40
+ * push tier — heartbeats, backpressure, SSE — and on shape it is exactly this.
41
+ * It is not imported here for a reason that survives the preference:
42
+ * `cursedbelt-server/sync` is a **leaf** whose only runtime dependency is `hono`
43
+ * (see `./index.ts`), and `cursedbelt-cc` is a separate published package with zero
44
+ * consumers today, parked whole. Importing it would make every app that wants an
45
+ * op log install the cc platform to get one — the precise trade `./index.ts` was
46
+ * split to refuse, and the one `leafSubpathsImportNothing.spec.ts` now gates.
47
+ *
48
+ * The hub below is therefore the same model at a tenth the surface: this channel
49
+ * carries one frame kind, to one peer, with no tenant dimension. If `cursedbelt-cc`
50
+ * ever gains consumers and the two packages can depend on each other, {@link
51
+ * SyncSignalHub} is a strict subset of `ServerPushHub` and swaps in without
52
+ * touching a producer.
53
+ *
54
+ * ## The Workers path, named and not required yet
55
+ *
56
+ * On Cloudflare the durable form of "the Mac holds a connection all day" is a
57
+ * **Durable Object with WebSocket Hibernation**: the socket stays open while the
58
+ * object is evicted from memory, so an idle connection is not billed for duration.
59
+ * That is what keeps this free at rest, and it is the shape task `195` (the Mac is
60
+ * a client, never an origin) lands on. Nothing here needs it today — SSE over the
61
+ * existing receiver works on Bun and on Workers alike — but the frame format below
62
+ * is deliberately transport-free so the move is a transport swap, not a protocol
63
+ * change.
64
+ */
65
+ /** SSE frame headers. `x-accel-buffering` keeps nginx from holding the stream in a
66
+ * proxy buffer, which turns a live channel into a silent one — the same trap that
67
+ * makes a cached `200` sit over a dead origin elsewhere in this fleet. */
68
+ export const SIGNAL_SSE_HEADERS = Object.freeze({
69
+ "content-type": "text/event-stream",
70
+ "cache-control": "no-store",
71
+ connection: "keep-alive",
72
+ "x-accel-buffering": "no",
73
+ });
74
+ /**
75
+ * Comment-frame interval. Without it an idle stream dies silently behind a proxy
76
+ * with an idle timeout and the client never learns it should reconnect.
77
+ *
78
+ * 🔴 This is a heartbeat on an ALREADY-OPEN socket, not a request. It is row (c) of
79
+ * the 2026-09-15 ruling — a local housekeeping timer that issues no outbound
80
+ * request — and it is why that row had to be carved out explicitly: a rule that
81
+ * said "no `setInterval`" would have deleted the one mechanism that keeps a
82
+ * no-polling design alive.
83
+ */
84
+ const HEARTBEAT_MS = 15_000;
85
+ export function createSyncSignalHub() {
86
+ const listeners = new Set();
87
+ let closed = false;
88
+ return {
89
+ announce(news) {
90
+ if (closed)
91
+ return;
92
+ // Snapshot: a listener that unsubscribes itself mid-fanout (the normal
93
+ // teardown path) must not mutate the set being iterated.
94
+ for (const listener of [...listeners]) {
95
+ try {
96
+ listener(news);
97
+ }
98
+ catch {
99
+ // A broken transport is that subscriber's problem, never the
100
+ // producer's: drop it and keep fanning out. An `announce` may be
101
+ // called from inside an op-log write, so it can never throw upward.
102
+ listeners.delete(listener);
103
+ }
104
+ }
105
+ },
106
+ subscribe(listener) {
107
+ if (closed)
108
+ return () => undefined;
109
+ listeners.add(listener);
110
+ return () => {
111
+ listeners.delete(listener);
112
+ };
113
+ },
114
+ get subscriberCount() {
115
+ return listeners.size;
116
+ },
117
+ close() {
118
+ if (closed)
119
+ return;
120
+ closed = true;
121
+ listeners.clear();
122
+ },
123
+ };
124
+ }
125
+ /**
126
+ * Bridge a hub subscription onto a `ReadableStream` of SSE bytes.
127
+ *
128
+ * The stream ends when the client disconnects (`signal`) or the hub closes; both
129
+ * paths run the same teardown, so a dropped connection cannot leave a subscription
130
+ * — or an interval — behind.
131
+ */
132
+ export function signalStream(hub, options = {}) {
133
+ const heartbeatMs = options.heartbeatMs ?? HEARTBEAT_MS;
134
+ const setIntervalFn = options.setIntervalFn ?? ((fn, ms) => setInterval(fn, ms));
135
+ const clearIntervalFn = options.clearIntervalFn ??
136
+ ((handle) => clearInterval(handle));
137
+ const encoder = new TextEncoder();
138
+ let unsubscribe = () => undefined;
139
+ let heartbeat = null;
140
+ let done = false;
141
+ return new ReadableStream({
142
+ start(controller) {
143
+ const teardown = () => {
144
+ if (done)
145
+ return;
146
+ done = true;
147
+ unsubscribe();
148
+ if (heartbeat !== null)
149
+ clearIntervalFn(heartbeat);
150
+ heartbeat = null;
151
+ try {
152
+ controller.close();
153
+ }
154
+ catch {
155
+ // Already closed by the runtime when the socket died — expected.
156
+ }
157
+ };
158
+ const write = (chunk) => {
159
+ if (done)
160
+ return;
161
+ try {
162
+ controller.enqueue(encoder.encode(chunk));
163
+ }
164
+ catch {
165
+ teardown();
166
+ }
167
+ };
168
+ if (options.initial)
169
+ write(encodeNewsFrame(options.initial));
170
+ unsubscribe = hub.subscribe((news) => {
171
+ write(encodeNewsFrame(news));
172
+ });
173
+ heartbeat = setIntervalFn(() => {
174
+ // A comment frame. It wakes no client-side handler and issues no
175
+ // request — it keeps proxies from calling the connection idle.
176
+ write(": keep-alive\n\n");
177
+ }, heartbeatMs);
178
+ if (options.signal) {
179
+ if (options.signal.aborted)
180
+ teardown();
181
+ else
182
+ options.signal.addEventListener("abort", teardown, { once: true });
183
+ }
184
+ },
185
+ cancel() {
186
+ done = true;
187
+ unsubscribe();
188
+ if (heartbeat !== null)
189
+ clearIntervalFn(heartbeat);
190
+ heartbeat = null;
191
+ },
192
+ });
193
+ }
194
+ /** Serialize one announcement as an SSE frame. */
195
+ export const encodeNewsFrame = (news) => `event: news\ndata: ${JSON.stringify(news)}\n\n`;
196
+ /** A ready-to-return SSE `Response` — what the receiver's `GET /signal` answers. */
197
+ export const signalResponse = (hub, options = {}) => new Response(signalStream(hub, options), { headers: { ...SIGNAL_SSE_HEADERS } });
198
+ /**
199
+ * Hold one stream open against the peer, calling {@link SignalClientOptions.onNews}
200
+ * on every `news` frame, and reconnect with jittered backoff **while disconnected**.
201
+ *
202
+ * 🔴 Read the loop shape as load-bearing. The reconnect is `await sleep(ms)` inside
203
+ * an async loop, NOT a `setTimeout` whose callback calls `fetch`. The second shape
204
+ * is byte-for-byte what a poll looks like, and a reviewer — or a checker — cannot
205
+ * tell the two apart once it is written that way. Here, a connected client is
206
+ * parked on `reader.read()` and issues nothing; `requestCount` stops moving the
207
+ * moment a connection succeeds, and that is what `signal.spec.ts` asserts over 24
208
+ * simulated hours.
209
+ *
210
+ * The jitter is not decoration either: a fixed backoff across N clients recovering
211
+ * from the same outage is a synchronized stampede at the instant the peer comes
212
+ * back up — the worst possible moment to send it a thundering herd.
213
+ */
214
+ export function connectSyncSignal(options) {
215
+ const doFetch = options.fetchImpl ?? fetch;
216
+ const base = options.basePath.replace(/\/$/, "");
217
+ const backoffBase = options.backoffBaseMs ?? 1_000;
218
+ const backoffMax = options.backoffMaxMs ?? 5 * 60_000;
219
+ const sleep = options.sleepFn ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
220
+ const jitter = options.jitterFn ?? Math.random;
221
+ const log = options.log ?? (() => undefined);
222
+ let stopped = false;
223
+ let connected = false;
224
+ let requests = 0;
225
+ let failures = 0;
226
+ let abort = null;
227
+ /** Open one stream and stay on it until it ends. Throws on any failure. */
228
+ const runOnce = async () => {
229
+ const controller = new AbortController();
230
+ abort = controller;
231
+ requests += 1;
232
+ const response = await doFetch(`${base}/signal?peer=${encodeURIComponent(options.selfId)}`, {
233
+ // 🔴 A followed redirect is how a moved hostname became eight days of
234
+ // silence on the `/pull` side — see `./http.ts`'s header. Same rule here.
235
+ redirect: "manual",
236
+ headers: {
237
+ authorization: `Bearer ${options.token}`,
238
+ "x-sync-peer": options.selfId,
239
+ accept: "text/event-stream",
240
+ },
241
+ signal: controller.signal,
242
+ });
243
+ if (response.status >= 300 && response.status < 400) {
244
+ throw new Error(`sync/signal: ${response.status} — the peer address redirects to ` +
245
+ `${response.headers.get("location") ?? "an unnamed destination"}.`);
246
+ }
247
+ if (!response.ok)
248
+ throw new Error(`sync/signal: ${response.status}`);
249
+ if (!response.body)
250
+ throw new Error("sync/signal: the peer sent no stream body");
251
+ connected = true;
252
+ failures = 0;
253
+ const reader = response.body.getReader();
254
+ const decoder = new TextDecoder();
255
+ let buffer = "";
256
+ try {
257
+ while (!stopped) {
258
+ const { done, value } = await reader.read();
259
+ if (done)
260
+ break;
261
+ buffer += decoder.decode(value, { stream: true });
262
+ // SSE frames are separated by a blank line. A partial tail stays in
263
+ // the buffer until the rest of it arrives.
264
+ let split = buffer.indexOf("\n\n");
265
+ while (split !== -1) {
266
+ const frame = buffer.slice(0, split);
267
+ buffer = buffer.slice(split + 2);
268
+ const news = parseNewsFrame(frame);
269
+ if (news)
270
+ options.onNews(news);
271
+ split = buffer.indexOf("\n\n");
272
+ }
273
+ }
274
+ }
275
+ finally {
276
+ connected = false;
277
+ try {
278
+ await reader.cancel();
279
+ }
280
+ catch {
281
+ // The socket is already gone — that is the normal end of a stream.
282
+ }
283
+ }
284
+ };
285
+ /**
286
+ * The supervisor. Every `await` here is either the open stream itself or a sleep
287
+ * between failures; there is no path that waits on a healthy peer in order to
288
+ * ask it something.
289
+ */
290
+ const supervise = async () => {
291
+ while (!stopped) {
292
+ try {
293
+ await runOnce();
294
+ }
295
+ catch (error) {
296
+ if (stopped)
297
+ return;
298
+ failures += 1;
299
+ if (failures === 1) {
300
+ log(`[sync] signal stream lost — reconnecting. This is normal (peer asleep / offline): ` +
301
+ `${error instanceof Error ? error.message : String(error)}`);
302
+ }
303
+ }
304
+ if (stopped)
305
+ return;
306
+ const step = Math.min(backoffMax, backoffBase * 2 ** Math.max(0, failures - 1));
307
+ await sleep(Math.round(step * (0.5 + jitter() * 0.5)));
308
+ }
309
+ };
310
+ void supervise();
311
+ return {
312
+ stop() {
313
+ stopped = true;
314
+ abort?.abort();
315
+ abort = null;
316
+ },
317
+ get connected() {
318
+ return connected;
319
+ },
320
+ get requestCount() {
321
+ return requests;
322
+ },
323
+ };
324
+ }
325
+ /** `event: news\ndata: {...}` → {@link SyncNews}, or null for a heartbeat/unknown. */
326
+ export function parseNewsFrame(frame) {
327
+ let isNews = false;
328
+ let data = null;
329
+ for (const line of frame.split("\n")) {
330
+ if (line.startsWith(":"))
331
+ continue; // comment frame — the heartbeat
332
+ if (line.startsWith("event:"))
333
+ isNews = line.slice(6).trim() === "news";
334
+ else if (line.startsWith("data:"))
335
+ data = line.slice(5).trim();
336
+ }
337
+ if (!isNews || data === null)
338
+ return null;
339
+ try {
340
+ const parsed = JSON.parse(data);
341
+ if (typeof parsed.head !== "number" || typeof parsed.instanceId !== "string")
342
+ return null;
343
+ return { head: parsed.head, instanceId: parsed.instanceId };
344
+ }
345
+ catch {
346
+ return null;
347
+ }
348
+ }