@dimah-s3/server 1.5.10 → 1.6.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
@@ -1,4 +1,4 @@
1
- import { S3_API_ROUTES, isAPIError, APIError, S3_ERROR_CODES, confirmBodySchema, deleteQuerySchema, deleteBatchBodySchema, downloadQuerySchema, fileQuerySchema, multipartAbortBodySchema, multipartCompleteBodySchema, multipartInitBodySchema, multipartListPartsQuerySchema, multipartSignPartBodySchema, uploadBodySchema, S3_MAX_EXPIRES_IN, buildContentDisposition, S3_MAX_POST_OBJECT_BYTES, resolveStoredFileName, S3_DEFAULT_EXPIRES_IN, matchesFileTypes, ROUTE_NAME_PATTERN, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
1
+ import { S3_API_ROUTES, isAPIError, APIError, S3_ERROR_CODES, confirmBodySchema, deleteQuerySchema, deleteBatchBodySchema, downloadQuerySchema, fileQuerySchema, multipartAbortBodySchema, multipartCompleteBodySchema, multipartInitBodySchema, multipartListPartsQuerySchema, multipartSignPartBodySchema, uploadBodySchema, buildContentDisposition, S3_MAX_EXPIRES_IN, S3_MAX_POST_OBJECT_BYTES, resolveStoredFileName, S3_DEFAULT_EXPIRES_IN, matchesFileTypes, ROUTE_NAME_PATTERN, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
2
2
  import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
3
3
  import { createMiddleware, createEndpoint } from 'better-call';
4
4
  import * as z from 'zod';
@@ -102,6 +102,15 @@ function normalizeExpiresIn(value, maxExpiresIn = S3_MAX_EXPIRES_IN) {
102
102
  const max = Number.isFinite(maxExpiresIn) && maxExpiresIn > 0 ? Math.floor(maxExpiresIn) : S3_MAX_EXPIRES_IN;
103
103
  return Math.min(expires, max);
104
104
  }
105
+ function clampDownloadExpiresIn(options) {
106
+ const max = options.maxExpiresIn;
107
+ if (options.resolvedExpiresIn != null) {
108
+ return normalizeExpiresIn(options.resolvedExpiresIn, max);
109
+ }
110
+ const cap = normalizeExpiresIn(options.routeExpiresIn, max);
111
+ if (options.requested == null) return cap;
112
+ return Math.min(normalizeExpiresIn(options.requested, max), cap);
113
+ }
105
114
 
106
115
  // src/helpers/logger.ts
107
116
  var defaultLogger = {
@@ -443,8 +452,9 @@ function assertDistinctKeyPrefixes(routes) {
443
452
  }
444
453
 
445
454
  // src/helpers/request.ts
455
+ var SYNTHETIC_ORIGIN = "http://dimah-s3.local";
446
456
  function requestFromHeaders(headers) {
447
- return new Request("http://dimah-s3.local", { headers });
457
+ return new Request(SYNTHETIC_ORIGIN, { headers });
448
458
  }
449
459
  function assertWithinMaxFileSize(maxFileSize, size) {
450
460
  if (typeof maxFileSize === "number" && size > maxFileSize) {
@@ -662,6 +672,9 @@ function withS3Validation(options) {
662
672
  ...options,
663
673
  ...options.body !== void 0 ? { body: compileIfZod(options.body) } : {},
664
674
  ...options.query !== void 0 ? { query: compileIfZod(options.query) } : {},
675
+ // better-call reads the body before hooks. Clone first so
676
+ // `request.clone().json()` in guard / resolveScope still works.
677
+ ...options.body !== void 0 && options.cloneRequest === void 0 ? { cloneRequest: true } : {},
665
678
  onValidationError: options.onValidationError ?? (({ message }) => {
666
679
  throw errors.validationError(message);
667
680
  })
@@ -788,21 +801,25 @@ async function handleDownload(config, input, request) {
788
801
  const resolved = await route.download.resolve?.({
789
802
  ...stored,
790
803
  fileName: input.fileName,
791
- disposition: input.disposition ?? route.download.disposition
804
+ disposition: input.disposition ?? route.download.disposition,
805
+ expiresIn: input.expiresIn
792
806
  });
793
807
  const fileName = resolved?.fileName ?? input.fileName;
794
808
  const disposition = resolved?.disposition ?? input.disposition ?? route.download.disposition ?? "attachment";
795
- const expiresIn = normalizeExpiresIn(
796
- resolved?.expiresIn ?? route.download.expiresIn,
797
- config.maxExpiresIn
798
- );
809
+ const mode = route.download.mode ?? "presign";
810
+ const expiresIn = mode === "proxy" ? 0 : clampDownloadExpiresIn({
811
+ requested: input.expiresIn,
812
+ routeExpiresIn: route.download.expiresIn,
813
+ resolvedExpiresIn: resolved?.expiresIn,
814
+ maxExpiresIn: config.maxExpiresIn
815
+ });
799
816
  await runHook(route.download.guard, {
800
817
  ...stored,
801
818
  fileName,
802
- disposition
819
+ disposition,
820
+ expiresIn
803
821
  });
804
822
  await headObjectOrNotFound(route.client, bucket, key);
805
- const mode = route.download.mode ?? "presign";
806
823
  const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
807
824
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
808
825
  route: input.route,
@@ -824,7 +841,7 @@ async function handleDownload(config, input, request) {
824
841
  ...stored,
825
842
  fileName,
826
843
  url,
827
- expiresIn: mode === "proxy" ? 0 : expiresIn
844
+ expiresIn
828
845
  },
829
846
  config
830
847
  );
@@ -832,7 +849,7 @@ async function handleDownload(config, input, request) {
832
849
  bucket,
833
850
  key,
834
851
  url,
835
- expiresIn: mode === "proxy" ? 0 : expiresIn,
852
+ expiresIn,
836
853
  mode,
837
854
  disposition
838
855
  };
@@ -855,10 +872,11 @@ function catalogUpload(upload2) {
855
872
  ...upload2.replace ? { replace: upload2.replace } : {}
856
873
  };
857
874
  }
858
- function catalogDownload(download2) {
875
+ function catalogDownload(download2, maxExpiresIn) {
859
876
  if (!isEnabled(download2)) return { enabled: false };
860
877
  return {
861
878
  enabled: true,
879
+ expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
862
880
  ...download2.disposition ? { disposition: download2.disposition } : {},
863
881
  ...download2.mode ? { mode: download2.mode } : {}
864
882
  };
@@ -868,7 +886,7 @@ function buildRouteCatalog(config) {
868
886
  for (const [name, route] of Object.entries(config.routes)) {
869
887
  routes[name] = {
870
888
  upload: catalogUpload(route.upload),
871
- download: catalogDownload(route.download),
889
+ download: catalogDownload(route.download, config.maxExpiresIn),
872
890
  delete: isEnabled(route.delete) ? { enabled: true } : { enabled: false }
873
891
  };
874
892
  }