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