@dimah-s3/server 1.6.0 → 1.7.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.
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/api/routes/file.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,qBAAqB,EAEtB,MAAM,gBAAgB,CAAC;AA8ExB,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;YAMhB,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/api/routes/file.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,qBAAqB,EAEtB,MAAM,gBAAgB,CAAC;AAwFxB,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;YAMhB,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
@@ -1,4 +1,6 @@
1
1
  import { type HeadObjectCommandOutput, type S3Client } from "@aws-sdk/client-s3";
2
+ /** HeadObject as a yes/no. Errors other than not-found still throw. */
3
+ export declare function objectExists(client: S3Client, bucket: string, key: string): Promise<boolean>;
2
4
  /** HeadObject, mapping AWS not-found to `OBJECT_NOT_FOUND`. */
3
5
  export declare function headObjectOrNotFound(client: S3Client, bucket: string, key: string): Promise<HeadObjectCommandOutput>;
4
6
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"head-object.d.ts","sourceRoot":"","sources":["../../src/helpers/head-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,QAAQ,EACd,MAAM,oBAAoB,CAAC;AAI5B,+DAA+D;AAC/D,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,uBAAuB,CAAC,CAIlC;AAED;;;GAGG;AACH,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,uBAAuB,CAAC,CAgBlC;AAED,yEAAyE;AACzE,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,uBAAuB,GAAG,MAAM,CAK1E"}
1
+ {"version":3,"file":"head-object.d.ts","sourceRoot":"","sources":["../../src/helpers/head-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,QAAQ,EACd,MAAM,oBAAoB,CAAC;AAI5B,uEAAuE;AACvE,wBAAsB,YAAY,CAChC,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,OAAO,CAAC,CAQlB;AAED,+DAA+D;AAC/D,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,uBAAuB,CAAC,CAIlC;AAED;;;GAGG;AACH,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,uBAAuB,CAAC,CAgBlC;AAED,yEAAyE;AACzE,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,uBAAuB,GAAG,MAAM,CAK1E"}
@@ -9,8 +9,9 @@ export type { ResolvedObject } from "./resolve-target";
9
9
  export { assertFeatureEnabled, getResolvedRoute, openRoute, openStoredTarget, openUploadTarget, storedObjectContext, } from "./resolve-route";
10
10
  export type { OpenedTarget, OpenedUploadTarget, OpenUploadInput, } from "./resolve-route";
11
11
  export { assertDeclaredConstraints, assertVerifiedConstraints, assertWithinMaxFileSize, } from "./constraints";
12
- export { headObjectAfterMultipartComplete, headObjectOrNotFound, requireContentLength, } from "./head-object";
12
+ export { headObjectAfterMultipartComplete, headObjectOrNotFound, objectExists, requireContentLength, } from "./head-object";
13
13
  export { listAllParts, listedPartsByteSize } from "./list-parts";
14
+ export { applyInlineTypes } from "./inline-types";
14
15
  export { resolveLogger, defaultLogger } from "./logger";
15
16
  export { finalizeConfirmedObject } from "./finalize-confirmed";
16
17
  export { DIMAH_PREVIOUS_KEY_META, previousKeyFromMetadata, stripPreviousKeyMeta, } from "./previous-key";
@@ -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,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
+ {"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,YAAY,EACZ,oBAAoB,GACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,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"}
@@ -0,0 +1,12 @@
1
+ import { type ContentDispositionType, type ResolveInlineDispositionResult } from "@dimah-s3/core";
2
+ /**
3
+ * Route `download.inlineTypes` after HeadObject. Without a list, the
4
+ * disposition is signed as is.
5
+ */
6
+ export declare function applyInlineTypes(inlineTypes: readonly string[] | undefined, input: {
7
+ key: string;
8
+ fileName?: string;
9
+ disposition: ContentDispositionType;
10
+ contentType?: string;
11
+ }): ResolveInlineDispositionResult;
12
+ //# sourceMappingURL=inline-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inline-types.d.ts","sourceRoot":"","sources":["../../src/helpers/inline-types.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,8BAA8B,EACpC,MAAM,gBAAgB,CAAC;AAExB;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,EAC1C,KAAK,EAAE;IACL,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,sBAAsB,CAAC;IACpC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GACA,8BAA8B,CAQhC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
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';
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, resolveInlineDisposition, fileNameFromKey, 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
- import { PutObjectCommand, DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
3
+ import { PutObjectCommand, DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, HeadObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3';
4
4
  import { createMiddleware, createEndpoint, createRouter, toResponse } from 'better-call';
5
5
  import * as z from 'zod';
6
6
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
@@ -570,6 +570,15 @@ async function openStoredTarget(config, input, request, feature) {
570
570
  stored: storedObjectContext(request, route2, target.key)
571
571
  };
572
572
  }
573
+ async function objectExists(client, bucket, key) {
574
+ try {
575
+ await client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
576
+ return true;
577
+ } catch (err) {
578
+ if (isAwsNotFound(err)) return false;
579
+ throw err;
580
+ }
581
+ }
573
582
  async function headObjectOrNotFound(client, bucket, key) {
574
583
  return sendOrObjectNotFound(
575
584
  () => client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
@@ -623,6 +632,15 @@ function listedPartsByteSize(parts, excludePartNumber) {
623
632
  }
624
633
  return total;
625
634
  }
635
+ function applyInlineTypes(inlineTypes, input) {
636
+ if (!inlineTypes) return { disposition: input.disposition };
637
+ return resolveInlineDisposition({
638
+ contentType: input.contentType,
639
+ fileName: input.fileName ?? fileNameFromKey(input.key),
640
+ allow: inlineTypes,
641
+ requested: input.disposition
642
+ });
643
+ }
626
644
  async function finalizeConfirmedObject(client, bucket, key, input) {
627
645
  const { config, route: route2, stored, head, uploadId, acl } = input;
628
646
  const rawMetadata = head.Metadata ?? {};
@@ -826,7 +844,7 @@ var confirm = createS3Endpoint(
826
844
  return handleConfirm(ctx.context.config, ctx.body, ctx.context.request);
827
845
  }
828
846
  );
829
- async function handleDelete(config, input, request) {
847
+ async function deleteStoredObject(config, input, request) {
830
848
  const { route: route2, key, bucket, stored } = await openStoredTarget(
831
849
  config,
832
850
  input,
@@ -834,34 +852,30 @@ async function handleDelete(config, input, request) {
834
852
  "delete"
835
853
  );
836
854
  await runHook(route2.delete.guard, stored);
837
- await headObjectOrNotFound(route2.client, bucket, key);
838
- await route2.client.send(
839
- new DeleteObjectCommand({ Bucket: bucket, Key: key })
855
+ const existed = await objectExists(route2.client, bucket, key);
856
+ if (existed) {
857
+ await route2.client.send(
858
+ new DeleteObjectCommand({ Bucket: bucket, Key: key })
859
+ );
860
+ }
861
+ await runLifecycleHook(
862
+ route2.delete.onDeleted,
863
+ { ...stored, existed },
864
+ config
840
865
  );
841
- await runLifecycleHook(route2.delete.onDeleted, stored, config);
842
- return { success: true, bucket, key };
866
+ return { success: true, bucket, key, existed };
843
867
  }
844
868
  var deleteObject = createS3Endpoint(
845
869
  S3_API_ROUTES.delete,
846
870
  { method: "DELETE", query: deleteQuerySchema },
847
871
  async (ctx) => {
848
- return handleDelete(ctx.context.config, ctx.query, ctx.context.request);
872
+ return deleteStoredObject(
873
+ ctx.context.config,
874
+ ctx.query,
875
+ ctx.context.request
876
+ );
849
877
  }
850
878
  );
851
- async function deleteOne(config, routeName, key, request) {
852
- const { route: route2, bucket, stored } = await openStoredTarget(
853
- config,
854
- { route: routeName, key },
855
- request,
856
- "delete"
857
- );
858
- await runHook(route2.delete.guard, stored);
859
- await headObjectOrNotFound(route2.client, bucket, stored.key);
860
- await route2.client.send(
861
- new DeleteObjectCommand({ Bucket: bucket, Key: stored.key })
862
- );
863
- await runLifecycleHook(route2.delete.onDeleted, stored, config);
864
- }
865
879
  var deleteBatch = createS3Endpoint(
866
880
  S3_API_ROUTES.deleteBatch,
867
881
  { method: "POST", body: deleteBatchBodySchema },
@@ -871,8 +885,12 @@ var deleteBatch = createS3Endpoint(
871
885
  const results = [];
872
886
  for (const key of ctx.body.keys) {
873
887
  try {
874
- await deleteOne(config, ctx.body.route, key, request);
875
- results.push({ key, success: true });
888
+ const { existed } = await deleteStoredObject(
889
+ config,
890
+ { route: ctx.body.route, key },
891
+ request
892
+ );
893
+ results.push({ key, success: true, existed });
876
894
  } catch (err) {
877
895
  results.push({
878
896
  key,
@@ -923,19 +941,26 @@ async function handleDownload(config, input, request) {
923
941
  disposition,
924
942
  expiresIn
925
943
  });
926
- await headObjectOrNotFound(route2.client, bucket, key);
927
- const contentDisposition = fileName ? buildContentDisposition(fileName, disposition) : disposition;
944
+ const head = await headObjectOrNotFound(route2.client, bucket, key);
945
+ const signed = applyInlineTypes(route2.download.inlineTypes, {
946
+ key,
947
+ fileName,
948
+ disposition,
949
+ contentType: head.ContentType
950
+ });
951
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
928
952
  const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
929
953
  route: input.route,
930
954
  key,
931
955
  fileName,
932
- disposition
956
+ disposition: signed.disposition
933
957
  }) : await getSignedUrl(
934
958
  route2.client,
935
959
  new GetObjectCommand({
936
960
  Bucket: bucket,
937
961
  Key: key,
938
- ResponseContentDisposition: contentDisposition
962
+ ResponseContentDisposition: contentDisposition,
963
+ ResponseContentType: signed.contentType
939
964
  }),
940
965
  { expiresIn }
941
966
  );
@@ -955,7 +980,7 @@ async function handleDownload(config, input, request) {
955
980
  url,
956
981
  expiresIn,
957
982
  mode,
958
- disposition
983
+ disposition: signed.disposition
959
984
  };
960
985
  }
961
986
  var download = createS3Endpoint(
@@ -982,6 +1007,7 @@ function catalogDownload(download2, maxExpiresIn) {
982
1007
  enabled: true,
983
1008
  expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
984
1009
  ...download2.disposition ? { disposition: download2.disposition } : {},
1010
+ ...download2.inlineTypes ? { inlineTypes: [...download2.inlineTypes] } : {},
985
1011
  ...download2.mode ? { mode: download2.mode } : {}
986
1012
  };
987
1013
  }
@@ -1022,13 +1048,20 @@ async function handleFile(config, input, request) {
1022
1048
  fileName,
1023
1049
  disposition
1024
1050
  });
1025
- await headObjectOrNotFound(route2.client, bucket, key);
1051
+ const head = await headObjectOrNotFound(route2.client, bucket, key);
1052
+ const signed = applyInlineTypes(route2.download.inlineTypes, {
1053
+ key,
1054
+ fileName,
1055
+ disposition,
1056
+ contentType: head.ContentType
1057
+ });
1058
+ const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
1026
1059
  const result = await sendOrObjectNotFound(
1027
1060
  () => route2.client.send(
1028
1061
  new GetObjectCommand({
1029
1062
  Bucket: bucket,
1030
1063
  Key: key,
1031
- ResponseContentDisposition: fileName ? buildContentDisposition(fileName, disposition) : disposition
1064
+ ResponseContentDisposition: contentDisposition
1032
1065
  })
1033
1066
  )
1034
1067
  );
@@ -1036,11 +1069,12 @@ async function handleFile(config, input, request) {
1036
1069
  if (!body) throw errors.objectNotFound();
1037
1070
  const stream = "transformToWebStream" in body && typeof body.transformToWebStream === "function" ? body.transformToWebStream() : body;
1038
1071
  const headers = new Headers();
1039
- headers.set("Content-Type", result.ContentType ?? "application/octet-stream");
1040
1072
  headers.set(
1041
- "Content-Disposition",
1042
- fileName ? buildContentDisposition(fileName, disposition) : disposition
1073
+ "Content-Type",
1074
+ signed.contentType ?? result.ContentType ?? "application/octet-stream"
1043
1075
  );
1076
+ headers.set("Content-Disposition", contentDisposition);
1077
+ headers.set("X-Content-Type-Options", "nosniff");
1044
1078
  if (typeof result.ContentLength === "number") {
1045
1079
  headers.set("Content-Length", String(result.ContentLength));
1046
1080
  }