@gusto/baerly-storage 0.5.0 → 0.7.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.
- package/README.md +47 -124
- package/dist/API.md +161 -511
- package/dist/CHANGELOG.md +1148 -307
- package/dist/RECIPES.md +398 -80
- package/dist/THIRD-PARTY-LICENSES.txt +5 -26
- package/dist/{app-config-DyiFK3_k.d.ts → app-config-C6qW7m8S.d.ts} +119 -111
- package/dist/app-config.d.ts +31 -1
- package/dist/app-config.js +1 -2
- package/dist/app-config.js.map +1 -1
- package/dist/auth.d.ts +33 -28
- package/dist/auth.js +549 -985
- package/dist/auth.js.map +1 -1
- package/dist/baerly.js +130 -138
- package/dist/baerly.js.map +1 -1
- package/dist/baerly.spec.json +97 -22
- package/dist/capacity-warning-header-BlVopyiI.js +16 -0
- package/dist/capacity-warning-header-BlVopyiI.js.map +1 -0
- package/dist/{client-Fe8CwB73.d.ts → client-CHQTXpef.d.ts} +121 -36
- package/dist/client-react.d.ts +67 -20
- package/dist/client-react.js +240 -54
- package/dist/client-react.js.map +1 -1
- package/dist/client.d.ts +5 -4
- package/dist/client.js +51 -16
- package/dist/client.js.map +1 -1
- package/dist/{cloudflare-DzKBrUlg.js → cloudflare-CKHYXccw.js} +504 -124
- package/dist/cloudflare-CKHYXccw.js.map +1 -0
- package/dist/cloudflare.d.ts +106 -71
- package/dist/cloudflare.js +334 -271
- package/dist/cloudflare.js.map +1 -1
- package/dist/code-resolution-CL_7L0wS.js +22 -0
- package/dist/code-resolution-CL_7L0wS.js.map +1 -0
- package/dist/collection-api-DbyhLAY8.d.ts +1001 -0
- package/dist/config-DEzInG6M.js +4035 -0
- package/dist/config-DEzInG6M.js.map +1 -0
- package/dist/constants-D3GSH3tc.js +469 -0
- package/dist/constants-D3GSH3tc.js.map +1 -0
- package/dist/{context-DRZ3tZ5l.js → context-rlpSnHFA.js} +17 -17
- package/dist/context-rlpSnHFA.js.map +1 -0
- package/dist/cost-B52llVDx.js +330 -0
- package/dist/cost-B52llVDx.js.map +1 -0
- package/dist/{current-json-C6nhPOm1.js → current-json-CC6lE4Aj.js} +115 -32
- package/dist/current-json-CC6lE4Aj.js.map +1 -0
- package/dist/db-plFUNTSF.d.ts +130 -0
- package/dist/db-read-seams-BSs0m1z1.js +3261 -0
- package/dist/db-read-seams-BSs0m1z1.js.map +1 -0
- package/dist/{deploy-DonTr-CV.js → deploy-Obj4nE-Z.js} +12 -16
- package/dist/deploy-Obj4nE-Z.js.map +1 -0
- package/dist/{dev-banner-CWHIk8tH.d.ts → dev-banner-B5AUHmu1.d.ts} +1 -4
- package/dist/dev-landing-BI5UdmOb.d.ts +32 -0
- package/dist/dev-landing-CD-si4dW.js +50 -0
- package/dist/dev-landing-CD-si4dW.js.map +1 -0
- package/dist/dev-vite.d.ts +38 -39
- package/dist/dev-vite.js +67 -36
- package/dist/dev-vite.js.map +1 -1
- package/dist/dev.d.ts +3 -2
- package/dist/dev.js +2 -1
- package/dist/doctor-BA0i-YAN.js +270 -0
- package/dist/doctor-BA0i-YAN.js.map +1 -0
- package/dist/dump-D1vX8ozg.js +133 -0
- package/dist/dump-D1vX8ozg.js.map +1 -0
- package/dist/errors-7Rk1HGCE.js +160 -0
- package/dist/errors-7Rk1HGCE.js.map +1 -0
- package/dist/{validate-DtljsCh1.js → export-BVL2EvYE.js} +409 -19
- package/dist/export-BVL2EvYE.js.map +1 -0
- package/dist/fsck-BWkPgov4.js +145 -0
- package/dist/fsck-BWkPgov4.js.map +1 -0
- package/dist/gcs-admin-Dso1HWQj.d.ts +127 -0
- package/dist/gcs.d.ts +30 -0
- package/dist/gcs.js +1 -0
- package/dist/goog4-signer-CeLIABoE.js +331 -0
- package/dist/goog4-signer-CeLIABoE.js.map +1 -0
- package/dist/{http-DGd5tbNs.js → http-Z5rZyYMX.js} +898 -271
- package/dist/http-Z5rZyYMX.js.map +1 -0
- package/dist/http-transport-DNUM8kU-.js +1299 -0
- package/dist/http-transport-DNUM8kU-.js.map +1 -0
- package/dist/http.d.ts +366 -294
- package/dist/http.js +2 -2
- package/dist/index-JJrIu1pO.d.ts +135 -0
- package/dist/index-LEgHN0qJ.d.ts +50 -0
- package/dist/{index-CuA9Mi3K.d.ts → index-RiEYj8t0.d.ts} +291 -78
- package/dist/index.d.ts +27 -6
- package/dist/index.js +3 -5
- package/dist/inspect-BTsVYZKd.js +196 -0
- package/dist/inspect-BTsVYZKd.js.map +1 -0
- package/dist/{key-Di6yyxFu.js → key-C4FPZ9Io.js} +3 -4
- package/dist/{key-Di6yyxFu.js.map → key-C4FPZ9Io.js.map} +1 -1
- package/dist/ledger-Dj_bGgo0.js +192 -0
- package/dist/ledger-Dj_bGgo0.js.map +1 -0
- package/dist/legacy-names-DgZBtgOh.js +47 -0
- package/dist/legacy-names-DgZBtgOh.js.map +1 -0
- package/dist/log-C9W15Dfo.d.ts +94 -0
- package/dist/log-tail-DWJbneBF.js +142 -0
- package/dist/log-tail-DWJbneBF.js.map +1 -0
- package/dist/log-walk-C-vA-WGJ.js +111 -0
- package/dist/log-walk-C-vA-WGJ.js.map +1 -0
- package/dist/maintenance-Dd-Yakqj.js +1709 -0
- package/dist/maintenance-Dd-Yakqj.js.map +1 -0
- package/dist/maintenance-DevB6z4e.js +2237 -0
- package/dist/maintenance-DevB6z4e.js.map +1 -0
- package/dist/maintenance.d.ts +100 -3
- package/dist/maintenance.js +1 -3
- package/dist/node.d.ts +130 -142
- package/dist/node.js +4 -3
- package/dist/{observability-BmflLCdF.js → observability-C-ISCCLE.js} +1275 -332
- package/dist/observability-C-ISCCLE.js.map +1 -0
- package/dist/observability.d.ts +27 -3
- package/dist/observability.js +3 -3
- package/dist/path-segment-DBXG26PW.js +363 -0
- package/dist/path-segment-DBXG26PW.js.map +1 -0
- package/dist/{plan-a7PJHBCy.js → plan-BvQbaNMe.js} +19 -11
- package/dist/plan-BvQbaNMe.js.map +1 -0
- package/dist/request-CRWiTrza.js +272 -0
- package/dist/request-CRWiTrza.js.map +1 -0
- package/dist/restore-U6Im8V1V.js +1467 -0
- package/dist/restore-U6Im8V1V.js.map +1 -0
- package/dist/retry-OBtLB2GC.js +126 -0
- package/dist/retry-OBtLB2GC.js.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.js +12 -0
- package/dist/{runtime-spec-Dsw2-n8S.js → runtime-spec-B5mg3pHF.js} +189 -28
- package/dist/runtime-spec-B5mg3pHF.js.map +1 -0
- package/dist/{s3-http-B-z8th6O.d.ts → s3-http-DBC_wiCL.d.ts} +18 -10
- package/dist/s3.d.ts +20 -7
- package/dist/s3.js +4 -5
- package/dist/s3.js.map +1 -1
- package/dist/sha256-4FfGQNPw.js +22 -0
- package/dist/sha256-4FfGQNPw.js.map +1 -0
- package/dist/{shared-secret-DgEicd3x.js → shared-secret-Vkur-8r0.js} +27 -15
- package/dist/shared-secret-Vkur-8r0.js.map +1 -0
- package/dist/sigv4-sign-5IUflUD_.js +507 -0
- package/dist/sigv4-sign-5IUflUD_.js.map +1 -0
- package/dist/snapshot-CknzkDM0.js +148 -0
- package/dist/snapshot-CknzkDM0.js.map +1 -0
- package/dist/{src-BL_JoPTn.js → src-Actga-bO.js} +1004 -378
- package/dist/src-Actga-bO.js.map +1 -0
- package/dist/src-D0MQfvOp.js +812 -0
- package/dist/src-D0MQfvOp.js.map +1 -0
- package/dist/src-Dr9uo3dc.js +501 -0
- package/dist/src-Dr9uo3dc.js.map +1 -0
- package/dist/{subcommand-DoDA-K7d.js → subcommand-BLoF66dG.js} +89 -16
- package/dist/subcommand-BLoF66dG.js.map +1 -0
- package/dist/{time-DUqvFMHq.js → time-DRxTP9iv.js} +15 -34
- package/dist/time-DRxTP9iv.js.map +1 -0
- package/dist/types-suVbsEWS.d.ts +116 -0
- package/dist/{usage-DBV1eQHC.js → usage-CPzVhrQr.js} +40 -45
- package/dist/usage-CPzVhrQr.js.map +1 -0
- package/dist/uuid-BuT_c404.js +333 -0
- package/dist/uuid-BuT_c404.js.map +1 -0
- package/dist/verifier-Bmlmndzo.d.ts +95 -0
- package/dist/{wire-izfGZDvr.js → wire-DndvKU-K.js} +2 -3
- package/dist/wire-DndvKU-K.js.map +1 -0
- package/package.json +96 -69
- package/dist/aws4fetch.esm-p7eEzLCs.js +0 -1702
- package/dist/aws4fetch.esm-p7eEzLCs.js.map +0 -1
- package/dist/bytes-BXdamO3L.js +0 -11
- package/dist/bytes-BXdamO3L.js.map +0 -1
- package/dist/cloudflare-DzKBrUlg.js.map +0 -1
- package/dist/config-1tv0-Yzm.js +0 -2693
- package/dist/config-1tv0-Yzm.js.map +0 -1
- package/dist/constants-CfeMWQWk.js +0 -222
- package/dist/constants-CfeMWQWk.js.map +0 -1
- package/dist/context-DRZ3tZ5l.js.map +0 -1
- package/dist/cost-DSMIVqzL.js +0 -203
- package/dist/cost-DSMIVqzL.js.map +0 -1
- package/dist/current-json-C6nhPOm1.js.map +0 -1
- package/dist/db-DsPkOVEH.d.ts +0 -254
- package/dist/deploy-DonTr-CV.js.map +0 -1
- package/dist/doctor-DSIyCDg_.js +0 -346
- package/dist/doctor-DSIyCDg_.js.map +0 -1
- package/dist/dump-Bh5k26VQ.js +0 -94
- package/dist/dump-Bh5k26VQ.js.map +0 -1
- package/dist/errors-Dfa3p78c.js +0 -78
- package/dist/errors-Dfa3p78c.js.map +0 -1
- package/dist/export-DtF5tDIM.js +0 -401
- package/dist/export-DtF5tDIM.js.map +0 -1
- package/dist/fsck-D_djMO5D.js +0 -279
- package/dist/fsck-D_djMO5D.js.map +0 -1
- package/dist/http-DGd5tbNs.js.map +0 -1
- package/dist/index-0rZcMBwy.d.ts +0 -115
- package/dist/index-BNRh23K9.d.ts +0 -162
- package/dist/index-BYk9oCNf.d.ts +0 -1112
- package/dist/indexes-Dn9hbGQG.d.ts +0 -22
- package/dist/inspect-6WZ-pC7-.js +0 -133
- package/dist/inspect-6WZ-pC7-.js.map +0 -1
- package/dist/log-tail-D98U1JBo.js +0 -522
- package/dist/log-tail-D98U1JBo.js.map +0 -1
- package/dist/maintenance-B4Dki6Ig.js +0 -2157
- package/dist/maintenance-B4Dki6Ig.js.map +0 -1
- package/dist/maintenance-B_e0T99L.d.ts +0 -537
- package/dist/maintenance-C7coQp4c.js +0 -1418
- package/dist/maintenance-C7coQp4c.js.map +0 -1
- package/dist/normalize-rjnZLwS4.js +0 -133
- package/dist/normalize-rjnZLwS4.js.map +0 -1
- package/dist/observability-BmflLCdF.js.map +0 -1
- package/dist/plan-a7PJHBCy.js.map +0 -1
- package/dist/query-rAtXNQQB.js +0 -1773
- package/dist/query-rAtXNQQB.js.map +0 -1
- package/dist/rebuild-index-BVzQSZlm.js +0 -81
- package/dist/rebuild-index-BVzQSZlm.js.map +0 -1
- package/dist/request-u0M51mhJ.js +0 -72
- package/dist/request-u0M51mhJ.js.map +0 -1
- package/dist/restore-R-4rbciV.js +0 -778
- package/dist/restore-R-4rbciV.js.map +0 -1
- package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
- package/dist/runtime-spec-Dsw2-n8S.js.map +0 -1
- package/dist/shared-secret-DgEicd3x.js.map +0 -1
- package/dist/src-BL_JoPTn.js.map +0 -1
- package/dist/src-CYHFDrKz.js +0 -536
- package/dist/src-CYHFDrKz.js.map +0 -1
- package/dist/src-DYZ4H1aF.js +0 -392
- package/dist/src-DYZ4H1aF.js.map +0 -1
- package/dist/subcommand-DoDA-K7d.js.map +0 -1
- package/dist/time-DUqvFMHq.js.map +0 -1
- package/dist/usage-DBV1eQHC.js.map +0 -1
- package/dist/validate-DtljsCh1.js.map +0 -1
- package/dist/wire-izfGZDvr.js.map +0 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { s as BaerlyError } from "./baerly.js";
|
|
2
|
+
import { u as decodeJsonBytes } from "./current-json-CC6lE4Aj.js";
|
|
3
|
+
import { v as LOG_FORWARD_PROBE_CAP } from "./config-DEzInG6M.js";
|
|
4
|
+
/**
|
|
5
|
+
* The single constructor for a log-object key:
|
|
6
|
+
* `<manifestPrefix>/log/<seq>.json`. `manifestPrefix` is the
|
|
7
|
+
* `<…>/manifests/<collection>` segment — this helper adds `/log/`.
|
|
8
|
+
* `<seq>` is unpadded decimal, so lexicographic LIST order is not numeric
|
|
9
|
+
* order (`log/10.json` precedes `log/2.json`). The descending base-32 LSN
|
|
10
|
+
* encoding applies to LSN values and the LSN half of `/v1/since` cursors,
|
|
11
|
+
* not these keys.
|
|
12
|
+
* Route all callers here so the key shape lives in one place.
|
|
13
|
+
*/
|
|
14
|
+
const logObjectKey = (manifestPrefix, seq) => `${manifestPrefix}/log/${seq}.json`;
|
|
15
|
+
// ../server/src/log-tail.ts
|
|
16
|
+
/**
|
|
17
|
+
* Tolerant forward-probe for the true committed log tail. The strict
|
|
18
|
+
* `[log_seq_start, hint)` range is dense (a hole is corruption, owned
|
|
19
|
+
* by `walkLogRange`); this owns only the tolerant `[hint, ∞)` probe,
|
|
20
|
+
* where `hint` is a lower bound — the real tail may sit at or past it.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* A storage call refused before it was sent: the request ledger's
|
|
24
|
+
* `CapacityExceeded`, or a platform subrequest-limit refusal (both carry
|
|
25
|
+
* `cause.refused`). Nothing was read or changed by the refused call.
|
|
26
|
+
*/
|
|
27
|
+
const isStorageRefusal = (error) => error instanceof BaerlyError && error.cause?.refused === true;
|
|
28
|
+
/**
|
|
29
|
+
* Probe a bounded chunk of committed entries from `hint`. An occupied
|
|
30
|
+
* chunk certifies only that the tail is at or past its exclusive bound.
|
|
31
|
+
* Decode failures throw by default. With tolerant occupancy probing,
|
|
32
|
+
* malformed slots remain occupied, `complete` becomes false, and probing
|
|
33
|
+
* continues to the first missing slot or cap.
|
|
34
|
+
*
|
|
35
|
+
* With `stopOnRefusal`, a slot GET refused before it was sent (see
|
|
36
|
+
* {@link isStorageRefusal}) ends the chunk instead of throwing: an
|
|
37
|
+
* "at least" chunk whose `lowerBound` is the refused slot, carrying the
|
|
38
|
+
* entries read before it, so a caller delivering prefixes keeps them.
|
|
39
|
+
*/
|
|
40
|
+
const probeTailChunk = async (storage, logPrefix, hint, opts) => {
|
|
41
|
+
const cap = opts?.cap ?? 1e5;
|
|
42
|
+
const getOpts = opts?.signal !== void 0 ? { signal: opts.signal } : void 0;
|
|
43
|
+
const entries = [];
|
|
44
|
+
let complete = true;
|
|
45
|
+
for (let i = 0; i < cap; i++) {
|
|
46
|
+
const seq = hint + i;
|
|
47
|
+
const key = logObjectKey(logPrefix, seq);
|
|
48
|
+
let got;
|
|
49
|
+
try {
|
|
50
|
+
got = await storage.get(key, getOpts);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (opts?.stopOnRefusal === true && isStorageRefusal(error)) return {
|
|
53
|
+
kind: "at-least",
|
|
54
|
+
lowerBound: seq,
|
|
55
|
+
entries,
|
|
56
|
+
complete
|
|
57
|
+
};
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
if (got === null) return {
|
|
61
|
+
kind: "exact",
|
|
62
|
+
tail: seq,
|
|
63
|
+
entries,
|
|
64
|
+
complete
|
|
65
|
+
};
|
|
66
|
+
opts?.onBody?.(got.body.byteLength);
|
|
67
|
+
try {
|
|
68
|
+
entries.push(decodeJsonBytes(got.body));
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (opts?.tolerateMalformed === true) {
|
|
71
|
+
complete = false;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
throw new BaerlyError("InvalidResponse", `probeTailChunk: malformed log entry at ${key}: ${error.message}`, { cause: error });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
kind: "at-least",
|
|
79
|
+
lowerBound: hint + cap,
|
|
80
|
+
entries,
|
|
81
|
+
complete
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Discover the true committed tail and fold entries in `[hint, tail)`.
|
|
86
|
+
* `tail` is the first empty seq (>= hint); `entries` are the
|
|
87
|
+
* LogEntries in `[hint, tail)` in seq order.
|
|
88
|
+
*
|
|
89
|
+
* Retention precondition: derive `hint` from a fresh `current.json` read
|
|
90
|
+
* and require `hint >= current.log_seq_start`; the usual start is
|
|
91
|
+
* `Math.max(current.log_seq_start, current.tail_hint)`. The floor and hint
|
|
92
|
+
* must come from that same manifest read. Entries below the floor may
|
|
93
|
+
* already be reclaimed, and this probe interprets the first missing slot
|
|
94
|
+
* as the tail. A sub-floor hint may therefore report a false tail and omit
|
|
95
|
+
* later live entries. This storage-level helper intentionally accepts no
|
|
96
|
+
* manifest and cannot enforce the precondition for its caller.
|
|
97
|
+
*
|
|
98
|
+
* `cap` bounds the walk; exhausting it THROWS `Internal` (never silently
|
|
99
|
+
* truncates) — see the cap-exhaustion comment below.
|
|
100
|
+
*/
|
|
101
|
+
const probeTailFrom = async (storage, logPrefix, hint, opts) => {
|
|
102
|
+
const result = await probeTailChunk(storage, logPrefix, hint, opts);
|
|
103
|
+
if (result.kind === "exact") return {
|
|
104
|
+
tail: result.tail,
|
|
105
|
+
entries: result.entries
|
|
106
|
+
};
|
|
107
|
+
throw new BaerlyError("Internal", `probeTailFrom: forward probe exceeded ${opts?.cap ?? 1e5} from hint ${hint} on ${logPrefix}`);
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* First empty log seq at/after `hint` — the slot a new commit creates —
|
|
111
|
+
* found by galloping search (`O(log gap)` GETs vs `probeTailFrom`'s
|
|
112
|
+
* `O(gap)`). Used by the writer's per-commit tail-find, where `tail_hint`
|
|
113
|
+
* can lag the true tail. Position only; no bodies.
|
|
114
|
+
*
|
|
115
|
+
* PRECONDITION: occupancy MUST be a dense prefix from `hint` (galloping
|
|
116
|
+
* assumes monotone occupied→empty; a hole ABOVE `hint` would be skipped
|
|
117
|
+
* and the gallop could return a tail past live entries). Contrast
|
|
118
|
+
* `probeTailFrom`, which stops at the first 404 and is hole-tolerant.
|
|
119
|
+
*/
|
|
120
|
+
const findLogTail = async (storage, logPrefix, hint, opts) => {
|
|
121
|
+
const getOpts = opts?.signal !== void 0 ? { signal: opts.signal } : void 0;
|
|
122
|
+
const exists = async (seq) => await storage.get(logObjectKey(logPrefix, seq), getOpts) !== null;
|
|
123
|
+
if (!await exists(hint)) return hint;
|
|
124
|
+
let lo = hint;
|
|
125
|
+
let step = 1;
|
|
126
|
+
let hi = hint + step;
|
|
127
|
+
while (await exists(hi)) {
|
|
128
|
+
lo = hi;
|
|
129
|
+
step *= 2;
|
|
130
|
+
hi = lo + step;
|
|
131
|
+
if (step > 1e5) throw new BaerlyError("Internal", `findLogTail: galloping probe exceeded ${LOG_FORWARD_PROBE_CAP} from hint ${hint} on ${logPrefix}`);
|
|
132
|
+
}
|
|
133
|
+
while (hi - lo > 1) {
|
|
134
|
+
const mid = lo + Math.floor((hi - lo) / 2);
|
|
135
|
+
if (await exists(mid)) lo = mid;
|
|
136
|
+
else hi = mid;
|
|
137
|
+
}
|
|
138
|
+
return hi;
|
|
139
|
+
};
|
|
140
|
+
export { logObjectKey as i, probeTailChunk as n, probeTailFrom as r, findLogTail as t };
|
|
141
|
+
|
|
142
|
+
//# sourceMappingURL=log-tail-DWJbneBF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-tail-DWJbneBF.js","names":[],"sources":["../packages/protocol/src/log-key.ts","../packages/server/src/log-tail.ts"],"sourcesContent":["/**\n * Log-object key construction — a zero-import leaf module.\n *\n * Kept separate from `log.ts` on purpose: `log.ts` carries the heavier\n * `lsn` parsing runtime (`lsnParts`, the LSN regex, `str2uintDesc`).\n * Routing `gc` / `writer` / `log-walk` through a key helper that lived in\n * `log.ts` dragged that whole module into the `maintenance.js` bundle\n * closure (+3.8 KB raw). This module imports nothing, so consumers that\n * only need the key shape pull only the key shape.\n */\n\n/** S3 path segment for log entries, under the manifest prefix. */\nexport const LOG_KEY_PREFIX = \"log\";\n\n/**\n * The single constructor for a log-object key:\n * `<manifestPrefix>/log/<seq>.json`. `manifestPrefix` is the\n * `<…>/manifests/<collection>` segment — this helper adds `/log/`.\n * `<seq>` is unpadded decimal, so lexicographic LIST order is not numeric\n * order (`log/10.json` precedes `log/2.json`). The descending base-32 LSN\n * encoding applies to LSN values and the LSN half of `/v1/since` cursors,\n * not these keys.\n * Route all callers here so the key shape lives in one place.\n */\nexport const logObjectKey = (manifestPrefix: string, seq: number): string =>\n `${manifestPrefix}/${LOG_KEY_PREFIX}/${seq}.json`;\n","/**\n * Tolerant forward-probe for the true committed log tail. The strict\n * `[log_seq_start, hint)` range is dense (a hole is corruption, owned\n * by `walkLogRange`); this owns only the tolerant `[hint, ∞)` probe,\n * where `hint` is a lower bound — the real tail may sit at or past it.\n */\n\nimport {\n type LogEntry,\n type RequestPool,\n type Storage,\n BaerlyError,\n currentRequestLedger,\n decodeJsonBytes,\n LOG_FORWARD_PROBE_CAP,\n logObjectKey,\n} from \"@baerly/protocol\";\n\n/** A forward probe's slot cap, and which bound set it. */\nexport interface ForwardProbeCap {\n /** Slots the probe may GET. */\n readonly slots: number;\n /**\n * `\"request\"` when the active invocation's request ledger set the cap:\n * exhausting it is a capacity refusal (`CapacityExceeded`), the whole\n * answer refused. `\"runaway\"` when {@link LOG_FORWARD_PROBE_CAP} did:\n * exhausting it is the `Internal` runaway alarm.\n */\n readonly bound: \"request\" | \"runaway\";\n}\n\n/**\n * The forward-probe cap for a read in the active invocation:\n * `min(LOG_FORWARD_PROBE_CAP, ledger remaining on pool)`. Every read path\n * that probes past `tail_hint` (collection reads, `/v1/since`) sizes its\n * probe here, so a probe never plans more GETs than the request can\n * issue, and running out is a capacity refusal rather than the runaway\n * alarm. Without a ledger (or with one that never refuses, as on Node) the\n * runaway cap applies.\n */\nexport const forwardProbeCap = (pool: RequestPool = \"internal\"): ForwardProbeCap => {\n const remaining = currentRequestLedger()?.remaining(pool) ?? Infinity;\n return remaining < LOG_FORWARD_PROBE_CAP\n ? { slots: Math.max(0, remaining), bound: \"request\" }\n : { slots: LOG_FORWARD_PROBE_CAP, bound: \"runaway\" };\n};\n\n/**\n * A storage call refused before it was sent: the request ledger's\n * `CapacityExceeded`, or a platform subrequest-limit refusal (both carry\n * `cause.refused`). Nothing was read or changed by the refused call.\n */\nexport const isStorageRefusal = (error: unknown): boolean =>\n error instanceof BaerlyError &&\n (error.cause as { refused?: boolean } | undefined)?.refused === true;\n\nexport type TailProbeChunk =\n | {\n readonly kind: \"exact\";\n readonly tail: number;\n readonly entries: LogEntry[];\n readonly complete: boolean;\n }\n | {\n readonly kind: \"at-least\";\n readonly lowerBound: number;\n readonly entries: LogEntry[];\n readonly complete: boolean;\n };\n\n/**\n * Probe a bounded chunk of committed entries from `hint`. An occupied\n * chunk certifies only that the tail is at or past its exclusive bound.\n * Decode failures throw by default. With tolerant occupancy probing,\n * malformed slots remain occupied, `complete` becomes false, and probing\n * continues to the first missing slot or cap.\n *\n * With `stopOnRefusal`, a slot GET refused before it was sent (see\n * {@link isStorageRefusal}) ends the chunk instead of throwing: an\n * \"at least\" chunk whose `lowerBound` is the refused slot, carrying the\n * entries read before it, so a caller delivering prefixes keeps them.\n */\nexport const probeTailChunk = async (\n storage: Storage,\n logPrefix: string,\n hint: number,\n opts?: {\n signal?: AbortSignal;\n cap?: number;\n tolerateMalformed?: boolean;\n /** Called with each occupied slot's stored body length before it is decoded; may throw to stop the probe. */\n onBody?: (bytes: number) => void;\n /** End the chunk at a refused slot GET rather than throw. */\n stopOnRefusal?: boolean;\n },\n): Promise<TailProbeChunk> => {\n const cap = opts?.cap ?? LOG_FORWARD_PROBE_CAP;\n const getOpts = opts?.signal !== undefined ? { signal: opts.signal } : undefined;\n const entries: LogEntry[] = [];\n let complete = true;\n for (let i = 0; i < cap; i++) {\n const seq = hint + i;\n const key = logObjectKey(logPrefix, seq);\n let got;\n try {\n got = await storage.get(key, getOpts);\n } catch (error) {\n if (opts?.stopOnRefusal === true && isStorageRefusal(error)) {\n return { kind: \"at-least\", lowerBound: seq, entries, complete };\n }\n throw error;\n }\n if (got === null) {\n return { kind: \"exact\", tail: seq, entries, complete };\n }\n opts?.onBody?.(got.body.byteLength);\n try {\n entries.push(decodeJsonBytes<LogEntry>(got.body));\n } catch (error) {\n if (opts?.tolerateMalformed === true) {\n complete = false;\n continue;\n }\n // A malformed body at an occupied slot is a protocol violation —\n // surface it as InvalidResponse (the same code readLogEntry uses)\n // rather than a raw SyntaxError leaking out of the probe.\n throw new BaerlyError(\n \"InvalidResponse\",\n `probeTailChunk: malformed log entry at ${key}: ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n return { kind: \"at-least\", lowerBound: hint + cap, entries, complete };\n};\n\n/**\n * Discover the true committed tail and fold entries in `[hint, tail)`.\n * `tail` is the first empty seq (>= hint); `entries` are the\n * LogEntries in `[hint, tail)` in seq order.\n *\n * Retention precondition: derive `hint` from a fresh `current.json` read\n * and require `hint >= current.log_seq_start`; the usual start is\n * `Math.max(current.log_seq_start, current.tail_hint)`. The floor and hint\n * must come from that same manifest read. Entries below the floor may\n * already be reclaimed, and this probe interprets the first missing slot\n * as the tail. A sub-floor hint may therefore report a false tail and omit\n * later live entries. This storage-level helper intentionally accepts no\n * manifest and cannot enforce the precondition for its caller.\n *\n * `cap` bounds the walk; exhausting it THROWS `Internal` (never silently\n * truncates) — see the cap-exhaustion comment below.\n */\nexport const probeTailFrom = async (\n storage: Storage,\n logPrefix: string,\n hint: number,\n opts?: { signal?: AbortSignal; cap?: number },\n): Promise<{ tail: number; entries: LogEntry[] }> => {\n const result = await probeTailChunk(storage, logPrefix, hint, opts);\n if (result.kind === \"exact\") {\n return { tail: result.tail, entries: result.entries };\n }\n // Cap exhausted without hitting a 404 — the true tail is past `cap`.\n // Returning `hint+cap` here would silently truncate every downstream\n // read (since.ts / query.ts / export), so we\n // THROW instead, mirroring `findLogTail`'s cap-exhaustion guard. With\n // the maintenance tick keeping `tail_hint` within\n // `MAINTENANCE_TAIL_HINT_REFRESH_WRITES` of the true tail (HR-2), this\n // is a pure runaway alarm — a >cap gap means maintenance is\n // broken/disabled (a graduation/operator concern), never normal\n // operation. No partial-accept escape hatch: no caller legitimately\n // needs to walk past the cap.\n throw new BaerlyError(\n \"Internal\",\n `probeTailFrom: forward probe exceeded ${opts?.cap ?? LOG_FORWARD_PROBE_CAP} from hint ${hint} on ${logPrefix}`,\n );\n};\n\n/**\n * {@link probeTailFrom} for a read, capped by {@link forwardProbeCap}. When\n * the request ledger set the cap and every capped slot is occupied, the\n * tail is past what this request can probe: throws `refusal()` (a whole\n * `CapacityExceeded` answer) rather than the runaway alarm. `onBody` sees\n * each stored body length before decode, as on {@link probeTailChunk}.\n *\n * @throws the `refusal()` error when the ledger cap is exhausted.\n * @throws BaerlyError code=\"Internal\" when {@link LOG_FORWARD_PROBE_CAP} is.\n */\nexport const probeReadTail = async (\n storage: Storage,\n logPrefix: string,\n hint: number,\n opts: {\n readonly refusal: () => BaerlyError;\n readonly pool?: RequestPool;\n readonly onBody?: (bytes: number) => void;\n readonly signal?: AbortSignal;\n },\n): Promise<{ tail: number; entries: LogEntry[] }> => {\n const cap = forwardProbeCap(opts.pool);\n const result = await probeTailChunk(storage, logPrefix, hint, {\n cap: cap.slots,\n ...(opts.onBody !== undefined && { onBody: opts.onBody }),\n ...(opts.signal !== undefined && { signal: opts.signal }),\n });\n if (result.kind === \"exact\") {\n return { tail: result.tail, entries: result.entries };\n }\n if (cap.bound === \"request\") {\n throw opts.refusal();\n }\n throw new BaerlyError(\n \"Internal\",\n `probeReadTail: forward probe exceeded ${cap.slots} from hint ${hint} on ${logPrefix}`,\n );\n};\n\n/**\n * First empty log seq at/after `hint` — the slot a new commit creates —\n * found by galloping search (`O(log gap)` GETs vs `probeTailFrom`'s\n * `O(gap)`). Used by the writer's per-commit tail-find, where `tail_hint`\n * can lag the true tail. Position only; no bodies.\n *\n * PRECONDITION: occupancy MUST be a dense prefix from `hint` (galloping\n * assumes monotone occupied→empty; a hole ABOVE `hint` would be skipped\n * and the gallop could return a tail past live entries). Contrast\n * `probeTailFrom`, which stops at the first 404 and is hole-tolerant.\n */\nexport const findLogTail = async (\n storage: Storage,\n logPrefix: string,\n hint: number,\n opts?: { signal?: AbortSignal },\n): Promise<number> => {\n const getOpts = opts?.signal !== undefined ? { signal: opts.signal } : undefined;\n const exists = async (seq: number): Promise<boolean> =>\n (await storage.get(logObjectKey(logPrefix, seq), getOpts)) !== null;\n if (!(await exists(hint))) {\n return hint;\n }\n // Gallop to bracket the tail in `(lo, hi]` (lo occupied, hi empty).\n let lo = hint;\n let step = 1;\n let hi = hint + step;\n while (await exists(hi)) {\n lo = hi;\n step *= 2;\n hi = lo + step;\n if (step > LOG_FORWARD_PROBE_CAP) {\n throw new BaerlyError(\n \"Internal\",\n `findLogTail: galloping probe exceeded ${LOG_FORWARD_PROBE_CAP} from hint ${hint} on ${logPrefix}`,\n );\n }\n }\n while (hi - lo > 1) {\n const mid = lo + Math.floor((hi - lo) / 2);\n if (await exists(mid)) {\n lo = mid;\n } else {\n hi = mid;\n }\n }\n return hi;\n};\n"],"mappings":";;;;;;;;;;;;;AAwBA,MAAa,gBAAgB,gBAAwB,QACnD,GAAG,eAAe,OAAqB,IAAI;;;;;;;;;;;;;AC2B7C,MAAa,oBAAoB,UAC/B,iBAAiB,eAChB,MAAM,OAA6C,YAAY;;;;;;;;;;;;;AA4BlE,MAAa,iBAAiB,OAC5B,SACA,WACA,MACA,SAS4B;CAC5B,MAAM,MAAM,MAAM,OAAA;CAClB,MAAM,UAAU,MAAM,WAAW,KAAA,IAAY,EAAE,QAAQ,KAAK,OAAO,IAAI,KAAA;CACvE,MAAM,UAAsB,CAAC;CAC7B,IAAI,WAAW;CACf,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK;EAC5B,MAAM,MAAM,OAAO;EACnB,MAAM,MAAM,aAAa,WAAW,GAAG;EACvC,IAAI;EACJ,IAAI;GACF,MAAM,MAAM,QAAQ,IAAI,KAAK,OAAO;EACtC,SAAS,OAAO;GACd,IAAI,MAAM,kBAAkB,QAAQ,iBAAiB,KAAK,GACxD,OAAO;IAAE,MAAM;IAAY,YAAY;IAAK;IAAS;GAAS;GAEhE,MAAM;EACR;EACA,IAAI,QAAQ,MACV,OAAO;GAAE,MAAM;GAAS,MAAM;GAAK;GAAS;EAAS;EAEvD,MAAM,SAAS,IAAI,KAAK,UAAU;EAClC,IAAI;GACF,QAAQ,KAAK,gBAA0B,IAAI,IAAI,CAAC;EAClD,SAAS,OAAO;GACd,IAAI,MAAM,sBAAsB,MAAM;IACpC,WAAW;IACX;GACF;GAIA,MAAM,IAAI,YACR,mBACA,0CAA0C,IAAI,IAAK,MAAgB,WACnE,EAAE,OAAO,MAAM,CACjB;EACF;CACF;CACA,OAAO;EAAE,MAAM;EAAY,YAAY,OAAO;EAAK;EAAS;CAAS;AACvE;;;;;;;;;;;;;;;;;;AAmBA,MAAa,gBAAgB,OAC3B,SACA,WACA,MACA,SACmD;CACnD,MAAM,SAAS,MAAM,eAAe,SAAS,WAAW,MAAM,IAAI;CAClE,IAAI,OAAO,SAAS,SAClB,OAAO;EAAE,MAAM,OAAO;EAAM,SAAS,OAAO;CAAQ;CAYtD,MAAM,IAAI,YACR,YACA,yCAAyC,MAAM,OAAA,IAA6B,aAAa,KAAK,MAAM,WACtG;AACF;;;;;;;;;;;;AAoDA,MAAa,cAAc,OACzB,SACA,WACA,MACA,SACoB;CACpB,MAAM,UAAU,MAAM,WAAW,KAAA,IAAY,EAAE,QAAQ,KAAK,OAAO,IAAI,KAAA;CACvE,MAAM,SAAS,OAAO,QACnB,MAAM,QAAQ,IAAI,aAAa,WAAW,GAAG,GAAG,OAAO,MAAO;CACjE,IAAI,CAAE,MAAM,OAAO,IAAI,GACrB,OAAO;CAGT,IAAI,KAAK;CACT,IAAI,OAAO;CACX,IAAI,KAAK,OAAO;CAChB,OAAO,MAAM,OAAO,EAAE,GAAG;EACvB,KAAK;EACL,QAAQ;EACR,KAAK,KAAK;EACV,IAAI,OAAA,KACF,MAAM,IAAI,YACR,YACA,yCAAyC,sBAAsB,aAAa,KAAK,MAAM,WACzF;CAEJ;CACA,OAAO,KAAK,KAAK,GAAG;EAClB,MAAM,MAAM,KAAK,KAAK,OAAO,KAAK,MAAM,CAAC;EACzC,IAAI,MAAM,OAAO,GAAG,GAClB,KAAK;OAEL,KAAK;CAET;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { s as BaerlyError } from "./baerly.js";
|
|
2
|
+
import { u as decodeJsonBytes } from "./current-json-CC6lE4Aj.js";
|
|
3
|
+
import { i as logObjectKey } from "./log-tail-DWJbneBF.js";
|
|
4
|
+
// ../server/src/log-walk.ts
|
|
5
|
+
/**
|
|
6
|
+
* Shared log-walk primitives consumed by the read path (`query.ts`),
|
|
7
|
+
* the compactor (`compactor.ts`), and the optional integrity walk in
|
|
8
|
+
* `Writer.#singleAttemptCommit` (`writer.ts`).
|
|
9
|
+
*
|
|
10
|
+
* Two operations: read one log entry, or walk a contiguous range of
|
|
11
|
+
* log entries with bounded parallelism. Each kernel call site
|
|
12
|
+
* previously inlined the same range-build + unbounded `Promise.all`
|
|
13
|
+
* pattern; on a free-tier collection where the compactor lags the
|
|
14
|
+
* tail to 50-100 entries, that fans out 50-100 concurrent GETs per
|
|
15
|
+
* call — and a contended writer multiplies that by its forward
|
|
16
|
+
* probes. `walkLogRange` caps concurrency at
|
|
17
|
+
* {@link MAX_PARALLEL_LOG_READS} so a single call costs at most that
|
|
18
|
+
* many simultaneous GETs against the storage backend.
|
|
19
|
+
*
|
|
20
|
+
* Order invariant: results are returned in seq order regardless of
|
|
21
|
+
* resolution order. The read-side fold is order-sensitive (`U` then
|
|
22
|
+
* `D` on the same doc must apply in that order); callers may iterate
|
|
23
|
+
* the returned array as-is.
|
|
24
|
+
*
|
|
25
|
+
* Signal invariant: when the caller's `AbortSignal` fires, no further
|
|
26
|
+
* chunks dispatch. In-flight reads see the signal via the underlying
|
|
27
|
+
* `Storage.get` call.
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Like {@link readLogEntry}, but also surfaces `bytes` — the
|
|
31
|
+
* byteLength of the STORED log-object body exactly as it was GET back
|
|
32
|
+
* from storage (`got.body.byteLength`).
|
|
33
|
+
*
|
|
34
|
+
* The compactor uses this to sum the byte cost of the folded slice.
|
|
35
|
+
* The stored body byteLength IS what the writer PUT for each entry,
|
|
36
|
+
* so summing `got.body.byteLength` over the folded range yields the
|
|
37
|
+
* exact cost — no re-encode, no round-trip assumption (a re-encode
|
|
38
|
+
* of the parsed `LogEntry` could drift if key-order/whitespace ever
|
|
39
|
+
* diverged from what was PUT).
|
|
40
|
+
*
|
|
41
|
+
* Same error semantics as {@link readLogEntry}.
|
|
42
|
+
*/
|
|
43
|
+
const readLogEntryWithBytes = async (storage, key, opts) => {
|
|
44
|
+
const got = await storage.get(key, opts?.signal !== void 0 ? { signal: opts.signal } : void 0);
|
|
45
|
+
if (got === null) throw new BaerlyError("Internal", `log-walk: missing log entry at ${key}; protocol invariant violation`);
|
|
46
|
+
opts?.onBody?.(got.body.byteLength);
|
|
47
|
+
let entry;
|
|
48
|
+
try {
|
|
49
|
+
entry = decodeJsonBytes(got.body);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw new BaerlyError("InvalidResponse", `log-walk: malformed log entry at ${key}`, { cause: error });
|
|
52
|
+
}
|
|
53
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) throw new BaerlyError("InvalidResponse", `log-walk: log entry at ${key} is not a JSON object`);
|
|
54
|
+
return {
|
|
55
|
+
entry,
|
|
56
|
+
bytes: got.body.byteLength
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Walk `[fromSeq, toSeqExclusive)` of `${logPrefix}/log/<seq>.json`
|
|
61
|
+
* in chunks of {@link MAX_PARALLEL_LOG_READS} concurrent GETs.
|
|
62
|
+
* Returns the materialised entries in seq order.
|
|
63
|
+
*
|
|
64
|
+
* Retention precondition: derive `fromSeq` from a fresh `current.json`
|
|
65
|
+
* read and require `fromSeq >= current.log_seq_start`. The floor and
|
|
66
|
+
* starting sequence must come from that same manifest read. Entries below
|
|
67
|
+
* the floor may already be reclaimed; a sub-floor walk that reaches one of
|
|
68
|
+
* those holes throws `Internal` even when later live entries still exist.
|
|
69
|
+
* This storage-level helper intentionally accepts no manifest and cannot
|
|
70
|
+
* enforce the precondition for its caller.
|
|
71
|
+
*
|
|
72
|
+
* Empty range (`fromSeq >= toSeqExclusive`) returns `[]` and issues
|
|
73
|
+
* zero GETs.
|
|
74
|
+
*
|
|
75
|
+
* Same error semantics as {@link readLogEntry}: a missing entry
|
|
76
|
+
* throws `Internal`, a malformed body throws `InvalidResponse`.
|
|
77
|
+
*/
|
|
78
|
+
const walkLogRange = async (storage, logPrefix, fromSeq, toSeqExclusive, opts) => {
|
|
79
|
+
return (await walkLogRangeWithBytes(storage, logPrefix, fromSeq, toSeqExclusive, opts)).map((r) => r.entry);
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Like {@link walkLogRange}, but each element also carries `bytes` —
|
|
83
|
+
* the STORED log-object body byteLength (see {@link readLogEntryWithBytes}).
|
|
84
|
+
* Same seq-order, empty-range, error, and bounded-parallelism
|
|
85
|
+
* ({@link MAX_PARALLEL_LOG_READS}) guarantees as {@link walkLogRange};
|
|
86
|
+
* this is the bytes-aware core `walkLogRange` is implemented on top of.
|
|
87
|
+
*
|
|
88
|
+
* The compactor consumes this so it gets BOTH the parsed entries (for
|
|
89
|
+
* the fold) AND the exact byte sum (for `mean_entry_bytes` stamping) from a
|
|
90
|
+
* single walk.
|
|
91
|
+
*/
|
|
92
|
+
const walkLogRangeWithBytes = async (storage, logPrefix, fromSeq, toSeqExclusive, opts) => {
|
|
93
|
+
if (fromSeq >= toSeqExclusive) return [];
|
|
94
|
+
const total = toSeqExclusive - fromSeq;
|
|
95
|
+
const out = Array.from({ length: total });
|
|
96
|
+
for (let chunkStart = 0; chunkStart < total; chunkStart += 16) {
|
|
97
|
+
opts?.signal?.throwIfAborted();
|
|
98
|
+
const chunkEnd = Math.min(chunkStart + 16, total);
|
|
99
|
+
const promises = [];
|
|
100
|
+
for (let i = chunkStart; i < chunkEnd; i++) {
|
|
101
|
+
const seq = fromSeq + i;
|
|
102
|
+
promises.push(readLogEntryWithBytes(storage, logObjectKey(logPrefix, seq), opts));
|
|
103
|
+
}
|
|
104
|
+
const results = await Promise.all(promises);
|
|
105
|
+
for (let i = 0; i < results.length; i++) out[chunkStart + i] = results[i];
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
};
|
|
109
|
+
export { walkLogRangeWithBytes as n, walkLogRange as t };
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=log-walk-C-vA-WGJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-walk-C-vA-WGJ.js","names":[],"sources":["../packages/server/src/log-walk.ts"],"sourcesContent":["/**\n * Shared log-walk primitives consumed by the read path (`query.ts`),\n * the compactor (`compactor.ts`), and the optional integrity walk in\n * `Writer.#singleAttemptCommit` (`writer.ts`).\n *\n * Two operations: read one log entry, or walk a contiguous range of\n * log entries with bounded parallelism. Each kernel call site\n * previously inlined the same range-build + unbounded `Promise.all`\n * pattern; on a free-tier collection where the compactor lags the\n * tail to 50-100 entries, that fans out 50-100 concurrent GETs per\n * call — and a contended writer multiplies that by its forward\n * probes. `walkLogRange` caps concurrency at\n * {@link MAX_PARALLEL_LOG_READS} so a single call costs at most that\n * many simultaneous GETs against the storage backend.\n *\n * Order invariant: results are returned in seq order regardless of\n * resolution order. The read-side fold is order-sensitive (`U` then\n * `D` on the same doc must apply in that order); callers may iterate\n * the returned array as-is.\n *\n * Signal invariant: when the caller's `AbortSignal` fires, no further\n * chunks dispatch. In-flight reads see the signal via the underlying\n * `Storage.get` call.\n */\n\nimport {\n type DocumentData,\n type LogEntry,\n type Storage,\n BaerlyError,\n decodeJsonBytes,\n logObjectKey,\n MAX_PARALLEL_LOG_READS,\n} from \"@baerly/protocol\";\nimport { isOwnCollection } from \"./collection-alias.ts\";\n\n/** Options for the log-walk reads. */\nexport interface LogWalkOptions {\n readonly signal?: AbortSignal;\n /**\n * Called with each entry's stored body length before it is decoded. A\n * throw rejects the walk: the read path's admission budget charges here.\n */\n readonly onBody?: (bytes: number) => void;\n}\n\n/**\n * Fetch a single log entry. Centralises the \"missing entry inside the\n * visible log range is a protocol-invariant violation\" rule used by\n * every caller.\n *\n * @throws BaerlyError code=\"Internal\" when the key resolves to no\n * body — a hole inside `[log_seq_start, tail_hint)` is a corruption\n * the kernel can't continue past.\n * @throws BaerlyError code=\"InvalidResponse\" when the body isn't\n * valid JSON or isn't a JSON object.\n */\nexport const readLogEntry = async (\n storage: Storage,\n key: string,\n opts?: { signal?: AbortSignal },\n): Promise<LogEntry> => {\n const got = await readLogEntryWithBytes(storage, key, opts);\n return got.entry;\n};\n\n/**\n * Like {@link readLogEntry}, but also surfaces `bytes` — the\n * byteLength of the STORED log-object body exactly as it was GET back\n * from storage (`got.body.byteLength`).\n *\n * The compactor uses this to sum the byte cost of the folded slice.\n * The stored body byteLength IS what the writer PUT for each entry,\n * so summing `got.body.byteLength` over the folded range yields the\n * exact cost — no re-encode, no round-trip assumption (a re-encode\n * of the parsed `LogEntry` could drift if key-order/whitespace ever\n * diverged from what was PUT).\n *\n * Same error semantics as {@link readLogEntry}.\n */\nconst readLogEntryWithBytes = async (\n storage: Storage,\n key: string,\n opts?: LogWalkOptions,\n): Promise<{ entry: LogEntry; bytes: number }> => {\n const got = await storage.get(\n key,\n opts?.signal !== undefined ? { signal: opts.signal } : undefined,\n );\n if (got === null) {\n throw new BaerlyError(\n \"Internal\",\n `log-walk: missing log entry at ${key}; protocol invariant violation`,\n );\n }\n opts?.onBody?.(got.body.byteLength);\n let entry: unknown;\n try {\n entry = decodeJsonBytes<unknown>(got.body);\n } catch (error) {\n throw new BaerlyError(\"InvalidResponse\", `log-walk: malformed log entry at ${key}`, {\n cause: error,\n });\n }\n if (typeof entry !== \"object\" || entry === null || Array.isArray(entry)) {\n throw new BaerlyError(\"InvalidResponse\", `log-walk: log entry at ${key} is not a JSON object`);\n }\n return { entry: entry as LogEntry, bytes: got.body.byteLength };\n};\n\n/**\n * Walk `[fromSeq, toSeqExclusive)` of `${logPrefix}/log/<seq>.json`\n * in chunks of {@link MAX_PARALLEL_LOG_READS} concurrent GETs.\n * Returns the materialised entries in seq order.\n *\n * Retention precondition: derive `fromSeq` from a fresh `current.json`\n * read and require `fromSeq >= current.log_seq_start`. The floor and\n * starting sequence must come from that same manifest read. Entries below\n * the floor may already be reclaimed; a sub-floor walk that reaches one of\n * those holes throws `Internal` even when later live entries still exist.\n * This storage-level helper intentionally accepts no manifest and cannot\n * enforce the precondition for its caller.\n *\n * Empty range (`fromSeq >= toSeqExclusive`) returns `[]` and issues\n * zero GETs.\n *\n * Same error semantics as {@link readLogEntry}: a missing entry\n * throws `Internal`, a malformed body throws `InvalidResponse`.\n */\nexport const walkLogRange = async (\n storage: Storage,\n logPrefix: string,\n fromSeq: number,\n toSeqExclusive: number,\n opts?: LogWalkOptions,\n): Promise<LogEntry[]> => {\n const withBytes = await walkLogRangeWithBytes(storage, logPrefix, fromSeq, toSeqExclusive, opts);\n return withBytes.map((r) => r.entry);\n};\n\n/**\n * Like {@link walkLogRange}, but each element also carries `bytes` —\n * the STORED log-object body byteLength (see {@link readLogEntryWithBytes}).\n * Same seq-order, empty-range, error, and bounded-parallelism\n * ({@link MAX_PARALLEL_LOG_READS}) guarantees as {@link walkLogRange};\n * this is the bytes-aware core `walkLogRange` is implemented on top of.\n *\n * The compactor consumes this so it gets BOTH the parsed entries (for\n * the fold) AND the exact byte sum (for `mean_entry_bytes` stamping) from a\n * single walk.\n */\nexport const walkLogRangeWithBytes = async (\n storage: Storage,\n logPrefix: string,\n fromSeq: number,\n toSeqExclusive: number,\n opts?: LogWalkOptions,\n): Promise<Array<{ entry: LogEntry; bytes: number }>> => {\n if (fromSeq >= toSeqExclusive) {\n return [];\n }\n const total = toSeqExclusive - fromSeq;\n const out: Array<{ entry: LogEntry; bytes: number }> = Array.from({ length: total });\n for (let chunkStart = 0; chunkStart < total; chunkStart += MAX_PARALLEL_LOG_READS) {\n opts?.signal?.throwIfAborted();\n const chunkEnd = Math.min(chunkStart + MAX_PARALLEL_LOG_READS, total);\n const promises: Array<Promise<{ entry: LogEntry; bytes: number }>> = [];\n for (let i = chunkStart; i < chunkEnd; i++) {\n const seq = fromSeq + i;\n promises.push(readLogEntryWithBytes(storage, logObjectKey(logPrefix, seq), opts));\n }\n const results = await Promise.all(promises);\n for (let i = 0; i < results.length; i++) {\n out[chunkStart + i] = results[i]!;\n }\n }\n return out;\n};\n\n/**\n * Fold a sequence of `LogEntry` records onto a doc-id-keyed map,\n * applying the protocol's per-doc-replace semantics:\n *\n * - `I` / `U`: when `entry.after !== undefined`, set\n * `map.set(entry.doc_id, entry.after)`. Entries with `after ===\n * undefined` (the partial-merge writer's future patch-only shape)\n * are ignored — today's writer always emits `after`, so this is a\n * forward-compat guard, not a live branch.\n * - `D`: tombstone — `map.delete(entry.doc_id)`.\n *\n * Entries whose `collection` does not match `opts.collection`, or\n * whose `doc_id` is `undefined`, are skipped.\n *\n * This is the canonical fold used by the read path (`query.ts`).\n * Centralising it keeps the\n * \"straight `set` on I/U; tombstone on D\" protocol invariant in one place.\n *\n * @see ../../../docs/spec/sync-protocol.md\n */\nexport const foldLogEntriesOnto = <T extends DocumentData>(\n map: Map<string, T>,\n entries: Iterable<LogEntry>,\n opts: {\n readonly collection: string;\n },\n): void => {\n const { collection } = opts;\n for (const entry of entries) {\n if (!isOwnCollection(entry.collection, collection)) {\n continue;\n }\n if (entry.doc_id === undefined) {\n continue;\n }\n switch (entry.op) {\n case \"I\":\n case \"U\": {\n if (entry.after === undefined) {\n continue;\n }\n map.set(entry.doc_id, entry.after as T);\n break;\n }\n case \"D\": {\n map.delete(entry.doc_id);\n break;\n }\n }\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFA,MAAM,wBAAwB,OAC5B,SACA,KACA,SACgD;CAChD,MAAM,MAAM,MAAM,QAAQ,IACxB,KACA,MAAM,WAAW,KAAA,IAAY,EAAE,QAAQ,KAAK,OAAO,IAAI,KAAA,CACzD;CACA,IAAI,QAAQ,MACV,MAAM,IAAI,YACR,YACA,kCAAkC,IAAI,+BACxC;CAEF,MAAM,SAAS,IAAI,KAAK,UAAU;CAClC,IAAI;CACJ,IAAI;EACF,QAAQ,gBAAyB,IAAI,IAAI;CAC3C,SAAS,OAAO;EACd,MAAM,IAAI,YAAY,mBAAmB,oCAAoC,OAAO,EAClF,OAAO,MACT,CAAC;CACH;CACA,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,KAAK,GACpE,MAAM,IAAI,YAAY,mBAAmB,0BAA0B,IAAI,sBAAsB;CAE/F,OAAO;EAAS;EAAmB,OAAO,IAAI,KAAK;CAAW;AAChE;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,eAAe,OAC1B,SACA,WACA,SACA,gBACA,SACwB;CAExB,QAAO,MADiB,sBAAsB,SAAS,WAAW,SAAS,gBAAgB,IAAI,EAAA,CAC9E,KAAK,MAAM,EAAE,KAAK;AACrC;;;;;;;;;;;;AAaA,MAAa,wBAAwB,OACnC,SACA,WACA,SACA,gBACA,SACuD;CACvD,IAAI,WAAW,gBACb,OAAO,CAAC;CAEV,MAAM,QAAQ,iBAAiB;CAC/B,MAAM,MAAiD,MAAM,KAAK,EAAE,QAAQ,MAAM,CAAC;CACnF,KAAK,IAAI,aAAa,GAAG,aAAa,OAAO,cAAA,IAAsC;EACjF,MAAM,QAAQ,eAAe;EAC7B,MAAM,WAAW,KAAK,IAAI,aAAA,IAAqC,KAAK;EACpE,MAAM,WAA+D,CAAC;EACtE,KAAK,IAAI,IAAI,YAAY,IAAI,UAAU,KAAK;GAC1C,MAAM,MAAM,UAAU;GACtB,SAAS,KAAK,sBAAsB,SAAS,aAAa,WAAW,GAAG,GAAG,IAAI,CAAC;EAClF;EACA,MAAM,UAAU,MAAM,QAAQ,IAAI,QAAQ;EAC1C,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAClC,IAAI,aAAa,KAAK,QAAQ;CAElC;CACA,OAAO;AACT"}
|