@edgestore/server 1.0.0-next.3 → 1.0.0-next.5
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 +2 -8
- package/agent-docs/README.md +20 -0
- package/agent-docs/astro.md +147 -0
- package/agent-docs/backend-client.md +284 -0
- package/agent-docs/configuration.md +300 -0
- package/agent-docs/express.md +124 -0
- package/agent-docs/fastify.md +123 -0
- package/agent-docs/hono.md +131 -0
- package/agent-docs/next.md +82 -0
- package/agent-docs/provider-azure-blob.md +85 -0
- package/agent-docs/provider-custom.md +281 -0
- package/agent-docs/provider-edgestore.md +53 -0
- package/agent-docs/provider-s3.md +270 -0
- package/agent-docs/remix.md +117 -0
- package/agent-docs/tanstack-start.md +99 -0
- package/agent-docs/troubleshooting.md +83 -0
- package/dist/adapters/astro/index.d.ts +2 -2
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +1 -2
- package/dist/adapters/dispatcher.d.ts +2 -3
- package/dist/adapters/dispatcher.d.ts.map +1 -1
- package/dist/adapters/dispatcher.js +17 -22
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +1 -5
- package/dist/adapters/fastify/index.d.ts +2 -2
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +1 -5
- package/dist/adapters/hono/index.d.ts +2 -2
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +1 -2
- package/dist/adapters/next/app/index.d.ts +2 -2
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +1 -2
- package/dist/adapters/next/pages/index.d.ts +2 -2
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +1 -5
- package/dist/adapters/remix/index.d.ts +2 -2
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +1 -2
- package/dist/adapters/shared.d.ts +45 -67
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +60 -97
- package/dist/adapters/start/index.d.ts +2 -2
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +1 -2
- package/dist/core/client/bucketClient.d.ts +0 -1
- package/dist/core/client/bucketClient.d.ts.map +1 -1
- package/dist/core/client/bucketClient.js +6 -28
- package/dist/core/client/index.d.ts +29 -26
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +2 -3
- package/dist/core/index.d.ts +2 -20
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -13
- package/dist/core/provider.d.ts +11 -1
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/provider.js +21 -1
- package/dist/core/router.d.ts +25 -0
- package/dist/core/router.d.ts.map +1 -0
- package/dist/core/router.js +49 -0
- package/dist/core/routerRules.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/providers/azure-blob/index.d.ts +7 -0
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +7 -0
- package/dist/providers/edgestore/index.d.ts +10 -11
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +26 -15
- package/dist/providers/s3/backendUpload.d.ts +13 -0
- package/dist/providers/s3/backendUpload.d.ts.map +1 -0
- package/dist/providers/s3/backendUpload.js +97 -0
- package/dist/providers/s3/index.d.ts +57 -93
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +241 -90
- package/dist/providers/s3/keys.d.ts +10 -0
- package/dist/providers/s3/keys.d.ts.map +1 -0
- package/dist/providers/s3/keys.js +40 -0
- package/dist/providers/s3/multipart.d.ts +25 -0
- package/dist/providers/s3/multipart.d.ts.map +1 -0
- package/dist/providers/s3/multipart.js +132 -0
- package/dist/providers/s3/objectHeaders.d.ts +4 -0
- package/dist/providers/s3/objectHeaders.d.ts.map +1 -0
- package/dist/providers/s3/objectHeaders.js +22 -0
- package/dist/providers/s3/options.d.ts +107 -0
- package/dist/providers/s3/options.d.ts.map +1 -0
- package/dist/version.d.ts +1 -1
- package/package.json +7 -5
- package/src/adapters/astro/index.ts +3 -4
- package/src/adapters/dispatcher.ts +21 -30
- package/src/adapters/express/index.ts +3 -9
- package/src/adapters/fastify/index.ts +3 -9
- package/src/adapters/hono/index.ts +3 -4
- package/src/adapters/next/app/index.ts +3 -4
- package/src/adapters/next/pages/index.ts +3 -9
- package/src/adapters/remix/index.ts +3 -4
- package/src/adapters/shared.ts +90 -163
- package/src/adapters/start/index.ts +3 -4
- package/src/core/client/bucketClient.ts +10 -41
- package/src/core/client/index.ts +32 -48
- package/src/core/index.ts +2 -36
- package/src/core/provider.ts +28 -5
- package/src/core/router.ts +97 -0
- package/src/index.ts +1 -3
- package/src/providers/azure-blob/index.ts +2 -0
- package/src/providers/edgestore/index.ts +28 -14
- package/src/providers/s3/backendUpload.ts +117 -0
- package/src/providers/s3/index.ts +302 -223
- package/src/providers/s3/keys.ts +59 -0
- package/src/providers/s3/multipart.ts +196 -0
- package/src/providers/s3/objectHeaders.ts +23 -0
- package/src/providers/s3/options.ts +125 -0
- package/src/version.ts +1 -1
|
@@ -1,140 +1,291 @@
|
|
|
1
|
-
import { S3Client,
|
|
1
|
+
import { S3Client, DeleteObjectsCommand, HeadObjectCommand, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
|
|
2
2
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
3
|
+
import { planMultipartUpload } from '@edgestore/sdk';
|
|
3
4
|
import { z } from 'zod';
|
|
4
5
|
import { defineProvider } from '../../core/provider.js';
|
|
5
6
|
import { getEnv } from '../../libs/env.js';
|
|
7
|
+
import { uploadObject } from './backendUpload.js';
|
|
8
|
+
import { createObjectKeys } from './keys.js';
|
|
9
|
+
import { createMultipartUploads } from './multipart.js';
|
|
10
|
+
import { objectUploadHeaders } from './objectHeaders.js';
|
|
6
11
|
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
function expiration(value) {
|
|
13
|
+
if (!Number.isInteger(value) || value < 1 || value > 604800) {
|
|
14
|
+
throw new RangeError('S3 signed URL expiration must be an integer between 1 and 604800 seconds.');
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
/** Multipart sessions are EdgeStore JWTs, not S3 URLs, so S3's cap does not apply. */ function sessionLifetime(value) {
|
|
19
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
20
|
+
throw new RangeError('S3 multipart session lifetime must be a positive integer number of seconds.');
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function s3(options = {}) {
|
|
25
|
+
const { credentials: configuredCredentials, accessKeyId = getEnv('ES_AWS_ACCESS_KEY_ID'), secretAccessKey = getEnv('ES_AWS_SECRET_ACCESS_KEY'), region = getEnv('ES_AWS_REGION'), bucketName = getEnv('ES_AWS_BUCKET_NAME'), endpoint = getEnv('ES_AWS_ENDPOINT'), forcePathStyle = getEnv('ES_AWS_FORCE_PATH_STYLE') === 'true' } = options;
|
|
26
|
+
const client = options.client ?? new S3Client({
|
|
15
27
|
region,
|
|
16
|
-
credentials,
|
|
17
28
|
endpoint,
|
|
18
|
-
forcePathStyle
|
|
29
|
+
forcePathStyle,
|
|
30
|
+
credentials: configuredCredentials ?? (accessKeyId && secretAccessKey ? {
|
|
31
|
+
accessKeyId,
|
|
32
|
+
secretAccessKey
|
|
33
|
+
} : undefined),
|
|
34
|
+
// Presigning without a body must not sign an empty-body optional checksum.
|
|
35
|
+
requestChecksumCalculation: 'WHEN_REQUIRED'
|
|
19
36
|
});
|
|
20
|
-
const
|
|
21
|
-
function
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
const baseUrl = options.baseUrl ?? getEnv('EDGE_STORE_BASE_URL') ?? (endpoint ? `${endpoint.replace(/\/+$/, '')}/${bucketName}` : region ? `https://${bucketName}.s3.${region}.amazonaws.com` : async ()=>`https://${bucketName}.s3.${await client.config.region()}.amazonaws.com`);
|
|
38
|
+
const keys = async ()=>createObjectKeys(typeof baseUrl === 'function' ? await baseUrl() : baseUrl);
|
|
39
|
+
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|
|
40
|
+
const readExpiresIn = expiration(options.signedUrlExpiresIn ?? 3600);
|
|
41
|
+
function bucket() {
|
|
42
|
+
if (!bucketName) throw new Error('S3 bucketName is not configured in S3ProviderOptions.');
|
|
43
|
+
return bucketName;
|
|
44
|
+
}
|
|
45
|
+
function plan(sizeBytes) {
|
|
46
|
+
if (!Number.isSafeInteger(sizeBytes) || sizeBytes < 0) throw new RangeError('S3 upload size must be a nonnegative safe integer.');
|
|
47
|
+
const result = planMultipartUpload({
|
|
48
|
+
sizeBytes,
|
|
49
|
+
thresholdBytes: options.multipart?.thresholdBytes,
|
|
50
|
+
preferredPartSizeBytes: options.multipart?.partSizeBytes,
|
|
51
|
+
forceMultipart: sizeBytes > 5 * 1024 ** 3
|
|
52
|
+
});
|
|
53
|
+
if (result && result.partSizeBytes > 5 * 1024 ** 3) throw new RangeError('S3 multipart parts cannot exceed 5 GiB.');
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
// Validate multipart settings even before the first large upload.
|
|
57
|
+
plan(0);
|
|
58
|
+
const multipart = createMultipartUploads({
|
|
59
|
+
client,
|
|
60
|
+
bucket,
|
|
61
|
+
partUrlExpiresIn: uploadExpiresIn,
|
|
62
|
+
sessionExpiresIn: sessionLifetime(options.multipart?.sessionExpiresIn ?? 86400),
|
|
63
|
+
audience: `edgestore:s3:${endpoint ?? region ?? 'aws'}:${bucketName}`,
|
|
64
|
+
secret: ()=>options.jwtSecret ?? getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY')
|
|
65
|
+
});
|
|
66
|
+
async function prepare({ bucketName: logicalBucket, fileInfo }) {
|
|
67
|
+
const physicalBucket = bucket();
|
|
68
|
+
const objectKeys = await keys();
|
|
69
|
+
const extension = fileInfo.extension ? `.${fileInfo.extension.replace(/^\./, '')}` : '';
|
|
70
|
+
const defaultPath = [
|
|
71
|
+
...fileInfo.isPublic ? [
|
|
72
|
+
'_public'
|
|
73
|
+
] : [],
|
|
74
|
+
...fileInfo.path.map((part)=>part.value),
|
|
75
|
+
fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`
|
|
76
|
+
].join('/');
|
|
77
|
+
const args = {
|
|
78
|
+
edgestoreBucketName: logicalBucket,
|
|
79
|
+
fileInfo,
|
|
80
|
+
defaultPath
|
|
81
|
+
};
|
|
82
|
+
const relativePath = objectKeys.normalizeRelativePath(options.path ? await options.path(args) : defaultPath);
|
|
83
|
+
const key = `${logicalBucket}/${relativePath}`;
|
|
84
|
+
const objectOptions = typeof options.objectOptions === 'function' ? await options.objectOptions(args) : options.objectOptions;
|
|
85
|
+
const input = {
|
|
86
|
+
...objectOptions,
|
|
87
|
+
Bucket: physicalBucket,
|
|
88
|
+
Key: key,
|
|
89
|
+
ContentLength: fileInfo.size,
|
|
90
|
+
ContentType: fileInfo.type || 'application/octet-stream'
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
input,
|
|
94
|
+
key,
|
|
95
|
+
url: objectKeys.toUrl(key),
|
|
96
|
+
plan: plan(fileInfo.size)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function signedRead(key, expiresIn = readExpiresIn) {
|
|
100
|
+
const ttl = expiration(expiresIn);
|
|
101
|
+
const signedUrl = await getSignedUrl(client, new GetObjectCommand({
|
|
102
|
+
Bucket: bucket(),
|
|
103
|
+
Key: key
|
|
104
|
+
}), {
|
|
105
|
+
expiresIn: ttl
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
url: (await keys()).toUrl(key),
|
|
109
|
+
signedUrl,
|
|
110
|
+
expiresAt: new Date(Date.now() + ttl * 1000),
|
|
111
|
+
expiresIn: ttl
|
|
112
|
+
};
|
|
24
113
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
throw new Error();
|
|
114
|
+
// Presigned browser uploads must not sign a checksum of the empty body.
|
|
115
|
+
let presignable;
|
|
116
|
+
function assertPresignableClient() {
|
|
117
|
+
return presignable ?? (presignable = (async ()=>{
|
|
118
|
+
const policy = options.client ? await client.config.requestChecksumCalculation?.() : undefined;
|
|
119
|
+
if (policy && policy !== 'WHEN_REQUIRED') {
|
|
120
|
+
throw new Error('An injected S3 client must use requestChecksumCalculation: "WHEN_REQUIRED" for browser uploads.');
|
|
32
121
|
}
|
|
33
|
-
|
|
34
|
-
} catch {
|
|
35
|
-
throw new Error('File URL does not belong to this S3 provider.');
|
|
36
|
-
}
|
|
37
|
-
if (!objectKey.startsWith(`${edgestoreBucketName}/`)) {
|
|
38
|
-
throw new Error(`File does not belong to EdgeStore bucket "${edgestoreBucketName}".`);
|
|
39
|
-
}
|
|
40
|
-
return objectKey;
|
|
122
|
+
})());
|
|
41
123
|
}
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
throw new Error('S3 paths must stay within the EdgeStore bucket prefix.');
|
|
46
|
-
}
|
|
47
|
-
return path;
|
|
124
|
+
async function readForUpload(key, params) {
|
|
125
|
+
if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
|
|
126
|
+
return signedRead(key, params.autoSignedUrls.expiresIn);
|
|
48
127
|
}
|
|
49
128
|
return defineProvider({
|
|
50
129
|
name: 's3',
|
|
51
130
|
baseUrl,
|
|
52
131
|
reference: {
|
|
53
|
-
schema: z.
|
|
54
|
-
|
|
55
|
-
|
|
132
|
+
schema: z.union([
|
|
133
|
+
z.object({
|
|
134
|
+
key: z.string().min(1)
|
|
135
|
+
}),
|
|
136
|
+
z.object({
|
|
137
|
+
url: z.string().url()
|
|
138
|
+
})
|
|
139
|
+
]),
|
|
56
140
|
fromUrl: (url)=>({
|
|
57
141
|
url
|
|
58
142
|
})
|
|
59
143
|
},
|
|
60
|
-
async init () {
|
|
144
|
+
async init ({ router }) {
|
|
145
|
+
bucket();
|
|
146
|
+
for (const logicalBucket of Object.values(router.buckets)){
|
|
147
|
+
if (logicalBucket._def.accessControl !== undefined && logicalBucket._def.accessControl !== 'private') {
|
|
148
|
+
throw new Error('S3 supports accessControl("private") with backend signed URLs, not cookie-based access-control rules.');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
61
151
|
return {};
|
|
62
152
|
},
|
|
63
153
|
uploads: {
|
|
154
|
+
supportedOptions: {
|
|
155
|
+
temporary: false,
|
|
156
|
+
replaceTargetUrl: false
|
|
157
|
+
},
|
|
64
158
|
async request (params) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
159
|
+
await assertPresignableClient();
|
|
160
|
+
const prepared = await prepare(params);
|
|
161
|
+
const read = await readForUpload(prepared.key, params);
|
|
162
|
+
const access = {
|
|
163
|
+
key: prepared.key,
|
|
164
|
+
accessUrl: prepared.url,
|
|
165
|
+
...read ? {
|
|
166
|
+
accessSignedUrl: read.signedUrl,
|
|
167
|
+
accessSignedUrlExpiresAt: read.expiresAt,
|
|
168
|
+
accessSignedUrlExpiresIn: read.expiresIn
|
|
169
|
+
} : {}
|
|
170
|
+
};
|
|
171
|
+
if (prepared.plan) {
|
|
172
|
+
return {
|
|
173
|
+
...access,
|
|
174
|
+
multipart: await multipart.request(prepared.input, prepared.plan)
|
|
175
|
+
};
|
|
68
176
|
}
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
177
|
+
const uploadHeaders = objectUploadHeaders(prepared.input);
|
|
178
|
+
return {
|
|
179
|
+
...access,
|
|
180
|
+
uploadUrl: await getSignedUrl(client, new PutObjectCommand(prepared.input), {
|
|
181
|
+
expiresIn: uploadExpiresIn,
|
|
182
|
+
signableHeaders: new Set([
|
|
183
|
+
'content-type'
|
|
184
|
+
]),
|
|
185
|
+
unhoistableHeaders: new Set(Object.keys(uploadHeaders).map((name)=>name.toLowerCase()))
|
|
186
|
+
}),
|
|
187
|
+
uploadHeaders
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
multipart: multipart.operations,
|
|
191
|
+
async upload (params) {
|
|
192
|
+
const prepared = await prepare({
|
|
193
|
+
...params,
|
|
194
|
+
fileInfo: {
|
|
195
|
+
...params.fileInfo,
|
|
196
|
+
size: params.source.size,
|
|
197
|
+
type: params.source.type || params.fileInfo.type
|
|
198
|
+
}
|
|
87
199
|
});
|
|
88
|
-
|
|
89
|
-
|
|
200
|
+
// Sign the read URL before sending bytes: nothing fallible may run
|
|
201
|
+
// after the object is committed.
|
|
202
|
+
const signedReadUrl = await readForUpload(prepared.key, params);
|
|
203
|
+
await uploadObject({
|
|
204
|
+
client,
|
|
205
|
+
input: prepared.input,
|
|
206
|
+
plan: prepared.plan,
|
|
207
|
+
source: params.source,
|
|
208
|
+
signal: params.signal,
|
|
209
|
+
onProgress: params.onProgress
|
|
90
210
|
});
|
|
211
|
+
// The object is committed. Describe it from what we sent, so a failed
|
|
212
|
+
// or canceled follow-up request cannot turn success into an error.
|
|
213
|
+
const uploadedAt = new Date();
|
|
91
214
|
return {
|
|
92
|
-
|
|
93
|
-
|
|
215
|
+
file: {
|
|
216
|
+
key: prepared.key,
|
|
217
|
+
url: prepared.url,
|
|
218
|
+
sizeBytes: params.source.size,
|
|
219
|
+
uploadedAt,
|
|
220
|
+
updatedAt: uploadedAt
|
|
221
|
+
},
|
|
222
|
+
signedReadUrl
|
|
94
223
|
};
|
|
95
224
|
}
|
|
96
225
|
},
|
|
97
226
|
files: {
|
|
98
|
-
async get ({ bucketName:
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
227
|
+
async get ({ bucketName: logicalBucket, file }) {
|
|
228
|
+
const objectKeys = await keys();
|
|
229
|
+
const key = objectKeys.fromReference(logicalBucket, file);
|
|
230
|
+
const { ContentLength, LastModified } = await client.send(new HeadObjectCommand({
|
|
231
|
+
Bucket: bucket(),
|
|
232
|
+
Key: key
|
|
103
233
|
}));
|
|
104
|
-
if (ContentLength === undefined || !LastModified)
|
|
105
|
-
throw new Error('File not found');
|
|
106
|
-
}
|
|
234
|
+
if (ContentLength === undefined || !LastModified) throw new Error('File not found');
|
|
107
235
|
return {
|
|
108
|
-
|
|
236
|
+
key,
|
|
237
|
+
url: objectKeys.toUrl(key),
|
|
109
238
|
sizeBytes: ContentLength,
|
|
110
239
|
uploadedAt: LastModified,
|
|
111
240
|
updatedAt: LastModified
|
|
112
241
|
};
|
|
113
242
|
},
|
|
114
|
-
async
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
243
|
+
async getSignedUrls ({ bucketName: logicalBucket, files, expiresIn }) {
|
|
244
|
+
const objectKeys = await keys();
|
|
245
|
+
const objectKeysToSign = files.map((file)=>objectKeys.fromReference(logicalBucket, file));
|
|
246
|
+
return Promise.all(objectKeysToSign.map((key)=>signedRead(key, expiresIn)));
|
|
247
|
+
},
|
|
248
|
+
async delete ({ bucketName: logicalBucket, files }) {
|
|
249
|
+
const physicalBucket = bucket();
|
|
250
|
+
const objectKeys = await keys();
|
|
251
|
+
const paths = files.map((file)=>objectKeys.fromReference(logicalBucket, file));
|
|
252
|
+
const results = [];
|
|
253
|
+
// DeleteObjects accepts up to 1,000 keys per request.
|
|
254
|
+
for(let start = 0; start < paths.length; start += 1000){
|
|
255
|
+
const batch = paths.slice(start, start + 1000);
|
|
256
|
+
const errors = new Map();
|
|
120
257
|
try {
|
|
121
|
-
await
|
|
122
|
-
Bucket:
|
|
123
|
-
|
|
258
|
+
const { Errors } = await client.send(new DeleteObjectsCommand({
|
|
259
|
+
Bucket: physicalBucket,
|
|
260
|
+
Delete: {
|
|
261
|
+
Objects: [
|
|
262
|
+
...new Set(batch)
|
|
263
|
+
].map((Key)=>({
|
|
264
|
+
Key
|
|
265
|
+
})),
|
|
266
|
+
Quiet: true
|
|
267
|
+
}
|
|
124
268
|
}));
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
269
|
+
for (const error of Errors ?? []){
|
|
270
|
+
if (error.Key) errors.set(error.Key, error.Message ?? 'Delete failed');
|
|
271
|
+
}
|
|
128
272
|
} catch (error) {
|
|
129
|
-
|
|
273
|
+
const message = error instanceof Error ? error.message : 'Delete failed';
|
|
274
|
+
for (const key of batch)errors.set(key, message);
|
|
275
|
+
}
|
|
276
|
+
for (const key of batch){
|
|
277
|
+
const message = errors.get(key);
|
|
278
|
+
results.push(message === undefined ? {
|
|
279
|
+
success: true
|
|
280
|
+
} : {
|
|
130
281
|
success: false,
|
|
131
282
|
error: {
|
|
132
283
|
code: 'DELETE_FAILED',
|
|
133
|
-
message
|
|
284
|
+
message
|
|
134
285
|
}
|
|
135
|
-
};
|
|
286
|
+
});
|
|
136
287
|
}
|
|
137
|
-
}
|
|
288
|
+
}
|
|
138
289
|
return {
|
|
139
290
|
results
|
|
140
291
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function createObjectKeys(baseUrl: string): {
|
|
2
|
+
toUrl: (objectKey: string) => string;
|
|
3
|
+
fromReference: (bucketName: string, file: {
|
|
4
|
+
url: string;
|
|
5
|
+
} | {
|
|
6
|
+
key: string;
|
|
7
|
+
}) => string;
|
|
8
|
+
normalizeRelativePath: (value: string) => string;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../../src/providers/s3/keys.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM;uBAGX,MAAM;gCAuC3B,MAAM,QACZ;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE;mCAdH,MAAM;EA6B7C"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
function createObjectKeys(baseUrl) {
|
|
2
|
+
const normalizedBaseUrl = baseUrl.replace(/\/+$/, '');
|
|
3
|
+
function objectKeyToUrl(objectKey) {
|
|
4
|
+
const encodedKey = objectKey.split('/').map((segment)=>encodeURIComponent(segment)).join('/');
|
|
5
|
+
return `${normalizedBaseUrl}/${encodedKey}`;
|
|
6
|
+
}
|
|
7
|
+
function urlToObjectKey(url) {
|
|
8
|
+
try {
|
|
9
|
+
const fileUrl = new URL(url);
|
|
10
|
+
const providerUrl = new URL(`${normalizedBaseUrl}/`);
|
|
11
|
+
if (fileUrl.origin !== providerUrl.origin || !fileUrl.pathname.startsWith(providerUrl.pathname)) {
|
|
12
|
+
throw new Error();
|
|
13
|
+
}
|
|
14
|
+
return decodeURIComponent(fileUrl.pathname.slice(providerUrl.pathname.length));
|
|
15
|
+
} catch {
|
|
16
|
+
throw new Error('File URL does not belong to this S3 provider.');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function normalizeRelativePath(value) {
|
|
20
|
+
const path = value.replace(/^\/+|\/+$/g, '');
|
|
21
|
+
if (path.length === 0 || path.split('/').some((segment)=>segment === '.' || segment === '..')) {
|
|
22
|
+
throw new Error('S3 paths must stay within the EdgeStore bucket prefix.');
|
|
23
|
+
}
|
|
24
|
+
return path;
|
|
25
|
+
}
|
|
26
|
+
/** Keys from both URLs and key references must stay inside the logical bucket. */ function fromReference(bucketName, file) {
|
|
27
|
+
const key = 'url' in file ? urlToObjectKey(file.url) : file.key;
|
|
28
|
+
if (normalizeRelativePath(key) !== key || !key.startsWith(`${bucketName}/`)) {
|
|
29
|
+
throw new Error(`File does not belong to EdgeStore bucket "${bucketName}".`);
|
|
30
|
+
}
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
toUrl: objectKeyToUrl,
|
|
35
|
+
fromReference,
|
|
36
|
+
normalizeRelativePath
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { createObjectKeys };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PutObjectCommandInput, type S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import { type MultipartUploadPlan } from '@edgestore/sdk';
|
|
3
|
+
import { type ProviderMultipartUploads } from '@edgestore/shared';
|
|
4
|
+
/** The signed upload ID is a bearer capability scoped to one authorized object. */
|
|
5
|
+
export declare function createMultipartUploads({ client, bucket, audience, secret, partUrlExpiresIn, sessionExpiresIn, }: {
|
|
6
|
+
client: S3Client;
|
|
7
|
+
bucket: () => string;
|
|
8
|
+
audience: string;
|
|
9
|
+
secret: () => string | undefined;
|
|
10
|
+
partUrlExpiresIn: number;
|
|
11
|
+
sessionExpiresIn: number;
|
|
12
|
+
}): {
|
|
13
|
+
operations: ProviderMultipartUploads;
|
|
14
|
+
request(input: PutObjectCommandInput, plan: MultipartUploadPlan): Promise<{
|
|
15
|
+
key: string;
|
|
16
|
+
uploadId: string;
|
|
17
|
+
partSize: number;
|
|
18
|
+
totalParts: number;
|
|
19
|
+
parts: {
|
|
20
|
+
partNumber: number;
|
|
21
|
+
uploadUrl: string;
|
|
22
|
+
}[];
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=multipart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.d.ts","sourceRoot":"","sources":["../../../src/providers/s3/multipart.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,qBAAqB,EAC1B,KAAK,QAAQ,EACd,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,mBAAmB,CAAC;AAc3B,mFAAmF;AACnF,wBAAgB,sBAAsB,CAAC,EACrC,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,gBAAgB,EAChB,gBAAgB,GACjB,EAAE;IACD,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,EAAE,MAAM,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;IACjC,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;;mBAiHwB,qBAAqB,QAAQ,mBAAmB;;;;;;;;;;EAwCxE"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { CreateMultipartUploadCommand, CompleteMultipartUploadCommand, UploadPartCommand, AbortMultipartUploadCommand } from '@aws-sdk/client-s3';
|
|
2
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
3
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
4
|
+
import { SignJWT, jwtVerify } from 'jose';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { INITIAL_MULTIPART_PART_URLS } from '../../core/provider.js';
|
|
7
|
+
|
|
8
|
+
const sessionSchema = z.object({
|
|
9
|
+
key: z.string(),
|
|
10
|
+
uploadId: z.string(),
|
|
11
|
+
size: z.number().int().nonnegative(),
|
|
12
|
+
partSize: z.number().int().positive(),
|
|
13
|
+
totalParts: z.number().int().min(1).max(10000)
|
|
14
|
+
});
|
|
15
|
+
/** The signed upload ID is a bearer capability scoped to one authorized object. */ function createMultipartUploads({ client, bucket, audience, secret, partUrlExpiresIn, sessionExpiresIn }) {
|
|
16
|
+
function signingKey() {
|
|
17
|
+
const value = secret();
|
|
18
|
+
if (!value) throw new Error('S3 multipart uploads require jwtSecret or EDGE_STORE_JWT_SECRET.');
|
|
19
|
+
return new TextEncoder().encode(value);
|
|
20
|
+
}
|
|
21
|
+
async function readSession(token, key) {
|
|
22
|
+
try {
|
|
23
|
+
const { payload } = await jwtVerify(token, signingKey(), {
|
|
24
|
+
algorithms: [
|
|
25
|
+
'HS256'
|
|
26
|
+
],
|
|
27
|
+
audience
|
|
28
|
+
});
|
|
29
|
+
const session = sessionSchema.parse(payload);
|
|
30
|
+
if (session.key !== key) throw new Error('Wrong object key');
|
|
31
|
+
return session;
|
|
32
|
+
} catch {
|
|
33
|
+
throw new EdgeStoreError({
|
|
34
|
+
code: 'BAD_REQUEST',
|
|
35
|
+
message: 'Invalid or expired S3 multipart upload session.'
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function validateParts(session, parts, complete = false) {
|
|
40
|
+
if (!parts.length || new Set(parts).size !== parts.length || parts.some((part)=>!Number.isInteger(part) || part < 1 || part > session.totalParts) || complete && parts.length !== session.totalParts) {
|
|
41
|
+
throw new EdgeStoreError({
|
|
42
|
+
code: 'BAD_REQUEST',
|
|
43
|
+
message: 'Invalid S3 multipart part numbers.'
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function abort(session) {
|
|
48
|
+
await client.send(new AbortMultipartUploadCommand({
|
|
49
|
+
Bucket: bucket(),
|
|
50
|
+
Key: session.key,
|
|
51
|
+
UploadId: session.uploadId
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
async function signParts(session, parts) {
|
|
55
|
+
validateParts(session, parts);
|
|
56
|
+
return Promise.all(parts.map(async (partNumber)=>({
|
|
57
|
+
partNumber,
|
|
58
|
+
uploadUrl: await getSignedUrl(client, new UploadPartCommand({
|
|
59
|
+
Bucket: bucket(),
|
|
60
|
+
Key: session.key,
|
|
61
|
+
UploadId: session.uploadId,
|
|
62
|
+
PartNumber: partNumber,
|
|
63
|
+
ContentLength: Math.min(session.partSize, session.size - (partNumber - 1) * session.partSize)
|
|
64
|
+
}), {
|
|
65
|
+
expiresIn: partUrlExpiresIn
|
|
66
|
+
})
|
|
67
|
+
})));
|
|
68
|
+
}
|
|
69
|
+
const operations = {
|
|
70
|
+
async requestParts ({ uploadId, key, parts }) {
|
|
71
|
+
return {
|
|
72
|
+
parts: await signParts(await readSession(uploadId, key), parts)
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
async complete ({ uploadId, key, parts }) {
|
|
76
|
+
const session = await readSession(uploadId, key);
|
|
77
|
+
validateParts(session, parts.map((part)=>part.partNumber), true);
|
|
78
|
+
await client.send(new CompleteMultipartUploadCommand({
|
|
79
|
+
Bucket: bucket(),
|
|
80
|
+
Key: key,
|
|
81
|
+
UploadId: session.uploadId,
|
|
82
|
+
MultipartUpload: {
|
|
83
|
+
Parts: [
|
|
84
|
+
...parts
|
|
85
|
+
].sort((a, b)=>a.partNumber - b.partNumber).map((part)=>({
|
|
86
|
+
PartNumber: part.partNumber,
|
|
87
|
+
ETag: part.eTag
|
|
88
|
+
}))
|
|
89
|
+
}
|
|
90
|
+
}));
|
|
91
|
+
},
|
|
92
|
+
async abort ({ uploadId, key }) {
|
|
93
|
+
await abort(await readSession(uploadId, key));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
operations,
|
|
98
|
+
async request (input, plan) {
|
|
99
|
+
// Validate signing configuration before allocating storage resources.
|
|
100
|
+
const key = signingKey();
|
|
101
|
+
const { ContentLength, ...objectInput } = input;
|
|
102
|
+
const result = await client.send(new CreateMultipartUploadCommand(objectInput));
|
|
103
|
+
if (!result.UploadId || !input.Key || ContentLength === undefined) {
|
|
104
|
+
throw new Error('S3 did not return a multipart upload ID.');
|
|
105
|
+
}
|
|
106
|
+
const session = {
|
|
107
|
+
key: input.Key,
|
|
108
|
+
uploadId: result.UploadId,
|
|
109
|
+
size: ContentLength,
|
|
110
|
+
partSize: plan.partSizeBytes,
|
|
111
|
+
totalParts: plan.totalParts
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
const uploadId = await new SignJWT(session).setProtectedHeader({
|
|
115
|
+
alg: 'HS256'
|
|
116
|
+
}).setAudience(audience).setIssuedAt().setExpirationTime(`${sessionExpiresIn}s`).sign(key);
|
|
117
|
+
return {
|
|
118
|
+
key: session.key,
|
|
119
|
+
uploadId,
|
|
120
|
+
partSize: session.partSize,
|
|
121
|
+
totalParts: session.totalParts,
|
|
122
|
+
parts: await signParts(session, plan.partNumbers.slice(0, INITIAL_MULTIPART_PART_URLS))
|
|
123
|
+
};
|
|
124
|
+
} catch (error) {
|
|
125
|
+
await abort(session).catch(()=>undefined);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export { createMultipartUploads };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PutObjectCommandInput } from '@aws-sdk/client-s3';
|
|
2
|
+
/** Keep signed object settings and the browser's actual PUT headers aligned. */
|
|
3
|
+
export declare function objectUploadHeaders(input: PutObjectCommandInput): Record<string, string>;
|
|
4
|
+
//# sourceMappingURL=objectHeaders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objectHeaders.d.ts","sourceRoot":"","sources":["../../../src/providers/s3/objectHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,gFAAgF;AAChF,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,qBAAqB,0BAmB/D"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Keep signed object settings and the browser's actual PUT headers aligned. */ function objectUploadHeaders(input) {
|
|
2
|
+
const headers = {};
|
|
3
|
+
const fields = {
|
|
4
|
+
ContentType: 'Content-Type',
|
|
5
|
+
CacheControl: 'Cache-Control',
|
|
6
|
+
ContentDisposition: 'Content-Disposition',
|
|
7
|
+
StorageClass: 'x-amz-storage-class',
|
|
8
|
+
ServerSideEncryption: 'x-amz-server-side-encryption',
|
|
9
|
+
SSEKMSKeyId: 'x-amz-server-side-encryption-aws-kms-key-id',
|
|
10
|
+
Tagging: 'x-amz-tagging'
|
|
11
|
+
};
|
|
12
|
+
for (const field of Object.keys(fields)){
|
|
13
|
+
const value = input[field];
|
|
14
|
+
if (value !== undefined) headers[fields[field]] = value;
|
|
15
|
+
}
|
|
16
|
+
for (const [key, value] of Object.entries(input.Metadata ?? {})){
|
|
17
|
+
headers[`x-amz-meta-${key}`] = value;
|
|
18
|
+
}
|
|
19
|
+
return headers;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { objectUploadHeaders };
|