@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,6 +3,10 @@ import type { S3EndpointContext } from "./context";
3
3
  /**
4
4
  * Wrap better-call endpoints so `s3.api.upload({ body, headers })` injects
5
5
  * `routerContext` the same way HTTP `createRouter` does.
6
+ *
7
+ * The synthetic `Request` includes method, pathname, `query`, and a JSON
8
+ * `body`, so guards see the same request an HTTP call would. A `request`
9
+ * argument is passed through unchanged.
6
10
  */
7
11
  export declare function bindEndpoints<E extends Record<string, Endpoint>>(endpoints: E, context: Pick<S3EndpointContext, "config">): E;
8
12
  //# sourceMappingURL=bind-endpoints.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bind-endpoints.d.ts","sourceRoot":"","sources":["../../src/api/bind-endpoints.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAGnD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC9D,SAAS,EAAE,CAAC,EACZ,OAAO,EAAE,IAAI,CAAC,iBAAiB,EAAE,QAAQ,CAAC,GACzC,CAAC,CAsBH"}
1
+ {"version":3,"file":"bind-endpoints.d.ts","sourceRoot":"","sources":["../../src/api/bind-endpoints.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC9D,SAAS,EAAE,CAAC,EACZ,OAAO,EAAE,IAAI,CAAC,iBAAiB,EAAE,QAAQ,CAAC,GACzC,CAAC,CA8BH"}
@@ -126,8 +126,9 @@ type CreateS3Endpoint = typeof createEndpointWithContext;
126
126
  * Paths are absolute under `basePath` (e.g. `/db/objects`). Context
127
127
  * (`config`, `request`) is injected by the router / `s3.api`.
128
128
  * Zod failures throw `APIError` (`VALIDATION_ERROR`) via
129
- * better-call `onValidationError`. Zod `body` / `query` schemas are
130
- * compiled with `z.compile()` so valid requests skip the interpreter.
129
+ * better-call `onValidationError`. Endpoints with a `body` set
130
+ * `cloneRequest` so hooks can still read that JSON. Zod `body` / `query`
131
+ * schemas are compiled with `z.compile()` so valid requests skip the interpreter.
131
132
  * Unsupported schemas, and runtimes that reject `new Function`, keep
132
133
  * the runtime parser.
133
134
  *
@@ -1 +1 @@
1
- {"version":3,"file":"create-s3-endpoint.d.ts","sourceRoot":"","sources":["../../src/api/create-s3-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAKnE,QAAA,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAE7B,CAAC;AAwBH,KAAK,gBAAgB,GAAG,OAAO,yBAAyB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAgBE,CAAC"}
1
+ {"version":3,"file":"create-s3-endpoint.d.ts","sourceRoot":"","sources":["../../src/api/create-s3-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAKnE,QAAA,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAE7B,CAAC;AA6BH,KAAK,gBAAgB,GAAG,OAAO,yBAAyB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAgBE,CAAC"}
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 = {
@@ -315,8 +324,9 @@ function isEnabled(feature) {
315
324
  }
316
325
 
317
326
  // src/helpers/request.ts
327
+ var SYNTHETIC_ORIGIN = "http://dimah-s3.local";
318
328
  function requestFromHeaders(headers) {
319
- return new Request("http://dimah-s3.local", { headers });
329
+ return new Request(SYNTHETIC_ORIGIN, { headers });
320
330
  }
321
331
  function assertWithinMaxFileSize(maxFileSize, size) {
322
332
  if (typeof maxFileSize === "number" && size > maxFileSize) {
@@ -536,6 +546,9 @@ function withS3Validation(options) {
536
546
  ...options,
537
547
  ...options.body !== void 0 ? { body: compileIfZod(options.body) } : {},
538
548
  ...options.query !== void 0 ? { query: compileIfZod(options.query) } : {},
549
+ // better-call reads the body before hooks. Clone first so
550
+ // `request.clone().json()` in guard / resolveScope still works.
551
+ ...options.body !== void 0 && options.cloneRequest === void 0 ? { cloneRequest: true } : {},
539
552
  onValidationError: options.onValidationError ?? (({ message }) => {
540
553
  throw errors.validationError(message);
541
554
  })
@@ -660,21 +673,25 @@ async function handleDownload(config, input, request) {
660
673
  const resolved = await route.download.resolve?.({
661
674
  ...stored,
662
675
  fileName: input.fileName,
663
- disposition: input.disposition ?? route.download.disposition
676
+ disposition: input.disposition ?? route.download.disposition,
677
+ expiresIn: input.expiresIn
664
678
  });
665
679
  const fileName = resolved?.fileName ?? input.fileName;
666
680
  const disposition = resolved?.disposition ?? input.disposition ?? route.download.disposition ?? "attachment";
667
- const expiresIn = normalizeExpiresIn(
668
- resolved?.expiresIn ?? route.download.expiresIn,
669
- config.maxExpiresIn
670
- );
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
+ });
671
688
  await runHook(route.download.guard, {
672
689
  ...stored,
673
690
  fileName,
674
- disposition
691
+ disposition,
692
+ expiresIn
675
693
  });
676
694
  await headObjectOrNotFound(route.client, bucket, key);
677
- const mode = route.download.mode ?? "presign";
678
695
  const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
679
696
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
680
697
  route: input.route,
@@ -696,7 +713,7 @@ async function handleDownload(config, input, request) {
696
713
  ...stored,
697
714
  fileName,
698
715
  url,
699
- expiresIn: mode === "proxy" ? 0 : expiresIn
716
+ expiresIn
700
717
  },
701
718
  config
702
719
  );
@@ -704,7 +721,7 @@ async function handleDownload(config, input, request) {
704
721
  bucket,
705
722
  key,
706
723
  url,
707
- expiresIn: mode === "proxy" ? 0 : expiresIn,
724
+ expiresIn,
708
725
  mode,
709
726
  disposition
710
727
  };
@@ -727,10 +744,11 @@ function catalogUpload(upload2) {
727
744
  ...upload2.replace ? { replace: upload2.replace } : {}
728
745
  };
729
746
  }
730
- function catalogDownload(download2) {
747
+ function catalogDownload(download2, maxExpiresIn) {
731
748
  if (!isEnabled(download2)) return { enabled: false };
732
749
  return {
733
750
  enabled: true,
751
+ expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
734
752
  ...download2.disposition ? { disposition: download2.disposition } : {},
735
753
  ...download2.mode ? { mode: download2.mode } : {}
736
754
  };
@@ -740,7 +758,7 @@ function buildRouteCatalog(config) {
740
758
  for (const [name, route] of Object.entries(config.routes)) {
741
759
  routes[name] = {
742
760
  upload: catalogUpload(route.upload),
743
- download: catalogDownload(route.download),
761
+ download: catalogDownload(route.download, config.maxExpiresIn),
744
762
  delete: isEnabled(route.delete) ? { enabled: true } : { enabled: false }
745
763
  };
746
764
  }