@gusto/baerly-storage 0.6.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 -512
- package/dist/CHANGELOG.md +1137 -378
- 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-ypCm3zjt.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-wrVYf4bb.js → cloudflare-CKHYXccw.js} +61 -82
- 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-Di_Q01qx.js → deploy-Obj4nE-Z.js} +10 -14
- 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 +9 -14
- package/dist/dev-vite.js +11 -10
- 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-Bx6xSNJj.d.ts → gcs-admin-Dso1HWQj.d.ts} +18 -14
- package/dist/gcs.d.ts +19 -7
- package/dist/gcs.js +1 -2
- package/dist/{goog4-signer-Z7KnJ1su.js → goog4-signer-CeLIABoE.js} +102 -92
- package/dist/goog4-signer-CeLIABoE.js.map +1 -0
- package/dist/{http-7zT29K3P.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-DP3ClvcA.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 +119 -92
- package/dist/node.js +3 -3
- package/dist/{observability-Ch-VQJyE.js → observability-C-ISCCLE.js} +1247 -326
- 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-C4qgiXSg.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--LykAdl1.js → runtime-spec-B5mg3pHF.js} +189 -28
- package/dist/runtime-spec-B5mg3pHF.js.map +1 -0
- package/dist/{s3-http-DD5gDc7t.d.ts → s3-http-DBC_wiCL.d.ts} +14 -8
- package/dist/s3.d.ts +20 -7
- package/dist/s3.js +4 -5
- package/dist/s3.js.map +1 -1
- package/dist/{sha256-CqcKTjw9.js → sha256-4FfGQNPw.js} +9 -9
- 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/{aws4fetch.esm-CXE5ZbAa.js → sigv4-sign-5IUflUD_.js} +143 -96
- 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-BMFfDUI2.js → src-Actga-bO.js} +949 -363
- 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-BV68pqwJ.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-BMZR8o6U.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 +90 -69
- package/dist/aws4fetch.esm-CXE5ZbAa.js.map +0 -1
- package/dist/bytes-BXdamO3L.js +0 -11
- package/dist/bytes-BXdamO3L.js.map +0 -1
- package/dist/cloudflare-wrVYf4bb.js.map +0 -1
- package/dist/config-B6nTy8e2.js +0 -3095
- package/dist/config-B6nTy8e2.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-_SE_SVgZ.js +0 -203
- package/dist/cost-_SE_SVgZ.js.map +0 -1
- package/dist/current-json-C6nhPOm1.js.map +0 -1
- package/dist/db-DjjthkTi.d.ts +0 -254
- package/dist/deploy-Di_Q01qx.js.map +0 -1
- package/dist/doctor-RWVRpvat.js +0 -102
- package/dist/doctor-RWVRpvat.js.map +0 -1
- package/dist/dump-m2vEYDNY.js +0 -94
- package/dist/dump-m2vEYDNY.js.map +0 -1
- package/dist/errors-Dfa3p78c.js +0 -78
- package/dist/errors-Dfa3p78c.js.map +0 -1
- package/dist/export-DenKwEQu.js +0 -401
- package/dist/export-DenKwEQu.js.map +0 -1
- package/dist/fsck-2Vq-7-e2.js +0 -279
- package/dist/fsck-2Vq-7-e2.js.map +0 -1
- package/dist/goog4-signer-Z7KnJ1su.js.map +0 -1
- package/dist/http-7zT29K3P.js.map +0 -1
- package/dist/http-transport-BTgVCY5r.js +0 -1250
- package/dist/http-transport-BTgVCY5r.js.map +0 -1
- package/dist/index-BHz92Acj.d.ts +0 -162
- package/dist/index-CEZKQsKt.d.ts +0 -1113
- package/dist/index-uE-w2dgj.d.ts +0 -115
- package/dist/indexes-Dn9hbGQG.d.ts +0 -22
- package/dist/inspect-BpYbH1DM.js +0 -133
- package/dist/inspect-BpYbH1DM.js.map +0 -1
- package/dist/log-tail-QWU4wlu-.js +0 -514
- package/dist/log-tail-QWU4wlu-.js.map +0 -1
- package/dist/maintenance-BGHGt2Yg.d.ts +0 -537
- package/dist/maintenance-DULFcpd1.js +0 -2144
- package/dist/maintenance-DULFcpd1.js.map +0 -1
- package/dist/maintenance-fEi-UZ2o.js +0 -1412
- package/dist/maintenance-fEi-UZ2o.js.map +0 -1
- package/dist/normalize-rjnZLwS4.js +0 -133
- package/dist/normalize-rjnZLwS4.js.map +0 -1
- package/dist/observability-Ch-VQJyE.js.map +0 -1
- package/dist/plan-C4qgiXSg.js.map +0 -1
- package/dist/query-C43r-6ib.js +0 -1773
- package/dist/query-C43r-6ib.js.map +0 -1
- package/dist/rebuild-index-BH1dc1p6.js +0 -81
- package/dist/rebuild-index-BH1dc1p6.js.map +0 -1
- package/dist/request-u0M51mhJ.js +0 -72
- package/dist/request-u0M51mhJ.js.map +0 -1
- package/dist/restore-D0VYWzfP.js +0 -778
- package/dist/restore-D0VYWzfP.js.map +0 -1
- package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
- package/dist/runtime-spec--LykAdl1.js.map +0 -1
- package/dist/sha256-CqcKTjw9.js.map +0 -1
- package/dist/shared-secret-DgEicd3x.js.map +0 -1
- package/dist/src-BMFfDUI2.js.map +0 -1
- package/dist/src-DYZ4H1aF.js +0 -392
- package/dist/src-DYZ4H1aF.js.map +0 -1
- package/dist/src-DvbTEya2.js +0 -536
- package/dist/src-DvbTEya2.js.map +0 -1
- package/dist/subcommand-BV68pqwJ.js.map +0 -1
- package/dist/time-DUqvFMHq.js.map +0 -1
- package/dist/usage-BMZR8o6U.js.map +0 -1
- package/dist/validate-DtljsCh1.js.map +0 -1
- package/dist/wire-izfGZDvr.js.map +0 -1
|
@@ -1,9 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as
|
|
3
|
-
import {
|
|
4
|
-
import { t as
|
|
5
|
-
import { i as
|
|
6
|
-
|
|
1
|
+
import { t as BaerlyError } from "./errors-7Rk1HGCE.js";
|
|
2
|
+
import { t as assertValidStorageKey } from "./key-C4FPZ9Io.js";
|
|
3
|
+
import { n as currentRequestHold, r as currentRequestLedger } from "./ledger-Dj_bGgo0.js";
|
|
4
|
+
import { t as retryStorageCall } from "./retry-OBtLB2GC.js";
|
|
5
|
+
import { i as s3ExpiredTokenCode, o as parseListObjectsV2CommandOutput, r as retryAfterCause, t as mapS3StorageError } from "./http-transport-DNUM8kU-.js";
|
|
6
|
+
// packages/adapter-node/src/s3-http.ts
|
|
7
|
+
/**
|
|
8
|
+
* Drop a weak-validator prefix from a response ETag. R2's S3 API compresses
|
|
9
|
+
* `application/json` GET responses over ~159 bytes and returns the stored
|
|
10
|
+
* ETag as `W/"<md5>"`; `If-Match` with that weak value always fails, while
|
|
11
|
+
* the bare `"<md5>"` matches the stored object (and a wrong one is still
|
|
12
|
+
* refused). `current.json` and `gc/pending.json` advance by exactly that
|
|
13
|
+
* get-then-`ifMatch`, so without this no fold lands on R2 over S3.
|
|
14
|
+
*/
|
|
15
|
+
const strongEtag = (etag) => etag.startsWith("W/") ? etag.slice(2) : etag;
|
|
16
|
+
let attachInvalidate;
|
|
17
|
+
/**
|
|
18
|
+
* Internal wiring (not re-exported from the package entries): make `storage`
|
|
19
|
+
* drop the credentials that signed a request (`refreshingSigner(...).invalidate`)
|
|
20
|
+
* and resend once per storage call when S3 answers `400 ExpiredToken` /
|
|
21
|
+
* `TokenRefreshRequired`. The resend is charged to the request ledger like any
|
|
22
|
+
* attempt; a second rejection, or storage without this wiring, is
|
|
23
|
+
* `AccessDenied` naming the S3 code.
|
|
24
|
+
*/
|
|
25
|
+
const withCredentialInvalidation = (storage, invalidate) => {
|
|
26
|
+
attachInvalidate(storage, invalidate);
|
|
27
|
+
return storage;
|
|
28
|
+
};
|
|
7
29
|
/**
|
|
8
30
|
* `Storage` impl that speaks the S3 REST API directly. Works against
|
|
9
31
|
* any S3-compatible endpoint — AWS S3, R2 (S3-compat), Minio. Does not
|
|
@@ -20,6 +42,7 @@ var S3HttpStorage = class {
|
|
|
20
42
|
#bucket;
|
|
21
43
|
#fetch;
|
|
22
44
|
#sign;
|
|
45
|
+
#invalidate;
|
|
23
46
|
#retries;
|
|
24
47
|
#backoffMs;
|
|
25
48
|
constructor(options) {
|
|
@@ -27,60 +50,81 @@ var S3HttpStorage = class {
|
|
|
27
50
|
this.#bucket = options.bucket;
|
|
28
51
|
this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
29
52
|
this.#sign = options.sign;
|
|
30
|
-
this.#retries = options.retries
|
|
31
|
-
this.#backoffMs = options.backoffMs
|
|
53
|
+
this.#retries = options.retries;
|
|
54
|
+
this.#backoffMs = options.backoffMs;
|
|
55
|
+
}
|
|
56
|
+
static {
|
|
57
|
+
attachInvalidate = (storage, invalidate) => {
|
|
58
|
+
storage.#invalidate = invalidate;
|
|
59
|
+
};
|
|
32
60
|
}
|
|
33
|
-
#objectUrl(key
|
|
61
|
+
#objectUrl(key) {
|
|
34
62
|
assertValidStorageKey(key);
|
|
35
|
-
|
|
36
|
-
return versionId !== void 0 ? `${base}?versionId=${encodeURIComponent(versionId)}` : base;
|
|
63
|
+
return `${this.#endpoint}/${this.#bucket}/${encodeURIComponent(key)}`;
|
|
37
64
|
}
|
|
38
|
-
async #
|
|
65
|
+
async #send(req) {
|
|
39
66
|
const signed = this.#sign ? await this.#sign(req) : req;
|
|
40
67
|
try {
|
|
41
|
-
return
|
|
68
|
+
return {
|
|
69
|
+
res: await this.#fetch(signed),
|
|
70
|
+
signed
|
|
71
|
+
};
|
|
42
72
|
} catch (error) {
|
|
43
|
-
if (error instanceof BaerlyError) throw error;
|
|
44
|
-
|
|
73
|
+
if (error instanceof BaerlyError || signed.signal.aborted) throw error;
|
|
74
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
75
|
+
throw new BaerlyError("NetworkError", msg, { cause: error });
|
|
45
76
|
}
|
|
46
77
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Send one request. An expired-session-token `400` drops the credentials
|
|
80
|
+
* that signed it and resends once per storage call (`call` is shared by the
|
|
81
|
+
* attempts of one `#retry`, so the retry policy cannot add a second
|
|
82
|
+
* refresh). The resend is charged to the ledger here because it is a
|
|
83
|
+
* request `retryStorageCall` did not count. A second expired-token
|
|
84
|
+
* response is returned as is and maps to `AccessDenied`.
|
|
85
|
+
*/
|
|
86
|
+
async #dispatch(make, call) {
|
|
87
|
+
const first = await this.#send(make());
|
|
88
|
+
if (this.#invalidate === void 0 || call.refreshed || await s3ExpiredTokenCode(first.res) === void 0) return first.res;
|
|
89
|
+
await first.res.body?.cancel();
|
|
90
|
+
first.signed.signal.throwIfAborted();
|
|
91
|
+
call.refreshed = true;
|
|
92
|
+
this.#invalidate(first.signed);
|
|
93
|
+
currentRequestLedger()?.acquire("external", currentRequestHold());
|
|
94
|
+
return (await this.#send(make())).res;
|
|
95
|
+
}
|
|
96
|
+
#retry(fn, opts) {
|
|
97
|
+
const call = { refreshed: false };
|
|
98
|
+
return retryStorageCall(() => fn(call), {
|
|
99
|
+
...this.#retries !== void 0 && { retries: this.#retries },
|
|
100
|
+
...this.#backoffMs !== void 0 && { backoffMs: this.#backoffMs },
|
|
101
|
+
conditionalCreate: opts?.ifNoneMatch === "*",
|
|
102
|
+
compareAndSwap: opts?.ifMatch !== void 0 && opts.ifNoneMatch !== "*",
|
|
103
|
+
pool: "external",
|
|
104
|
+
...opts?.signal !== void 0 && { signal: opts.signal }
|
|
51
105
|
});
|
|
52
106
|
}
|
|
53
107
|
async get(key, opts) {
|
|
54
108
|
opts?.signal?.throwIfAborted();
|
|
55
|
-
const url = this.#objectUrl(key
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return this.#retry(async () => {
|
|
59
|
-
const res = await this.#dispatch(new Request(url, {
|
|
109
|
+
const url = this.#objectUrl(key);
|
|
110
|
+
return this.#retry(async (call) => {
|
|
111
|
+
const res = await this.#dispatch(() => new Request(url, {
|
|
60
112
|
method: "GET",
|
|
61
|
-
headers,
|
|
62
113
|
signal: opts?.signal ?? null
|
|
63
|
-
}));
|
|
114
|
+
}), call);
|
|
64
115
|
switch (res.status) {
|
|
65
116
|
case 200: {
|
|
66
117
|
const etag = res.headers.get("ETag");
|
|
67
118
|
if (etag === null) throw new BaerlyError("InvalidResponse", `GET ${key}: missing ETag`);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
body,
|
|
72
|
-
etag,
|
|
73
|
-
versionId
|
|
74
|
-
} : {
|
|
75
|
-
body,
|
|
76
|
-
etag
|
|
119
|
+
return {
|
|
120
|
+
body: new Uint8Array(await res.arrayBuffer()),
|
|
121
|
+
etag: strongEtag(etag)
|
|
77
122
|
};
|
|
78
123
|
}
|
|
79
|
-
case 304:
|
|
80
124
|
case 404: return null;
|
|
81
|
-
default: throw await
|
|
125
|
+
default: throw await mapS3StorageError(res, "GET", key);
|
|
82
126
|
}
|
|
83
|
-
});
|
|
127
|
+
}, opts);
|
|
84
128
|
}
|
|
85
129
|
async put(key, body, opts) {
|
|
86
130
|
opts?.signal?.throwIfAborted();
|
|
@@ -89,40 +133,38 @@ var S3HttpStorage = class {
|
|
|
89
133
|
headers.set("Content-Type", opts?.contentType ?? "application/octet-stream");
|
|
90
134
|
if (opts?.ifMatch !== void 0) headers.set("If-Match", opts.ifMatch);
|
|
91
135
|
if (opts?.ifNoneMatch === "*") headers.set("If-None-Match", "*");
|
|
92
|
-
return this.#retry(async () => {
|
|
93
|
-
const res = await this.#dispatch(new Request(url, {
|
|
136
|
+
return this.#retry(async (call) => {
|
|
137
|
+
const res = await this.#dispatch(() => new Request(url, {
|
|
94
138
|
method: "PUT",
|
|
95
139
|
headers,
|
|
96
140
|
body,
|
|
97
141
|
signal: opts?.signal ?? null
|
|
98
|
-
}));
|
|
99
|
-
if (res.status === 412) throw new BaerlyError("
|
|
100
|
-
if (res.status === 409 && opts?.ifNoneMatch === "*") throw new BaerlyError("NetworkError", `PUT ${key}: 409 ConditionalRequestConflict (contended conditional
|
|
142
|
+
}), call);
|
|
143
|
+
if (res.status === 412) throw new BaerlyError("WriteConflict", `PUT ${key}: precondition failed`);
|
|
144
|
+
if (res.status === 409 && (opts?.ifNoneMatch === "*" || opts?.ifMatch !== void 0)) throw new BaerlyError("NetworkError", `PUT ${key}: 409 ConditionalRequestConflict (contended conditional write; retryable)`, { cause: {
|
|
101
145
|
status: res.status,
|
|
102
146
|
...retryAfterCause(res)
|
|
103
|
-
});
|
|
104
|
-
if (res.status === 404 && opts?.ifMatch !== void 0) throw new BaerlyError("
|
|
105
|
-
if (res.status !== 200 && res.status !== 204) throw await
|
|
147
|
+
} });
|
|
148
|
+
if (res.status === 404 && opts?.ifMatch !== void 0) throw new BaerlyError("WriteConflict", `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but key does not exist)`);
|
|
149
|
+
if (res.status !== 200 && res.status !== 204) throw await mapS3StorageError(res, "PUT", key);
|
|
106
150
|
const etag = res.headers.get("ETag");
|
|
107
151
|
if (etag === null) throw new BaerlyError("InvalidResponse", `PUT ${key}: missing ETag`);
|
|
108
152
|
const dateStr = res.headers.get("Date");
|
|
109
|
-
const
|
|
110
|
-
const result = { etag };
|
|
153
|
+
const result = { etag: strongEtag(etag) };
|
|
111
154
|
if (dateStr !== null) result.serverDate = new Date(dateStr);
|
|
112
|
-
if (versionId !== void 0) result.versionId = versionId;
|
|
113
155
|
return result;
|
|
114
|
-
});
|
|
156
|
+
}, opts);
|
|
115
157
|
}
|
|
116
158
|
async delete(key, opts) {
|
|
117
159
|
opts?.signal?.throwIfAborted();
|
|
118
160
|
const url = this.#objectUrl(key);
|
|
119
|
-
await this.#retry(async () => {
|
|
120
|
-
const res = await this.#dispatch(new Request(url, {
|
|
161
|
+
await this.#retry(async (call) => {
|
|
162
|
+
const res = await this.#dispatch(() => new Request(url, {
|
|
121
163
|
method: "DELETE",
|
|
122
164
|
signal: opts?.signal ?? null
|
|
123
|
-
}));
|
|
124
|
-
if (res.status !== 200 && res.status !== 204 && res.status !== 404) throw await
|
|
125
|
-
});
|
|
165
|
+
}), call);
|
|
166
|
+
if (res.status !== 200 && res.status !== 204 && res.status !== 404) throw await mapS3StorageError(res, "DELETE", key);
|
|
167
|
+
}, opts);
|
|
126
168
|
}
|
|
127
169
|
async *list(prefix, opts) {
|
|
128
170
|
opts?.signal?.throwIfAborted();
|
|
@@ -142,42 +184,19 @@ var S3HttpStorage = class {
|
|
|
142
184
|
params.set("max-keys", String(Math.min(remaining, 1e3)));
|
|
143
185
|
}
|
|
144
186
|
const url = `${this.#endpoint}/${this.#bucket}/?${params.toString()}`;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (res.status === 200) return {
|
|
154
|
-
kind: "ok",
|
|
155
|
-
body: await res.text()
|
|
156
|
-
};
|
|
157
|
-
if (res.status === 429) return {
|
|
158
|
-
kind: "ratelimited",
|
|
159
|
-
retryAfterSeconds: parseRetryAfter(res.headers.get("Retry-After"))
|
|
160
|
-
};
|
|
161
|
-
throw await mapStorageError(res, "LIST", prefix);
|
|
162
|
-
});
|
|
163
|
-
if (outcome.kind === "ok") {
|
|
164
|
-
parsed = parseListObjectsV2CommandOutput(outcome.body);
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
lastHint = outcome.retryAfterSeconds;
|
|
168
|
-
const hintMs = outcome.retryAfterSeconds !== void 0 ? outcome.retryAfterSeconds * 1e3 : 0;
|
|
169
|
-
await delay(Math.max(hintMs, RATE_LIMIT_BACKOFF_MILLIS));
|
|
170
|
-
}
|
|
171
|
-
if (parsed === void 0) throw new BaerlyError("NetworkError", `LIST ${prefix}: rate-limited`, {
|
|
172
|
-
status: 429,
|
|
173
|
-
...lastHint !== void 0 ? { retryAfterSeconds: lastHint } : {}
|
|
174
|
-
});
|
|
187
|
+
const parsed = parseListObjectsV2CommandOutput(await this.#retry(async (call) => {
|
|
188
|
+
const res = await this.#dispatch(() => new Request(url, {
|
|
189
|
+
method: "GET",
|
|
190
|
+
signal: opts?.signal ?? null
|
|
191
|
+
}), call);
|
|
192
|
+
if (res.status !== 200) throw await mapS3StorageError(res, "LIST", prefix);
|
|
193
|
+
return res.text();
|
|
194
|
+
}, opts));
|
|
175
195
|
for (const entry of parsed.Contents ?? []) {
|
|
176
196
|
if (entry.Key === void 0) continue;
|
|
177
197
|
yield {
|
|
178
198
|
key: entry.Key,
|
|
179
|
-
etag: entry.ETag ?? ""
|
|
180
|
-
...entry.LastModified !== void 0 && { lastModified: entry.LastModified }
|
|
199
|
+
etag: entry.ETag ?? ""
|
|
181
200
|
};
|
|
182
201
|
yielded += 1;
|
|
183
202
|
if (opts?.maxKeys !== void 0 && yielded >= opts.maxKeys) return;
|
|
@@ -187,8 +206,7 @@ var S3HttpStorage = class {
|
|
|
187
206
|
}
|
|
188
207
|
}
|
|
189
208
|
};
|
|
190
|
-
|
|
191
|
-
//#region node_modules/.pnpm/aws4fetch@1.0.20/node_modules/aws4fetch/dist/aws4fetch.esm.mjs
|
|
209
|
+
// node_modules/.pnpm/aws4fetch@1.0.20/node_modules/aws4fetch/dist/aws4fetch.esm.mjs
|
|
192
210
|
/**
|
|
193
211
|
* @license MIT <https://opensource.org/licenses/MIT>
|
|
194
212
|
* @copyright Michael Hart 2024
|
|
@@ -440,10 +458,11 @@ function guessServiceRegion(url, headers) {
|
|
|
440
458
|
else if (region === "s3" || region === "s3-accelerate") {
|
|
441
459
|
region = "us-east-1";
|
|
442
460
|
service = "s3";
|
|
443
|
-
} else if (service === "iot")
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
461
|
+
} else if (service === "iot") {
|
|
462
|
+
if (hostname.startsWith("iot.")) service = "execute-api";
|
|
463
|
+
else if (hostname.startsWith("data.jobs.iot.")) service = "iot-jobs-data";
|
|
464
|
+
else service = pathname === "/mqtt" ? "iotdevicegateway" : "iotdata";
|
|
465
|
+
} else if (service === "autoscaling") {
|
|
447
466
|
const targetPrefix = (headers.get("X-Amz-Target") || "").split(".")[0];
|
|
448
467
|
if (targetPrefix === "AnyScaleFrontendService") service = "application-autoscaling";
|
|
449
468
|
else if (targetPrefix === "AnyScaleScalingPlannerFrontendService") service = "autoscaling-plans";
|
|
@@ -454,7 +473,35 @@ function guessServiceRegion(url, headers) {
|
|
|
454
473
|
else if (region && /-\d$/.test(service) && !/-\d$/.test(region)) [service, region] = [region, service];
|
|
455
474
|
return [HOST_SERVICES[service] || service, region || ""];
|
|
456
475
|
}
|
|
457
|
-
|
|
458
|
-
|
|
476
|
+
// packages/adapter-node/src/sigv4-sign.ts
|
|
477
|
+
/**
|
|
478
|
+
* Sign an S3 request through `aws4fetch` without touching its body.
|
|
479
|
+
*
|
|
480
|
+
* Given a `Request` with a `Content-Type` and no `X-Amz-Content-Sha256`,
|
|
481
|
+
* `AwsClient.sign` reads the whole body through `req.clone().arrayBuffer()`
|
|
482
|
+
* and rebuilds the request around the copy, although for S3 it signs
|
|
483
|
+
* `UNSIGNED-PAYLOAD` and never hashes the body. Signing only the method,
|
|
484
|
+
* URL and headers yields the same `Authorization` and header set without
|
|
485
|
+
* that copy. The header defaults to `UNSIGNED-PAYLOAD` exactly as
|
|
486
|
+
* `aws4fetch` defaults it for S3, so the signature is unchanged.
|
|
487
|
+
*
|
|
488
|
+
* The signed headers go back onto a `Request` built from the original, so
|
|
489
|
+
* the original body source survives and Node still sends a `Content-Length`.
|
|
490
|
+
* Handing `aws4fetch` the original `Request` with `X-Amz-Content-Sha256`
|
|
491
|
+
* already set would skip the copy too, but it rebuilds the request around
|
|
492
|
+
* `req.body` (a `ReadableStream`), and undici then sends
|
|
493
|
+
* `Transfer-Encoding: chunked` with no length, which AWS S3 `PutObject`
|
|
494
|
+
* rejects.
|
|
495
|
+
*/
|
|
496
|
+
async function signHeadersOnly(client, req) {
|
|
497
|
+
const headers = new Headers(req.headers);
|
|
498
|
+
if (!headers.has("X-Amz-Content-Sha256")) headers.set("X-Amz-Content-Sha256", "UNSIGNED-PAYLOAD");
|
|
499
|
+
const signed = await client.sign(req.url, {
|
|
500
|
+
method: req.method,
|
|
501
|
+
headers
|
|
502
|
+
});
|
|
503
|
+
return new Request(req, { headers: signed.headers });
|
|
504
|
+
}
|
|
505
|
+
export { withCredentialInvalidation as i, AwsClient as n, S3HttpStorage as r, signHeadersOnly as t };
|
|
459
506
|
|
|
460
|
-
//# sourceMappingURL=
|
|
507
|
+
//# sourceMappingURL=sigv4-sign-5IUflUD_.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sigv4-sign-5IUflUD_.js","names":[],"sources":["../packages/adapter-node/src/s3-http.ts","../node_modules/.pnpm/aws4fetch@1.0.20/node_modules/aws4fetch/dist/aws4fetch.esm.mjs","../packages/adapter-node/src/sigv4-sign.ts"],"sourcesContent":["import {\n BaerlyError,\n assertValidStorageKey,\n retryStorageCall,\n type Storage,\n type StorageGetOptions,\n type StorageGetResult,\n type StorageListEntry,\n type StoragePutOptions,\n type StoragePutResult,\n currentRequestHold,\n currentRequestLedger,\n} from \"@baerly/protocol\";\nimport { mapS3StorageError, retryAfterCause, s3ExpiredTokenCode } from \"./http-transport.ts\";\nimport { parseListObjectsV2CommandOutput } from \"./xml.ts\";\n\n/**\n * Drop a weak-validator prefix from a response ETag. R2's S3 API compresses\n * `application/json` GET responses over ~159 bytes and returns the stored\n * ETag as `W/\"<md5>\"`; `If-Match` with that weak value always fails, while\n * the bare `\"<md5>\"` matches the stored object (and a wrong one is still\n * refused). `current.json` and `gc/pending.json` advance by exactly that\n * get-then-`ifMatch`, so without this no fold lands on R2 over S3.\n */\nconst strongEtag = (etag: string): string => (etag.startsWith(\"W/\") ? etag.slice(2) : etag);\n\n/** State shared by the attempts of one storage call. */\ninterface CallState {\n /** The call already refreshed credentials after an expired-token response. */\n refreshed: boolean;\n}\n\n/**\n * Construction-time options for {@link S3HttpStorage}. Everything is\n * optional except `endpoint` and `bucket`; the consumer chooses how\n * to satisfy `fetch` (browser/Node global vs. injected) and `sign`\n * (anonymous Minio vs. SigV4-signed via aws4fetch).\n */\nexport interface S3HttpStorageOptions {\n /**\n * S3 endpoint URL, e.g. `https://s3.us-east-1.amazonaws.com` or\n * `http://127.0.0.1:9102` (Minio). Trailing slashes are trimmed.\n */\n endpoint: string;\n /** S3 bucket name. */\n bucket: string;\n /**\n * `fetch` implementation. Defaults to `globalThis.fetch.bind(globalThis)`.\n * Tests pass a `vi.fn()` stub.\n */\n fetch?: typeof fetch;\n /**\n * Signs requests before send. Pass `undefined` for anonymous\n * (Minio without creds). The seam is `(req) => Promise<req>` so\n * SigV4 implementations like `aws4fetch.AwsClient.sign` plug in\n * directly.\n */\n sign?: (req: Request) => Promise<Request>;\n /**\n * Retries after the first attempt, for throttled and transient failures\n * alike (and per `list` page). Default `STORAGE_RETRY_MAX_ATTEMPTS - 1`\n * (2, so 3 attempts in total); `0` sends each request once. Must be a\n * non-negative integer; any other value makes each request reject with\n * `InvalidConfig` before it is sent.\n */\n retries?: number;\n /**\n * Backoff base in ms, overriding both default bases (50 ms after a\n * transient failure, 1 s after a throttle). Full jitter applies either\n * way. Omit for the default policy.\n */\n backoffMs?: number;\n}\n\nlet attachInvalidate: (storage: S3HttpStorage, invalidate: (signed: Request) => void) => void;\n\n/**\n * Internal wiring (not re-exported from the package entries): make `storage`\n * drop the credentials that signed a request (`refreshingSigner(...).invalidate`)\n * and resend once per storage call when S3 answers `400 ExpiredToken` /\n * `TokenRefreshRequired`. The resend is charged to the request ledger like any\n * attempt; a second rejection, or storage without this wiring, is\n * `AccessDenied` naming the S3 code.\n */\nexport const withCredentialInvalidation = (\n storage: S3HttpStorage,\n invalidate: (signed: Request) => void,\n): S3HttpStorage => {\n attachInvalidate(storage, invalidate);\n return storage;\n};\n\n/**\n * `Storage` impl that speaks the S3 REST API directly. Works against\n * any S3-compatible endpoint — AWS S3, R2 (S3-compat), Minio. Does not\n * depend on any AWS SDK. GCS is NOT supported over this S3-interop path\n * (its conditional-write semantics can't linearize the commit log); use\n * the native `GcsHttpStorage` adapter instead.\n *\n * Authentication is plugged in via the `sign` callback; this class\n * itself does no SigV4. For Cloudflare Workers, the R2 binding fast\n * path lives in `@baerly/adapter-cloudflare`.\n */\nexport class S3HttpStorage implements Storage {\n readonly #endpoint: string;\n readonly #bucket: string;\n readonly #fetch: typeof fetch;\n readonly #sign?: (req: Request) => Promise<Request>;\n #invalidate?: (signed: Request) => void;\n readonly #retries: number | undefined;\n readonly #backoffMs: number | undefined;\n\n constructor(options: S3HttpStorageOptions) {\n this.#endpoint = options.endpoint.replace(/\\/+$/, \"\");\n this.#bucket = options.bucket;\n this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);\n this.#sign = options.sign;\n this.#retries = options.retries;\n this.#backoffMs = options.backoffMs;\n }\n\n static {\n attachInvalidate = (storage, invalidate) => {\n storage.#invalidate = invalidate;\n };\n }\n\n #objectUrl(key: string): string {\n // Choke point for every key-addressing verb — reject unaddressable\n // `.`/`..`/empty keys before URL normalization turns them into a\n // bucket-root 403. @see storage-compatibility.md \"Key namespace\".\n assertValidStorageKey(key);\n return `${this.#endpoint}/${this.#bucket}/${encodeURIComponent(key)}`;\n }\n\n async #send(req: Request): Promise<{ res: Response; signed: Request }> {\n const signed = this.#sign ? await this.#sign(req) : req;\n try {\n return { res: await this.#fetch(signed), signed };\n } catch (error) {\n // A cancelled call surfaces as the caller's abort reason, whatever\n // it is, not a retryable NetworkError; the retry loop ends on it.\n if (error instanceof BaerlyError || signed.signal.aborted) {\n throw error;\n }\n const msg = error instanceof Error ? error.message : String(error);\n throw new BaerlyError(\"NetworkError\", msg, { cause: error });\n }\n }\n\n /**\n * Send one request. An expired-session-token `400` drops the credentials\n * that signed it and resends once per storage call (`call` is shared by the\n * attempts of one `#retry`, so the retry policy cannot add a second\n * refresh). The resend is charged to the ledger here because it is a\n * request `retryStorageCall` did not count. A second expired-token\n * response is returned as is and maps to `AccessDenied`.\n */\n async #dispatch(make: () => Request, call: CallState): Promise<Response> {\n const first = await this.#send(make());\n if (\n this.#invalidate === undefined ||\n call.refreshed ||\n (await s3ExpiredTokenCode(first.res)) === undefined\n ) {\n return first.res;\n }\n // Release the socket held by the unread original body (the code was read from a clone).\n await first.res.body?.cancel();\n // A resend after the caller aborted would only re-sign and fail.\n first.signed.signal.throwIfAborted();\n call.refreshed = true;\n this.#invalidate(first.signed);\n currentRequestLedger()?.acquire(\"external\", currentRequestHold());\n const second = await this.#send(make());\n return second.res;\n }\n\n #retry<T>(\n fn: (call: CallState) => Promise<T>,\n opts?: Pick<StoragePutOptions, \"ifMatch\" | \"ifNoneMatch\" | \"signal\">,\n ): Promise<T> {\n const call: CallState = { refreshed: false };\n return retryStorageCall(() => fn(call), {\n ...(this.#retries !== undefined && { retries: this.#retries }),\n ...(this.#backoffMs !== undefined && { backoffMs: this.#backoffMs }),\n conditionalCreate: opts?.ifNoneMatch === \"*\",\n // An If-Match PUT is never resent: a resend after a lost ack would\n // see its own write as a 412.\n compareAndSwap: opts?.ifMatch !== undefined && opts.ifNoneMatch !== \"*\",\n // Every request leaves through `fetch`: the ledger's external pool.\n pool: \"external\",\n // Any caller abort ends the call; the loop never resends past it.\n ...(opts?.signal !== undefined && { signal: opts.signal }),\n });\n }\n\n async get(key: string, opts?: StorageGetOptions): Promise<StorageGetResult | null> {\n opts?.signal?.throwIfAborted();\n const url = this.#objectUrl(key);\n return this.#retry(async (call) => {\n const res = await this.#dispatch(\n () => new Request(url, { method: \"GET\", signal: opts?.signal ?? null }),\n call,\n );\n switch (res.status) {\n case 200: {\n const etag = res.headers.get(\"ETag\");\n if (etag === null) {\n throw new BaerlyError(\"InvalidResponse\", `GET ${key}: missing ETag`);\n }\n const body = new Uint8Array(await res.arrayBuffer());\n return { body, etag: strongEtag(etag) };\n }\n case 404: {\n return null;\n }\n default: {\n throw await mapS3StorageError(res, \"GET\", key);\n }\n }\n }, opts);\n }\n\n async put(key: string, body: Uint8Array, opts?: StoragePutOptions): Promise<StoragePutResult> {\n opts?.signal?.throwIfAborted();\n const url = this.#objectUrl(key);\n const headers = new Headers();\n headers.set(\"Content-Type\", opts?.contentType ?? \"application/octet-stream\");\n if (opts?.ifMatch !== undefined) {\n headers.set(\"If-Match\", opts.ifMatch);\n }\n if (opts?.ifNoneMatch === \"*\") {\n headers.set(\"If-None-Match\", \"*\");\n }\n return this.#retry(async (call) => {\n const res = await this.#dispatch(\n () =>\n new Request(url, {\n method: \"PUT\",\n headers,\n // TS lib.dom narrows BodyInit and a generic `Uint8Array<ArrayBufferLike>`\n // is not assignable; the runtime accepts it.\n body: body as BodyInit,\n signal: opts?.signal ?? null,\n }),\n call,\n );\n if (res.status === 412) {\n throw new BaerlyError(\"WriteConflict\", `PUT ${key}: precondition failed`);\n }\n // AWS S3 returns 409 ConditionalRequestConflict when concurrent\n // conditional writes race: a create (If-None-Match:\"*\") or a CAS\n // (If-Match). Minio returns 412. 409 means the write was contended\n // and may NOT have landed, so it maps to a retryable NetworkError.\n // For a create, this adapter's retry re-issues the same PUT, which\n // resolves to 200 (we win) or 412 (key changed/present →\n // WriteConflict → the Writer's readback adopts or re-probes); a\n // direct WriteConflict here would adopt-read a possibly-absent\n // entry. A CAS gets no resend, so its caller sees the NetworkError.\n if (res.status === 409 && (opts?.ifNoneMatch === \"*\" || opts?.ifMatch !== undefined)) {\n throw new BaerlyError(\n \"NetworkError\",\n `PUT ${key}: 409 ConditionalRequestConflict (contended conditional write; retryable)`,\n { cause: { status: res.status, ...retryAfterCause(res) } },\n );\n }\n // S3-compatible servers diverge on `If-Match` against a missing\n // key: AWS S3 returns 412, Minio returns 404 (NoSuchKey). Map\n // 404-with-ifMatch to the same `WriteConflict` semantic so consumers\n // don't have to special-case the wire reply.\n if (res.status === 404 && opts?.ifMatch !== undefined) {\n throw new BaerlyError(\n \"WriteConflict\",\n `PUT ${key}: precondition failed (ifMatch=${opts.ifMatch} but key does not exist)`,\n );\n }\n if (res.status !== 200 && res.status !== 204) {\n throw await mapS3StorageError(res, \"PUT\", key);\n }\n const etag = res.headers.get(\"ETag\");\n if (etag === null) {\n throw new BaerlyError(\"InvalidResponse\", `PUT ${key}: missing ETag`);\n }\n const dateStr = res.headers.get(\"Date\");\n const result: { etag: string; serverDate?: Date } = { etag: strongEtag(etag) };\n if (dateStr !== null) {\n result.serverDate = new Date(dateStr);\n }\n return result;\n }, opts);\n }\n\n async delete(key: string, opts?: { signal?: AbortSignal }): Promise<void> {\n opts?.signal?.throwIfAborted();\n const url = this.#objectUrl(key);\n await this.#retry(async (call) => {\n const res = await this.#dispatch(\n () => new Request(url, { method: \"DELETE\", signal: opts?.signal ?? null }),\n call,\n );\n // 200 / 204 / 404 → success (idempotent); any other status is a real\n // failure (403 → AccessDenied, 429/≥500 → retryable NetworkError, else\n // InvalidResponse) rather than a silently-swallowed non-2xx.\n if (res.status !== 200 && res.status !== 204 && res.status !== 404) {\n throw await mapS3StorageError(res, \"DELETE\", key);\n }\n }, opts);\n }\n\n async *list(\n prefix: string,\n opts?: { startAfter?: string; maxKeys?: number; signal?: AbortSignal },\n ): AsyncIterable<StorageListEntry> {\n opts?.signal?.throwIfAborted();\n let yielded = 0;\n let continuationToken: string | undefined;\n const startAfter = opts?.startAfter ?? \"\";\n while (true) {\n opts?.signal?.throwIfAborted();\n const params = new URLSearchParams();\n params.set(\"list-type\", \"2\");\n params.set(\"prefix\", prefix);\n // Force S3 to URL-encode object keys in the response so keys\n // containing XML-hostile or whitespace bytes survive the round trip.\n // The XML parser (`xmlVal` in ./xml.ts) reverses this on `Key`; only\n // the key family (Key/Prefix/Delimiter/StartAfter) is encoded — ETag\n // and NextContinuationToken come back verbatim.\n params.set(\"encoding-type\", \"url\");\n if (continuationToken !== undefined) {\n params.set(\"continuation-token\", continuationToken);\n } else if (startAfter !== \"\") {\n params.set(\"start-after\", startAfter);\n }\n if (opts?.maxKeys !== undefined) {\n const remaining = opts.maxKeys - yielded;\n params.set(\"max-keys\", String(Math.min(remaining, 1000)));\n }\n const url = `${this.#endpoint}/${this.#bucket}/?${params.toString()}`;\n\n // One retry budget per page: 429 and 5xx share the attempt counter,\n // so a page costs at most `retries + 1` requests whatever fails.\n const parsed = parseListObjectsV2CommandOutput(\n await this.#retry(async (call) => {\n const res = await this.#dispatch(\n () => new Request(url, { method: \"GET\", signal: opts?.signal ?? null }),\n call,\n );\n if (res.status !== 200) {\n throw await mapS3StorageError(res, \"LIST\", prefix);\n }\n return res.text();\n }, opts),\n );\n\n for (const entry of parsed.Contents ?? []) {\n if (entry.Key === undefined) {\n continue;\n }\n yield {\n key: entry.Key,\n etag: entry.ETag ?? \"\",\n };\n yielded += 1;\n if (opts?.maxKeys !== undefined && yielded >= opts.maxKeys) {\n return;\n }\n }\n continuationToken = parsed.NextContinuationToken;\n if (continuationToken === undefined || continuationToken === \"\") {\n return;\n }\n }\n }\n}\n","/**\n * @license MIT <https://opensource.org/licenses/MIT>\n * @copyright Michael Hart 2024\n */\nconst encoder = new TextEncoder();\nconst HOST_SERVICES = {\n appstream2: 'appstream',\n cloudhsmv2: 'cloudhsm',\n email: 'ses',\n marketplace: 'aws-marketplace',\n mobile: 'AWSMobileHubService',\n pinpoint: 'mobiletargeting',\n queue: 'sqs',\n 'git-codecommit': 'codecommit',\n 'mturk-requester-sandbox': 'mturk-requester',\n 'personalize-runtime': 'personalize',\n};\nconst UNSIGNABLE_HEADERS = new Set([\n 'authorization',\n 'content-type',\n 'content-length',\n 'user-agent',\n 'presigned-expires',\n 'expect',\n 'x-amzn-trace-id',\n 'range',\n 'connection',\n]);\nclass AwsClient {\n constructor({ accessKeyId, secretAccessKey, sessionToken, service, region, cache, retries, initRetryMs }) {\n if (accessKeyId == null) throw new TypeError('accessKeyId is a required option')\n if (secretAccessKey == null) throw new TypeError('secretAccessKey is a required option')\n this.accessKeyId = accessKeyId;\n this.secretAccessKey = secretAccessKey;\n this.sessionToken = sessionToken;\n this.service = service;\n this.region = region;\n this.cache = cache || new Map();\n this.retries = retries != null ? retries : 10;\n this.initRetryMs = initRetryMs || 50;\n }\n async sign(input, init) {\n if (input instanceof Request) {\n const { method, url, headers, body } = input;\n init = Object.assign({ method, url, headers }, init);\n if (init.body == null && headers.has('Content-Type')) {\n init.body = body != null && headers.has('X-Amz-Content-Sha256') ? body : await input.clone().arrayBuffer();\n }\n input = url;\n }\n const signer = new AwsV4Signer(Object.assign({ url: input.toString() }, init, this, init && init.aws));\n const signed = Object.assign({}, init, await signer.sign());\n delete signed.aws;\n try {\n return new Request(signed.url.toString(), signed)\n } catch (e) {\n if (e instanceof TypeError) {\n return new Request(signed.url.toString(), Object.assign({ duplex: 'half' }, signed))\n }\n throw e\n }\n }\n async fetch(input, init) {\n for (let i = 0; i <= this.retries; i++) {\n const fetched = fetch(await this.sign(input, init));\n if (i === this.retries) {\n return fetched\n }\n const res = await fetched;\n if (res.status < 500 && res.status !== 429) {\n return res\n }\n await new Promise(resolve => setTimeout(resolve, Math.random() * this.initRetryMs * Math.pow(2, i)));\n }\n throw new Error('An unknown error occurred, ensure retries is not negative')\n }\n}\nclass AwsV4Signer {\n constructor({ method, url, headers, body, accessKeyId, secretAccessKey, sessionToken, service, region, cache, datetime, signQuery, appendSessionToken, allHeaders, singleEncode }) {\n if (url == null) throw new TypeError('url is a required option')\n if (accessKeyId == null) throw new TypeError('accessKeyId is a required option')\n if (secretAccessKey == null) throw new TypeError('secretAccessKey is a required option')\n this.method = method || (body ? 'POST' : 'GET');\n this.url = new URL(url);\n this.headers = new Headers(headers || {});\n this.body = body;\n this.accessKeyId = accessKeyId;\n this.secretAccessKey = secretAccessKey;\n this.sessionToken = sessionToken;\n let guessedService, guessedRegion;\n if (!service || !region) {\n[guessedService, guessedRegion] = guessServiceRegion(this.url, this.headers);\n }\n this.service = service || guessedService || '';\n this.region = region || guessedRegion || 'us-east-1';\n this.cache = cache || new Map();\n this.datetime = datetime || new Date().toISOString().replace(/[:-]|\\.\\d{3}/g, '');\n this.signQuery = signQuery;\n this.appendSessionToken = appendSessionToken || this.service === 'iotdevicegateway';\n this.headers.delete('Host');\n if (this.service === 's3' && !this.signQuery && !this.headers.has('X-Amz-Content-Sha256')) {\n this.headers.set('X-Amz-Content-Sha256', 'UNSIGNED-PAYLOAD');\n }\n const params = this.signQuery ? this.url.searchParams : this.headers;\n params.set('X-Amz-Date', this.datetime);\n if (this.sessionToken && !this.appendSessionToken) {\n params.set('X-Amz-Security-Token', this.sessionToken);\n }\n this.signableHeaders = ['host', ...this.headers.keys()]\n .filter(header => allHeaders || !UNSIGNABLE_HEADERS.has(header))\n .sort();\n this.signedHeaders = this.signableHeaders.join(';');\n this.canonicalHeaders = this.signableHeaders\n .map(header => header + ':' + (header === 'host' ? this.url.host : (this.headers.get(header) || '').replace(/\\s+/g, ' ')))\n .join('\\n');\n this.credentialString = [this.datetime.slice(0, 8), this.region, this.service, 'aws4_request'].join('/');\n if (this.signQuery) {\n if (this.service === 's3' && !params.has('X-Amz-Expires')) {\n params.set('X-Amz-Expires', '86400');\n }\n params.set('X-Amz-Algorithm', 'AWS4-HMAC-SHA256');\n params.set('X-Amz-Credential', this.accessKeyId + '/' + this.credentialString);\n params.set('X-Amz-SignedHeaders', this.signedHeaders);\n }\n if (this.service === 's3') {\n try {\n this.encodedPath = decodeURIComponent(this.url.pathname.replace(/\\+/g, ' '));\n } catch (e) {\n this.encodedPath = this.url.pathname;\n }\n } else {\n this.encodedPath = this.url.pathname.replace(/\\/+/g, '/');\n }\n if (!singleEncode) {\n this.encodedPath = encodeURIComponent(this.encodedPath).replace(/%2F/g, '/');\n }\n this.encodedPath = encodeRfc3986(this.encodedPath);\n const seenKeys = new Set();\n this.encodedSearch = [...this.url.searchParams]\n .filter(([k]) => {\n if (!k) return false\n if (this.service === 's3') {\n if (seenKeys.has(k)) return false\n seenKeys.add(k);\n }\n return true\n })\n .map(pair => pair.map(p => encodeRfc3986(encodeURIComponent(p))))\n .sort(([k1, v1], [k2, v2]) => k1 < k2 ? -1 : k1 > k2 ? 1 : v1 < v2 ? -1 : v1 > v2 ? 1 : 0)\n .map(pair => pair.join('='))\n .join('&');\n }\n async sign() {\n if (this.signQuery) {\n this.url.searchParams.set('X-Amz-Signature', await this.signature());\n if (this.sessionToken && this.appendSessionToken) {\n this.url.searchParams.set('X-Amz-Security-Token', this.sessionToken);\n }\n } else {\n this.headers.set('Authorization', await this.authHeader());\n }\n return {\n method: this.method,\n url: this.url,\n headers: this.headers,\n body: this.body,\n }\n }\n async authHeader() {\n return [\n 'AWS4-HMAC-SHA256 Credential=' + this.accessKeyId + '/' + this.credentialString,\n 'SignedHeaders=' + this.signedHeaders,\n 'Signature=' + (await this.signature()),\n ].join(', ')\n }\n async signature() {\n const date = this.datetime.slice(0, 8);\n const cacheKey = [this.secretAccessKey, date, this.region, this.service].join();\n let kCredentials = this.cache.get(cacheKey);\n if (!kCredentials) {\n const kDate = await hmac('AWS4' + this.secretAccessKey, date);\n const kRegion = await hmac(kDate, this.region);\n const kService = await hmac(kRegion, this.service);\n kCredentials = await hmac(kService, 'aws4_request');\n this.cache.set(cacheKey, kCredentials);\n }\n return buf2hex(await hmac(kCredentials, await this.stringToSign()))\n }\n async stringToSign() {\n return [\n 'AWS4-HMAC-SHA256',\n this.datetime,\n this.credentialString,\n buf2hex(await hash(await this.canonicalString())),\n ].join('\\n')\n }\n async canonicalString() {\n return [\n this.method.toUpperCase(),\n this.encodedPath,\n this.encodedSearch,\n this.canonicalHeaders + '\\n',\n this.signedHeaders,\n await this.hexBodyHash(),\n ].join('\\n')\n }\n async hexBodyHash() {\n let hashHeader = this.headers.get('X-Amz-Content-Sha256') || (this.service === 's3' && this.signQuery ? 'UNSIGNED-PAYLOAD' : null);\n if (hashHeader == null) {\n if (this.body && typeof this.body !== 'string' && !('byteLength' in this.body)) {\n throw new Error('body must be a string, ArrayBuffer or ArrayBufferView, unless you include the X-Amz-Content-Sha256 header')\n }\n hashHeader = buf2hex(await hash(this.body || ''));\n }\n return hashHeader\n }\n}\nasync function hmac(key, string) {\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n typeof key === 'string' ? encoder.encode(key) : key,\n { name: 'HMAC', hash: { name: 'SHA-256' } },\n false,\n ['sign'],\n );\n return crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(string))\n}\nasync function hash(content) {\n return crypto.subtle.digest('SHA-256', typeof content === 'string' ? encoder.encode(content) : content)\n}\nconst HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];\nfunction buf2hex(arrayBuffer) {\n const buffer = new Uint8Array(arrayBuffer);\n let out = '';\n for (let idx = 0; idx < buffer.length; idx++) {\n const n = buffer[idx];\n out += HEX_CHARS[(n >>> 4) & 0xF];\n out += HEX_CHARS[n & 0xF];\n }\n return out\n}\nfunction encodeRfc3986(urlEncodedStr) {\n return urlEncodedStr.replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())\n}\nfunction guessServiceRegion(url, headers) {\n const { hostname, pathname } = url;\n if (hostname.endsWith('.on.aws')) {\n const match = hostname.match(/^[^.]{1,63}\\.lambda-url\\.([^.]{1,63})\\.on\\.aws$/);\n return match != null ? ['lambda', match[1] || ''] : ['', '']\n }\n if (hostname.endsWith('.r2.cloudflarestorage.com')) {\n return ['s3', 'auto']\n }\n if (hostname.endsWith('.backblazeb2.com')) {\n const match = hostname.match(/^(?:[^.]{1,63}\\.)?s3\\.([^.]{1,63})\\.backblazeb2\\.com$/);\n return match != null ? ['s3', match[1] || ''] : ['', '']\n }\n const match = hostname.replace('dualstack.', '').match(/([^.]{1,63})\\.(?:([^.]{0,63})\\.)?amazonaws\\.com(?:\\.cn)?$/);\n let service = (match && match[1]) || '';\n let region = match && match[2];\n if (region === 'us-gov') {\n region = 'us-gov-west-1';\n } else if (region === 's3' || region === 's3-accelerate') {\n region = 'us-east-1';\n service = 's3';\n } else if (service === 'iot') {\n if (hostname.startsWith('iot.')) {\n service = 'execute-api';\n } else if (hostname.startsWith('data.jobs.iot.')) {\n service = 'iot-jobs-data';\n } else {\n service = pathname === '/mqtt' ? 'iotdevicegateway' : 'iotdata';\n }\n } else if (service === 'autoscaling') {\n const targetPrefix = (headers.get('X-Amz-Target') || '').split('.')[0];\n if (targetPrefix === 'AnyScaleFrontendService') {\n service = 'application-autoscaling';\n } else if (targetPrefix === 'AnyScaleScalingPlannerFrontendService') {\n service = 'autoscaling-plans';\n }\n } else if (region == null && service.startsWith('s3-')) {\n region = service.slice(3).replace(/^fips-|^external-1/, '');\n service = 's3';\n } else if (service.endsWith('-fips')) {\n service = service.slice(0, -5);\n } else if (region && /-\\d$/.test(service) && !/-\\d$/.test(region)) {\n[service, region] = [region, service];\n }\n return [HOST_SERVICES[service] || service, region || '']\n}\n\nexport { AwsClient, AwsV4Signer };\n","import type { AwsClient } from \"aws4fetch\";\n\n/**\n * Sign an S3 request through `aws4fetch` without touching its body.\n *\n * Given a `Request` with a `Content-Type` and no `X-Amz-Content-Sha256`,\n * `AwsClient.sign` reads the whole body through `req.clone().arrayBuffer()`\n * and rebuilds the request around the copy, although for S3 it signs\n * `UNSIGNED-PAYLOAD` and never hashes the body. Signing only the method,\n * URL and headers yields the same `Authorization` and header set without\n * that copy. The header defaults to `UNSIGNED-PAYLOAD` exactly as\n * `aws4fetch` defaults it for S3, so the signature is unchanged.\n *\n * The signed headers go back onto a `Request` built from the original, so\n * the original body source survives and Node still sends a `Content-Length`.\n * Handing `aws4fetch` the original `Request` with `X-Amz-Content-Sha256`\n * already set would skip the copy too, but it rebuilds the request around\n * `req.body` (a `ReadableStream`), and undici then sends\n * `Transfer-Encoding: chunked` with no length, which AWS S3 `PutObject`\n * rejects.\n */\nexport async function signHeadersOnly(client: AwsClient, req: Request): Promise<Request> {\n const headers = new Headers(req.headers);\n if (!headers.has(\"X-Amz-Content-Sha256\")) {\n headers.set(\"X-Amz-Content-Sha256\", \"UNSIGNED-PAYLOAD\");\n }\n const signed = await client.sign(req.url, { method: req.method, headers });\n return new Request(req, { headers: signed.headers });\n}\n"],"ignoreList":[1],"mappings":";;;;;;;;;;;;;;AAwBA,MAAM,cAAc,SAA0B,KAAK,WAAW,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI;AAkDtF,IAAI;;;;;;;;;AAUJ,MAAa,8BACX,SACA,eACkB;CAClB,iBAAiB,SAAS,UAAU;CACpC,OAAO;AACT;;;;;;;;;;;;AAaA,IAAa,gBAAb,MAA8C;CACnC;CACA;CACA;CACA;CACT;CACS;CACA;CAET,YAAY,SAA+B;EACzC,KAAK,YAAY,QAAQ,SAAS,QAAQ,QAAQ,EAAE;EACpD,KAAK,UAAU,QAAQ;EACvB,KAAK,SAAS,QAAQ,SAAS,WAAW,MAAM,KAAK,UAAU;EAC/D,KAAK,QAAQ,QAAQ;EACrB,KAAK,WAAW,QAAQ;EACxB,KAAK,aAAa,QAAQ;CAC5B;CAEA;EACE,oBAAoB,SAAS,eAAe;GAC1C,QAAQ,cAAc;EACxB;CACF;CAEA,WAAW,KAAqB;EAI9B,sBAAsB,GAAG;EACzB,OAAO,GAAG,KAAK,UAAU,GAAG,KAAK,QAAQ,GAAG,mBAAmB,GAAG;CACpE;CAEA,MAAM,MAAM,KAA2D;EACrE,MAAM,SAAS,KAAK,QAAQ,MAAM,KAAK,MAAM,GAAG,IAAI;EACpD,IAAI;GACF,OAAO;IAAE,KAAK,MAAM,KAAK,OAAO,MAAM;IAAG;GAAO;EAClD,SAAS,OAAO;GAGd,IAAI,iBAAiB,eAAe,OAAO,OAAO,SAChD,MAAM;GAER,MAAM,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;GACjE,MAAM,IAAI,YAAY,gBAAgB,KAAK,EAAE,OAAO,MAAM,CAAC;EAC7D;CACF;;;;;;;;;CAUA,MAAM,UAAU,MAAqB,MAAoC;EACvE,MAAM,QAAQ,MAAM,KA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AAU;OACL,IAAI,iBAAiB,yCAC1B,UAAU;CAEd,OAAO,IAAI,UAAU,QAAQ,QAAQ,WAAW,KAAK,GAAG;EACtD,SAAS,QAAQ,MAAM,CAAC,CAAC,CAAC,QAAQ,sBAAsB,EAAE;EAC1D,UAAU;CACZ,OAAO,IAAI,QAAQ,SAAS,OAAO,GACjC,UAAU,QAAQ,MAAM,GAAG,EAAE;MACxB,IAAI,UAAU,OAAO,KAAK,OAAO,KAAK,CAAC,OAAO,KAAK,MAAM,GAClE,CAAC,SAAS,UAAU,CAAC,QAAQ,OAAO;CAElC,OAAO,CAAC,cAAc,YAAY,SAAS,UAAU,EAAE;AACzD;;;;;;;;;;;;;;;;;;;;;AC5QA,eAAsB,gBAAgB,QAAmB,KAAgC;CACvF,MAAM,UAAU,IAAI,QAAQ,IAAI,OAAO;CACvC,IAAI,CAAC,QAAQ,IAAI,sBAAsB,GACrC,QAAQ,IAAI,wBAAwB,kBAAkB;CAExD,MAAM,SAAS,MAAM,OAAO,KAAK,IAAI,KAAK;EAAE,QAAQ,IAAI;EAAQ;CAAQ,CAAC;CACzE,OAAO,IAAI,QAAQ,KAAK,EAAE,SAAS,OAAO,QAAQ,CAAC;AACrD"}
|
|
@@ -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":";;;;;;;;;;;;;;AAYA,MAAa,gBAAgB,UAAuC,UAAU,KAAK;;;;;;;;;ACHnF,MAAM,uCAAuB,IAAI,QAAqB;;AAGtD,MAAa,oBAA2C,QAAc;CACpE,qBAAqB,IAAI,GAAG;CAC5B,OAAO;AACT;;ACZA,MAAM,gCACJ;;;;;;;;;;;;;;;;;AAkBF,MAAa,mBAAmB,QAAgB,aAA8B;CAC5E,IAAI,WAAW,UACb,OAAO;CAET,IAAI,OAAO,WAAW,YAAY,OAAO,UAAU,KAAK,MAAM,SAAS,UAAU,KAAK,GACpF,MAAM,iBACJ,IAAI,YACF,iBACA,cAAc,KAAK,UAAU,QAAQ,EAAE,+DAA+D,KAAK,UAAU,MAAM,EAAE,uEAC7H,EAAE,YAAY,8BAA8B,CAC9C,CACF;CAEF,OAAO;AACT;;;;;;;;;;;;;;;ACVA,MAAM,iBAAiB;;;;;;;AAQvB,MAAM,aAAa;AAEnB,MAAM,UAAU,MAAM,aAAa;AAEnC,MAAM,OAAO,MAAsB,EAAE,SAAS,CAAC,CAAC,SAAS,YAAY,GAAG;;;;;;;;;;;AAYxE,MAAa,eACX,kBACA,QACA,QACA,WACW;CAIX,IACE,CAAC,OAAO,UAAU,MAAM,KACxB,CAAC,OAAO,UAAU,MAAM,KACxB,SAAS,KACT,SAAS,KACT,SAAS,UACT,SAAS,SAET,MAAM,IAAI,YAAY,iBAAiB,+BAA+B,OAAO,IAAI,OAAO,EAAE;CAE5F,IAAI,CAAC,iBAAiB,K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