@dimah-s3/server 1.4.2 → 1.5.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 +101 -4
- package/dist/api/index.js.map +1 -1
- package/dist/api/routes/multipart/init.d.ts.map +1 -1
- package/dist/api/routes/upload.d.ts.map +1 -1
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/object-s3.d.ts +21 -0
- package/dist/helpers/object-s3.d.ts.map +1 -0
- package/dist/helpers/resolve-route.d.ts +2 -5
- package/dist/helpers/resolve-route.d.ts.map +1 -1
- package/dist/helpers/resolve-target.d.ts +2 -2
- package/dist/helpers/resolve-target.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +120 -10
- package/dist/index.js.map +1 -1
- package/dist/plugins.js +105 -8
- package/dist/plugins.js.map +1 -1
- package/dist/put.d.ts.map +1 -1
- package/dist/types/config.d.ts +2 -1
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/hook-contexts.d.ts +18 -1
- package/dist/types/hook-contexts.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +6 -6
package/dist/api/index.js
CHANGED
|
@@ -169,6 +169,73 @@ function previousKeyFromMetadata(metadata) {
|
|
|
169
169
|
return value || void 0;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
// src/helpers/object-s3.ts
|
|
173
|
+
var MAX_S3_TAGS = 10;
|
|
174
|
+
var MAX_TAG_KEY_CHARS = 128;
|
|
175
|
+
var MAX_TAG_VALUE_CHARS = 256;
|
|
176
|
+
function present(value) {
|
|
177
|
+
const trimmed = value?.trim();
|
|
178
|
+
return trimmed ? trimmed : void 0;
|
|
179
|
+
}
|
|
180
|
+
function assertTagEntry(key, value) {
|
|
181
|
+
if (key.length === 0) {
|
|
182
|
+
throw errors.validationError("S3 object tag keys cannot be empty");
|
|
183
|
+
}
|
|
184
|
+
if (key.length > MAX_TAG_KEY_CHARS) {
|
|
185
|
+
throw errors.validationError(
|
|
186
|
+
`S3 object tag keys cannot exceed ${MAX_TAG_KEY_CHARS} characters`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (value.length > MAX_TAG_VALUE_CHARS) {
|
|
190
|
+
throw errors.validationError(
|
|
191
|
+
`S3 object tag values cannot exceed ${MAX_TAG_VALUE_CHARS} characters`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function encodeObjectTagging(tagging) {
|
|
196
|
+
const entries = Object.entries(tagging);
|
|
197
|
+
if (entries.length > MAX_S3_TAGS) {
|
|
198
|
+
throw errors.validationError(
|
|
199
|
+
`S3 object tagging allows at most ${MAX_S3_TAGS} tags`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return entries.map(([key, value]) => {
|
|
203
|
+
assertTagEntry(key, value);
|
|
204
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
205
|
+
}).join("&");
|
|
206
|
+
}
|
|
207
|
+
function taggingHeader(tagging) {
|
|
208
|
+
if (!tagging || Object.keys(tagging).length === 0) return void 0;
|
|
209
|
+
return encodeObjectTagging(tagging);
|
|
210
|
+
}
|
|
211
|
+
function normalizeObjectS3(info) {
|
|
212
|
+
const storageClass = present(info?.storageClass);
|
|
213
|
+
const cacheControl = present(info?.cacheControl);
|
|
214
|
+
const tagging = info?.tagging && Object.keys(info.tagging).length > 0 ? info.tagging : void 0;
|
|
215
|
+
if (tagging) encodeObjectTagging(tagging);
|
|
216
|
+
return {
|
|
217
|
+
...storageClass ? { storageClass } : {},
|
|
218
|
+
...cacheControl ? { cacheControl } : {},
|
|
219
|
+
...tagging ? { tagging } : {}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function objectCommandExtras(opts) {
|
|
223
|
+
const tagging = taggingHeader(opts.tagging);
|
|
224
|
+
return {
|
|
225
|
+
...opts.storageClass ? { StorageClass: opts.storageClass } : {},
|
|
226
|
+
...opts.cacheControl ? { CacheControl: opts.cacheControl } : {},
|
|
227
|
+
...tagging ? { Tagging: tagging } : {}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function objectPutHeaders(opts) {
|
|
231
|
+
const tagging = taggingHeader(opts.tagging);
|
|
232
|
+
return {
|
|
233
|
+
...opts.cacheControl ? { "Cache-Control": opts.cacheControl } : {},
|
|
234
|
+
...opts.storageClass ? { "x-amz-storage-class": opts.storageClass } : {},
|
|
235
|
+
...tagging ? { "x-amz-tagging": tagging } : {}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
172
239
|
// src/helpers/resolve-target.ts
|
|
173
240
|
function assertSafeObjectKey(key) {
|
|
174
241
|
const normalized = normalizeObjectKey(key);
|
|
@@ -231,7 +298,8 @@ async function resolveUploadTarget(route, context) {
|
|
|
231
298
|
key,
|
|
232
299
|
bucket,
|
|
233
300
|
metadata: Object.keys(metadata).length > 0 ? metadata : info?.metadata,
|
|
234
|
-
acl: resolveAcl(info, route)
|
|
301
|
+
acl: resolveAcl(info, route),
|
|
302
|
+
...normalizeObjectS3(info)
|
|
235
303
|
};
|
|
236
304
|
}
|
|
237
305
|
function resolveStoredTarget(route, key) {
|
|
@@ -855,7 +923,17 @@ var multipartComplete = createS3Endpoint(
|
|
|
855
923
|
async function handleMultipartInit(config, input, request) {
|
|
856
924
|
const fileSize = Math.floor(input.fileSize);
|
|
857
925
|
const fileName = input.fileName;
|
|
858
|
-
const {
|
|
926
|
+
const {
|
|
927
|
+
route,
|
|
928
|
+
key,
|
|
929
|
+
bucket,
|
|
930
|
+
metadata,
|
|
931
|
+
acl,
|
|
932
|
+
storageClass,
|
|
933
|
+
cacheControl,
|
|
934
|
+
tagging,
|
|
935
|
+
stored
|
|
936
|
+
} = await openUploadTarget(
|
|
859
937
|
config,
|
|
860
938
|
{
|
|
861
939
|
route: input.route,
|
|
@@ -867,6 +945,7 @@ async function handleMultipartInit(config, input, request) {
|
|
|
867
945
|
request,
|
|
868
946
|
"multipart"
|
|
869
947
|
);
|
|
948
|
+
const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
|
|
870
949
|
await runHook(route.upload.guard, {
|
|
871
950
|
...stored,
|
|
872
951
|
file: {
|
|
@@ -877,6 +956,7 @@ async function handleMultipartInit(config, input, request) {
|
|
|
877
956
|
metadata,
|
|
878
957
|
clientMetadata: input.metadata,
|
|
879
958
|
acl,
|
|
959
|
+
...objectS3,
|
|
880
960
|
replace: route.upload.replace
|
|
881
961
|
});
|
|
882
962
|
const { UploadId } = await route.client.send(
|
|
@@ -886,7 +966,8 @@ async function handleMultipartInit(config, input, request) {
|
|
|
886
966
|
ContentType: input.contentType,
|
|
887
967
|
ContentDisposition: buildContentDisposition(fileName),
|
|
888
968
|
Metadata: metadata,
|
|
889
|
-
ACL: acl
|
|
969
|
+
ACL: acl,
|
|
970
|
+
...objectCommandExtras(objectS3)
|
|
890
971
|
})
|
|
891
972
|
);
|
|
892
973
|
if (!UploadId) {
|
|
@@ -906,6 +987,7 @@ async function handleMultipartInit(config, input, request) {
|
|
|
906
987
|
metadata,
|
|
907
988
|
clientMetadata: input.metadata,
|
|
908
989
|
acl,
|
|
990
|
+
...objectS3,
|
|
909
991
|
replace: route.upload.replace
|
|
910
992
|
},
|
|
911
993
|
config
|
|
@@ -1053,7 +1135,17 @@ function objectUserMetadata(metadata) {
|
|
|
1053
1135
|
async function handleUpload(config, input, request) {
|
|
1054
1136
|
const fileSize = Math.floor(input.fileSize);
|
|
1055
1137
|
const fileName = input.fileName;
|
|
1056
|
-
const {
|
|
1138
|
+
const {
|
|
1139
|
+
route,
|
|
1140
|
+
key,
|
|
1141
|
+
bucket,
|
|
1142
|
+
metadata,
|
|
1143
|
+
acl,
|
|
1144
|
+
storageClass,
|
|
1145
|
+
cacheControl,
|
|
1146
|
+
tagging,
|
|
1147
|
+
stored
|
|
1148
|
+
} = await openUploadTarget(
|
|
1057
1149
|
config,
|
|
1058
1150
|
{
|
|
1059
1151
|
route: input.route,
|
|
@@ -1065,6 +1157,7 @@ async function handleUpload(config, input, request) {
|
|
|
1065
1157
|
request,
|
|
1066
1158
|
"upload"
|
|
1067
1159
|
);
|
|
1160
|
+
const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
|
|
1068
1161
|
const method = route.upload.method ?? "POST";
|
|
1069
1162
|
if (route.upload.checksum && !input.checksum) {
|
|
1070
1163
|
throw errors.validationError("Checksum is required for this route");
|
|
@@ -1089,6 +1182,7 @@ async function handleUpload(config, input, request) {
|
|
|
1089
1182
|
metadata,
|
|
1090
1183
|
clientMetadata: input.metadata,
|
|
1091
1184
|
acl,
|
|
1185
|
+
...objectS3,
|
|
1092
1186
|
replace: route.upload.replace
|
|
1093
1187
|
};
|
|
1094
1188
|
await runHook(route.upload.guard, hookCtx);
|
|
@@ -1097,6 +1191,7 @@ async function handleUpload(config, input, request) {
|
|
|
1097
1191
|
"Content-Type": contentType,
|
|
1098
1192
|
...objectUserMetadata(metadata),
|
|
1099
1193
|
"Content-Disposition": buildContentDisposition(fileName),
|
|
1194
|
+
...objectPutHeaders(objectS3),
|
|
1100
1195
|
...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
|
|
1101
1196
|
};
|
|
1102
1197
|
const url2 = await getSignedUrl(
|
|
@@ -1109,6 +1204,7 @@ async function handleUpload(config, input, request) {
|
|
|
1109
1204
|
Metadata: metadata,
|
|
1110
1205
|
ContentDisposition: buildContentDisposition(fileName),
|
|
1111
1206
|
ContentLength: fileSize,
|
|
1207
|
+
...objectCommandExtras(objectS3),
|
|
1112
1208
|
...input.checksum ? { ChecksumSHA256: input.checksum, ChecksumAlgorithm: "SHA256" } : {}
|
|
1113
1209
|
}),
|
|
1114
1210
|
{
|
|
@@ -1142,6 +1238,7 @@ async function handleUpload(config, input, request) {
|
|
|
1142
1238
|
"Content-Type": contentType,
|
|
1143
1239
|
"Content-Disposition": buildContentDisposition(fileName),
|
|
1144
1240
|
...objectUserMetadata(metadata),
|
|
1241
|
+
...objectPutHeaders(objectS3),
|
|
1145
1242
|
...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
|
|
1146
1243
|
};
|
|
1147
1244
|
const { url, fields: signedFields } = await createPresignedPost(
|