@dimah-s3/server 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/plugins.js CHANGED
@@ -169,6 +169,73 @@ function previousKeyFromMetadata(metadata) {
169
169
  return value || void 0;
170
170
  }
171
171
 
172
+ // src/helpers/object-s3.ts
173
+ var MAX_S3_TAGS = 10;
174
+ var MAX_TAG_KEY_CHARS = 128;
175
+ var MAX_TAG_VALUE_CHARS = 256;
176
+ function present(value) {
177
+ const trimmed = value?.trim();
178
+ return trimmed ? trimmed : void 0;
179
+ }
180
+ function assertTagEntry(key, value) {
181
+ if (key.length === 0) {
182
+ throw errors.validationError("S3 object tag keys cannot be empty");
183
+ }
184
+ if (key.length > MAX_TAG_KEY_CHARS) {
185
+ throw errors.validationError(
186
+ `S3 object tag keys cannot exceed ${MAX_TAG_KEY_CHARS} characters`
187
+ );
188
+ }
189
+ if (value.length > MAX_TAG_VALUE_CHARS) {
190
+ throw errors.validationError(
191
+ `S3 object tag values cannot exceed ${MAX_TAG_VALUE_CHARS} characters`
192
+ );
193
+ }
194
+ }
195
+ function encodeObjectTagging(tagging) {
196
+ const entries = Object.entries(tagging);
197
+ if (entries.length > MAX_S3_TAGS) {
198
+ throw errors.validationError(
199
+ `S3 object tagging allows at most ${MAX_S3_TAGS} tags`
200
+ );
201
+ }
202
+ return entries.map(([key, value]) => {
203
+ assertTagEntry(key, value);
204
+ return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
205
+ }).join("&");
206
+ }
207
+ function taggingHeader(tagging) {
208
+ if (!tagging || Object.keys(tagging).length === 0) return void 0;
209
+ return encodeObjectTagging(tagging);
210
+ }
211
+ function normalizeObjectS3(info) {
212
+ const storageClass = present(info?.storageClass);
213
+ const cacheControl = present(info?.cacheControl);
214
+ const tagging = info?.tagging && Object.keys(info.tagging).length > 0 ? info.tagging : void 0;
215
+ if (tagging) encodeObjectTagging(tagging);
216
+ return {
217
+ ...storageClass ? { storageClass } : {},
218
+ ...cacheControl ? { cacheControl } : {},
219
+ ...tagging ? { tagging } : {}
220
+ };
221
+ }
222
+ function objectCommandExtras(opts) {
223
+ const tagging = taggingHeader(opts.tagging);
224
+ return {
225
+ ...opts.storageClass ? { StorageClass: opts.storageClass } : {},
226
+ ...opts.cacheControl ? { CacheControl: opts.cacheControl } : {},
227
+ ...tagging ? { Tagging: tagging } : {}
228
+ };
229
+ }
230
+ function objectPutHeaders(opts) {
231
+ const tagging = taggingHeader(opts.tagging);
232
+ return {
233
+ ...opts.cacheControl ? { "Cache-Control": opts.cacheControl } : {},
234
+ ...opts.storageClass ? { "x-amz-storage-class": opts.storageClass } : {},
235
+ ...tagging ? { "x-amz-tagging": tagging } : {}
236
+ };
237
+ }
238
+
172
239
  // src/helpers/resolve-target.ts
173
240
  function assertSafeObjectKey(key) {
174
241
  const normalized = normalizeObjectKey(key);
@@ -231,7 +298,8 @@ async function resolveUploadTarget(route, context) {
231
298
  key,
232
299
  bucket,
233
300
  metadata: Object.keys(metadata).length > 0 ? metadata : info?.metadata,
234
- acl: resolveAcl(info, route)
301
+ acl: resolveAcl(info, route),
302
+ ...normalizeObjectS3(info)
235
303
  };
236
304
  }
237
305
  function resolveStoredTarget(route, key) {
@@ -512,33 +580,29 @@ function listedPartsByteSize(parts, excludePartNumber) {
512
580
  }
513
581
  async function finalizeConfirmedObject(client, bucket, key, input) {
514
582
  const { config, route, stored, head, uploadId, acl } = input;
515
- const contentLength = requireContentLength(head);
516
- const fileName = resolveStoredFileName(head.ContentDisposition, key);
517
583
  const rawMetadata = head.Metadata ?? {};
584
+ const fileName = resolveStoredFileName(head.ContentDisposition, key);
585
+ let context;
518
586
  try {
587
+ const contentLength = requireContentLength(head);
519
588
  assertVerifiedConstraints(route.upload, {
520
589
  fileName,
521
590
  contentType: head.ContentType,
522
591
  contentLength
523
592
  });
524
- } catch (err) {
525
- await deleteObjectBestEffort(client, bucket, key);
526
- throw err;
527
- }
528
- const metadata = stripPreviousKeyMeta(rawMetadata);
529
- const context = {
530
- ...stored,
531
- contentType: head.ContentType,
532
- contentLength,
533
- eTag: head.ETag?.replace(/"/g, ""),
534
- metadata,
535
- acl,
536
- fileName,
537
- versionId: head.VersionId,
538
- lastModified: head.LastModified?.toISOString(),
539
- uploadId
540
- };
541
- try {
593
+ const metadata = stripPreviousKeyMeta(rawMetadata);
594
+ context = {
595
+ ...stored,
596
+ contentType: head.ContentType,
597
+ contentLength,
598
+ eTag: head.ETag?.replace(/"/g, ""),
599
+ metadata,
600
+ acl,
601
+ fileName,
602
+ versionId: head.VersionId,
603
+ lastModified: head.LastModified?.toISOString(),
604
+ uploadId
605
+ };
542
606
  await runLifecycleHook(route.upload.onConfirmed, context, config);
543
607
  } catch (err) {
544
608
  await deleteObjectBestEffort(client, bucket, key);
@@ -987,7 +1051,17 @@ var multipartComplete = createS3Endpoint(
987
1051
  async function handleMultipartInit(config, input, request) {
988
1052
  const fileSize = Math.floor(input.fileSize);
989
1053
  const fileName = input.fileName;
990
- const { route, key, bucket, metadata, acl, stored } = await openUploadTarget(
1054
+ const {
1055
+ route,
1056
+ key,
1057
+ bucket,
1058
+ metadata,
1059
+ acl,
1060
+ storageClass,
1061
+ cacheControl,
1062
+ tagging,
1063
+ stored
1064
+ } = await openUploadTarget(
991
1065
  config,
992
1066
  {
993
1067
  route: input.route,
@@ -999,6 +1073,7 @@ async function handleMultipartInit(config, input, request) {
999
1073
  request,
1000
1074
  "multipart"
1001
1075
  );
1076
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
1002
1077
  await runHook(route.upload.guard, {
1003
1078
  ...stored,
1004
1079
  file: {
@@ -1009,6 +1084,7 @@ async function handleMultipartInit(config, input, request) {
1009
1084
  metadata,
1010
1085
  clientMetadata: input.metadata,
1011
1086
  acl,
1087
+ ...objectS3,
1012
1088
  replace: route.upload.replace
1013
1089
  });
1014
1090
  const { UploadId } = await route.client.send(
@@ -1018,7 +1094,8 @@ async function handleMultipartInit(config, input, request) {
1018
1094
  ContentType: input.contentType,
1019
1095
  ContentDisposition: buildContentDisposition(fileName),
1020
1096
  Metadata: metadata,
1021
- ACL: acl
1097
+ ACL: acl,
1098
+ ...objectCommandExtras(objectS3)
1022
1099
  })
1023
1100
  );
1024
1101
  if (!UploadId) {
@@ -1038,12 +1115,24 @@ async function handleMultipartInit(config, input, request) {
1038
1115
  metadata,
1039
1116
  clientMetadata: input.metadata,
1040
1117
  acl,
1118
+ ...objectS3,
1041
1119
  replace: route.upload.replace
1042
1120
  },
1043
1121
  config
1044
1122
  );
1045
1123
  } catch (err) {
1046
1124
  await abortMultipartBestEffort(route.client, bucket, key, UploadId);
1125
+ try {
1126
+ await runLifecycleHook(
1127
+ route.upload.multipart.onAbort,
1128
+ {
1129
+ ...stored,
1130
+ uploadId: UploadId
1131
+ },
1132
+ config
1133
+ );
1134
+ } catch {
1135
+ }
1047
1136
  throw err;
1048
1137
  }
1049
1138
  return { bucket, key, uploadId: UploadId };
@@ -1174,7 +1263,17 @@ function objectUserMetadata(metadata) {
1174
1263
  async function handleUpload(config, input, request) {
1175
1264
  const fileSize = Math.floor(input.fileSize);
1176
1265
  const fileName = input.fileName;
1177
- const { route, key, bucket, metadata, acl, stored } = await openUploadTarget(
1266
+ const {
1267
+ route,
1268
+ key,
1269
+ bucket,
1270
+ metadata,
1271
+ acl,
1272
+ storageClass,
1273
+ cacheControl,
1274
+ tagging,
1275
+ stored
1276
+ } = await openUploadTarget(
1178
1277
  config,
1179
1278
  {
1180
1279
  route: input.route,
@@ -1186,6 +1285,7 @@ async function handleUpload(config, input, request) {
1186
1285
  request,
1187
1286
  "upload"
1188
1287
  );
1288
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
1189
1289
  const method = route.upload.method ?? "POST";
1190
1290
  if (route.upload.checksum && !input.checksum) {
1191
1291
  throw errors.validationError("Checksum is required for this route");
@@ -1210,6 +1310,7 @@ async function handleUpload(config, input, request) {
1210
1310
  metadata,
1211
1311
  clientMetadata: input.metadata,
1212
1312
  acl,
1313
+ ...objectS3,
1213
1314
  replace: route.upload.replace
1214
1315
  };
1215
1316
  await runHook(route.upload.guard, hookCtx);
@@ -1218,6 +1319,7 @@ async function handleUpload(config, input, request) {
1218
1319
  "Content-Type": contentType,
1219
1320
  ...objectUserMetadata(metadata),
1220
1321
  "Content-Disposition": buildContentDisposition(fileName),
1322
+ ...objectPutHeaders(objectS3),
1221
1323
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1222
1324
  };
1223
1325
  const url2 = await getSignedUrl(
@@ -1230,6 +1332,7 @@ async function handleUpload(config, input, request) {
1230
1332
  Metadata: metadata,
1231
1333
  ContentDisposition: buildContentDisposition(fileName),
1232
1334
  ContentLength: fileSize,
1335
+ ...objectCommandExtras(objectS3),
1233
1336
  ...input.checksum ? { ChecksumSHA256: input.checksum, ChecksumAlgorithm: "SHA256" } : {}
1234
1337
  }),
1235
1338
  {
@@ -1263,6 +1366,7 @@ async function handleUpload(config, input, request) {
1263
1366
  "Content-Type": contentType,
1264
1367
  "Content-Disposition": buildContentDisposition(fileName),
1265
1368
  ...objectUserMetadata(metadata),
1369
+ ...objectPutHeaders(objectS3),
1266
1370
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1267
1371
  };
1268
1372
  const { url, fields: signedFields } = await createPresignedPost(
@@ -1373,10 +1477,10 @@ var RESERVED_PLUGIN_IDS = [
1373
1477
 
1374
1478
  // src/plugin/apply-plugins.ts
1375
1479
  function mergeHookField(pluginHooks, userHook, order = "plugins-first") {
1376
- const present = (order === "user-first" ? [userHook, ...pluginHooks] : [...pluginHooks, userHook]).filter((h) => h != null);
1377
- if (present.length === 0) return void 0;
1378
- if (present.length === 1) return present[0];
1379
- return chainHooks(...present);
1480
+ const present2 = (order === "user-first" ? [userHook, ...pluginHooks] : [...pluginHooks, userHook]).filter((h) => h != null);
1481
+ if (present2.length === 0) return void 0;
1482
+ if (present2.length === 1) return present2[0];
1483
+ return chainHooks(...present2);
1380
1484
  }
1381
1485
  function mergeHooks(keys, plugins, userFeature, pick, lifecycleKeys = /* @__PURE__ */ new Set()) {
1382
1486
  const merged = { ...userFeature };