@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,148 @@
|
|
|
1
|
+
import { s as BaerlyError } from "./baerly.js";
|
|
2
|
+
import { d as encodeJsonBytes, l as unsupportedSchemaVersionMessage, u as decodeJsonBytes } from "./current-json-CC6lE4Aj.js";
|
|
3
|
+
import { u as sha256Hex } from "./config-DEzInG6M.js";
|
|
4
|
+
// ../protocol/src/snapshot-hash.ts
|
|
5
|
+
/**
|
|
6
|
+
* SHA-256 of `bytes` as a 64-char lowercase hex string. Used to seal
|
|
7
|
+
* snapshot filenames so a crashed mid-PUT can't produce a body that
|
|
8
|
+
* readers consume as truth: the filename embeds the hash; readers
|
|
9
|
+
* recompute on load and reject any mismatch as "file missing."
|
|
10
|
+
*
|
|
11
|
+
* Distinct from {@link versionFromContent} (the legacy 32-hex content-key
|
|
12
|
+
* scheme) — snapshot bodies are larger and longer-lived;
|
|
13
|
+
* 256-bit collision resistance matters here.
|
|
14
|
+
*/
|
|
15
|
+
const snapshotHash = (bytes) => sha256Hex(bytes);
|
|
16
|
+
// ../server/src/caller-facing-error.ts
|
|
17
|
+
/**
|
|
18
|
+
* Errors whose message is written for the caller of the public API rather
|
|
19
|
+
* than for the operator. The HTTP layer exposes a contextual code's message
|
|
20
|
+
* (`InvalidConfig`) only for errors in this set; any other `InvalidConfig`,
|
|
21
|
+
* such as a storage or adapter configuration fault that can carry bucket
|
|
22
|
+
* layout, stays scrubbed.
|
|
23
|
+
*/
|
|
24
|
+
const CALLER_FACING_ERRORS = /* @__PURE__ */ new WeakSet();
|
|
25
|
+
/** Mark `err` as caller input at the point it is thrown, and return it. */
|
|
26
|
+
const markCallerFacing = (err) => {
|
|
27
|
+
CALLER_FACING_ERRORS.add(err);
|
|
28
|
+
return err;
|
|
29
|
+
};
|
|
30
|
+
// ../server/src/collection-alias.ts
|
|
31
|
+
const ALIASED_COLLECTION_RESOLUTION = "An earlier release stored this collection under a non-NFC name, and this name addresses the same stored objects, so it cannot be read or written safely. Keep the app on a different NFC name: export the stored data with `baerly admin dump --collection=<stored name>` and restore it with `baerly admin restore` under an NFC name that is not canonically equivalent to either spelling. For a mixed legacy log, dump with --recover-legacy-names selects one exact spelling and requires a matching snapshot or no snapshot.";
|
|
32
|
+
/**
|
|
33
|
+
* `true` when a stored log entry, snapshot, or snapshot manifest that
|
|
34
|
+
* names collection `stored` belongs to collection `expected`. Live
|
|
35
|
+
* folds, compaction, capacity checks, and ordinary exports filter through
|
|
36
|
+
* this guard. Explicit CLI recovery selects exact spellings instead.
|
|
37
|
+
*
|
|
38
|
+
* R2 stores canonically-equivalent keys as one object, so an NFC name
|
|
39
|
+
* addresses the objects a legacy non-NFC name wrote (and vice versa).
|
|
40
|
+
* Those records carry the other spelling. Skipping them would read as an
|
|
41
|
+
* empty collection and let a fold advance the floor past them, after
|
|
42
|
+
* which log retirement deletes them, so a canonically-equivalent but
|
|
43
|
+
* byte-different name throws instead of returning `false`.
|
|
44
|
+
*
|
|
45
|
+
* @throws BaerlyError code="InvalidConfig" — `stored` and `expected`
|
|
46
|
+
* differ but are canonically equivalent. Marked caller-facing.
|
|
47
|
+
*/
|
|
48
|
+
const isOwnCollection = (stored, expected) => {
|
|
49
|
+
if (stored === expected) return true;
|
|
50
|
+
if (typeof stored === "string" && stored.normalize("NFC") === expected.normalize("NFC")) throw markCallerFacing(new BaerlyError("InvalidConfig", `collection ${JSON.stringify(expected)} addresses data stored under the canonically-equivalent name ${JSON.stringify(stored)} (a different Unicode normalization); refusing to read or rewrite it`, { resolution: ALIASED_COLLECTION_RESOLUTION }));
|
|
51
|
+
return false;
|
|
52
|
+
};
|
|
53
|
+
// ../server/src/snapshot.ts
|
|
54
|
+
/**
|
|
55
|
+
* Snapshot file shape + parse. Lifted out of `./compactor.ts` so that
|
|
56
|
+
* `_internal/kernel.ts` — which re-exports these four symbols to the CLI,
|
|
57
|
+
* bench, and tests — does not transitively pull `withObservability` via
|
|
58
|
+
* `compact()`. `./compactor.ts` imports its own snapshot primitives back
|
|
59
|
+
* from this module.
|
|
60
|
+
*
|
|
61
|
+
* @see ../../../docs/spec/sync-protocol.md
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Single-level snapshot at L9; the level prefix is forward-compatible
|
|
65
|
+
* with future L0..L9 rolling merges without a wire change.
|
|
66
|
+
*/
|
|
67
|
+
const SNAPSHOT_LEVEL = 9;
|
|
68
|
+
/**
|
|
69
|
+
* `minSeq` and `maxSeq` are padded to {@link SEQ_DIGITS} digits so
|
|
70
|
+
* `list(prefix)` returns snapshots in seq order without needing to
|
|
71
|
+
* parse the filename. 12 digits caps at ~10^12 entries per collection;
|
|
72
|
+
* a real bucket hits S3's per-prefix QPS ceiling long before that.
|
|
73
|
+
*/
|
|
74
|
+
const SEQ_DIGITS = 12;
|
|
75
|
+
const MAX_SEQ = 10 ** SEQ_DIGITS - 1;
|
|
76
|
+
const pad = (n) => n.toString().padStart(SEQ_DIGITS, "0");
|
|
77
|
+
/**
|
|
78
|
+
* Build the on-bucket key for a snapshot file. `sha256` must be the
|
|
79
|
+
* lowercase hex digest of the canonical body
|
|
80
|
+
* ({@link encodeSnapshotBody}); the filename embeds it so a crashed
|
|
81
|
+
* mid-PUT body that doesn't match its own filename hash is rejected
|
|
82
|
+
* by readers as "missing."
|
|
83
|
+
*
|
|
84
|
+
* @throws BaerlyError code="InvalidConfig" — `minSeq`/`maxSeq` are
|
|
85
|
+
* out of range or `sha256` is not 64 hex chars.
|
|
86
|
+
*/
|
|
87
|
+
const snapshotKey = (collectionPrefix, minSeq, maxSeq, sha256) => {
|
|
88
|
+
if (!Number.isInteger(minSeq) || !Number.isInteger(maxSeq) || minSeq < 0 || maxSeq < 0 || minSeq > maxSeq || maxSeq > MAX_SEQ) throw new BaerlyError("InvalidConfig", `snapshotKey: invalid range [${minSeq}, ${maxSeq})`);
|
|
89
|
+
if (!/^[0-9a-f]{64}$/.test(sha256)) throw new BaerlyError("InvalidConfig", "snapshotKey: sha256 must be 64 hex chars");
|
|
90
|
+
return `${collectionPrefix}/snapshot/L${SNAPSHOT_LEVEL}/${pad(minSeq)}-${pad(maxSeq)}-${sha256}.json`;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Canonical byte encoding of a {@link SnapshotBody}. Only the
|
|
94
|
+
* compactor produces snapshots, and only this function does the
|
|
95
|
+
* serialisation — so the byte output is deterministic for a given
|
|
96
|
+
* `SnapshotBody` (identical input ⇒ identical bytes ⇒ identical
|
|
97
|
+
* filename hash).
|
|
98
|
+
*/
|
|
99
|
+
const encodeSnapshotBody = (s) => encodeJsonBytes(s);
|
|
100
|
+
/**
|
|
101
|
+
* Load a snapshot file as a `Map<_id, DocumentData>`. Verifies the
|
|
102
|
+
* body hash against the filename, checks the schema version, and
|
|
103
|
+
* cross-checks `body.collection` against the caller's expectation.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* import { loadSnapshotAsMap } from "@baerly/server/_internal/kernel";
|
|
108
|
+
*
|
|
109
|
+
* const map =
|
|
110
|
+
* srcCurrent.snapshot === null
|
|
111
|
+
* ? new Map<string, DocumentData>()
|
|
112
|
+
* : await loadSnapshotAsMap(src.storage, srcCurrent.snapshot, "tickets");
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @throws BaerlyError code="Internal" — pointer resolves to no body,
|
|
116
|
+
* or the body's SHA-256 doesn't match the hash embedded in the
|
|
117
|
+
* filename. (Protocol invariant violation; a corrupted snapshot
|
|
118
|
+
* the reader cannot continue past.)
|
|
119
|
+
* @throws BaerlyError code="InvalidResponse" — body isn't valid JSON,
|
|
120
|
+
* isn't an object, has an unsupported `schema_version`, or carries a
|
|
121
|
+
* `collection` that doesn't match `expectedCollection`.
|
|
122
|
+
*/
|
|
123
|
+
const loadSnapshotAsMap = async (storage, key, expectedCollection, signal) => {
|
|
124
|
+
const got = await storage.get(key, signal !== void 0 ? { signal } : void 0);
|
|
125
|
+
if (got === null) throw new BaerlyError("Internal", `compact: snapshot pointer ${key} resolves to no body; protocol violation`);
|
|
126
|
+
const filename = key.slice(key.lastIndexOf("/") + 1).replace(/\.json$/, "");
|
|
127
|
+
const lastDash = filename.lastIndexOf("-");
|
|
128
|
+
const expectedHash = filename.slice(lastDash + 1);
|
|
129
|
+
const actualHash = await snapshotHash(got.body);
|
|
130
|
+
if (actualHash !== expectedHash) throw new BaerlyError("Internal", `compact: snapshot ${key} body hash mismatch (got ${actualHash}); protocol violation`);
|
|
131
|
+
let parsed;
|
|
132
|
+
try {
|
|
133
|
+
parsed = decodeJsonBytes(got.body);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
throw new BaerlyError("InvalidResponse", `compact: snapshot ${key} body is not valid JSON`, { cause: error });
|
|
136
|
+
}
|
|
137
|
+
if (parsed === null || typeof parsed !== "object") throw new BaerlyError("InvalidResponse", `compact: snapshot ${key} body is not an object`);
|
|
138
|
+
const body = parsed;
|
|
139
|
+
if (body.schema_version !== 1) throw new BaerlyError("InvalidResponse", unsupportedSchemaVersionMessage(`compact: snapshot ${key}`, body.schema_version, 1));
|
|
140
|
+
if (!isOwnCollection(body.collection, expectedCollection)) throw new BaerlyError("InvalidResponse", `compact: snapshot ${key} carries collection ${body.collection}, expected ${expectedCollection}`);
|
|
141
|
+
if (!Array.isArray(body.docs)) throw new BaerlyError("InvalidResponse", `compact: snapshot ${key} body.docs is not an array`);
|
|
142
|
+
const out = /* @__PURE__ */ new Map();
|
|
143
|
+
for (const row of body.docs) out.set(row._id, row.body);
|
|
144
|
+
return out;
|
|
145
|
+
};
|
|
146
|
+
export { snapshotHash as a, isOwnCollection as i, loadSnapshotAsMap as n, snapshotKey as r, encodeSnapshotBody as t };
|
|
147
|
+
|
|
148
|
+
//# sourceMappingURL=snapshot-CknzkDM0.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot-CknzkDM0.js","names":[],"sources":["../packages/protocol/src/snapshot-hash.ts","../packages/server/src/caller-facing-error.ts","../packages/server/src/collection-alias.ts","../packages/server/src/snapshot.ts"],"sourcesContent":["import { sha256Hex } from \"./sha256.ts\";\n\n/**\n * SHA-256 of `bytes` as a 64-char lowercase hex string. Used to seal\n * snapshot filenames so a crashed mid-PUT can't produce a body that\n * readers consume as truth: the filename embeds the hash; readers\n * recompute on load and reject any mismatch as \"file missing.\"\n *\n * Distinct from {@link versionFromContent} (the legacy 32-hex content-key\n * scheme) — snapshot bodies are larger and longer-lived;\n * 256-bit collision resistance matters here.\n */\nexport const snapshotHash = (bytes: Uint8Array): Promise<string> => sha256Hex(bytes);\n","import { BaerlyError } from \"@baerly/protocol\";\n\n/**\n * Errors whose message is written for the caller of the public API rather\n * than for the operator. The HTTP layer exposes a contextual code's message\n * (`InvalidConfig`) only for errors in this set; any other `InvalidConfig`,\n * such as a storage or adapter configuration fault that can carry bucket\n * layout, stays scrubbed.\n */\nconst CALLER_FACING_ERRORS = new WeakSet<BaerlyError>();\n\n/** Mark `err` as caller input at the point it is thrown, and return it. */\nexport const markCallerFacing = <E extends BaerlyError>(err: E): E => {\n CALLER_FACING_ERRORS.add(err);\n return err;\n};\n\n/** True when `err` was marked with {@link markCallerFacing}. */\nexport const isCallerFacing = (err: BaerlyError): boolean => CALLER_FACING_ERRORS.has(err);\n\n/**\n * Run a caller-input check, marking any `BaerlyError` it throws with\n * {@link markCallerFacing}. For shared checks (from `@baerly/protocol`)\n * that cannot mark their own errors.\n */\nexport const callerFacingCheck = (check: () => void): void => {\n try {\n check();\n } catch (error) {\n if (error instanceof BaerlyError) {\n markCallerFacing(error);\n }\n throw error;\n }\n};\n","import { BaerlyError } from \"@baerly/protocol\";\nimport { markCallerFacing } from \"./caller-facing-error.ts\";\n\nconst ALIASED_COLLECTION_RESOLUTION =\n \"An earlier release stored this collection under a non-NFC name, and this name addresses the same stored objects, so it cannot be read or written safely. Keep the app on a different NFC name: export the stored data with `baerly admin dump --collection=<stored name>` and restore it with `baerly admin restore` under an NFC name that is not canonically equivalent to either spelling. For a mixed legacy log, dump with --recover-legacy-names selects one exact spelling and requires a matching snapshot or no snapshot.\";\n\n/**\n * `true` when a stored log entry, snapshot, or snapshot manifest that\n * names collection `stored` belongs to collection `expected`. Live\n * folds, compaction, capacity checks, and ordinary exports filter through\n * this guard. Explicit CLI recovery selects exact spellings instead.\n *\n * R2 stores canonically-equivalent keys as one object, so an NFC name\n * addresses the objects a legacy non-NFC name wrote (and vice versa).\n * Those records carry the other spelling. Skipping them would read as an\n * empty collection and let a fold advance the floor past them, after\n * which log retirement deletes them, so a canonically-equivalent but\n * byte-different name throws instead of returning `false`.\n *\n * @throws BaerlyError code=\"InvalidConfig\" — `stored` and `expected`\n * differ but are canonically equivalent. Marked caller-facing.\n */\nexport const isOwnCollection = (stored: string, expected: string): boolean => {\n if (stored === expected) {\n return true;\n }\n if (typeof stored === \"string\" && stored.normalize(\"NFC\") === expected.normalize(\"NFC\")) {\n throw markCallerFacing(\n new BaerlyError(\n \"InvalidConfig\",\n `collection ${JSON.stringify(expected)} addresses data stored under the canonically-equivalent name ${JSON.stringify(stored)} (a different Unicode normalization); refusing to read or rewrite it`,\n { resolution: ALIASED_COLLECTION_RESOLUTION },\n ),\n );\n }\n return false;\n};\n","/**\n * Snapshot file shape + parse. Lifted out of `./compactor.ts` so that\n * `_internal/kernel.ts` — which re-exports these four symbols to the CLI,\n * bench, and tests — does not transitively pull `withObservability` via\n * `compact()`. `./compactor.ts` imports its own snapshot primitives back\n * from this module.\n *\n * @see ../../../docs/spec/sync-protocol.md\n */\n\nimport {\n BaerlyError,\n decodeJsonBytes,\n type DocumentData,\n encodeJsonBytes,\n snapshotHash,\n SNAPSHOT_SCHEMA_VERSION,\n type Storage,\n unsupportedSchemaVersionMessage,\n} from \"@baerly/protocol\";\nimport { isOwnCollection } from \"./collection-alias.ts\";\n\n/**\n * Single-level snapshot at L9; the level prefix is forward-compatible\n * with future L0..L9 rolling merges without a wire change.\n */\nconst SNAPSHOT_LEVEL = 9 as const;\n\n/**\n * `minSeq` and `maxSeq` are padded to {@link SEQ_DIGITS} digits so\n * `list(prefix)` returns snapshots in seq order without needing to\n * parse the filename. 12 digits caps at ~10^12 entries per collection;\n * a real bucket hits S3's per-prefix QPS ceiling long before that.\n */\nconst SEQ_DIGITS = 12 as const;\n\nconst MAX_SEQ = 10 ** SEQ_DIGITS - 1;\n\nconst pad = (n: number): string => n.toString().padStart(SEQ_DIGITS, \"0\");\n\n/**\n * Build the on-bucket key for a snapshot file. `sha256` must be the\n * lowercase hex digest of the canonical body\n * ({@link encodeSnapshotBody}); the filename embeds it so a crashed\n * mid-PUT body that doesn't match its own filename hash is rejected\n * by readers as \"missing.\"\n *\n * @throws BaerlyError code=\"InvalidConfig\" — `minSeq`/`maxSeq` are\n * out of range or `sha256` is not 64 hex chars.\n */\nexport const snapshotKey = (\n collectionPrefix: string,\n minSeq: number,\n maxSeq: number,\n sha256: string,\n): string => {\n // `Number.isInteger` first: every `<`/`>` below is false against NaN,\n // so a non-finite seq would sail through and `pad()` it into a\n // `000000000NaN` filename.\n if (\n !Number.isInteger(minSeq) ||\n !Number.isInteger(maxSeq) ||\n minSeq < 0 ||\n maxSeq < 0 ||\n minSeq > maxSeq ||\n maxSeq > MAX_SEQ\n ) {\n throw new BaerlyError(\"InvalidConfig\", `snapshotKey: invalid range [${minSeq}, ${maxSeq})`);\n }\n if (!/^[0-9a-f]{64}$/.test(sha256)) {\n throw new BaerlyError(\"InvalidConfig\", \"snapshotKey: sha256 must be 64 hex chars\");\n }\n return `${collectionPrefix}/snapshot/L${SNAPSHOT_LEVEL}/${pad(minSeq)}-${pad(maxSeq)}-${sha256}.json`;\n};\n\n/**\n * On-bucket body of a snapshot file. The reader recomputes the SHA-256\n * over the canonical serialisation ({@link encodeSnapshotBody}) and\n * rejects any mismatch with the filename hash.\n *\n * `docs` is sorted by `_id` for deterministic byte output — two\n * compactions over the same fold produce byte-identical snapshots,\n * which means same filename, which means an idempotent re-run never\n * leaves an orphan.\n */\nexport interface SnapshotBody {\n readonly schema_version: typeof SNAPSHOT_SCHEMA_VERSION;\n readonly min_seq: number;\n readonly max_seq: number;\n readonly collection: string;\n readonly docs: ReadonlyArray<{\n readonly _id: string;\n readonly body: DocumentData;\n }>;\n}\n\n/**\n * Canonical byte encoding of a {@link SnapshotBody}. Only the\n * compactor produces snapshots, and only this function does the\n * serialisation — so the byte output is deterministic for a given\n * `SnapshotBody` (identical input ⇒ identical bytes ⇒ identical\n * filename hash).\n */\nexport const encodeSnapshotBody = (s: SnapshotBody): Uint8Array => encodeJsonBytes(s);\n\n/**\n * Load a snapshot file as a `Map<_id, DocumentData>`. Verifies the\n * body hash against the filename, checks the schema version, and\n * cross-checks `body.collection` against the caller's expectation.\n *\n * @example\n * ```ts\n * import { loadSnapshotAsMap } from \"@baerly/server/_internal/kernel\";\n *\n * const map =\n * srcCurrent.snapshot === null\n * ? new Map<string, DocumentData>()\n * : await loadSnapshotAsMap(src.storage, srcCurrent.snapshot, \"tickets\");\n * ```\n *\n * @throws BaerlyError code=\"Internal\" — pointer resolves to no body,\n * or the body's SHA-256 doesn't match the hash embedded in the\n * filename. (Protocol invariant violation; a corrupted snapshot\n * the reader cannot continue past.)\n * @throws BaerlyError code=\"InvalidResponse\" — body isn't valid JSON,\n * isn't an object, has an unsupported `schema_version`, or carries a\n * `collection` that doesn't match `expectedCollection`.\n */\nexport const loadSnapshotAsMap = async (\n storage: Storage,\n key: string,\n expectedCollection: string,\n signal?: AbortSignal,\n): Promise<Map<string, DocumentData>> => {\n const got = await storage.get(key, signal !== undefined ? { signal } : undefined);\n if (got === null) {\n throw new BaerlyError(\n \"Internal\",\n `compact: snapshot pointer ${key} resolves to no body; protocol violation`,\n );\n }\n // Verify hash: the filename shape is\n // `<...>/snapshot/L<n>/<min>-<max>-<sha256>.json`. The sha256 is\n // the segment after the LAST `-` (the two seq fields don't contain\n // dashes), with `.json` stripped.\n const filename = key.slice(key.lastIndexOf(\"/\") + 1).replace(/\\.json$/, \"\");\n const lastDash = filename.lastIndexOf(\"-\");\n const expectedHash = filename.slice(lastDash + 1);\n const actualHash = await snapshotHash(got.body);\n if (actualHash !== expectedHash) {\n throw new BaerlyError(\n \"Internal\",\n `compact: snapshot ${key} body hash mismatch (got ${actualHash}); protocol violation`,\n );\n }\n let parsed: unknown;\n try {\n parsed = decodeJsonBytes(got.body);\n } catch (error) {\n throw new BaerlyError(\"InvalidResponse\", `compact: snapshot ${key} body is not valid JSON`, {\n cause: error,\n });\n }\n if (parsed === null || typeof parsed !== \"object\") {\n throw new BaerlyError(\"InvalidResponse\", `compact: snapshot ${key} body is not an object`);\n }\n const body = parsed as SnapshotBody;\n if (body.schema_version !== SNAPSHOT_SCHEMA_VERSION) {\n throw new BaerlyError(\n \"InvalidResponse\",\n unsupportedSchemaVersionMessage(\n `compact: snapshot ${key}`,\n body.schema_version,\n SNAPSHOT_SCHEMA_VERSION,\n ),\n );\n }\n if (!isOwnCollection(body.collection, expectedCollection)) {\n throw new BaerlyError(\n \"InvalidResponse\",\n `compact: snapshot ${key} carries collection ${body.collection}, expected ${expectedCollection}`,\n );\n }\n if (!Array.isArray(body.docs)) {\n throw new BaerlyError(\"InvalidResponse\", `compact: snapshot ${key} body.docs is not an array`);\n }\n const out = new Map<string, DocumentData>();\n for (const row of body.docs) {\n out.set(row._id, row.body);\n }\n return out;\n};\n"],"mappings":"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"}
|