@indigoai-us/hq-cloud 6.18.1-alpha.6 → 6.18.1

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 (75) hide show
  1. package/dist/bandwidth.d.ts +11 -45
  2. package/dist/bandwidth.d.ts.map +1 -1
  3. package/dist/bandwidth.js +18 -231
  4. package/dist/bandwidth.js.map +1 -1
  5. package/dist/bandwidth.test.js +4 -124
  6. package/dist/bandwidth.test.js.map +1 -1
  7. package/dist/bin/sync-runner-company.d.ts +0 -2
  8. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  9. package/dist/bin/sync-runner-company.js +0 -6
  10. package/dist/bin/sync-runner-company.js.map +1 -1
  11. package/dist/bin/sync-runner-manifest.d.ts +2 -0
  12. package/dist/bin/sync-runner-manifest.d.ts.map +1 -1
  13. package/dist/bin/sync-runner-manifest.js +439 -1
  14. package/dist/bin/sync-runner-manifest.js.map +1 -1
  15. package/dist/bin/sync-runner-manifest.test.js +374 -16
  16. package/dist/bin/sync-runner-manifest.test.js.map +1 -1
  17. package/dist/bin/sync-runner-watch-loop.d.ts +1 -26
  18. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  19. package/dist/bin/sync-runner-watch-loop.js +3 -217
  20. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  21. package/dist/bin/sync-runner.d.ts +2 -26
  22. package/dist/bin/sync-runner.d.ts.map +1 -1
  23. package/dist/bin/sync-runner.js +5 -14
  24. package/dist/bin/sync-runner.js.map +1 -1
  25. package/dist/bin/sync-runner.test.js +1 -107
  26. package/dist/bin/sync-runner.test.js.map +1 -1
  27. package/dist/cli/share.d.ts +6 -18
  28. package/dist/cli/share.d.ts.map +1 -1
  29. package/dist/cli/share.js +47 -92
  30. package/dist/cli/share.js.map +1 -1
  31. package/dist/cli/share.test.js +3 -3
  32. package/dist/cli/share.test.js.map +1 -1
  33. package/dist/cli/sync.d.ts +4 -17
  34. package/dist/cli/sync.d.ts.map +1 -1
  35. package/dist/cli/sync.js +27 -155
  36. package/dist/cli/sync.js.map +1 -1
  37. package/dist/cli/sync.test.js +1 -315
  38. package/dist/cli/sync.test.js.map +1 -1
  39. package/dist/manifest/upload-manifest.d.ts +6 -0
  40. package/dist/manifest/upload-manifest.d.ts.map +1 -1
  41. package/dist/manifest/upload-manifest.js +18 -3
  42. package/dist/manifest/upload-manifest.js.map +1 -1
  43. package/dist/object-io.d.ts +7 -33
  44. package/dist/object-io.d.ts.map +1 -1
  45. package/dist/object-io.js +140 -326
  46. package/dist/object-io.js.map +1 -1
  47. package/dist/object-io.test.js +1 -377
  48. package/dist/object-io.test.js.map +1 -1
  49. package/dist/s3.d.ts +3 -8
  50. package/dist/s3.d.ts.map +1 -1
  51. package/dist/s3.js +27 -304
  52. package/dist/s3.js.map +1 -1
  53. package/dist/s3.test.js +0 -163
  54. package/dist/s3.test.js.map +1 -1
  55. package/dist/sync/inbound-event-admission.d.ts.map +1 -1
  56. package/dist/sync/inbound-event-admission.js +0 -3
  57. package/dist/sync/inbound-event-admission.js.map +1 -1
  58. package/dist/sync/inbound-event-admission.test.js +0 -6
  59. package/dist/sync/inbound-event-admission.test.js.map +1 -1
  60. package/dist/sync/push-receiver.d.ts.map +1 -1
  61. package/dist/sync/push-receiver.js +1 -30
  62. package/dist/sync/push-receiver.js.map +1 -1
  63. package/dist/sync/push-receiver.test.js +0 -97
  64. package/dist/sync/push-receiver.test.js.map +1 -1
  65. package/dist/sync-core.d.ts +1 -2
  66. package/dist/sync-core.d.ts.map +1 -1
  67. package/dist/sync-core.js +10 -5
  68. package/dist/sync-core.js.map +1 -1
  69. package/dist/sync-core.test.js +0 -16
  70. package/dist/sync-core.test.js.map +1 -1
  71. package/package.json +1 -1
  72. package/dist/s3-download-stream-hash.test.d.ts +0 -2
  73. package/dist/s3-download-stream-hash.test.d.ts.map +0 -1
  74. package/dist/s3-download-stream-hash.test.js +0 -70
  75. package/dist/s3-download-stream-hash.test.js.map +0 -1
package/dist/object-io.js CHANGED
@@ -25,7 +25,6 @@
25
25
  import { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsV2Command, DeleteObjectCommand, HeadObjectCommand, } from "@aws-sdk/client-s3";
26
26
  import * as http from "node:http";
27
27
  import * as https from "node:https";
28
- import { Transform } from "node:stream";
29
28
  import { isAbortOrTimeoutError, isTransientNetworkError } from "./lib/net-errors.js";
30
29
  import { headersForObjectLockPut, isObjectLockChecksumRequiredBody, isObjectLockChecksumRequiredError, ObjectLockChecksumRequiredError, } from "./lib/s3-content-checksum.js";
31
30
  export { ObjectLockChecksumRequiredError };
@@ -37,97 +36,13 @@ import { VaultClientError } from "./vault-client.js";
37
36
  * socket fails the leg as transient instead of freezing the runner.
38
37
  */
39
38
  export const OBJECT_IO_REQUEST_TIMEOUT_MS = 60_000;
40
- /** Maximum idle interval while consuming one streamed object body. */
41
- export const OBJECT_IO_BODY_IDLE_TIMEOUT_MS = 15_000;
42
39
  let objectIORequestTimeoutMsForTest;
43
- let objectIOBodyIdleTimeoutMsForTest;
44
40
  export function objectIORequestTimeoutMs() {
45
41
  return objectIORequestTimeoutMsForTest ?? OBJECT_IO_REQUEST_TIMEOUT_MS;
46
42
  }
47
43
  export function setObjectIORequestTimeoutMsForTesting(ms) {
48
44
  objectIORequestTimeoutMsForTest = ms;
49
45
  }
50
- export function setObjectIOBodyIdleTimeoutMsForTesting(ms) {
51
- objectIOBodyIdleTimeoutMsForTest = ms;
52
- }
53
- function objectIOBodyIdleTimeoutMs() {
54
- return objectIOBodyIdleTimeoutMsForTest ?? OBJECT_IO_BODY_IDLE_TIMEOUT_MS;
55
- }
56
- export class ObjectBodyIdleTimeoutError extends Error {
57
- constructor(key, timeoutMs) {
58
- super(`object body made no progress for ${timeoutMs} ms: ${key}`);
59
- this.name = "ObjectBodyIdleTimeoutError";
60
- }
61
- }
62
- /** Bound a streamed GET after response headers arrive. */
63
- export async function* withObjectBodyIdleTimeout(body, key, timeoutMs = objectIOBodyIdleTimeoutMs(), onTimeout) {
64
- if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
65
- throw new Error("object body idle timeout must be positive");
66
- }
67
- const iterator = body[Symbol.asyncIterator]();
68
- let complete = false;
69
- let closed = false;
70
- const close = () => {
71
- if (closed)
72
- return;
73
- closed = true;
74
- try {
75
- void Promise.resolve(iterator.return?.()).catch(() => undefined);
76
- }
77
- catch {
78
- // Preserve the timeout/error as the authoritative transfer result.
79
- }
80
- };
81
- try {
82
- while (true) {
83
- let timer;
84
- const item = await Promise.race([
85
- Promise.resolve(iterator.next()),
86
- new Promise((_resolve, reject) => {
87
- timer = setTimeout(() => {
88
- const error = new ObjectBodyIdleTimeoutError(key, timeoutMs);
89
- try {
90
- onTimeout?.(error);
91
- }
92
- catch {
93
- // Preserve the timeout/error as the authoritative transfer result.
94
- }
95
- close();
96
- reject(error);
97
- }, timeoutMs);
98
- }),
99
- ]).finally(() => {
100
- if (timer !== undefined)
101
- clearTimeout(timer);
102
- });
103
- if (item.done) {
104
- complete = true;
105
- return;
106
- }
107
- yield item.value;
108
- }
109
- }
110
- finally {
111
- if (!complete)
112
- close();
113
- }
114
- }
115
- function cancelObjectBody(body, error) {
116
- if (!body || typeof body !== "object")
117
- return;
118
- const stream = body;
119
- try {
120
- if (typeof stream.destroy === "function") {
121
- stream.destroy(error);
122
- }
123
- else if (typeof stream.cancel === "function") {
124
- void Promise.resolve(stream.cancel(error)).catch(() => undefined);
125
- }
126
- }
127
- catch {
128
- // The idle timeout remains the authoritative transfer failure.
129
- }
130
- }
131
46
  function objectIOAbortSignal() {
132
47
  return AbortSignal.timeout(objectIORequestTimeoutMs());
133
48
  }
@@ -327,19 +242,11 @@ export class S3SdkObjectIO {
327
242
  }
328
243
  }
329
244
  async putObject(input) {
330
- if (input.bypassBandwidthGovernor)
331
- return this.putObjectWithGovernor(input);
332
- return getBandwidthGovernor().withChoreTransfer(() => this.putObjectWithGovernor(input));
333
- }
334
- async putObjectWithGovernor(input) {
335
- const governor = getBandwidthGovernor();
336
245
  // Data-plane pacing: acquire tokens for the whole body before the PUT so
337
246
  // aggregate upload+download throughput stays under the governor's cap.
338
- if (!input.bypassBandwidthGovernor) {
339
- await governor.acquire(input.body.byteLength);
340
- }
247
+ await getBandwidthGovernor().acquire(input.body.byteLength);
341
248
  try {
342
- const send = () => this.sendWithCredentialRetry((client, abortSignal) => client.send(new PutObjectCommand({
249
+ const res = await this.sendWithCredentialRetry((client, abortSignal) => client.send(new PutObjectCommand({
343
250
  Bucket: this.bucket,
344
251
  Key: input.key,
345
252
  Body: input.body,
@@ -356,9 +263,6 @@ export class S3SdkObjectIO {
356
263
  ...(input.ifMatch ? { IfMatch: quoteEtag(input.ifMatch) } : {}),
357
264
  ...(input.ifNoneMatch ? { IfNoneMatch: input.ifNoneMatch } : {}),
358
265
  }), { abortSignal }));
359
- const res = input.bypassBandwidthGovernor
360
- ? await send()
361
- : await governor.measureRawTransfer(input.body.byteLength, send);
362
266
  return { etag: res.ETag || "" };
363
267
  }
364
268
  catch (err) {
@@ -368,51 +272,20 @@ export class S3SdkObjectIO {
368
272
  throw err;
369
273
  }
370
274
  }
371
- async getObjectStream(key, options = {}) {
372
- const governor = getBandwidthGovernor();
373
- const release = options.bypassBandwidthGovernor
374
- ? undefined
375
- : await governor.reserveChoreTransfer();
376
- try {
377
- const res = await this.sendWithCredentialRetry((client, abortSignal) => client.send(new GetObjectCommand({
378
- Bucket: this.bucket,
379
- Key: key,
380
- ...(options.rangeStart !== undefined
381
- ? { Range: `bytes=${options.rangeStart}-` }
382
- : {}),
383
- ...(options.ifMatch ? { IfMatch: quoteEtag(options.ifMatch) } : {}),
384
- }), { abortSignal }));
385
- if (!res.Body) {
386
- throw new Error(`Empty response for ${key}`);
387
- }
388
- const rawBody = res.Body;
389
- const observedBody = options.bypassBandwidthGovernor
390
- ? rawBody
391
- : governor.observeRawBody(rawBody);
392
- const idleBoundBody = withObjectBodyIdleTimeout(observedBody, key, objectIOBodyIdleTimeoutMs(), (error) => cancelObjectBody(res.Body, error));
393
- return {
394
- body: options.bypassBandwidthGovernor
395
- ? idleBoundBody
396
- : governor.governBody(idleBoundBody, release),
397
- metadata: res.Metadata,
398
- etag: res.ETag,
399
- versionId: res.VersionId,
400
- contentLength: res.ContentLength,
401
- contentRange: res.ContentRange,
402
- statusCode: res.$metadata?.httpStatusCode,
403
- destroy: (error) => {
404
- cancelObjectBody(res.Body, error ?? new Error(`GET body closed: ${key}`));
405
- release?.();
406
- },
407
- };
408
- }
409
- catch (error) {
410
- release?.();
411
- throw error;
275
+ async getObjectStream(key) {
276
+ const res = await this.sendWithCredentialRetry((client, abortSignal) => client.send(new GetObjectCommand({ Bucket: this.bucket, Key: key }), { abortSignal }));
277
+ if (!res.Body) {
278
+ throw new Error(`Empty response for ${key}`);
412
279
  }
280
+ return {
281
+ // Data-plane pacing: each chunk acquires governor tokens before it is
282
+ // yielded (backpressure — the SDK stream pauses while we wait).
283
+ body: getBandwidthGovernor().throttleBody(res.Body),
284
+ metadata: res.Metadata,
285
+ };
413
286
  }
414
- async getObject(key, options = {}) {
415
- const res = await this.getObjectStream(key, options);
287
+ async getObject(key) {
288
+ const res = await this.getObjectStream(key);
416
289
  const body = await drainToBuffer(res.body);
417
290
  return { body, metadata: res.metadata };
418
291
  }
@@ -449,7 +322,6 @@ export class S3SdkObjectIO {
449
322
  lastModified: res.LastModified || new Date(),
450
323
  etag: res.ETag || "",
451
324
  size: res.ContentLength || 0,
452
- versionId: res.VersionId,
453
325
  metadata: res.Metadata,
454
326
  storageClass: res.StorageClass ?? "STANDARD",
455
327
  };
@@ -508,41 +380,39 @@ function expectedContentLength(headers) {
508
380
  function contentLengthError(expected, actual) {
509
381
  return Object.assign(new Error(`presigned GET content-length mismatch: expected ${expected} bytes, received ${actual}`), { code: "ERR_HQ_TRUNCATED_RESPONSE" });
510
382
  }
511
- function checkedNodeBody(message, expected) {
383
+ async function* checkedNodeBody(message, expected) {
512
384
  let received = 0;
513
- const body = new Transform({
514
- transform: (chunk, _encoding, callback) => {
515
- received += chunk.byteLength;
516
- if (expected !== undefined && received > expected) {
517
- callback(contentLengthError(expected, received));
518
- return;
385
+ let exhausted = false;
386
+ try {
387
+ try {
388
+ for await (const chunk of message) {
389
+ const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
390
+ received += bytes.byteLength;
391
+ if (expected !== undefined && received > expected) {
392
+ throw contentLengthError(expected, received);
393
+ }
394
+ yield bytes;
519
395
  }
520
- callback(null, chunk);
521
- },
522
- flush: (callback) => {
396
+ }
397
+ catch (err) {
398
+ // A FIN before Node has received the declared bytes normally surfaces as
399
+ // an "aborted" stream error. Preserve the useful data-integrity cause.
523
400
  if (expected !== undefined && received !== expected) {
524
- callback(contentLengthError(expected, received));
525
- return;
401
+ throw contentLengthError(expected, received);
526
402
  }
527
- callback();
528
- },
529
- });
530
- message.once("error", (error) => {
531
- body.destroy(expected !== undefined && received !== expected
532
- ? contentLengthError(expected, received)
533
- : error);
534
- });
535
- message.once("aborted", () => {
536
- body.destroy(expected !== undefined && received !== expected
537
- ? contentLengthError(expected, received)
538
- : new Error("presigned GET body aborted"));
539
- });
540
- body.once("close", () => {
541
- if (!message.destroyed)
403
+ throw err;
404
+ }
405
+ exhausted = true;
406
+ if (expected !== undefined && received !== expected) {
407
+ throw contentLengthError(expected, received);
408
+ }
409
+ }
410
+ finally {
411
+ // Consumers such as HEAD only inspect headers, and a caller can stop after
412
+ // a prefix. Destroying in both cases releases the socket immediately.
413
+ if (!exhausted)
542
414
  message.destroy();
543
- });
544
- message.pipe(body);
545
- return body;
415
+ }
546
416
  }
547
417
  function nodePresignedGet(url, headers) {
548
418
  const target = new URL(url);
@@ -558,15 +428,11 @@ function nodePresignedGet(url, headers) {
558
428
  reject(err);
559
429
  return;
560
430
  }
561
- const body = checkedNodeBody(message, expected);
562
431
  resolve({
563
432
  status: message.statusCode ?? 0,
564
433
  headers: responseHeaders,
565
- body,
566
- destroy: (error) => {
567
- body.destroy(error);
568
- message.destroy(error);
569
- },
434
+ body: checkedNodeBody(message, expected),
435
+ destroy: () => message.destroy(),
570
436
  });
571
437
  };
572
438
  const request = target.protocol === "https:"
@@ -1041,46 +907,33 @@ export class PresignObjectIO {
1041
907
  if (isPresignFenceStrictEnabled()) {
1042
908
  this.requireSignedPutPreconditions(input.key, input, row.headers);
1043
909
  }
1044
- const governor = getBandwidthGovernor();
1045
- const performPut = async () => {
1046
- // Data-plane pacing: acquire tokens for the whole body before the PUT so
1047
- // aggregate upload+download throughput stays under the governor's cap.
1048
- if (!input.bypassBandwidthGovernor) {
1049
- await governor.acquire(input.body.byteLength);
910
+ // Data-plane pacing: acquire tokens for the whole body before the PUT so
911
+ // aggregate upload+download throughput stays under the governor's cap.
912
+ await getBandwidthGovernor().acquire(input.body.byteLength);
913
+ // The server signs Content-Type, SSE-KMS, and every x-amz-meta-* into the
914
+ // signature and returns them in `headers`; they MUST be replayed verbatim
915
+ // or SigV4 rejects the PUT.
916
+ const res = await fetchWithRetry(row.url, {
917
+ method: "PUT",
918
+ body: input.body,
919
+ headers: headersForObjectLockPut(row.url, row.headers, input.body),
920
+ }, `presigned PUT ${input.key}`);
921
+ if (res.status === 412) {
922
+ // The signed conditional header fenced this write off: the remote
923
+ // moved past our baseline (If-Match) or the key already exists
924
+ // (If-None-Match). Same shape as the SDK's PreconditionFailed so the
925
+ // push path routes both transports through one conflict handler.
926
+ const detail = await safeText(res);
927
+ throw preconditionFailedError(input.key, detail);
928
+ }
929
+ if (!res.ok) {
930
+ const detail = await safeText(res);
931
+ if (isObjectLockChecksumRequiredBody(res.status, detail)) {
932
+ throw new ObjectLockChecksumRequiredError(input.key, detail);
1050
933
  }
1051
- const send = async () => {
1052
- // The server signs Content-Type, SSE-KMS, and every x-amz-meta-* into the
1053
- // signature and returns them in `headers`; they MUST be replayed verbatim
1054
- // or SigV4 rejects the PUT.
1055
- const res = await fetchWithRetry(row.url, {
1056
- method: "PUT",
1057
- body: input.body,
1058
- headers: headersForObjectLockPut(row.url, row.headers, input.body),
1059
- }, `presigned PUT ${input.key}`);
1060
- if (res.status === 412) {
1061
- // The signed conditional header fenced this write off: the remote
1062
- // moved past our baseline (If-Match) or the key already exists
1063
- // (If-None-Match). Same shape as the SDK's PreconditionFailed so the
1064
- // push path routes both transports through one conflict handler.
1065
- const detail = await safeText(res);
1066
- throw preconditionFailedError(input.key, detail);
1067
- }
1068
- if (!res.ok) {
1069
- const detail = await safeText(res);
1070
- if (isObjectLockChecksumRequiredBody(res.status, detail)) {
1071
- throw new ObjectLockChecksumRequiredError(input.key, detail);
1072
- }
1073
- throw new Error(`presigned PUT failed for ${input.key}: ${res.status} ${detail}`);
1074
- }
1075
- return { etag: stripQuotes(res.headers.get("etag") ?? undefined) };
1076
- };
1077
- return input.bypassBandwidthGovernor
1078
- ? send()
1079
- : governor.measureRawTransfer(input.body.byteLength, send);
1080
- };
1081
- return input.bypassBandwidthGovernor
1082
- ? performPut()
1083
- : getBandwidthGovernor().withChoreTransfer(performPut);
934
+ throw new Error(`presigned PUT failed for ${input.key}: ${res.status} ${detail}`);
935
+ }
936
+ return { etag: stripQuotes(res.headers.get("etag") ?? undefined) };
1084
937
  }
1085
938
  /**
1086
939
  * True-streaming GET with bounded open-time outer retries.
@@ -1096,119 +949,81 @@ export class PresignObjectIO {
1096
949
  * consumer that already received prefix bytes). That recovery lives at the
1097
950
  * whole-download seam in s3.ts `downloadFile` (fresh stream → fresh temp).
1098
951
  */
1099
- async getObjectStream(key, options = {}) {
1100
- const governor = getBandwidthGovernor();
1101
- const release = options.bypassBandwidthGovernor
1102
- ? undefined
1103
- : await governor.reserveChoreTransfer();
1104
- let leaseHandedOff = false;
1105
- try {
1106
- let lastError;
1107
- // After a proven-expired URL or exhausted same-URL transport failure, the
1108
- // next attempt must mint a new presign rather than replaying a dead URL.
1109
- let forceFreshUrl = false;
1110
- for (let attempt = 0; attempt <= FETCH_MAX_RETRIES; attempt++) {
1111
- if (attempt > 0) {
1112
- const backoff = FETCH_BASE_DELAY_MS * 2 ** (attempt - 1);
1113
- const jitter = Math.floor(Math.random() * FETCH_BASE_DELAY_MS);
1114
- await sleep(backoff + jitter);
1115
- }
1116
- // RateLimited / per-key Forbidden from the mint path are definitive and
1117
- // propagate to the caller without outer transport retry.
1118
- let row;
1119
- if (forceFreshUrl) {
1120
- this.invalidateGetCache(key);
1121
- row = await this.presignSingle("get", key);
1122
- }
1123
- else {
1124
- row = await this.resolveGetUrlForBody(key);
1125
- }
1126
- forceFreshUrl = false;
1127
- let res;
1128
- try {
1129
- const requestHeaders = { ...(row.headers ?? {}) };
1130
- if (options.rangeStart !== undefined) {
1131
- requestHeaders.Range = `bytes=${options.rangeStart}-`;
1132
- }
1133
- if (options.ifMatch) {
1134
- requestHeaders["If-Match"] = quoteEtag(options.ifMatch);
1135
- }
1136
- res = await presignedGetWithRetry(row.url, Object.keys(requestHeaders).length > 0 ? requestHeaders : undefined, `presigned GET ${key}`);
1137
- }
1138
- catch (err) {
1139
- // presignedGetWithRetry already burned same-URL connection retries.
1140
- // One outer pass with a freshly minted URL recovers poisoned sockets
1141
- // that keep failing on the same signed endpoint.
1142
- if (isRetryablePresignedGetError(err) && attempt < FETCH_MAX_RETRIES) {
1143
- lastError = err;
1144
- forceFreshUrl = true;
1145
- continue;
1146
- }
1147
- throw err;
1148
- }
1149
- if (res.status === 404) {
1150
- res.destroy();
1151
- throw notFoundError(key);
1152
- }
1153
- if (res.status === 403) {
1154
- const detail = await safePresignedGetText(res);
1155
- this.invalidateGetCache(key);
1156
- if (isExpiredPresignDetail(detail) && attempt < FETCH_MAX_RETRIES) {
1157
- lastError = new Error(`presigned GET expired for ${key}: 403 ${detail}`);
1158
- forceFreshUrl = true;
1159
- continue;
1160
- }
1161
- // True ACL/KMS/bucket denial — never conflate with absence, and do
1162
- // not burn re-presigns on a definitive Forbidden.
1163
- throw accessDeniedError(key, detail || "presigned GET returned 403");
1164
- }
1165
- if (res.status === 412 || res.status === 416) {
1166
- const detail = await safePresignedGetText(res);
1167
- throw preconditionFailedError(key, detail);
952
+ async getObjectStream(key) {
953
+ let lastError;
954
+ // After a proven-expired URL or exhausted same-URL transport failure, the
955
+ // next attempt must mint a new presign rather than replaying a dead URL.
956
+ let forceFreshUrl = false;
957
+ for (let attempt = 0; attempt <= FETCH_MAX_RETRIES; attempt++) {
958
+ if (attempt > 0) {
959
+ const backoff = FETCH_BASE_DELAY_MS * 2 ** (attempt - 1);
960
+ const jitter = Math.floor(Math.random() * FETCH_BASE_DELAY_MS);
961
+ await sleep(backoff + jitter);
962
+ }
963
+ // RateLimited / per-key Forbidden from the mint path are definitive and
964
+ // propagate to the caller without outer transport retry.
965
+ let row;
966
+ if (forceFreshUrl) {
967
+ this.invalidateGetCache(key);
968
+ row = await this.presignSingle("get", key);
969
+ }
970
+ else {
971
+ row = await this.resolveGetUrlForBody(key);
972
+ }
973
+ forceFreshUrl = false;
974
+ let res;
975
+ try {
976
+ res = await presignedGetWithRetry(row.url, row.headers, `presigned GET ${key}`);
977
+ }
978
+ catch (err) {
979
+ // presignedGetWithRetry already burned same-URL connection retries.
980
+ // One outer pass with a freshly minted URL recovers poisoned sockets
981
+ // that keep failing on the same signed endpoint.
982
+ if (isRetryablePresignedGetError(err) && attempt < FETCH_MAX_RETRIES) {
983
+ lastError = err;
984
+ forceFreshUrl = true;
985
+ continue;
1168
986
  }
1169
- if (res.status < 200 || res.status >= 300) {
1170
- // Transient 5xx already retried inside presignedGetWithRetry; a final
1171
- // non-ok status is reported to the caller without outer re-presign.
1172
- const detail = await safePresignedGetText(res);
1173
- throw new Error(`presigned GET failed for ${key}: ${res.status} ${detail}`);
987
+ throw err;
988
+ }
989
+ if (res.status === 404) {
990
+ res.destroy();
991
+ throw notFoundError(key);
992
+ }
993
+ if (res.status === 403) {
994
+ const detail = await safePresignedGetText(res);
995
+ this.invalidateGetCache(key);
996
+ if (isExpiredPresignDetail(detail) && attempt < FETCH_MAX_RETRIES) {
997
+ lastError = new Error(`presigned GET expired for ${key}: 403 ${detail}`);
998
+ forceFreshUrl = true;
999
+ continue;
1174
1000
  }
1175
- // Live body stream — do not drain. Callers that need a Buffer use
1176
- // getObject; downloadFile streams to a temp path and retries mid-stream
1177
- // transport cuts around the whole attempt.
1178
- const rawBody = res.body ?? (async function* () { })();
1179
- const observedBody = options.bypassBandwidthGovernor
1180
- ? rawBody
1181
- : governor.observeRawBody(rawBody);
1182
- const idleBoundBody = withObjectBodyIdleTimeout(observedBody, key, objectIOBodyIdleTimeoutMs(), (error) => res.destroy(error));
1183
- const result = {
1184
- body: options.bypassBandwidthGovernor
1185
- ? idleBoundBody
1186
- : governor.governBody(idleBoundBody, release),
1187
- metadata: metaFromHeaders(res.headers),
1188
- etag: res.headers.get("etag") ?? undefined,
1189
- versionId: res.headers.get("x-amz-version-id") ?? undefined,
1190
- contentLength: expectedContentLength(res.headers),
1191
- contentRange: res.headers.get("content-range") ?? undefined,
1192
- statusCode: res.status,
1193
- destroy: (error) => {
1194
- res.destroy(error);
1195
- release?.();
1196
- },
1197
- };
1198
- leaseHandedOff = true;
1199
- return result;
1001
+ // True ACL/KMS/bucket denial — never conflate with absence, and do
1002
+ // not burn re-presigns on a definitive Forbidden.
1003
+ throw accessDeniedError(key, detail || "presigned GET returned 403");
1200
1004
  }
1201
- throw lastError instanceof Error
1202
- ? lastError
1203
- : new Error(`presigned GET ${key}: failed after ${FETCH_MAX_RETRIES} retries`);
1204
- }
1205
- finally {
1206
- if (!leaseHandedOff)
1207
- release?.();
1005
+ if (res.status < 200 || res.status >= 300) {
1006
+ // Transient 5xx already retried inside presignedGetWithRetry; a final
1007
+ // non-ok status is reported to the caller without outer re-presign.
1008
+ const detail = await safePresignedGetText(res);
1009
+ throw new Error(`presigned GET failed for ${key}: ${res.status} ${detail}`);
1010
+ }
1011
+ // Live body stream — do not drain. Callers that need a Buffer use
1012
+ // getObject; downloadFile streams to a temp path and retries mid-stream
1013
+ // transport cuts around the whole attempt.
1014
+ return {
1015
+ // Data-plane pacing: chunks acquire governor tokens before yielding
1016
+ // (backpressure — the socket pauses while we wait).
1017
+ body: getBandwidthGovernor().throttleBody(res.body ?? (async function* () { })()),
1018
+ metadata: metaFromHeaders(res.headers),
1019
+ };
1208
1020
  }
1021
+ throw lastError instanceof Error
1022
+ ? lastError
1023
+ : new Error(`presigned GET ${key}: failed after ${FETCH_MAX_RETRIES} retries`);
1209
1024
  }
1210
- async getObject(key, options = {}) {
1211
- const res = await this.getObjectStream(key, options);
1025
+ async getObject(key) {
1026
+ const res = await this.getObjectStream(key);
1212
1027
  const body = await drainToBuffer(res.body);
1213
1028
  return { body, metadata: res.metadata };
1214
1029
  }
@@ -1318,7 +1133,6 @@ export class PresignObjectIO {
1318
1133
  lastModified: parseLastModified(res.headers.get("last-modified")),
1319
1134
  etag: stripQuotes(res.headers.get("etag") ?? undefined),
1320
1135
  size: Number(res.headers.get("content-length") ?? "0"),
1321
- versionId: res.headers.get("x-amz-version-id") ?? undefined,
1322
1136
  storageClass: res.headers.get("x-amz-storage-class") ?? "STANDARD",
1323
1137
  metadata: metaFromHeaders(res.headers),
1324
1138
  };