@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.
@@ -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"}
@@ -1,3 +1,16 @@
1
- /** Build a synthetic Request for server-side `api` calls. */
1
+ /** Headers-only request for `s3.put` and the middleware fallback. */
2
2
  export declare function requestFromHeaders(headers?: HeadersInit): Request;
3
+ /**
4
+ * Request for a direct `s3.api` call when the caller did not pass one.
5
+ * Copies scalar `query` values and a plain JSON `body` so hooks see the same
6
+ * request an HTTP call would. better-call has no option for this: direct
7
+ * invocation passes `body` / `query` as objects and leaves `request` to us.
8
+ */
9
+ export declare function requestFromDirectCall(input: {
10
+ method: string;
11
+ pathname: string;
12
+ headers?: HeadersInit;
13
+ query?: unknown;
14
+ body?: unknown;
15
+ }): Request;
3
16
  //# sourceMappingURL=request.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/helpers/request.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAEjE"}
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/helpers/request.ts"],"names":[],"mappings":"AAGA,qEAAqE;AACrE,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,GAAG,OAAO,CAMV"}
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';
@@ -9,8 +9,40 @@ import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
9
9
  // src/dimah-s3.ts
10
10
 
11
11
  // src/helpers/request.ts
12
+ var SYNTHETIC_ORIGIN = "http://dimah-s3.local";
12
13
  function requestFromHeaders(headers) {
13
- return new Request("http://dimah-s3.local", { headers });
14
+ return new Request(SYNTHETIC_ORIGIN, { headers });
15
+ }
16
+ function requestFromDirectCall(input) {
17
+ const url = new URL(input.pathname, SYNTHETIC_ORIGIN);
18
+ appendQuery(url, input.query);
19
+ const headers = new Headers(input.headers);
20
+ const body = jsonBody(input.method, input.body, headers);
21
+ return new Request(url, { method: input.method, headers, body });
22
+ }
23
+ function appendQuery(url, query) {
24
+ if (query == null || typeof query !== "object" || Array.isArray(query))
25
+ return;
26
+ for (const [key, value] of Object.entries(query)) {
27
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
28
+ continue;
29
+ }
30
+ url.searchParams.set(key, String(value));
31
+ }
32
+ }
33
+ function jsonBody(method, body, headers) {
34
+ if (body == null || method === "GET" || method === "HEAD" || !isPlainJson(body)) {
35
+ return;
36
+ }
37
+ headers.delete("content-length");
38
+ headers.delete("transfer-encoding");
39
+ headers.set("content-type", "application/json");
40
+ return JSON.stringify(body);
41
+ }
42
+ function isPlainJson(body) {
43
+ if (typeof body !== "object" || body === null) return false;
44
+ const proto = Object.getPrototypeOf(body);
45
+ return proto === Object.prototype || proto === Array.prototype;
14
46
  }
15
47
  async function deleteObjectBestEffort(client, bucket, key) {
16
48
  try {
@@ -108,6 +140,15 @@ function normalizeExpiresIn(value, maxExpiresIn = S3_MAX_EXPIRES_IN) {
108
140
  const max = Number.isFinite(maxExpiresIn) && maxExpiresIn > 0 ? Math.floor(maxExpiresIn) : S3_MAX_EXPIRES_IN;
109
141
  return Math.min(expires, max);
110
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
+ }
111
152
 
112
153
  // src/helpers/logger.ts
113
154
  var defaultLogger = {
@@ -735,6 +776,9 @@ function withS3Validation(options) {
735
776
  ...options,
736
777
  ...options.body !== void 0 ? { body: compileIfZod(options.body) } : {},
737
778
  ...options.query !== void 0 ? { query: compileIfZod(options.query) } : {},
779
+ // better-call reads the body before hooks. Clone first so
780
+ // `request.clone().json()` in guard / resolveScope still works.
781
+ ...options.body !== void 0 && options.cloneRequest === void 0 ? { cloneRequest: true } : {},
738
782
  onValidationError: options.onValidationError ?? (({ message }) => {
739
783
  throw errors.validationError(message);
740
784
  })
@@ -861,21 +905,25 @@ async function handleDownload(config, input, request) {
861
905
  const resolved = await route2.download.resolve?.({
862
906
  ...stored,
863
907
  fileName: input.fileName,
864
- disposition: input.disposition ?? route2.download.disposition
908
+ disposition: input.disposition ?? route2.download.disposition,
909
+ expiresIn: input.expiresIn
865
910
  });
866
911
  const fileName = resolved?.fileName ?? input.fileName;
867
912
  const disposition = resolved?.disposition ?? input.disposition ?? route2.download.disposition ?? "attachment";
868
- const expiresIn = normalizeExpiresIn(
869
- resolved?.expiresIn ?? route2.download.expiresIn,
870
- config.maxExpiresIn
871
- );
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
+ });
872
920
  await runHook(route2.download.guard, {
873
921
  ...stored,
874
922
  fileName,
875
- disposition
923
+ disposition,
924
+ expiresIn
876
925
  });
877
926
  await headObjectOrNotFound(route2.client, bucket, key);
878
- const mode = route2.download.mode ?? "presign";
879
927
  const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
880
928
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
881
929
  route: input.route,
@@ -897,7 +945,7 @@ async function handleDownload(config, input, request) {
897
945
  ...stored,
898
946
  fileName,
899
947
  url,
900
- expiresIn: mode === "proxy" ? 0 : expiresIn
948
+ expiresIn
901
949
  },
902
950
  config
903
951
  );
@@ -905,7 +953,7 @@ async function handleDownload(config, input, request) {
905
953
  bucket,
906
954
  key,
907
955
  url,
908
- expiresIn: mode === "proxy" ? 0 : expiresIn,
956
+ expiresIn,
909
957
  mode,
910
958
  disposition
911
959
  };
@@ -928,10 +976,11 @@ function catalogUpload(upload2) {
928
976
  ...upload2.replace ? { replace: upload2.replace } : {}
929
977
  };
930
978
  }
931
- function catalogDownload(download2) {
979
+ function catalogDownload(download2, maxExpiresIn) {
932
980
  if (!isEnabled(download2)) return { enabled: false };
933
981
  return {
934
982
  enabled: true,
983
+ expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
935
984
  ...download2.disposition ? { disposition: download2.disposition } : {},
936
985
  ...download2.mode ? { mode: download2.mode } : {}
937
986
  };
@@ -941,7 +990,7 @@ function buildRouteCatalog(config) {
941
990
  for (const [name, route2] of Object.entries(config.routes)) {
942
991
  routes[name] = {
943
992
  upload: catalogUpload(route2.upload),
944
- download: catalogDownload(route2.download),
993
+ download: catalogDownload(route2.download, config.maxExpiresIn),
945
994
  delete: isEnabled(route2.delete) ? { enabled: true } : { enabled: false }
946
995
  };
947
996
  }
@@ -1721,17 +1770,22 @@ function applyPlugins(config) {
1721
1770
  function definePlugin(plugin) {
1722
1771
  return plugin;
1723
1772
  }
1724
-
1725
- // src/api/bind-endpoints.ts
1726
1773
  function bindEndpoints(endpoints, context) {
1727
1774
  const api = {};
1728
1775
  for (const [key, endpoint] of Object.entries(endpoints)) {
1729
1776
  const bound = (async (input = {}) => {
1730
1777
  const headers = input.headers;
1731
- const request = input.request ?? requestFromHeaders(headers);
1778
+ const provided = input.request;
1779
+ const request = provided ?? requestFromDirectCall({
1780
+ method: endpointMethod(endpoint),
1781
+ pathname: endpointPathname(endpoint, context.config.basePath),
1782
+ headers,
1783
+ query: input.query,
1784
+ body: input.body
1785
+ });
1732
1786
  return endpoint({
1733
1787
  ...input,
1734
- headers: headers ?? request.headers,
1788
+ headers: provided ? headers ?? request.headers : request.headers,
1735
1789
  request,
1736
1790
  context
1737
1791
  });
@@ -1742,6 +1796,18 @@ function bindEndpoints(endpoints, context) {
1742
1796
  }
1743
1797
  return api;
1744
1798
  }
1799
+ function endpointMethod(endpoint) {
1800
+ const method = endpoint.options?.method;
1801
+ if (Array.isArray(method)) return method[0] ?? "GET";
1802
+ if (typeof method === "string" && method !== "*") return method;
1803
+ return "GET";
1804
+ }
1805
+ function endpointPathname(endpoint, basePath) {
1806
+ const prefix = normalizeS3ApiBasePath(basePath ?? S3_API_BASE_PATH);
1807
+ const path = endpoint.path ?? "/";
1808
+ const suffix = path.startsWith("/") ? path : `/${path}`;
1809
+ return `${prefix}${suffix}` || "/";
1810
+ }
1745
1811
 
1746
1812
  // src/api/router.ts
1747
1813
  var NETWORK_CODES = /* @__PURE__ */ new Set([