@dimah-s3/server 1.5.11 → 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.
- package/dist/api/index.js +94 -46
- package/dist/api/index.js.map +1 -1
- package/dist/api/routes/catalog.d.ts.map +1 -1
- package/dist/api/routes/delete-batch.d.ts.map +1 -1
- package/dist/api/routes/delete.d.ts +10 -0
- package/dist/api/routes/delete.d.ts.map +1 -1
- package/dist/api/routes/download.d.ts +2 -0
- package/dist/api/routes/download.d.ts.map +1 -1
- package/dist/api/routes/file.d.ts.map +1 -1
- package/dist/api/routes/index.d.ts +2 -0
- package/dist/api/routes/index.d.ts.map +1 -1
- package/dist/helpers/expires.d.ts +15 -0
- package/dist/helpers/expires.d.ts.map +1 -1
- package/dist/helpers/head-object.d.ts +2 -0
- package/dist/helpers/head-object.d.ts.map +1 -1
- package/dist/helpers/index.d.ts +3 -2
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/inline-types.d.ts +12 -0
- package/dist/helpers/inline-types.d.ts.map +1 -0
- package/dist/index.js +94 -46
- package/dist/index.js.map +1 -1
- package/dist/plugins.js +94 -46
- package/dist/plugins.js.map +1 -1
- package/dist/types/config.d.ts +15 -1
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/hook-contexts.d.ts +20 -3
- package/dist/types/hook-contexts.d.ts.map +1 -1
- package/package.json +11 -11
package/dist/plugins.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S3_API_ROUTES, isAPIError, APIError, S3_ERROR_CODES, confirmBodySchema, deleteQuerySchema, deleteBatchBodySchema, downloadQuerySchema, fileQuerySchema, multipartAbortBodySchema, multipartCompleteBodySchema, multipartInitBodySchema, multipartListPartsQuerySchema, multipartSignPartBodySchema, uploadBodySchema,
|
|
2
|
-
import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand
|
|
1
|
+
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, resolveInlineDisposition, fileNameFromKey, S3_DEFAULT_EXPIRES_IN, matchesFileTypes, ROUTE_NAME_PATTERN, normalizeObjectKey, sanitizeFileName } from '@dimah-s3/core';
|
|
2
|
+
import { DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, PutObjectCommand, HeadObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3';
|
|
3
3
|
import { createMiddleware, createEndpoint } from 'better-call';
|
|
4
4
|
import * as z from 'zod';
|
|
5
5
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
@@ -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 = {
|
|
@@ -526,6 +535,15 @@ async function openStoredTarget(config, input, request, feature) {
|
|
|
526
535
|
stored: storedObjectContext(request, route, target.key)
|
|
527
536
|
};
|
|
528
537
|
}
|
|
538
|
+
async function objectExists(client, bucket, key) {
|
|
539
|
+
try {
|
|
540
|
+
await client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
|
|
541
|
+
return true;
|
|
542
|
+
} catch (err) {
|
|
543
|
+
if (isAwsNotFound(err)) return false;
|
|
544
|
+
throw err;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
529
547
|
async function headObjectOrNotFound(client, bucket, key) {
|
|
530
548
|
return sendOrObjectNotFound(
|
|
531
549
|
() => client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
|
|
@@ -579,6 +597,15 @@ function listedPartsByteSize(parts, excludePartNumber) {
|
|
|
579
597
|
}
|
|
580
598
|
return total;
|
|
581
599
|
}
|
|
600
|
+
function applyInlineTypes(inlineTypes, input) {
|
|
601
|
+
if (!inlineTypes) return { disposition: input.disposition };
|
|
602
|
+
return resolveInlineDisposition({
|
|
603
|
+
contentType: input.contentType,
|
|
604
|
+
fileName: input.fileName ?? fileNameFromKey(input.key),
|
|
605
|
+
allow: inlineTypes,
|
|
606
|
+
requested: input.disposition
|
|
607
|
+
});
|
|
608
|
+
}
|
|
582
609
|
async function finalizeConfirmedObject(client, bucket, key, input) {
|
|
583
610
|
const { config, route, stored, head, uploadId, acl } = input;
|
|
584
611
|
const rawMetadata = head.Metadata ?? {};
|
|
@@ -713,7 +740,7 @@ var confirm = createS3Endpoint(
|
|
|
713
740
|
return handleConfirm(ctx.context.config, ctx.body, ctx.context.request);
|
|
714
741
|
}
|
|
715
742
|
);
|
|
716
|
-
async function
|
|
743
|
+
async function deleteStoredObject(config, input, request) {
|
|
717
744
|
const { route, key, bucket, stored } = await openStoredTarget(
|
|
718
745
|
config,
|
|
719
746
|
input,
|
|
@@ -721,34 +748,30 @@ async function handleDelete(config, input, request) {
|
|
|
721
748
|
"delete"
|
|
722
749
|
);
|
|
723
750
|
await runHook(route.delete.guard, stored);
|
|
724
|
-
await
|
|
725
|
-
|
|
726
|
-
|
|
751
|
+
const existed = await objectExists(route.client, bucket, key);
|
|
752
|
+
if (existed) {
|
|
753
|
+
await route.client.send(
|
|
754
|
+
new DeleteObjectCommand({ Bucket: bucket, Key: key })
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
await runLifecycleHook(
|
|
758
|
+
route.delete.onDeleted,
|
|
759
|
+
{ ...stored, existed },
|
|
760
|
+
config
|
|
727
761
|
);
|
|
728
|
-
|
|
729
|
-
return { success: true, bucket, key };
|
|
762
|
+
return { success: true, bucket, key, existed };
|
|
730
763
|
}
|
|
731
764
|
var deleteObject = createS3Endpoint(
|
|
732
765
|
S3_API_ROUTES.delete,
|
|
733
766
|
{ method: "DELETE", query: deleteQuerySchema },
|
|
734
767
|
async (ctx) => {
|
|
735
|
-
return
|
|
768
|
+
return deleteStoredObject(
|
|
769
|
+
ctx.context.config,
|
|
770
|
+
ctx.query,
|
|
771
|
+
ctx.context.request
|
|
772
|
+
);
|
|
736
773
|
}
|
|
737
774
|
);
|
|
738
|
-
async function deleteOne(config, routeName, key, request) {
|
|
739
|
-
const { route, bucket, stored } = await openStoredTarget(
|
|
740
|
-
config,
|
|
741
|
-
{ route: routeName, key },
|
|
742
|
-
request,
|
|
743
|
-
"delete"
|
|
744
|
-
);
|
|
745
|
-
await runHook(route.delete.guard, stored);
|
|
746
|
-
await headObjectOrNotFound(route.client, bucket, stored.key);
|
|
747
|
-
await route.client.send(
|
|
748
|
-
new DeleteObjectCommand({ Bucket: bucket, Key: stored.key })
|
|
749
|
-
);
|
|
750
|
-
await runLifecycleHook(route.delete.onDeleted, stored, config);
|
|
751
|
-
}
|
|
752
775
|
var deleteBatch = createS3Endpoint(
|
|
753
776
|
S3_API_ROUTES.deleteBatch,
|
|
754
777
|
{ method: "POST", body: deleteBatchBodySchema },
|
|
@@ -758,8 +781,12 @@ var deleteBatch = createS3Endpoint(
|
|
|
758
781
|
const results = [];
|
|
759
782
|
for (const key of ctx.body.keys) {
|
|
760
783
|
try {
|
|
761
|
-
|
|
762
|
-
|
|
784
|
+
const { existed } = await deleteStoredObject(
|
|
785
|
+
config,
|
|
786
|
+
{ route: ctx.body.route, key },
|
|
787
|
+
request
|
|
788
|
+
);
|
|
789
|
+
results.push({ key, success: true, existed });
|
|
763
790
|
} catch (err) {
|
|
764
791
|
results.push({
|
|
765
792
|
key,
|
|
@@ -792,33 +819,44 @@ async function handleDownload(config, input, request) {
|
|
|
792
819
|
const resolved = await route.download.resolve?.({
|
|
793
820
|
...stored,
|
|
794
821
|
fileName: input.fileName,
|
|
795
|
-
disposition: input.disposition ?? route.download.disposition
|
|
822
|
+
disposition: input.disposition ?? route.download.disposition,
|
|
823
|
+
expiresIn: input.expiresIn
|
|
796
824
|
});
|
|
797
825
|
const fileName = resolved?.fileName ?? input.fileName;
|
|
798
826
|
const disposition = resolved?.disposition ?? input.disposition ?? route.download.disposition ?? "attachment";
|
|
799
|
-
const
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
827
|
+
const mode = route.download.mode ?? "presign";
|
|
828
|
+
const expiresIn = mode === "proxy" ? 0 : clampDownloadExpiresIn({
|
|
829
|
+
requested: input.expiresIn,
|
|
830
|
+
routeExpiresIn: route.download.expiresIn,
|
|
831
|
+
resolvedExpiresIn: resolved?.expiresIn,
|
|
832
|
+
maxExpiresIn: config.maxExpiresIn
|
|
833
|
+
});
|
|
803
834
|
await runHook(route.download.guard, {
|
|
804
835
|
...stored,
|
|
805
836
|
fileName,
|
|
806
|
-
disposition
|
|
837
|
+
disposition,
|
|
838
|
+
expiresIn
|
|
807
839
|
});
|
|
808
|
-
await headObjectOrNotFound(route.client, bucket, key);
|
|
809
|
-
const
|
|
810
|
-
|
|
840
|
+
const head = await headObjectOrNotFound(route.client, bucket, key);
|
|
841
|
+
const signed = applyInlineTypes(route.download.inlineTypes, {
|
|
842
|
+
key,
|
|
843
|
+
fileName,
|
|
844
|
+
disposition,
|
|
845
|
+
contentType: head.ContentType
|
|
846
|
+
});
|
|
847
|
+
const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
|
|
811
848
|
const url = mode === "proxy" ? fileUrl(request, config.basePath ?? "/api/s3", {
|
|
812
849
|
route: input.route,
|
|
813
850
|
key,
|
|
814
851
|
fileName,
|
|
815
|
-
disposition
|
|
852
|
+
disposition: signed.disposition
|
|
816
853
|
}) : await getSignedUrl(
|
|
817
854
|
route.client,
|
|
818
855
|
new GetObjectCommand({
|
|
819
856
|
Bucket: bucket,
|
|
820
857
|
Key: key,
|
|
821
|
-
ResponseContentDisposition: contentDisposition
|
|
858
|
+
ResponseContentDisposition: contentDisposition,
|
|
859
|
+
ResponseContentType: signed.contentType
|
|
822
860
|
}),
|
|
823
861
|
{ expiresIn }
|
|
824
862
|
);
|
|
@@ -828,7 +866,7 @@ async function handleDownload(config, input, request) {
|
|
|
828
866
|
...stored,
|
|
829
867
|
fileName,
|
|
830
868
|
url,
|
|
831
|
-
expiresIn
|
|
869
|
+
expiresIn
|
|
832
870
|
},
|
|
833
871
|
config
|
|
834
872
|
);
|
|
@@ -836,9 +874,9 @@ async function handleDownload(config, input, request) {
|
|
|
836
874
|
bucket,
|
|
837
875
|
key,
|
|
838
876
|
url,
|
|
839
|
-
expiresIn
|
|
877
|
+
expiresIn,
|
|
840
878
|
mode,
|
|
841
|
-
disposition
|
|
879
|
+
disposition: signed.disposition
|
|
842
880
|
};
|
|
843
881
|
}
|
|
844
882
|
var download = createS3Endpoint(
|
|
@@ -859,11 +897,13 @@ function catalogUpload(upload2) {
|
|
|
859
897
|
...upload2.replace ? { replace: upload2.replace } : {}
|
|
860
898
|
};
|
|
861
899
|
}
|
|
862
|
-
function catalogDownload(download2) {
|
|
900
|
+
function catalogDownload(download2, maxExpiresIn) {
|
|
863
901
|
if (!isEnabled(download2)) return { enabled: false };
|
|
864
902
|
return {
|
|
865
903
|
enabled: true,
|
|
904
|
+
expiresIn: normalizeExpiresIn(download2.expiresIn, maxExpiresIn),
|
|
866
905
|
...download2.disposition ? { disposition: download2.disposition } : {},
|
|
906
|
+
...download2.inlineTypes ? { inlineTypes: [...download2.inlineTypes] } : {},
|
|
867
907
|
...download2.mode ? { mode: download2.mode } : {}
|
|
868
908
|
};
|
|
869
909
|
}
|
|
@@ -872,7 +912,7 @@ function buildRouteCatalog(config) {
|
|
|
872
912
|
for (const [name, route] of Object.entries(config.routes)) {
|
|
873
913
|
routes[name] = {
|
|
874
914
|
upload: catalogUpload(route.upload),
|
|
875
|
-
download: catalogDownload(route.download),
|
|
915
|
+
download: catalogDownload(route.download, config.maxExpiresIn),
|
|
876
916
|
delete: isEnabled(route.delete) ? { enabled: true } : { enabled: false }
|
|
877
917
|
};
|
|
878
918
|
}
|
|
@@ -904,13 +944,20 @@ async function handleFile(config, input, request) {
|
|
|
904
944
|
fileName,
|
|
905
945
|
disposition
|
|
906
946
|
});
|
|
907
|
-
await headObjectOrNotFound(route.client, bucket, key);
|
|
947
|
+
const head = await headObjectOrNotFound(route.client, bucket, key);
|
|
948
|
+
const signed = applyInlineTypes(route.download.inlineTypes, {
|
|
949
|
+
key,
|
|
950
|
+
fileName,
|
|
951
|
+
disposition,
|
|
952
|
+
contentType: head.ContentType
|
|
953
|
+
});
|
|
954
|
+
const contentDisposition = fileName ? buildContentDisposition(fileName, signed.disposition) : signed.disposition;
|
|
908
955
|
const result = await sendOrObjectNotFound(
|
|
909
956
|
() => route.client.send(
|
|
910
957
|
new GetObjectCommand({
|
|
911
958
|
Bucket: bucket,
|
|
912
959
|
Key: key,
|
|
913
|
-
ResponseContentDisposition:
|
|
960
|
+
ResponseContentDisposition: contentDisposition
|
|
914
961
|
})
|
|
915
962
|
)
|
|
916
963
|
);
|
|
@@ -918,11 +965,12 @@ async function handleFile(config, input, request) {
|
|
|
918
965
|
if (!body) throw errors.objectNotFound();
|
|
919
966
|
const stream = "transformToWebStream" in body && typeof body.transformToWebStream === "function" ? body.transformToWebStream() : body;
|
|
920
967
|
const headers = new Headers();
|
|
921
|
-
headers.set("Content-Type", result.ContentType ?? "application/octet-stream");
|
|
922
968
|
headers.set(
|
|
923
|
-
"Content-
|
|
924
|
-
|
|
969
|
+
"Content-Type",
|
|
970
|
+
signed.contentType ?? result.ContentType ?? "application/octet-stream"
|
|
925
971
|
);
|
|
972
|
+
headers.set("Content-Disposition", contentDisposition);
|
|
973
|
+
headers.set("X-Content-Type-Options", "nosniff");
|
|
926
974
|
if (typeof result.ContentLength === "number") {
|
|
927
975
|
headers.set("Content-Length", String(result.ContentLength));
|
|
928
976
|
}
|