@dimah-s3/server 1.4.2 → 1.5.1

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) {
@@ -983,7 +1051,17 @@ var multipartComplete = createS3Endpoint(
983
1051
  async function handleMultipartInit(config, input, request) {
984
1052
  const fileSize = Math.floor(input.fileSize);
985
1053
  const fileName = input.fileName;
986
- 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(
987
1065
  config,
988
1066
  {
989
1067
  route: input.route,
@@ -995,6 +1073,7 @@ async function handleMultipartInit(config, input, request) {
995
1073
  request,
996
1074
  "multipart"
997
1075
  );
1076
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
998
1077
  await runHook(route.upload.guard, {
999
1078
  ...stored,
1000
1079
  file: {
@@ -1005,6 +1084,7 @@ async function handleMultipartInit(config, input, request) {
1005
1084
  metadata,
1006
1085
  clientMetadata: input.metadata,
1007
1086
  acl,
1087
+ ...objectS3,
1008
1088
  replace: route.upload.replace
1009
1089
  });
1010
1090
  const { UploadId } = await route.client.send(
@@ -1014,7 +1094,8 @@ async function handleMultipartInit(config, input, request) {
1014
1094
  ContentType: input.contentType,
1015
1095
  ContentDisposition: buildContentDisposition(fileName),
1016
1096
  Metadata: metadata,
1017
- ACL: acl
1097
+ ACL: acl,
1098
+ ...objectCommandExtras(objectS3)
1018
1099
  })
1019
1100
  );
1020
1101
  if (!UploadId) {
@@ -1034,6 +1115,7 @@ async function handleMultipartInit(config, input, request) {
1034
1115
  metadata,
1035
1116
  clientMetadata: input.metadata,
1036
1117
  acl,
1118
+ ...objectS3,
1037
1119
  replace: route.upload.replace
1038
1120
  },
1039
1121
  config
@@ -1181,7 +1263,17 @@ function objectUserMetadata(metadata) {
1181
1263
  async function handleUpload(config, input, request) {
1182
1264
  const fileSize = Math.floor(input.fileSize);
1183
1265
  const fileName = input.fileName;
1184
- 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(
1185
1277
  config,
1186
1278
  {
1187
1279
  route: input.route,
@@ -1193,6 +1285,7 @@ async function handleUpload(config, input, request) {
1193
1285
  request,
1194
1286
  "upload"
1195
1287
  );
1288
+ const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
1196
1289
  const method = route.upload.method ?? "POST";
1197
1290
  if (route.upload.checksum && !input.checksum) {
1198
1291
  throw errors.validationError("Checksum is required for this route");
@@ -1217,6 +1310,7 @@ async function handleUpload(config, input, request) {
1217
1310
  metadata,
1218
1311
  clientMetadata: input.metadata,
1219
1312
  acl,
1313
+ ...objectS3,
1220
1314
  replace: route.upload.replace
1221
1315
  };
1222
1316
  await runHook(route.upload.guard, hookCtx);
@@ -1225,6 +1319,7 @@ async function handleUpload(config, input, request) {
1225
1319
  "Content-Type": contentType,
1226
1320
  ...objectUserMetadata(metadata),
1227
1321
  "Content-Disposition": buildContentDisposition(fileName),
1322
+ ...objectPutHeaders(objectS3),
1228
1323
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1229
1324
  };
1230
1325
  const url2 = await getSignedUrl(
@@ -1237,6 +1332,7 @@ async function handleUpload(config, input, request) {
1237
1332
  Metadata: metadata,
1238
1333
  ContentDisposition: buildContentDisposition(fileName),
1239
1334
  ContentLength: fileSize,
1335
+ ...objectCommandExtras(objectS3),
1240
1336
  ...input.checksum ? { ChecksumSHA256: input.checksum, ChecksumAlgorithm: "SHA256" } : {}
1241
1337
  }),
1242
1338
  {
@@ -1270,6 +1366,7 @@ async function handleUpload(config, input, request) {
1270
1366
  "Content-Type": contentType,
1271
1367
  "Content-Disposition": buildContentDisposition(fileName),
1272
1368
  ...objectUserMetadata(metadata),
1369
+ ...objectPutHeaders(objectS3),
1273
1370
  ...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
1274
1371
  };
1275
1372
  const { url, fields: signedFields } = await createPresignedPost(
@@ -1380,10 +1477,10 @@ var RESERVED_PLUGIN_IDS = [
1380
1477
 
1381
1478
  // src/plugin/apply-plugins.ts
1382
1479
  function mergeHookField(pluginHooks, userHook, order = "plugins-first") {
1383
- const present = (order === "user-first" ? [userHook, ...pluginHooks] : [...pluginHooks, userHook]).filter((h) => h != null);
1384
- if (present.length === 0) return void 0;
1385
- if (present.length === 1) return present[0];
1386
- 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);
1387
1484
  }
1388
1485
  function mergeHooks(keys, plugins, userFeature, pick, lifecycleKeys = /* @__PURE__ */ new Set()) {
1389
1486
  const merged = { ...userFeature };