@dimah-s3/server 1.5.11 → 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/api/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createMiddleware, createEndpoint } from 'better-call';
2
2
  import * as z from 'zod';
3
- 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, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
3
+ 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, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
4
4
  import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
5
5
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
6
6
  import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
@@ -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 = {
@@ -664,21 +673,25 @@ async function handleDownload(config, input, request) {
664
673
  const resolved = await route.download.resolve?.({
665
674
  ...stored,
666
675
  fileName: input.fileName,
667
- disposition: input.disposition ?? route.download.disposition
676
+ disposition: input.disposition ?? route.download.disposition,
677
+ expiresIn: input.expiresIn
668
678
  });
669
679
  const fileName = resolved?.fileName ?? input.fileName;
670
680
  const disposition = resolved?.disposition ?? input.disposition ?? route.download.disposition ?? "attachment";
671
- const expiresIn = normalizeExpiresIn(
672
- resolved?.expiresIn ?? route.download.expiresIn,
673
- config.maxExpiresIn
674
- );
681
+ const mode = route.download.mode ?? "presign";
682
+ const expiresIn = mode === "proxy" ? 0 : clampDownloadExpiresIn({
683
+ requested: input.expiresIn,
684
+ routeExpiresIn: route.download.expiresIn,
685
+ resolvedExpiresIn: resolved?.expiresIn,
686
+ maxExpiresIn: config.maxExpiresIn
687
+ });
675
688
  await runHook(route.download.guard, {
676
689
  ...stored,
677
690
  fileName,
678
- disposition
691
+ disposition,
692
+ expiresIn
679
693
  });
680
694
  await headObjectOrNotFound(route.client, bucket, key);
681
- const mode = route.download.mode ?? "presign";
682
695
  const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
683
696
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
684
697
  route: input.route,
@@ -700,7 +713,7 @@ async function handleDownload(config, input, request) {
700
713
  ...stored,
701
714
  fileName,
702
715
  url,
703
- expiresIn: mode === "proxy" ? 0 : expiresIn
716
+ expiresIn
704
717
  },
705
718
  config
706
719
  );
@@ -708,7 +721,7 @@ async function handleDownload(config, input, request) {
708
721
  bucket,
709
722
  key,
710
723
  url,
711
- expiresIn: mode === "proxy" ? 0 : expiresIn,
724
+ expiresIn,
712
725
  mode,
713
726
  disposition
714
727
  };
@@ -731,10 +744,11 @@ function catalogUpload(upload2) {
731
744
  ...upload2.replace ? { replace: upload2.replace } : {}
732
745
  };
733
746
  }
734
- function catalogDownload(download2) {
747
+ function catalogDownload(download2, maxExpiresIn) {
735
748
  if (!isEnabled(download2)) return { enabled: false };
736
749
  return {
737
750
  enabled: true,
751
+ expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
738
752
  ...download2.disposition ? { disposition: download2.disposition } : {},
739
753
  ...download2.mode ? { mode: download2.mode } : {}
740
754
  };
@@ -744,7 +758,7 @@ function buildRouteCatalog(config) {
744
758
  for (const [name, route] of Object.entries(config.routes)) {
745
759
  routes[name] = {
746
760
  upload: catalogUpload(route.upload),
747
- download: catalogDownload(route.download),
761
+ download: catalogDownload(route.download, config.maxExpiresIn),
748
762
  delete: isEnabled(route.delete) ? { enabled: true } : { enabled: false }
749
763
  };
750
764
  }