@bymax-one/nest-storage 1.1.10 → 1.1.12

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.
@@ -1,7 +1,7 @@
1
1
  import { HttpStatus, Injectable, Inject, Global, Module, Logger, HttpException } from '@nestjs/common';
2
2
  import { S3Client, GetBucketLifecycleConfigurationCommand, DeleteBucketLifecycleCommand, PutBucketLifecycleConfigurationCommand, HeadObjectCommand, GetObjectCommand, DeleteObjectCommand, HeadBucketCommand, PutObjectCommand, ListObjectsV2Command, CopyObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, CompleteMultipartUploadCommand, AbortMultipartUploadCommand, DeleteObjectsCommand } from '@aws-sdk/client-s3';
3
3
  import { Upload } from '@aws-sdk/lib-storage';
4
- import { PassThrough, Readable } from 'stream';
4
+ import { PassThrough, Transform, pipeline, Readable } from 'stream';
5
5
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
6
6
  import { createHash } from 'crypto';
7
7
 
@@ -51,7 +51,8 @@ var STORAGE_ERROR_CODES = {
51
51
  STORAGE_PRECONDITION_FAILED: "STORAGE_PRECONDITION_FAILED",
52
52
  STORAGE_BUCKET_NOT_FOUND: "STORAGE_BUCKET_NOT_FOUND",
53
53
  STORAGE_BUCKET_ACCESS_DENIED: "STORAGE_BUCKET_ACCESS_DENIED",
54
- STORAGE_BUCKET_REGION_MISMATCH: "STORAGE_BUCKET_REGION_MISMATCH"
54
+ STORAGE_BUCKET_REGION_MISMATCH: "STORAGE_BUCKET_REGION_MISMATCH",
55
+ STORAGE_REQUEST_ABORTED: "STORAGE_REQUEST_ABORTED"
55
56
  };
56
57
 
57
58
  // src/server/errors/storage-error-messages.ts
@@ -81,8 +82,10 @@ var STORAGE_ERROR_MESSAGES = {
81
82
  [STORAGE_ERROR_CODES.STORAGE_PRECONDITION_FAILED]: "The object does not satisfy the supplied precondition",
82
83
  [STORAGE_ERROR_CODES.STORAGE_BUCKET_NOT_FOUND]: "The bucket does not exist",
83
84
  [STORAGE_ERROR_CODES.STORAGE_BUCKET_ACCESS_DENIED]: "The bucket cannot be accessed with these credentials (it may also not exist)",
84
- [STORAGE_ERROR_CODES.STORAGE_BUCKET_REGION_MISMATCH]: "The bucket belongs to a different region than the configured one"
85
+ [STORAGE_ERROR_CODES.STORAGE_BUCKET_REGION_MISMATCH]: "The bucket belongs to a different region than the configured one",
86
+ [STORAGE_ERROR_CODES.STORAGE_REQUEST_ABORTED]: "The call was aborted by its AbortSignal"
85
87
  };
88
+ var CLIENT_CLOSED_REQUEST = 499;
86
89
  var STORAGE_ERROR_STATUS = {
87
90
  [STORAGE_ERROR_CODES.STORAGE_NOT_CONFIGURED]: HttpStatus.SERVICE_UNAVAILABLE,
88
91
  [STORAGE_ERROR_CODES.STORAGE_KEY_INVALID]: HttpStatus.BAD_REQUEST,
@@ -101,7 +104,7 @@ var STORAGE_ERROR_STATUS = {
101
104
  [STORAGE_ERROR_CODES.STORAGE_BUCKET_UNDEFINED]: HttpStatus.BAD_REQUEST,
102
105
  [STORAGE_ERROR_CODES.STORAGE_MULTIPART_ABORTED]: HttpStatus.INTERNAL_SERVER_ERROR,
103
106
  [STORAGE_ERROR_CODES.STORAGE_INVALID_CONFIG]: HttpStatus.INTERNAL_SERVER_ERROR,
104
- [STORAGE_ERROR_CODES.STORAGE_TIMEOUT]: HttpStatus.GATEWAY_TIMEOUT,
107
+ [STORAGE_ERROR_CODES.STORAGE_TIMEOUT]: HttpStatus.SERVICE_UNAVAILABLE,
105
108
  [STORAGE_ERROR_CODES.STORAGE_LIFECYCLE_RULE_INVALID]: HttpStatus.BAD_REQUEST,
106
109
  [STORAGE_ERROR_CODES.STORAGE_LIFECYCLE_UNSUPPORTED]: HttpStatus.NOT_IMPLEMENTED,
107
110
  [STORAGE_ERROR_CODES.STORAGE_LIFECYCLE_REJECTED]: HttpStatus.UNPROCESSABLE_ENTITY,
@@ -109,7 +112,8 @@ var STORAGE_ERROR_STATUS = {
109
112
  [STORAGE_ERROR_CODES.STORAGE_PRECONDITION_FAILED]: HttpStatus.PRECONDITION_FAILED,
110
113
  [STORAGE_ERROR_CODES.STORAGE_BUCKET_NOT_FOUND]: HttpStatus.NOT_FOUND,
111
114
  [STORAGE_ERROR_CODES.STORAGE_BUCKET_ACCESS_DENIED]: HttpStatus.FORBIDDEN,
112
- [STORAGE_ERROR_CODES.STORAGE_BUCKET_REGION_MISMATCH]: HttpStatus.INTERNAL_SERVER_ERROR
115
+ [STORAGE_ERROR_CODES.STORAGE_BUCKET_REGION_MISMATCH]: HttpStatus.INTERNAL_SERVER_ERROR,
116
+ [STORAGE_ERROR_CODES.STORAGE_REQUEST_ABORTED]: CLIENT_CLOSED_REQUEST
113
117
  };
114
118
 
115
119
  // src/server/errors/storage-exception.ts
@@ -130,6 +134,9 @@ var StorageException = class extends HttpException {
130
134
  }
131
135
  };
132
136
 
137
+ // src/server/constants/timer.constants.ts
138
+ var MAX_TIMER_MS = 2147483647;
139
+
133
140
  // src/server/utils/header-value.ts
134
141
  var HEADER_VALUE = /^[\t \x21-\x7E]*$/;
135
142
  var HEADER_NAME = /^[!#$%&'*+.^_`|~0-9a-z-]+$/;
@@ -178,6 +185,34 @@ function isObject(value) {
178
185
  function isNonEmptyString(value) {
179
186
  return typeof value === "string" && value.length > 0;
180
187
  }
188
+ var BEFORE_AUTHORITY = /^(?:[a-z][a-z\d+.-]*:)?[/\\]*/i;
189
+ function asParsed(value) {
190
+ return value.replace(/[\t\n\r]/g, "").replace(/^[\p{Cc} ]+/u, "");
191
+ }
192
+ function parsedCarriesCredentials(value) {
193
+ try {
194
+ const url = new URL(value);
195
+ return url.username !== "" || url.password !== "";
196
+ } catch {
197
+ return false;
198
+ }
199
+ }
200
+ function authorityCarriesCredentials(text) {
201
+ const rest = text.replace(BEFORE_AUTHORITY, "");
202
+ const end = rest.search(/[/?#]/);
203
+ return (end === -1 ? rest : rest.slice(0, end)).includes("@");
204
+ }
205
+ function urlCarriesCredentials(value) {
206
+ return parsedCarriesCredentials(value) || authorityCarriesCredentials(asParsed(value));
207
+ }
208
+ function assertTimerMs(options, name, min) {
209
+ const value = options[name];
210
+ if (value !== void 0 && (typeof value !== "number" || !Number.isInteger(value) || value < min || value > MAX_TIMER_MS)) {
211
+ throw invalid(
212
+ `${name} must be an integer from ${String(min)} to ${String(MAX_TIMER_MS)}` + (min === 0 ? " (0 disables the guard)" : "")
213
+ );
214
+ }
215
+ }
181
216
  function validateOptions(options) {
182
217
  if (!isObject(options)) {
183
218
  throw invalid("options object is required");
@@ -185,6 +220,12 @@ function validateOptions(options) {
185
220
  if (!isNonEmptyString(options.endpoint)) {
186
221
  throw invalid("options.endpoint must be a non-empty string");
187
222
  }
223
+ for (const name of ["endpoint", "publicBaseUrl", "cdnBaseUrl"]) {
224
+ const value = options[name];
225
+ if (typeof value === "string" && urlCarriesCredentials(value)) {
226
+ throw invalid(`options.${name} must not carry credentials; pass them in options.credentials`);
227
+ }
228
+ }
188
229
  if (!isNonEmptyString(options.region)) {
189
230
  throw invalid("options.region must be a non-empty string");
190
231
  }
@@ -202,10 +243,13 @@ function validateOptions(options) {
202
243
  if (isObject(multipart) && typeof multipart.partSizeBytes === "number" && multipart.partSizeBytes < MIN_PART_SIZE_BYTES) {
203
244
  throw invalid("multipart.partSizeBytes must be >= 5 MB (S3 hard limit)");
204
245
  }
205
- const idle = options.uploadIdleTimeoutMs;
206
- if (idle !== void 0 && (typeof idle !== "number" || !Number.isInteger(idle) || idle < 0)) {
207
- throw invalid("uploadIdleTimeoutMs must be an integer >= 0 (0 disables the guard)");
246
+ const maxAttempts = options.maxAttempts;
247
+ if (maxAttempts !== void 0 && (typeof maxAttempts !== "number" || !Number.isInteger(maxAttempts) || maxAttempts < 1)) {
248
+ throw invalid("maxAttempts must be an integer >= 1");
208
249
  }
250
+ assertTimerMs(options, "requestTimeoutMs", 1);
251
+ assertTimerMs(options, "uploadIdleTimeoutMs", 0);
252
+ assertTimerMs(options, "bodyIdleTimeoutMs", 0);
209
253
  if (options.serverSideEncryption === "aws:kms" && !options.kmsKeyId) {
210
254
  throw invalid('kmsKeyId is required when serverSideEncryption === "aws:kms"');
211
255
  }
@@ -287,6 +331,7 @@ function applyDefaults(options) {
287
331
  maxAttempts: options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS,
288
332
  requestTimeoutMs,
289
333
  uploadIdleTimeoutMs: options.uploadIdleTimeoutMs ?? requestTimeoutMs,
334
+ bodyIdleTimeoutMs: options.bodyIdleTimeoutMs ?? requestTimeoutMs,
290
335
  hasCredentials
291
336
  };
292
337
  Object.defineProperty(resolved, "credentials", {
@@ -296,6 +341,18 @@ function applyDefaults(options) {
296
341
  });
297
342
  return resolved;
298
343
  }
344
+
345
+ // src/server/utils/describe-endpoint.ts
346
+ function describeEndpoint(endpoint) {
347
+ try {
348
+ const url = new URL(endpoint);
349
+ return `${url.protocol}//${url.host}`;
350
+ } catch {
351
+ return "<unparseable endpoint>";
352
+ }
353
+ }
354
+
355
+ // src/server/providers/s3-client.provider.ts
299
356
  var S3ClientProvider = class {
300
357
  constructor(options) {
301
358
  this.options = options;
@@ -381,7 +438,7 @@ var S3ClientProvider = class {
381
438
  responseChecksumValidation: this.options.responseChecksumValidation
382
439
  };
383
440
  this.logger.log(
384
- `S3Client initialized: endpoint=${this.options.endpoint} region=${this.options.region}`
441
+ `S3Client initialized: endpoint=${describeEndpoint(this.options.endpoint)} region=${this.options.region}`
385
442
  );
386
443
  return new S3Client(config);
387
444
  }
@@ -450,6 +507,57 @@ KeyResolverService = __decorateClass([
450
507
  Injectable(),
451
508
  __decorateParam(0, Inject(BYMAX_STORAGE_OPTIONS))
452
509
  ], KeyResolverService);
510
+ var UNREACHABLE_CODES = /* @__PURE__ */ new Set([
511
+ "ECONNREFUSED",
512
+ "ECONNRESET",
513
+ "ECONNABORTED",
514
+ "ENOTFOUND",
515
+ "EAI_AGAIN",
516
+ "EHOSTUNREACH",
517
+ "ENETUNREACH",
518
+ "EPIPE",
519
+ "ETIMEDOUT"
520
+ ]);
521
+ var UNREACHABLE_IN_MESSAGE = new RegExp(
522
+ `^(?:AWS SDK error wrapper for \\w+: )?(?:(?:connect|read|write|getaddrinfo) (?:${[...UNREACHABLE_CODES].join("|")})\\b|socket hang up)`
523
+ );
524
+ var ADDRESSES_FAILED = "AWS SDK error wrapper for AggregateError";
525
+ var CREDENTIAL_CODES = /* @__PURE__ */ new Set(["InvalidAccessKeyId", "SignatureDoesNotMatch"]);
526
+ function cutConnection(failure, noAnswer) {
527
+ const codes = [failure.code, failure.cause?.code];
528
+ if (Array.isArray(failure.errors)) {
529
+ codes.push(...failure.errors.map((inner) => inner.code));
530
+ }
531
+ if (codes.some((code) => typeof code === "string" && UNREACHABLE_CODES.has(code))) {
532
+ return true;
533
+ }
534
+ const message = typeof failure.message === "string" ? failure.message : "";
535
+ return noAnswer && (UNREACHABLE_IN_MESSAGE.test(message) || message.startsWith(ADDRESSES_FAILED));
536
+ }
537
+ function classifyProviderFailure(err) {
538
+ const failure = err ?? {};
539
+ const status = failure.$metadata?.httpStatusCode;
540
+ if (failure.name === "TimeoutError" || cutConnection(failure, status === void 0)) {
541
+ return "unavailable";
542
+ }
543
+ if (typeof status === "number" && status >= 500 && status !== 501) {
544
+ return "unavailable";
545
+ }
546
+ const awsCode = failure.Code ?? failure.name;
547
+ if (status === 403 && typeof awsCode === "string" && CREDENTIAL_CODES.has(awsCode)) {
548
+ return "credentials-refused";
549
+ }
550
+ return "other";
551
+ }
552
+ function providerFailureStatus(err) {
553
+ const failure = classifyProviderFailure(err);
554
+ if (failure === "unavailable") {
555
+ return HttpStatus.SERVICE_UNAVAILABLE;
556
+ }
557
+ return failure === "credentials-refused" ? HttpStatus.INTERNAL_SERVER_ERROR : void 0;
558
+ }
559
+
560
+ // src/server/errors/aws-error-mapper.ts
453
561
  function mapAwsError(err, context) {
454
562
  const e = err ?? {};
455
563
  const httpStatus = e.$metadata?.httpStatusCode;
@@ -487,11 +595,11 @@ function mapAwsError(err, context) {
487
595
  );
488
596
  }
489
597
  if (e.name === "TimeoutError") {
490
- return new StorageException(STORAGE_ERROR_CODES.STORAGE_TIMEOUT, HttpStatus.GATEWAY_TIMEOUT, details);
598
+ return new StorageException(STORAGE_ERROR_CODES.STORAGE_TIMEOUT, void 0, details);
491
599
  }
492
600
  return new StorageException(
493
601
  STORAGE_ERROR_CODES.STORAGE_PROVIDER_ERROR,
494
- HttpStatus.BAD_GATEWAY,
602
+ providerFailureStatus(err),
495
603
  details
496
604
  );
497
605
  }
@@ -860,6 +968,25 @@ async function readStreamUpTo(source, maxBytes) {
860
968
  }
861
969
  return Buffer.concat(chunks);
862
970
  }
971
+ async function readStream(source) {
972
+ const chunks = [];
973
+ for await (const chunk of source) {
974
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
975
+ }
976
+ return Buffer.concat(chunks);
977
+ }
978
+ function toUploadBody(body) {
979
+ if (typeof body === "string") {
980
+ return Buffer.from(body, "utf8");
981
+ }
982
+ if (body instanceof ArrayBuffer) {
983
+ return Buffer.from(body);
984
+ }
985
+ if (ArrayBuffer.isView(body) && !(body instanceof Uint8Array)) {
986
+ return Buffer.from(body.buffer, body.byteOffset, body.byteLength);
987
+ }
988
+ return body;
989
+ }
863
990
 
864
991
  // src/server/utils/mime-match.ts
865
992
  var SINGLE_MEDIA_TYPE = /^[ \t]*[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+[ \t]*(?:;[\t \x21-\x2B\x2D-\x7E]*)?$/;
@@ -991,6 +1118,38 @@ ValidationService = __decorateClass([
991
1118
  __decorateParam(0, Inject(BYMAX_STORAGE_OPTIONS)),
992
1119
  __decorateParam(1, Inject(BYMAX_STORAGE_UPLOAD_VALIDATORS))
993
1120
  ], ValidationService);
1121
+
1122
+ // src/server/utils/describe-error.ts
1123
+ var NON_PRINTABLE_REJECTION = "non-printable rejection value";
1124
+ function describeError(value) {
1125
+ try {
1126
+ if (typeof value === "object" && value !== null) {
1127
+ const message = value.message;
1128
+ if (typeof message === "string") {
1129
+ return message;
1130
+ }
1131
+ }
1132
+ return String(value);
1133
+ } catch {
1134
+ return NON_PRINTABLE_REJECTION;
1135
+ }
1136
+ }
1137
+
1138
+ // src/server/utils/log-value.ts
1139
+ var LEFT_LITERAL_BY_JSON = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu;
1140
+ function logValue(value) {
1141
+ const text = typeof value === "string" ? value : describeError(value);
1142
+ return JSON.stringify(text).replace(
1143
+ LEFT_LITERAL_BY_JSON,
1144
+ (char) => {
1145
+ const codePoint = Number(char.codePointAt(0));
1146
+ const hex = codePoint.toString(16);
1147
+ return codePoint > 65535 ? `\\u{${hex}}` : `\\u${hex.padStart(4, "0")}`;
1148
+ }
1149
+ );
1150
+ }
1151
+
1152
+ // src/server/services/file-scanner.service.ts
994
1153
  var FileScannerService = class {
995
1154
  constructor(options, scanner) {
996
1155
  this.options = options;
@@ -1042,7 +1201,7 @@ var FileScannerService = class {
1042
1201
  }
1043
1202
  const result = await this.scanner.scan(input);
1044
1203
  if (result.status === "infected") {
1045
- this.logger.warn(`Infected file detected: key=${input.key} engine=${result.engine} threat=${result.threat ?? "unknown"}`);
1204
+ this.logger.warn(`Infected file detected: key=${logValue(input.key)} engine=${logValue(result.engine)} threat=${logValue(result.threat ?? "unknown")}`);
1046
1205
  throw new StorageException(STORAGE_ERROR_CODES.STORAGE_SCAN_INFECTED, void 0, {
1047
1206
  engine: result.engine,
1048
1207
  threat: result.threat,
@@ -1056,7 +1215,7 @@ var FileScannerService = class {
1056
1215
  details: result.details
1057
1216
  });
1058
1217
  }
1059
- this.logger.warn(`Inconclusive scan result accepted: key=${input.key} engine=${result.engine}`);
1218
+ this.logger.warn(`Inconclusive scan result accepted: key=${logValue(input.key)} engine=${logValue(result.engine)}`);
1060
1219
  }
1061
1220
  return result;
1062
1221
  }
@@ -1066,22 +1225,6 @@ FileScannerService = __decorateClass([
1066
1225
  __decorateParam(0, Inject(BYMAX_STORAGE_OPTIONS)),
1067
1226
  __decorateParam(1, Inject(BYMAX_STORAGE_FILE_SCANNER))
1068
1227
  ], FileScannerService);
1069
-
1070
- // src/server/utils/describe-error.ts
1071
- var NON_PRINTABLE_REJECTION = "non-printable rejection value";
1072
- function describeError(value) {
1073
- try {
1074
- if (typeof value === "object" && value !== null) {
1075
- const message = value.message;
1076
- if (typeof message === "string") {
1077
- return message;
1078
- }
1079
- }
1080
- return String(value);
1081
- } catch {
1082
- return NON_PRINTABLE_REJECTION;
1083
- }
1084
- }
1085
1228
  var SourceIdleError = class extends Error {
1086
1229
  constructor(idleMs) {
1087
1230
  super(`The upload source delivered no bytes for ${String(idleMs)} ms`);
@@ -1107,7 +1250,7 @@ function withinIdleTime(next, idleMs) {
1107
1250
  );
1108
1251
  });
1109
1252
  }
1110
- function withIdleTimeout(source, idleMs) {
1253
+ function withIdleTimeout(source, idleMs, toError) {
1111
1254
  const iterator = source[Symbol.asyncIterator]();
1112
1255
  const wrapper = new Readable({
1113
1256
  objectMode: false,
@@ -1120,7 +1263,7 @@ function withIdleTimeout(source, idleMs) {
1120
1263
  wrapper.push(step.done === true ? null : step.value);
1121
1264
  },
1122
1265
  (error) => {
1123
- wrapper.destroy(error);
1266
+ wrapper.destroy(toError ? toError(error) : error);
1124
1267
  }
1125
1268
  );
1126
1269
  },
@@ -1138,24 +1281,69 @@ function guardIdleBody(body, idleMs) {
1138
1281
  }
1139
1282
  return idleMs === 0 ? body : withIdleTimeout(body, idleMs);
1140
1283
  }
1284
+ var SizeLimitError = class extends Error {
1285
+ constructor(limitBytes, kind) {
1286
+ super(`The upload body is longer than ${String(limitBytes)} bytes`);
1287
+ this.limitBytes = limitBytes;
1288
+ this.kind = kind;
1289
+ this.name = "SizeLimitError";
1290
+ }
1291
+ limitBytes;
1292
+ kind;
1293
+ };
1294
+ function withSizeLimit(source, limitBytes, kind) {
1295
+ let seen = 0;
1296
+ const guard = new Transform({
1297
+ transform(chunk, _encoding, callback) {
1298
+ seen += Buffer.byteLength(chunk);
1299
+ if (seen > limitBytes) {
1300
+ callback(new SizeLimitError(limitBytes, kind));
1301
+ return;
1302
+ }
1303
+ callback(null, chunk);
1304
+ }
1305
+ });
1306
+ pipeline(source, guard, () => void 0);
1307
+ return guard;
1308
+ }
1309
+
1310
+ // src/server/errors/size-limit-exception.ts
1311
+ function sizeLimitException(limitBytes, kind, context) {
1312
+ const bound = kind === "declared" ? { declaredSize: limitBytes } : { maxSize: limitBytes };
1313
+ return new StorageException(STORAGE_ERROR_CODES.STORAGE_SIZE_EXCEEDED, void 0, {
1314
+ ...context,
1315
+ ...bound
1316
+ });
1317
+ }
1318
+ function mapSizeLimitError(err, context) {
1319
+ if (!(err instanceof SizeLimitError)) {
1320
+ return void 0;
1321
+ }
1322
+ return sizeLimitException(err.limitBytes, err.kind, context);
1323
+ }
1141
1324
 
1142
1325
  // src/server/errors/multipart-error-mapper.ts
1143
1326
  var ABORT_FAILED_MESSAGE = "Multipart upload failed and could not be aborted";
1144
1327
  function mapMultipartError(err, context) {
1145
1328
  if (err instanceof SourceIdleError) {
1146
- return new StorageException(STORAGE_ERROR_CODES.STORAGE_TIMEOUT, HttpStatus.GATEWAY_TIMEOUT, {
1329
+ return new StorageException(STORAGE_ERROR_CODES.STORAGE_TIMEOUT, void 0, {
1147
1330
  ...context,
1148
1331
  phase: "source-idle",
1149
1332
  idleMs: err.idleMs
1150
1333
  });
1151
1334
  }
1335
+ const tooLong = mapSizeLimitError(err, context);
1336
+ if (tooLong) {
1337
+ return tooLong;
1338
+ }
1152
1339
  const bucketMissing = mapAwsError(err, context);
1153
1340
  if (bucketMissing.code === STORAGE_ERROR_CODES.STORAGE_BUCKET_NOT_FOUND) {
1154
1341
  return bucketMissing;
1155
1342
  }
1156
1343
  return new StorageException(
1157
1344
  STORAGE_ERROR_CODES.STORAGE_MULTIPART_ABORTED,
1158
- void 0,
1345
+ // An outage ends the upload as an outage; any other cause is a failure of the upload itself.
1346
+ providerFailureStatus(err),
1159
1347
  { ...context, awsMessage: describeError(err) },
1160
1348
  // The code's message says the upload was aborted; it is not, when the abort itself failed.
1161
1349
  context.abort === "failed" ? ABORT_FAILED_MESSAGE : void 0
@@ -1186,13 +1374,103 @@ function withCleanupFailure(scanError, keys, cleanupError) {
1186
1374
  cleanupError: describeCleanupError(cleanupError)
1187
1375
  });
1188
1376
  }
1377
+ var CLEANUP_DEADLINE_MS = 1e4;
1378
+ function reasonName(signal) {
1379
+ const reason = signal.reason;
1380
+ if (typeof reason === "object" && reason !== null && "name" in reason && typeof reason.name === "string") {
1381
+ return reason.name;
1382
+ }
1383
+ return "AbortError";
1384
+ }
1385
+ function requestAborted(signal, context) {
1386
+ return new StorageException(STORAGE_ERROR_CODES.STORAGE_REQUEST_ABORTED, void 0, {
1387
+ ...context,
1388
+ reason: reasonName(signal)
1389
+ });
1390
+ }
1391
+ function throwIfAborted(signal, context) {
1392
+ if (signal?.aborted) {
1393
+ throw requestAborted(signal, context);
1394
+ }
1395
+ }
1396
+ function sendOptions(signal) {
1397
+ return signal ? { abortSignal: signal } : void 0;
1398
+ }
1399
+ function failureOrAbort(signal, context, mapped) {
1400
+ return signal?.aborted ? requestAborted(signal, context) : mapped;
1401
+ }
1402
+ function bindToSignal(client, signal) {
1403
+ let created = Promise.resolve(void 0);
1404
+ const bound = new Proxy(client, {
1405
+ get(target, property) {
1406
+ if (property !== "send") {
1407
+ const value = Reflect.get(target, property, target);
1408
+ return value;
1409
+ }
1410
+ return (command, options) => {
1411
+ if (command instanceof CreateMultipartUploadCommand) {
1412
+ const sent = target.send(command, options);
1413
+ created = sent.then(
1414
+ (response) => response.UploadId,
1415
+ () => void 0
1416
+ );
1417
+ return sent;
1418
+ }
1419
+ return target.send(command, { ...options, abortSignal: signal });
1420
+ };
1421
+ }
1422
+ });
1423
+ return { client: bound, createdUploadId: () => created };
1424
+ }
1425
+ async function within(promise, ms) {
1426
+ let timer;
1427
+ const late = new Promise((resolve) => {
1428
+ timer = setTimeout(() => {
1429
+ resolve(void 0);
1430
+ }, ms);
1431
+ });
1432
+ try {
1433
+ return await Promise.race([promise, late]);
1434
+ } finally {
1435
+ clearTimeout(timer);
1436
+ }
1437
+ }
1438
+ async function abortable(work, signal, context) {
1439
+ if (!signal) {
1440
+ return work;
1441
+ }
1442
+ if (signal.aborted) {
1443
+ work.catch(() => void 0);
1444
+ throw requestAborted(signal, context);
1445
+ }
1446
+ const release = new AbortController();
1447
+ const aborted = new Promise((_resolve, reject) => {
1448
+ signal.addEventListener(
1449
+ "abort",
1450
+ () => {
1451
+ reject(requestAborted(signal, context));
1452
+ },
1453
+ // Stryker disable next-line BooleanLiteral: `release.abort()` in the `finally` removes the listener, and a signal aborts once, so `once` is not observable (equivalent).
1454
+ { once: true, signal: release.signal }
1455
+ );
1456
+ });
1457
+ try {
1458
+ return await Promise.race([work, aborted]);
1459
+ } finally {
1460
+ release.abort();
1461
+ work.catch(() => void 0);
1462
+ }
1463
+ }
1464
+
1465
+ // src/server/utils/multipart-abort.ts
1189
1466
  async function closeMultipartUpload(client, bucket, key, uploadId) {
1190
1467
  if (uploadId === void 0) {
1191
1468
  return {};
1192
1469
  }
1193
1470
  try {
1194
1471
  await client.send(
1195
- new AbortMultipartUploadCommand({ Bucket: bucket, Key: key, UploadId: uploadId })
1472
+ new AbortMultipartUploadCommand({ Bucket: bucket, Key: key, UploadId: uploadId }),
1473
+ { abortSignal: AbortSignal.timeout(CLEANUP_DEADLINE_MS) }
1196
1474
  );
1197
1475
  return { abort: "completed" };
1198
1476
  } catch (err) {
@@ -1202,6 +1480,9 @@ async function closeMultipartUpload(client, bucket, key, uploadId) {
1202
1480
  return { abort: "failed", abortError: describeError(err) };
1203
1481
  }
1204
1482
  }
1483
+ function closeWhenCreated(client, bucket, key, created) {
1484
+ void created.then((uploadId) => closeMultipartUpload(client, bucket, key, uploadId));
1485
+ }
1205
1486
  async function deleteObjectsChunk(client, bucket, chunk, stripPrefix) {
1206
1487
  const deleted = [];
1207
1488
  const failed = [];
@@ -1320,6 +1601,141 @@ function pickUploadStrategy(body, declaredSize, thresholdBytes) {
1320
1601
  return "single-shot";
1321
1602
  }
1322
1603
 
1604
+ // src/server/utils/upload-body.ts
1605
+ function withBytesBody(options) {
1606
+ const body = toUploadBody(options.body);
1607
+ return isBufferLike(body) ? { ...options, body, size: body.byteLength } : { ...options, body };
1608
+ }
1609
+ function boundStreamSize(body, declaredSize, options) {
1610
+ const limit = declaredSize ?? options.validation?.maxSizeBytes;
1611
+ const singleShot = pickUploadStrategy(body, declaredSize, options.multipart.thresholdBytes) === "single-shot";
1612
+ if (isBufferLike(body) || singleShot || limit === void 0) {
1613
+ return body;
1614
+ }
1615
+ return withSizeLimit(body, limit, declaredSize === void 0 ? "max" : "declared");
1616
+ }
1617
+
1618
+ // src/server/utils/download-body.ts
1619
+ function guardDownloadBody(body, idleMs, context, signal) {
1620
+ if (idleMs === 0) {
1621
+ return body;
1622
+ }
1623
+ return withIdleTimeout(body, idleMs, (error) => {
1624
+ if (error instanceof SourceIdleError) {
1625
+ return new StorageException(STORAGE_ERROR_CODES.STORAGE_TIMEOUT, void 0, {
1626
+ ...context,
1627
+ phase: "body-idle",
1628
+ idleMs: error.idleMs
1629
+ });
1630
+ }
1631
+ return signal?.aborted ? requestAborted(signal, context) : error;
1632
+ });
1633
+ }
1634
+
1635
+ // src/server/utils/public-url.ts
1636
+ function buildPublicUrl(options, finalKey, bucket) {
1637
+ const base = trimTrailingSlashes(options.cdnBaseUrl ?? options.publicBaseUrl);
1638
+ return baseCarriesBucket(base, bucket) ? `${base}/${finalKey}` : `${base}/${bucket}/${finalKey}`;
1639
+ }
1640
+ function baseCarriesBucket(base, bucket) {
1641
+ const schemeless = base.replace(/^[a-z][a-z\d+.-]*:\/\//i, "");
1642
+ const pathStart = schemeless.indexOf("/");
1643
+ const authority = pathStart === -1 ? schemeless : schemeless.slice(0, pathStart);
1644
+ const path = pathStart === -1 ? "" : schemeless.slice(pathStart);
1645
+ const leadingHostLabel = authority.split(".")[0];
1646
+ const pathSegments = path.split("/").filter((segment) => segment.length > 0);
1647
+ return leadingHostLabel === bucket || pathSegments.includes(bucket);
1648
+ }
1649
+
1650
+ // src/server/utils/upload-support.ts
1651
+ function normalizeScanBody(body) {
1652
+ return Buffer.isBuffer(body) ? body : Buffer.from(body);
1653
+ }
1654
+ function forwardProgress(uploader, onProgress) {
1655
+ if (!onProgress) return;
1656
+ uploader.on("httpUploadProgress", (event) => {
1657
+ emitProgress(onProgress, event.loaded, event.total, event.part);
1658
+ });
1659
+ }
1660
+ function normalizeSingleShotBody(body) {
1661
+ if (isBufferLike(body)) {
1662
+ return Buffer.isBuffer(body) ? body : Buffer.from(body);
1663
+ }
1664
+ return body;
1665
+ }
1666
+ function buildUploadResult(keyResolver, options, params) {
1667
+ return {
1668
+ key: keyResolver.stripPrefix(params.finalKey),
1669
+ bucket: params.bucket,
1670
+ etag: params.etag ?? "",
1671
+ contentType: params.contentType,
1672
+ publicUrl: buildPublicUrl(options, params.finalKey, params.bucket),
1673
+ multipart: params.multipart,
1674
+ fromIdempotencyCache: false,
1675
+ ...params.size !== void 0 ? { size: params.size } : {},
1676
+ ...params.versionId !== void 0 ? { versionId: params.versionId } : {}
1677
+ };
1678
+ }
1679
+ function emitProgress(onProgress, loaded, total, part) {
1680
+ onProgress({
1681
+ loaded: loaded ?? 0,
1682
+ ...total !== void 0 ? { total } : {},
1683
+ ...part !== void 0 ? { part } : {}
1684
+ });
1685
+ }
1686
+ function putObjectHeaders(options, resolved, finalKey, bucket) {
1687
+ return {
1688
+ Bucket: bucket,
1689
+ Key: finalKey,
1690
+ ContentType: options.contentType,
1691
+ CacheControl: buildCacheControl(options.cacheControl, resolved.defaultCacheControl),
1692
+ ContentDisposition: buildContentDisposition(
1693
+ options.contentDisposition,
1694
+ resolved.defaultContentDisposition
1695
+ ),
1696
+ // `public-read` only takes effect on buckets that allow ACLs (see header-utils).
1697
+ ACL: buildACL(options.publicRead, resolved.defaultPublicRead),
1698
+ Metadata: options.metadata,
1699
+ ...buildSSE(options.serverSideEncryption, options.kmsKeyId, resolved)
1700
+ };
1701
+ }
1702
+ function validationInput(options, finalKey) {
1703
+ return {
1704
+ key: finalKey,
1705
+ body: options.body,
1706
+ contentType: options.contentType,
1707
+ ...options.size !== void 0 ? { size: options.size } : {},
1708
+ ...options.metadata !== void 0 ? { metadata: options.metadata } : {},
1709
+ cacheControl: options.cacheControl,
1710
+ contentDisposition: options.contentDisposition,
1711
+ kmsKeyId: options.kmsKeyId
1712
+ };
1713
+ }
1714
+
1715
+ // src/server/utils/download-buffer.ts
1716
+ function assertMaxBytes(maxBytes) {
1717
+ if (maxBytes !== void 0 && (typeof maxBytes !== "number" || !Number.isInteger(maxBytes) || maxBytes < 1)) {
1718
+ throw new StorageException(STORAGE_ERROR_CODES.STORAGE_VALIDATION_FAILED, void 0, {
1719
+ reason: "maxBytes must be an integer >= 1"
1720
+ });
1721
+ }
1722
+ }
1723
+ async function readDownloadBody(stream, metadata, maxBytes) {
1724
+ if (maxBytes === void 0) {
1725
+ return readStream(stream);
1726
+ }
1727
+ const context = { key: metadata.key, bucket: metadata.bucket };
1728
+ if (metadata.size > maxBytes) {
1729
+ destroyStream(stream);
1730
+ throw sizeLimitException(maxBytes, "max", { ...context, contentLength: metadata.size });
1731
+ }
1732
+ const buffer = await readStreamUpTo(stream, maxBytes);
1733
+ if (buffer === void 0) {
1734
+ throw sizeLimitException(maxBytes, "max", context);
1735
+ }
1736
+ return buffer;
1737
+ }
1738
+
1323
1739
  // src/server/services/storage.service.ts
1324
1740
  var MAX_LIST_KEYS = 1e3;
1325
1741
  var DELETE_BATCH_LIMIT = 1e3;
@@ -1363,17 +1779,19 @@ var StorageService = class {
1363
1779
  }
1364
1780
  }
1365
1781
  /** The upload itself; `upload()` destroys a stream body when this rejects. */
1366
- async uploadOwningBody(options) {
1782
+ async uploadOwningBody(requested) {
1367
1783
  this.assertConfigured();
1368
- const rawBody = options.body;
1784
+ const rawBody = requested.body;
1369
1785
  if (rawBody === void 0 || rawBody === null) {
1370
1786
  throw new StorageException(STORAGE_ERROR_CODES.STORAGE_BODY_MISSING);
1371
1787
  }
1788
+ const options = withBytesBody(requested);
1372
1789
  if (!options.contentType) {
1373
1790
  throw new StorageException(STORAGE_ERROR_CODES.STORAGE_CONTENT_TYPE_REQUIRED);
1374
1791
  }
1375
1792
  const finalKey = this.keyResolver.normalize(options.key);
1376
1793
  const bucket = this.resolveBucket(options.bucket);
1794
+ throwIfAborted(options.abortSignal, { key: finalKey, bucket, op: "upload" });
1377
1795
  if (options.idempotencyKey) {
1378
1796
  const cacheKey = this.idempotencyCache.computeKey(options.idempotencyKey, finalKey);
1379
1797
  const cached = this.idempotencyCache.get(cacheKey);
@@ -1383,32 +1801,24 @@ var StorageService = class {
1383
1801
  return { ...cached, fromIdempotencyCache: true };
1384
1802
  }
1385
1803
  }
1386
- const validated = await this.validation.validate({
1387
- key: finalKey,
1388
- body: options.body,
1389
- contentType: options.contentType,
1390
- ...options.size !== void 0 ? { size: options.size } : {},
1391
- ...options.metadata !== void 0 ? { metadata: options.metadata } : {},
1392
- cacheControl: options.cacheControl,
1393
- contentDisposition: options.contentDisposition,
1394
- kmsKeyId: options.kmsKeyId
1395
- });
1396
- const body = await this.bufferForPreUploadScan(validated.body, finalKey);
1804
+ const context = { key: finalKey, bucket, op: "upload" };
1805
+ const signal = options.abortSignal;
1806
+ const input = validationInput(options, finalKey);
1807
+ const validated = await abortable(this.validation.validate(input), signal, context);
1808
+ const scanned = await abortable(this.bufferForPreUploadScan(validated.body, finalKey), signal, context);
1809
+ const body = boundStreamSize(scanned, options.size, this.options);
1397
1810
  const validatedOptions = { ...options, body };
1398
- await this.runPreUploadScan(body, finalKey, bucket, options);
1811
+ await abortable(this.runPreUploadScan(body, finalKey, bucket, options), signal, context);
1812
+ throwIfAborted(signal, context);
1399
1813
  const strategy = pickUploadStrategy(
1400
1814
  validatedOptions.body,
1401
1815
  validatedOptions.size,
1402
1816
  this.options.multipart.thresholdBytes
1403
1817
  );
1404
1818
  const result = strategy === "multipart" ? await this.uploadMultipart(validatedOptions, finalKey, bucket) : await this.uploadSingleShot(validatedOptions, finalKey, bucket);
1405
- await this.runPostUploadScan(finalKey, bucket, options);
1406
- if (options.idempotencyKey) {
1407
- this.idempotencyCache.set(
1408
- this.idempotencyCache.computeKey(options.idempotencyKey, finalKey),
1409
- result
1410
- );
1411
- }
1819
+ const stored = { ...context, stored: true };
1820
+ await abortable(this.runPostUploadScan(finalKey, bucket, options), signal, stored);
1821
+ this.rememberUpload(options, finalKey, result);
1412
1822
  return result;
1413
1823
  }
1414
1824
  /**
@@ -1447,20 +1857,20 @@ var StorageService = class {
1447
1857
  }
1448
1858
  await this.scanner.scan({
1449
1859
  mode: "pre-upload",
1450
- body: this.normalizeScanBody(body),
1860
+ body: normalizeScanBody(body),
1451
1861
  key: finalKey,
1452
1862
  bucket,
1453
1863
  contentType: options.contentType,
1454
1864
  ...options.size !== void 0 ? { size: options.size } : {}
1455
1865
  });
1456
1866
  }
1457
- /**
1458
- * Coerces the scan body to a `Buffer` so the scanner always receives its declared
1459
- * contract type. In pre-upload mode a stream has already been buffered by
1460
- * {@link bufferForPreUploadScan}, so only `Buffer` and `Uint8Array` arrive here.
1461
- */
1462
- normalizeScanBody(body) {
1463
- return Buffer.isBuffer(body) ? body : Buffer.from(body);
1867
+ /** Remembers the result of an upload under its idempotency key, when it has one. */
1868
+ rememberUpload(options, finalKey, result) {
1869
+ if (!options.idempotencyKey) return;
1870
+ this.idempotencyCache.set(
1871
+ this.idempotencyCache.computeKey(options.idempotencyKey, finalKey),
1872
+ result
1873
+ );
1464
1874
  }
1465
1875
  /**
1466
1876
  * Runs a post-upload scan. On infection, deletes the just-uploaded object
@@ -1481,11 +1891,11 @@ var StorageService = class {
1481
1891
  } catch (scanErr) {
1482
1892
  const key = this.keyResolver.stripPrefix(finalKey);
1483
1893
  try {
1484
- await this.delete(key, { bucket });
1894
+ await this.delete(key, { bucket, abortSignal: AbortSignal.timeout(CLEANUP_DEADLINE_MS) });
1485
1895
  } catch (deleteErr) {
1486
1896
  this.logger.error(
1487
- `STORAGE_SCAN_CLEANUP_FAILED: the rejected object is still stored: ${finalKey}`,
1488
- describeError(deleteErr)
1897
+ `STORAGE_SCAN_CLEANUP_FAILED: the rejected object is still stored: ${logValue(finalKey)}`,
1898
+ logValue(describeError(deleteErr))
1489
1899
  );
1490
1900
  throw withCleanupFailure(scanErr, { key, storedKey: finalKey }, deleteErr);
1491
1901
  }
@@ -1504,11 +1914,13 @@ var StorageService = class {
1504
1914
  this.assertConfigured();
1505
1915
  const finalKey = this.keyResolver.normalize(key);
1506
1916
  const bucket = this.resolveBucket(options?.bucket);
1917
+ const context = { key: finalKey, bucket, op: "head" };
1918
+ throwIfAborted(options?.abortSignal, context);
1507
1919
  try {
1508
- const response = await this.s3Provider.getClient().send(new HeadObjectCommand({ Bucket: bucket, Key: finalKey }));
1920
+ const response = await this.s3Provider.getClient().send(new HeadObjectCommand({ Bucket: bucket, Key: finalKey }), sendOptions(options?.abortSignal));
1509
1921
  return this.toObjectMetadata(response, finalKey, bucket);
1510
1922
  } catch (err) {
1511
- throw mapAwsError(err, { key: finalKey, bucket, op: "head" });
1923
+ throw failureOrAbort(options?.abortSignal, context, mapAwsError(err, context));
1512
1924
  }
1513
1925
  }
1514
1926
  /**
@@ -1527,7 +1939,10 @@ var StorageService = class {
1527
1939
  if (err instanceof StorageException && err.code === STORAGE_ERROR_CODES.STORAGE_OBJECT_NOT_FOUND) {
1528
1940
  return false;
1529
1941
  }
1530
- this.logger.warn(`exists() \u2014 treating a non-404 error as "false": ${describeError(err)}`);
1942
+ if (err instanceof StorageException && err.code === STORAGE_ERROR_CODES.STORAGE_REQUEST_ABORTED) {
1943
+ throw err;
1944
+ }
1945
+ this.logger.warn(`exists() \u2014 treating a non-404 error as "false": ${logValue(describeError(err))}`);
1531
1946
  return false;
1532
1947
  }
1533
1948
  }
@@ -1542,7 +1957,7 @@ var StorageService = class {
1542
1957
  getPublicUrl(key, options) {
1543
1958
  const finalKey = this.keyResolver.normalize(key);
1544
1959
  const bucket = this.resolveBucket(options?.bucket);
1545
- return this.buildPublicUrl(finalKey, bucket);
1960
+ return buildPublicUrl(this.options, finalKey, bucket);
1546
1961
  }
1547
1962
  /**
1548
1963
  * Streams an object. The returned `stream` is a Node `Readable` consumable via
@@ -1558,6 +1973,8 @@ var StorageService = class {
1558
1973
  assertHeaderValues({ range: options.range, ifNoneMatch: options.ifNoneMatch, ifMatch: options.ifMatch });
1559
1974
  const finalKey = this.keyResolver.normalize(options.key);
1560
1975
  const bucket = this.resolveBucket(options.bucket);
1976
+ const context = { key: finalKey, bucket, op: "download" };
1977
+ throwIfAborted(options.abortSignal, context);
1561
1978
  try {
1562
1979
  const response = await this.s3Provider.getClient().send(
1563
1980
  new GetObjectCommand({
@@ -1566,7 +1983,8 @@ var StorageService = class {
1566
1983
  Range: options.range,
1567
1984
  IfNoneMatch: options.ifNoneMatch,
1568
1985
  IfMatch: options.ifMatch
1569
- })
1986
+ }),
1987
+ sendOptions(options.abortSignal)
1570
1988
  );
1571
1989
  if (!response.Body) {
1572
1990
  throw new StorageException(STORAGE_ERROR_CODES.STORAGE_OBJECT_NOT_FOUND, void 0, {
@@ -1574,26 +1992,33 @@ var StorageService = class {
1574
1992
  bucket
1575
1993
  });
1576
1994
  }
1577
- const stream = response.Body;
1995
+ const stream = guardDownloadBody(
1996
+ response.Body,
1997
+ this.options.bodyIdleTimeoutMs,
1998
+ { key: finalKey, bucket },
1999
+ options.abortSignal
2000
+ );
1578
2001
  return { stream, metadata: this.toObjectMetadata(response, finalKey, bucket) };
1579
2002
  } catch (err) {
1580
2003
  if (err instanceof StorageException) {
1581
2004
  throw err;
1582
2005
  }
1583
- throw mapAwsError(err, { key: finalKey, bucket, op: "download" });
2006
+ throw failureOrAbort(options.abortSignal, context, mapAwsError(err, context));
1584
2007
  }
1585
2008
  }
1586
2009
  /**
1587
- * Materializes an object fully into memory via the sdk-stream-mixin. NOT
1588
- * recommended for files larger than 10 MB — use {@link download} for those.
2010
+ * Reads an object fully into memory through the same idle-guarded stream as {@link download}.
2011
+ * Without `maxBytes` the whole object is held, however large; use {@link download} for large ones.
1589
2012
  *
1590
- * @param options - The download request.
2013
+ * @param options - The download request; `maxBytes` is the most bytes to hold.
1591
2014
  * @returns The object buffer and its metadata.
2015
+ * @throws StorageException `STORAGE_SIZE_EXCEEDED` past `maxBytes`, by `Content-Length` or by
2016
+ * bytes; `STORAGE_VALIDATION_FAILED` for a `maxBytes` that is not an integer of at least 1.
1592
2017
  */
1593
2018
  async downloadBuffer(options) {
2019
+ assertMaxBytes(options.maxBytes);
1594
2020
  const { stream, metadata } = await this.download(options);
1595
- const bytes = await stream.transformToByteArray();
1596
- return { buffer: Buffer.from(bytes), metadata };
2021
+ return { buffer: await readDownloadBody(stream, metadata, options.maxBytes), metadata };
1597
2022
  }
1598
2023
  /**
1599
2024
  * Deletes an object. Idempotent: a missing object is a no-op (logged as a
@@ -1606,14 +2031,16 @@ var StorageService = class {
1606
2031
  this.assertConfigured();
1607
2032
  const finalKey = this.keyResolver.normalize(key);
1608
2033
  const bucket = this.resolveBucket(options?.bucket);
2034
+ const context = { key: finalKey, bucket, op: "delete" };
2035
+ throwIfAborted(options?.abortSignal, context);
1609
2036
  try {
1610
- await this.s3Provider.getClient().send(new DeleteObjectCommand({ Bucket: bucket, Key: finalKey }));
2037
+ await this.s3Provider.getClient().send(new DeleteObjectCommand({ Bucket: bucket, Key: finalKey }), sendOptions(options?.abortSignal));
1611
2038
  } catch (err) {
1612
- const mapped = mapAwsError(err, { key: finalKey, bucket, op: "delete" });
2039
+ const mapped = failureOrAbort(options?.abortSignal, context, mapAwsError(err, context));
1613
2040
  if (mapped.code !== STORAGE_ERROR_CODES.STORAGE_OBJECT_NOT_FOUND) {
1614
2041
  throw mapped;
1615
2042
  }
1616
- this.logger.warn(`delete() \u2014 key not found (idempotent no-op): ${finalKey}`);
2043
+ this.logger.warn(`delete() \u2014 key not found (idempotent no-op): ${logValue(finalKey)}`);
1617
2044
  }
1618
2045
  this.idempotencyCache.invalidate(bucket, this.keyResolver.stripPrefix(finalKey));
1619
2046
  }
@@ -1631,10 +2058,12 @@ var StorageService = class {
1631
2058
  async checkBucket(options) {
1632
2059
  this.assertConfigured();
1633
2060
  const bucket = this.resolveBucket(options?.bucket);
2061
+ const context = { bucket, op: "checkBucket" };
2062
+ throwIfAborted(options?.abortSignal, context);
1634
2063
  try {
1635
- await this.s3Provider.getClient().send(new HeadBucketCommand({ Bucket: bucket }));
2064
+ await this.s3Provider.getClient().send(new HeadBucketCommand({ Bucket: bucket }), sendOptions(options?.abortSignal));
1636
2065
  } catch (err) {
1637
- throw mapBucketCheckError(err, { bucket, op: "checkBucket" });
2066
+ throw failureOrAbort(options?.abortSignal, context, mapBucketCheckError(err, context));
1638
2067
  }
1639
2068
  }
1640
2069
  /** Throws when the S3 client was never built (missing credentials). */
@@ -1651,26 +2080,6 @@ var StorageService = class {
1651
2080
  }
1652
2081
  return bucket;
1653
2082
  }
1654
- /** Builds a public URL, avoiding a duplicated bucket segment in the base. */
1655
- buildPublicUrl(finalKey, bucket) {
1656
- const base = trimTrailingSlashes(this.options.cdnBaseUrl ?? this.options.publicBaseUrl);
1657
- return this.baseCarriesBucket(base, bucket) ? `${base}/${finalKey}` : `${base}/${bucket}/${finalKey}`;
1658
- }
1659
- /**
1660
- * Reports whether the base URL already carries the bucket as a real segment —
1661
- * the leading host label (virtual-hosted-style `bucket.host`) or a path segment
1662
- * (path-style `/bucket` or `/bucket/…`) — rather than an incidental substring,
1663
- * so `bucket="test"` is not matched by a `latest` elsewhere in the base.
1664
- */
1665
- baseCarriesBucket(base, bucket) {
1666
- const schemeless = base.replace(/^[a-z][a-z\d+.-]*:\/\//i, "");
1667
- const pathStart = schemeless.indexOf("/");
1668
- const authority = pathStart === -1 ? schemeless : schemeless.slice(0, pathStart);
1669
- const path = pathStart === -1 ? "" : schemeless.slice(pathStart);
1670
- const leadingHostLabel = authority.split(".")[0];
1671
- const pathSegments = path.split("/").filter((segment) => segment.length > 0);
1672
- return leadingHostLabel === bucket || pathSegments.includes(bucket);
1673
- }
1674
2083
  /** Maps an S3 metadata response onto the public `ObjectMetadata` shape. */
1675
2084
  toObjectMetadata(response, finalKey, bucket) {
1676
2085
  return {
@@ -1691,27 +2100,16 @@ var StorageService = class {
1691
2100
  async uploadSingleShot(options, finalKey, bucket) {
1692
2101
  const total = options.size ?? getBodySize(options.body);
1693
2102
  const input = {
1694
- Bucket: bucket,
1695
- Key: finalKey,
1696
- Body: this.normalizeSingleShotBody(options.body),
1697
- ContentType: options.contentType,
1698
- ContentLength: total,
1699
- CacheControl: buildCacheControl(options.cacheControl, this.options.defaultCacheControl),
1700
- ContentDisposition: buildContentDisposition(
1701
- options.contentDisposition,
1702
- this.options.defaultContentDisposition
1703
- ),
1704
- // `public-read` only takes effect on buckets that allow ACLs (see header-utils).
1705
- ACL: buildACL(options.publicRead, this.options.defaultPublicRead),
1706
- Metadata: options.metadata,
1707
- ...buildSSE(options.serverSideEncryption, options.kmsKeyId, this.options)
2103
+ ...putObjectHeaders(options, this.options, finalKey, bucket),
2104
+ Body: normalizeSingleShotBody(options.body),
2105
+ ContentLength: total
1708
2106
  };
1709
2107
  try {
1710
- const response = await this.s3Provider.getClient().send(new PutObjectCommand(input));
2108
+ const response = await this.s3Provider.getClient().send(new PutObjectCommand(input), sendOptions(options.abortSignal));
1711
2109
  if (options.onProgress) {
1712
- this.emitProgress(options.onProgress, total, total);
2110
+ emitProgress(options.onProgress, total, total);
1713
2111
  }
1714
- return this.buildUploadResult({
2112
+ return buildUploadResult(this.keyResolver, this.options, {
1715
2113
  finalKey,
1716
2114
  bucket,
1717
2115
  etag: response.ETag,
@@ -1721,7 +2119,8 @@ var StorageService = class {
1721
2119
  multipart: false
1722
2120
  });
1723
2121
  } catch (err) {
1724
- throw mapAwsError(err, { key: finalKey, bucket, op: "upload-single" });
2122
+ const context = { key: finalKey, bucket, op: "upload-single" };
2123
+ throw failureOrAbort(options.abortSignal, context, mapAwsError(err, context));
1725
2124
  }
1726
2125
  }
1727
2126
  /**
@@ -1732,30 +2131,26 @@ var StorageService = class {
1732
2131
  */
1733
2132
  async uploadMultipart(options, finalKey, bucket) {
1734
2133
  const params = {
1735
- Bucket: bucket,
1736
- Key: finalKey,
1737
- Body: guardIdleBody(options.body, this.options.uploadIdleTimeoutMs),
1738
- ContentType: options.contentType,
1739
- CacheControl: buildCacheControl(options.cacheControl, this.options.defaultCacheControl),
1740
- ContentDisposition: buildContentDisposition(
1741
- options.contentDisposition,
1742
- this.options.defaultContentDisposition
1743
- ),
1744
- ACL: buildACL(options.publicRead, this.options.defaultPublicRead),
1745
- Metadata: options.metadata,
1746
- ...buildSSE(options.serverSideEncryption, options.kmsKeyId, this.options)
2134
+ ...putObjectHeaders(options, this.options, finalKey, bucket),
2135
+ Body: guardIdleBody(options.body, this.options.uploadIdleTimeoutMs)
1747
2136
  };
2137
+ const signal = options.abortSignal;
2138
+ const bound = signal ? bindToSignal(this.s3Provider.getClient(), signal) : void 0;
1748
2139
  const uploader = new Upload({
1749
- client: this.s3Provider.getClient(),
2140
+ client: bound?.client ?? this.s3Provider.getClient(),
1750
2141
  params,
1751
2142
  queueSize: this.options.multipart.queueSize,
1752
2143
  partSize: this.options.multipart.partSizeBytes,
1753
2144
  leavePartsOnError: true
1754
2145
  });
1755
- this.forwardProgress(uploader, options.onProgress);
2146
+ forwardProgress(uploader, options.onProgress);
2147
+ const abortUpload = () => {
2148
+ void uploader.abort();
2149
+ };
2150
+ signal?.addEventListener("abort", abortUpload, { once: true });
1756
2151
  try {
1757
2152
  const response = await uploader.done();
1758
- return this.buildUploadResult({
2153
+ return buildUploadResult(this.keyResolver, this.options, {
1759
2154
  finalKey,
1760
2155
  bucket,
1761
2156
  etag: response.ETag,
@@ -1765,54 +2160,33 @@ var StorageService = class {
1765
2160
  multipart: true
1766
2161
  });
1767
2162
  } catch (err) {
1768
- return this.failMultipart(err, finalKey, bucket, uploader.uploadId);
2163
+ return await this.failMultipart(err, finalKey, bucket, {
2164
+ uploadId: uploader.uploadId,
2165
+ signal,
2166
+ createdUploadId: bound?.createdUploadId
2167
+ });
2168
+ } finally {
2169
+ signal?.removeEventListener("abort", abortUpload);
1769
2170
  }
1770
2171
  }
1771
- /** Relays the uploader's progress events to the caller's callback, when there is one. */
1772
- forwardProgress(uploader, onProgress) {
1773
- if (!onProgress) return;
1774
- uploader.on("httpUploadProgress", (event) => {
1775
- this.emitProgress(onProgress, event.loaded, event.total, event.part);
1776
- });
1777
- }
1778
- /** Aborts the multipart upload that failed, then throws the original failure with the outcome. */
1779
- async failMultipart(err, finalKey, bucket, uploadId) {
1780
- const abort = await closeMultipartUpload(
1781
- this.s3Provider.getClient(),
1782
- bucket,
1783
- finalKey,
1784
- uploadId
1785
- );
1786
- throw mapMultipartError(err, { key: finalKey, bucket, ...abort });
1787
- }
1788
- /** Converts a `Uint8Array` body to a `Buffer` for SDK safety; passes others through. */
1789
- normalizeSingleShotBody(body) {
1790
- if (isBufferLike(body)) {
1791
- return Buffer.isBuffer(body) ? body : Buffer.from(body);
2172
+ /**
2173
+ * Aborts the multipart upload that failed, then throws the original failure with the outcome. Whether
2174
+ * the failure is an abort is decided first: a signal that fires during the cleanup does not turn a
2175
+ * real failure into one. An upload id the provider answered late is waited for, within the cleanup
2176
+ * deadline, so that an abort during `CreateMultipartUpload` still aborts the upload it created.
2177
+ */
2178
+ async failMultipart(err, finalKey, bucket, upload) {
2179
+ const abortedBy = upload.signal?.aborted === true ? upload.signal : void 0;
2180
+ const client = this.s3Provider.getClient();
2181
+ const lookup = abortedBy ? upload.createdUploadId?.() : void 0;
2182
+ let uploadId = upload.uploadId;
2183
+ if (uploadId === void 0 && lookup) {
2184
+ uploadId = await within(lookup, CLEANUP_DEADLINE_MS);
2185
+ if (uploadId === void 0) closeWhenCreated(client, bucket, finalKey, lookup);
1792
2186
  }
1793
- return body;
1794
- }
1795
- /** Assembles an `UploadResult`, omitting optional fields that are absent. */
1796
- buildUploadResult(params) {
1797
- return {
1798
- key: this.keyResolver.stripPrefix(params.finalKey),
1799
- bucket: params.bucket,
1800
- etag: params.etag ?? "",
1801
- contentType: params.contentType,
1802
- publicUrl: this.buildPublicUrl(params.finalKey, params.bucket),
1803
- multipart: params.multipart,
1804
- fromIdempotencyCache: false,
1805
- ...params.size !== void 0 ? { size: params.size } : {},
1806
- ...params.versionId !== void 0 ? { versionId: params.versionId } : {}
1807
- };
1808
- }
1809
- /** Emits a progress event, omitting `total`/`part` when they are unknown. */
1810
- emitProgress(onProgress, loaded, total, part) {
1811
- onProgress({
1812
- loaded: loaded ?? 0,
1813
- ...total !== void 0 ? { total } : {},
1814
- ...part !== void 0 ? { part } : {}
1815
- });
2187
+ const abort = await closeMultipartUpload(client, bucket, finalKey, uploadId);
2188
+ const context = { key: finalKey, bucket, op: "upload-multipart", ...abort };
2189
+ throw abortedBy ? requestAborted(abortedBy, context) : mapMultipartError(err, context);
1816
2190
  }
1817
2191
  /**
1818
2192
  * Lists objects in the bucket as a single page. Applies the global key prefix
@@ -1828,6 +2202,8 @@ var StorageService = class {
1828
2202
  const bucket = this.resolveBucket(options.bucket);
1829
2203
  const maxKeys = Math.min(options.maxKeys ?? MAX_LIST_KEYS, MAX_LIST_KEYS);
1830
2204
  const prefix = options.prefix ? this.keyResolver.normalize(options.prefix) : this.keyResolver.getPrefix();
2205
+ const context = { bucket, prefix, op: "list" };
2206
+ throwIfAborted(options.abortSignal, context);
1831
2207
  try {
1832
2208
  const response = await this.s3Provider.getClient().send(
1833
2209
  new ListObjectsV2Command({
@@ -1836,11 +2212,12 @@ var StorageService = class {
1836
2212
  MaxKeys: maxKeys,
1837
2213
  ContinuationToken: options.continuationToken,
1838
2214
  Delimiter: options.delimiter
1839
- })
2215
+ }),
2216
+ sendOptions(options.abortSignal)
1840
2217
  );
1841
2218
  return toListResult(response, (key) => this.keyResolver.stripPrefix(key));
1842
2219
  } catch (err) {
1843
- throw mapAwsError(err, { bucket, prefix, op: "list" });
2220
+ throw failureOrAbort(options.abortSignal, context, mapAwsError(err, context));
1844
2221
  }
1845
2222
  }
1846
2223
  /**