@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.
@@ -3,4 +3,19 @@
3
3
  * to {@link maxExpiresIn} (SigV4 max by default).
4
4
  */
5
5
  export declare function normalizeExpiresIn(value: unknown, maxExpiresIn?: number): number;
6
+ /**
7
+ * Signed download TTL.
8
+ * `download.resolve` wins, then the shorter of the client request and the
9
+ * route cap. Both are clamped to `maxExpiresIn`.
10
+ */
11
+ export declare function clampDownloadExpiresIn(options: {
12
+ /** Client `expiresIn`, when the query sent one. */
13
+ requested?: number;
14
+ /** Route `download.expiresIn`. Omitted uses the protocol default. */
15
+ routeExpiresIn?: number;
16
+ /** `download.resolve` return. Replaces the cap and the client request. */
17
+ resolvedExpiresIn?: number;
18
+ /** Instance ceiling. Defaults to the SigV4 maximum. */
19
+ maxExpiresIn?: number;
20
+ }): number;
6
21
  //# sourceMappingURL=expires.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"expires.d.ts","sourceRoot":"","sources":["../../src/helpers/expires.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,EACd,YAAY,GAAE,MAA0B,GACvC,MAAM,CASR"}
1
+ {"version":3,"file":"expires.d.ts","sourceRoot":"","sources":["../../src/helpers/expires.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,EACd,YAAY,GAAE,MAA0B,GACvC,MAAM,CASR;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE;IAC9C,mDAAmD;IACnD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,uDAAuD;IACvD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,GAAG,MAAM,CAQT"}
@@ -1,6 +1,6 @@
1
1
  export { abortMultipartBestEffort, deleteObjectBestEffort, } from "./best-effort";
2
2
  export { isAwsNotFound, sendOrObjectNotFound } from "./is-aws-not-found";
3
- export { normalizeExpiresIn } from "./expires";
3
+ export { clampDownloadExpiresIn, normalizeExpiresIn } from "./expires";
4
4
  export { runHook, runLifecycleHook, runObjectHook } from "./hooks";
5
5
  export { isEnabled, isFeatureOn, normalizeFeature, normalizeRoute, normalizeRoutes, } from "./features";
6
6
  export { requestFromHeaders } from "./request";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACnE,OAAO,EACL,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,cAAc,EACd,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,YAAY,EACZ,kBAAkB,EAClB,eAAe,GAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,gCAAgC,EAChC,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACvE,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACnE,OAAO,EACL,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,cAAc,EACd,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,YAAY,EACZ,kBAAkB,EAClB,eAAe,GAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,gCAAgC,EAChC,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
package/dist/index.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, resolveStoredFileName, S3_MAX_EXPIRES_IN, buildContentDisposition, S3_MAX_POST_OBJECT_BYTES, normalizeS3ApiBasePath, S3_API_BASE_PATH, matchesFileTypes, S3_DEFAULT_EXPIRES_IN, normalizeObjectKey, sanitizeFileName, ROUTE_NAME_PATTERN } 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, resolveStoredFileName, buildContentDisposition, S3_MAX_EXPIRES_IN, S3_MAX_POST_OBJECT_BYTES, normalizeS3ApiBasePath, S3_API_BASE_PATH, matchesFileTypes, S3_DEFAULT_EXPIRES_IN, normalizeObjectKey, sanitizeFileName, ROUTE_NAME_PATTERN } from '@dimah-s3/core';
2
2
  export { APIError, S3_ERROR_CODES, isAPIError, isS3ErrorCode } from '@dimah-s3/core';
3
3
  import { PutObjectCommand, DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
4
4
  import { createMiddleware, createEndpoint, createRouter, toResponse } from 'better-call';
@@ -140,6 +140,15 @@ function normalizeExpiresIn(value, maxExpiresIn = S3_MAX_EXPIRES_IN) {
140
140
  const max = Number.isFinite(maxExpiresIn) && maxExpiresIn > 0 ? Math.floor(maxExpiresIn) : S3_MAX_EXPIRES_IN;
141
141
  return Math.min(expires, max);
142
142
  }
143
+ function clampDownloadExpiresIn(options) {
144
+ const max = options.maxExpiresIn;
145
+ if (options.resolvedExpiresIn != null) {
146
+ return normalizeExpiresIn(options.resolvedExpiresIn, max);
147
+ }
148
+ const cap = normalizeExpiresIn(options.routeExpiresIn, max);
149
+ if (options.requested == null) return cap;
150
+ return Math.min(normalizeExpiresIn(options.requested, max), cap);
151
+ }
143
152
 
144
153
  // src/helpers/logger.ts
145
154
  var defaultLogger = {
@@ -896,21 +905,25 @@ async function handleDownload(config, input, request) {
896
905
  const resolved = await route2.download.resolve?.({
897
906
  ...stored,
898
907
  fileName: input.fileName,
899
- disposition: input.disposition ?? route2.download.disposition
908
+ disposition: input.disposition ?? route2.download.disposition,
909
+ expiresIn: input.expiresIn
900
910
  });
901
911
  const fileName = resolved?.fileName ?? input.fileName;
902
912
  const disposition = resolved?.disposition ?? input.disposition ?? route2.download.disposition ?? "attachment";
903
- const expiresIn = normalizeExpiresIn(
904
- resolved?.expiresIn ?? route2.download.expiresIn,
905
- config.maxExpiresIn
906
- );
913
+ const mode = route2.download.mode ?? "presign";
914
+ const expiresIn = mode === "proxy" ? 0 : clampDownloadExpiresIn({
915
+ requested: input.expiresIn,
916
+ routeExpiresIn: route2.download.expiresIn,
917
+ resolvedExpiresIn: resolved?.expiresIn,
918
+ maxExpiresIn: config.maxExpiresIn
919
+ });
907
920
  await runHook(route2.download.guard, {
908
921
  ...stored,
909
922
  fileName,
910
- disposition
923
+ disposition,
924
+ expiresIn
911
925
  });
912
926
  await headObjectOrNotFound(route2.client, bucket, key);
913
- const mode = route2.download.mode ?? "presign";
914
927
  const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
915
928
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
916
929
  route: input.route,
@@ -932,7 +945,7 @@ async function handleDownload(config, input, request) {
932
945
  ...stored,
933
946
  fileName,
934
947
  url,
935
- expiresIn: mode === "proxy" ? 0 : expiresIn
948
+ expiresIn
936
949
  },
937
950
  config
938
951
  );
@@ -940,7 +953,7 @@ async function handleDownload(config, input, request) {
940
953
  bucket,
941
954
  key,
942
955
  url,
943
- expiresIn: mode === "proxy" ? 0 : expiresIn,
956
+ expiresIn,
944
957
  mode,
945
958
  disposition
946
959
  };
@@ -963,10 +976,11 @@ function catalogUpload(upload2) {
963
976
  ...upload2.replace ? { replace: upload2.replace } : {}
964
977
  };
965
978
  }
966
- function catalogDownload(download2) {
979
+ function catalogDownload(download2, maxExpiresIn) {
967
980
  if (!isEnabled(download2)) return { enabled: false };
968
981
  return {
969
982
  enabled: true,
983
+ expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
970
984
  ...download2.disposition ? { disposition: download2.disposition } : {},
971
985
  ...download2.mode ? { mode: download2.mode } : {}
972
986
  };
@@ -976,7 +990,7 @@ function buildRouteCatalog(config) {
976
990
  for (const [name, route2] of Object.entries(config.routes)) {
977
991
  routes[name] = {
978
992
  upload: catalogUpload(route2.upload),
979
- download: catalogDownload(route2.download),
993
+ download: catalogDownload(route2.download, config.maxExpiresIn),
980
994
  delete: isEnabled(route2.delete) ? { enabled: true } : { enabled: false }
981
995
  };
982
996
  }