@narumitw/pi-sync 0.52.0 → 0.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +3 -4
  2. package/dist/chunks/{chunk-7D6RDCVC.ts → chunk-BX3K7YW7.ts} +2 -2
  3. package/dist/chunks/{chunk-QHSQCK7L.ts → chunk-FHNISD7A.ts} +2 -2
  4. package/dist/chunks/{chunk-2CBF6SMT.ts → chunk-I5IG4WV4.ts} +2 -2
  5. package/dist/chunks/{chunk-WKPX2SIC.ts → chunk-NGIJQSRE.ts} +2 -2
  6. package/dist/chunks/{chunk-2GUXI2XT.ts → chunk-QTCGWQP3.ts} +74 -30
  7. package/dist/chunks/chunk-QTCGWQP3.ts.map +7 -0
  8. package/dist/chunks/{conflict-review-T3PB7ZSD.ts → conflict-review-NK5CM6UA.ts} +3 -3
  9. package/dist/chunks/{manager-result-dispatcher-VIDT3QBU.ts → manager-result-dispatcher-35TM3ZR3.ts} +4 -4
  10. package/dist/chunks/{manager-ui-HSMHS3OK.ts → manager-ui-6AYMNXUP.ts} +7 -7
  11. package/dist/chunks/{remote-selection-ui-SMF5CLFO.ts → remote-selection-ui-BB5SX6P5.ts} +3 -3
  12. package/dist/chunks/{s3-backend-DJ2NT3SZ.ts → s3-backend-O2AS3TH6.ts} +99 -9
  13. package/dist/chunks/s3-backend-O2AS3TH6.ts.map +7 -0
  14. package/dist/chunks/{sync-inspection-VBJF2FWC.ts → sync-inspection-RLIHUYDV.ts} +3 -3
  15. package/dist/chunks/{sync-operations-27VQUDH2.ts → sync-operations-RBUOUJWL.ts} +4 -4
  16. package/dist/index.ts +5 -5
  17. package/package.json +1 -1
  18. package/src/backends/s3/s3-backend.ts +23 -5
  19. package/src/backends/s3/s3-client.ts +33 -6
  20. package/src/backends/s3/s3-conditions.ts +67 -0
  21. package/src/sync/merge-apply.ts +76 -28
  22. package/dist/chunks/chunk-2GUXI2XT.ts.map +0 -7
  23. package/dist/chunks/s3-backend-DJ2NT3SZ.ts.map +0 -7
  24. /package/dist/chunks/{chunk-7D6RDCVC.ts.map → chunk-BX3K7YW7.ts.map} +0 -0
  25. /package/dist/chunks/{chunk-QHSQCK7L.ts.map → chunk-FHNISD7A.ts.map} +0 -0
  26. /package/dist/chunks/{chunk-2CBF6SMT.ts.map → chunk-I5IG4WV4.ts.map} +0 -0
  27. /package/dist/chunks/{chunk-WKPX2SIC.ts.map → chunk-NGIJQSRE.ts.map} +0 -0
  28. /package/dist/chunks/{conflict-review-T3PB7ZSD.ts.map → conflict-review-NK5CM6UA.ts.map} +0 -0
  29. /package/dist/chunks/{manager-result-dispatcher-VIDT3QBU.ts.map → manager-result-dispatcher-35TM3ZR3.ts.map} +0 -0
  30. /package/dist/chunks/{manager-ui-HSMHS3OK.ts.map → manager-ui-6AYMNXUP.ts.map} +0 -0
  31. /package/dist/chunks/{remote-selection-ui-SMF5CLFO.ts.map → remote-selection-ui-BB5SX6P5.ts.map} +0 -0
  32. /package/dist/chunks/{sync-inspection-VBJF2FWC.ts.map → sync-inspection-RLIHUYDV.ts.map} +0 -0
  33. /package/dist/chunks/{sync-operations-27VQUDH2.ts.map → sync-operations-RBUOUJWL.ts.map} +0 -0
@@ -2,10 +2,10 @@
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
4
  dispatchManagerResult
5
- } from "./chunk-WKPX2SIC.ts";
6
- import "./chunk-2CBF6SMT.ts";
5
+ } from "./chunk-NGIJQSRE.ts";
6
+ import "./chunk-I5IG4WV4.ts";
7
7
  import "./chunk-V6VZ5TLG.ts";
8
- import "./chunk-QHSQCK7L.ts";
8
+ import "./chunk-FHNISD7A.ts";
9
9
  import "./chunk-K3RIEL6J.ts";
10
10
  import "./chunk-AMJRUXFT.ts";
11
11
  import "./chunk-CHSIAC4N.ts";
@@ -20,4 +20,4 @@ import "./chunk-HUZ5CR4W.ts";
20
20
  export {
21
21
  dispatchManagerResult
22
22
  };
23
- //# sourceMappingURL=manager-result-dispatcher-VIDT3QBU.ts.map
23
+ //# sourceMappingURL=manager-result-dispatcher-35TM3ZR3.ts.map
@@ -23,12 +23,12 @@ import {
23
23
  } from "./chunk-4SBTAVFL.ts";
24
24
  import {
25
25
  dispatchManagerResult
26
- } from "./chunk-WKPX2SIC.ts";
26
+ } from "./chunk-NGIJQSRE.ts";
27
27
  import {
28
28
  runCancellableOperation
29
- } from "./chunk-2CBF6SMT.ts";
29
+ } from "./chunk-I5IG4WV4.ts";
30
30
  import "./chunk-V6VZ5TLG.ts";
31
- import "./chunk-QHSQCK7L.ts";
31
+ import "./chunk-FHNISD7A.ts";
32
32
  import {
33
33
  SETUP_SWITCH_ACTION_OPTIONS,
34
34
  saveOnSwitch,
@@ -172,7 +172,7 @@ function blockedSyncMenuItem(label, manager) {
172
172
  };
173
173
  }
174
174
  async function showManagerAttention(ctx, attention, runRoute, signal, onSelectionResolved) {
175
- const { showRemoteSelectionReview } = await import("./remote-selection-ui-SMF5CLFO.ts");
175
+ const { showRemoteSelectionReview } = await import("./remote-selection-ui-BB5SX6P5.ts");
176
176
  if (signal?.aborted) return "close";
177
177
  const review = await showRemoteSelectionReview(ctx, attention.decision.setupName, signal, void 0, {
178
178
  decision: attention.decision,
@@ -908,7 +908,7 @@ async function showSyncSettings(ctx, runRoute, signal) {
908
908
  },
909
909
  "remote-include": async ({ signal: actionSignal }) => {
910
910
  const reviewSignal = signal ? AbortSignal.any([signal, actionSignal]) : actionSignal;
911
- const { showRemoteSelectionReview } = await import("./remote-selection-ui-SMF5CLFO.ts");
911
+ const { showRemoteSelectionReview } = await import("./remote-selection-ui-BB5SX6P5.ts");
912
912
  if (reviewSignal.aborted) return { kind: "rejected" };
913
913
  const review = await showRemoteSelectionReview(ctx, setupName, reviewSignal, void 0, {
914
914
  origin: "settings",
@@ -1563,7 +1563,7 @@ async function showSyncManager(ctx, runRoute, sessionSignal, options = {}) {
1563
1563
  if (!attention) {
1564
1564
  const observation = options.getObservation?.();
1565
1565
  if (!observation) return { kind: "stay" };
1566
- const { showRemoteSelectionReview } = await import("./remote-selection-ui-SMF5CLFO.ts");
1566
+ const { showRemoteSelectionReview } = await import("./remote-selection-ui-BB5SX6P5.ts");
1567
1567
  if (sessionSignal?.aborted) return { kind: "close" };
1568
1568
  const config = await loadConfig();
1569
1569
  if (sessionSignal?.aborted) return { kind: "close" };
@@ -1729,4 +1729,4 @@ function syncMainMenuItem(label) {
1729
1729
  export {
1730
1730
  showSyncManager
1731
1731
  };
1732
- //# sourceMappingURL=manager-ui-HSMHS3OK.ts.map
1732
+ //# sourceMappingURL=manager-ui-6AYMNXUP.ts.map
@@ -2,9 +2,9 @@
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
4
  showRemoteSelectionReview
5
- } from "./chunk-2CBF6SMT.ts";
5
+ } from "./chunk-I5IG4WV4.ts";
6
6
  import "./chunk-V6VZ5TLG.ts";
7
- import "./chunk-QHSQCK7L.ts";
7
+ import "./chunk-FHNISD7A.ts";
8
8
  import "./chunk-K3RIEL6J.ts";
9
9
  import "./chunk-AMJRUXFT.ts";
10
10
  import "./chunk-CHSIAC4N.ts";
@@ -19,4 +19,4 @@ import "./chunk-HUZ5CR4W.ts";
19
19
  export {
20
20
  showRemoteSelectionReview
21
21
  };
22
- //# sourceMappingURL=remote-selection-ui-SMF5CLFO.ts.map
22
+ //# sourceMappingURL=remote-selection-ui-BB5SX6P5.ts.map
@@ -143,16 +143,21 @@ var S3Client = class {
143
143
  }
144
144
  throw lastError;
145
145
  }
146
- async putJson(key, value) {
146
+ async putJson(key, value, options = {}) {
147
147
  const body = Buffer.from(JSON.stringify(value, null, " "), "utf8");
148
- await this.putBuffer(key, body, "application/json");
148
+ await this.putBuffer(key, body, "application/json", options);
149
149
  }
150
150
  async putBuffer(key, body, contentType, options = {}) {
151
151
  const headers = { "content-type": contentType };
152
152
  if (options.ifAbsent) headers["if-none-match"] = "*";
153
+ if (options.ifMatch) headers["if-match"] = options.ifMatch;
153
154
  const response = await this.request("PUT", key, body, headers);
154
- if (options.ifAbsent && response.status === 412) {
155
- throw new S3ObjectAlreadyExistsError(key);
155
+ if ((options.ifAbsent || options.ifMatch) && (response.status === 412 || response.status === 409)) {
156
+ await response.body?.cancel();
157
+ if (options.ifAbsent && response.status === 412) throw new S3ObjectAlreadyExistsError(key);
158
+ if (options.ifAbsent || options.ifMatch) {
159
+ throw new S3HttpError(`S3 conditional PUT rejected (${response.status}).`, response.status);
160
+ }
156
161
  }
157
162
  if (!response.ok) {
158
163
  throw new S3HttpError(
@@ -160,6 +165,17 @@ var S3Client = class {
160
165
  response.status
161
166
  );
162
167
  }
168
+ await response.body?.cancel();
169
+ }
170
+ async delete(key) {
171
+ const response = await this.request("DELETE", key);
172
+ if (!response.ok && response.status !== 404) {
173
+ throw new S3HttpError(
174
+ `S3 DELETE failed (${response.status}): ${await this.readErrorText(response)}`,
175
+ response.status
176
+ );
177
+ }
178
+ await response.body?.cancel();
163
179
  }
164
180
  async request(method, key, body, extraHeaders = {}) {
165
181
  const url = new URL(this.endpoint.toString());
@@ -331,6 +347,66 @@ function isSecurityTokenInvalidArgument(text) {
331
347
  return text.includes("<Code>InvalidArgument</Code>") && text.includes("<Message>X-Amz-Security-Token</Message>");
332
348
  }
333
349
 
350
+ // src/backends/s3/s3-conditions.ts
351
+ import { randomUUID } from "node:crypto";
352
+ function requireS3Etag(value) {
353
+ if (!value || !/^"[^"\r\n]+"$/u.test(value)) throw new Error("S3 conditional publication requires a strong ETag.");
354
+ return value;
355
+ }
356
+ async function verifyS3Conditions(config, signal) {
357
+ const key = posixJoin(
358
+ config.destination.prefix === "./" ? "" : config.destination.prefix,
359
+ ".pi-sync-probes",
360
+ randomUUID()
361
+ );
362
+ const client = new S3Client(config, signal);
363
+ let failure;
364
+ let attempted = false;
365
+ try {
366
+ signal?.throwIfAborted();
367
+ attempted = true;
368
+ await client.putBuffer(key, Buffer.from("first"), "text/plain", { ifAbsent: true });
369
+ const first = await client.getBuffer(key);
370
+ const etag = requireS3Etag(first.etag);
371
+ if (!first.value?.equals(Buffer.from("first"))) throw new Error("S3 conditional probe read did not match.");
372
+ await expectRejected(client.putBuffer(key, Buffer.from("ignored"), "text/plain", { ifAbsent: true }));
373
+ await expectRejected(client.putBuffer(key, Buffer.from("ignored"), "text/plain", { ifMatch: '"pi-sync-stale"' }));
374
+ const unchanged = await client.getBuffer(key);
375
+ if (!unchanged.value?.equals(first.value) || unchanged.etag !== etag)
376
+ throw new Error("S3 failed precondition changed the probe.");
377
+ await client.putBuffer(key, Buffer.from("second"), "text/plain", { ifMatch: etag });
378
+ const second = await client.getBuffer(key);
379
+ if (!second.value?.equals(Buffer.from("second")) || requireS3Etag(second.etag) === etag) {
380
+ throw new Error("S3 conditional write did not rotate the probe ETag.");
381
+ }
382
+ await expectRejected(client.putBuffer(key, Buffer.from("ignored"), "text/plain", { ifMatch: etag }));
383
+ const final = await client.getBuffer(key);
384
+ if (!final.value?.equals(second.value) || final.etag !== second.etag)
385
+ throw new Error("S3 stale ETag changed the probe.");
386
+ signal?.throwIfAborted();
387
+ } catch (error) {
388
+ failure = error;
389
+ }
390
+ if (attempted) {
391
+ try {
392
+ await new S3Client(config, AbortSignal.timeout(1e4)).delete(key);
393
+ } catch (error) {
394
+ throw new Error(`S3 probe cleanup failed; remove ${key}.`, { cause: failure ?? error });
395
+ }
396
+ }
397
+ if (failure) throw failure;
398
+ signal?.throwIfAborted();
399
+ }
400
+ async function expectRejected(operation) {
401
+ try {
402
+ await operation;
403
+ } catch (error) {
404
+ if (error instanceof S3ObjectAlreadyExistsError || error instanceof S3HttpError && error.status === 412) return;
405
+ throw error;
406
+ }
407
+ throw new Error("S3 server ignored a write precondition; publication stopped for safety.");
408
+ }
409
+
334
410
  // src/backends/s3/s3-backend.ts
335
411
  var VERSION = 1;
336
412
  var POST_COMMIT_TIMEOUT_MS = 3e4;
@@ -346,7 +422,7 @@ var S3SyncBackend = class {
346
422
  postCommitTimeoutMs;
347
423
  identity;
348
424
  destination;
349
- capability = "read-check-write-verify";
425
+ capability = "conditional-required";
350
426
  checksums = /* @__PURE__ */ new Map();
351
427
  sameRevision(left, right) {
352
428
  return left === right;
@@ -381,6 +457,8 @@ var S3SyncBackend = class {
381
457
  async publishSnapshot(snapshot, expected, options = {}) {
382
458
  throwIfAborted(options.signal);
383
459
  assertSnapshotIdentity(snapshot, this.config.destination.namespace);
460
+ await verifyS3Conditions(this.config, options.signal);
461
+ throwIfAborted(options.signal);
384
462
  const stagedKey = snapshotKey(this.config, snapshot.id);
385
463
  const encoded = await encodeSnapshot(snapshot);
386
464
  throwIfAborted(options.signal);
@@ -416,12 +494,18 @@ var S3SyncBackend = class {
416
494
  currentHead: current
417
495
  });
418
496
  }
497
+ const condition = currentObject.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(currentObject.etag) };
419
498
  throwIfAborted(options.signal);
420
499
  options.onCommit?.();
421
500
  const commitClient = new S3Client(this.config, AbortSignal.timeout(this.postCommitTimeoutMs));
422
501
  try {
423
- await commitClient.putJson(latestKey(this.config), pointer);
502
+ await commitClient.putJson(latestKey(this.config), pointer, condition);
424
503
  } catch (error) {
504
+ if (error instanceof S3ObjectAlreadyExistsError || error instanceof S3HttpError && (error.status === 412 || error.status === 409)) {
505
+ throw new SyncBackendConflictError("Remote conditional publication was rejected; reread and retry.", {
506
+ cause: error
507
+ });
508
+ }
425
509
  throw new SyncBackendPublicationOutcomeUnknownError(
426
510
  `Remote publication outcome is unknown: ${errorMessage2(error)}`,
427
511
  { cause: error }
@@ -543,8 +627,14 @@ var S3SyncBackend = class {
543
627
  async updateHistory(client, pointer) {
544
628
  const object = await client.getJson(historyKey(this.config));
545
629
  const snapshots = object.missing ? [] : requireHistory(object.value, this.config.destination.namespace);
546
- const next = [...snapshots.filter((snapshot) => snapshot.snapshot !== pointer.snapshot), pointer].slice(-100);
547
- await client.putJson(historyKey(this.config), { version: VERSION, snapshots: next });
630
+ const existing = snapshots.find((snapshot) => snapshot.snapshot === pointer.snapshot);
631
+ if (existing) {
632
+ if (!samePointer(existing, pointer)) throw new Error("Remote history rebound an immutable snapshot reference.");
633
+ return;
634
+ }
635
+ const next = [...snapshots, pointer].slice(-100);
636
+ const condition = object.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(object.etag) };
637
+ await client.putJson(historyKey(this.config), { version: VERSION, snapshots: next }, condition);
548
638
  }
549
639
  };
550
640
  function latestKey(config) {
@@ -731,4 +821,4 @@ export {
731
821
  snapshotKey,
732
822
  storageRoot
733
823
  };
734
- //# sourceMappingURL=s3-backend-DJ2NT3SZ.ts.map
824
+ //# sourceMappingURL=s3-backend-O2AS3TH6.ts.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/backends/s3/s3-backend.ts", "../../src/backends/s3/s3-client.ts", "../../src/backends/s3/s3-conditions.ts"],
4
+ "sourcesContent": ["import { createHash } from \"node:crypto\";\nimport { encodeKey, posixJoin } from \"../../paths.js\";\nimport { sessionTokenWarnings } from \"../../settings/config.js\";\nimport { decodeSnapshot, encodeSnapshot } from \"../../snapshot/snapshot-codec.js\";\nimport type { Snapshot } from \"../../snapshot/snapshot-types.js\";\nimport { validatePortableSnapshot } from \"../../sync/local-fields.js\";\nimport { syncErrorGuidance } from \"../../sync/sync-error-guidance.js\";\nimport { portableSnapshotSelection } from \"../../sync/sync-policy.js\";\nimport type { LatestPointer, RemoteObject, ResolvedS3Backend } from \"../backend-types.js\";\nimport {\n type BackendDiagnostic,\n type ExpectedRemoteHead,\n type PublishSnapshotOptions,\n type PublishSnapshotResult,\n type RemoteHead,\n type RemoteHistoryEntry,\n type SyncBackend,\n SyncBackendConflictError,\n SyncBackendPublicationOutcomeUnknownError,\n} from \"../sync-backend.js\";\nimport { S3Client, S3HttpError, S3ObjectAlreadyExistsError } from \"./s3-client.js\";\nimport { requireS3Etag, verifyS3Conditions } from \"./s3-conditions.js\";\n\nconst VERSION = 1;\nconst POST_COMMIT_TIMEOUT_MS = 30_000;\n\nexport class S3SyncBackend implements SyncBackend {\n readonly identity: string;\n readonly destination: string;\n readonly capability = \"conditional-required\" as const;\n private readonly checksums = new Map<string, string>();\n\n constructor(\n private readonly config: ResolvedS3Backend,\n private readonly postCommitTimeoutMs = POST_COMMIT_TIMEOUT_MS,\n ) {\n assertSafeDestination(config);\n this.identity = s3BackendIdentity(config);\n this.destination = s3Destination(config);\n }\n\n sameRevision(left: string, right: string) {\n return left === right;\n }\n\n async readHead(signal?: AbortSignal): Promise<RemoteHead | undefined> {\n const object = await new S3Client(this.config, signal).getJson<LatestPointer>(latestKey(this.config));\n throwIfAborted(signal);\n if (object.missing) return undefined;\n const pointer = requirePointer(\n object.value,\n \"Remote latest pointer is malformed.\",\n this.config.destination.namespace,\n );\n this.registerChecksum(pointer.snapshot, pointer.sha256);\n return remoteHead(pointer, this.identity, object.etag);\n }\n\n async readSnapshot(reference: string, signal?: AbortSignal): Promise<Snapshot> {\n const expectedChecksum = this.checksums.get(reference) ?? (await this.resolveChecksum(reference, signal));\n const object = await new S3Client(this.config, signal).getBuffer(snapshotKey(this.config, reference));\n throwIfAborted(signal);\n if (!object.value) throw new Error(`Snapshot not found: ${reference}`);\n if (expectedChecksum && sha256(object.value) !== expectedChecksum) {\n throw new Error(\"Remote snapshot checksum mismatch.\");\n }\n const snapshot = await decodeSnapshot(object.value, { signal });\n if (snapshot.id !== reference || snapshot.profile !== this.config.destination.namespace) {\n throw new Error(\"Remote snapshot identity mismatch.\");\n }\n validateSnapshotBundle(snapshot);\n return snapshot;\n }\n\n async publishSnapshot(\n snapshot: Snapshot,\n expected: ExpectedRemoteHead,\n options: PublishSnapshotOptions = {},\n ): Promise<PublishSnapshotResult> {\n throwIfAborted(options.signal);\n assertSnapshotIdentity(snapshot, this.config.destination.namespace);\n await verifyS3Conditions(this.config, options.signal);\n throwIfAborted(options.signal);\n const stagedKey = snapshotKey(this.config, snapshot.id);\n const encoded = await encodeSnapshot(snapshot);\n throwIfAborted(options.signal);\n const pointer = pointerFor(this.config, snapshot, sha256(encoded));\n const cancellableClient = new S3Client(this.config, options.signal);\n try {\n await cancellableClient.putBuffer(stagedKey, encoded, \"application/gzip\", {\n ifAbsent: true,\n });\n } catch (error) {\n if (!(error instanceof S3ObjectAlreadyExistsError)) throw error;\n const existing = await cancellableClient.getBuffer(stagedKey);\n if (!existing.value || sha256(existing.value) !== pointer.sha256) {\n throw new SyncBackendConflictError(\n `Immutable snapshot id already exists with different content: ${snapshot.id}`,\n );\n }\n }\n const currentObject = await cancellableClient.getJson<LatestPointer>(latestKey(this.config));\n throwIfAborted(options.signal);\n let current: RemoteHead | undefined;\n if (!currentObject.missing) {\n const currentPointer = requirePointer(\n currentObject.value,\n \"Remote latest pointer is malformed.\",\n this.config.destination.namespace,\n );\n this.registerChecksum(currentPointer.snapshot, currentPointer.sha256);\n current = remoteHead(currentPointer, this.identity, currentObject.etag);\n }\n if (!matchesExpected(current, expected)) {\n throw new SyncBackendConflictError(\"Remote changed while pushing. Run /sync pull first, then retry.\", {\n currentHead: current,\n });\n }\n const condition = currentObject.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(currentObject.etag) };\n throwIfAborted(options.signal);\n options.onCommit?.();\n\n // The active-head PUT is the publication boundary. Do not bind it or its\n // verification to a user-cancellation signal after the boundary begins.\n const commitClient = new S3Client(this.config, AbortSignal.timeout(this.postCommitTimeoutMs));\n try {\n await commitClient.putJson(latestKey(this.config), pointer, condition);\n } catch (error) {\n if (\n error instanceof S3ObjectAlreadyExistsError ||\n (error instanceof S3HttpError && (error.status === 412 || error.status === 409))\n ) {\n throw new SyncBackendConflictError(\"Remote conditional publication was rejected; reread and retry.\", {\n cause: error,\n });\n }\n throw new SyncBackendPublicationOutcomeUnknownError(\n `Remote publication outcome is unknown: ${errorMessage(error)}`,\n { cause: error },\n );\n }\n\n let verifiedObject: RemoteObject<LatestPointer>;\n try {\n verifiedObject = await commitClient.getJson<LatestPointer>(latestKey(this.config));\n } catch (error) {\n throw new SyncBackendPublicationOutcomeUnknownError(\n `Remote snapshot may be active, but publication could not be verified: ${errorMessage(error)}`,\n { cause: error },\n );\n }\n let verifiedPointer: LatestPointer;\n try {\n verifiedPointer = requirePointer(\n verifiedObject.value,\n \"Remote latest pointer is malformed after publication.\",\n this.config.destination.namespace,\n );\n } catch (error) {\n throw new SyncBackendPublicationOutcomeUnknownError(\n `Remote snapshot may be active, but publication verification was malformed: ${errorMessage(error)}`,\n { cause: error },\n );\n }\n if (!samePointer(verifiedPointer, pointer)) {\n throw new SyncBackendConflictError(\n \"Remote latest changed immediately after push. Run /sync status before continuing.\",\n {\n phase: \"after-commit\",\n currentHead: remoteHead(verifiedPointer, this.identity, verifiedObject.etag),\n candidateMayHaveBeenActive: true,\n },\n );\n }\n this.registerChecksum(verifiedPointer.snapshot, verifiedPointer.sha256);\n const head = remoteHead(verifiedPointer, this.identity, verifiedObject.etag);\n const warning = await this.updateHistorySafely(commitClient, pointer);\n return { head, warnings: warning ? [warning] : [] };\n }\n\n async listHistory(signal?: AbortSignal): Promise<RemoteHistoryEntry[]> {\n const object = await new S3Client(this.config, signal).getJson<{\n version: number;\n snapshots: LatestPointer[];\n }>(historyKey(this.config));\n throwIfAborted(signal);\n if (object.missing) return [];\n const pointers = requireHistory(object.value, this.config.destination.namespace);\n for (const pointer of pointers) {\n const known = this.checksums.get(pointer.snapshot);\n if (known && known !== pointer.sha256) {\n throw new Error(\"Remote history conflicts with an already observed snapshot checksum.\");\n }\n }\n for (const pointer of pointers) this.registerChecksum(pointer.snapshot, pointer.sha256);\n return pointers.map(remoteHistoryEntry);\n }\n\n async diagnose(signal?: AbortSignal): Promise<BackendDiagnostic[]> {\n throwIfAborted(signal);\n const diagnostics: BackendDiagnostic[] = [\n {\n key: \"s3-config\",\n level: \"info\",\n message: `s3 config: ok (${this.config.destination.bucket}/${storageRoot(this.config)})`,\n },\n ...sessionTokenWarnings(this.config.profile).map((message) => ({\n key: \"s3-session-token\",\n level: \"warning\" as const,\n message,\n })),\n ];\n const deadline = AbortSignal.timeout(10_000);\n const probeSignal = signal ? AbortSignal.any([signal, deadline]) : deadline;\n try {\n const result = await new S3Client(this.config, probeSignal).diagnoseRead(latestKey(this.config));\n throwIfAborted(probeSignal);\n const messages = {\n readable: \"s3 remote read: HTTP success. Snapshot contents and write access are not tested.\",\n \"missing-key\":\n \"s3 remote read: no snapshot at this path (NoSuchKey). Write access is not tested; verify the bucket and path before first sync.\",\n \"missing-bucket\":\n \"s3 remote read: bucket not found (NoSuchBucket). Create the bucket with your provider or edit the setup's bucket name.\",\n \"unknown-404\":\n \"s3 remote read: HTTP 404; cannot confirm whether the bucket or snapshot is missing. Verify the endpoint, bucket, and path with your provider. Write access is not tested.\",\n };\n diagnostics.push({\n key: \"s3-read\",\n level: result === \"readable\" ? \"info\" : \"warning\",\n message: messages[result],\n });\n } catch (error) {\n throwIfAborted(signal);\n diagnostics.push({\n key: \"s3-read\",\n level: \"warning\",\n message: syncErrorGuidance(deadline.aborted ? new Error(\"S3 diagnostic request timed out.\") : error),\n });\n }\n return diagnostics;\n }\n\n private async resolveChecksum(reference: string, signal?: AbortSignal) {\n await this.readHead(signal);\n const headChecksum = this.checksums.get(reference);\n if (headChecksum) return headChecksum;\n await this.listHistory(signal);\n return this.checksums.get(reference);\n }\n\n private registerChecksum(reference: string, checksum: string) {\n const known = this.checksums.get(reference);\n if (known && known !== checksum) {\n throw new Error(\"Remote snapshot reference was rebound to a different checksum.\");\n }\n this.checksums.set(reference, checksum);\n }\n\n private async updateHistorySafely(client: S3Client, pointer: LatestPointer) {\n try {\n await this.updateHistory(client, pointer);\n return undefined;\n } catch (error) {\n return `Remote snapshot is active, but history could not be updated: ${errorMessage(error)}. Run /sync doctor before relying on history.`;\n }\n }\n\n private async updateHistory(client: S3Client, pointer: LatestPointer) {\n const object = await client.getJson<{ version: number; snapshots: LatestPointer[] }>(historyKey(this.config));\n const snapshots = object.missing ? [] : requireHistory(object.value, this.config.destination.namespace);\n const existing = snapshots.find((snapshot) => snapshot.snapshot === pointer.snapshot);\n if (existing) {\n if (!samePointer(existing, pointer)) throw new Error(\"Remote history rebound an immutable snapshot reference.\");\n return;\n }\n const next = [...snapshots, pointer].slice(-100);\n const condition = object.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(object.etag) };\n await client.putJson(historyKey(this.config), { version: VERSION, snapshots: next }, condition);\n }\n}\n\nexport function latestKey(config: ResolvedS3Backend) {\n return posixJoin(storageRoot(config), \"latest.json\");\n}\n\nexport function historyKey(config: ResolvedS3Backend) {\n return posixJoin(storageRoot(config), \"history.json\");\n}\n\nexport function snapshotKey(config: ResolvedS3Backend, id: string) {\n requireSnapshotReference(id);\n return posixJoin(storageRoot(config), \"snapshots\", `${id}.json.gz`);\n}\n\nexport function storageRoot(config: ResolvedS3Backend) {\n return config.destination.prefix === \"./\" ? \"\" : config.destination.prefix;\n}\n\nexport function pointerFor(config: ResolvedS3Backend, snapshot: Snapshot, checksum: string): LatestPointer {\n return {\n version: VERSION,\n profile: config.destination.namespace,\n snapshot: snapshot.id,\n sha256: checksum,\n createdAt: snapshot.createdAt,\n machine: snapshot.machine,\n syncSessions: snapshot.syncSessions === true || snapshot.files.some((file) => file.path.startsWith(\"sessions/\")),\n ...(snapshot.selection === undefined ? {} : { selection: snapshot.selection }),\n };\n}\n\nexport function s3BackendIdentity(config: ResolvedS3Backend) {\n const destination = JSON.stringify([\n secretFreeEndpoint(config.profile.endpoint),\n trimSlashes(config.destination.bucket),\n trimSlashes(config.destination.prefix),\n ]);\n return `s3:${sha256(Buffer.from(destination))}`;\n}\n\nfunction s3Destination(config: ResolvedS3Backend) {\n let host = secretFreeEndpoint(config.profile.endpoint);\n try {\n host = new URL(host).hostname;\n } catch {\n host = \"invalid S3 endpoint\";\n }\n return `${host} \u00B7 ${config.destination.bucket}/${storageRoot(config)}`;\n}\n\nfunction secretFreeEndpoint(value: string) {\n const normalized = value.trim();\n try {\n const url = new URL(normalized);\n url.username = \"\";\n url.password = \"\";\n return url.toString();\n } catch {\n return normalized.replace(/\\/\\/[^/@\\s]+@/u, \"//\");\n }\n}\n\nfunction trimSlashes(value: string) {\n return value.replace(/^\\/+|\\/+$/g, \"\");\n}\n\nfunction remoteHistoryEntry(pointer: LatestPointer): RemoteHistoryEntry {\n return {\n snapshotRef: pointer.snapshot,\n snapshotId: pointer.snapshot,\n createdAt: pointer.createdAt,\n machine: pointer.machine,\n syncSessions: pointer.syncSessions === true,\n };\n}\n\nfunction remoteHead(pointer: LatestPointer, identity: string, etag?: string): RemoteHead {\n return {\n snapshotRef: pointer.snapshot,\n snapshotId: pointer.snapshot,\n revision: `s3:${sha256(Buffer.from(canonicalJson([identity, etag ?? null, pointer])))}`,\n createdAt: pointer.createdAt,\n machine: pointer.machine,\n syncSessions: pointer.syncSessions === true,\n ...(pointer.selection === undefined ? {} : { selection: pointer.selection }),\n };\n}\n\nfunction samePointer(left: LatestPointer, right: LatestPointer) {\n return canonicalJson(left) === canonicalJson(right);\n}\n\nfunction canonicalJson(value: unknown) {\n return JSON.stringify(canonicalValue(value));\n}\n\nfunction canonicalValue(value: unknown): unknown {\n if (Array.isArray(value)) return value.map(canonicalValue);\n if (!value || typeof value !== \"object\") return value;\n return Object.fromEntries(\n Object.keys(value)\n .sort()\n .map((key) => [key, canonicalValue((value as Record<string, unknown>)[key])]),\n );\n}\n\nfunction matchesExpected(head: RemoteHead | undefined, expected: ExpectedRemoteHead) {\n if (expected.kind === \"missing\") return head === undefined;\n return head?.revision === expected.revision;\n}\n\nfunction requirePointer(value: LatestPointer | undefined, message: string, expectedProfile: string) {\n if (\n !value ||\n value.version !== VERSION ||\n value.profile !== expectedProfile ||\n typeof value.snapshot !== \"string\" ||\n !isSafeSnapshotReference(value.snapshot) ||\n typeof value.sha256 !== \"string\" ||\n !/^[0-9a-f]{64}$/.test(value.sha256) ||\n typeof value.createdAt !== \"string\" ||\n !isSafeMetadata(value.createdAt, 64) ||\n typeof value.machine !== \"string\" ||\n !isSafeMetadata(value.machine, 256) ||\n (value.syncSessions !== undefined && typeof value.syncSessions !== \"boolean\")\n ) {\n throw new Error(message);\n }\n if (value.selection !== undefined) portableSnapshotSelection(value.selection);\n return value;\n}\n\nfunction requireHistory(value: { version: number; snapshots: LatestPointer[] } | undefined, expectedProfile: string) {\n if (!value || value.version !== VERSION || !Array.isArray(value.snapshots)) {\n throw new Error(\"Remote history is malformed.\");\n }\n const pointers = value.snapshots.map((pointer) =>\n requirePointer(pointer, \"Remote history entry is malformed.\", expectedProfile),\n );\n const references = new Set<string>();\n for (const pointer of pointers) {\n if (references.has(pointer.snapshot)) {\n throw new Error(\"Remote history contains duplicate snapshot references.\");\n }\n references.add(pointer.snapshot);\n }\n return pointers;\n}\n\nfunction assertSnapshotIdentity(snapshot: Snapshot, expectedProfile: string) {\n validatePortableSnapshot(snapshot);\n if (\n snapshot.profile !== expectedProfile ||\n !isSafeSnapshotReference(snapshot.id) ||\n !isSafeMetadata(snapshot.createdAt, 64) ||\n !isSafeMetadata(snapshot.machine, 256) ||\n !Array.isArray(snapshot.files)\n ) {\n throw new Error(\"Invalid snapshot identity for S3 publication.\");\n }\n}\n\nfunction validateSnapshotBundle(snapshot: Snapshot) {\n const paths = new Set<string>();\n for (const file of snapshot.files) {\n if (\n !file ||\n typeof file.path !== \"string\" ||\n typeof file.contentBase64 !== \"string\" ||\n typeof file.sha256 !== \"string\" ||\n !/^[0-9a-f]{64}$/.test(file.sha256) ||\n paths.has(file.path)\n ) {\n throw new Error(\"Remote snapshot bundle is malformed.\");\n }\n const content = Buffer.from(file.contentBase64, \"base64\");\n if (content.toString(\"base64\") !== file.contentBase64 || sha256(content) !== file.sha256) {\n throw new Error(\"Remote snapshot file checksum mismatch.\");\n }\n paths.add(file.path);\n }\n}\n\nfunction assertSafeDestination(config: ResolvedS3Backend) {\n for (const [label, value, allowEmpty] of [\n [\"bucket\", config.destination.bucket, false],\n [\"prefix\", config.destination.prefix, true],\n [\"namespace\", config.destination.namespace, false],\n ] as const) {\n if (label === \"prefix\" && value === \"./\") continue;\n if (\n (!allowEmpty && value.length === 0) ||\n value.includes(\"\\\\\") ||\n hasControlCharacter(value) ||\n value.split(\"/\").some((segment) => segment === \".\" || segment === \"..\")\n ) {\n throw new Error(`Invalid S3 storage location ${label}.`);\n }\n }\n}\n\nfunction requireSnapshotReference(reference: string) {\n if (!isSafeSnapshotReference(reference)) {\n throw new Error(\"Invalid S3 snapshot reference.\");\n }\n}\n\nfunction isSafeSnapshotReference(reference: string) {\n return (\n reference.length > 0 &&\n reference.length <= 512 &&\n reference !== \".\" &&\n reference !== \"..\" &&\n !reference.includes(\"/\") &&\n !reference.includes(\"\\\\\") &&\n !hasControlCharacter(reference)\n );\n}\n\nfunction isSafeMetadata(value: string, maxLength: number) {\n return value.length > 0 && value.length <= maxLength && !hasControlCharacter(value);\n}\n\nfunction hasControlCharacter(value: string) {\n return [...value].some((character) => {\n const code = character.codePointAt(0) ?? 0;\n return code < 0x20 || code === 0x7f;\n });\n}\n\nfunction throwIfAborted(signal?: AbortSignal) {\n if (!signal?.aborted) return;\n throw signal.reason instanceof Error ? signal.reason : new DOMException(\"The operation was aborted\", \"AbortError\");\n}\n\nfunction sha256(value: Buffer) {\n return createHash(\"sha256\").update(value).digest(\"hex\");\n}\n\nfunction errorMessage(error: unknown) {\n return error instanceof Error ? error.message : String(error);\n}\n\nexport { encodeKey };\n", "import { createHash, createHmac } from \"node:crypto\";\nimport { setTimeout as sleep } from \"node:timers/promises\";\nimport { encodeKey, posixJoin } from \"../../paths.js\";\nimport type { RemoteObject, ResolvedS3Backend } from \"../backend-types.js\";\n\nconst MAX_JSON_RESPONSE_BYTES = 1024 * 1024;\nconst MAX_SNAPSHOT_RESPONSE_BYTES = 256 * 1024 * 1024;\nconst MAX_ERROR_RESPONSE_BYTES = 64 * 1024;\nconst DEFAULT_REQUEST_TIMEOUT_MS = 30_000;\n\nfunction iso8601Basic(date: Date) {\n return date.toISOString().replace(/[:-]|\\.\\d{3}/g, \"\");\n}\n\nfunction isCloudflareR2Endpoint(endpoint: string | undefined) {\n const value = endpoint?.trim();\n if (!value) return false;\n try {\n const hostname = new URL(value).hostname.toLowerCase();\n return hostname === \"r2.cloudflarestorage.com\" || hostname.endsWith(\".r2.cloudflarestorage.com\");\n } catch {\n return false;\n }\n}\n\nexport class S3ObjectAlreadyExistsError extends Error {\n constructor(readonly key: string) {\n super(`S3 object already exists: ${key}`);\n this.name = \"S3ObjectAlreadyExistsError\";\n }\n}\n\nexport class S3HttpError extends Error {\n constructor(\n message: string,\n readonly status: number,\n ) {\n super(message);\n this.name = \"S3HttpError\";\n }\n}\n\nexport class S3Client {\n private config: ResolvedS3Backend;\n private endpoint: URL;\n private signal?: AbortSignal;\n private requestTimeoutMs: number;\n private omitSessionTokenAfterRejection = false;\n\n constructor(config: ResolvedS3Backend, signal?: AbortSignal, requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {\n this.config = config;\n this.endpoint = new URL(config.profile.endpoint);\n this.signal = signal;\n this.requestTimeoutMs = requestTimeoutMs;\n }\n\n /** Read-only access probe. A bare 404 cannot distinguish a missing key from a missing bucket. */\n async diagnoseRead(key: string): Promise<\"readable\" | \"missing-key\" | \"missing-bucket\" | \"unknown-404\"> {\n this.signal?.throwIfAborted();\n const response = await this.request(\"GET\", key);\n if (response.ok) {\n await response.body?.cancel();\n this.signal?.throwIfAborted();\n return \"readable\";\n }\n const detail = await this.readErrorText(response);\n this.signal?.throwIfAborted();\n if (response.status === 404) {\n const code = /<Code>\\s*([A-Za-z]+)\\s*<\\/Code>/u.exec(detail)?.[1];\n if (code === \"NoSuchBucket\") return \"missing-bucket\";\n if (code === \"NoSuchKey\") return \"missing-key\";\n return \"unknown-404\";\n }\n throw new S3HttpError(`S3 remote read failed (HTTP ${response.status}).`, response.status);\n }\n\n async getJson<T>(key: string): Promise<RemoteObject<T>> {\n const maxAttempts = 3;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const object = await this.request(\"GET\", key);\n if (object.status === 404) return { missing: true };\n if (!object.ok) {\n throw new S3HttpError(`S3 GET failed (${object.status}): ${await this.readErrorText(object)}`, object.status);\n }\n const body = await readBoundedText(object, MAX_JSON_RESPONSE_BYTES, \"S3 JSON response\");\n // R2 can intermittently return an empty 200 body (read-after-write\n // inconsistency on a long-lived keep-alive connection), which makes\n // response.json() throw \"JSON Parse error: Unexpected EOF\" under Bun.\n // Retry so the transient blip is absorbed instead of surfacing as a\n // \"pi-sync auto sync skipped\" warning on every session start.\n if (body.length > 0) {\n return {\n value: JSON.parse(body) as T,\n etag: normalizeEtag(object.headers.get(\"etag\")),\n missing: false,\n };\n }\n lastError = new Error(`S3 GET returned an empty body for ${key}`);\n if (attempt < maxAttempts) {\n await sleep(250 * attempt, undefined, { signal: this.signal });\n }\n }\n throw lastError;\n }\n\n async getBuffer(key: string): Promise<RemoteObject<Buffer>> {\n const maxAttempts = 3;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const object = await this.request(\"GET\", key);\n if (object.status === 404) return { missing: true };\n if (!object.ok) {\n throw new S3HttpError(`S3 GET failed (${object.status}): ${await this.readErrorText(object)}`, object.status);\n }\n const buffer = await readBoundedBuffer(object, MAX_SNAPSHOT_RESPONSE_BYTES, \"S3 snapshot response\");\n // R2 can intermittently return an empty 200 body on a long-lived\n // keep-alive connection (same root cause as the getJson retry\n // above). getBuffer is only used for snapshot .json.gz payloads,\n // which are always non-empty, so an empty body is a transient\n // blip, not a legitimate response. Retry so the checksum guard\n // downstream doesn't surface it as \"Remote snapshot checksum\n // mismatch\".\n if (buffer.length > 0) {\n return { value: buffer, etag: normalizeEtag(object.headers.get(\"etag\")), missing: false };\n }\n lastError = new Error(`S3 GET returned an empty body for ${key}`);\n if (attempt < maxAttempts) {\n await sleep(250 * attempt, undefined, { signal: this.signal });\n }\n }\n throw lastError;\n }\n\n async putJson(key: string, value: unknown, options: { ifAbsent?: boolean; ifMatch?: string } = {}) {\n const body = Buffer.from(JSON.stringify(value, null, \"\\t\"), \"utf8\");\n await this.putBuffer(key, body, \"application/json\", options);\n }\n\n async putBuffer(\n key: string,\n body: Buffer,\n contentType: string,\n options: { ifAbsent?: boolean; ifMatch?: string } = {},\n ) {\n const headers: Record<string, string> = { \"content-type\": contentType };\n if (options.ifAbsent) headers[\"if-none-match\"] = \"*\";\n if (options.ifMatch) headers[\"if-match\"] = options.ifMatch;\n const response = await this.request(\"PUT\", key, body, headers);\n if ((options.ifAbsent || options.ifMatch) && (response.status === 412 || response.status === 409)) {\n await response.body?.cancel();\n if (options.ifAbsent && response.status === 412) throw new S3ObjectAlreadyExistsError(key);\n if (options.ifAbsent || options.ifMatch) {\n throw new S3HttpError(`S3 conditional PUT rejected (${response.status}).`, response.status);\n }\n }\n if (!response.ok) {\n throw new S3HttpError(\n `S3 PUT failed (${response.status}): ${await this.readErrorText(response)}`,\n response.status,\n );\n }\n await response.body?.cancel();\n }\n\n async delete(key: string) {\n const response = await this.request(\"DELETE\", key);\n if (!response.ok && response.status !== 404) {\n throw new S3HttpError(\n `S3 DELETE failed (${response.status}): ${await this.readErrorText(response)}`,\n response.status,\n );\n }\n await response.body?.cancel();\n }\n\n private async request(\n method: \"GET\" | \"PUT\" | \"DELETE\",\n key: string,\n body?: Buffer,\n extraHeaders: Record<string, string> = {},\n ) {\n const url = new URL(this.endpoint.toString());\n url.pathname = posixJoin(url.pathname, this.config.destination.bucket, encodeKey(key));\n const send = async (sessionToken: string | undefined) => {\n const transportSignal = this.transportSignal();\n const headers = await signedHeaders({\n method,\n url,\n body,\n extraHeaders,\n accessKeyId: this.config.profile.accessKeyId,\n secretAccessKey: this.config.profile.secretAccessKey,\n sessionToken,\n region: this.config.profile.region,\n });\n try {\n return await fetch(url, {\n method,\n headers,\n body: body ? new Uint8Array(body) : undefined,\n signal: transportSignal,\n });\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") throw error;\n if (error instanceof Error && error.name === \"TimeoutError\") {\n throw new Error(\"S3 request timed out.\", { cause: error });\n }\n throw new Error(`S3 request failed: ${this.redact(errorMessage(error))}`, { cause: error });\n }\n };\n const sessionToken = this.omitSessionTokenAfterRejection ? undefined : this.config.profile.sessionToken;\n const response = await send(sessionToken);\n try {\n if (!(await this.shouldRetryWithoutSessionToken(response, sessionToken))) return response;\n } catch (error) {\n await response.body?.cancel().catch(() => undefined);\n throw error;\n }\n await response.body?.cancel();\n const retry = await send(undefined);\n if (retry.ok || retry.status === 404) this.omitSessionTokenAfterRejection = true;\n return retry;\n }\n\n private transportSignal() {\n const timeout = AbortSignal.timeout(this.requestTimeoutMs);\n return this.signal ? AbortSignal.any([this.signal, timeout]) : timeout;\n }\n\n private async shouldRetryWithoutSessionToken(response: Response, sessionToken: string | undefined) {\n if (\n !sessionToken ||\n !isCloudflareR2Endpoint(this.config.profile.endpoint) ||\n response.ok ||\n response.status !== 400\n ) {\n return false;\n }\n return isSecurityTokenInvalidArgument(\n await readBoundedText(response.clone(), MAX_ERROR_RESPONSE_BYTES, \"S3 error response\"),\n );\n }\n\n private async readErrorText(response: Response) {\n try {\n return this.redact(await readBoundedText(response, MAX_ERROR_RESPONSE_BYTES, \"S3 error response\"));\n } catch (error) {\n return this.redact(errorMessage(error));\n }\n }\n\n private redact(value: string) {\n let redacted = value;\n let endpointUsername: string | undefined;\n let endpointPassword: string | undefined;\n let endpointQueryValues: string[] = [];\n try {\n const endpoint = new URL(this.config.profile.endpoint);\n endpointUsername = endpoint.username;\n endpointPassword = endpoint.password;\n endpointQueryValues = [...endpoint.searchParams.values()];\n } catch {\n // The constructor already validates normal runtime endpoints.\n }\n for (const secret of [\n this.config.profile.accessKeyId,\n this.config.profile.secretAccessKey,\n this.config.profile.sessionToken,\n endpointUsername,\n endpointPassword,\n ...endpointQueryValues,\n ]) {\n if (secret) redacted = redacted.replaceAll(secret, \"[REDACTED]\");\n }\n return redacted;\n }\n}\n\nasync function readBoundedText(response: Response, limit: number, label: string) {\n return (await readBoundedBuffer(response, limit, label)).toString(\"utf8\");\n}\n\nasync function readBoundedBuffer(response: Response, limit: number, label: string) {\n const contentLength = Number(response.headers.get(\"content-length\"));\n if (Number.isFinite(contentLength) && contentLength > limit) {\n // Do not await cancellation of a cloned stream before its sibling can be released.\n void response.body?.cancel().catch(() => undefined);\n throw new Error(`${label} exceeds the ${limit}-byte limit.`);\n }\n if (!response.body) return Buffer.alloc(0);\n const reader = response.body.getReader();\n const chunks: Buffer[] = [];\n let total = 0;\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n total += value.byteLength;\n if (total > limit) {\n void reader.cancel().catch(() => undefined);\n throw new Error(`${label} exceeds the ${limit}-byte limit.`);\n }\n chunks.push(Buffer.from(value));\n }\n } finally {\n reader.releaseLock();\n }\n return Buffer.concat(chunks, total);\n}\n\nfunction errorMessage(error: unknown) {\n return error instanceof Error ? error.message : String(error);\n}\n\nasync function signedHeaders(input: {\n method: string;\n url: URL;\n body?: Buffer;\n extraHeaders: Record<string, string>;\n accessKeyId: string;\n secretAccessKey: string;\n sessionToken?: string;\n region: string;\n}) {\n const now = new Date();\n const amzDate = iso8601Basic(now);\n const dateStamp = amzDate.slice(0, 8);\n const payloadHash = sha256(input.body ?? Buffer.alloc(0));\n const headers: Record<string, string> = {\n ...lowercaseKeys(input.extraHeaders),\n host: input.url.host,\n \"x-amz-content-sha256\": payloadHash,\n \"x-amz-date\": amzDate,\n };\n if (input.sessionToken) headers[\"x-amz-security-token\"] = input.sessionToken;\n const signedHeaderNames = Object.keys(headers).sort();\n const canonicalHeaders = signedHeaderNames.map((name) => `${name}:${headers[name]?.trim()}\\n`).join(\"\");\n const canonicalRequest = [\n input.method,\n input.url.pathname,\n input.url.searchParams.toString(),\n canonicalHeaders,\n signedHeaderNames.join(\";\"),\n payloadHash,\n ].join(\"\\n\");\n const scope = `${dateStamp}/${input.region}/s3/aws4_request`;\n const stringToSign = [\"AWS4-HMAC-SHA256\", amzDate, scope, sha256(Buffer.from(canonicalRequest))].join(\"\\n\");\n const signingKey = hmac(\n hmac(hmac(hmac(Buffer.from(`AWS4${input.secretAccessKey}`), dateStamp), input.region), \"s3\"),\n \"aws4_request\",\n );\n const signature = createHmac(\"sha256\", signingKey).update(stringToSign).digest(\"hex\");\n return {\n ...headers,\n authorization: `AWS4-HMAC-SHA256 Credential=${input.accessKeyId}/${scope}, SignedHeaders=${signedHeaderNames.join(\";\")}, Signature=${signature}`,\n };\n}\n\nfunction hmac(key: Buffer, value: string) {\n return createHmac(\"sha256\", key).update(value).digest();\n}\n\nfunction sha256(value: Buffer) {\n return createHash(\"sha256\").update(value).digest(\"hex\");\n}\n\nfunction lowercaseKeys(value: Record<string, string>) {\n return Object.fromEntries(Object.entries(value).map(([key, item]) => [key.toLowerCase(), item]));\n}\n\nfunction normalizeEtag(value: string | null) {\n return value ?? undefined;\n}\n\nfunction isSecurityTokenInvalidArgument(text: string) {\n return text.includes(\"<Code>InvalidArgument</Code>\") && text.includes(\"<Message>X-Amz-Security-Token</Message>\");\n}\n", "import { randomUUID } from \"node:crypto\";\nimport { posixJoin } from \"../../paths.js\";\nimport type { ResolvedS3Backend } from \"../backend-types.js\";\nimport { S3Client, S3HttpError, S3ObjectAlreadyExistsError } from \"./s3-client.js\";\n\nexport function requireS3Etag(value: string | undefined) {\n if (!value || !/^\"[^\"\\r\\n]+\"$/u.test(value)) throw new Error(\"S3 conditional publication requires a strong ETag.\");\n return value;\n}\n\n/** Never probe the active pointer or immutable snapshots; ignored conditions must not corrupt either. */\nexport async function verifyS3Conditions(config: ResolvedS3Backend, signal?: AbortSignal) {\n const key = posixJoin(\n config.destination.prefix === \"./\" ? \"\" : config.destination.prefix,\n \".pi-sync-probes\",\n randomUUID(),\n );\n const client = new S3Client(config, signal);\n let failure: unknown;\n let attempted = false;\n try {\n signal?.throwIfAborted();\n attempted = true;\n await client.putBuffer(key, Buffer.from(\"first\"), \"text/plain\", { ifAbsent: true });\n const first = await client.getBuffer(key);\n const etag = requireS3Etag(first.etag);\n if (!first.value?.equals(Buffer.from(\"first\"))) throw new Error(\"S3 conditional probe read did not match.\");\n await expectRejected(client.putBuffer(key, Buffer.from(\"ignored\"), \"text/plain\", { ifAbsent: true }));\n await expectRejected(client.putBuffer(key, Buffer.from(\"ignored\"), \"text/plain\", { ifMatch: '\"pi-sync-stale\"' }));\n const unchanged = await client.getBuffer(key);\n if (!unchanged.value?.equals(first.value) || unchanged.etag !== etag)\n throw new Error(\"S3 failed precondition changed the probe.\");\n await client.putBuffer(key, Buffer.from(\"second\"), \"text/plain\", { ifMatch: etag });\n const second = await client.getBuffer(key);\n if (!second.value?.equals(Buffer.from(\"second\")) || requireS3Etag(second.etag) === etag) {\n throw new Error(\"S3 conditional write did not rotate the probe ETag.\");\n }\n await expectRejected(client.putBuffer(key, Buffer.from(\"ignored\"), \"text/plain\", { ifMatch: etag }));\n const final = await client.getBuffer(key);\n if (!final.value?.equals(second.value) || final.etag !== second.etag)\n throw new Error(\"S3 stale ETag changed the probe.\");\n signal?.throwIfAborted();\n } catch (error) {\n failure = error;\n }\n // Cleanup has its own bounded deadline even when the owning session was cancelled.\n if (attempted) {\n try {\n await new S3Client(config, AbortSignal.timeout(10_000)).delete(key);\n } catch (error) {\n throw new Error(`S3 probe cleanup failed; remove ${key}.`, { cause: failure ?? error });\n }\n }\n if (failure) throw failure;\n signal?.throwIfAborted();\n}\n\nasync function expectRejected(operation: Promise<void>) {\n try {\n await operation;\n } catch (error) {\n // 409 is retryable contention, not proof that a failed condition was enforced.\n if (error instanceof S3ObjectAlreadyExistsError || (error instanceof S3HttpError && error.status === 412)) return;\n throw error;\n }\n throw new Error(\"S3 server ignored a write precondition; publication stopped for safety.\");\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,cAAAA,mBAAkB;;;ACA3B,SAAS,YAAY,kBAAkB;AACvC,SAAS,cAAc,aAAa;AAIpC,IAAM,0BAA0B,OAAO;AACvC,IAAM,8BAA8B,MAAM,OAAO;A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,SACE,UAAU,SAAS,KACnB,UAAU,UAAU,OACpB,cAAc,OACd,cAAc,QACd,CAAC,UAAU,SAAS,GAAG,KACvB,CAAC,UAAU,SAAS,IAAI,KACxB,CAAC,oBAAoB,SAAS;AAElC;AAEA,SAAS,eAAe,OAAe,WAAmB;AACxD,SAAO,MAAM,SAAS,KAAK,MAAM,UAAU,aAAa,CAAC,oBAAoB,KAAK;AACpF;AAEA,SAAS,oBAAoB,OAAe;AAC1C,SAAO,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,cAAc;AACpC,UAAM,OAAO,UAAU,YAAY,CAAC,KAAK;AACzC,WAAO,OAAO,MAAQ,SAAS;AAAA,EACjC,CAAC;AACH;AAEA,SAAS,eAAe,QAAsB;AAC5C,MAAI,CAAC,QAAQ,QAAS;AACtB,QAAM,OAAO,kBAAkB,QAAQ,OAAO,SAAS,IAAI,aAAa,6BAA6B,YAAY;AACnH;AAEA,SAASA,QAAO,OAAe;AAC7B,SAAOE,YAAW,QAAQ,EAAE,OAAO,KAAK,EAAE,OAAO,KAAK;AACxD;AAEA,SAASD,cAAa,OAAgB;AACpC,SAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAC9D;",
6
+ "names": ["createHash", "sessionToken", "sha256", "errorMessage", "createHash"]
7
+ }
@@ -2,9 +2,9 @@
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
4
  inspectSync
5
- } from "./chunk-7D6RDCVC.ts";
5
+ } from "./chunk-BX3K7YW7.ts";
6
6
  import "./chunk-BDB4IDM3.ts";
7
- import "./chunk-QHSQCK7L.ts";
7
+ import "./chunk-FHNISD7A.ts";
8
8
  import "./chunk-PWMWOJP4.ts";
9
9
  import "./chunk-4QVPTIZB.ts";
10
10
  import "./chunk-BVVP33U5.ts";
@@ -17,4 +17,4 @@ import "./chunk-HUZ5CR4W.ts";
17
17
  export {
18
18
  inspectSync
19
19
  };
20
- //# sourceMappingURL=sync-inspection-VBJF2FWC.ts.map
20
+ //# sourceMappingURL=sync-inspection-RLIHUYDV.ts.map
@@ -2,7 +2,7 @@
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
4
  inspectSync
5
- } from "./chunk-7D6RDCVC.ts";
5
+ } from "./chunk-BX3K7YW7.ts";
6
6
  import {
7
7
  applySnapshot,
8
8
  countPreservedRemoteFiles,
@@ -14,7 +14,7 @@ import {
14
14
  formatSnapshotOnlyDiff,
15
15
  mergeSync,
16
16
  publicationCapabilityDescription
17
- } from "./chunk-2GUXI2XT.ts";
17
+ } from "./chunk-QTCGWQP3.ts";
18
18
  import {
19
19
  readMergeJournal,
20
20
  requireNoMergeJournal,
@@ -41,7 +41,7 @@ import {
41
41
  } from "./chunk-V6VZ5TLG.ts";
42
42
  import {
43
43
  createSyncBackend
44
- } from "./chunk-QHSQCK7L.ts";
44
+ } from "./chunk-FHNISD7A.ts";
45
45
  import "./chunk-ANWNQIR7.ts";
46
46
  import {
47
47
  backupLocal,
@@ -876,4 +876,4 @@ export {
876
876
  status,
877
877
  syncBoth
878
878
  };
879
- //# sourceMappingURL=sync-operations-27VQUDH2.ts.map
879
+ //# sourceMappingURL=sync-operations-RBUOUJWL.ts.map
package/dist/index.ts CHANGED
@@ -176,7 +176,7 @@ async function executeCommand(rawArgs, ctx, signal, loaders, onCommit, setup) {
176
176
  return { kind: "completed" };
177
177
  }
178
178
  case "conflicts": {
179
- const { showConflicts } = await import("./chunks/conflict-review-T3PB7ZSD.ts");
179
+ const { showConflicts } = await import("./chunks/conflict-review-NK5CM6UA.ts");
180
180
  throwIfAborted(options.signal);
181
181
  await withLock("sync", () => showConflicts(ctx, options));
182
182
  return { kind: "completed" };
@@ -376,7 +376,7 @@ async function handleCommand(rawArgs, ctx, sessionSignal, loaders, attention) {
376
376
  );
377
377
  if (!rawArgs.trim()) {
378
378
  try {
379
- const { showSyncManager } = await import("./chunks/manager-ui-HSMHS3OK.ts");
379
+ const { showSyncManager } = await import("./chunks/manager-ui-6AYMNXUP.ts");
380
380
  if (sessionSignal.aborted) return;
381
381
  await showSyncManager(ctx, run, sessionSignal, {
382
382
  getAttention: () => attention.current(),
@@ -476,7 +476,7 @@ function directSelectionOrigin(rawArgs) {
476
476
  async function resolveSelectionAttention(ctx, attention, signal, loaders, options = {}) {
477
477
  const current = attention.current();
478
478
  if (!current || signal.aborted) return;
479
- const { dispatchManagerResult } = await import("./chunks/manager-result-dispatcher-VIDT3QBU.ts");
479
+ const { dispatchManagerResult } = await import("./chunks/manager-result-dispatcher-35TM3ZR3.ts");
480
480
  if (signal.aborted || attention.current() !== current) return;
481
481
  await dispatchManagerResult(
482
482
  ctx,
@@ -755,11 +755,11 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4) {
755
755
  // src/sync/sync-loaders.ts
756
756
  function createSyncLoaders(dependencies) {
757
757
  const loaders = {
758
- inspection: cachedModuleLoader(dependencies.loadSyncInspection ?? (() => import("./chunks/sync-inspection-VBJF2FWC.ts"))),
758
+ inspection: cachedModuleLoader(dependencies.loadSyncInspection ?? (() => import("./chunks/sync-inspection-RLIHUYDV.ts"))),
759
759
  setupSwitch: cachedModuleLoader(dependencies.loadSetupSwitch ?? (() => import("./chunks/setup-switch-KTUWL6DE.ts"))),
760
760
  snapshot: cachedModuleLoader(dependencies.loadSnapshot ?? (() => import("./chunks/snapshot-THS5EY2L.ts"))),
761
761
  syncState: cachedModuleLoader(dependencies.loadSyncState ?? (() => import("./chunks/sync-state-CFL4RRRB.ts"))),
762
- operations: cachedModuleLoader(dependencies.loadSyncOperations ?? (() => import("./chunks/sync-operations-27VQUDH2.ts")))
762
+ operations: cachedModuleLoader(dependencies.loadSyncOperations ?? (() => import("./chunks/sync-operations-RBUOUJWL.ts")))
763
763
  };
764
764
  return loaders;
765
765
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-sync",
3
- "version": "0.52.0",
3
+ "version": "0.53.0",
4
4
  "description": "Pi extension that syncs Pi configuration through Git, WebDAV, Cloudflare R2, or S3-compatible storage.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,7 +18,8 @@ import {
18
18
  SyncBackendConflictError,
19
19
  SyncBackendPublicationOutcomeUnknownError,
20
20
  } from "../sync-backend.js";
21
- import { S3Client, S3ObjectAlreadyExistsError } from "./s3-client.js";
21
+ import { S3Client, S3HttpError, S3ObjectAlreadyExistsError } from "./s3-client.js";
22
+ import { requireS3Etag, verifyS3Conditions } from "./s3-conditions.js";
22
23
 
23
24
  const VERSION = 1;
24
25
  const POST_COMMIT_TIMEOUT_MS = 30_000;
@@ -26,7 +27,7 @@ const POST_COMMIT_TIMEOUT_MS = 30_000;
26
27
  export class S3SyncBackend implements SyncBackend {
27
28
  readonly identity: string;
28
29
  readonly destination: string;
29
- readonly capability = "read-check-write-verify" as const;
30
+ readonly capability = "conditional-required" as const;
30
31
  private readonly checksums = new Map<string, string>();
31
32
 
32
33
  constructor(
@@ -78,6 +79,8 @@ export class S3SyncBackend implements SyncBackend {
78
79
  ): Promise<PublishSnapshotResult> {
79
80
  throwIfAborted(options.signal);
80
81
  assertSnapshotIdentity(snapshot, this.config.destination.namespace);
82
+ await verifyS3Conditions(this.config, options.signal);
83
+ throwIfAborted(options.signal);
81
84
  const stagedKey = snapshotKey(this.config, snapshot.id);
82
85
  const encoded = await encodeSnapshot(snapshot);
83
86
  throwIfAborted(options.signal);
@@ -113,6 +116,7 @@ export class S3SyncBackend implements SyncBackend {
113
116
  currentHead: current,
114
117
  });
115
118
  }
119
+ const condition = currentObject.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(currentObject.etag) };
116
120
  throwIfAborted(options.signal);
117
121
  options.onCommit?.();
118
122
 
@@ -120,8 +124,16 @@ export class S3SyncBackend implements SyncBackend {
120
124
  // verification to a user-cancellation signal after the boundary begins.
121
125
  const commitClient = new S3Client(this.config, AbortSignal.timeout(this.postCommitTimeoutMs));
122
126
  try {
123
- await commitClient.putJson(latestKey(this.config), pointer);
127
+ await commitClient.putJson(latestKey(this.config), pointer, condition);
124
128
  } catch (error) {
129
+ if (
130
+ error instanceof S3ObjectAlreadyExistsError ||
131
+ (error instanceof S3HttpError && (error.status === 412 || error.status === 409))
132
+ ) {
133
+ throw new SyncBackendConflictError("Remote conditional publication was rejected; reread and retry.", {
134
+ cause: error,
135
+ });
136
+ }
125
137
  throw new SyncBackendPublicationOutcomeUnknownError(
126
138
  `Remote publication outcome is unknown: ${errorMessage(error)}`,
127
139
  { cause: error },
@@ -256,8 +268,14 @@ export class S3SyncBackend implements SyncBackend {
256
268
  private async updateHistory(client: S3Client, pointer: LatestPointer) {
257
269
  const object = await client.getJson<{ version: number; snapshots: LatestPointer[] }>(historyKey(this.config));
258
270
  const snapshots = object.missing ? [] : requireHistory(object.value, this.config.destination.namespace);
259
- const next = [...snapshots.filter((snapshot) => snapshot.snapshot !== pointer.snapshot), pointer].slice(-100);
260
- await client.putJson(historyKey(this.config), { version: VERSION, snapshots: next });
271
+ const existing = snapshots.find((snapshot) => snapshot.snapshot === pointer.snapshot);
272
+ if (existing) {
273
+ if (!samePointer(existing, pointer)) throw new Error("Remote history rebound an immutable snapshot reference.");
274
+ return;
275
+ }
276
+ const next = [...snapshots, pointer].slice(-100);
277
+ const condition = object.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(object.etag) };
278
+ await client.putJson(historyKey(this.config), { version: VERSION, snapshots: next }, condition);
261
279
  }
262
280
  }
263
281
 
@@ -132,17 +132,27 @@ export class S3Client {
132
132
  throw lastError;
133
133
  }
134
134
 
135
- async putJson(key: string, value: unknown) {
135
+ async putJson(key: string, value: unknown, options: { ifAbsent?: boolean; ifMatch?: string } = {}) {
136
136
  const body = Buffer.from(JSON.stringify(value, null, "\t"), "utf8");
137
- await this.putBuffer(key, body, "application/json");
137
+ await this.putBuffer(key, body, "application/json", options);
138
138
  }
139
139
 
140
- async putBuffer(key: string, body: Buffer, contentType: string, options: { ifAbsent?: boolean } = {}) {
140
+ async putBuffer(
141
+ key: string,
142
+ body: Buffer,
143
+ contentType: string,
144
+ options: { ifAbsent?: boolean; ifMatch?: string } = {},
145
+ ) {
141
146
  const headers: Record<string, string> = { "content-type": contentType };
142
147
  if (options.ifAbsent) headers["if-none-match"] = "*";
148
+ if (options.ifMatch) headers["if-match"] = options.ifMatch;
143
149
  const response = await this.request("PUT", key, body, headers);
144
- if (options.ifAbsent && response.status === 412) {
145
- throw new S3ObjectAlreadyExistsError(key);
150
+ if ((options.ifAbsent || options.ifMatch) && (response.status === 412 || response.status === 409)) {
151
+ await response.body?.cancel();
152
+ if (options.ifAbsent && response.status === 412) throw new S3ObjectAlreadyExistsError(key);
153
+ if (options.ifAbsent || options.ifMatch) {
154
+ throw new S3HttpError(`S3 conditional PUT rejected (${response.status}).`, response.status);
155
+ }
146
156
  }
147
157
  if (!response.ok) {
148
158
  throw new S3HttpError(
@@ -150,9 +160,26 @@ export class S3Client {
150
160
  response.status,
151
161
  );
152
162
  }
163
+ await response.body?.cancel();
164
+ }
165
+
166
+ async delete(key: string) {
167
+ const response = await this.request("DELETE", key);
168
+ if (!response.ok && response.status !== 404) {
169
+ throw new S3HttpError(
170
+ `S3 DELETE failed (${response.status}): ${await this.readErrorText(response)}`,
171
+ response.status,
172
+ );
173
+ }
174
+ await response.body?.cancel();
153
175
  }
154
176
 
155
- private async request(method: "GET" | "PUT", key: string, body?: Buffer, extraHeaders: Record<string, string> = {}) {
177
+ private async request(
178
+ method: "GET" | "PUT" | "DELETE",
179
+ key: string,
180
+ body?: Buffer,
181
+ extraHeaders: Record<string, string> = {},
182
+ ) {
156
183
  const url = new URL(this.endpoint.toString());
157
184
  url.pathname = posixJoin(url.pathname, this.config.destination.bucket, encodeKey(key));
158
185
  const send = async (sessionToken: string | undefined) => {