@indigoai-us/hq-cloud 6.18.3-alpha.0 → 6.18.4-alpha.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 (96) hide show
  1. package/dist/bandwidth.d.ts +44 -10
  2. package/dist/bandwidth.d.ts.map +1 -1
  3. package/dist/bandwidth.js +219 -17
  4. package/dist/bandwidth.js.map +1 -1
  5. package/dist/bandwidth.test.js +79 -4
  6. package/dist/bandwidth.test.js.map +1 -1
  7. package/dist/bin/sync-runner-company.d.ts +2 -0
  8. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  9. package/dist/bin/sync-runner-company.js +15 -0
  10. package/dist/bin/sync-runner-company.js.map +1 -1
  11. package/dist/bin/sync-runner-company.test.js +56 -0
  12. package/dist/bin/sync-runner-company.test.js.map +1 -1
  13. package/dist/bin/sync-runner-events.test.js +15 -0
  14. package/dist/bin/sync-runner-events.test.js.map +1 -1
  15. package/dist/bin/sync-runner-watch-loop.d.ts +31 -5
  16. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-watch-loop.js +228 -10
  18. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  19. package/dist/bin/sync-runner.d.ts +31 -2
  20. package/dist/bin/sync-runner.d.ts.map +1 -1
  21. package/dist/bin/sync-runner.js +14 -5
  22. package/dist/bin/sync-runner.js.map +1 -1
  23. package/dist/bin/sync-runner.test.js +120 -39
  24. package/dist/bin/sync-runner.test.js.map +1 -1
  25. package/dist/cli/share.d.ts +26 -8
  26. package/dist/cli/share.d.ts.map +1 -1
  27. package/dist/cli/share.js +206 -60
  28. package/dist/cli/share.js.map +1 -1
  29. package/dist/cli/share.test.js +224 -3
  30. package/dist/cli/share.test.js.map +1 -1
  31. package/dist/cli/sync.d.ts +21 -4
  32. package/dist/cli/sync.d.ts.map +1 -1
  33. package/dist/cli/sync.js +139 -18
  34. package/dist/cli/sync.js.map +1 -1
  35. package/dist/cli/sync.test.js +230 -1
  36. package/dist/cli/sync.test.js.map +1 -1
  37. package/dist/lib/machine-id.d.ts +7 -0
  38. package/dist/lib/machine-id.d.ts.map +1 -1
  39. package/dist/lib/machine-id.js +54 -7
  40. package/dist/lib/machine-id.js.map +1 -1
  41. package/dist/lib/machine-id.test.js +20 -1
  42. package/dist/lib/machine-id.test.js.map +1 -1
  43. package/dist/lib/ontology-entity-push.d.ts +51 -0
  44. package/dist/lib/ontology-entity-push.d.ts.map +1 -0
  45. package/dist/lib/ontology-entity-push.js +125 -0
  46. package/dist/lib/ontology-entity-push.js.map +1 -0
  47. package/dist/lib/ontology-entity-push.test.d.ts +2 -0
  48. package/dist/lib/ontology-entity-push.test.d.ts.map +1 -0
  49. package/dist/lib/ontology-entity-push.test.js +149 -0
  50. package/dist/lib/ontology-entity-push.test.js.map +1 -0
  51. package/dist/object-io.d.ts +33 -7
  52. package/dist/object-io.d.ts.map +1 -1
  53. package/dist/object-io.js +329 -140
  54. package/dist/object-io.js.map +1 -1
  55. package/dist/object-io.test.js +400 -1
  56. package/dist/object-io.test.js.map +1 -1
  57. package/dist/s3-download-stream-hash.test.d.ts +2 -0
  58. package/dist/s3-download-stream-hash.test.d.ts.map +1 -0
  59. package/dist/s3-download-stream-hash.test.js +70 -0
  60. package/dist/s3-download-stream-hash.test.js.map +1 -0
  61. package/dist/s3.d.ts +8 -3
  62. package/dist/s3.d.ts.map +1 -1
  63. package/dist/s3.js +304 -27
  64. package/dist/s3.js.map +1 -1
  65. package/dist/s3.test.js +163 -0
  66. package/dist/s3.test.js.map +1 -1
  67. package/dist/sync/event-sync.d.ts +14 -4
  68. package/dist/sync/event-sync.d.ts.map +1 -1
  69. package/dist/sync/event-sync.js +145 -14
  70. package/dist/sync/event-sync.js.map +1 -1
  71. package/dist/sync/event-sync.test.js +266 -1
  72. package/dist/sync/event-sync.test.js.map +1 -1
  73. package/dist/sync/inbound-event-admission.d.ts.map +1 -1
  74. package/dist/sync/inbound-event-admission.js +3 -0
  75. package/dist/sync/inbound-event-admission.js.map +1 -1
  76. package/dist/sync/inbound-event-admission.test.js +6 -0
  77. package/dist/sync/inbound-event-admission.test.js.map +1 -1
  78. package/dist/sync/push-transport.d.ts +15 -0
  79. package/dist/sync/push-transport.d.ts.map +1 -1
  80. package/dist/sync/push-transport.js +27 -0
  81. package/dist/sync/push-transport.js.map +1 -1
  82. package/dist/sync/push-transport.test.js +14 -1
  83. package/dist/sync/push-transport.test.js.map +1 -1
  84. package/dist/sync-core.d.ts +2 -1
  85. package/dist/sync-core.d.ts.map +1 -1
  86. package/dist/sync-core.js +5 -10
  87. package/dist/sync-core.js.map +1 -1
  88. package/dist/sync-core.test.js +16 -0
  89. package/dist/sync-core.test.js.map +1 -1
  90. package/dist/vault-client.d.ts +5 -0
  91. package/dist/vault-client.d.ts.map +1 -1
  92. package/dist/vault-client.js +7 -0
  93. package/dist/vault-client.js.map +1 -1
  94. package/dist/vault-client.test.js +39 -0
  95. package/dist/vault-client.test.js.map +1 -1
  96. package/package.json +1 -1
package/dist/object-io.js CHANGED
@@ -25,6 +25,7 @@
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";
28
29
  import { isAbortOrTimeoutError, isTransientNetworkError } from "./lib/net-errors.js";
29
30
  import { headersForObjectLockPut, isObjectLockChecksumRequiredBody, isObjectLockChecksumRequiredError, ObjectLockChecksumRequiredError, } from "./lib/s3-content-checksum.js";
30
31
  export { ObjectLockChecksumRequiredError };
@@ -36,13 +37,97 @@ import { VaultClientError } from "./vault-client.js";
36
37
  * socket fails the leg as transient instead of freezing the runner.
37
38
  */
38
39
  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;
39
42
  let objectIORequestTimeoutMsForTest;
43
+ let objectIOBodyIdleTimeoutMsForTest;
40
44
  export function objectIORequestTimeoutMs() {
41
45
  return objectIORequestTimeoutMsForTest ?? OBJECT_IO_REQUEST_TIMEOUT_MS;
42
46
  }
43
47
  export function setObjectIORequestTimeoutMsForTesting(ms) {
44
48
  objectIORequestTimeoutMsForTest = ms;
45
49
  }
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
+ }
46
131
  function objectIOAbortSignal() {
47
132
  return AbortSignal.timeout(objectIORequestTimeoutMs());
48
133
  }
@@ -242,11 +327,19 @@ export class S3SdkObjectIO {
242
327
  }
243
328
  }
244
329
  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();
245
336
  // Data-plane pacing: acquire tokens for the whole body before the PUT so
246
337
  // aggregate upload+download throughput stays under the governor's cap.
247
- await getBandwidthGovernor().acquire(input.body.byteLength);
338
+ if (!input.bypassBandwidthGovernor) {
339
+ await governor.acquire(input.body.byteLength);
340
+ }
248
341
  try {
249
- const res = await this.sendWithCredentialRetry((client, abortSignal) => client.send(new PutObjectCommand({
342
+ const send = () => this.sendWithCredentialRetry((client, abortSignal) => client.send(new PutObjectCommand({
250
343
  Bucket: this.bucket,
251
344
  Key: input.key,
252
345
  Body: input.body,
@@ -263,6 +356,9 @@ export class S3SdkObjectIO {
263
356
  ...(input.ifMatch ? { IfMatch: quoteEtag(input.ifMatch) } : {}),
264
357
  ...(input.ifNoneMatch ? { IfNoneMatch: input.ifNoneMatch } : {}),
265
358
  }), { abortSignal }));
359
+ const res = input.bypassBandwidthGovernor
360
+ ? await send()
361
+ : await governor.measureRawTransfer(input.body.byteLength, send);
266
362
  return { etag: res.ETag || "" };
267
363
  }
268
364
  catch (err) {
@@ -272,20 +368,51 @@ export class S3SdkObjectIO {
272
368
  throw err;
273
369
  }
274
370
  }
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}`);
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;
279
412
  }
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
- };
286
413
  }
287
- async getObject(key) {
288
- const res = await this.getObjectStream(key);
414
+ async getObject(key, options = {}) {
415
+ const res = await this.getObjectStream(key, options);
289
416
  const body = await drainToBuffer(res.body);
290
417
  return { body, metadata: res.metadata };
291
418
  }
@@ -322,6 +449,7 @@ export class S3SdkObjectIO {
322
449
  lastModified: res.LastModified || new Date(),
323
450
  etag: res.ETag || "",
324
451
  size: res.ContentLength || 0,
452
+ versionId: res.VersionId,
325
453
  metadata: res.Metadata,
326
454
  storageClass: res.StorageClass ?? "STANDARD",
327
455
  };
@@ -380,39 +508,41 @@ function expectedContentLength(headers) {
380
508
  function contentLengthError(expected, actual) {
381
509
  return Object.assign(new Error(`presigned GET content-length mismatch: expected ${expected} bytes, received ${actual}`), { code: "ERR_HQ_TRUNCATED_RESPONSE" });
382
510
  }
383
- async function* checkedNodeBody(message, expected) {
511
+ function checkedNodeBody(message, expected) {
384
512
  let received = 0;
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;
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;
395
519
  }
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.
520
+ callback(null, chunk);
521
+ },
522
+ flush: (callback) => {
400
523
  if (expected !== undefined && received !== expected) {
401
- throw contentLengthError(expected, received);
524
+ callback(contentLengthError(expected, received));
525
+ return;
402
526
  }
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)
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)
414
542
  message.destroy();
415
- }
543
+ });
544
+ message.pipe(body);
545
+ return body;
416
546
  }
417
547
  function nodePresignedGet(url, headers) {
418
548
  const target = new URL(url);
@@ -428,11 +558,15 @@ function nodePresignedGet(url, headers) {
428
558
  reject(err);
429
559
  return;
430
560
  }
561
+ const body = checkedNodeBody(message, expected);
431
562
  resolve({
432
563
  status: message.statusCode ?? 0,
433
564
  headers: responseHeaders,
434
- body: checkedNodeBody(message, expected),
435
- destroy: () => message.destroy(),
565
+ body,
566
+ destroy: (error) => {
567
+ body.destroy(error);
568
+ message.destroy(error);
569
+ },
436
570
  });
437
571
  };
438
572
  const request = target.protocol === "https:"
@@ -494,6 +628,9 @@ function firstRowOrThrow(results, key, op) {
494
628
  throw new Error(`presign ${op} returned no row for ${key}`);
495
629
  }
496
630
  if (row.error || !row.url) {
631
+ if (row.code === "PLAN_LIMIT_REACHED" && row.upgradeUrl) {
632
+ throw new VaultClientError(row.error ?? "Plan limit reached", 402, undefined, { code: row.code, path: key, upgradeUrl: row.upgradeUrl });
633
+ }
497
634
  // An expected per-key denial (see {@link isSkipWithLogDenialCode}) must
498
635
  // throw the SAME `name: "Forbidden"` shape the GET/HEAD path uses so it
499
636
  // routes through the `isAccessDenied` skip in sync.ts / share.ts. Without
@@ -907,33 +1044,46 @@ export class PresignObjectIO {
907
1044
  if (isPresignFenceStrictEnabled()) {
908
1045
  this.requireSignedPutPreconditions(input.key, input, row.headers);
909
1046
  }
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);
1047
+ const governor = getBandwidthGovernor();
1048
+ const performPut = async () => {
1049
+ // Data-plane pacing: acquire tokens for the whole body before the PUT so
1050
+ // aggregate upload+download throughput stays under the governor's cap.
1051
+ if (!input.bypassBandwidthGovernor) {
1052
+ await governor.acquire(input.body.byteLength);
933
1053
  }
934
- throw new Error(`presigned PUT failed for ${input.key}: ${res.status} ${detail}`);
935
- }
936
- return { etag: stripQuotes(res.headers.get("etag") ?? undefined) };
1054
+ const send = async () => {
1055
+ // The server signs Content-Type, SSE-KMS, and every x-amz-meta-* into the
1056
+ // signature and returns them in `headers`; they MUST be replayed verbatim
1057
+ // or SigV4 rejects the PUT.
1058
+ const res = await fetchWithRetry(row.url, {
1059
+ method: "PUT",
1060
+ body: input.body,
1061
+ headers: headersForObjectLockPut(row.url, row.headers, input.body),
1062
+ }, `presigned PUT ${input.key}`);
1063
+ if (res.status === 412) {
1064
+ // The signed conditional header fenced this write off: the remote
1065
+ // moved past our baseline (If-Match) or the key already exists
1066
+ // (If-None-Match). Same shape as the SDK's PreconditionFailed so the
1067
+ // push path routes both transports through one conflict handler.
1068
+ const detail = await safeText(res);
1069
+ throw preconditionFailedError(input.key, detail);
1070
+ }
1071
+ if (!res.ok) {
1072
+ const detail = await safeText(res);
1073
+ if (isObjectLockChecksumRequiredBody(res.status, detail)) {
1074
+ throw new ObjectLockChecksumRequiredError(input.key, detail);
1075
+ }
1076
+ throw new Error(`presigned PUT failed for ${input.key}: ${res.status} ${detail}`);
1077
+ }
1078
+ return { etag: stripQuotes(res.headers.get("etag") ?? undefined) };
1079
+ };
1080
+ return input.bypassBandwidthGovernor
1081
+ ? send()
1082
+ : governor.measureRawTransfer(input.body.byteLength, send);
1083
+ };
1084
+ return input.bypassBandwidthGovernor
1085
+ ? performPut()
1086
+ : getBandwidthGovernor().withChoreTransfer(performPut);
937
1087
  }
938
1088
  /**
939
1089
  * True-streaming GET with bounded open-time outer retries.
@@ -949,81 +1099,119 @@ export class PresignObjectIO {
949
1099
  * consumer that already received prefix bytes). That recovery lives at the
950
1100
  * whole-download seam in s3.ts `downloadFile` (fresh stream → fresh temp).
951
1101
  */
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;
1102
+ async getObjectStream(key, options = {}) {
1103
+ const governor = getBandwidthGovernor();
1104
+ const release = options.bypassBandwidthGovernor
1105
+ ? undefined
1106
+ : await governor.reserveChoreTransfer();
1107
+ let leaseHandedOff = false;
1108
+ try {
1109
+ let lastError;
1110
+ // After a proven-expired URL or exhausted same-URL transport failure, the
1111
+ // next attempt must mint a new presign rather than replaying a dead URL.
1112
+ let forceFreshUrl = false;
1113
+ for (let attempt = 0; attempt <= FETCH_MAX_RETRIES; attempt++) {
1114
+ if (attempt > 0) {
1115
+ const backoff = FETCH_BASE_DELAY_MS * 2 ** (attempt - 1);
1116
+ const jitter = Math.floor(Math.random() * FETCH_BASE_DELAY_MS);
1117
+ await sleep(backoff + jitter);
986
1118
  }
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;
1119
+ // RateLimited / per-key Forbidden from the mint path are definitive and
1120
+ // propagate to the caller without outer transport retry.
1121
+ let row;
1122
+ if (forceFreshUrl) {
1123
+ this.invalidateGetCache(key);
1124
+ row = await this.presignSingle("get", key);
1000
1125
  }
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");
1004
- }
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}`);
1126
+ else {
1127
+ row = await this.resolveGetUrlForBody(key);
1128
+ }
1129
+ forceFreshUrl = false;
1130
+ let res;
1131
+ try {
1132
+ const requestHeaders = { ...(row.headers ?? {}) };
1133
+ if (options.rangeStart !== undefined) {
1134
+ requestHeaders.Range = `bytes=${options.rangeStart}-`;
1135
+ }
1136
+ if (options.ifMatch) {
1137
+ requestHeaders["If-Match"] = quoteEtag(options.ifMatch);
1138
+ }
1139
+ res = await presignedGetWithRetry(row.url, Object.keys(requestHeaders).length > 0 ? requestHeaders : undefined, `presigned GET ${key}`);
1140
+ }
1141
+ catch (err) {
1142
+ // presignedGetWithRetry already burned same-URL connection retries.
1143
+ // One outer pass with a freshly minted URL recovers poisoned sockets
1144
+ // that keep failing on the same signed endpoint.
1145
+ if (isRetryablePresignedGetError(err) && attempt < FETCH_MAX_RETRIES) {
1146
+ lastError = err;
1147
+ forceFreshUrl = true;
1148
+ continue;
1149
+ }
1150
+ throw err;
1151
+ }
1152
+ if (res.status === 404) {
1153
+ res.destroy();
1154
+ throw notFoundError(key);
1155
+ }
1156
+ if (res.status === 403) {
1157
+ const detail = await safePresignedGetText(res);
1158
+ this.invalidateGetCache(key);
1159
+ if (isExpiredPresignDetail(detail) && attempt < FETCH_MAX_RETRIES) {
1160
+ lastError = new Error(`presigned GET expired for ${key}: 403 ${detail}`);
1161
+ forceFreshUrl = true;
1162
+ continue;
1163
+ }
1164
+ // True ACL/KMS/bucket denial — never conflate with absence, and do
1165
+ // not burn re-presigns on a definitive Forbidden.
1166
+ throw accessDeniedError(key, detail || "presigned GET returned 403");
1167
+ }
1168
+ if (res.status === 412 || res.status === 416) {
1169
+ const detail = await safePresignedGetText(res);
1170
+ throw preconditionFailedError(key, detail);
1171
+ }
1172
+ if (res.status < 200 || res.status >= 300) {
1173
+ // Transient 5xx already retried inside presignedGetWithRetry; a final
1174
+ // non-ok status is reported to the caller without outer re-presign.
1175
+ const detail = await safePresignedGetText(res);
1176
+ throw new Error(`presigned GET failed for ${key}: ${res.status} ${detail}`);
1177
+ }
1178
+ // Live body stream — do not drain. Callers that need a Buffer use
1179
+ // getObject; downloadFile streams to a temp path and retries mid-stream
1180
+ // transport cuts around the whole attempt.
1181
+ const rawBody = res.body ?? (async function* () { })();
1182
+ const observedBody = options.bypassBandwidthGovernor
1183
+ ? rawBody
1184
+ : governor.observeRawBody(rawBody);
1185
+ const idleBoundBody = withObjectBodyIdleTimeout(observedBody, key, objectIOBodyIdleTimeoutMs(), (error) => res.destroy(error));
1186
+ const result = {
1187
+ body: options.bypassBandwidthGovernor
1188
+ ? idleBoundBody
1189
+ : governor.governBody(idleBoundBody, release),
1190
+ metadata: metaFromHeaders(res.headers),
1191
+ etag: res.headers.get("etag") ?? undefined,
1192
+ versionId: res.headers.get("x-amz-version-id") ?? undefined,
1193
+ contentLength: expectedContentLength(res.headers),
1194
+ contentRange: res.headers.get("content-range") ?? undefined,
1195
+ statusCode: res.status,
1196
+ destroy: (error) => {
1197
+ res.destroy(error);
1198
+ release?.();
1199
+ },
1200
+ };
1201
+ leaseHandedOff = true;
1202
+ return result;
1010
1203
  }
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
- };
1204
+ throw lastError instanceof Error
1205
+ ? lastError
1206
+ : new Error(`presigned GET ${key}: failed after ${FETCH_MAX_RETRIES} retries`);
1207
+ }
1208
+ finally {
1209
+ if (!leaseHandedOff)
1210
+ release?.();
1020
1211
  }
1021
- throw lastError instanceof Error
1022
- ? lastError
1023
- : new Error(`presigned GET ${key}: failed after ${FETCH_MAX_RETRIES} retries`);
1024
1212
  }
1025
- async getObject(key) {
1026
- const res = await this.getObjectStream(key);
1213
+ async getObject(key, options = {}) {
1214
+ const res = await this.getObjectStream(key, options);
1027
1215
  const body = await drainToBuffer(res.body);
1028
1216
  return { body, metadata: res.metadata };
1029
1217
  }
@@ -1133,6 +1321,7 @@ export class PresignObjectIO {
1133
1321
  lastModified: parseLastModified(res.headers.get("last-modified")),
1134
1322
  etag: stripQuotes(res.headers.get("etag") ?? undefined),
1135
1323
  size: Number(res.headers.get("content-length") ?? "0"),
1324
+ versionId: res.headers.get("x-amz-version-id") ?? undefined,
1136
1325
  storageClass: res.headers.get("x-amz-storage-class") ?? "STANDARD",
1137
1326
  metadata: metaFromHeaders(res.headers),
1138
1327
  };