@dimah-s3/server 1.7.1 → 2.0.0-beta.1
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/README.md +72 -16
- package/dist/adapters/express.d.ts +7 -7
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +94 -94
- package/dist/adapters/express.js.map +1 -1
- package/dist/adapters/fastify.d.ts +11 -5
- package/dist/adapters/fastify.d.ts.map +1 -1
- package/dist/adapters/fastify.js +94 -94
- package/dist/adapters/fastify.js.map +1 -1
- package/dist/adapters/node.d.ts +20 -3
- package/dist/adapters/node.d.ts.map +1 -1
- package/dist/adapters/node.js +92 -92
- package/dist/adapters/node.js.map +1 -1
- package/dist/api/context.d.ts +8 -4
- package/dist/api/context.d.ts.map +1 -1
- package/dist/api/{routes/index.d.ts → core-endpoints.d.ts} +43 -68
- package/dist/api/core-endpoints.d.ts.map +1 -0
- package/dist/api/create-s3-endpoint.d.ts +70 -48
- package/dist/api/create-s3-endpoint.d.ts.map +1 -1
- package/dist/api/create-s3-middleware.d.ts +19 -16
- package/dist/api/create-s3-middleware.d.ts.map +1 -1
- package/dist/api/router-context.d.ts +23 -0
- package/dist/api/router-context.d.ts.map +1 -0
- package/dist/api/router.d.ts +15 -5
- package/dist/api/router.d.ts.map +1 -1
- package/dist/config/feature-toggle.d.ts +16 -0
- package/dist/config/feature-toggle.d.ts.map +1 -0
- package/dist/config/resolve-config.d.ts +9 -0
- package/dist/config/resolve-config.d.ts.map +1 -0
- package/dist/config/resolve-routes.d.ts +37 -0
- package/dist/config/resolve-routes.d.ts.map +1 -0
- package/dist/dimah-s3.d.ts +22 -32
- package/dist/dimah-s3.d.ts.map +1 -1
- package/dist/helpers/authorize-download.d.ts +25 -0
- package/dist/helpers/authorize-download.d.ts.map +1 -0
- package/dist/helpers/aws-errors.d.ts +16 -0
- package/dist/helpers/aws-errors.d.ts.map +1 -0
- package/dist/helpers/best-effort.d.ts +22 -3
- package/dist/helpers/best-effort.d.ts.map +1 -1
- package/dist/helpers/checksum-config.d.ts +13 -0
- package/dist/helpers/checksum-config.d.ts.map +1 -0
- package/dist/helpers/constraints.d.ts +17 -12
- package/dist/helpers/constraints.d.ts.map +1 -1
- package/dist/helpers/error-boundary.d.ts +28 -0
- package/dist/helpers/error-boundary.d.ts.map +1 -0
- package/dist/helpers/finalize-confirmed.d.ts +42 -13
- package/dist/helpers/finalize-confirmed.d.ts.map +1 -1
- package/dist/helpers/head-object.d.ts +6 -9
- package/dist/helpers/head-object.d.ts.map +1 -1
- package/dist/helpers/hooks.d.ts +29 -15
- package/dist/helpers/hooks.d.ts.map +1 -1
- package/dist/helpers/inline-types.d.ts +24 -7
- package/dist/helpers/inline-types.d.ts.map +1 -1
- package/dist/helpers/list-parts.d.ts +4 -6
- package/dist/helpers/list-parts.d.ts.map +1 -1
- package/dist/helpers/metadata-encoding.d.ts +15 -0
- package/dist/helpers/metadata-encoding.d.ts.map +1 -0
- package/dist/helpers/object-meta.d.ts +39 -0
- package/dist/helpers/object-meta.d.ts.map +1 -0
- package/dist/helpers/object-s3.d.ts +5 -2
- package/dist/helpers/object-s3.d.ts.map +1 -1
- package/dist/helpers/open-target.d.ts +89 -0
- package/dist/helpers/open-target.d.ts.map +1 -0
- package/dist/helpers/part-layout.d.ts +19 -0
- package/dist/helpers/part-layout.d.ts.map +1 -0
- package/dist/helpers/presign.d.ts +54 -0
- package/dist/helpers/presign.d.ts.map +1 -0
- package/dist/helpers/request.d.ts +6 -11
- package/dist/helpers/request.d.ts.map +1 -1
- package/dist/helpers/resolve-target.d.ts +28 -20
- package/dist/helpers/resolve-target.d.ts.map +1 -1
- package/dist/helpers/sniff-object.d.ts +22 -0
- package/dist/helpers/sniff-object.d.ts.map +1 -0
- package/dist/helpers/ticket.d.ts +75 -0
- package/dist/helpers/ticket.d.ts.map +1 -0
- package/dist/index.d.ts +4 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2647 -1579
- package/dist/index.js.map +1 -1
- package/dist/operations/catalog.d.ts +16 -0
- package/dist/operations/catalog.d.ts.map +1 -0
- package/dist/operations/confirm.d.ts +19 -0
- package/dist/operations/confirm.d.ts.map +1 -0
- package/dist/operations/delete-many.d.ts +18 -0
- package/dist/operations/delete-many.d.ts.map +1 -0
- package/dist/operations/delete.d.ts +14 -0
- package/dist/operations/delete.d.ts.map +1 -0
- package/dist/operations/download.d.ts +20 -0
- package/dist/operations/download.d.ts.map +1 -0
- package/dist/operations/file.d.ts +38 -0
- package/dist/operations/file.d.ts.map +1 -0
- package/dist/operations/multipart/abort.d.ts +20 -0
- package/dist/operations/multipart/abort.d.ts.map +1 -0
- package/dist/operations/multipart/complete.d.ts +21 -0
- package/dist/operations/multipart/complete.d.ts.map +1 -0
- package/dist/operations/multipart/init.d.ts +19 -0
- package/dist/operations/multipart/init.d.ts.map +1 -0
- package/dist/operations/multipart/list-parts.d.ts +11 -0
- package/dist/operations/multipart/list-parts.d.ts.map +1 -0
- package/dist/operations/multipart/open-multipart.d.ts +25 -0
- package/dist/operations/multipart/open-multipart.d.ts.map +1 -0
- package/dist/operations/multipart/sign-part.d.ts +20 -0
- package/dist/operations/multipart/sign-part.d.ts.map +1 -0
- package/dist/operations/put.d.ts +18 -0
- package/dist/operations/put.d.ts.map +1 -0
- package/dist/operations/run-operation.d.ts +18 -0
- package/dist/operations/run-operation.d.ts.map +1 -0
- package/dist/operations/s3-api.d.ts +12 -0
- package/dist/operations/s3-api.d.ts.map +1 -0
- package/dist/operations/upload.d.ts +19 -0
- package/dist/operations/upload.d.ts.map +1 -0
- package/dist/plugin/apply-plugins.d.ts +9 -11
- package/dist/plugin/apply-plugins.d.ts.map +1 -1
- package/dist/plugin/chain-hooks.d.ts +12 -9
- package/dist/plugin/chain-hooks.d.ts.map +1 -1
- package/dist/plugin/define-plugin.d.ts +11 -4
- package/dist/plugin/define-plugin.d.ts.map +1 -1
- package/dist/plugin/index.d.ts +1 -2
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/types.d.ts +52 -56
- package/dist/plugin/types.d.ts.map +1 -1
- package/dist/plugins.d.ts +10 -8
- package/dist/plugins.d.ts.map +1 -1
- package/dist/plugins.js +325 -1627
- package/dist/plugins.js.map +1 -1
- package/dist/route.d.ts +0 -1
- package/dist/route.d.ts.map +1 -1
- package/dist/types/config.d.ts +171 -172
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/hook-contexts.d.ts +139 -147
- package/dist/types/hook-contexts.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/locals.d.ts +37 -0
- package/dist/types/locals.d.ts.map +1 -0
- package/dist/types/resolved.d.ts +118 -0
- package/dist/types/resolved.d.ts.map +1 -0
- package/package.json +8 -12
- package/dist/api/bind-endpoints.d.ts +0 -12
- package/dist/api/bind-endpoints.d.ts.map +0 -1
- package/dist/api/index.d.ts +0 -6
- package/dist/api/index.d.ts.map +0 -1
- package/dist/api/index.js +0 -1353
- package/dist/api/index.js.map +0 -1
- package/dist/api/routes/catalog.d.ts +0 -9
- package/dist/api/routes/catalog.d.ts.map +0 -1
- package/dist/api/routes/confirm.d.ts +0 -13
- package/dist/api/routes/confirm.d.ts.map +0 -1
- package/dist/api/routes/delete-batch.d.ts +0 -14
- package/dist/api/routes/delete-batch.d.ts.map +0 -1
- package/dist/api/routes/delete.d.ts +0 -26
- package/dist/api/routes/delete.d.ts.map +0 -1
- package/dist/api/routes/download.d.ts +0 -21
- package/dist/api/routes/download.d.ts.map +0 -1
- package/dist/api/routes/file.d.ts +0 -20
- package/dist/api/routes/file.d.ts.map +0 -1
- package/dist/api/routes/index.d.ts.map +0 -1
- package/dist/api/routes/multipart/abort.d.ts +0 -16
- package/dist/api/routes/multipart/abort.d.ts.map +0 -1
- package/dist/api/routes/multipart/complete.d.ts +0 -22
- package/dist/api/routes/multipart/complete.d.ts.map +0 -1
- package/dist/api/routes/multipart/init.d.ts +0 -22
- package/dist/api/routes/multipart/init.d.ts.map +0 -1
- package/dist/api/routes/multipart/list-parts.d.ts +0 -17
- package/dist/api/routes/multipart/list-parts.d.ts.map +0 -1
- package/dist/api/routes/multipart/part.d.ts +0 -20
- package/dist/api/routes/multipart/part.d.ts.map +0 -1
- package/dist/api/routes/upload.d.ts +0 -22
- package/dist/api/routes/upload.d.ts.map +0 -1
- package/dist/errors.d.ts +0 -23
- package/dist/errors.d.ts.map +0 -1
- package/dist/helpers/expires.d.ts +0 -21
- package/dist/helpers/expires.d.ts.map +0 -1
- package/dist/helpers/features.d.ts +0 -17
- package/dist/helpers/features.d.ts.map +0 -1
- package/dist/helpers/index.d.ts +0 -19
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/is-aws-not-found.d.ts +0 -5
- package/dist/helpers/is-aws-not-found.d.ts.map +0 -1
- package/dist/helpers/previous-key.d.ts +0 -5
- package/dist/helpers/previous-key.d.ts.map +0 -1
- package/dist/helpers/resolve-route.d.ts +0 -33
- package/dist/helpers/resolve-route.d.ts.map +0 -1
- package/dist/plugin/hook-registry.d.ts +0 -21
- package/dist/plugin/hook-registry.d.ts.map +0 -1
- package/dist/put.d.ts +0 -15
- package/dist/put.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,52 +1,395 @@
|
|
|
1
|
-
import { S3_API_ROUTES,
|
|
2
|
-
export { APIError, S3_ERROR_CODES, isAPIError, isS3ErrorCode } from '@dimah-s3/core';
|
|
3
|
-
import { PutObjectCommand, DeleteObjectCommand, GetObjectCommand, CreateMultipartUploadCommand, UploadPartCommand, HeadObjectCommand, ListPartsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3';
|
|
1
|
+
import { S3_API_ROUTES, s3Error, APIError, S3_ERROR_CODES, S3_MAX_EXPIRES_IN, resolveStoredFileName, resolveInlineDisposition, buildContentDisposition, isAPIError, normalizeS3ApiBasePath, S3_API_BASE_PATH, normalizeObjectKey, ROUTE_NAME_PATTERN, DEFAULT_INLINE_TYPES, S3_DEFAULT_EXPIRES_IN, isS3ErrorCode, S3_MAX_OBJECT_SIZE, base64UrlEncode, matchesFileTypes, describeFileType, S3_MAX_POST_OBJECT_BYTES, S3_MIN_PART_SIZE, S3_MAX_PART_SIZE, base64UrlDecode, effectiveContentType, isSniffableType, matchesMagicBytes, sniffContentType, S3_DEFAULT_PART_SIZE, S3_MAX_PART_NUMBER, SNIFF_BYTE_LENGTH } from '@dimah-s3/core';
|
|
2
|
+
export { APIError, S3_ERROR_CODES, isAPIError, isS3ErrorCode, s3Error } from '@dimah-s3/core';
|
|
4
3
|
import { createMiddleware, createEndpoint, createRouter, toResponse } from 'better-call';
|
|
5
|
-
import
|
|
4
|
+
import { statusCodes, APIError as APIError$1 } from 'better-call/error';
|
|
5
|
+
import * as z2 from 'zod';
|
|
6
|
+
import { s3ObjectAclSchema, uploadBodySchema, ticketBodySchema, multipartSignPartBodySchema, multipartInitBodySchema, deleteManyBodySchema, deleteQuerySchema, fileQuerySchema, downloadQuerySchema } from '@dimah-s3/core/schema';
|
|
7
|
+
import { GetObjectCommand, HeadObjectCommand, DeleteObjectCommand, DeleteObjectsCommand, AbortMultipartUploadCommand, CompleteMultipartUploadCommand, CreateMultipartUploadCommand, PutObjectCommand, ListPartsCommand, UploadPartCommand } from '@aws-sdk/client-s3';
|
|
6
8
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
7
9
|
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
|
|
8
10
|
|
|
9
11
|
// src/dimah-s3.ts
|
|
12
|
+
var NETWORK_CODES = /* @__PURE__ */ new Set([
|
|
13
|
+
"EAI_AGAIN",
|
|
14
|
+
"ECONNREFUSED",
|
|
15
|
+
"ECONNRESET",
|
|
16
|
+
"EHOSTUNREACH",
|
|
17
|
+
"ENETUNREACH",
|
|
18
|
+
"ENOTFOUND",
|
|
19
|
+
"EPIPE",
|
|
20
|
+
"ETIMEDOUT",
|
|
21
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
22
|
+
"UND_ERR_SOCKET"
|
|
23
|
+
]);
|
|
24
|
+
var STATUS_NAMES = new Map(
|
|
25
|
+
Object.entries(statusCodes).map(([name, status]) => [status, name])
|
|
26
|
+
);
|
|
27
|
+
var ConfigError = class extends Error {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "ConfigError";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function toAPIError(error, env, request, operation) {
|
|
34
|
+
if (isHttpError(error)) return normalizeAPIError(error);
|
|
35
|
+
const networkCode = socketErrorCode(error);
|
|
36
|
+
const result = networkCode ? s3Error("S3_NETWORK_ERROR", { code: networkCode }, { cause: error }) : s3Error("INTERNAL_ERROR", void 0, { cause: error });
|
|
37
|
+
if (!request.signal.aborted) {
|
|
38
|
+
report(error, result, env, { request, operation });
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
function report(error, result, env, context) {
|
|
43
|
+
env.logger.error?.(logMessage(error, result), error);
|
|
44
|
+
const failed = (hookError) => {
|
|
45
|
+
env.logger.error?.("onError threw", hookError);
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
void Promise.resolve(env.onError?.(error, context)).catch(failed);
|
|
49
|
+
} catch (hookError) {
|
|
50
|
+
failed(hookError);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function isHttpError(error) {
|
|
54
|
+
if (error instanceof APIError || error instanceof APIError$1) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
const candidate = error;
|
|
58
|
+
return candidate?.name === "APIError" && typeof candidate.statusCode === "number" && typeof candidate.body?.code === "string";
|
|
59
|
+
}
|
|
60
|
+
function logMessage(error, result) {
|
|
61
|
+
if (error instanceof ConfigError) return `Config error: ${error.message}`;
|
|
62
|
+
if (result.code === "S3_NETWORK_ERROR") return result.message;
|
|
63
|
+
return error instanceof Error ? error.message : String(error);
|
|
64
|
+
}
|
|
65
|
+
function socketErrorCode(error) {
|
|
66
|
+
const candidates = [error, error?.cause];
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
const code = candidate?.code;
|
|
69
|
+
if (typeof code === "string" && NETWORK_CODES.has(code)) return code;
|
|
70
|
+
}
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
function normalizeAPIError(error) {
|
|
74
|
+
if (error instanceof APIError) return error;
|
|
75
|
+
const body = error.body ?? {};
|
|
76
|
+
const status = typeof error.statusCode === "number" ? error.statusCode : 500;
|
|
77
|
+
const message = typeof body.message === "string" ? body.message : error.message;
|
|
78
|
+
const code = typeof body.code === "string" ? body.code : STATUS_NAMES.get(status) ?? "INTERNAL_ERROR";
|
|
79
|
+
switch (code) {
|
|
80
|
+
case "UNSUPPORTED_MEDIA_TYPE":
|
|
81
|
+
return s3Error("UNSUPPORTED_MEDIA_TYPE", void 0, { cause: error });
|
|
82
|
+
case "BAD_REQUEST":
|
|
83
|
+
case "VALIDATION_ERROR":
|
|
84
|
+
return s3Error("VALIDATION_ERROR", { issue: message }, { cause: error });
|
|
85
|
+
case "INTERNAL_SERVER_ERROR":
|
|
86
|
+
return s3Error("INTERNAL_ERROR", void 0, { cause: error });
|
|
87
|
+
}
|
|
88
|
+
return new APIError(
|
|
89
|
+
status,
|
|
90
|
+
{
|
|
91
|
+
code,
|
|
92
|
+
message,
|
|
93
|
+
...body.params !== void 0 ? { params: body.params } : {},
|
|
94
|
+
cause: error
|
|
95
|
+
},
|
|
96
|
+
error.headers
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/helpers/hooks.ts
|
|
101
|
+
function createLocals(seed) {
|
|
102
|
+
const locals = /* @__PURE__ */ Object.create(null);
|
|
103
|
+
if (seed) mergeLocals(locals, seed);
|
|
104
|
+
return locals;
|
|
105
|
+
}
|
|
106
|
+
async function runGuards(guards, context) {
|
|
107
|
+
for (const { plugin, hook } of guards) {
|
|
108
|
+
const result = await hook(context);
|
|
109
|
+
if (result === void 0) continue;
|
|
110
|
+
if (!isGuardResult(result)) {
|
|
111
|
+
throw new ConfigError(
|
|
112
|
+
`${guardName(plugin)} for "${context.operation}" returned ${describeValue(result)}. Return nothing or { locals } to allow, and throw to reject.`
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
mergeLocals(context.locals, result.locals);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function runLifecycle(hooks, context, options = {}) {
|
|
119
|
+
for (const { plugin, hook } of hooks) {
|
|
120
|
+
try {
|
|
121
|
+
await hook(context);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (plugin !== void 0) await options.onPluginError?.(error);
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function mergeLocals(target, source) {
|
|
129
|
+
for (const [key, value] of Object.entries(source)) {
|
|
130
|
+
target[key] = value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function isGuardResult(value) {
|
|
134
|
+
if (!isPlainObject(value)) return false;
|
|
135
|
+
const keys = Object.keys(value);
|
|
136
|
+
return keys.length === 1 && keys[0] === "locals" && typeof value.locals === "object" && value.locals !== null && !Array.isArray(value.locals);
|
|
137
|
+
}
|
|
138
|
+
function isPlainObject(value) {
|
|
139
|
+
if (typeof value !== "object" || value === null) return false;
|
|
140
|
+
const proto = Object.getPrototypeOf(value);
|
|
141
|
+
return proto === Object.prototype || proto === null;
|
|
142
|
+
}
|
|
143
|
+
function guardName(plugin) {
|
|
144
|
+
return plugin === void 0 ? "A guard" : `The "${plugin}" plugin guard`;
|
|
145
|
+
}
|
|
146
|
+
function describeValue(value) {
|
|
147
|
+
switch (typeof value) {
|
|
148
|
+
case "string":
|
|
149
|
+
return "a string";
|
|
150
|
+
case "function":
|
|
151
|
+
return "a function";
|
|
152
|
+
case "object":
|
|
153
|
+
if (value === null) return "null";
|
|
154
|
+
return Array.isArray(value) ? "an array" : "an object other than { locals }";
|
|
155
|
+
default:
|
|
156
|
+
return String(value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
10
159
|
|
|
11
160
|
// src/helpers/request.ts
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
161
|
+
function syntheticRequest(config, init = {}) {
|
|
162
|
+
return new Request(config.baseURL ?? `http://localhost${config.basePath}`, {
|
|
163
|
+
headers: init.headers,
|
|
164
|
+
signal: init.signal
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/api/router-context.ts
|
|
169
|
+
var RESOLVED_CONFIG = /* @__PURE__ */ Symbol.for("dimah-s3.resolved-config");
|
|
170
|
+
function createRouterContext(config, resolved) {
|
|
171
|
+
return { config, [RESOLVED_CONFIG]: resolved };
|
|
172
|
+
}
|
|
173
|
+
function resolvedConfig(context) {
|
|
174
|
+
const resolved = context[RESOLVED_CONFIG];
|
|
175
|
+
if (!resolved) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
"createS3Endpoint needs the dimahS3 router context. Mount the endpoint as a plugin endpoint and call it through the HTTP handler."
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return resolved;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/api/create-s3-middleware.ts
|
|
184
|
+
function contextMiddleware(options) {
|
|
185
|
+
return createMiddleware(async (ctx) => {
|
|
186
|
+
const resolved = resolvedConfig(ctx.context);
|
|
187
|
+
const injected = ctx.context;
|
|
188
|
+
const { config } = injected;
|
|
189
|
+
const request = ctx.request ?? syntheticRequest(resolved, { headers: ctx.headers });
|
|
190
|
+
if (injected.locals) return { config, request, locals: injected.locals };
|
|
191
|
+
const locals = createLocals();
|
|
192
|
+
if (options.runGuards) {
|
|
193
|
+
try {
|
|
194
|
+
await runGuards(resolved.guard, {
|
|
195
|
+
request,
|
|
196
|
+
operation: "plugin",
|
|
197
|
+
locals
|
|
198
|
+
});
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw toAPIError(error, resolved, request, "plugin");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { config, request, locals };
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
var s3ContextMiddleware = contextMiddleware({ runGuards: true });
|
|
207
|
+
var coreContextMiddleware = contextMiddleware({ runGuards: false });
|
|
208
|
+
createMiddleware.create({
|
|
209
|
+
use: [s3ContextMiddleware]
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// src/api/create-s3-endpoint.ts
|
|
213
|
+
createEndpoint.create({
|
|
214
|
+
use: [s3ContextMiddleware]
|
|
215
|
+
});
|
|
216
|
+
var coreEndpoint = createEndpoint.create({
|
|
217
|
+
use: [coreContextMiddleware]
|
|
218
|
+
});
|
|
219
|
+
var BODY_MEDIA_TYPES = ["application/json"];
|
|
220
|
+
new Set(Object.values(S3_API_ROUTES));
|
|
221
|
+
function createCoreEndpoint(path, options, handler) {
|
|
222
|
+
return buildEndpoint(coreEndpoint, path, options, handler);
|
|
223
|
+
}
|
|
224
|
+
function buildEndpoint(create, path, options, handler, operation) {
|
|
225
|
+
const endpoint = create(path, endpointOptions(path, options), async (ctx) => {
|
|
226
|
+
try {
|
|
227
|
+
return await handler(ctx);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
const apiError = toAPIError(
|
|
230
|
+
error,
|
|
231
|
+
resolvedConfig(ctx.context),
|
|
232
|
+
ctx.context.request,
|
|
233
|
+
operation
|
|
234
|
+
);
|
|
235
|
+
new Headers(apiError.headers).forEach((value, name) => {
|
|
236
|
+
ctx.setHeader(name, value);
|
|
237
|
+
});
|
|
238
|
+
throw apiError;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return withCallContext(endpoint);
|
|
242
|
+
}
|
|
243
|
+
function endpointOptions(path, options) {
|
|
244
|
+
const { method } = options;
|
|
245
|
+
if (!["GET", "POST", "PATCH", "DELETE"].includes(method)) {
|
|
246
|
+
throw new Error(
|
|
247
|
+
`createS3Endpoint("${path}"): method must be GET, POST, PATCH or DELETE.`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const readsBody = method === "POST" || method === "PATCH";
|
|
251
|
+
if (!readsBody && options.body !== void 0) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
`createS3Endpoint("${path}"): ${method} reads the query, so it cannot have a body schema.`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
...options,
|
|
258
|
+
...options.body !== void 0 ? { body: compileIfZod(options.body) } : {},
|
|
259
|
+
...options.query !== void 0 ? { query: compileIfZod(options.query) } : {},
|
|
260
|
+
metadata: readsBody ? { ...options.metadata, allowedMediaTypes: BODY_MEDIA_TYPES } : options.metadata,
|
|
261
|
+
// A DELETE is never parsed for a body, so a form post cannot reach it.
|
|
262
|
+
...method === "DELETE" ? { disableBody: true } : {},
|
|
263
|
+
// The router reads the body before the hooks run. Reading a clone keeps
|
|
264
|
+
// the original request readable for guards.
|
|
265
|
+
...readsBody && options.cloneRequest === void 0 ? { cloneRequest: true } : {},
|
|
266
|
+
onValidationError: options.onValidationError ?? (({ message }) => {
|
|
267
|
+
throw s3Error("VALIDATION_ERROR", { issue: message });
|
|
268
|
+
})
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function compileIfZod(schema) {
|
|
272
|
+
if ("_zod" in schema) {
|
|
273
|
+
return z2.compile(schema);
|
|
274
|
+
}
|
|
275
|
+
return schema;
|
|
276
|
+
}
|
|
277
|
+
function withCallContext(endpoint) {
|
|
278
|
+
const call2 = (input = {}) => endpoint({ ...input, context: { ...input.context } });
|
|
279
|
+
return Object.assign(call2, {
|
|
280
|
+
path: endpoint.path,
|
|
281
|
+
options: endpoint.options
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/api/router.ts
|
|
286
|
+
function createS3Router(endpoints, env) {
|
|
287
|
+
const { resolved } = env;
|
|
288
|
+
const router = createRouter(endpoints, {
|
|
289
|
+
basePath: resolved.basePath,
|
|
290
|
+
routerContext: createRouterContext(env.config, resolved),
|
|
291
|
+
openapi: { disabled: true },
|
|
292
|
+
allowedMediaTypes: BODY_MEDIA_TYPES,
|
|
293
|
+
onError: (error, request) => {
|
|
294
|
+
throw toAPIError(error, resolved, request);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
return {
|
|
298
|
+
handler: async (request) => {
|
|
299
|
+
try {
|
|
300
|
+
const capped = await capRequestBody(
|
|
301
|
+
request,
|
|
302
|
+
resolved.maxRequestBodySize
|
|
303
|
+
);
|
|
304
|
+
const response = await router.handler(capped);
|
|
305
|
+
return isUnmatched(response) ? toResponse(s3Error("NOT_FOUND")) : response;
|
|
306
|
+
} catch (error) {
|
|
307
|
+
return toResponse(toAPIError(error, resolved, request));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async function capRequestBody(request, maxSize) {
|
|
313
|
+
if (request.body === null) return request;
|
|
314
|
+
const declared = Number(request.headers.get("content-length") ?? 0);
|
|
315
|
+
if (declared > maxSize) throw s3Error("REQUEST_TOO_LARGE", { maxSize });
|
|
316
|
+
const reader = request.body.getReader();
|
|
317
|
+
const chunks = [];
|
|
318
|
+
let size = 0;
|
|
319
|
+
for (; ; ) {
|
|
320
|
+
const { done, value } = await reader.read();
|
|
321
|
+
if (done) break;
|
|
322
|
+
size += value.byteLength;
|
|
323
|
+
if (size > maxSize) {
|
|
324
|
+
reader.cancel().catch(() => void 0);
|
|
325
|
+
throw s3Error("REQUEST_TOO_LARGE", { maxSize });
|
|
29
326
|
}
|
|
30
|
-
|
|
327
|
+
chunks.push(value);
|
|
31
328
|
}
|
|
329
|
+
return new Request(request, { body: new Blob(chunks) });
|
|
330
|
+
}
|
|
331
|
+
function isUnmatched(response) {
|
|
332
|
+
return response.status === 404 && response.body === null;
|
|
333
|
+
}
|
|
334
|
+
function fields(error) {
|
|
335
|
+
return typeof error === "object" && error !== null ? error : {};
|
|
336
|
+
}
|
|
337
|
+
function named(error, names) {
|
|
338
|
+
const { name, Code } = fields(error);
|
|
339
|
+
return typeof name === "string" && names.includes(name) || typeof Code === "string" && names.includes(Code);
|
|
340
|
+
}
|
|
341
|
+
function matches(error, names, status) {
|
|
342
|
+
return named(error, names) || awsStatus(error) === status;
|
|
343
|
+
}
|
|
344
|
+
function awsStatus(error) {
|
|
345
|
+
const status = fields(error).$metadata?.httpStatusCode;
|
|
346
|
+
return typeof status === "number" ? status : void 0;
|
|
347
|
+
}
|
|
348
|
+
function isAwsNotFound(error) {
|
|
349
|
+
if (named(error, ["NoSuchBucket"])) return false;
|
|
350
|
+
return matches(error, ["NoSuchKey", "NotFound", "NoSuchUpload"], 404);
|
|
32
351
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
352
|
+
function isAwsPreconditionFailed(error) {
|
|
353
|
+
return matches(error, ["PreconditionFailed"], 412);
|
|
354
|
+
}
|
|
355
|
+
function isAwsNotImplemented(error) {
|
|
356
|
+
return matches(error, ["NotImplemented"], 501);
|
|
357
|
+
}
|
|
358
|
+
function isAwsRangeNotSatisfiable(error) {
|
|
359
|
+
return matches(error, ["InvalidRange"], 416);
|
|
360
|
+
}
|
|
361
|
+
async function sendOrObjectNotFound(send) {
|
|
362
|
+
try {
|
|
363
|
+
return await send();
|
|
364
|
+
} catch (error) {
|
|
365
|
+
if (isAwsNotFound(error)) {
|
|
366
|
+
throw s3Error("OBJECT_NOT_FOUND", void 0, { cause: error });
|
|
367
|
+
}
|
|
368
|
+
throw error;
|
|
36
369
|
}
|
|
37
|
-
headers.delete("content-length");
|
|
38
|
-
headers.delete("transfer-encoding");
|
|
39
|
-
headers.set("content-type", "application/json");
|
|
40
|
-
return JSON.stringify(body);
|
|
41
370
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
371
|
+
|
|
372
|
+
// src/helpers/best-effort.ts
|
|
373
|
+
async function deleteObjectIfMatch(client, input) {
|
|
374
|
+
const target = { Bucket: input.bucket, Key: input.key };
|
|
375
|
+
if (input.eTag === void 0) {
|
|
376
|
+
await client.send(new DeleteObjectCommand(target));
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
await client.send(
|
|
381
|
+
new DeleteObjectCommand({ ...target, IfMatch: input.eTag })
|
|
382
|
+
);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (isAwsPreconditionFailed(error)) return false;
|
|
385
|
+
if (!isAwsNotImplemented(error)) throw error;
|
|
386
|
+
await client.send(new DeleteObjectCommand(target));
|
|
387
|
+
}
|
|
388
|
+
return true;
|
|
46
389
|
}
|
|
47
|
-
async function deleteObjectBestEffort(client, bucket, key) {
|
|
390
|
+
async function deleteObjectBestEffort(client, bucket, key, eTag) {
|
|
48
391
|
try {
|
|
49
|
-
await client
|
|
392
|
+
await deleteObjectIfMatch(client, { bucket, key, eTag });
|
|
50
393
|
} catch {
|
|
51
394
|
}
|
|
52
395
|
}
|
|
@@ -62,188 +405,194 @@ async function abortMultipartBestEffort(client, bucket, key, uploadId) {
|
|
|
62
405
|
} catch {
|
|
63
406
|
}
|
|
64
407
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
forbidden: () => APIError.from("FORBIDDEN", S3_ERROR_CODES.FORBIDDEN),
|
|
69
|
-
conflict: () => APIError.from("CONFLICT", S3_ERROR_CODES.CONFLICT),
|
|
70
|
-
internalError: () => APIError.from("INTERNAL_SERVER_ERROR", S3_ERROR_CODES.INTERNAL_ERROR),
|
|
71
|
-
objectNotFound: () => APIError.from("NOT_FOUND", S3_ERROR_CODES.OBJECT_NOT_FOUND),
|
|
72
|
-
featureDisabled: (feature) => APIError.from("NOT_FOUND", {
|
|
73
|
-
code: S3_ERROR_CODES.FEATURE_DISABLED.code,
|
|
74
|
-
message: S3_ERROR_CODES.FEATURE_DISABLED.message.replaceAll(
|
|
75
|
-
"{feature}",
|
|
76
|
-
feature
|
|
77
|
-
),
|
|
78
|
-
params: { feature }
|
|
79
|
-
}),
|
|
80
|
-
invalidKey: () => APIError.from("BAD_REQUEST", S3_ERROR_CODES.INVALID_KEY),
|
|
81
|
-
unknownRoute: (route2) => APIError.from("NOT_FOUND", {
|
|
82
|
-
code: S3_ERROR_CODES.UNKNOWN_ROUTE.code,
|
|
83
|
-
message: S3_ERROR_CODES.UNKNOWN_ROUTE.message,
|
|
84
|
-
params: { route: route2 }
|
|
85
|
-
}),
|
|
86
|
-
fileTypeNotAllowed: (fileName, contentType) => APIError.from("BAD_REQUEST", {
|
|
87
|
-
code: S3_ERROR_CODES.FILE_TYPE_NOT_ALLOWED.code,
|
|
88
|
-
message: S3_ERROR_CODES.FILE_TYPE_NOT_ALLOWED.message,
|
|
89
|
-
params: {
|
|
90
|
-
fileName,
|
|
91
|
-
...contentType ? { contentType } : {}
|
|
92
|
-
}
|
|
93
|
-
}),
|
|
94
|
-
s3NetworkError: (code) => APIError.from("BAD_GATEWAY", {
|
|
95
|
-
code: S3_ERROR_CODES.S3_NETWORK_ERROR.code,
|
|
96
|
-
message: S3_ERROR_CODES.S3_NETWORK_ERROR.message.replaceAll(
|
|
97
|
-
"{code}",
|
|
98
|
-
code
|
|
99
|
-
),
|
|
100
|
-
params: { code }
|
|
101
|
-
}),
|
|
102
|
-
multipartPartMissing: (partNumber) => APIError.from("BAD_REQUEST", {
|
|
103
|
-
code: S3_ERROR_CODES.MULTIPART_PART_MISSING.code,
|
|
104
|
-
message: S3_ERROR_CODES.MULTIPART_PART_MISSING.message.replaceAll(
|
|
105
|
-
"{partNumber}",
|
|
106
|
-
String(partNumber)
|
|
107
|
-
),
|
|
108
|
-
params: { partNumber }
|
|
109
|
-
}),
|
|
110
|
-
payloadTooLarge: (message = S3_ERROR_CODES.PAYLOAD_TOO_LARGE.message) => APIError.from("PAYLOAD_TOO_LARGE", {
|
|
111
|
-
...S3_ERROR_CODES.PAYLOAD_TOO_LARGE,
|
|
112
|
-
message
|
|
113
|
-
}),
|
|
114
|
-
quotaExceeded: (message = S3_ERROR_CODES.QUOTA_EXCEEDED.message) => APIError.from("PAYLOAD_TOO_LARGE", {
|
|
115
|
-
...S3_ERROR_CODES.QUOTA_EXCEEDED,
|
|
116
|
-
message
|
|
117
|
-
}),
|
|
118
|
-
validationError: (message) => APIError.from("BAD_REQUEST", {
|
|
119
|
-
code: S3_ERROR_CODES.VALIDATION_ERROR.code,
|
|
120
|
-
message
|
|
121
|
-
})
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// src/helpers/is-aws-not-found.ts
|
|
125
|
-
function isAwsNotFound(err) {
|
|
126
|
-
const e = err;
|
|
127
|
-
return e?.name === "NoSuchKey" || e?.name === "NotFound" || e?.name === "NoSuchUpload" || e?.Code === "NoSuchKey" || e?.Code === "NotFound" || e?.Code === "NoSuchUpload" || e?.$metadata?.httpStatusCode === 404;
|
|
128
|
-
}
|
|
129
|
-
async function sendOrObjectNotFound(send) {
|
|
130
|
-
try {
|
|
131
|
-
return await send();
|
|
132
|
-
} catch (err) {
|
|
133
|
-
if (isAwsNotFound(err)) throw errors.objectNotFound();
|
|
134
|
-
throw err;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function normalizeExpiresIn(value, maxExpiresIn = S3_MAX_EXPIRES_IN) {
|
|
138
|
-
const n = Number(value);
|
|
139
|
-
const expires = Number.isFinite(n) && n > 0 ? Math.floor(n) : S3_DEFAULT_EXPIRES_IN;
|
|
140
|
-
const max = Number.isFinite(maxExpiresIn) && maxExpiresIn > 0 ? Math.floor(maxExpiresIn) : S3_MAX_EXPIRES_IN;
|
|
141
|
-
return Math.min(expires, max);
|
|
142
|
-
}
|
|
143
|
-
function clampDownloadExpiresIn(options) {
|
|
144
|
-
const max = options.maxExpiresIn;
|
|
145
|
-
if (options.resolvedExpiresIn != null) {
|
|
146
|
-
return normalizeExpiresIn(options.resolvedExpiresIn, max);
|
|
408
|
+
function assertWithinMaxFileSize(maxFileSize, size) {
|
|
409
|
+
if (maxFileSize !== void 0 && size > maxFileSize) {
|
|
410
|
+
throw s3Error("FILE_TOO_LARGE", { size, maxFileSize });
|
|
147
411
|
}
|
|
148
|
-
const cap = normalizeExpiresIn(options.routeExpiresIn, max);
|
|
149
|
-
if (options.requested == null) return cap;
|
|
150
|
-
return Math.min(normalizeExpiresIn(options.requested, max), cap);
|
|
151
412
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
413
|
+
function assertFileConstraints(constraints, file) {
|
|
414
|
+
const { fileTypes, maxFileSize } = constraints;
|
|
415
|
+
const name = file.name ?? "";
|
|
416
|
+
if (fileTypes?.length && !matchesFileTypes(name, file.type, fileTypes)) {
|
|
417
|
+
throw s3Error("FILE_TYPE_NOT_ALLOWED", {
|
|
418
|
+
type: describeFileType(name, file.type),
|
|
419
|
+
allowed: fileTypes.join(", ")
|
|
420
|
+
});
|
|
160
421
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (logger === false) return {};
|
|
164
|
-
if (!logger) return defaultLogger;
|
|
165
|
-
return logger;
|
|
422
|
+
if (file.size === 0) throw s3Error("FILE_EMPTY");
|
|
423
|
+
assertWithinMaxFileSize(maxFileSize, file.size);
|
|
166
424
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
throw new APIError("FORBIDDEN", {
|
|
173
|
-
message: err.message,
|
|
174
|
-
cause: err
|
|
425
|
+
function assertSingleUploadSize(size) {
|
|
426
|
+
if (size > S3_MAX_POST_OBJECT_BYTES) {
|
|
427
|
+
throw s3Error("FILE_TOO_LARGE", {
|
|
428
|
+
size,
|
|
429
|
+
maxFileSize: S3_MAX_POST_OBJECT_BYTES
|
|
175
430
|
});
|
|
176
431
|
}
|
|
177
|
-
throw errors.forbidden();
|
|
178
432
|
}
|
|
179
|
-
|
|
180
|
-
|
|
433
|
+
var PLAIN_VALUE = /^(?:[\x21-\x7e](?:[\x20-\x7e]*[\x21-\x7e])?)?$/;
|
|
434
|
+
var ENCODED_WORD_LIKE = /^=\?.*\?=$/s;
|
|
435
|
+
var ENCODED_WORD = /^=\?([^?*]+)(?:\*[^?]*)?\?([bq])\?([^?]*)\?=$/i;
|
|
436
|
+
var UTF8_CHARSETS = /* @__PURE__ */ new Set(["utf-8", "utf8", "us-ascii"]);
|
|
437
|
+
function encodeMetadataValue(value) {
|
|
438
|
+
if (PLAIN_VALUE.test(value) && !ENCODED_WORD_LIKE.test(value)) return value;
|
|
439
|
+
return `=?UTF-8?B?${toBase64(new TextEncoder().encode(value))}?=`;
|
|
440
|
+
}
|
|
441
|
+
function decodeMetadataValue(value) {
|
|
442
|
+
const words = value.trim().split(/\s+/);
|
|
443
|
+
const chunks = [];
|
|
444
|
+
for (const word of words) {
|
|
445
|
+
const match = ENCODED_WORD.exec(word);
|
|
446
|
+
if (!match || !UTF8_CHARSETS.has(match[1].toLowerCase())) return value;
|
|
447
|
+
const bytes = match[2].toLowerCase() === "b" ? fromBase64(match[3]) : fromQuotedPrintable(match[3]);
|
|
448
|
+
if (!bytes) return value;
|
|
449
|
+
chunks.push(bytes);
|
|
450
|
+
}
|
|
181
451
|
try {
|
|
182
|
-
|
|
183
|
-
} catch
|
|
184
|
-
|
|
452
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(concat(chunks));
|
|
453
|
+
} catch {
|
|
454
|
+
return value;
|
|
185
455
|
}
|
|
186
456
|
}
|
|
187
|
-
|
|
188
|
-
|
|
457
|
+
function toBase64(bytes) {
|
|
458
|
+
const url = base64UrlEncode(bytes).replaceAll("-", "+").replaceAll("_", "/");
|
|
459
|
+
return url.padEnd(Math.ceil(url.length / 4) * 4, "=");
|
|
460
|
+
}
|
|
461
|
+
function fromBase64(text) {
|
|
462
|
+
if (!/^[a-z0-9+/]*={0,2}$/i.test(text) || text.length % 4 !== 0) {
|
|
463
|
+
return void 0;
|
|
464
|
+
}
|
|
189
465
|
try {
|
|
190
|
-
return
|
|
191
|
-
|
|
192
|
-
|
|
466
|
+
return base64UrlDecode(
|
|
467
|
+
text.replace(/=+$/, "").replaceAll("+", "-").replaceAll("/", "_")
|
|
468
|
+
);
|
|
469
|
+
} catch {
|
|
470
|
+
return void 0;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function fromQuotedPrintable(text) {
|
|
474
|
+
const bytes = [];
|
|
475
|
+
for (let i = 0; i < text.length; i++) {
|
|
476
|
+
const char = text[i];
|
|
477
|
+
if (char === "_") {
|
|
478
|
+
bytes.push(32);
|
|
479
|
+
} else if (char === "=") {
|
|
480
|
+
const hex = text.slice(i + 1, i + 3);
|
|
481
|
+
if (!/^[0-9a-f]{2}$/i.test(hex)) return void 0;
|
|
482
|
+
bytes.push(Number.parseInt(hex, 16));
|
|
483
|
+
i += 2;
|
|
484
|
+
} else {
|
|
485
|
+
const code = char.charCodeAt(0);
|
|
486
|
+
if (code < 33 || code > 126) return void 0;
|
|
487
|
+
bytes.push(code);
|
|
488
|
+
}
|
|
193
489
|
}
|
|
490
|
+
return Uint8Array.from(bytes);
|
|
194
491
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
resolveLogger(config?.logger).error?.("lifecycle hook failed", err);
|
|
202
|
-
config?.onError?.(err, { request: context.request });
|
|
203
|
-
throw errors.internalError();
|
|
492
|
+
function concat(chunks) {
|
|
493
|
+
const out = new Uint8Array(chunks.reduce((n, chunk) => n + chunk.length, 0));
|
|
494
|
+
let offset = 0;
|
|
495
|
+
for (const chunk of chunks) {
|
|
496
|
+
out.set(chunk, offset);
|
|
497
|
+
offset += chunk.length;
|
|
204
498
|
}
|
|
499
|
+
return out;
|
|
205
500
|
}
|
|
206
501
|
|
|
207
|
-
// src/helpers/
|
|
208
|
-
var
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
502
|
+
// src/helpers/object-meta.ts
|
|
503
|
+
var UPLOAD_MARKER_META = "dimah-upload";
|
|
504
|
+
var PREVIOUS_KEY_META = "dimah-previous-key";
|
|
505
|
+
var MAX_METADATA_BYTES = 2048;
|
|
506
|
+
var INTERNAL_KEYS = /* @__PURE__ */ new Set([
|
|
507
|
+
UPLOAD_MARKER_META,
|
|
508
|
+
PREVIOUS_KEY_META
|
|
509
|
+
]);
|
|
510
|
+
var METADATA_KEY = /^[!#$%&'*+.^`|~\w-]+$/;
|
|
511
|
+
function buildObjectMetadata(input) {
|
|
512
|
+
const result = {};
|
|
513
|
+
for (const [name, value] of Object.entries(input.metadata ?? {})) {
|
|
514
|
+
const key = name.toLowerCase();
|
|
515
|
+
if (!METADATA_KEY.test(key)) {
|
|
516
|
+
throw new ConfigError(`S3 metadata key "${name}" is not a valid name.`);
|
|
517
|
+
}
|
|
518
|
+
if (INTERNAL_KEYS.has(key)) {
|
|
519
|
+
throw new ConfigError(`S3 metadata key "${name}" is reserved.`);
|
|
520
|
+
}
|
|
521
|
+
if (Object.hasOwn(result, key)) {
|
|
522
|
+
throw new ConfigError(`S3 metadata key "${name}" is set twice.`);
|
|
523
|
+
}
|
|
524
|
+
result[key] = encodeMetadataValue(value);
|
|
525
|
+
}
|
|
526
|
+
const userBytes = metadataByteSize(result);
|
|
527
|
+
result[UPLOAD_MARKER_META] = input.uploadToken;
|
|
528
|
+
if (input.previousKey !== void 0) {
|
|
529
|
+
result[PREVIOUS_KEY_META] = encodeMetadataValue(input.previousKey);
|
|
530
|
+
}
|
|
531
|
+
const totalBytes = metadataByteSize(result);
|
|
532
|
+
if (totalBytes > MAX_METADATA_BYTES) {
|
|
533
|
+
throw new ConfigError(
|
|
534
|
+
`S3 metadata is ${totalBytes} bytes (${userBytes} from upload.object() and ${totalBytes - userBytes} added by dimah-s3); S3 allows ${MAX_METADATA_BYTES}. Keys and values count once encoded.`
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
return result;
|
|
538
|
+
}
|
|
539
|
+
function readObjectMetadata(raw) {
|
|
540
|
+
const metadata = {};
|
|
541
|
+
let uploadToken;
|
|
542
|
+
let previousKey;
|
|
543
|
+
for (const [name, value] of Object.entries(raw ?? {})) {
|
|
544
|
+
const key = name.toLowerCase();
|
|
545
|
+
const decoded = decodeMetadataValue(value);
|
|
546
|
+
if (key === UPLOAD_MARKER_META) uploadToken = decoded;
|
|
547
|
+
else if (key === PREVIOUS_KEY_META) previousKey = decoded || void 0;
|
|
548
|
+
else metadata[key] = decoded;
|
|
549
|
+
}
|
|
550
|
+
return { metadata, uploadToken, previousKey };
|
|
551
|
+
}
|
|
552
|
+
function readUploadMarker(metadata) {
|
|
553
|
+
return readObjectMetadata(metadata).uploadToken;
|
|
213
554
|
}
|
|
214
|
-
function
|
|
215
|
-
const
|
|
216
|
-
|
|
555
|
+
function metadataByteSize(metadata) {
|
|
556
|
+
const encoder2 = new TextEncoder();
|
|
557
|
+
let bytes = 0;
|
|
558
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
559
|
+
bytes += encoder2.encode(key).length + encoder2.encode(value).length;
|
|
560
|
+
}
|
|
561
|
+
return bytes;
|
|
217
562
|
}
|
|
218
563
|
|
|
219
564
|
// src/helpers/object-s3.ts
|
|
220
565
|
var MAX_S3_TAGS = 10;
|
|
221
566
|
var MAX_TAG_KEY_CHARS = 128;
|
|
222
567
|
var MAX_TAG_VALUE_CHARS = 256;
|
|
568
|
+
var PRINTABLE_ASCII = /^[\x20-\x7e]*$/;
|
|
223
569
|
function present(value) {
|
|
224
570
|
const trimmed = value?.trim();
|
|
225
571
|
return trimmed ? trimmed : void 0;
|
|
226
572
|
}
|
|
227
573
|
function assertTagEntry(key, value) {
|
|
228
574
|
if (key.length === 0) {
|
|
229
|
-
throw
|
|
575
|
+
throw new ConfigError("S3 object tag keys cannot be empty.");
|
|
230
576
|
}
|
|
231
577
|
if (key.length > MAX_TAG_KEY_CHARS) {
|
|
232
|
-
throw
|
|
233
|
-
`S3 object tag keys cannot exceed ${MAX_TAG_KEY_CHARS} characters
|
|
578
|
+
throw new ConfigError(
|
|
579
|
+
`S3 object tag keys cannot exceed ${MAX_TAG_KEY_CHARS} characters.`
|
|
234
580
|
);
|
|
235
581
|
}
|
|
236
582
|
if (value.length > MAX_TAG_VALUE_CHARS) {
|
|
237
|
-
throw
|
|
238
|
-
`S3 object tag values cannot exceed ${MAX_TAG_VALUE_CHARS} characters
|
|
583
|
+
throw new ConfigError(
|
|
584
|
+
`S3 object tag values cannot exceed ${MAX_TAG_VALUE_CHARS} characters.`
|
|
239
585
|
);
|
|
240
586
|
}
|
|
587
|
+
if (!PRINTABLE_ASCII.test(key) || !PRINTABLE_ASCII.test(value)) {
|
|
588
|
+
throw new ConfigError(`S3 object tag "${key}" must be printable ASCII.`);
|
|
589
|
+
}
|
|
241
590
|
}
|
|
242
591
|
function encodeObjectTagging(tagging) {
|
|
243
592
|
const entries = Object.entries(tagging);
|
|
244
593
|
if (entries.length > MAX_S3_TAGS) {
|
|
245
|
-
throw
|
|
246
|
-
`S3 object tagging allows at most ${MAX_S3_TAGS} tags
|
|
594
|
+
throw new ConfigError(
|
|
595
|
+
`S3 object tagging allows at most ${MAX_S3_TAGS} tags.`
|
|
247
596
|
);
|
|
248
597
|
}
|
|
249
598
|
return entries.map(([key, value]) => {
|
|
@@ -286,15 +635,15 @@ function objectPutHeaders(opts) {
|
|
|
286
635
|
// src/helpers/resolve-target.ts
|
|
287
636
|
function assertSafeObjectKey(key) {
|
|
288
637
|
const normalized = normalizeObjectKey(key);
|
|
289
|
-
if (!normalized) throw
|
|
638
|
+
if (!normalized) throw s3Error("KEY_INVALID");
|
|
290
639
|
return normalized;
|
|
291
640
|
}
|
|
292
641
|
function applyPrefix(prefix, key) {
|
|
293
642
|
const p = assertSafeObjectKey(prefix);
|
|
294
643
|
const k = assertSafeObjectKey(key);
|
|
295
644
|
if (k.startsWith(`${p}/`)) return k;
|
|
296
|
-
if (k === p) throw
|
|
297
|
-
return `${p}/${k}
|
|
645
|
+
if (k === p) throw s3Error("KEY_INVALID");
|
|
646
|
+
return assertSafeObjectKey(`${p}/${k}`);
|
|
298
647
|
}
|
|
299
648
|
function nestKeyUnderPrefix(keyPrefix, key) {
|
|
300
649
|
const safe = assertSafeObjectKey(key);
|
|
@@ -306,10 +655,11 @@ function assertStoredKey(key, keyPrefix) {
|
|
|
306
655
|
if (keyPrefix === false) return safe;
|
|
307
656
|
const root = assertSafeObjectKey(keyPrefix);
|
|
308
657
|
if (safe.startsWith(`${root}/`)) return safe;
|
|
309
|
-
throw
|
|
658
|
+
throw s3Error("KEY_INVALID");
|
|
310
659
|
}
|
|
660
|
+
var KEY_UNSAFE = /[\\\p{Cc}]/gu;
|
|
311
661
|
function objectKeyFileName(fileName) {
|
|
312
|
-
const sanitized =
|
|
662
|
+
const sanitized = fileName.replaceAll(KEY_UNSAFE, "_").replaceAll("${", "_{");
|
|
313
663
|
const leaf = sanitized.split("/").findLast((part) => part.length > 0);
|
|
314
664
|
if (!leaf || leaf === "." || leaf === "..") return "file";
|
|
315
665
|
return leaf;
|
|
@@ -319,288 +669,1020 @@ function generateObjectKey(folder, fileName) {
|
|
|
319
669
|
if (folder === false) return assertSafeObjectKey(leaf);
|
|
320
670
|
return applyPrefix(folder, leaf);
|
|
321
671
|
}
|
|
322
|
-
function
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
};
|
|
332
|
-
const info = await runObjectHook(route2.upload.object, objectContext) ?? void 0;
|
|
333
|
-
const key = info?.key ? nestKeyUnderPrefix(route2.keyPrefix, info.key) : generateObjectKey(
|
|
334
|
-
info?.folder != null && info.folder !== "" ? nestKeyUnderPrefix(route2.keyPrefix, info.folder) : route2.keyPrefix === false ? false : route2.keyPrefix,
|
|
335
|
-
context.file.name
|
|
336
|
-
);
|
|
337
|
-
const metadata = { ...info?.metadata };
|
|
338
|
-
if (info?.previousKey) {
|
|
339
|
-
metadata[DIMAH_PREVIOUS_KEY_META] = nestKeyUnderPrefix(
|
|
340
|
-
route2.keyPrefix,
|
|
341
|
-
info.previousKey
|
|
342
|
-
);
|
|
672
|
+
async function resolveUploadObject(route2, context) {
|
|
673
|
+
const info = await route2.upload.object?.(context) ?? void 0;
|
|
674
|
+
const { keyPrefix } = route2;
|
|
675
|
+
let key;
|
|
676
|
+
if (info?.key) {
|
|
677
|
+
key = nestKeyUnderPrefix(keyPrefix, info.key);
|
|
678
|
+
} else {
|
|
679
|
+
const folder = info?.keyFolder ? nestKeyUnderPrefix(keyPrefix, info.keyFolder) : keyPrefix;
|
|
680
|
+
key = generateObjectKey(folder, context.file.name);
|
|
343
681
|
}
|
|
344
682
|
return {
|
|
345
683
|
key,
|
|
346
|
-
bucket,
|
|
347
|
-
|
|
348
|
-
|
|
684
|
+
bucket: context.bucket,
|
|
685
|
+
acl: info?.acl ?? route2.upload.acl,
|
|
686
|
+
...info?.metadata ? { metadata: info.metadata } : {},
|
|
687
|
+
...info?.previousKey ? { previousKey: nestKeyUnderPrefix(keyPrefix, info.previousKey) } : {},
|
|
349
688
|
...normalizeObjectS3(info)
|
|
350
689
|
};
|
|
351
690
|
}
|
|
352
|
-
function
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
691
|
+
async function assertObjectContent(client, input) {
|
|
692
|
+
const type = effectiveContentType(input.type, input.name);
|
|
693
|
+
if (type === void 0 || !isSniffableType(type)) return;
|
|
694
|
+
const bytes = await readObjectStart(client, input);
|
|
695
|
+
if (matchesMagicBytes(bytes, type)) return;
|
|
696
|
+
throw s3Error("FILE_CONTENT_MISMATCH", {
|
|
697
|
+
type,
|
|
698
|
+
detected: sniffContentType(bytes) ?? "unknown"
|
|
699
|
+
});
|
|
360
700
|
}
|
|
361
|
-
function
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
701
|
+
async function readObjectStart(client, input) {
|
|
702
|
+
let response;
|
|
703
|
+
try {
|
|
704
|
+
response = await sendOrObjectNotFound(
|
|
705
|
+
() => client.send(
|
|
706
|
+
new GetObjectCommand({
|
|
707
|
+
Bucket: input.bucket,
|
|
708
|
+
Key: input.key,
|
|
709
|
+
Range: `bytes=0-${SNIFF_BYTE_LENGTH - 1}`
|
|
710
|
+
})
|
|
711
|
+
)
|
|
365
712
|
);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
if (isAwsRangeNotSatisfiable(error)) return new Uint8Array();
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
if (!response.Body) return new Uint8Array();
|
|
718
|
+
const reader = response.Body.transformToWebStream().getReader();
|
|
719
|
+
const bytes = new Uint8Array(SNIFF_BYTE_LENGTH);
|
|
720
|
+
let length = 0;
|
|
721
|
+
try {
|
|
722
|
+
while (length < SNIFF_BYTE_LENGTH) {
|
|
723
|
+
const { done, value } = await reader.read();
|
|
724
|
+
if (done) break;
|
|
725
|
+
const chunk = value.subarray(0, SNIFF_BYTE_LENGTH - length);
|
|
726
|
+
bytes.set(chunk, length);
|
|
727
|
+
length += chunk.length;
|
|
728
|
+
}
|
|
729
|
+
} finally {
|
|
730
|
+
await reader.cancel().catch(() => {
|
|
731
|
+
});
|
|
366
732
|
}
|
|
733
|
+
return bytes.subarray(0, length);
|
|
367
734
|
}
|
|
368
735
|
|
|
369
|
-
// src/helpers/
|
|
370
|
-
function
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
736
|
+
// src/helpers/finalize-confirmed.ts
|
|
737
|
+
async function finalizeConfirmed(input) {
|
|
738
|
+
const { route: route2, context, head } = input;
|
|
739
|
+
const { bucket, key } = context;
|
|
740
|
+
const stored = readObjectMetadata(head.Metadata);
|
|
741
|
+
if (stored.uploadToken !== input.uploadToken) {
|
|
742
|
+
throw s3Error("OBJECT_NOT_FOUND");
|
|
743
|
+
}
|
|
744
|
+
const deleteObject = () => deleteObjectBestEffort(route2.client, bucket, key, head.ETag);
|
|
745
|
+
let object;
|
|
746
|
+
try {
|
|
747
|
+
object = verifiedObject(head, key, stored.metadata);
|
|
748
|
+
await verifyUpload(input, object);
|
|
749
|
+
} catch (error) {
|
|
750
|
+
if (isRejection(error)) await deleteObject();
|
|
751
|
+
throw error;
|
|
752
|
+
}
|
|
753
|
+
const previousKey = previousKeyToDelete(
|
|
754
|
+
stored.previousKey,
|
|
755
|
+
key,
|
|
756
|
+
route2.keyPrefix
|
|
757
|
+
);
|
|
758
|
+
const confirmed = {
|
|
759
|
+
...context,
|
|
760
|
+
kind: input.kind,
|
|
761
|
+
uploadToken: input.uploadToken,
|
|
762
|
+
uploadId: input.uploadId,
|
|
763
|
+
file: input.file,
|
|
764
|
+
acl: input.acl,
|
|
765
|
+
object,
|
|
766
|
+
...previousKey !== void 0 ? { previousKey } : {}
|
|
767
|
+
};
|
|
768
|
+
const hooks = route2.upload.onConfirmed;
|
|
769
|
+
await runLifecycle(
|
|
770
|
+
hooks.filter((entry) => entry.plugin !== void 0),
|
|
771
|
+
confirmed,
|
|
772
|
+
{
|
|
773
|
+
// A transient plugin failure keeps the object for the retry.
|
|
774
|
+
onPluginError: async (error) => {
|
|
775
|
+
if (isRejection(error)) await deleteObject();
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
);
|
|
779
|
+
try {
|
|
780
|
+
await runLifecycle(
|
|
781
|
+
hooks.filter((entry) => entry.plugin === void 0),
|
|
782
|
+
confirmed
|
|
379
783
|
);
|
|
784
|
+
} catch (error) {
|
|
785
|
+
throw userHookFailure(error);
|
|
380
786
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
function isFeatureOn(value, defaultOn = false) {
|
|
384
|
-
if (value === false) return false;
|
|
385
|
-
if (value === void 0) return defaultOn;
|
|
386
|
-
if (value === true) return true;
|
|
387
|
-
if ("enabled" in value && typeof value.enabled === "boolean") {
|
|
388
|
-
return value.enabled;
|
|
787
|
+
if (previousKey !== void 0) {
|
|
788
|
+
await deleteObjectBestEffort(route2.client, bucket, previousKey);
|
|
389
789
|
}
|
|
390
|
-
return
|
|
790
|
+
return { key, bucket, ...object, acl: input.acl };
|
|
391
791
|
}
|
|
392
|
-
function
|
|
393
|
-
if (
|
|
394
|
-
|
|
395
|
-
if (!defaultOn) return { enabled: false };
|
|
396
|
-
return { enabled: true };
|
|
792
|
+
function verifiedObject(head, key, metadata) {
|
|
793
|
+
if (typeof head.ContentLength !== "number") {
|
|
794
|
+
throw new TypeError(`HeadObject returned no ContentLength for "${key}".`);
|
|
397
795
|
}
|
|
398
|
-
|
|
399
|
-
|
|
796
|
+
return {
|
|
797
|
+
name: resolveStoredFileName(head.ContentDisposition, key),
|
|
798
|
+
size: head.ContentLength,
|
|
799
|
+
type: head.ContentType,
|
|
800
|
+
eTag: head.ETag?.replaceAll('"', ""),
|
|
801
|
+
versionId: head.VersionId,
|
|
802
|
+
lastModified: head.LastModified?.toISOString(),
|
|
803
|
+
metadata
|
|
804
|
+
};
|
|
400
805
|
}
|
|
401
|
-
function
|
|
402
|
-
const
|
|
403
|
-
if (
|
|
404
|
-
|
|
405
|
-
|
|
806
|
+
async function verifyUpload(input, object) {
|
|
807
|
+
const { route: route2, context, kind, file, acl } = input;
|
|
808
|
+
if (object.size !== file.size) {
|
|
809
|
+
throw s3Error("UPLOAD_SIZE_MISMATCH", {
|
|
810
|
+
expected: file.size,
|
|
811
|
+
actual: object.size
|
|
812
|
+
});
|
|
406
813
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
814
|
+
assertFileConstraints(route2.upload, object);
|
|
815
|
+
if (route2.upload.verifyContent) {
|
|
816
|
+
await assertObjectContent(route2.client, {
|
|
817
|
+
bucket: context.bucket,
|
|
818
|
+
key: context.key,
|
|
819
|
+
type: object.type,
|
|
820
|
+
name: object.name
|
|
821
|
+
});
|
|
412
822
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
);
|
|
823
|
+
if (kind !== "server") {
|
|
824
|
+
await runGuards(route2.upload.confirmGuard, {
|
|
825
|
+
...context,
|
|
826
|
+
kind,
|
|
827
|
+
uploadToken: input.uploadToken,
|
|
828
|
+
uploadId: input.uploadId,
|
|
829
|
+
file,
|
|
830
|
+
acl,
|
|
831
|
+
object
|
|
832
|
+
});
|
|
424
833
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
834
|
+
}
|
|
835
|
+
function isRejection(error) {
|
|
836
|
+
return isAPIError(error) && error.statusCode < 500;
|
|
837
|
+
}
|
|
838
|
+
function userHookFailure(error) {
|
|
839
|
+
if (!isAPIError(error)) return error;
|
|
840
|
+
return new Error(`upload.onConfirmed failed: ${error.message}`, {
|
|
841
|
+
cause: error
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
function previousKeyToDelete(previousKey, key, keyPrefix) {
|
|
845
|
+
if (previousKey === void 0) return void 0;
|
|
846
|
+
try {
|
|
847
|
+
const safe = assertStoredKey(previousKey, keyPrefix);
|
|
848
|
+
return safe === key ? void 0 : safe;
|
|
849
|
+
} catch {
|
|
850
|
+
return void 0;
|
|
429
851
|
}
|
|
430
|
-
|
|
431
|
-
|
|
852
|
+
}
|
|
853
|
+
function headObjectOrNotFound(client, bucket, key, options = {}) {
|
|
854
|
+
return sendOrObjectNotFound(
|
|
855
|
+
() => client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }), options)
|
|
432
856
|
);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
857
|
+
}
|
|
858
|
+
var VERSION = "v1";
|
|
859
|
+
var HKDF_INFO = "dimah-s3/upload/v1";
|
|
860
|
+
var SECRET_ENV = "DIMAH_S3_SECRET";
|
|
861
|
+
var MIN_SECRET_LENGTH = 32;
|
|
862
|
+
var MAX_TICKET_LENGTH = 8192;
|
|
863
|
+
var ABORT_GRACE_SECONDS = 7 * 24 * 60 * 60;
|
|
864
|
+
var SUBJECT_HASH_LENGTH = 22;
|
|
865
|
+
var DEV_SECRET = /* @__PURE__ */ Symbol.for("dimah-s3.dev-secret");
|
|
866
|
+
var encoder = new TextEncoder();
|
|
867
|
+
var payloadBase = {
|
|
868
|
+
id: z2.string().min(1),
|
|
869
|
+
r: z2.string().min(1),
|
|
870
|
+
k: z2.string().min(1),
|
|
871
|
+
n: z2.string().min(1),
|
|
872
|
+
s: z2.int().nonnegative(),
|
|
873
|
+
t: z2.string().optional(),
|
|
874
|
+
a: s3ObjectAclSchema,
|
|
875
|
+
c: z2.union([z2.literal(0), z2.literal(1)]),
|
|
876
|
+
sb: z2.string().length(SUBJECT_HASH_LENGTH).optional(),
|
|
877
|
+
iat: z2.int(),
|
|
878
|
+
exp: z2.int()
|
|
879
|
+
};
|
|
880
|
+
var payloadSchema = z2.discriminatedUnion("m", [
|
|
881
|
+
z2.strictObject({
|
|
882
|
+
...payloadBase,
|
|
883
|
+
m: z2.literal("single"),
|
|
884
|
+
/** Presign verb: `post` or `put`. */
|
|
885
|
+
v: z2.enum(["post", "put"])
|
|
886
|
+
}),
|
|
887
|
+
z2.strictObject({
|
|
888
|
+
...payloadBase,
|
|
889
|
+
m: z2.literal("multipart"),
|
|
890
|
+
u: z2.string().min(1),
|
|
891
|
+
ps: z2.int().positive(),
|
|
892
|
+
pc: z2.int().positive()
|
|
893
|
+
})
|
|
894
|
+
]);
|
|
895
|
+
function createUploadToken() {
|
|
896
|
+
return base64UrlEncode(crypto.getRandomValues(new Uint8Array(16)));
|
|
897
|
+
}
|
|
898
|
+
function parseSecretOption(secret) {
|
|
899
|
+
if (secret === void 0) return void 0;
|
|
900
|
+
const secrets = typeof secret === "string" ? [secret] : [...secret];
|
|
901
|
+
if (secrets.length === 0) {
|
|
902
|
+
throw new Error("dimahS3: secret must not be an empty array.");
|
|
903
|
+
}
|
|
904
|
+
if (secrets.some((value) => value.length < MIN_SECRET_LENGTH)) {
|
|
438
905
|
throw new Error(
|
|
439
|
-
`dimahS3
|
|
906
|
+
`dimahS3: every secret must have at least ${MIN_SECRET_LENGTH} characters.`
|
|
440
907
|
);
|
|
441
908
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
909
|
+
return secrets;
|
|
910
|
+
}
|
|
911
|
+
async function issueTicket(env, input, subject) {
|
|
912
|
+
const [key] = await ticketKeys(env);
|
|
913
|
+
const payload = {
|
|
914
|
+
id: input.id,
|
|
915
|
+
r: input.route,
|
|
916
|
+
k: input.key,
|
|
917
|
+
n: input.file.name,
|
|
918
|
+
s: input.file.size,
|
|
919
|
+
t: input.file.type,
|
|
920
|
+
a: input.acl,
|
|
921
|
+
c: input.checksum ? 1 : 0,
|
|
922
|
+
sb: subject === void 0 ? void 0 : await subjectHash(key, subject),
|
|
923
|
+
iat: epochSeconds(/* @__PURE__ */ new Date()),
|
|
924
|
+
exp: epochSeconds(input.expiresAt),
|
|
925
|
+
...input.mode === "multipart" ? {
|
|
926
|
+
m: "multipart",
|
|
927
|
+
u: input.uploadId,
|
|
928
|
+
ps: input.partSize,
|
|
929
|
+
pc: input.partCount
|
|
930
|
+
} : { m: "single", v: input.kind }
|
|
461
931
|
};
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
throw new
|
|
932
|
+
const body = `${VERSION}.${base64UrlEncode(JSON.stringify(payload))}`;
|
|
933
|
+
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(body));
|
|
934
|
+
const ticket = `${body}.${base64UrlEncode(new Uint8Array(signature))}`;
|
|
935
|
+
if (ticket.length > MAX_TICKET_LENGTH) {
|
|
936
|
+
throw new ConfigError(
|
|
937
|
+
`The upload ticket for key "${input.key}" is ${ticket.length} characters; the limit is ${MAX_TICKET_LENGTH}. Use a shorter object key.`
|
|
938
|
+
);
|
|
467
939
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
940
|
+
return { ticket, expiresAt: new Date(payload.exp * 1e3) };
|
|
941
|
+
}
|
|
942
|
+
async function readTicket(env, ticket, options) {
|
|
943
|
+
const keys = await ticketKeys(env);
|
|
944
|
+
const payload = await verifyPayload(keys, ticket);
|
|
945
|
+
if (payload?.m !== options.mode) throw s3Error("UPLOAD_TICKET_INVALID");
|
|
946
|
+
const grace = options.operation === "multipart.abort" ? ABORT_GRACE_SECONDS : 0;
|
|
947
|
+
if (epochSeconds(/* @__PURE__ */ new Date()) > payload.exp + grace) {
|
|
948
|
+
throw s3Error("UPLOAD_TICKET_EXPIRED");
|
|
471
949
|
}
|
|
472
|
-
|
|
473
|
-
return routes;
|
|
950
|
+
return fromPayload(payload);
|
|
474
951
|
}
|
|
475
|
-
function
|
|
476
|
-
|
|
952
|
+
async function assertTicketSubject(env, ticket, context) {
|
|
953
|
+
if (ticket.subjectHash === void 0) return;
|
|
954
|
+
const subject = await env.ticketSubject?.(context);
|
|
955
|
+
if (subject !== void 0) {
|
|
956
|
+
for (const key of await ticketKeys(env)) {
|
|
957
|
+
if (equal(await subjectHash(key, subject), ticket.subjectHash)) return;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
throw s3Error("UPLOAD_TICKET_INVALID");
|
|
477
961
|
}
|
|
478
|
-
function
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
962
|
+
async function verifyPayload(keys, ticket) {
|
|
963
|
+
const parts = ticket.split(".");
|
|
964
|
+
if (parts.length !== 3 || parts[0] !== VERSION) return void 0;
|
|
965
|
+
const [, payloadPart, signaturePart] = parts;
|
|
966
|
+
let signature;
|
|
967
|
+
try {
|
|
968
|
+
signature = base64UrlDecode(signaturePart);
|
|
969
|
+
} catch {
|
|
970
|
+
return void 0;
|
|
971
|
+
}
|
|
972
|
+
const body = encoder.encode(`${VERSION}.${payloadPart}`);
|
|
973
|
+
let verified = false;
|
|
974
|
+
for (const key of keys) {
|
|
975
|
+
if (await crypto.subtle.verify("HMAC", key, signature, body)) {
|
|
976
|
+
verified = true;
|
|
977
|
+
break;
|
|
491
978
|
}
|
|
492
979
|
}
|
|
980
|
+
if (!verified) return void 0;
|
|
981
|
+
try {
|
|
982
|
+
const json = JSON.parse(
|
|
983
|
+
new TextDecoder().decode(base64UrlDecode(payloadPart))
|
|
984
|
+
);
|
|
985
|
+
const parsed = payloadSchema.safeParse(json);
|
|
986
|
+
return parsed.success ? parsed.data : void 0;
|
|
987
|
+
} catch {
|
|
988
|
+
return void 0;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
function fromPayload(payload) {
|
|
992
|
+
const base = {
|
|
993
|
+
id: payload.id,
|
|
994
|
+
route: payload.r,
|
|
995
|
+
key: payload.k,
|
|
996
|
+
file: { name: payload.n, size: payload.s, type: payload.t },
|
|
997
|
+
acl: payload.a,
|
|
998
|
+
checksum: payload.c === 1,
|
|
999
|
+
issuedAt: new Date(payload.iat * 1e3),
|
|
1000
|
+
expiresAt: new Date(payload.exp * 1e3),
|
|
1001
|
+
subjectHash: payload.sb
|
|
1002
|
+
};
|
|
1003
|
+
return payload.m === "multipart" ? {
|
|
1004
|
+
...base,
|
|
1005
|
+
mode: "multipart",
|
|
1006
|
+
uploadId: payload.u,
|
|
1007
|
+
partSize: payload.ps,
|
|
1008
|
+
partCount: payload.pc
|
|
1009
|
+
} : { ...base, mode: "single", kind: payload.v };
|
|
1010
|
+
}
|
|
1011
|
+
async function subjectHash(key, subject) {
|
|
1012
|
+
const mac = await crypto.subtle.sign(
|
|
1013
|
+
"HMAC",
|
|
1014
|
+
key,
|
|
1015
|
+
encoder.encode(`sub\0${subject}`)
|
|
1016
|
+
);
|
|
1017
|
+
return base64UrlEncode(new Uint8Array(mac)).slice(0, SUBJECT_HASH_LENGTH);
|
|
1018
|
+
}
|
|
1019
|
+
function equal(a, b) {
|
|
1020
|
+
if (a.length !== b.length) return false;
|
|
1021
|
+
let diff = 0;
|
|
1022
|
+
for (let i = 0; i < a.length; i++) {
|
|
1023
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
1024
|
+
}
|
|
1025
|
+
return diff === 0;
|
|
493
1026
|
}
|
|
494
|
-
function
|
|
495
|
-
|
|
496
|
-
|
|
1027
|
+
function epochSeconds(date) {
|
|
1028
|
+
return Math.floor(date.getTime() / 1e3);
|
|
1029
|
+
}
|
|
1030
|
+
var keyCache = /* @__PURE__ */ new Map();
|
|
1031
|
+
function ticketKeys(env) {
|
|
1032
|
+
return Promise.all(ticketSecrets(env).map(deriveKey));
|
|
1033
|
+
}
|
|
1034
|
+
function deriveKey(secret) {
|
|
1035
|
+
let key = keyCache.get(secret);
|
|
1036
|
+
if (!key) {
|
|
1037
|
+
key = crypto.subtle.importKey("raw", encoder.encode(secret), "HKDF", false, ["deriveKey"]).then(
|
|
1038
|
+
(material) => crypto.subtle.deriveKey(
|
|
1039
|
+
{
|
|
1040
|
+
name: "HKDF",
|
|
1041
|
+
hash: "SHA-256",
|
|
1042
|
+
salt: new Uint8Array(),
|
|
1043
|
+
info: encoder.encode(HKDF_INFO)
|
|
1044
|
+
},
|
|
1045
|
+
material,
|
|
1046
|
+
{ name: "HMAC", hash: "SHA-256", length: 256 },
|
|
1047
|
+
false,
|
|
1048
|
+
["sign", "verify"]
|
|
1049
|
+
)
|
|
1050
|
+
);
|
|
1051
|
+
keyCache.set(secret, key);
|
|
497
1052
|
}
|
|
1053
|
+
return key;
|
|
498
1054
|
}
|
|
499
|
-
function
|
|
500
|
-
if (
|
|
501
|
-
|
|
1055
|
+
function ticketSecrets(env) {
|
|
1056
|
+
if (env.secret) return env.secret;
|
|
1057
|
+
const fromEnv = readEnv(SECRET_ENV);
|
|
1058
|
+
if (fromEnv) {
|
|
1059
|
+
if (fromEnv.length < MIN_SECRET_LENGTH) {
|
|
1060
|
+
throw new ConfigError(
|
|
1061
|
+
`${SECRET_ENV} must have at least ${MIN_SECRET_LENGTH} characters.`
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
return [fromEnv];
|
|
1065
|
+
}
|
|
1066
|
+
const nodeEnv = readEnv("NODE_ENV");
|
|
1067
|
+
if (nodeEnv === "development" || nodeEnv === "test") {
|
|
1068
|
+
return [developmentSecret(env)];
|
|
502
1069
|
}
|
|
503
|
-
|
|
1070
|
+
throw new ConfigError(
|
|
1071
|
+
`Set ${SECRET_ENV} (at least ${MIN_SECRET_LENGTH} characters, the same on every instance) or dimahS3({ secret }) to sign upload tickets.`
|
|
1072
|
+
);
|
|
504
1073
|
}
|
|
505
|
-
function
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if (
|
|
509
|
-
|
|
1074
|
+
function developmentSecret(env) {
|
|
1075
|
+
const store = globalThis;
|
|
1076
|
+
let secret = store[DEV_SECRET];
|
|
1077
|
+
if (secret === void 0) {
|
|
1078
|
+
secret = base64UrlEncode(crypto.getRandomValues(new Uint8Array(32)));
|
|
1079
|
+
store[DEV_SECRET] = secret;
|
|
1080
|
+
env.logger.warn?.(
|
|
1081
|
+
`${SECRET_ENV} is not set, so upload tickets use a random development secret. Set it before deploying.`
|
|
1082
|
+
);
|
|
510
1083
|
}
|
|
1084
|
+
return secret;
|
|
1085
|
+
}
|
|
1086
|
+
function readEnv(name) {
|
|
1087
|
+
return globalThis.process?.env?.[name] || void 0;
|
|
511
1088
|
}
|
|
512
1089
|
|
|
513
|
-
// src/helpers/
|
|
514
|
-
function
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
}
|
|
525
|
-
function
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
1090
|
+
// src/helpers/open-target.ts
|
|
1091
|
+
function getRoute(config, name) {
|
|
1092
|
+
if (!Object.hasOwn(config.routes, name)) {
|
|
1093
|
+
throw s3Error("UNKNOWN_ROUTE", { route: name });
|
|
1094
|
+
}
|
|
1095
|
+
return config.routes[name];
|
|
1096
|
+
}
|
|
1097
|
+
function assertFeature(route2, feature) {
|
|
1098
|
+
if (!isFeatureEnabled(route2, feature)) {
|
|
1099
|
+
throw s3Error("FEATURE_DISABLED", { feature });
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
function isFeatureEnabled(route2, feature) {
|
|
1103
|
+
switch (feature) {
|
|
1104
|
+
case "multipart":
|
|
1105
|
+
return route2.upload.multipart.enabled;
|
|
1106
|
+
case "proxy":
|
|
1107
|
+
return route2.download.enabled && route2.download.mode === "proxy";
|
|
1108
|
+
default:
|
|
1109
|
+
return route2[feature].enabled;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
async function openRoute(config, context, name, feature) {
|
|
1113
|
+
const route2 = getRoute(config, name);
|
|
1114
|
+
assertFeature(route2, feature);
|
|
1115
|
+
const routeContext = { ...context, route: route2.name };
|
|
1116
|
+
await runGuards(route2.guard, routeContext);
|
|
1117
|
+
return { route: route2, context: routeContext };
|
|
1118
|
+
}
|
|
1119
|
+
async function openObject(config, context, input, feature) {
|
|
1120
|
+
const opened = await openRoute(config, context, input.route, feature);
|
|
1121
|
+
const key = assertStoredKey(input.key, opened.route.keyPrefix);
|
|
1122
|
+
return {
|
|
1123
|
+
route: opened.route,
|
|
1124
|
+
context: { ...opened.context, key, bucket: opened.route.bucket }
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
var DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
|
1128
|
+
async function openUploadTarget(config, context, input, kind) {
|
|
1129
|
+
const feature = kind === "multipart" ? "multipart" : "upload";
|
|
1130
|
+
const { route: route2, context: routeContext } = await openRoute(
|
|
1131
|
+
config,
|
|
1132
|
+
context,
|
|
1133
|
+
input.route,
|
|
1134
|
+
feature
|
|
1135
|
+
);
|
|
1136
|
+
const ticketSubject = kind === "server" ? void 0 : await config.ticketSubject?.(routeContext);
|
|
1137
|
+
assertFileConstraints(route2.upload, input.file);
|
|
1138
|
+
if (kind !== "multipart") assertSingleUploadSize(input.file.size);
|
|
1139
|
+
if ((kind === "post" || kind === "put") && route2.upload.checksum) {
|
|
1140
|
+
if (!input.checksum) throw s3Error("CHECKSUM_REQUIRED");
|
|
1141
|
+
}
|
|
1142
|
+
const object = await resolveUploadObject(route2, {
|
|
1143
|
+
...routeContext,
|
|
1144
|
+
file: input.file,
|
|
1145
|
+
bucket: route2.bucket,
|
|
1146
|
+
keyPrefix: route2.keyPrefix,
|
|
1147
|
+
clientMetadata: input.clientMetadata
|
|
547
1148
|
});
|
|
548
|
-
const
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
type: input.contentType
|
|
555
|
-
},
|
|
556
|
-
clientMetadata: input.metadata
|
|
1149
|
+
const metadata = input.metadata ? { ...object.metadata, ...input.metadata } : object.metadata;
|
|
1150
|
+
const uploadToken = createUploadToken();
|
|
1151
|
+
const s3Metadata = buildObjectMetadata({
|
|
1152
|
+
metadata,
|
|
1153
|
+
uploadToken,
|
|
1154
|
+
previousKey: object.previousKey
|
|
557
1155
|
});
|
|
1156
|
+
const guardContext = {
|
|
1157
|
+
...routeContext,
|
|
1158
|
+
key: object.key,
|
|
1159
|
+
bucket: object.bucket,
|
|
1160
|
+
kind,
|
|
1161
|
+
file: input.file,
|
|
1162
|
+
acl: object.acl,
|
|
1163
|
+
metadata,
|
|
1164
|
+
clientMetadata: input.clientMetadata,
|
|
1165
|
+
storageClass: object.storageClass,
|
|
1166
|
+
cacheControl: object.cacheControl,
|
|
1167
|
+
tagging: object.tagging
|
|
1168
|
+
};
|
|
1169
|
+
await runGuards(route2.upload.guard, guardContext);
|
|
558
1170
|
return {
|
|
559
1171
|
route: route2,
|
|
560
|
-
|
|
561
|
-
|
|
1172
|
+
context: guardContext,
|
|
1173
|
+
object: { ...object, ...metadata ? { metadata } : {} },
|
|
1174
|
+
uploadToken,
|
|
1175
|
+
ticketSubject,
|
|
1176
|
+
s3Metadata,
|
|
1177
|
+
contentType: input.file.type ?? DEFAULT_CONTENT_TYPE,
|
|
1178
|
+
contentDisposition: buildContentDisposition(input.file.name)
|
|
562
1179
|
};
|
|
563
1180
|
}
|
|
564
|
-
async function
|
|
565
|
-
const
|
|
566
|
-
const
|
|
1181
|
+
async function openTicket(config, context, token, feature) {
|
|
1182
|
+
const mode = feature === "multipart" ? "multipart" : "single";
|
|
1183
|
+
const ticket = await readTicket(config, token, {
|
|
1184
|
+
mode,
|
|
1185
|
+
operation: context.operation
|
|
1186
|
+
});
|
|
1187
|
+
if (!Object.hasOwn(config.routes, ticket.route)) {
|
|
1188
|
+
throw s3Error("UPLOAD_TICKET_INVALID");
|
|
1189
|
+
}
|
|
1190
|
+
const route2 = config.routes[ticket.route];
|
|
1191
|
+
assertFeature(route2, feature);
|
|
1192
|
+
let key;
|
|
1193
|
+
try {
|
|
1194
|
+
key = assertStoredKey(ticket.key, route2.keyPrefix);
|
|
1195
|
+
} catch {
|
|
1196
|
+
throw s3Error("UPLOAD_TICKET_INVALID");
|
|
1197
|
+
}
|
|
1198
|
+
const routeContext = { ...context, route: route2.name };
|
|
1199
|
+
await runGuards(route2.guard, routeContext);
|
|
1200
|
+
await assertTicketSubject(config, ticket, routeContext);
|
|
567
1201
|
return {
|
|
568
1202
|
route: route2,
|
|
569
|
-
|
|
570
|
-
|
|
1203
|
+
ticket,
|
|
1204
|
+
context: { ...routeContext, key, bucket: route2.bucket }
|
|
571
1205
|
};
|
|
572
1206
|
}
|
|
573
|
-
|
|
1207
|
+
|
|
1208
|
+
// src/operations/run-operation.ts
|
|
1209
|
+
async function runOperation(config, operation, options, run) {
|
|
1210
|
+
const request = options.request ?? syntheticRequest(config, options);
|
|
574
1211
|
try {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
1212
|
+
const context = {
|
|
1213
|
+
request,
|
|
1214
|
+
operation,
|
|
1215
|
+
locals: createLocals(options.locals)
|
|
1216
|
+
};
|
|
1217
|
+
await runGuards(config.guard, context);
|
|
1218
|
+
return await run(context);
|
|
1219
|
+
} catch (error) {
|
|
1220
|
+
throw toAPIError(error, config, request, operation);
|
|
580
1221
|
}
|
|
581
1222
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
1223
|
+
|
|
1224
|
+
// src/operations/confirm.ts
|
|
1225
|
+
function confirmUpload(config, body, options = {}) {
|
|
1226
|
+
return runOperation(config, "confirm", options, async (call2) => {
|
|
1227
|
+
const { route: route2, ticket, context } = await openTicket(
|
|
1228
|
+
config,
|
|
1229
|
+
call2,
|
|
1230
|
+
body.ticket,
|
|
1231
|
+
"upload"
|
|
1232
|
+
);
|
|
1233
|
+
const head = await headObjectOrNotFound(
|
|
1234
|
+
route2.client,
|
|
1235
|
+
context.bucket,
|
|
1236
|
+
context.key
|
|
1237
|
+
);
|
|
1238
|
+
return finalizeConfirmed({
|
|
1239
|
+
route: route2,
|
|
1240
|
+
context,
|
|
1241
|
+
kind: ticket.kind,
|
|
1242
|
+
uploadToken: ticket.id,
|
|
1243
|
+
file: ticket.file,
|
|
1244
|
+
acl: ticket.acl,
|
|
1245
|
+
head
|
|
1246
|
+
});
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
function deleteStoredObject(config, payload, options = {}) {
|
|
1250
|
+
return runOperation(config, "delete", options, async (call2) => {
|
|
1251
|
+
const { route: route2, context } = await openObject(
|
|
1252
|
+
config,
|
|
1253
|
+
call2,
|
|
1254
|
+
payload,
|
|
1255
|
+
"delete"
|
|
1256
|
+
);
|
|
1257
|
+
await runGuards(route2.delete.guard, context);
|
|
1258
|
+
const { bucket, key } = context;
|
|
1259
|
+
await route2.client.send(
|
|
1260
|
+
new DeleteObjectCommand({ Bucket: bucket, Key: key })
|
|
1261
|
+
);
|
|
1262
|
+
await runLifecycle(route2.delete.onDeleted, context);
|
|
1263
|
+
return { key, bucket };
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
function deleteManyObjects(config, payload, options = {}) {
|
|
1267
|
+
return runOperation(config, "deleteMany", options, async (call2) => {
|
|
1268
|
+
const { route: route2, context: routeContext } = await openRoute(
|
|
1269
|
+
config,
|
|
1270
|
+
call2,
|
|
1271
|
+
payload.route,
|
|
1272
|
+
"delete"
|
|
1273
|
+
);
|
|
1274
|
+
const deleted = [];
|
|
1275
|
+
const failed = [];
|
|
1276
|
+
const fail = (key, error) => {
|
|
1277
|
+
const apiError = toAPIError(error, config, call2.request, call2.operation);
|
|
1278
|
+
failed.push({ key, error: errorShape(apiError) });
|
|
1279
|
+
};
|
|
1280
|
+
const allowed = /* @__PURE__ */ new Map();
|
|
1281
|
+
for (const input of new Set(payload.keys)) {
|
|
1282
|
+
try {
|
|
1283
|
+
const key = assertStoredKey(input, route2.keyPrefix);
|
|
1284
|
+
if (allowed.has(key)) continue;
|
|
1285
|
+
const context = {
|
|
1286
|
+
...routeContext,
|
|
1287
|
+
key,
|
|
1288
|
+
bucket: route2.bucket,
|
|
1289
|
+
locals: createLocals(routeContext.locals)
|
|
1290
|
+
};
|
|
1291
|
+
await runGuards(route2.delete.guard, context);
|
|
1292
|
+
allowed.set(key, context);
|
|
1293
|
+
} catch (error) {
|
|
1294
|
+
fail(input, error);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
if (allowed.size === 0) return { deleted, failed };
|
|
1298
|
+
const { Errors = [] } = await route2.client.send(
|
|
1299
|
+
new DeleteObjectsCommand({
|
|
1300
|
+
Bucket: route2.bucket,
|
|
1301
|
+
Delete: {
|
|
1302
|
+
Objects: [...allowed.keys()].map((Key) => ({ Key })),
|
|
1303
|
+
Quiet: true
|
|
1304
|
+
}
|
|
1305
|
+
})
|
|
1306
|
+
);
|
|
1307
|
+
for (const { Key, Code = "UnknownError", Message } of Errors) {
|
|
1308
|
+
if (Key === void 0 || !allowed.delete(Key)) continue;
|
|
1309
|
+
const detail = Message ? `${Code}: ${Message}` : Code;
|
|
1310
|
+
fail(Key, new Error(`DeleteObjects failed for "${Key}": ${detail}`));
|
|
1311
|
+
}
|
|
1312
|
+
for (const [key, context] of allowed) {
|
|
1313
|
+
try {
|
|
1314
|
+
await runLifecycle(route2.delete.onDeleted, context);
|
|
1315
|
+
deleted.push(key);
|
|
1316
|
+
} catch (error) {
|
|
1317
|
+
fail(key, error);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
return { deleted, failed };
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
function errorShape(error) {
|
|
1324
|
+
const { code, message, params } = error;
|
|
1325
|
+
return { code, message, ...params ? { params } : {} };
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// src/helpers/authorize-download.ts
|
|
1329
|
+
async function authorizeDownload(config, route2, context, request) {
|
|
1330
|
+
const { download } = route2;
|
|
1331
|
+
const presign2 = request.mode === "presign";
|
|
1332
|
+
const requested = request.disposition ?? download.disposition;
|
|
1333
|
+
const resolved = await download.resolve?.({
|
|
1334
|
+
...context,
|
|
1335
|
+
mode: request.mode,
|
|
1336
|
+
name: request.name,
|
|
1337
|
+
disposition: requested,
|
|
1338
|
+
...presign2 ? { expiresIn: request.expiresIn } : {}
|
|
1339
|
+
}) ?? void 0;
|
|
1340
|
+
const guardContext = {
|
|
1341
|
+
...context,
|
|
1342
|
+
mode: request.mode,
|
|
1343
|
+
name: resolved?.name ?? request.name,
|
|
1344
|
+
disposition: resolved?.disposition ?? requested,
|
|
1345
|
+
...presign2 ? {
|
|
1346
|
+
expiresIn: signedExpiresIn(
|
|
1347
|
+
request.expiresIn,
|
|
1348
|
+
resolved?.expiresIn,
|
|
1349
|
+
download.maxExpiresIn,
|
|
1350
|
+
config.maxExpiresIn
|
|
1351
|
+
)
|
|
1352
|
+
} : {}
|
|
1353
|
+
};
|
|
1354
|
+
await runGuards(download.guard, guardContext);
|
|
1355
|
+
return { context: guardContext, resolved };
|
|
1356
|
+
}
|
|
1357
|
+
function signedExpiresIn(requested, resolved, routeMaxExpiresIn, maxExpiresIn) {
|
|
1358
|
+
if (resolved !== void 0) {
|
|
1359
|
+
if (!Number.isSafeInteger(resolved) || resolved <= 0) {
|
|
1360
|
+
throw new ConfigError(
|
|
1361
|
+
`download.resolve returned expiresIn ${String(resolved)}; return a positive integer of seconds.`
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
return Math.min(resolved, maxExpiresIn);
|
|
1365
|
+
}
|
|
1366
|
+
return Math.min(requested ?? routeMaxExpiresIn, routeMaxExpiresIn);
|
|
1367
|
+
}
|
|
1368
|
+
function resolveObjectDisposition(inlineTypes, input) {
|
|
1369
|
+
const name = input.name ?? resolveStoredFileName(input.contentDisposition, input.key);
|
|
1370
|
+
const resolved = resolveInlineDisposition({
|
|
1371
|
+
contentType: input.contentType,
|
|
1372
|
+
fileName: name,
|
|
1373
|
+
allow: inlineTypes,
|
|
1374
|
+
requested: input.requested
|
|
1375
|
+
});
|
|
1376
|
+
return {
|
|
1377
|
+
...resolved,
|
|
1378
|
+
header: name ? buildContentDisposition(name, resolved.disposition) : resolved.disposition
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
// src/operations/download.ts
|
|
1383
|
+
function presignDownload(config, query, options = {}) {
|
|
1384
|
+
return runOperation(config, "download", options, async (call2) => {
|
|
1385
|
+
const { route: route2, context } = await openObject(
|
|
1386
|
+
config,
|
|
1387
|
+
call2,
|
|
1388
|
+
query,
|
|
1389
|
+
"download"
|
|
1390
|
+
);
|
|
1391
|
+
const { download } = route2;
|
|
1392
|
+
const { mode } = download;
|
|
1393
|
+
const presign2 = mode === "presign";
|
|
1394
|
+
const { context: guardContext } = await authorizeDownload(
|
|
1395
|
+
config,
|
|
1396
|
+
route2,
|
|
1397
|
+
context,
|
|
1398
|
+
{
|
|
1399
|
+
mode,
|
|
1400
|
+
name: query.name,
|
|
1401
|
+
disposition: query.disposition,
|
|
1402
|
+
expiresIn: query.expiresIn
|
|
1403
|
+
}
|
|
1404
|
+
);
|
|
1405
|
+
const { bucket, key } = context;
|
|
1406
|
+
const head = await headObjectOrNotFound(route2.client, bucket, key, {
|
|
1407
|
+
abortSignal: call2.request.signal
|
|
1408
|
+
});
|
|
1409
|
+
const sent = resolveObjectDisposition(download.inlineTypes, {
|
|
1410
|
+
key,
|
|
1411
|
+
name: guardContext.name,
|
|
1412
|
+
requested: guardContext.disposition,
|
|
1413
|
+
contentType: head.ContentType,
|
|
1414
|
+
contentDisposition: head.ContentDisposition
|
|
1415
|
+
});
|
|
1416
|
+
const url = presign2 ? await getSignedUrl(
|
|
1417
|
+
route2.client,
|
|
1418
|
+
new GetObjectCommand({
|
|
1419
|
+
Bucket: bucket,
|
|
1420
|
+
Key: key,
|
|
1421
|
+
ResponseContentDisposition: sent.header,
|
|
1422
|
+
ResponseContentType: sent.contentType
|
|
1423
|
+
}),
|
|
1424
|
+
{ expiresIn: guardContext.expiresIn }
|
|
1425
|
+
) : proxyUrl(config, {
|
|
1426
|
+
route: route2.name,
|
|
1427
|
+
key,
|
|
1428
|
+
name: guardContext.name,
|
|
1429
|
+
disposition: sent.disposition
|
|
1430
|
+
});
|
|
1431
|
+
await runLifecycle(download.onPresigned, {
|
|
1432
|
+
...guardContext,
|
|
1433
|
+
disposition: sent.disposition,
|
|
1434
|
+
url
|
|
1435
|
+
});
|
|
1436
|
+
const base = { key, bucket, url, disposition: sent.disposition };
|
|
1437
|
+
return guardContext.expiresIn === void 0 ? { ...base, mode: "proxy" } : { ...base, mode: "presign", expiresIn: guardContext.expiresIn };
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
function proxyUrl(config, query) {
|
|
1441
|
+
const params = new URLSearchParams({ route: query.route, key: query.key });
|
|
1442
|
+
if (query.name !== void 0) params.set("name", query.name);
|
|
1443
|
+
params.set("disposition", query.disposition);
|
|
1444
|
+
const base = config.baseURL ?? config.basePath;
|
|
1445
|
+
return `${base}${S3_API_ROUTES.file}?${params}`;
|
|
1446
|
+
}
|
|
1447
|
+
var FILE_CACHE_CONTROL = "private, no-cache";
|
|
1448
|
+
var FILE_HEADERS = {
|
|
1449
|
+
"Content-Security-Policy": "default-src 'none'; img-src 'self' data:; media-src 'self'; style-src 'unsafe-inline'; sandbox",
|
|
1450
|
+
"X-Content-Type-Options": "nosniff",
|
|
1451
|
+
"Cross-Origin-Resource-Policy": "same-origin",
|
|
1452
|
+
"Accept-Ranges": "bytes"
|
|
1453
|
+
};
|
|
1454
|
+
var SINGLE_RANGE = /^bytes=(?:(\d+)-(\d*)|-(\d+))$/i;
|
|
1455
|
+
async function serveFile(config, query, options = {}) {
|
|
1456
|
+
try {
|
|
1457
|
+
return await runOperation(config, "file", options, async (call2) => {
|
|
1458
|
+
const { route: route2, context } = await openObject(config, call2, query, "proxy");
|
|
1459
|
+
const { download } = route2;
|
|
1460
|
+
const authorized = await authorizeDownload(config, route2, context, {
|
|
1461
|
+
mode: "proxy",
|
|
1462
|
+
name: query.name,
|
|
1463
|
+
disposition: query.disposition
|
|
1464
|
+
});
|
|
1465
|
+
const { resolved } = authorized;
|
|
1466
|
+
const { name, disposition } = authorized.context;
|
|
1467
|
+
const headers = fileHeaders(resolved?.cacheControl);
|
|
1468
|
+
let output;
|
|
1469
|
+
try {
|
|
1470
|
+
output = await getObject(route2, context, call2.request);
|
|
1471
|
+
} catch (error) {
|
|
1472
|
+
if (awsStatus(error) === 304) return notModified(error, headers);
|
|
1473
|
+
throw await getObjectError(error, route2, context, call2.request.signal);
|
|
1474
|
+
}
|
|
1475
|
+
const sent = resolveObjectDisposition(download.inlineTypes, {
|
|
1476
|
+
key: context.key,
|
|
1477
|
+
name,
|
|
1478
|
+
requested: disposition,
|
|
1479
|
+
contentType: output.ContentType,
|
|
1480
|
+
contentDisposition: output.ContentDisposition
|
|
1481
|
+
});
|
|
1482
|
+
headers.set(
|
|
1483
|
+
"Content-Type",
|
|
1484
|
+
sent.contentType ?? output.ContentType ?? "application/octet-stream"
|
|
1485
|
+
);
|
|
1486
|
+
headers.set("Content-Disposition", sent.header);
|
|
1487
|
+
if (output.ContentLength !== void 0) {
|
|
1488
|
+
headers.set("Content-Length", String(output.ContentLength));
|
|
1489
|
+
}
|
|
1490
|
+
if (output.ContentRange) {
|
|
1491
|
+
headers.set("Content-Range", output.ContentRange);
|
|
1492
|
+
}
|
|
1493
|
+
if (output.ContentEncoding) {
|
|
1494
|
+
headers.set("Content-Encoding", output.ContentEncoding);
|
|
1495
|
+
}
|
|
1496
|
+
if (output.ContentLanguage) {
|
|
1497
|
+
headers.set("Content-Language", output.ContentLanguage);
|
|
1498
|
+
}
|
|
1499
|
+
setValidators(headers, output.ETag, output.LastModified?.toUTCString());
|
|
1500
|
+
return new Response(output.Body?.transformToWebStream() ?? null, {
|
|
1501
|
+
status: output.ContentRange ? 206 : 200,
|
|
1502
|
+
headers
|
|
1503
|
+
});
|
|
1504
|
+
});
|
|
1505
|
+
} catch (error) {
|
|
1506
|
+
throw withFileHeaders(error);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
function fileHeaders(cacheControl = FILE_CACHE_CONTROL) {
|
|
1510
|
+
const headers = new Headers(FILE_HEADERS);
|
|
1511
|
+
headers.set("Cache-Control", cacheControl);
|
|
1512
|
+
return headers;
|
|
1513
|
+
}
|
|
1514
|
+
async function getObject(route2, context, request) {
|
|
1515
|
+
const range = singleRange(request.headers.get("range"));
|
|
1516
|
+
const ifRange = range ? request.headers.get("if-range") : null;
|
|
1517
|
+
const send = (Range) => route2.client.send(
|
|
1518
|
+
new GetObjectCommand({
|
|
1519
|
+
Bucket: context.bucket,
|
|
1520
|
+
Key: context.key,
|
|
1521
|
+
Range,
|
|
1522
|
+
...conditionalInput(request.headers)
|
|
1523
|
+
}),
|
|
1524
|
+
{ abortSignal: request.signal }
|
|
585
1525
|
);
|
|
1526
|
+
const output = await send(range);
|
|
1527
|
+
if (ifRange === null || !output.ContentRange) return output;
|
|
1528
|
+
if (ifRangeMatches(ifRange, output)) return output;
|
|
1529
|
+
await output.Body?.transformToWebStream().cancel();
|
|
1530
|
+
return send(void 0);
|
|
1531
|
+
}
|
|
1532
|
+
function singleRange(header) {
|
|
1533
|
+
const match = header ? SINGLE_RANGE.exec(header.trim()) : null;
|
|
1534
|
+
if (!match) return void 0;
|
|
1535
|
+
const [, first, last, suffix] = match;
|
|
1536
|
+
if (suffix !== void 0) return `bytes=-${suffix}`;
|
|
1537
|
+
if (last !== "" && Number(last) < Number(first)) return void 0;
|
|
1538
|
+
return `bytes=${first}-${last}`;
|
|
1539
|
+
}
|
|
1540
|
+
function conditionalInput(headers) {
|
|
1541
|
+
return {
|
|
1542
|
+
IfMatch: headers.get("if-match") ?? void 0,
|
|
1543
|
+
IfNoneMatch: headers.get("if-none-match") ?? void 0,
|
|
1544
|
+
IfModifiedSince: httpDate(headers.get("if-modified-since")),
|
|
1545
|
+
IfUnmodifiedSince: httpDate(headers.get("if-unmodified-since"))
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
function httpDate(value) {
|
|
1549
|
+
if (value === null) return void 0;
|
|
1550
|
+
const time = Date.parse(value);
|
|
1551
|
+
return Number.isNaN(time) ? void 0 : new Date(time);
|
|
1552
|
+
}
|
|
1553
|
+
function ifRangeMatches(ifRange, output) {
|
|
1554
|
+
const value = ifRange.trim();
|
|
1555
|
+
if (value.startsWith('"')) return value === output.ETag;
|
|
1556
|
+
const time = Date.parse(value);
|
|
1557
|
+
return !Number.isNaN(time) && output.LastModified !== void 0 && Math.floor(output.LastModified.getTime() / 1e3) === Math.floor(time / 1e3);
|
|
586
1558
|
}
|
|
587
|
-
|
|
588
|
-
const
|
|
589
|
-
|
|
1559
|
+
function notModified(error, headers) {
|
|
1560
|
+
const s3Headers = new Headers(
|
|
1561
|
+
error.$response?.headers
|
|
590
1562
|
);
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
return
|
|
1563
|
+
setValidators(
|
|
1564
|
+
headers,
|
|
1565
|
+
s3Headers.get("etag") ?? void 0,
|
|
1566
|
+
s3Headers.get("last-modified") ?? void 0
|
|
1567
|
+
);
|
|
1568
|
+
return new Response(null, { status: 304, headers });
|
|
597
1569
|
}
|
|
598
|
-
function
|
|
599
|
-
|
|
600
|
-
|
|
1570
|
+
async function getObjectError(error, route2, context, abortSignal) {
|
|
1571
|
+
const status = awsStatus(error);
|
|
1572
|
+
if (status === 412) {
|
|
1573
|
+
return s3Error("PRECONDITION_FAILED", void 0, { cause: error });
|
|
1574
|
+
}
|
|
1575
|
+
if (status === 416) {
|
|
1576
|
+
const rangeError = s3Error("RANGE_NOT_SATISFIABLE", void 0, {
|
|
1577
|
+
cause: error
|
|
1578
|
+
});
|
|
1579
|
+
const size = actualObjectSize(error) ?? await storedSize(route2, context, abortSignal);
|
|
1580
|
+
if (size !== void 0) {
|
|
1581
|
+
rangeError.headers = { "Content-Range": `bytes */${size}` };
|
|
1582
|
+
}
|
|
1583
|
+
return rangeError;
|
|
601
1584
|
}
|
|
1585
|
+
if (isAwsNotFound(error)) {
|
|
1586
|
+
return s3Error("OBJECT_NOT_FOUND", void 0, { cause: error });
|
|
1587
|
+
}
|
|
1588
|
+
return error;
|
|
1589
|
+
}
|
|
1590
|
+
function actualObjectSize(error) {
|
|
1591
|
+
const value = error.ActualObjectSize;
|
|
1592
|
+
const size = typeof value === "string" && /^\d+$/.test(value) ? Number(value) : value;
|
|
1593
|
+
return typeof size === "number" && Number.isSafeInteger(size) && size >= 0 ? size : void 0;
|
|
1594
|
+
}
|
|
1595
|
+
async function storedSize(route2, context, abortSignal) {
|
|
1596
|
+
const head = await headObjectOrNotFound(
|
|
1597
|
+
route2.client,
|
|
1598
|
+
context.bucket,
|
|
1599
|
+
context.key,
|
|
1600
|
+
{ abortSignal }
|
|
1601
|
+
);
|
|
602
1602
|
return head.ContentLength;
|
|
603
1603
|
}
|
|
1604
|
+
function setValidators(headers, eTag, lastModified) {
|
|
1605
|
+
if (eTag) headers.set("ETag", eTag);
|
|
1606
|
+
if (lastModified) headers.set("Last-Modified", lastModified);
|
|
1607
|
+
}
|
|
1608
|
+
function withFileHeaders(error) {
|
|
1609
|
+
if (!isAPIError(error)) return error;
|
|
1610
|
+
const headers = fileHeaders();
|
|
1611
|
+
new Headers(error.headers).forEach((value, name) => {
|
|
1612
|
+
headers.set(name, value);
|
|
1613
|
+
});
|
|
1614
|
+
error.headers = headers;
|
|
1615
|
+
return error;
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
// src/operations/multipart/open-multipart.ts
|
|
1619
|
+
async function openMultipart(config, call2, token) {
|
|
1620
|
+
const { route: route2, ticket, context } = await openTicket(
|
|
1621
|
+
config,
|
|
1622
|
+
call2,
|
|
1623
|
+
token,
|
|
1624
|
+
"multipart"
|
|
1625
|
+
);
|
|
1626
|
+
return {
|
|
1627
|
+
route: route2,
|
|
1628
|
+
ticket,
|
|
1629
|
+
objectContext: context,
|
|
1630
|
+
context: {
|
|
1631
|
+
...context,
|
|
1632
|
+
uploadId: ticket.uploadId,
|
|
1633
|
+
uploadToken: ticket.id,
|
|
1634
|
+
file: ticket.file
|
|
1635
|
+
},
|
|
1636
|
+
layout: {
|
|
1637
|
+
size: ticket.file.size,
|
|
1638
|
+
partSize: ticket.partSize,
|
|
1639
|
+
partCount: ticket.partCount
|
|
1640
|
+
}
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
// src/operations/multipart/abort.ts
|
|
1645
|
+
function abortMultipart(config, body, options = {}) {
|
|
1646
|
+
return runOperation(config, "multipart.abort", options, async (call2) => {
|
|
1647
|
+
const { route: route2, context } = await openMultipart(config, call2, body.ticket);
|
|
1648
|
+
await runGuards(route2.upload.multipart.guard, {
|
|
1649
|
+
...context,
|
|
1650
|
+
action: "abort"
|
|
1651
|
+
});
|
|
1652
|
+
try {
|
|
1653
|
+
await route2.client.send(
|
|
1654
|
+
new AbortMultipartUploadCommand({
|
|
1655
|
+
Bucket: context.bucket,
|
|
1656
|
+
Key: context.key,
|
|
1657
|
+
UploadId: context.uploadId
|
|
1658
|
+
})
|
|
1659
|
+
);
|
|
1660
|
+
} catch (error) {
|
|
1661
|
+
if (!isAwsNotFound(error)) throw error;
|
|
1662
|
+
if (await wasCompleted(route2, context)) throw s3Error("CONFLICT");
|
|
1663
|
+
}
|
|
1664
|
+
await runLifecycle(route2.upload.multipart.onAbort, context);
|
|
1665
|
+
return {
|
|
1666
|
+
key: context.key,
|
|
1667
|
+
bucket: context.bucket,
|
|
1668
|
+
uploadId: context.uploadId
|
|
1669
|
+
};
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
async function wasCompleted(route2, context) {
|
|
1673
|
+
try {
|
|
1674
|
+
const head = await headObjectOrNotFound(
|
|
1675
|
+
route2.client,
|
|
1676
|
+
context.bucket,
|
|
1677
|
+
context.key
|
|
1678
|
+
);
|
|
1679
|
+
return readUploadMarker(head.Metadata) === context.uploadToken;
|
|
1680
|
+
} catch (error) {
|
|
1681
|
+
if (isS3ErrorCode(error, "OBJECT_NOT_FOUND")) return false;
|
|
1682
|
+
throw error;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
var LIST_PARTS_PAGE_SIZE = 1e3;
|
|
604
1686
|
async function listAllParts(client, input) {
|
|
605
1687
|
const parts = [];
|
|
606
1688
|
let partNumberMarker;
|
|
@@ -612,1322 +1694,1308 @@ async function listAllParts(client, input) {
|
|
|
612
1694
|
Bucket: input.bucket,
|
|
613
1695
|
Key: input.key,
|
|
614
1696
|
UploadId: input.uploadId,
|
|
615
|
-
|
|
1697
|
+
MaxParts: LIST_PARTS_PAGE_SIZE,
|
|
1698
|
+
PartNumberMarker: partNumberMarker
|
|
616
1699
|
})
|
|
617
1700
|
)
|
|
618
1701
|
);
|
|
619
1702
|
parts.push(...response.Parts ?? []);
|
|
620
1703
|
isTruncated = response.IsTruncated === true;
|
|
621
|
-
partNumberMarker =
|
|
1704
|
+
partNumberMarker = response.NextPartNumberMarker;
|
|
1705
|
+
if (isTruncated && partNumberMarker === void 0) {
|
|
1706
|
+
throw new Error(
|
|
1707
|
+
`ListParts for "${input.key}" is truncated but has no NextPartNumberMarker.`
|
|
1708
|
+
);
|
|
1709
|
+
}
|
|
622
1710
|
}
|
|
623
1711
|
return parts;
|
|
624
1712
|
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
1713
|
+
var MIB = 1024 * 1024;
|
|
1714
|
+
function partLayout(size, preferred) {
|
|
1715
|
+
if (size > S3_MAX_OBJECT_SIZE) {
|
|
1716
|
+
throw s3Error("FILE_TOO_LARGE", {
|
|
1717
|
+
size,
|
|
1718
|
+
maxFileSize: S3_MAX_OBJECT_SIZE
|
|
1719
|
+
});
|
|
632
1720
|
}
|
|
633
|
-
|
|
1721
|
+
const wanted = Math.max(
|
|
1722
|
+
preferred ?? S3_DEFAULT_PART_SIZE,
|
|
1723
|
+
Math.ceil(size / S3_MAX_PART_NUMBER)
|
|
1724
|
+
);
|
|
1725
|
+
const partSize = Math.min(
|
|
1726
|
+
Math.max(Math.ceil(wanted / MIB) * MIB, S3_MIN_PART_SIZE),
|
|
1727
|
+
S3_MAX_PART_SIZE
|
|
1728
|
+
);
|
|
1729
|
+
return { size, partSize, partCount: Math.ceil(size / partSize) };
|
|
634
1730
|
}
|
|
635
|
-
function
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
1731
|
+
function partByteSize(layout, partNumber) {
|
|
1732
|
+
return partNumber < layout.partCount ? layout.partSize : layout.size - layout.partSize * (layout.partCount - 1);
|
|
1733
|
+
}
|
|
1734
|
+
function assertPartNumber(layout, partNumber) {
|
|
1735
|
+
if (!Number.isInteger(partNumber) || partNumber < 1 || partNumber > layout.partCount) {
|
|
1736
|
+
throw s3Error("MULTIPART_PART_INVALID", {
|
|
1737
|
+
partNumber,
|
|
1738
|
+
partCount: layout.partCount
|
|
1739
|
+
});
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
// src/operations/multipart/complete.ts
|
|
1744
|
+
function completeMultipart(config, body, options = {}) {
|
|
1745
|
+
return runOperation(config, "multipart.complete", options, async (call2) => {
|
|
1746
|
+
const { route: route2, ticket, objectContext, context, layout } = await openMultipart(config, call2, body.ticket);
|
|
1747
|
+
await runGuards(route2.upload.multipart.guard, {
|
|
1748
|
+
...context,
|
|
1749
|
+
action: "complete"
|
|
1750
|
+
});
|
|
1751
|
+
const { bucket, key, uploadId } = context;
|
|
1752
|
+
const listed = await listPartsOfOpenUpload(route2.client, context);
|
|
1753
|
+
if (listed) {
|
|
1754
|
+
const parts = completedParts(listed, layout, ticket.checksum);
|
|
1755
|
+
try {
|
|
1756
|
+
await sendOrObjectNotFound(
|
|
1757
|
+
() => route2.client.send(
|
|
1758
|
+
new CompleteMultipartUploadCommand({
|
|
1759
|
+
Bucket: bucket,
|
|
1760
|
+
Key: key,
|
|
1761
|
+
UploadId: uploadId,
|
|
1762
|
+
MultipartUpload: { Parts: parts }
|
|
1763
|
+
})
|
|
1764
|
+
)
|
|
1765
|
+
);
|
|
1766
|
+
} catch (error) {
|
|
1767
|
+
if (!isS3ErrorCode(error, "OBJECT_NOT_FOUND")) throw error;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
const head = await headObjectOrNotFound(route2.client, bucket, key);
|
|
1771
|
+
const confirmed = await finalizeConfirmed({
|
|
1772
|
+
route: route2,
|
|
1773
|
+
context: objectContext,
|
|
1774
|
+
kind: "multipart",
|
|
1775
|
+
uploadToken: ticket.id,
|
|
1776
|
+
uploadId,
|
|
1777
|
+
file: ticket.file,
|
|
1778
|
+
acl: ticket.acl,
|
|
1779
|
+
head
|
|
1780
|
+
});
|
|
1781
|
+
return { ...confirmed, uploadId };
|
|
642
1782
|
});
|
|
643
1783
|
}
|
|
644
|
-
async function
|
|
645
|
-
const { config, route: route2, stored, head, uploadId, acl } = input;
|
|
646
|
-
const rawMetadata = head.Metadata ?? {};
|
|
647
|
-
const fileName = resolveStoredFileName(head.ContentDisposition, key);
|
|
648
|
-
let context;
|
|
1784
|
+
async function listPartsOfOpenUpload(client, context) {
|
|
649
1785
|
try {
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
1786
|
+
return await listAllParts(client, context);
|
|
1787
|
+
} catch (error) {
|
|
1788
|
+
if (isS3ErrorCode(error, "OBJECT_NOT_FOUND")) return void 0;
|
|
1789
|
+
throw error;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
function completedParts(listed, layout, checksum) {
|
|
1793
|
+
const byNumber = new Map(listed.map((part) => [part.PartNumber, part]));
|
|
1794
|
+
const parts = [];
|
|
1795
|
+
for (let partNumber = 1; partNumber <= layout.partCount; partNumber++) {
|
|
1796
|
+
const part = byNumber.get(partNumber);
|
|
1797
|
+
if (!part?.ETag) throw s3Error("MULTIPART_PART_MISSING", { partNumber });
|
|
1798
|
+
const expected = partByteSize(layout, partNumber);
|
|
1799
|
+
if (part.Size !== expected) {
|
|
1800
|
+
throw s3Error("UPLOAD_SIZE_MISMATCH", {
|
|
1801
|
+
expected,
|
|
1802
|
+
actual: part.Size ?? 0
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
if (checksum && !part.ChecksumSHA256) throw s3Error("CHECKSUM_REQUIRED");
|
|
1806
|
+
parts.push({
|
|
1807
|
+
PartNumber: partNumber,
|
|
1808
|
+
ETag: part.ETag,
|
|
1809
|
+
ChecksumSHA256: part.ChecksumSHA256
|
|
655
1810
|
});
|
|
656
|
-
const metadata = stripPreviousKeyMeta(rawMetadata);
|
|
657
|
-
context = {
|
|
658
|
-
...stored,
|
|
659
|
-
contentType: head.ContentType,
|
|
660
|
-
contentLength,
|
|
661
|
-
eTag: head.ETag?.replaceAll('"', ""),
|
|
662
|
-
metadata,
|
|
663
|
-
acl,
|
|
664
|
-
fileName,
|
|
665
|
-
versionId: head.VersionId,
|
|
666
|
-
lastModified: head.LastModified?.toISOString(),
|
|
667
|
-
uploadId
|
|
668
|
-
};
|
|
669
|
-
await runLifecycleHook(route2.upload.onConfirmed, context, config);
|
|
670
|
-
} catch (err) {
|
|
671
|
-
await deleteObjectBestEffort(client, bucket, key);
|
|
672
|
-
throw err;
|
|
673
1811
|
}
|
|
674
|
-
|
|
675
|
-
|
|
1812
|
+
return parts;
|
|
1813
|
+
}
|
|
1814
|
+
function initMultipart(config, body, options = {}) {
|
|
1815
|
+
return runOperation(config, "multipart.init", options, async (call2) => {
|
|
1816
|
+
const target = await openUploadTarget(
|
|
1817
|
+
config,
|
|
1818
|
+
call2,
|
|
1819
|
+
{
|
|
1820
|
+
route: body.route,
|
|
1821
|
+
file: { name: body.name, size: body.size, type: body.type },
|
|
1822
|
+
clientMetadata: body.metadata
|
|
1823
|
+
},
|
|
1824
|
+
"multipart"
|
|
1825
|
+
);
|
|
1826
|
+
const { route: route2, context, object, uploadToken, ticketSubject } = target;
|
|
1827
|
+
const { multipart, checksum } = route2.upload;
|
|
1828
|
+
const layout = partLayout(body.size, multipart.partSize);
|
|
1829
|
+
const { UploadId: uploadId } = await route2.client.send(
|
|
1830
|
+
new CreateMultipartUploadCommand({
|
|
1831
|
+
Bucket: object.bucket,
|
|
1832
|
+
Key: object.key,
|
|
1833
|
+
ContentType: target.contentType,
|
|
1834
|
+
ContentDisposition: target.contentDisposition,
|
|
1835
|
+
ACL: object.acl,
|
|
1836
|
+
Metadata: target.s3Metadata,
|
|
1837
|
+
ChecksumAlgorithm: checksum ? "SHA256" : void 0,
|
|
1838
|
+
...objectCommandExtras(object)
|
|
1839
|
+
})
|
|
1840
|
+
);
|
|
1841
|
+
if (!uploadId) {
|
|
1842
|
+
throw new Error(
|
|
1843
|
+
`CreateMultipartUpload returned no UploadId for "${object.key}".`
|
|
1844
|
+
);
|
|
1845
|
+
}
|
|
676
1846
|
try {
|
|
677
|
-
const
|
|
678
|
-
|
|
679
|
-
|
|
1847
|
+
const { ticket, expiresAt } = await issueTicket(
|
|
1848
|
+
config,
|
|
1849
|
+
{
|
|
1850
|
+
mode: "multipart",
|
|
1851
|
+
id: uploadToken,
|
|
1852
|
+
route: route2.name,
|
|
1853
|
+
key: object.key,
|
|
1854
|
+
file: context.file,
|
|
1855
|
+
acl: object.acl,
|
|
1856
|
+
checksum,
|
|
1857
|
+
expiresAt: new Date(Date.now() + multipart.ticketExpiresIn * 1e3),
|
|
1858
|
+
uploadId,
|
|
1859
|
+
partSize: layout.partSize,
|
|
1860
|
+
partCount: layout.partCount
|
|
1861
|
+
},
|
|
1862
|
+
ticketSubject
|
|
1863
|
+
);
|
|
1864
|
+
await runLifecycle(route2.upload.onStart, {
|
|
1865
|
+
...context,
|
|
1866
|
+
uploadToken,
|
|
1867
|
+
uploadId,
|
|
1868
|
+
expiresAt
|
|
1869
|
+
});
|
|
1870
|
+
return {
|
|
1871
|
+
key: object.key,
|
|
1872
|
+
bucket: object.bucket,
|
|
1873
|
+
uploadId,
|
|
1874
|
+
ticket,
|
|
1875
|
+
partSize: layout.partSize,
|
|
1876
|
+
partCount: layout.partCount,
|
|
1877
|
+
checksum
|
|
1878
|
+
};
|
|
1879
|
+
} catch (error) {
|
|
1880
|
+
await abortMultipartBestEffort(
|
|
1881
|
+
route2.client,
|
|
1882
|
+
object.bucket,
|
|
1883
|
+
object.key,
|
|
1884
|
+
uploadId
|
|
1885
|
+
);
|
|
1886
|
+
throw error;
|
|
680
1887
|
}
|
|
681
|
-
}
|
|
682
|
-
return {
|
|
683
|
-
key,
|
|
684
|
-
bucket,
|
|
685
|
-
contentType: context.contentType,
|
|
686
|
-
contentLength: context.contentLength,
|
|
687
|
-
eTag: context.eTag,
|
|
688
|
-
metadata: context.metadata ?? {},
|
|
689
|
-
acl,
|
|
690
|
-
fileName,
|
|
691
|
-
versionId: context.versionId,
|
|
692
|
-
lastModified: context.lastModified
|
|
693
|
-
};
|
|
1888
|
+
});
|
|
694
1889
|
}
|
|
695
1890
|
|
|
696
|
-
// src/
|
|
697
|
-
function
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
metadata,
|
|
712
|
-
acl,
|
|
713
|
-
storageClass,
|
|
714
|
-
cacheControl,
|
|
715
|
-
tagging,
|
|
716
|
-
stored
|
|
717
|
-
} = await openUploadTarget(
|
|
718
|
-
config,
|
|
1891
|
+
// src/operations/multipart/list-parts.ts
|
|
1892
|
+
function listMultipartParts(config, body, options = {}) {
|
|
1893
|
+
return runOperation(config, "multipart.listParts", options, async (call2) => {
|
|
1894
|
+
const { route: route2, context } = await openMultipart(config, call2, body.ticket);
|
|
1895
|
+
await runGuards(route2.upload.multipart.guard, {
|
|
1896
|
+
...context,
|
|
1897
|
+
action: "list"
|
|
1898
|
+
});
|
|
1899
|
+
const parts = await listAllParts(route2.client, context);
|
|
1900
|
+
return { parts: parts.flatMap(toPartInfo) };
|
|
1901
|
+
});
|
|
1902
|
+
}
|
|
1903
|
+
function toPartInfo(part) {
|
|
1904
|
+
if (part.PartNumber === void 0 || part.PartNumber < 1) return [];
|
|
1905
|
+
return [
|
|
719
1906
|
{
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
1907
|
+
partNumber: part.PartNumber,
|
|
1908
|
+
size: part.Size ?? 0,
|
|
1909
|
+
eTag: part.ETag?.replaceAll('"', "") ?? "",
|
|
1910
|
+
...part.ChecksumSHA256 ? { checksum: part.ChecksumSHA256 } : {}
|
|
1911
|
+
}
|
|
1912
|
+
];
|
|
1913
|
+
}
|
|
1914
|
+
var checks = /* @__PURE__ */ new WeakMap();
|
|
1915
|
+
async function assertChecksumConfig(client) {
|
|
1916
|
+
let check = checks.get(client);
|
|
1917
|
+
if (!check) {
|
|
1918
|
+
check = readChecksumConfig(client);
|
|
1919
|
+
checks.set(client, check);
|
|
1920
|
+
}
|
|
1921
|
+
const result = await check;
|
|
1922
|
+
if (result.ok) return;
|
|
1923
|
+
if (!result.reported) {
|
|
1924
|
+
result.reported = true;
|
|
1925
|
+
throw result.error;
|
|
1926
|
+
}
|
|
1927
|
+
throw s3Error("INTERNAL_ERROR", void 0, { cause: result.error });
|
|
1928
|
+
}
|
|
1929
|
+
async function readChecksumConfig(client) {
|
|
1930
|
+
const option = client.config.requestChecksumCalculation;
|
|
1931
|
+
const value = typeof option === "function" ? await option() : option;
|
|
1932
|
+
if (value === void 0 || value === "WHEN_REQUIRED") return { ok: true };
|
|
1933
|
+
const shown = typeof value === "string" ? value : typeof value;
|
|
1934
|
+
return {
|
|
1935
|
+
ok: false,
|
|
1936
|
+
reported: false,
|
|
1937
|
+
error: new ConfigError(
|
|
1938
|
+
`The S3Client has requestChecksumCalculation "${shown}", which breaks presigned uploads. Create it with requestChecksumCalculation: "WHEN_REQUIRED".`
|
|
1939
|
+
)
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
// src/helpers/presign.ts
|
|
1944
|
+
var SIGNABLE_HEADERS = /* @__PURE__ */ new Set([
|
|
1945
|
+
"content-type",
|
|
1946
|
+
"content-disposition",
|
|
1947
|
+
"cache-control",
|
|
1948
|
+
"content-length"
|
|
1949
|
+
]);
|
|
1950
|
+
var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set([
|
|
1951
|
+
"content-length",
|
|
1952
|
+
"host"
|
|
1953
|
+
]);
|
|
1954
|
+
function presignPut(client, input) {
|
|
1955
|
+
const objectS3 = {
|
|
1956
|
+
storageClass: input.storageClass,
|
|
1957
|
+
cacheControl: input.cacheControl,
|
|
1958
|
+
tagging: input.tagging
|
|
1959
|
+
};
|
|
1960
|
+
const headers = {
|
|
1961
|
+
"content-type": input.contentType,
|
|
1962
|
+
"content-disposition": input.contentDisposition,
|
|
1963
|
+
...lowercaseKeys(objectPutHeaders(objectS3)),
|
|
1964
|
+
"x-amz-acl": input.acl,
|
|
1965
|
+
...Object.fromEntries(
|
|
1966
|
+
Object.entries(input.metadata).map(([key, value]) => [
|
|
1967
|
+
`x-amz-meta-${key.toLowerCase()}`,
|
|
1968
|
+
value
|
|
1969
|
+
])
|
|
1970
|
+
),
|
|
1971
|
+
...input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {}
|
|
1972
|
+
};
|
|
1973
|
+
const command = new PutObjectCommand({
|
|
1974
|
+
Bucket: input.bucket,
|
|
1975
|
+
Key: input.key,
|
|
1976
|
+
ContentLength: input.size,
|
|
1977
|
+
ContentType: input.contentType,
|
|
1978
|
+
ContentDisposition: input.contentDisposition,
|
|
1979
|
+
ACL: input.acl,
|
|
1980
|
+
Metadata: input.metadata,
|
|
1981
|
+
...objectCommandExtras(objectS3),
|
|
1982
|
+
...input.checksum ? { ChecksumSHA256: input.checksum } : {}
|
|
742
1983
|
});
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
Body: body,
|
|
748
|
-
ContentType: input.contentType,
|
|
749
|
-
ContentLength: fileSize,
|
|
750
|
-
Metadata: metadata,
|
|
751
|
-
ACL: acl,
|
|
752
|
-
...objectCommandExtras(objectS3)
|
|
753
|
-
})
|
|
1984
|
+
return presign(
|
|
1985
|
+
client,
|
|
1986
|
+
headers,
|
|
1987
|
+
(options) => getSignedUrl(client, command, { ...options, expiresIn: input.expiresIn })
|
|
754
1988
|
);
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
1989
|
+
}
|
|
1990
|
+
function presignUploadPart(client, input) {
|
|
1991
|
+
const command = new UploadPartCommand({
|
|
1992
|
+
Bucket: input.bucket,
|
|
1993
|
+
Key: input.key,
|
|
1994
|
+
UploadId: input.uploadId,
|
|
1995
|
+
PartNumber: input.partNumber,
|
|
1996
|
+
ContentLength: input.size,
|
|
1997
|
+
...input.checksum ? { ChecksumSHA256: input.checksum } : {}
|
|
762
1998
|
});
|
|
1999
|
+
const headers = input.checksum ? { "x-amz-checksum-sha256": input.checksum } : {};
|
|
2000
|
+
return presign(
|
|
2001
|
+
client,
|
|
2002
|
+
headers,
|
|
2003
|
+
(options) => getSignedUrl(client, command, { ...options, expiresIn: input.expiresIn })
|
|
2004
|
+
);
|
|
763
2005
|
}
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
2006
|
+
async function presign(client, headers, sign) {
|
|
2007
|
+
await assertChecksumConfig(client);
|
|
2008
|
+
const names = Object.keys(headers);
|
|
2009
|
+
const url = await sign({
|
|
2010
|
+
// Only headers we send; UploadPart would otherwise sign the SDK's
|
|
2011
|
+
// default content-type.
|
|
2012
|
+
signableHeaders: new Set(
|
|
2013
|
+
[...names, "content-length"].filter((name) => SIGNABLE_HEADERS.has(name))
|
|
2014
|
+
),
|
|
2015
|
+
unhoistableHeaders: new Set(
|
|
2016
|
+
names.filter((name) => name.startsWith("x-amz-"))
|
|
2017
|
+
)
|
|
2018
|
+
});
|
|
2019
|
+
const signed = new URL(url).searchParams.get("X-Amz-SignedHeaders") ?? "";
|
|
2020
|
+
const expected = [...names, ...FORBIDDEN_HEADERS].sort().join(";");
|
|
2021
|
+
if (signed !== expected) {
|
|
767
2022
|
throw new Error(
|
|
768
|
-
|
|
2023
|
+
`The presigned URL signs "${signed}"; expected "${expected}".`
|
|
769
2024
|
);
|
|
770
2025
|
}
|
|
771
|
-
|
|
772
|
-
await runHook(injected.config.guard, { request });
|
|
773
|
-
return {
|
|
774
|
-
config: injected.config,
|
|
775
|
-
request
|
|
776
|
-
};
|
|
777
|
-
});
|
|
778
|
-
var createS3Middleware = createMiddleware.create({
|
|
779
|
-
use: [s3ContextMiddleware]
|
|
780
|
-
});
|
|
781
|
-
|
|
782
|
-
// src/api/create-s3-endpoint.ts
|
|
783
|
-
var createEndpointWithContext = createEndpoint.create({
|
|
784
|
-
use: [s3ContextMiddleware]
|
|
785
|
-
});
|
|
786
|
-
function compileIfZod(schema) {
|
|
787
|
-
if (schema !== null && typeof schema === "object" && "_zod" in schema) {
|
|
788
|
-
return z.compile(schema);
|
|
789
|
-
}
|
|
790
|
-
return schema;
|
|
2026
|
+
return { url, headers };
|
|
791
2027
|
}
|
|
792
|
-
function
|
|
793
|
-
return
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
2028
|
+
function lowercaseKeys(record) {
|
|
2029
|
+
return Object.fromEntries(
|
|
2030
|
+
Object.entries(record).map(([key, value]) => [key.toLowerCase(), value])
|
|
2031
|
+
);
|
|
2032
|
+
}
|
|
2033
|
+
var POST_FILENAME_PLACEHOLDER = "${filename}";
|
|
2034
|
+
async function presignPost(client, input) {
|
|
2035
|
+
if (input.key.includes(POST_FILENAME_PLACEHOLDER)) {
|
|
2036
|
+
throw s3Error("KEY_INVALID");
|
|
2037
|
+
}
|
|
2038
|
+
await assertChecksumConfig(client);
|
|
2039
|
+
const checksumFields = input.checksum ? {
|
|
2040
|
+
"x-amz-checksum-algorithm": "SHA256",
|
|
2041
|
+
"x-amz-checksum-sha256": input.checksum
|
|
2042
|
+
} : {};
|
|
2043
|
+
const conditions = [
|
|
2044
|
+
["eq", "$key", input.key],
|
|
2045
|
+
["content-length-range", input.size, input.size],
|
|
2046
|
+
...Object.entries(checksumFields).map(([name, value]) => [
|
|
2047
|
+
"eq",
|
|
2048
|
+
`$${name}`,
|
|
2049
|
+
value
|
|
2050
|
+
])
|
|
2051
|
+
];
|
|
2052
|
+
return createPresignedPost(client, {
|
|
2053
|
+
Bucket: input.bucket,
|
|
2054
|
+
Key: input.key,
|
|
2055
|
+
Conditions: conditions,
|
|
2056
|
+
Fields: {
|
|
2057
|
+
acl: input.acl,
|
|
2058
|
+
"Content-Type": input.contentType,
|
|
2059
|
+
"Content-Disposition": input.contentDisposition,
|
|
2060
|
+
...objectPutHeaders(input),
|
|
2061
|
+
...Object.fromEntries(
|
|
2062
|
+
Object.entries(input.metadata).map(([key, value]) => [
|
|
2063
|
+
`x-amz-meta-${key}`,
|
|
2064
|
+
value
|
|
2065
|
+
])
|
|
2066
|
+
),
|
|
2067
|
+
...checksumFields
|
|
2068
|
+
},
|
|
2069
|
+
Expires: input.expiresIn
|
|
2070
|
+
});
|
|
804
2071
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
2072
|
+
|
|
2073
|
+
// src/operations/multipart/sign-part.ts
|
|
2074
|
+
function signMultipartPart(config, body, options = {}) {
|
|
2075
|
+
return runOperation(config, "multipart.signPart", options, async (call2) => {
|
|
2076
|
+
const { route: route2, ticket, context, layout } = await openMultipart(
|
|
2077
|
+
config,
|
|
2078
|
+
call2,
|
|
2079
|
+
body.ticket
|
|
811
2080
|
);
|
|
2081
|
+
const { partNumber } = body;
|
|
2082
|
+
assertPartNumber(layout, partNumber);
|
|
2083
|
+
if (ticket.checksum && !body.checksum) {
|
|
2084
|
+
throw s3Error("CHECKSUM_REQUIRED");
|
|
2085
|
+
}
|
|
2086
|
+
const size = partByteSize(layout, partNumber);
|
|
2087
|
+
await runGuards(route2.upload.multipart.guard, {
|
|
2088
|
+
...context,
|
|
2089
|
+
action: "part",
|
|
2090
|
+
partNumber,
|
|
2091
|
+
size
|
|
2092
|
+
});
|
|
2093
|
+
const { expiresIn } = route2.upload;
|
|
2094
|
+
const { url, headers } = await presignUploadPart(route2.client, {
|
|
2095
|
+
bucket: context.bucket,
|
|
2096
|
+
key: context.key,
|
|
2097
|
+
uploadId: context.uploadId,
|
|
2098
|
+
partNumber,
|
|
2099
|
+
size,
|
|
2100
|
+
checksum: ticket.checksum ? body.checksum : void 0,
|
|
2101
|
+
expiresIn
|
|
2102
|
+
});
|
|
2103
|
+
return { url, partNumber, size, expiresIn, headers };
|
|
2104
|
+
});
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
// src/config/feature-toggle.ts
|
|
2108
|
+
function isEnabled(feature) {
|
|
2109
|
+
return feature.enabled;
|
|
2110
|
+
}
|
|
2111
|
+
function isFeatureOn(value, defaultOn = false) {
|
|
2112
|
+
if (value === void 0) return defaultOn;
|
|
2113
|
+
if (typeof value === "boolean") return value;
|
|
2114
|
+
if ("enabled" in value && typeof value.enabled === "boolean") {
|
|
2115
|
+
return value.enabled;
|
|
812
2116
|
}
|
|
813
|
-
return
|
|
814
|
-
|
|
815
|
-
optionsOrHandler
|
|
816
|
-
);
|
|
817
|
-
});
|
|
2117
|
+
return true;
|
|
2118
|
+
}
|
|
818
2119
|
|
|
819
|
-
// src/
|
|
820
|
-
|
|
821
|
-
|
|
2120
|
+
// src/operations/catalog.ts
|
|
2121
|
+
function getRouteCatalog(config, options = {}) {
|
|
2122
|
+
return runOperation(
|
|
822
2123
|
config,
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
2124
|
+
"catalog",
|
|
2125
|
+
options,
|
|
2126
|
+
async () => buildRouteCatalog(config)
|
|
826
2127
|
);
|
|
827
|
-
await runHook(route2.upload.confirmGuard, {
|
|
828
|
-
...stored,
|
|
829
|
-
replace: route2.upload.replace
|
|
830
|
-
});
|
|
831
|
-
const head = await headObjectOrNotFound(route2.client, bucket, key);
|
|
832
|
-
return finalizeConfirmedObject(route2.client, bucket, key, {
|
|
833
|
-
config,
|
|
834
|
-
route: route2,
|
|
835
|
-
stored,
|
|
836
|
-
head,
|
|
837
|
-
acl: route2.upload.acl
|
|
838
|
-
});
|
|
839
2128
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
return handleConfirm(ctx.context.config, ctx.body, ctx.context.request);
|
|
2129
|
+
function buildRouteCatalog(config) {
|
|
2130
|
+
const routes = {};
|
|
2131
|
+
for (const [name, route2] of Object.entries(config.routes)) {
|
|
2132
|
+
routes[name] = catalogEntry(route2);
|
|
845
2133
|
}
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
2134
|
+
return { routes };
|
|
2135
|
+
}
|
|
2136
|
+
function catalogEntry(route2) {
|
|
2137
|
+
const { upload, download } = route2;
|
|
2138
|
+
return {
|
|
2139
|
+
upload: isEnabled(upload) ? {
|
|
2140
|
+
enabled: true,
|
|
2141
|
+
...upload.fileTypes ? { fileTypes: [...upload.fileTypes] } : {},
|
|
2142
|
+
...upload.maxFileSize !== void 0 ? { maxFileSize: upload.maxFileSize } : {},
|
|
2143
|
+
multipart: upload.multipart.enabled,
|
|
2144
|
+
checksum: upload.checksum
|
|
2145
|
+
} : { enabled: false },
|
|
2146
|
+
download: isEnabled(download) ? {
|
|
2147
|
+
enabled: true,
|
|
2148
|
+
maxExpiresIn: download.maxExpiresIn,
|
|
2149
|
+
disposition: download.disposition,
|
|
2150
|
+
inlineTypes: download.inlineTypes ? [...download.inlineTypes] : [],
|
|
2151
|
+
mode: download.mode
|
|
2152
|
+
} : { enabled: false },
|
|
2153
|
+
delete: { enabled: route2.delete.enabled }
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
// src/operations/upload.ts
|
|
2158
|
+
function presignUpload(config, body, options = {}) {
|
|
2159
|
+
return runOperation(config, "upload", options, async (call2) => {
|
|
2160
|
+
const kind = presignKind(getRoute(config, body.route));
|
|
2161
|
+
const target = await openUploadTarget(
|
|
2162
|
+
config,
|
|
2163
|
+
call2,
|
|
2164
|
+
{
|
|
2165
|
+
route: body.route,
|
|
2166
|
+
file: { name: body.name, size: body.size, type: body.type },
|
|
2167
|
+
clientMetadata: body.metadata,
|
|
2168
|
+
checksum: body.checksum
|
|
2169
|
+
},
|
|
2170
|
+
kind
|
|
859
2171
|
);
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
{
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
2172
|
+
const { route: route2, context, object, uploadToken, ticketSubject } = target;
|
|
2173
|
+
const { expiresIn, confirmWindow } = route2.upload;
|
|
2174
|
+
const urlExpiresAt = new Date(Date.now() + expiresIn * 1e3);
|
|
2175
|
+
const { ticket, expiresAt } = await issueTicket(
|
|
2176
|
+
config,
|
|
2177
|
+
{
|
|
2178
|
+
mode: "single",
|
|
2179
|
+
kind,
|
|
2180
|
+
id: uploadToken,
|
|
2181
|
+
route: route2.name,
|
|
2182
|
+
key: object.key,
|
|
2183
|
+
file: context.file,
|
|
2184
|
+
acl: object.acl,
|
|
2185
|
+
checksum: route2.upload.checksum,
|
|
2186
|
+
expiresAt: new Date(urlExpiresAt.getTime() + confirmWindow * 1e3)
|
|
2187
|
+
},
|
|
2188
|
+
ticketSubject
|
|
876
2189
|
);
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
);
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
if (query.fileName) url.searchParams.set("fileName", query.fileName);
|
|
913
|
-
if (query.disposition) url.searchParams.set("disposition", query.disposition);
|
|
914
|
-
return url.toString();
|
|
915
|
-
}
|
|
916
|
-
async function handleDownload(config, input, request) {
|
|
917
|
-
const { route: route2, key, bucket, stored } = await openStoredTarget(
|
|
918
|
-
config,
|
|
919
|
-
input,
|
|
920
|
-
request,
|
|
921
|
-
"download"
|
|
922
|
-
);
|
|
923
|
-
const resolved = await route2.download.resolve?.({
|
|
924
|
-
...stored,
|
|
925
|
-
fileName: input.fileName,
|
|
926
|
-
disposition: input.disposition ?? route2.download.disposition,
|
|
927
|
-
expiresIn: input.expiresIn
|
|
928
|
-
});
|
|
929
|
-
const fileName = resolved?.fileName ?? input.fileName;
|
|
930
|
-
const disposition = resolved?.disposition ?? input.disposition ?? route2.download.disposition ?? "attachment";
|
|
931
|
-
const mode = route2.download.mode ?? "presign";
|
|
932
|
-
const expiresIn = mode === "proxy" ? 0 : clampDownloadExpiresIn({
|
|
933
|
-
requested: input.expiresIn,
|
|
934
|
-
routeExpiresIn: route2.download.expiresIn,
|
|
935
|
-
resolvedExpiresIn: resolved?.expiresIn,
|
|
936
|
-
maxExpiresIn: config.maxExpiresIn
|
|
937
|
-
});
|
|
938
|
-
await runHook(route2.download.guard, {
|
|
939
|
-
...stored,
|
|
940
|
-
fileName,
|
|
941
|
-
disposition,
|
|
942
|
-
expiresIn
|
|
943
|
-
});
|
|
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
|
|
2190
|
+
const input = {
|
|
2191
|
+
bucket: object.bucket,
|
|
2192
|
+
key: object.key,
|
|
2193
|
+
size: body.size,
|
|
2194
|
+
contentType: target.contentType,
|
|
2195
|
+
contentDisposition: target.contentDisposition,
|
|
2196
|
+
metadata: target.s3Metadata,
|
|
2197
|
+
acl: object.acl,
|
|
2198
|
+
checksum: body.checksum,
|
|
2199
|
+
expiresIn,
|
|
2200
|
+
storageClass: object.storageClass,
|
|
2201
|
+
cacheControl: object.cacheControl,
|
|
2202
|
+
tagging: object.tagging
|
|
2203
|
+
};
|
|
2204
|
+
const presigned = kind === "post" ? {
|
|
2205
|
+
method: "POST",
|
|
2206
|
+
...await presignPost(route2.client, input)
|
|
2207
|
+
} : {
|
|
2208
|
+
method: "PUT",
|
|
2209
|
+
...await presignPut(route2.client, input)
|
|
2210
|
+
};
|
|
2211
|
+
await runLifecycle(route2.upload.onStart, {
|
|
2212
|
+
...context,
|
|
2213
|
+
uploadToken,
|
|
2214
|
+
url: presigned.url,
|
|
2215
|
+
urlExpiresAt,
|
|
2216
|
+
expiresAt
|
|
2217
|
+
});
|
|
2218
|
+
return {
|
|
2219
|
+
key: object.key,
|
|
2220
|
+
bucket: object.bucket,
|
|
2221
|
+
expiresIn,
|
|
2222
|
+
ticket,
|
|
2223
|
+
...presigned
|
|
2224
|
+
};
|
|
950
2225
|
});
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
route2.client,
|
|
959
|
-
new GetObjectCommand({
|
|
960
|
-
Bucket: bucket,
|
|
961
|
-
Key: key,
|
|
962
|
-
ResponseContentDisposition: contentDisposition,
|
|
963
|
-
ResponseContentType: signed.contentType
|
|
964
|
-
}),
|
|
965
|
-
{ expiresIn }
|
|
966
|
-
);
|
|
967
|
-
await runLifecycleHook(
|
|
968
|
-
route2.download.onPresigned,
|
|
969
|
-
{
|
|
970
|
-
...stored,
|
|
971
|
-
fileName,
|
|
972
|
-
url,
|
|
973
|
-
expiresIn
|
|
974
|
-
},
|
|
975
|
-
config
|
|
976
|
-
);
|
|
2226
|
+
}
|
|
2227
|
+
function presignKind(route2) {
|
|
2228
|
+
return route2.upload.enabled && route2.upload.method === "POST" ? "post" : "put";
|
|
2229
|
+
}
|
|
2230
|
+
|
|
2231
|
+
// src/api/core-endpoints.ts
|
|
2232
|
+
function call(context) {
|
|
977
2233
|
return {
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
url,
|
|
981
|
-
expiresIn,
|
|
982
|
-
mode,
|
|
983
|
-
disposition: signed.disposition
|
|
2234
|
+
config: resolvedConfig(context),
|
|
2235
|
+
options: { request: context.request }
|
|
984
2236
|
};
|
|
985
2237
|
}
|
|
986
|
-
var
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
2238
|
+
var coreEndpoints = {
|
|
2239
|
+
upload: createCoreEndpoint(
|
|
2240
|
+
S3_API_ROUTES.upload,
|
|
2241
|
+
{ method: "POST", body: uploadBodySchema },
|
|
2242
|
+
(ctx) => {
|
|
2243
|
+
const { config, options } = call(ctx.context);
|
|
2244
|
+
return presignUpload(config, ctx.body, options);
|
|
2245
|
+
}
|
|
2246
|
+
),
|
|
2247
|
+
confirm: createCoreEndpoint(
|
|
2248
|
+
S3_API_ROUTES.confirm,
|
|
2249
|
+
{ method: "POST", body: ticketBodySchema },
|
|
2250
|
+
(ctx) => {
|
|
2251
|
+
const { config, options } = call(ctx.context);
|
|
2252
|
+
return confirmUpload(config, ctx.body, options);
|
|
2253
|
+
}
|
|
2254
|
+
),
|
|
2255
|
+
download: createCoreEndpoint(
|
|
2256
|
+
S3_API_ROUTES.download,
|
|
2257
|
+
{ method: "GET", query: downloadQuerySchema },
|
|
2258
|
+
(ctx) => {
|
|
2259
|
+
const { config, options } = call(ctx.context);
|
|
2260
|
+
return presignDownload(config, ctx.query, options);
|
|
2261
|
+
}
|
|
2262
|
+
),
|
|
2263
|
+
catalog: createCoreEndpoint(
|
|
2264
|
+
S3_API_ROUTES.catalog,
|
|
2265
|
+
{ method: "GET" },
|
|
2266
|
+
(ctx) => {
|
|
2267
|
+
const { config, options } = call(ctx.context);
|
|
2268
|
+
return getRouteCatalog(config, options);
|
|
2269
|
+
}
|
|
2270
|
+
),
|
|
2271
|
+
file: createCoreEndpoint(
|
|
2272
|
+
S3_API_ROUTES.file,
|
|
2273
|
+
{
|
|
2274
|
+
method: "GET",
|
|
2275
|
+
query: fileQuerySchema,
|
|
2276
|
+
// A bad query fails before serveFile runs; it gets the /file headers too.
|
|
2277
|
+
onValidationError: ({ message }) => {
|
|
2278
|
+
throw withFileHeaders(s3Error("VALIDATION_ERROR", { issue: message }));
|
|
2279
|
+
}
|
|
2280
|
+
},
|
|
2281
|
+
(ctx) => {
|
|
2282
|
+
const { config, options } = call(ctx.context);
|
|
2283
|
+
return serveFile(config, ctx.query, options);
|
|
2284
|
+
}
|
|
2285
|
+
),
|
|
2286
|
+
delete: createCoreEndpoint(
|
|
2287
|
+
S3_API_ROUTES.delete,
|
|
2288
|
+
{ method: "DELETE", query: deleteQuerySchema },
|
|
2289
|
+
(ctx) => {
|
|
2290
|
+
const { config, options } = call(ctx.context);
|
|
2291
|
+
return deleteStoredObject(config, ctx.query, options);
|
|
2292
|
+
}
|
|
2293
|
+
),
|
|
2294
|
+
deleteMany: createCoreEndpoint(
|
|
2295
|
+
S3_API_ROUTES.deleteMany,
|
|
2296
|
+
{ method: "POST", body: deleteManyBodySchema },
|
|
2297
|
+
(ctx) => {
|
|
2298
|
+
const { config, options } = call(ctx.context);
|
|
2299
|
+
return deleteManyObjects(config, ctx.body, options);
|
|
2300
|
+
}
|
|
2301
|
+
),
|
|
2302
|
+
multipartInit: createCoreEndpoint(
|
|
2303
|
+
S3_API_ROUTES.multipartInit,
|
|
2304
|
+
{ method: "POST", body: multipartInitBodySchema },
|
|
2305
|
+
async (ctx) => {
|
|
2306
|
+
const { config, options } = call(ctx.context);
|
|
2307
|
+
const result = await initMultipart(config, ctx.body, options);
|
|
2308
|
+
ctx.setStatus(201);
|
|
2309
|
+
return result;
|
|
2310
|
+
}
|
|
2311
|
+
),
|
|
2312
|
+
multipartSignPart: createCoreEndpoint(
|
|
2313
|
+
S3_API_ROUTES.multipartSignPart,
|
|
2314
|
+
{ method: "POST", body: multipartSignPartBodySchema },
|
|
2315
|
+
(ctx) => {
|
|
2316
|
+
const { config, options } = call(ctx.context);
|
|
2317
|
+
return signMultipartPart(config, ctx.body, options);
|
|
2318
|
+
}
|
|
2319
|
+
),
|
|
2320
|
+
multipartListParts: createCoreEndpoint(
|
|
2321
|
+
S3_API_ROUTES.multipartListParts,
|
|
2322
|
+
{ method: "POST", body: ticketBodySchema },
|
|
2323
|
+
(ctx) => {
|
|
2324
|
+
const { config, options } = call(ctx.context);
|
|
2325
|
+
return listMultipartParts(config, ctx.body, options);
|
|
2326
|
+
}
|
|
2327
|
+
),
|
|
2328
|
+
multipartComplete: createCoreEndpoint(
|
|
2329
|
+
S3_API_ROUTES.multipartComplete,
|
|
2330
|
+
{ method: "POST", body: ticketBodySchema },
|
|
2331
|
+
(ctx) => {
|
|
2332
|
+
const { config, options } = call(ctx.context);
|
|
2333
|
+
return completeMultipart(config, ctx.body, options);
|
|
2334
|
+
}
|
|
2335
|
+
),
|
|
2336
|
+
multipartAbort: createCoreEndpoint(
|
|
2337
|
+
S3_API_ROUTES.multipartAbort,
|
|
2338
|
+
{ method: "POST", body: ticketBodySchema },
|
|
2339
|
+
(ctx) => {
|
|
2340
|
+
const { config, options } = call(ctx.context);
|
|
2341
|
+
return abortMultipart(config, ctx.body, options);
|
|
2342
|
+
}
|
|
2343
|
+
)
|
|
2344
|
+
};
|
|
2345
|
+
|
|
2346
|
+
// src/helpers/logger.ts
|
|
2347
|
+
var defaultLogger = {
|
|
2348
|
+
error: (message, ...args) => {
|
|
2349
|
+
console.error("[dimah-s3]", message, ...args);
|
|
2350
|
+
},
|
|
2351
|
+
warn: (message, ...args) => {
|
|
2352
|
+
console.warn("[dimah-s3]", message, ...args);
|
|
991
2353
|
}
|
|
992
|
-
|
|
993
|
-
function
|
|
994
|
-
if (
|
|
995
|
-
return
|
|
996
|
-
|
|
997
|
-
fileTypes: upload2.fileTypes,
|
|
998
|
-
maxFileSize: upload2.maxFileSize,
|
|
999
|
-
multipart: isEnabled(upload2.multipart),
|
|
1000
|
-
...upload2.checksum ? { checksum: true } : {},
|
|
1001
|
-
...upload2.replace ? { replace: upload2.replace } : {}
|
|
1002
|
-
};
|
|
2354
|
+
};
|
|
2355
|
+
function resolveLogger(logger) {
|
|
2356
|
+
if (logger === false) return {};
|
|
2357
|
+
if (!logger) return defaultLogger;
|
|
2358
|
+
return logger;
|
|
1003
2359
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
};
|
|
2360
|
+
var DEFAULT_CONFIRM_WINDOW = 3600;
|
|
2361
|
+
var DEFAULT_MULTIPART_TICKET_EXPIRES_IN = 86400;
|
|
2362
|
+
function hookList(plugins, pick, user) {
|
|
2363
|
+
const list = [];
|
|
2364
|
+
for (const plugin of plugins) {
|
|
2365
|
+
const hook = pick(plugin);
|
|
2366
|
+
if (hook !== void 0) list.push({ plugin: plugin.id, hook });
|
|
2367
|
+
}
|
|
2368
|
+
if (user !== void 0) list.push({ hook: user });
|
|
2369
|
+
return list;
|
|
1013
2370
|
}
|
|
1014
|
-
function
|
|
2371
|
+
function resolveRoutes(config, options) {
|
|
2372
|
+
const entries = Object.entries(config.routes);
|
|
2373
|
+
if (entries.length === 0) {
|
|
2374
|
+
throw new Error("dimahS3: at least one route is required.");
|
|
2375
|
+
}
|
|
1015
2376
|
const routes = {};
|
|
1016
|
-
for (const [name, route2] of
|
|
1017
|
-
routes[name] =
|
|
1018
|
-
upload: catalogUpload(route2.upload),
|
|
1019
|
-
download: catalogDownload(route2.download, config.maxExpiresIn),
|
|
1020
|
-
delete: isEnabled(route2.delete) ? { enabled: true } : { enabled: false }
|
|
1021
|
-
};
|
|
2377
|
+
for (const [name, route2] of entries) {
|
|
2378
|
+
routes[name] = resolveRoute(name, route2, config, options);
|
|
1022
2379
|
}
|
|
1023
|
-
|
|
2380
|
+
assertDistinctKeyPrefixes(routes, options.logger);
|
|
2381
|
+
return routes;
|
|
1024
2382
|
}
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
2383
|
+
function resolveRoute(name, route2, instance, options) {
|
|
2384
|
+
assertRouteName(name);
|
|
2385
|
+
const client = route2.client ?? instance.client;
|
|
2386
|
+
const bucket = route2.bucket ?? instance.bucket;
|
|
2387
|
+
if (!client) {
|
|
2388
|
+
throw routeError(name, "set client on the route or on dimahS3().");
|
|
1030
2389
|
}
|
|
1031
|
-
)
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
const resolved = await route2.download.resolve?.({
|
|
1040
|
-
...stored,
|
|
1041
|
-
fileName: input.fileName,
|
|
1042
|
-
disposition: input.disposition ?? route2.download.disposition
|
|
1043
|
-
});
|
|
1044
|
-
const fileName = resolved?.fileName ?? input.fileName;
|
|
1045
|
-
const disposition = resolved?.disposition ?? input.disposition ?? route2.download.disposition ?? "attachment";
|
|
1046
|
-
await runHook(route2.download.guard, {
|
|
1047
|
-
...stored,
|
|
1048
|
-
fileName,
|
|
1049
|
-
disposition
|
|
1050
|
-
});
|
|
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;
|
|
1059
|
-
const result = await sendOrObjectNotFound(
|
|
1060
|
-
() => route2.client.send(
|
|
1061
|
-
new GetObjectCommand({
|
|
1062
|
-
Bucket: bucket,
|
|
1063
|
-
Key: key,
|
|
1064
|
-
ResponseContentDisposition: contentDisposition
|
|
1065
|
-
})
|
|
2390
|
+
if (!bucket) {
|
|
2391
|
+
throw routeError(name, "set bucket on the route or on dimahS3().");
|
|
2392
|
+
}
|
|
2393
|
+
const env = {
|
|
2394
|
+
name,
|
|
2395
|
+
maxExpiresIn: options.maxExpiresIn,
|
|
2396
|
+
plugins: (options.plugins ?? []).filter(
|
|
2397
|
+
(plugin) => route2.plugins?.[plugin.id] !== false
|
|
1066
2398
|
)
|
|
1067
|
-
|
|
1068
|
-
const
|
|
1069
|
-
|
|
1070
|
-
const
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
"Content-Type",
|
|
1074
|
-
signed.contentType ?? result.ContentType ?? "application/octet-stream"
|
|
1075
|
-
);
|
|
1076
|
-
headers.set("Content-Disposition", contentDisposition);
|
|
1077
|
-
headers.set("X-Content-Type-Options", "nosniff");
|
|
1078
|
-
if (typeof result.ContentLength === "number") {
|
|
1079
|
-
headers.set("Content-Length", String(result.ContentLength));
|
|
2399
|
+
};
|
|
2400
|
+
const upload = resolveUpload(route2.upload, env);
|
|
2401
|
+
const download = resolveDownload(route2.download, env);
|
|
2402
|
+
const deleteFeature = resolveDelete(route2.delete, env);
|
|
2403
|
+
if (!upload.enabled && !download.enabled && !deleteFeature.enabled) {
|
|
2404
|
+
throw routeError(name, "enable upload, download, or delete.");
|
|
1080
2405
|
}
|
|
1081
|
-
return
|
|
2406
|
+
return {
|
|
2407
|
+
name,
|
|
2408
|
+
client,
|
|
2409
|
+
bucket,
|
|
2410
|
+
keyPrefix: resolveKeyPrefix(name, route2.keyPrefix),
|
|
2411
|
+
plugins: route2.plugins ?? {},
|
|
2412
|
+
// Plugins add instance guards, not route guards.
|
|
2413
|
+
guard: route2.guard ? [{ hook: route2.guard }] : [],
|
|
2414
|
+
upload,
|
|
2415
|
+
download,
|
|
2416
|
+
delete: deleteFeature
|
|
2417
|
+
};
|
|
1082
2418
|
}
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
async (ctx) => {
|
|
1087
|
-
return handleFile(ctx.context.config, ctx.query, ctx.context.request);
|
|
2419
|
+
function resolveUpload(toggle, env) {
|
|
2420
|
+
if (!isFeatureOn(toggle)) {
|
|
2421
|
+
return { enabled: false, multipart: { enabled: false } };
|
|
1088
2422
|
}
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
);
|
|
1097
|
-
const
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
2423
|
+
const upload = toggle === true || !toggle ? {} : toggle;
|
|
2424
|
+
if (upload.verifyContent && upload.checksum === false) {
|
|
2425
|
+
throw routeError(
|
|
2426
|
+
env.name,
|
|
2427
|
+
"upload.checksum cannot be false while upload.verifyContent is on."
|
|
2428
|
+
);
|
|
2429
|
+
}
|
|
2430
|
+
const hooks = (plugin) => plugin.hooks?.upload;
|
|
2431
|
+
const resolved = {
|
|
2432
|
+
enabled: true,
|
|
2433
|
+
fileTypes: upload.fileTypes,
|
|
2434
|
+
maxFileSize: routePositiveInteger(
|
|
2435
|
+
env,
|
|
2436
|
+
"upload.maxFileSize",
|
|
2437
|
+
upload.maxFileSize
|
|
2438
|
+
),
|
|
2439
|
+
object: upload.object,
|
|
2440
|
+
acl: upload.acl ?? "private",
|
|
2441
|
+
method: upload.method ?? "PUT",
|
|
2442
|
+
expiresIn: Math.min(
|
|
2443
|
+
positiveInteger(
|
|
2444
|
+
env,
|
|
2445
|
+
"upload.expiresIn",
|
|
2446
|
+
upload.expiresIn,
|
|
2447
|
+
S3_DEFAULT_EXPIRES_IN
|
|
2448
|
+
),
|
|
2449
|
+
env.maxExpiresIn
|
|
2450
|
+
),
|
|
2451
|
+
confirmWindow: positiveInteger(
|
|
2452
|
+
env,
|
|
2453
|
+
"upload.confirmWindow",
|
|
2454
|
+
upload.confirmWindow,
|
|
2455
|
+
DEFAULT_CONFIRM_WINDOW
|
|
2456
|
+
),
|
|
2457
|
+
checksum: upload.checksum === true || upload.verifyContent === true,
|
|
2458
|
+
verifyContent: upload.verifyContent === true,
|
|
2459
|
+
multipart: resolveMultipart(upload.multipart, env),
|
|
2460
|
+
guard: hookList(env.plugins, (p) => hooks(p)?.guard, upload.guard),
|
|
2461
|
+
onStart: hookList(env.plugins, (p) => hooks(p)?.onStart, upload.onStart),
|
|
2462
|
+
confirmGuard: hookList(
|
|
2463
|
+
env.plugins,
|
|
2464
|
+
(p) => hooks(p)?.confirmGuard,
|
|
2465
|
+
upload.confirmGuard
|
|
2466
|
+
),
|
|
2467
|
+
onConfirmed: hookList(
|
|
2468
|
+
env.plugins,
|
|
2469
|
+
(p) => hooks(p)?.onConfirmed,
|
|
2470
|
+
upload.onConfirmed
|
|
1110
2471
|
)
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
route2.upload.multipart.onAbort,
|
|
1114
|
-
{
|
|
1115
|
-
...stored,
|
|
1116
|
-
uploadId
|
|
1117
|
-
},
|
|
1118
|
-
config
|
|
1119
|
-
);
|
|
1120
|
-
return { bucket, key, uploadId, aborted: true };
|
|
2472
|
+
};
|
|
2473
|
+
return resolved;
|
|
1121
2474
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
} catch (err) {
|
|
1169
|
-
await abortMultipartBestEffort(route2.client, bucket, key, uploadId);
|
|
1170
|
-
throw err;
|
|
1171
|
-
}
|
|
1172
|
-
const completeResult = await sendOrObjectNotFound(
|
|
1173
|
-
() => route2.client.send(
|
|
1174
|
-
new CompleteMultipartUploadCommand({
|
|
1175
|
-
Bucket: bucket,
|
|
1176
|
-
Key: key,
|
|
1177
|
-
UploadId: uploadId,
|
|
1178
|
-
MultipartUpload: { Parts: completeParts }
|
|
1179
|
-
})
|
|
2475
|
+
function resolveMultipart(toggle, env) {
|
|
2476
|
+
if (!isFeatureOn(toggle)) return { enabled: false };
|
|
2477
|
+
const multipart = toggle === true || !toggle ? {} : toggle;
|
|
2478
|
+
const hooks = (plugin) => plugin.hooks?.upload?.multipart;
|
|
2479
|
+
return {
|
|
2480
|
+
enabled: true,
|
|
2481
|
+
partSize: routePositiveInteger(
|
|
2482
|
+
env,
|
|
2483
|
+
"upload.multipart.partSize",
|
|
2484
|
+
multipart.partSize
|
|
2485
|
+
),
|
|
2486
|
+
ticketExpiresIn: positiveInteger(
|
|
2487
|
+
env,
|
|
2488
|
+
"upload.multipart.ticketExpiresIn",
|
|
2489
|
+
multipart.ticketExpiresIn,
|
|
2490
|
+
DEFAULT_MULTIPART_TICKET_EXPIRES_IN
|
|
2491
|
+
),
|
|
2492
|
+
guard: hookList(env.plugins, (p) => hooks(p)?.guard, multipart.guard),
|
|
2493
|
+
onAbort: hookList(env.plugins, (p) => hooks(p)?.onAbort, multipart.onAbort)
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
function resolveDownload(toggle, env) {
|
|
2497
|
+
if (!isFeatureOn(toggle)) return { enabled: false };
|
|
2498
|
+
const download = toggle === true || !toggle ? {} : toggle;
|
|
2499
|
+
const mode = download.mode ?? "presign";
|
|
2500
|
+
const hooks = (plugin) => plugin.hooks?.download;
|
|
2501
|
+
return {
|
|
2502
|
+
enabled: true,
|
|
2503
|
+
maxExpiresIn: Math.min(
|
|
2504
|
+
positiveInteger(
|
|
2505
|
+
env,
|
|
2506
|
+
"download.maxExpiresIn",
|
|
2507
|
+
download.maxExpiresIn,
|
|
2508
|
+
S3_DEFAULT_EXPIRES_IN
|
|
2509
|
+
),
|
|
2510
|
+
env.maxExpiresIn
|
|
2511
|
+
),
|
|
2512
|
+
disposition: download.disposition ?? "attachment",
|
|
2513
|
+
inlineTypes: download.inlineTypes ?? (mode === "proxy" ? DEFAULT_INLINE_TYPES.filter((type) => type !== "application/pdf") : DEFAULT_INLINE_TYPES),
|
|
2514
|
+
mode,
|
|
2515
|
+
resolve: download.resolve,
|
|
2516
|
+
guard: hookList(env.plugins, (p) => hooks(p)?.guard, download.guard),
|
|
2517
|
+
onPresigned: hookList(
|
|
2518
|
+
env.plugins,
|
|
2519
|
+
(p) => hooks(p)?.onPresigned,
|
|
2520
|
+
download.onPresigned
|
|
1180
2521
|
)
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
);
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
acl: route2.upload.acl
|
|
1197
|
-
});
|
|
1198
|
-
return { ...confirmed, uploadId };
|
|
2522
|
+
};
|
|
2523
|
+
}
|
|
2524
|
+
function resolveDelete(toggle, env) {
|
|
2525
|
+
if (!isFeatureOn(toggle)) return { enabled: false };
|
|
2526
|
+
const config = toggle === true || !toggle ? {} : toggle;
|
|
2527
|
+
const hooks = (plugin) => plugin.hooks?.delete;
|
|
2528
|
+
return {
|
|
2529
|
+
enabled: true,
|
|
2530
|
+
guard: hookList(env.plugins, (p) => hooks(p)?.guard, config.guard),
|
|
2531
|
+
onDeleted: hookList(
|
|
2532
|
+
env.plugins,
|
|
2533
|
+
(p) => hooks(p)?.onDeleted,
|
|
2534
|
+
config.onDeleted
|
|
2535
|
+
)
|
|
2536
|
+
};
|
|
1199
2537
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
return handleComplete(ctx.context.config, ctx.body, ctx.context.request);
|
|
1205
|
-
}
|
|
1206
|
-
);
|
|
1207
|
-
async function handleMultipartInit(config, input, request) {
|
|
1208
|
-
const fileSize = Math.floor(input.fileSize);
|
|
1209
|
-
const fileName = input.fileName;
|
|
1210
|
-
const {
|
|
1211
|
-
route: route2,
|
|
1212
|
-
key,
|
|
1213
|
-
bucket,
|
|
1214
|
-
metadata,
|
|
1215
|
-
acl,
|
|
1216
|
-
storageClass,
|
|
1217
|
-
cacheControl,
|
|
1218
|
-
tagging,
|
|
1219
|
-
stored
|
|
1220
|
-
} = await openUploadTarget(
|
|
1221
|
-
config,
|
|
1222
|
-
{
|
|
1223
|
-
route: input.route,
|
|
1224
|
-
fileName,
|
|
1225
|
-
fileSize,
|
|
1226
|
-
contentType: input.contentType,
|
|
1227
|
-
metadata: input.metadata
|
|
1228
|
-
},
|
|
1229
|
-
request,
|
|
1230
|
-
"multipart"
|
|
1231
|
-
);
|
|
1232
|
-
const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
|
|
1233
|
-
await runHook(route2.upload.guard, {
|
|
1234
|
-
...stored,
|
|
1235
|
-
file: {
|
|
1236
|
-
name: fileName,
|
|
1237
|
-
size: fileSize,
|
|
1238
|
-
type: input.contentType
|
|
1239
|
-
},
|
|
1240
|
-
metadata,
|
|
1241
|
-
clientMetadata: input.metadata,
|
|
1242
|
-
acl,
|
|
1243
|
-
...objectS3,
|
|
1244
|
-
replace: route2.upload.replace
|
|
1245
|
-
});
|
|
1246
|
-
const { UploadId } = await route2.client.send(
|
|
1247
|
-
new CreateMultipartUploadCommand({
|
|
1248
|
-
Bucket: bucket,
|
|
1249
|
-
Key: key,
|
|
1250
|
-
ContentType: input.contentType,
|
|
1251
|
-
ContentDisposition: buildContentDisposition(fileName),
|
|
1252
|
-
Metadata: metadata,
|
|
1253
|
-
ACL: acl,
|
|
1254
|
-
...objectCommandExtras(objectS3)
|
|
1255
|
-
})
|
|
1256
|
-
);
|
|
1257
|
-
if (!UploadId) {
|
|
1258
|
-
throw errors.internalError();
|
|
1259
|
-
}
|
|
1260
|
-
try {
|
|
1261
|
-
await runLifecycleHook(
|
|
1262
|
-
route2.upload.multipart.onInit,
|
|
1263
|
-
{
|
|
1264
|
-
...stored,
|
|
1265
|
-
uploadId: UploadId,
|
|
1266
|
-
file: {
|
|
1267
|
-
name: fileName,
|
|
1268
|
-
size: fileSize,
|
|
1269
|
-
type: input.contentType
|
|
1270
|
-
},
|
|
1271
|
-
metadata,
|
|
1272
|
-
clientMetadata: input.metadata,
|
|
1273
|
-
acl,
|
|
1274
|
-
...objectS3,
|
|
1275
|
-
replace: route2.upload.replace
|
|
1276
|
-
},
|
|
1277
|
-
config
|
|
1278
|
-
);
|
|
1279
|
-
} catch (err) {
|
|
1280
|
-
await abortMultipartBestEffort(route2.client, bucket, key, UploadId);
|
|
1281
|
-
try {
|
|
1282
|
-
await runLifecycleHook(
|
|
1283
|
-
route2.upload.multipart.onAbort,
|
|
1284
|
-
{
|
|
1285
|
-
...stored,
|
|
1286
|
-
uploadId: UploadId
|
|
1287
|
-
},
|
|
1288
|
-
config
|
|
1289
|
-
);
|
|
1290
|
-
} catch {
|
|
1291
|
-
}
|
|
1292
|
-
throw err;
|
|
1293
|
-
}
|
|
1294
|
-
return { bucket, key, uploadId: UploadId };
|
|
1295
|
-
}
|
|
1296
|
-
var multipartInit = createS3Endpoint(
|
|
1297
|
-
S3_API_ROUTES.multipartInit,
|
|
1298
|
-
{ method: "POST", body: multipartInitBodySchema },
|
|
1299
|
-
async (ctx) => {
|
|
1300
|
-
const result = await handleMultipartInit(
|
|
1301
|
-
ctx.context.config,
|
|
1302
|
-
ctx.body,
|
|
1303
|
-
ctx.context.request
|
|
2538
|
+
function assertRouteName(name) {
|
|
2539
|
+
if (!ROUTE_NAME_PATTERN.test(name)) {
|
|
2540
|
+
throw new Error(
|
|
2541
|
+
`dimahS3 route "${name}" is invalid. Use a letter followed by up to 63 letters, digits, underscores, or hyphens.`
|
|
1304
2542
|
);
|
|
1305
|
-
ctx.setStatus(201);
|
|
1306
|
-
return result;
|
|
1307
2543
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
uploadId,
|
|
1320
|
-
action: "list"
|
|
1321
|
-
});
|
|
1322
|
-
const listed = await listAllParts(route2.client, { bucket, key, uploadId });
|
|
1323
|
-
const parts = listed.flatMap((p) => {
|
|
1324
|
-
if (p.PartNumber == null || p.PartNumber < 1) return [];
|
|
1325
|
-
return [
|
|
1326
|
-
{
|
|
1327
|
-
partNumber: p.PartNumber,
|
|
1328
|
-
size: p.Size ?? 0,
|
|
1329
|
-
eTag: (p.ETag ?? "").replaceAll('"', "")
|
|
1330
|
-
}
|
|
1331
|
-
];
|
|
1332
|
-
});
|
|
1333
|
-
await runLifecycleHook(
|
|
1334
|
-
route2.upload.multipart.onList,
|
|
1335
|
-
{
|
|
1336
|
-
...stored,
|
|
1337
|
-
uploadId,
|
|
1338
|
-
parts
|
|
1339
|
-
},
|
|
1340
|
-
config
|
|
2544
|
+
}
|
|
2545
|
+
function routeError(name, message) {
|
|
2546
|
+
return new Error(`dimahS3 route "${name}": ${message}`);
|
|
2547
|
+
}
|
|
2548
|
+
function positiveInteger(env, field, value, fallback) {
|
|
2549
|
+
return routePositiveInteger(env, field, value) ?? fallback;
|
|
2550
|
+
}
|
|
2551
|
+
function routePositiveInteger(env, field, value) {
|
|
2552
|
+
return optionalPositiveInteger(
|
|
2553
|
+
value,
|
|
2554
|
+
`dimahS3 route "${env.name}": ${field}`
|
|
1341
2555
|
);
|
|
1342
|
-
return { parts };
|
|
1343
2556
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
return handleListParts(ctx.context.config, ctx.query, ctx.context.request);
|
|
2557
|
+
function optionalPositiveInteger(value, label) {
|
|
2558
|
+
if (value === void 0) return void 0;
|
|
2559
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
2560
|
+
throw new Error(`${label} must be a positive integer.`);
|
|
1349
2561
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
"
|
|
1357
|
-
);
|
|
1358
|
-
const uploadId = input.uploadId;
|
|
1359
|
-
const partNumber = input.partNumber;
|
|
1360
|
-
const expiresIn = normalizeExpiresIn(
|
|
1361
|
-
route2.upload.expiresIn,
|
|
1362
|
-
config.maxExpiresIn
|
|
1363
|
-
);
|
|
1364
|
-
const partSize = Math.floor(input.partSize);
|
|
1365
|
-
await runHook(route2.upload.multipart.guard, {
|
|
1366
|
-
...stored,
|
|
1367
|
-
uploadId,
|
|
1368
|
-
action: "part",
|
|
1369
|
-
partNumber,
|
|
1370
|
-
partSize
|
|
1371
|
-
});
|
|
1372
|
-
const maxFileSize = route2.upload.maxFileSize;
|
|
1373
|
-
assertWithinMaxFileSize(maxFileSize, partSize);
|
|
1374
|
-
if (typeof maxFileSize === "number") {
|
|
1375
|
-
const listed = await listAllParts(route2.client, { bucket, key, uploadId });
|
|
1376
|
-
assertWithinMaxFileSize(
|
|
1377
|
-
maxFileSize,
|
|
1378
|
-
listedPartsByteSize(listed, partNumber) + partSize
|
|
1379
|
-
);
|
|
2562
|
+
return value;
|
|
2563
|
+
}
|
|
2564
|
+
function resolveKeyPrefix(name, value) {
|
|
2565
|
+
if (value === false) return false;
|
|
2566
|
+
const normalized = normalizeObjectKey(value ?? name);
|
|
2567
|
+
if (!normalized) {
|
|
2568
|
+
throw routeError(name, "keyPrefix is not a valid object-key prefix.");
|
|
1380
2569
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
2570
|
+
return normalized;
|
|
2571
|
+
}
|
|
2572
|
+
function prefixesOverlap(a, b) {
|
|
2573
|
+
return a === b || a.startsWith(`${b}/`) || b.startsWith(`${a}/`);
|
|
2574
|
+
}
|
|
2575
|
+
function assertDistinctKeyPrefixes(routes, logger) {
|
|
2576
|
+
const list = Object.values(routes);
|
|
2577
|
+
for (let i = 0; i < list.length; i++) {
|
|
2578
|
+
for (let j = i + 1; j < list.length; j++) {
|
|
2579
|
+
const a = list[i];
|
|
2580
|
+
const b = list[j];
|
|
2581
|
+
if (a.bucket !== b.bucket) continue;
|
|
2582
|
+
if (a.keyPrefix === false || b.keyPrefix === false) {
|
|
2583
|
+
const [open, other] = a.keyPrefix === false ? [a, b] : [b, a];
|
|
2584
|
+
logger?.warn?.(
|
|
2585
|
+
`dimahS3 route "${open.name}" sets keyPrefix: false on bucket "${open.bucket}", which route "${other.name}" also uses, so its keys reach "${other.name}" objects. Give each route its own keyPrefix unless that is intended.`
|
|
2586
|
+
);
|
|
2587
|
+
continue;
|
|
2588
|
+
}
|
|
2589
|
+
if (prefixesOverlap(a.keyPrefix, b.keyPrefix)) {
|
|
2590
|
+
throw new Error(
|
|
2591
|
+
`dimahS3: routes "${a.name}" and "${b.name}" have overlapping keyPrefix values ("${a.keyPrefix}" and "${b.keyPrefix}") on bucket "${a.bucket}".`
|
|
2592
|
+
);
|
|
2593
|
+
}
|
|
1393
2594
|
}
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
|
|
2598
|
+
// src/config/resolve-config.ts
|
|
2599
|
+
var DEFAULT_MAX_REQUEST_BODY_SIZE = 1048576;
|
|
2600
|
+
function resolveConfig(config) {
|
|
2601
|
+
const plugins = config.plugins ?? [];
|
|
2602
|
+
const { basePath, baseURL } = resolveApiLocation(config);
|
|
2603
|
+
const maxExpiresIn = Math.min(
|
|
2604
|
+
optionalPositiveInteger(config.maxExpiresIn, "dimahS3: maxExpiresIn") ?? S3_MAX_EXPIRES_IN,
|
|
2605
|
+
S3_MAX_EXPIRES_IN
|
|
1394
2606
|
);
|
|
2607
|
+
const logger = resolveLogger(config.logger);
|
|
2608
|
+
const maxRequestBodySize = optionalPositiveInteger(
|
|
2609
|
+
config.maxRequestBodySize,
|
|
2610
|
+
"dimahS3: maxRequestBodySize"
|
|
2611
|
+
) ?? DEFAULT_MAX_REQUEST_BODY_SIZE;
|
|
1395
2612
|
return {
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
2613
|
+
basePath,
|
|
2614
|
+
...baseURL !== void 0 ? { baseURL } : {},
|
|
2615
|
+
secret: parseSecretOption(config.secret),
|
|
2616
|
+
ticketSubject: config.ticketSubject ?? plugins.find((plugin) => plugin.hooks?.ticketSubject)?.hooks?.ticketSubject,
|
|
2617
|
+
maxExpiresIn,
|
|
2618
|
+
maxRequestBodySize,
|
|
2619
|
+
logger,
|
|
2620
|
+
onError: config.onError,
|
|
2621
|
+
guard: hookList(plugins, (plugin) => plugin.hooks?.guard, config.guard),
|
|
2622
|
+
routes: resolveRoutes(config, { maxExpiresIn, plugins, logger })
|
|
1402
2623
|
};
|
|
1403
2624
|
}
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
return handleSignPart(ctx.context.config, ctx.body, ctx.context.request);
|
|
2625
|
+
function resolveApiLocation(config) {
|
|
2626
|
+
const basePath = config.basePath === void 0 ? void 0 : normalizeS3ApiBasePath(config.basePath);
|
|
2627
|
+
if (config.baseURL === void 0) {
|
|
2628
|
+
return { basePath: basePath ?? S3_API_BASE_PATH };
|
|
1409
2629
|
}
|
|
1410
|
-
);
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
const fileName = input.fileName;
|
|
1422
|
-
const {
|
|
1423
|
-
route: route2,
|
|
1424
|
-
key,
|
|
1425
|
-
bucket,
|
|
1426
|
-
metadata,
|
|
1427
|
-
acl,
|
|
1428
|
-
storageClass,
|
|
1429
|
-
cacheControl,
|
|
1430
|
-
tagging,
|
|
1431
|
-
stored
|
|
1432
|
-
} = await openUploadTarget(
|
|
1433
|
-
config,
|
|
1434
|
-
{
|
|
1435
|
-
route: input.route,
|
|
1436
|
-
fileName,
|
|
1437
|
-
fileSize,
|
|
1438
|
-
contentType: input.contentType,
|
|
1439
|
-
metadata: input.metadata
|
|
1440
|
-
},
|
|
1441
|
-
request,
|
|
1442
|
-
"upload"
|
|
1443
|
-
);
|
|
1444
|
-
const objectS3 = normalizeObjectS3({ storageClass, cacheControl, tagging });
|
|
1445
|
-
const method = route2.upload.method ?? "POST";
|
|
1446
|
-
if (route2.upload.checksum && !input.checksum) {
|
|
1447
|
-
throw errors.validationError("Checksum is required for this route");
|
|
1448
|
-
}
|
|
1449
|
-
if (method === "POST" && fileSize > S3_MAX_POST_OBJECT_BYTES) {
|
|
1450
|
-
throw errors.payloadTooLarge(
|
|
1451
|
-
`POST uploads cannot exceed ${S3_MAX_POST_OBJECT_BYTES} bytes. Enable upload.multipart or set upload.method to "PUT".`
|
|
2630
|
+
const url = parseURL(config.baseURL);
|
|
2631
|
+
if (url?.protocol !== "http:" && url?.protocol !== "https:") {
|
|
2632
|
+
throw new Error("dimahS3: baseURL must be an absolute http(s) URL.");
|
|
2633
|
+
}
|
|
2634
|
+
if (url.search || url.hash) {
|
|
2635
|
+
throw new Error("dimahS3: baseURL must not have a query or a hash.");
|
|
2636
|
+
}
|
|
2637
|
+
const urlPath = normalizeS3ApiBasePath(url.pathname);
|
|
2638
|
+
if (basePath !== void 0 && basePath !== urlPath) {
|
|
2639
|
+
throw new Error(
|
|
2640
|
+
`dimahS3: the baseURL path "${urlPath || "/"}" must equal basePath "${basePath || "/"}".`
|
|
1452
2641
|
);
|
|
1453
2642
|
}
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
};
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
2643
|
+
return { basePath: urlPath, baseURL: `${url.origin}${urlPath}` };
|
|
2644
|
+
}
|
|
2645
|
+
function parseURL(value) {
|
|
2646
|
+
try {
|
|
2647
|
+
return new URL(value);
|
|
2648
|
+
} catch {
|
|
2649
|
+
return void 0;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
var PUT_EXPIRES_IN_MS = 15 * 60 * 1e3;
|
|
2653
|
+
var putFileSchema = uploadBodySchema.pick({
|
|
2654
|
+
route: true,
|
|
2655
|
+
name: true,
|
|
2656
|
+
size: true,
|
|
2657
|
+
type: true
|
|
2658
|
+
});
|
|
2659
|
+
async function putObject(config, input, options = {}) {
|
|
2660
|
+
const { body, size } = readBody(input);
|
|
2661
|
+
const file = parsePutFile({
|
|
2662
|
+
route: input.route,
|
|
2663
|
+
name: input.name,
|
|
2664
|
+
size,
|
|
2665
|
+
type: input.type
|
|
2666
|
+
});
|
|
2667
|
+
return runOperation(config, "put", options, async (call2) => {
|
|
2668
|
+
const target = await openUploadTarget(
|
|
2669
|
+
config,
|
|
2670
|
+
call2,
|
|
2671
|
+
{
|
|
2672
|
+
route: file.route,
|
|
2673
|
+
file: { name: file.name, size: file.size, type: file.type },
|
|
2674
|
+
clientMetadata: input.clientMetadata,
|
|
2675
|
+
metadata: input.metadata
|
|
2676
|
+
},
|
|
2677
|
+
"server"
|
|
2678
|
+
);
|
|
2679
|
+
const { route: route2, context, object, uploadToken } = target;
|
|
2680
|
+
await runLifecycle(route2.upload.onStart, {
|
|
2681
|
+
...context,
|
|
2682
|
+
uploadToken,
|
|
2683
|
+
expiresAt: new Date(Date.now() + PUT_EXPIRES_IN_MS)
|
|
2684
|
+
});
|
|
2685
|
+
await route2.client.send(
|
|
1483
2686
|
new PutObjectCommand({
|
|
1484
|
-
Bucket: bucket,
|
|
1485
|
-
Key: key,
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
ContentDisposition:
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
...
|
|
2687
|
+
Bucket: object.bucket,
|
|
2688
|
+
Key: object.key,
|
|
2689
|
+
Body: sdkBody(route2.client, body),
|
|
2690
|
+
ContentLength: file.size,
|
|
2691
|
+
ContentType: target.contentType,
|
|
2692
|
+
ContentDisposition: target.contentDisposition,
|
|
2693
|
+
ACL: object.acl,
|
|
2694
|
+
Metadata: target.s3Metadata,
|
|
2695
|
+
...objectCommandExtras(object)
|
|
1493
2696
|
}),
|
|
1494
|
-
{
|
|
1495
|
-
expiresIn,
|
|
1496
|
-
signableHeaders: /* @__PURE__ */ new Set([
|
|
1497
|
-
"content-length",
|
|
1498
|
-
...input.checksum ? ["x-amz-checksum-sha256"] : []
|
|
1499
|
-
])
|
|
1500
|
-
}
|
|
2697
|
+
{ abortSignal: call2.request.signal }
|
|
1501
2698
|
);
|
|
1502
|
-
await
|
|
1503
|
-
route2.
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
url: url2,
|
|
1507
|
-
expiresIn
|
|
1508
|
-
},
|
|
1509
|
-
config
|
|
2699
|
+
const head = await headObjectOrNotFound(
|
|
2700
|
+
route2.client,
|
|
2701
|
+
object.bucket,
|
|
2702
|
+
object.key
|
|
1510
2703
|
);
|
|
1511
|
-
return {
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
2704
|
+
return finalizeConfirmed({
|
|
2705
|
+
route: route2,
|
|
2706
|
+
context,
|
|
2707
|
+
kind: "server",
|
|
2708
|
+
uploadToken,
|
|
2709
|
+
file: context.file,
|
|
2710
|
+
acl: object.acl,
|
|
2711
|
+
head
|
|
2712
|
+
});
|
|
2713
|
+
});
|
|
2714
|
+
}
|
|
2715
|
+
function readBody(input) {
|
|
2716
|
+
const { body } = input;
|
|
2717
|
+
let normalized;
|
|
2718
|
+
if (typeof body === "string") {
|
|
2719
|
+
const bytes = new TextEncoder().encode(body);
|
|
2720
|
+
normalized = { body: bytes, size: bytes.byteLength };
|
|
2721
|
+
} else if (body instanceof ArrayBuffer) {
|
|
2722
|
+
normalized = { body: new Uint8Array(body), size: body.byteLength };
|
|
2723
|
+
} else if (body instanceof Uint8Array) {
|
|
2724
|
+
normalized = { body, size: body.byteLength };
|
|
2725
|
+
} else if (body instanceof Blob) {
|
|
2726
|
+
normalized = { body: body.stream(), size: body.size };
|
|
2727
|
+
} else {
|
|
2728
|
+
normalized = { body, size: void 0 };
|
|
2729
|
+
}
|
|
2730
|
+
if (normalized.size === void 0) {
|
|
2731
|
+
if (input.size === void 0) {
|
|
2732
|
+
throw invalidPut("size is required for a ReadableStream body");
|
|
1536
2733
|
}
|
|
2734
|
+
return { body: normalized.body, size: input.size };
|
|
2735
|
+
}
|
|
2736
|
+
if (input.size !== void 0 && input.size !== normalized.size) {
|
|
2737
|
+
throw invalidPut(
|
|
2738
|
+
`size is ${input.size}, but the body has ${normalized.size} bytes`
|
|
2739
|
+
);
|
|
2740
|
+
}
|
|
2741
|
+
return { body: normalized.body, size: normalized.size };
|
|
2742
|
+
}
|
|
2743
|
+
function parsePutFile(value) {
|
|
2744
|
+
const result = putFileSchema.safeParse(value);
|
|
2745
|
+
if (result.success) return result.data;
|
|
2746
|
+
throw invalidPut(
|
|
2747
|
+
result.error.issues.map(
|
|
2748
|
+
(issue) => issue.path.length > 0 ? `${issue.path.join(".")}: ${issue.message}` : issue.message
|
|
2749
|
+
).join("; ")
|
|
1537
2750
|
);
|
|
1538
|
-
await runLifecycleHook(
|
|
1539
|
-
route2.upload.onPresigned,
|
|
1540
|
-
{
|
|
1541
|
-
...hookCtx,
|
|
1542
|
-
url,
|
|
1543
|
-
expiresIn
|
|
1544
|
-
},
|
|
1545
|
-
config
|
|
1546
|
-
);
|
|
1547
|
-
return {
|
|
1548
|
-
bucket,
|
|
1549
|
-
key,
|
|
1550
|
-
url,
|
|
1551
|
-
fields: signedFields,
|
|
1552
|
-
expiresIn,
|
|
1553
|
-
method: "POST"
|
|
1554
|
-
};
|
|
1555
2751
|
}
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
)
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
2752
|
+
function invalidPut(issue) {
|
|
2753
|
+
return s3Error("VALIDATION_ERROR", { issue: `s3.put: ${issue}` });
|
|
2754
|
+
}
|
|
2755
|
+
function sdkBody(client, body) {
|
|
2756
|
+
if (body instanceof Uint8Array) return body;
|
|
2757
|
+
const handler = client.config.requestHandler;
|
|
2758
|
+
const protocol = handler?.metadata?.handlerProtocol;
|
|
2759
|
+
const nodeStream = protocol === "http/1.1" || protocol === "h2" ? globalThis.process?.getBuiltinModule?.("node:stream") : void 0;
|
|
2760
|
+
return nodeStream ? nodeStream.Readable.fromWeb(body) : body;
|
|
2761
|
+
}
|
|
2762
|
+
function createS3Api(config) {
|
|
2763
|
+
return {
|
|
2764
|
+
upload: async (payload, options) => presignUpload(config, parse(uploadBodySchema, payload, "body"), options),
|
|
2765
|
+
confirm: async (payload, options) => confirmUpload(config, parse(ticketBodySchema, payload, "body"), options),
|
|
2766
|
+
download: async (payload, options) => presignDownload(
|
|
2767
|
+
config,
|
|
2768
|
+
parse(downloadQuerySchema, payload, "query"),
|
|
2769
|
+
options
|
|
2770
|
+
),
|
|
2771
|
+
catalog: async (options) => getRouteCatalog(config, options),
|
|
2772
|
+
delete: async (payload, options) => deleteStoredObject(
|
|
2773
|
+
config,
|
|
2774
|
+
parse(deleteQuerySchema, payload, "query"),
|
|
2775
|
+
options
|
|
2776
|
+
),
|
|
2777
|
+
deleteMany: async (payload, options) => deleteManyObjects(
|
|
2778
|
+
config,
|
|
2779
|
+
parse(deleteManyBodySchema, payload, "body"),
|
|
2780
|
+
options
|
|
2781
|
+
),
|
|
2782
|
+
multipart: {
|
|
2783
|
+
init: async (payload, options) => initMultipart(
|
|
2784
|
+
config,
|
|
2785
|
+
parse(multipartInitBodySchema, payload, "body"),
|
|
2786
|
+
options
|
|
2787
|
+
),
|
|
2788
|
+
signPart: async (payload, options) => signMultipartPart(
|
|
2789
|
+
config,
|
|
2790
|
+
parse(multipartSignPartBodySchema, payload, "body"),
|
|
2791
|
+
options
|
|
2792
|
+
),
|
|
2793
|
+
listParts: async (payload, options) => listMultipartParts(
|
|
2794
|
+
config,
|
|
2795
|
+
parse(ticketBodySchema, payload, "body"),
|
|
2796
|
+
options
|
|
2797
|
+
),
|
|
2798
|
+
complete: async (payload, options) => completeMultipart(
|
|
2799
|
+
config,
|
|
2800
|
+
parse(ticketBodySchema, payload, "body"),
|
|
2801
|
+
options
|
|
2802
|
+
),
|
|
2803
|
+
abort: async (payload, options) => abortMultipart(
|
|
2804
|
+
config,
|
|
2805
|
+
parse(ticketBodySchema, payload, "body"),
|
|
2806
|
+
options
|
|
2807
|
+
)
|
|
1588
2808
|
}
|
|
1589
2809
|
};
|
|
1590
2810
|
}
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
};
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
"onAbort",
|
|
1602
|
-
"onList"
|
|
1603
|
-
];
|
|
1604
|
-
var LIFECYCLE_FEATURE_HOOK_KEYS = {
|
|
1605
|
-
upload: ["onPresigned", "onConfirmed"],
|
|
1606
|
-
download: ["onPresigned"],
|
|
1607
|
-
delete: ["onDeleted"]
|
|
1608
|
-
};
|
|
1609
|
-
var LIFECYCLE_MULTIPART_HOOK_KEYS = [
|
|
1610
|
-
"onInit",
|
|
1611
|
-
"onAbort",
|
|
1612
|
-
"onList"
|
|
1613
|
-
];
|
|
1614
|
-
|
|
1615
|
-
// src/plugin/types.ts
|
|
1616
|
-
var RESERVED_PLUGIN_IDS = [
|
|
2811
|
+
function parse(schema, payload, source) {
|
|
2812
|
+
const result = schema.safeParse(payload);
|
|
2813
|
+
if (result.success) return result.data;
|
|
2814
|
+
const issue = result.error.issues.map((item) => {
|
|
2815
|
+
const path = [source, ...item.path.map(String)].join(".");
|
|
2816
|
+
return `[${path}] ${item.message}`;
|
|
2817
|
+
}).join("; ");
|
|
2818
|
+
throw s3Error("VALIDATION_ERROR", { issue });
|
|
2819
|
+
}
|
|
2820
|
+
var RESERVED_PLUGIN_IDS = /* @__PURE__ */ new Set([
|
|
1617
2821
|
"handler",
|
|
1618
2822
|
"api",
|
|
2823
|
+
"put",
|
|
1619
2824
|
"context",
|
|
1620
2825
|
"getPlugin",
|
|
1621
2826
|
"$ERROR_CODES",
|
|
1622
|
-
"$Infer"
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
"multipart"
|
|
1632
|
-
];
|
|
1633
|
-
|
|
1634
|
-
// src/plugin/apply-plugins.ts
|
|
1635
|
-
function mergeHookField(pluginHooks, userHook, order = "plugins-first") {
|
|
1636
|
-
const present2 = (order === "user-first" ? [userHook, ...pluginHooks] : [...pluginHooks, userHook]).filter((h) => h != null);
|
|
1637
|
-
if (present2.length === 0) return void 0;
|
|
1638
|
-
if (present2.length === 1) return present2[0];
|
|
1639
|
-
return chainHooks(...present2);
|
|
1640
|
-
}
|
|
1641
|
-
function mergeHooks(keys, plugins, userFeature, pick, lifecycleKeys = /* @__PURE__ */ new Set()) {
|
|
1642
|
-
const merged = { ...userFeature };
|
|
1643
|
-
for (const key of keys) {
|
|
1644
|
-
const next = mergeHookField(
|
|
1645
|
-
plugins.map((plugin) => pick(plugin)?.[key]),
|
|
1646
|
-
userFeature[key],
|
|
1647
|
-
lifecycleKeys.has(key) ? "user-first" : "plugins-first"
|
|
1648
|
-
);
|
|
1649
|
-
if (next !== void 0) merged[key] = next;
|
|
1650
|
-
}
|
|
1651
|
-
return merged;
|
|
1652
|
-
}
|
|
1653
|
-
function applyHooksToRoute(route2, plugins) {
|
|
1654
|
-
const active = plugins.filter((p) => !route2.skippedPluginIds.has(p.id));
|
|
1655
|
-
const upload2 = isEnabled(route2.upload) ? {
|
|
1656
|
-
...mergeHooks(
|
|
1657
|
-
FEATURE_HOOK_KEYS.upload,
|
|
1658
|
-
active,
|
|
1659
|
-
route2.upload,
|
|
1660
|
-
(p) => p.hooks?.upload,
|
|
1661
|
-
new Set(LIFECYCLE_FEATURE_HOOK_KEYS.upload)
|
|
1662
|
-
),
|
|
1663
|
-
multipart: isEnabled(route2.upload.multipart) ? mergeHooks(
|
|
1664
|
-
MULTIPART_HOOK_KEYS,
|
|
1665
|
-
active,
|
|
1666
|
-
route2.upload.multipart,
|
|
1667
|
-
(p) => p.hooks?.upload?.multipart,
|
|
1668
|
-
new Set(LIFECYCLE_MULTIPART_HOOK_KEYS)
|
|
1669
|
-
) : route2.upload.multipart
|
|
1670
|
-
} : route2.upload;
|
|
1671
|
-
return {
|
|
1672
|
-
...route2,
|
|
1673
|
-
upload: upload2,
|
|
1674
|
-
download: isEnabled(route2.download) ? mergeHooks(
|
|
1675
|
-
FEATURE_HOOK_KEYS.download,
|
|
1676
|
-
active,
|
|
1677
|
-
route2.download,
|
|
1678
|
-
(p) => p.hooks?.download,
|
|
1679
|
-
new Set(LIFECYCLE_FEATURE_HOOK_KEYS.download)
|
|
1680
|
-
) : route2.download,
|
|
1681
|
-
delete: isEnabled(route2.delete) ? mergeHooks(
|
|
1682
|
-
FEATURE_HOOK_KEYS.delete,
|
|
1683
|
-
active,
|
|
1684
|
-
route2.delete,
|
|
1685
|
-
(p) => p.hooks?.delete,
|
|
1686
|
-
new Set(LIFECYCLE_FEATURE_HOOK_KEYS.delete)
|
|
1687
|
-
) : route2.delete
|
|
1688
|
-
};
|
|
1689
|
-
}
|
|
1690
|
-
function applyPlugins(config) {
|
|
2827
|
+
"$Infer"
|
|
2828
|
+
]);
|
|
2829
|
+
var CORE_ENDPOINT_NAMES = new Set(
|
|
2830
|
+
Object.keys(S3_API_ROUTES)
|
|
2831
|
+
);
|
|
2832
|
+
var CORE_ENDPOINT_PATHS = new Set(
|
|
2833
|
+
Object.values(S3_API_ROUTES)
|
|
2834
|
+
);
|
|
2835
|
+
function applyPlugins(config, logger) {
|
|
1691
2836
|
const plugins = config.plugins ?? [];
|
|
1692
|
-
const
|
|
1693
|
-
|
|
2837
|
+
const byId = registerPlugins(plugins);
|
|
2838
|
+
assertDependencies(plugins, byId);
|
|
2839
|
+
const endpoints = collectEndpoints(plugins);
|
|
1694
2840
|
const context = {};
|
|
2841
|
+
for (const plugin of plugins) {
|
|
2842
|
+
if (plugin.context !== void 0) context[plugin.id] = plugin.context;
|
|
2843
|
+
}
|
|
1695
2844
|
const getPlugin = (id) => byId.get(id);
|
|
1696
2845
|
for (const plugin of plugins) {
|
|
1697
|
-
|
|
2846
|
+
const result = plugin.init?.(
|
|
2847
|
+
initEnv(plugin.id, config, getPlugin, logger)
|
|
2848
|
+
);
|
|
2849
|
+
if (isPromiseLike(result)) {
|
|
2850
|
+
void Promise.resolve(result).catch(() => void 0);
|
|
2851
|
+
throw new Error(
|
|
2852
|
+
`dimahS3: plugin "${plugin.id}" init returned a promise. init runs inside dimahS3(), so it must be synchronous; start async work lazily, in a hook or a context method.`
|
|
2853
|
+
);
|
|
2854
|
+
}
|
|
2855
|
+
}
|
|
2856
|
+
return { context, getPlugin, endpoints };
|
|
2857
|
+
}
|
|
2858
|
+
function isPromiseLike(value) {
|
|
2859
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
2860
|
+
}
|
|
2861
|
+
function registerPlugins(plugins) {
|
|
2862
|
+
const byId = /* @__PURE__ */ new Map();
|
|
2863
|
+
for (const plugin of plugins) {
|
|
2864
|
+
if (RESERVED_PLUGIN_IDS.has(plugin.id)) {
|
|
1698
2865
|
throw new Error(
|
|
1699
|
-
`
|
|
2866
|
+
`dimahS3: plugin id "${plugin.id}" is reserved on the instance. Choose a different id.`
|
|
1700
2867
|
);
|
|
1701
2868
|
}
|
|
1702
|
-
if (
|
|
2869
|
+
if (byId.has(plugin.id)) {
|
|
1703
2870
|
throw new Error(
|
|
1704
|
-
`
|
|
2871
|
+
`dimahS3: duplicate plugin id "${plugin.id}". Each plugin id must be unique.`
|
|
1705
2872
|
);
|
|
1706
2873
|
}
|
|
1707
|
-
seen.add(plugin.id);
|
|
1708
2874
|
byId.set(plugin.id, plugin);
|
|
1709
2875
|
}
|
|
2876
|
+
return byId;
|
|
2877
|
+
}
|
|
2878
|
+
function assertDependencies(plugins, byId) {
|
|
1710
2879
|
for (const plugin of plugins) {
|
|
1711
|
-
for (const
|
|
1712
|
-
if (!
|
|
2880
|
+
for (const dependency of plugin.dependsOn ?? []) {
|
|
2881
|
+
if (!byId.has(dependency)) {
|
|
1713
2882
|
throw new Error(
|
|
1714
|
-
`
|
|
2883
|
+
`dimahS3: plugin "${plugin.id}" depends on "${dependency}", which is not registered.`
|
|
1715
2884
|
);
|
|
1716
2885
|
}
|
|
1717
2886
|
}
|
|
1718
2887
|
}
|
|
1719
|
-
const
|
|
1720
|
-
const
|
|
1721
|
-
|
|
1722
|
-
if (
|
|
1723
|
-
if (visiting.has(id)) {
|
|
2888
|
+
const done = /* @__PURE__ */ new Set();
|
|
2889
|
+
const visit = (id, path) => {
|
|
2890
|
+
if (done.has(id)) return;
|
|
2891
|
+
if (path.includes(id)) {
|
|
1724
2892
|
throw new Error(
|
|
1725
|
-
`
|
|
2893
|
+
`dimahS3: circular plugin dependency ${[...path, id].join(" -> ")}.`
|
|
1726
2894
|
);
|
|
1727
2895
|
}
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
for (const dep of plugin?.dependsOn ?? []) {
|
|
1731
|
-
visit(dep, [...stack, id]);
|
|
2896
|
+
for (const dependency of byId.get(id)?.dependsOn ?? []) {
|
|
2897
|
+
visit(dependency, [...path, id]);
|
|
1732
2898
|
}
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
}
|
|
1739
|
-
for (const plugin of plugins) {
|
|
1740
|
-
if (plugin.context !== void 0) {
|
|
1741
|
-
context[plugin.id] = plugin.context;
|
|
1742
|
-
}
|
|
1743
|
-
}
|
|
1744
|
-
const reservedRoutePaths = new Set(Object.values(S3_API_ROUTES));
|
|
1745
|
-
const reservedNames = /* @__PURE__ */ new Set([...CORE_ENDPOINT_NAMES, "multipart"]);
|
|
1746
|
-
const seenEndpointKeys = /* @__PURE__ */ new Set();
|
|
2899
|
+
done.add(id);
|
|
2900
|
+
};
|
|
2901
|
+
for (const plugin of plugins) visit(plugin.id, []);
|
|
2902
|
+
}
|
|
2903
|
+
function collectEndpoints(plugins) {
|
|
1747
2904
|
const endpoints = {};
|
|
2905
|
+
const routes = /* @__PURE__ */ new Set();
|
|
1748
2906
|
for (const plugin of plugins) {
|
|
1749
2907
|
for (const [name, endpoint] of Object.entries(plugin.endpoints ?? {})) {
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
);
|
|
2908
|
+
const label = `dimahS3: plugin "${plugin.id}" endpoint "${name}"`;
|
|
2909
|
+
if (CORE_ENDPOINT_NAMES.has(name) || Object.hasOwn(endpoints, name)) {
|
|
2910
|
+
throw new Error(`${label} reuses an existing endpoint name.`);
|
|
1754
2911
|
}
|
|
1755
|
-
const path = endpoint
|
|
2912
|
+
const { path } = endpoint;
|
|
1756
2913
|
if (typeof path !== "string" || !path.startsWith("/")) {
|
|
1757
|
-
throw new Error(
|
|
1758
|
-
`Plugin "${plugin.id}" endpoint "${name}" path must start with "/".`
|
|
1759
|
-
);
|
|
2914
|
+
throw new Error(`${label} needs a path that starts with "/".`);
|
|
1760
2915
|
}
|
|
1761
|
-
if (
|
|
1762
|
-
throw new Error(
|
|
1763
|
-
`Plugin "${plugin.id}" endpoint "${name}" collides with a core route.`
|
|
1764
|
-
);
|
|
2916
|
+
if (CORE_ENDPOINT_PATHS.has(path)) {
|
|
2917
|
+
throw new Error(`${label} uses the core path ${path}.`);
|
|
1765
2918
|
}
|
|
1766
|
-
const
|
|
1767
|
-
for (const
|
|
1768
|
-
const
|
|
1769
|
-
if (
|
|
1770
|
-
throw new Error(
|
|
2919
|
+
const { method } = endpoint.options;
|
|
2920
|
+
for (const verb of Array.isArray(method) ? method : [method]) {
|
|
2921
|
+
const route2 = `${verb} ${path}`;
|
|
2922
|
+
if (routes.has(route2)) {
|
|
2923
|
+
throw new Error(`${label} duplicates ${route2}.`);
|
|
1771
2924
|
}
|
|
1772
|
-
|
|
2925
|
+
routes.add(route2);
|
|
1773
2926
|
}
|
|
1774
2927
|
endpoints[name] = endpoint;
|
|
1775
2928
|
}
|
|
1776
2929
|
}
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
plugin.init?.({ config: rest, getPlugin });
|
|
1781
|
-
}
|
|
1782
|
-
const routes = {};
|
|
1783
|
-
for (const [name, route2] of Object.entries(normalized)) {
|
|
1784
|
-
routes[name] = applyHooksToRoute(route2, plugins);
|
|
1785
|
-
}
|
|
1786
|
-
const guard = mergeHookField(
|
|
1787
|
-
plugins.map((p) => p.hooks?.guard),
|
|
1788
|
-
rest.guard
|
|
1789
|
-
);
|
|
1790
|
-
const merged = {
|
|
1791
|
-
...rest,
|
|
1792
|
-
routes,
|
|
1793
|
-
...guard !== void 0 ? { guard } : {}
|
|
1794
|
-
};
|
|
2930
|
+
return endpoints;
|
|
2931
|
+
}
|
|
2932
|
+
function initEnv(pluginId, config, getPlugin, logger) {
|
|
1795
2933
|
return {
|
|
1796
|
-
config
|
|
1797
|
-
context,
|
|
2934
|
+
config,
|
|
1798
2935
|
getPlugin,
|
|
1799
|
-
|
|
2936
|
+
routeOptions: (route2) => {
|
|
2937
|
+
if (!Object.hasOwn(config.routes, route2)) return void 0;
|
|
2938
|
+
const options = config.routes[route2].plugins;
|
|
2939
|
+
if (!options || !Object.hasOwn(options, pluginId)) return void 0;
|
|
2940
|
+
return options[pluginId];
|
|
2941
|
+
},
|
|
2942
|
+
logger
|
|
1800
2943
|
};
|
|
1801
2944
|
}
|
|
1802
2945
|
|
|
1803
|
-
// src/plugin/
|
|
1804
|
-
function
|
|
1805
|
-
return
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
const
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
});
|
|
1820
|
-
return endpoint({
|
|
1821
|
-
...input,
|
|
1822
|
-
headers: provided ? headers ?? request.headers : request.headers,
|
|
1823
|
-
request,
|
|
1824
|
-
context
|
|
1825
|
-
});
|
|
1826
|
-
});
|
|
1827
|
-
bound.path = endpoint.path;
|
|
1828
|
-
bound.options = endpoint.options;
|
|
1829
|
-
api[key] = bound;
|
|
1830
|
-
}
|
|
1831
|
-
return api;
|
|
1832
|
-
}
|
|
1833
|
-
function endpointMethod(endpoint) {
|
|
1834
|
-
const method = endpoint.options?.method;
|
|
1835
|
-
if (Array.isArray(method)) return method[0] ?? "GET";
|
|
1836
|
-
if (typeof method === "string" && method !== "*") return method;
|
|
1837
|
-
return "GET";
|
|
1838
|
-
}
|
|
1839
|
-
function endpointPathname(endpoint, basePath) {
|
|
1840
|
-
const prefix = normalizeS3ApiBasePath(basePath ?? S3_API_BASE_PATH);
|
|
1841
|
-
const path = endpoint.path ?? "/";
|
|
1842
|
-
const suffix = path.startsWith("/") ? path : `/${path}`;
|
|
1843
|
-
return `${prefix}${suffix}` || "/";
|
|
1844
|
-
}
|
|
1845
|
-
|
|
1846
|
-
// src/api/router.ts
|
|
1847
|
-
var NETWORK_CODES = /* @__PURE__ */ new Set([
|
|
1848
|
-
"EAI_AGAIN",
|
|
1849
|
-
"ECONNREFUSED",
|
|
1850
|
-
"ECONNRESET",
|
|
1851
|
-
"ETIMEDOUT",
|
|
1852
|
-
"ENOTFOUND"
|
|
1853
|
-
]);
|
|
1854
|
-
function onS3RouterError(error, config) {
|
|
1855
|
-
if (isAPIError(error)) return;
|
|
1856
|
-
const code = error?.code;
|
|
1857
|
-
const logger = resolveLogger(config?.logger);
|
|
1858
|
-
if (typeof code === "string" && NETWORK_CODES.has(code)) {
|
|
1859
|
-
const networkErr = errors.s3NetworkError(code);
|
|
1860
|
-
logger.error?.(networkErr.message, error);
|
|
1861
|
-
config?.onError?.(error, {});
|
|
1862
|
-
throw networkErr;
|
|
1863
|
-
}
|
|
1864
|
-
logger.error?.(error instanceof Error ? error.message : String(error), error);
|
|
1865
|
-
config?.onError?.(error, {});
|
|
1866
|
-
throw errors.internalError();
|
|
1867
|
-
}
|
|
1868
|
-
function withUnmatchedRouteJson(handler) {
|
|
1869
|
-
return async (request) => {
|
|
1870
|
-
const response = await handler(request);
|
|
1871
|
-
if (response.status !== 404) return response;
|
|
1872
|
-
const contentType = response.headers.get("content-type") ?? "";
|
|
1873
|
-
if (contentType.includes("application/json")) return response;
|
|
1874
|
-
return toResponse(errors.notFound());
|
|
2946
|
+
// src/plugin/chain-hooks.ts
|
|
2947
|
+
function chainHooks(...hooks) {
|
|
2948
|
+
return async (context) => {
|
|
2949
|
+
let unexpected;
|
|
2950
|
+
for (const hook of hooks) {
|
|
2951
|
+
if (!hook) continue;
|
|
2952
|
+
const result = await hook(context);
|
|
2953
|
+
if (result === void 0) continue;
|
|
2954
|
+
const locals = contextLocals(context);
|
|
2955
|
+
if (locals && isGuardResult(result)) {
|
|
2956
|
+
Object.assign(locals, result.locals);
|
|
2957
|
+
} else {
|
|
2958
|
+
unexpected ??= result;
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2961
|
+
return unexpected;
|
|
1875
2962
|
};
|
|
1876
2963
|
}
|
|
1877
|
-
function
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
basePath,
|
|
1883
|
-
routerContext: { config: env.config },
|
|
1884
|
-
openapi: { disabled: true },
|
|
1885
|
-
onError: (error) => onS3RouterError(error, env.config)
|
|
1886
|
-
});
|
|
1887
|
-
return {
|
|
1888
|
-
endpoints: bindEndpoints(router.endpoints, { config: env.config }),
|
|
1889
|
-
handler: withUnmatchedRouteJson(router.handler)
|
|
1890
|
-
};
|
|
2964
|
+
function contextLocals(context) {
|
|
2965
|
+
if (typeof context !== "object" || context === null) return void 0;
|
|
2966
|
+
if (!("locals" in context)) return void 0;
|
|
2967
|
+
const { locals } = context;
|
|
2968
|
+
return typeof locals === "object" && locals !== null ? locals : void 0;
|
|
1891
2969
|
}
|
|
1892
2970
|
|
|
1893
2971
|
// src/dimah-s3.ts
|
|
1894
2972
|
function dimahS3(config) {
|
|
1895
|
-
const
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
const api = {
|
|
1905
|
-
...bound,
|
|
1906
|
-
deleteMany: bound.deleteBatch,
|
|
1907
|
-
multipart: {
|
|
1908
|
-
init: bound.multipartInit,
|
|
1909
|
-
signPart: bound.multipartPart,
|
|
1910
|
-
listParts: bound.multipartListParts,
|
|
1911
|
-
complete: bound.multipartComplete,
|
|
1912
|
-
abort: bound.multipartAbort
|
|
1913
|
-
}
|
|
1914
|
-
};
|
|
2973
|
+
const resolved = resolveConfig(config);
|
|
2974
|
+
const { context, getPlugin, endpoints } = applyPlugins(
|
|
2975
|
+
config,
|
|
2976
|
+
resolved.logger
|
|
2977
|
+
);
|
|
2978
|
+
const router = createS3Router(
|
|
2979
|
+
{ ...coreEndpoints, ...endpoints },
|
|
2980
|
+
{ config, resolved }
|
|
2981
|
+
);
|
|
1915
2982
|
return {
|
|
1916
2983
|
handler: router.handler,
|
|
1917
|
-
api,
|
|
2984
|
+
api: createS3Api(resolved),
|
|
2985
|
+
put: (input, options) => putObject(resolved, input, options),
|
|
1918
2986
|
context,
|
|
1919
2987
|
getPlugin,
|
|
1920
2988
|
$ERROR_CODES: S3_ERROR_CODES,
|
|
1921
2989
|
$Infer: {},
|
|
1922
|
-
put: (input) => putObject(resolved, input),
|
|
1923
2990
|
...context
|
|
1924
2991
|
};
|
|
1925
2992
|
}
|
|
1926
2993
|
|
|
1927
|
-
// src/
|
|
1928
|
-
|
|
1929
|
-
|
|
2994
|
+
// src/route.ts
|
|
2995
|
+
function route(config) {
|
|
2996
|
+
return config;
|
|
2997
|
+
}
|
|
1930
2998
|
|
|
1931
|
-
export {
|
|
2999
|
+
export { chainHooks, dimahS3, route };
|
|
1932
3000
|
//# sourceMappingURL=index.js.map
|
|
1933
3001
|
//# sourceMappingURL=index.js.map
|