@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/api/index.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) {
@@ -384,33 +452,29 @@ function listedPartsByteSize(parts, excludePartNumber) {
384
452
  }
385
453
  async function finalizeConfirmedObject(client, bucket, key, input) {
386
454
  const { config, route, stored, head, uploadId, acl } = input;
387
- const contentLength = requireContentLength(head);
388
- const fileName = resolveStoredFileName(head.ContentDisposition, key);
389
455
  const rawMetadata = head.Metadata ?? {};
456
+ const fileName = resolveStoredFileName(head.ContentDisposition, key);
457
+ let context;
390
458
  try {
459
+ const contentLength = requireContentLength(head);
391
460
  assertVerifiedConstraints(route.upload, {
392
461
  fileName,
393
462
  contentType: head.ContentType,
394
463
  contentLength
395
464
  });
396
- } catch (err) {
397
- await deleteObjectBestEffort(client, bucket, key);
398
- throw err;
399
- }
400
- const metadata = stripPreviousKeyMeta(rawMetadata);
401
- const context = {
402
- ...stored,
403
- contentType: head.ContentType,
404
- contentLength,
405
- eTag: head.ETag?.replace(/"/g, ""),
406
- metadata,
407
- acl,
408
- fileName,
409
- versionId: head.VersionId,
410
- lastModified: head.LastModified?.toISOString(),
411
- uploadId
412
- };
413
- try {
465
+ const metadata = stripPreviousKeyMeta(rawMetadata);
466
+ context = {
467
+ ...stored,
468
+ contentType: head.ContentType,
469
+ contentLength,
470
+ eTag: head.ETag?.replace(/"/g, ""),
471
+ metadata,
472
+ acl,
473
+ fileName,
474
+ versionId: head.VersionId,
475
+ lastModified: head.LastModified?.toISOString(),
476
+ uploadId
477
+ };
414
478
  await runLifecycleHook(route.upload.onConfirmed, context, config);
415
479
  } catch (err) {
416
480
  await deleteObjectBestEffort(client, bucket, key);
@@ -859,7 +923,17 @@ var multipartComplete = createS3Endpoint(
859
923
  async function handleMultipartInit(config, input, request) {
860
924
  const fileSize = Math.floor(input.fileSize);
861
925
  const fileName = input.fileName;
862
- const { route, key, bucket, metadata, acl, stored } = await openUploadTarget(
926
+ const {
927
+ route,
928
+ key,
929
+ bucket,
930
+ metadata,
931
+ acl,
932
+ storageClass,
933
+ cacheControl,
934
+ tagging,
935
+ stored
936
+ } = await openUploadTarget(
863
937
  config,
864
938
  {
865
939
  route: input.route,
@@ -871,6 +945,7 @@ async function handleMultipartInit(config, input, request) {
871
945
  request,
872
946
  "multipart"
873
947
  );
948
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
874
949
  await runHook(route.upload.guard, {
875
950
  ...stored,
876
951
  file: {
@@ -881,6 +956,7 @@ async function handleMultipartInit(config, input, request) {
881
956
  metadata,
882
957
  clientMetadata: input.metadata,
883
958
  acl,
959
+ ...objectS3,
884
960
  replace: route.upload.replace
885
961
  });
886
962
  const { UploadId } = await route.client.send(
@@ -890,7 +966,8 @@ async function handleMultipartInit(config, input, request) {
890
966
  ContentType: input.contentType,
891
967
  ContentDisposition: buildContentDisposition(fileName),
892
968
  Metadata: metadata,
893
- ACL: acl
969
+ ACL: acl,
970
+ ...objectCommandExtras(objectS3)
894
971
  })
895
972
  );
896
973
  if (!UploadId) {
@@ -910,12 +987,24 @@ async function handleMultipartInit(config, input, request) {
910
987
  metadata,
911
988
  clientMetadata: input.metadata,
912
989
  acl,
990
+ ...objectS3,
913
991
  replace: route.upload.replace
914
992
  },
915
993
  config
916
994
  );
917
995
  } catch (err) {
918
996
  await abortMultipartBestEffort(route.client, bucket, key, UploadId);
997
+ try {
998
+ await runLifecycleHook(
999
+ route.upload.multipart.onAbort,
1000
+ {
1001
+ ...stored,
1002
+ uploadId: UploadId
1003
+ },
1004
+ config
1005
+ );
1006
+ } catch {
1007
+ }
919
1008
  throw err;
920
1009
  }
921
1010
  return { bucket, key, uploadId: UploadId };
@@ -1046,7 +1135,17 @@ function objectUserMetadata(metadata) {
1046
1135
  async function handleUpload(config, input, request) {
1047
1136
  const fileSize = Math.floor(input.fileSize);
1048
1137
  const fileName = input.fileName;
1049
- const { route, key, bucket, metadata, acl, stored } = await openUploadTarget(
1138
+ const {
1139
+ route,
1140
+ key,
1141
+ bucket,
1142
+ metadata,
1143
+ acl,
1144
+ storageClass,
1145
+ cacheControl,
1146
+ tagging,
1147
+ stored
1148
+ } = await openUploadTarget(
1050
1149
  config,
1051
1150
  {
1052
1151
  route: input.route,
@@ -1058,6 +1157,7 @@ async function handleUpload(config, input, request) {
1058
1157
  request,
1059
1158
  "upload"
1060
1159
  );
1160
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
1061
1161
  const method = route.upload.method ?? "POST";
1062
1162
  if (route.upload.checksum && !input.checksum) {
1063
1163
  throw errors.validationError("Checksum is required for this route");
@@ -1082,6 +1182,7 @@ async function handleUpload(config, input, request) {
1082
1182
  metadata,
1083
1183
  clientMetadata: input.metadata,
1084
1184
  acl,
1185
+ ...objectS3,
1085
1186
  replace: route.upload.replace
1086
1187
  };
1087
1188
  await runHook(route.upload.guard, hookCtx);
@@ -1090,6 +1191,7 @@ async function handleUpload(config, input, request) {
1090
1191
  "Content-Type": contentType,
1091
1192
  ...objectUserMetadata(metadata),
1092
1193
  "Content-Disposition": buildContentDisposition(fileName),
1194
+ ...objectPutHeaders(objectS3),
1093
1195
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1094
1196
  };
1095
1197
  const url2 = await getSignedUrl(
@@ -1102,6 +1204,7 @@ async function handleUpload(config, input, request) {
1102
1204
  Metadata: metadata,
1103
1205
  ContentDisposition: buildContentDisposition(fileName),
1104
1206
  ContentLength: fileSize,
1207
+ ...objectCommandExtras(objectS3),
1105
1208
  ...input.checksum ? { ChecksumSHA256: input.checksum, ChecksumAlgorithm: "SHA256" } : {}
1106
1209
  }),
1107
1210
  {
@@ -1135,6 +1238,7 @@ async function handleUpload(config, input, request) {
1135
1238
  "Content-Type": contentType,
1136
1239
  "Content-Disposition": buildContentDisposition(fileName),
1137
1240
  ...objectUserMetadata(metadata),
1241
+ ...objectPutHeaders(objectS3),
1138
1242
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1139
1243
  };
1140
1244
  const { url, fields: signedFields } = await createPresignedPost(