@edgestore/server 1.0.0-next.5 → 1.0.0-rc.6
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/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +1 -1
- package/agent-docs/backend-client.md +5 -8
- package/agent-docs/express.md +1 -1
- package/agent-docs/fastify.md +1 -1
- package/agent-docs/hono.md +1 -1
- package/agent-docs/next.md +1 -1
- package/agent-docs/provider-azure-blob.md +150 -57
- package/agent-docs/provider-custom.md +17 -3
- package/agent-docs/provider-s3.md +4 -3
- package/agent-docs/remix.md +1 -1
- package/agent-docs/tanstack-start.md +1 -1
- package/dist/adapters/shared.d.ts +12 -102
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +63 -108
- package/dist/core/client/index.d.ts +0 -4
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/provider.d.ts +0 -1
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/provider.js +1 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/providers/azure-blob/blocks.d.ts +48 -0
- package/dist/providers/azure-blob/blocks.d.ts.map +1 -0
- package/dist/providers/azure-blob/blocks.js +126 -0
- package/dist/providers/azure-blob/index.d.ts +76 -51
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +189 -107
- package/dist/providers/edgestore/index.d.ts +6 -22
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +19 -18
- package/dist/providers/s3/backendUpload.d.ts +2 -5
- package/dist/providers/s3/backendUpload.d.ts.map +1 -1
- package/dist/providers/s3/backendUpload.js +19 -57
- package/dist/providers/s3/index.d.ts +3 -12
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +23 -44
- package/dist/providers/s3/multipart.d.ts +3 -5
- package/dist/providers/s3/multipart.d.ts.map +1 -1
- package/dist/providers/s3/multipart.js +26 -62
- package/dist/providers/s3/options.d.ts +10 -32
- package/dist/providers/s3/options.d.ts.map +1 -1
- package/dist/providers/storage/multipartSession.d.ts +51 -0
- package/dist/providers/storage/multipartSession.d.ts.map +1 -0
- package/dist/providers/storage/multipartSession.js +83 -0
- package/dist/providers/storage/objectKeys.d.ts +41 -0
- package/dist/providers/storage/objectKeys.d.ts.map +1 -0
- package/dist/providers/{s3/keys.js → storage/objectKeys.js} +31 -4
- package/dist/providers/storage/transfer.d.ts +18 -0
- package/dist/providers/storage/transfer.d.ts.map +1 -0
- package/dist/providers/storage/transfer.js +69 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +4 -11
- package/src/adapters/shared.ts +81 -127
- package/src/core/client/index.ts +0 -4
- package/src/core/provider.ts +0 -8
- package/src/index.ts +3 -8
- package/src/providers/azure-blob/blocks.ts +201 -0
- package/src/providers/azure-blob/index.ts +289 -171
- package/src/providers/edgestore/index.ts +19 -22
- package/src/providers/s3/backendUpload.ts +20 -55
- package/src/providers/s3/index.ts +31 -58
- package/src/providers/s3/multipart.ts +36 -94
- package/src/providers/s3/options.ts +10 -33
- package/src/providers/storage/multipartSession.ts +145 -0
- package/src/providers/storage/objectKeys.ts +106 -0
- package/src/providers/storage/transfer.ts +105 -0
- package/src/version.ts +1 -1
- package/core/index.d.ts +0 -1
- package/core/index.js +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -3
- package/dist/providers/s3/keys.d.ts +0 -10
- package/dist/providers/s3/keys.d.ts.map +0 -1
- package/src/core/index.ts +0 -9
- package/src/providers/s3/keys.ts +0 -59
|
@@ -7,16 +7,11 @@ import {
|
|
|
7
7
|
type PutObjectCommandInput,
|
|
8
8
|
type S3Client,
|
|
9
9
|
} from '@aws-sdk/client-s3';
|
|
10
|
-
import {
|
|
11
|
-
DEFAULT_MULTIPART_CONCURRENCY,
|
|
12
|
-
type MultipartUploadPlan,
|
|
13
|
-
} from '@edgestore/sdk';
|
|
10
|
+
import type { MultipartUploadPlan } from '@edgestore/sdk';
|
|
14
11
|
import type { BackendUploadParams } from '@edgestore/shared';
|
|
12
|
+
import { createProgress, readPart, runConcurrently } from '../storage/transfer';
|
|
15
13
|
|
|
16
|
-
/**
|
|
17
|
-
* Uploads with the configured S3 client, which owns request retries. Multipart
|
|
18
|
-
* parts are read and sent a few at a time to bound memory use.
|
|
19
|
-
*/
|
|
14
|
+
/** Uploads with the configured S3 client, which owns request retries. */
|
|
20
15
|
export async function uploadObject({
|
|
21
16
|
client,
|
|
22
17
|
input,
|
|
@@ -30,17 +25,7 @@ export async function uploadObject({
|
|
|
30
25
|
plan: MultipartUploadPlan | null;
|
|
31
26
|
} & Pick<BackendUploadParams, 'source' | 'signal' | 'onProgress'>) {
|
|
32
27
|
signal?.throwIfAborted();
|
|
33
|
-
|
|
34
|
-
const progress = (bytes: number) => {
|
|
35
|
-
transferredBytes += bytes;
|
|
36
|
-
onProgress?.({
|
|
37
|
-
transferredBytes,
|
|
38
|
-
totalBytes: source.size,
|
|
39
|
-
percentage: source.size ? (transferredBytes / source.size) * 100 : 100,
|
|
40
|
-
phase: 'uploading',
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
progress(0);
|
|
28
|
+
const progress = createProgress(source.size, onProgress);
|
|
44
29
|
if (!plan) {
|
|
45
30
|
await client.send(
|
|
46
31
|
new PutObjectCommand({
|
|
@@ -60,46 +45,26 @@ export async function uploadObject({
|
|
|
60
45
|
);
|
|
61
46
|
if (!UploadId) throw new Error('S3 did not return a multipart upload ID.');
|
|
62
47
|
const session = { Bucket: input.Bucket, Key: input.Key, UploadId };
|
|
63
|
-
const controller = new AbortController();
|
|
64
|
-
const partSignal = signal
|
|
65
|
-
? AbortSignal.any([signal, controller.signal])
|
|
66
|
-
: controller.signal;
|
|
67
|
-
|
|
68
|
-
const uploadPart = async (partNumber: number) => {
|
|
69
|
-
const start = (partNumber - 1) * plan.partSizeBytes;
|
|
70
|
-
const body = new Uint8Array(
|
|
71
|
-
await source.slice(start, start + plan.partSizeBytes).arrayBuffer(),
|
|
72
|
-
);
|
|
73
|
-
const { ETag } = await client.send(
|
|
74
|
-
new UploadPartCommand({ ...session, PartNumber: partNumber, Body: body }),
|
|
75
|
-
{ abortSignal: partSignal },
|
|
76
|
-
);
|
|
77
|
-
if (!ETag) throw new Error('Missing S3 multipart ETag.');
|
|
78
|
-
progress(body.byteLength);
|
|
79
|
-
return { PartNumber: partNumber, ETag };
|
|
80
|
-
};
|
|
81
48
|
|
|
82
49
|
try {
|
|
83
|
-
const parts =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
50
|
+
const parts = await runConcurrently(
|
|
51
|
+
plan.partNumbers,
|
|
52
|
+
signal,
|
|
53
|
+
async (partNumber, partSignal) => {
|
|
54
|
+
const body = await readPart(source, partNumber, plan.partSizeBytes);
|
|
55
|
+
const { ETag } = await client.send(
|
|
56
|
+
new UploadPartCommand({
|
|
57
|
+
...session,
|
|
58
|
+
PartNumber: partNumber,
|
|
59
|
+
Body: body,
|
|
60
|
+
}),
|
|
61
|
+
{ abortSignal: partSignal },
|
|
62
|
+
);
|
|
63
|
+
if (!ETag) throw new Error('Missing S3 multipart ETag.');
|
|
64
|
+
progress(body.byteLength);
|
|
65
|
+
return { PartNumber: partNumber, ETag };
|
|
93
66
|
},
|
|
94
67
|
);
|
|
95
|
-
try {
|
|
96
|
-
await Promise.all(workers);
|
|
97
|
-
} catch (error) {
|
|
98
|
-
controller.abort(error);
|
|
99
|
-
await Promise.allSettled(workers);
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
102
|
-
signal?.throwIfAborted();
|
|
103
68
|
await client.send(
|
|
104
69
|
new CompleteMultipartUploadCommand({
|
|
105
70
|
...session,
|
|
@@ -15,8 +15,13 @@ import {
|
|
|
15
15
|
import { z } from 'zod';
|
|
16
16
|
import { defineProvider } from '../../core/provider';
|
|
17
17
|
import { getEnv } from '../../libs/env';
|
|
18
|
+
import { createMultipartSessions } from '../storage/multipartSession';
|
|
19
|
+
import { createObjectKeys } from '../storage/objectKeys';
|
|
20
|
+
import {
|
|
21
|
+
assertSignedUrlAccessControl,
|
|
22
|
+
signedUrlLifetime,
|
|
23
|
+
} from '../storage/transfer';
|
|
18
24
|
import { uploadObject } from './backendUpload';
|
|
19
|
-
import { createObjectKeys } from './keys';
|
|
20
25
|
import { createMultipartUploads } from './multipart';
|
|
21
26
|
import { objectUploadHeaders } from './objectHeaders';
|
|
22
27
|
import type { S3ProviderOptions } from './options';
|
|
@@ -28,35 +33,18 @@ export type {
|
|
|
28
33
|
S3ProviderOptions,
|
|
29
34
|
} from './options';
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
if (!Number.isInteger(value) || value < 1 || value > 604800) {
|
|
33
|
-
throw new RangeError(
|
|
34
|
-
'S3 signed URL expiration must be an integer between 1 and 604800 seconds.',
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Multipart sessions are EdgeStore JWTs, not S3 URLs, so S3's cap does not apply. */
|
|
41
|
-
function sessionLifetime(value: number) {
|
|
42
|
-
if (!Number.isSafeInteger(value) || value < 1) {
|
|
43
|
-
throw new RangeError(
|
|
44
|
-
'S3 multipart session lifetime must be a positive integer number of seconds.',
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
36
|
+
const expiration = (value: number) => signedUrlLifetime('S3', value);
|
|
49
37
|
|
|
50
38
|
export function s3(options: S3ProviderOptions = {}) {
|
|
51
39
|
const {
|
|
52
40
|
credentials: configuredCredentials,
|
|
53
|
-
accessKeyId = getEnv('ES_AWS_ACCESS_KEY_ID'),
|
|
54
|
-
secretAccessKey = getEnv('ES_AWS_SECRET_ACCESS_KEY'),
|
|
55
41
|
region = getEnv('ES_AWS_REGION'),
|
|
56
42
|
bucketName = getEnv('ES_AWS_BUCKET_NAME'),
|
|
57
43
|
endpoint = getEnv('ES_AWS_ENDPOINT'),
|
|
58
44
|
forcePathStyle = getEnv('ES_AWS_FORCE_PATH_STYLE') === 'true',
|
|
59
45
|
} = options;
|
|
46
|
+
const accessKeyId = getEnv('ES_AWS_ACCESS_KEY_ID');
|
|
47
|
+
const secretAccessKey = getEnv('ES_AWS_SECRET_ACCESS_KEY');
|
|
60
48
|
const client =
|
|
61
49
|
options.client ??
|
|
62
50
|
new S3Client({
|
|
@@ -81,7 +69,10 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
81
69
|
: async () =>
|
|
82
70
|
`https://${bucketName}.s3.${await client.config.region()}.amazonaws.com`);
|
|
83
71
|
const keys = async () =>
|
|
84
|
-
createObjectKeys(
|
|
72
|
+
createObjectKeys(
|
|
73
|
+
'S3',
|
|
74
|
+
typeof baseUrl === 'function' ? await baseUrl() : baseUrl,
|
|
75
|
+
);
|
|
85
76
|
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|
|
86
77
|
const readExpiresIn = expiration(options.signedUrlExpiresIn ?? 3600);
|
|
87
78
|
|
|
@@ -111,14 +102,12 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
111
102
|
client,
|
|
112
103
|
bucket,
|
|
113
104
|
partUrlExpiresIn: uploadExpiresIn,
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
getEnv('EDGE_STORE_JWT_SECRET') ??
|
|
121
|
-
getEnv('EDGE_STORE_SECRET_KEY'),
|
|
105
|
+
sessions: createMultipartSessions({
|
|
106
|
+
providerName: 'S3',
|
|
107
|
+
audience: `edgestore:s3:${endpoint ?? region ?? 'aws'}:${bucketName}`,
|
|
108
|
+
secret: options.jwtSecret,
|
|
109
|
+
expiresIn: options.multipart?.sessionExpiresIn ?? 86400,
|
|
110
|
+
}),
|
|
122
111
|
});
|
|
123
112
|
|
|
124
113
|
async function prepare({
|
|
@@ -127,22 +116,14 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
127
116
|
}: RequestUploadParams) {
|
|
128
117
|
const physicalBucket = bucket();
|
|
129
118
|
const objectKeys = await keys();
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
...(fileInfo.isPublic ? ['_public'] : []),
|
|
135
|
-
...fileInfo.path.map((part) => part.value),
|
|
136
|
-
fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`,
|
|
137
|
-
].join('/');
|
|
138
|
-
const args = { edgestoreBucketName: logicalBucket, fileInfo, defaultPath };
|
|
139
|
-
const relativePath = objectKeys.normalizeRelativePath(
|
|
140
|
-
options.path ? await options.path(args) : defaultPath,
|
|
119
|
+
const { key, pathArgs } = await objectKeys.forUpload(
|
|
120
|
+
logicalBucket,
|
|
121
|
+
fileInfo,
|
|
122
|
+
options.path,
|
|
141
123
|
);
|
|
142
|
-
const key = `${logicalBucket}/${relativePath}`;
|
|
143
124
|
const objectOptions =
|
|
144
125
|
typeof options.objectOptions === 'function'
|
|
145
|
-
? await options.objectOptions(
|
|
126
|
+
? await options.objectOptions(pathArgs)
|
|
146
127
|
: options.objectOptions;
|
|
147
128
|
const input: PutObjectCommandInput = {
|
|
148
129
|
...objectOptions,
|
|
@@ -195,7 +176,6 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
195
176
|
|
|
196
177
|
return defineProvider({
|
|
197
178
|
name: 's3',
|
|
198
|
-
baseUrl,
|
|
199
179
|
reference: {
|
|
200
180
|
schema: z.union([
|
|
201
181
|
z.object({ key: z.string().min(1) }),
|
|
@@ -205,16 +185,7 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
205
185
|
},
|
|
206
186
|
async init({ router }) {
|
|
207
187
|
bucket();
|
|
208
|
-
|
|
209
|
-
if (
|
|
210
|
-
logicalBucket._def.accessControl !== undefined &&
|
|
211
|
-
logicalBucket._def.accessControl !== 'private'
|
|
212
|
-
) {
|
|
213
|
-
throw new Error(
|
|
214
|
-
'S3 supports accessControl("private") with backend signed URLs, not cookie-based access-control rules.',
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
188
|
+
assertSignedUrlAccessControl('S3', router);
|
|
218
189
|
return {};
|
|
219
190
|
},
|
|
220
191
|
uploads: {
|
|
@@ -225,12 +196,14 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
225
196
|
const read = await readForUpload(prepared.key, params);
|
|
226
197
|
const access = {
|
|
227
198
|
key: prepared.key,
|
|
228
|
-
|
|
199
|
+
url: prepared.url,
|
|
229
200
|
...(read
|
|
230
201
|
? {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
202
|
+
signedReadUrl: {
|
|
203
|
+
signedUrl: read.signedUrl,
|
|
204
|
+
expiresAt: read.expiresAt,
|
|
205
|
+
expiresIn: read.expiresIn,
|
|
206
|
+
},
|
|
234
207
|
}
|
|
235
208
|
: {}),
|
|
236
209
|
};
|
|
@@ -12,90 +12,38 @@ import {
|
|
|
12
12
|
EdgeStoreError,
|
|
13
13
|
type ProviderMultipartUploads,
|
|
14
14
|
} from '@edgestore/shared';
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
partSize: z.number().int().positive(),
|
|
24
|
-
totalParts: z.number().int().min(1).max(10_000),
|
|
25
|
-
});
|
|
26
|
-
type Session = z.infer<typeof sessionSchema>;
|
|
15
|
+
import {
|
|
16
|
+
assertValidParts,
|
|
17
|
+
initialPartNumbers,
|
|
18
|
+
partLength,
|
|
19
|
+
sessionFromPlan,
|
|
20
|
+
type createMultipartSessions,
|
|
21
|
+
type MultipartSession,
|
|
22
|
+
} from '../storage/multipartSession';
|
|
27
23
|
|
|
28
|
-
/** The signed upload ID is a bearer capability scoped to one authorized object. */
|
|
29
24
|
export function createMultipartUploads({
|
|
30
25
|
client,
|
|
31
26
|
bucket,
|
|
32
|
-
|
|
33
|
-
secret,
|
|
27
|
+
sessions,
|
|
34
28
|
partUrlExpiresIn,
|
|
35
|
-
sessionExpiresIn,
|
|
36
29
|
}: {
|
|
37
30
|
client: S3Client;
|
|
38
31
|
bucket: () => string;
|
|
39
|
-
|
|
40
|
-
secret: () => string | undefined;
|
|
32
|
+
sessions: ReturnType<typeof createMultipartSessions>;
|
|
41
33
|
partUrlExpiresIn: number;
|
|
42
|
-
sessionExpiresIn: number;
|
|
43
34
|
}) {
|
|
44
|
-
function
|
|
45
|
-
const value = secret();
|
|
46
|
-
if (!value)
|
|
47
|
-
throw new Error(
|
|
48
|
-
'S3 multipart uploads require jwtSecret or EDGE_STORE_JWT_SECRET.',
|
|
49
|
-
);
|
|
50
|
-
return new TextEncoder().encode(value);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function readSession(token: string, key: string) {
|
|
54
|
-
try {
|
|
55
|
-
const { payload } = await jwtVerify(token, signingKey(), {
|
|
56
|
-
algorithms: ['HS256'],
|
|
57
|
-
audience,
|
|
58
|
-
});
|
|
59
|
-
const session = sessionSchema.parse(payload);
|
|
60
|
-
if (session.key !== key) throw new Error('Wrong object key');
|
|
61
|
-
return session;
|
|
62
|
-
} catch {
|
|
63
|
-
throw new EdgeStoreError({
|
|
64
|
-
code: 'BAD_REQUEST',
|
|
65
|
-
message: 'Invalid or expired S3 multipart upload session.',
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function validateParts(session: Session, parts: number[], complete = false) {
|
|
71
|
-
if (
|
|
72
|
-
!parts.length ||
|
|
73
|
-
new Set(parts).size !== parts.length ||
|
|
74
|
-
parts.some(
|
|
75
|
-
(part) =>
|
|
76
|
-
!Number.isInteger(part) || part < 1 || part > session.totalParts,
|
|
77
|
-
) ||
|
|
78
|
-
(complete && parts.length !== session.totalParts)
|
|
79
|
-
) {
|
|
80
|
-
throw new EdgeStoreError({
|
|
81
|
-
code: 'BAD_REQUEST',
|
|
82
|
-
message: 'Invalid S3 multipart part numbers.',
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function abort(session: Session) {
|
|
35
|
+
async function abort(session: MultipartSession) {
|
|
88
36
|
await client.send(
|
|
89
37
|
new AbortMultipartUploadCommand({
|
|
90
38
|
Bucket: bucket(),
|
|
91
39
|
Key: session.key,
|
|
92
|
-
UploadId: session.
|
|
40
|
+
UploadId: session.id,
|
|
93
41
|
}),
|
|
94
42
|
);
|
|
95
43
|
}
|
|
96
44
|
|
|
97
|
-
async function signParts(session:
|
|
98
|
-
|
|
45
|
+
async function signParts(session: MultipartSession, parts: number[]) {
|
|
46
|
+
assertValidParts(session, parts);
|
|
99
47
|
return Promise.all(
|
|
100
48
|
parts.map(async (partNumber) => ({
|
|
101
49
|
partNumber,
|
|
@@ -104,12 +52,9 @@ export function createMultipartUploads({
|
|
|
104
52
|
new UploadPartCommand({
|
|
105
53
|
Bucket: bucket(),
|
|
106
54
|
Key: session.key,
|
|
107
|
-
UploadId: session.
|
|
55
|
+
UploadId: session.id,
|
|
108
56
|
PartNumber: partNumber,
|
|
109
|
-
ContentLength:
|
|
110
|
-
session.partSize,
|
|
111
|
-
session.size - (partNumber - 1) * session.partSize,
|
|
112
|
-
),
|
|
57
|
+
ContentLength: partLength(session, partNumber),
|
|
113
58
|
}),
|
|
114
59
|
{ expiresIn: partUrlExpiresIn },
|
|
115
60
|
),
|
|
@@ -120,21 +65,28 @@ export function createMultipartUploads({
|
|
|
120
65
|
const operations: ProviderMultipartUploads = {
|
|
121
66
|
async requestParts({ uploadId, key, parts }) {
|
|
122
67
|
return {
|
|
123
|
-
parts: await signParts(await
|
|
68
|
+
parts: await signParts(await sessions.read(uploadId, key), parts),
|
|
124
69
|
};
|
|
125
70
|
},
|
|
126
71
|
async complete({ uploadId, key, parts }) {
|
|
127
|
-
const session = await
|
|
128
|
-
|
|
72
|
+
const session = await sessions.read(uploadId, key);
|
|
73
|
+
assertValidParts(
|
|
129
74
|
session,
|
|
130
75
|
parts.map((part) => part.partNumber),
|
|
131
|
-
true,
|
|
76
|
+
{ complete: true },
|
|
132
77
|
);
|
|
78
|
+
if (parts.some((part) => !part.eTag)) {
|
|
79
|
+
throw new EdgeStoreError({
|
|
80
|
+
code: 'BAD_REQUEST',
|
|
81
|
+
message:
|
|
82
|
+
'S3 multipart parts are missing ETags. Check that the bucket CORS configuration exposes the ETag header.',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
133
85
|
await client.send(
|
|
134
86
|
new CompleteMultipartUploadCommand({
|
|
135
87
|
Bucket: bucket(),
|
|
136
88
|
Key: key,
|
|
137
|
-
UploadId: session.
|
|
89
|
+
UploadId: session.id,
|
|
138
90
|
MultipartUpload: {
|
|
139
91
|
Parts: [...parts]
|
|
140
92
|
.sort((a, b) => a.partNumber - b.partNumber)
|
|
@@ -147,7 +99,7 @@ export function createMultipartUploads({
|
|
|
147
99
|
);
|
|
148
100
|
},
|
|
149
101
|
async abort({ uploadId, key }) {
|
|
150
|
-
await abort(await
|
|
102
|
+
await abort(await sessions.read(uploadId, key));
|
|
151
103
|
},
|
|
152
104
|
};
|
|
153
105
|
|
|
@@ -155,7 +107,7 @@ export function createMultipartUploads({
|
|
|
155
107
|
operations,
|
|
156
108
|
async request(input: PutObjectCommandInput, plan: MultipartUploadPlan) {
|
|
157
109
|
// Validate signing configuration before allocating storage resources.
|
|
158
|
-
|
|
110
|
+
sessions.assertConfigured();
|
|
159
111
|
const { ContentLength, ...objectInput } = input;
|
|
160
112
|
const result = await client.send(
|
|
161
113
|
new CreateMultipartUploadCommand(objectInput),
|
|
@@ -163,29 +115,19 @@ export function createMultipartUploads({
|
|
|
163
115
|
if (!result.UploadId || !input.Key || ContentLength === undefined) {
|
|
164
116
|
throw new Error('S3 did not return a multipart upload ID.');
|
|
165
117
|
}
|
|
166
|
-
const session
|
|
118
|
+
const session = sessionFromPlan({
|
|
167
119
|
key: input.Key,
|
|
168
|
-
|
|
120
|
+
id: result.UploadId,
|
|
169
121
|
size: ContentLength,
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
};
|
|
122
|
+
plan,
|
|
123
|
+
});
|
|
173
124
|
try {
|
|
174
|
-
const uploadId = await new SignJWT(session)
|
|
175
|
-
.setProtectedHeader({ alg: 'HS256' })
|
|
176
|
-
.setAudience(audience)
|
|
177
|
-
.setIssuedAt()
|
|
178
|
-
.setExpirationTime(`${sessionExpiresIn}s`)
|
|
179
|
-
.sign(key);
|
|
180
125
|
return {
|
|
181
126
|
key: session.key,
|
|
182
|
-
uploadId,
|
|
127
|
+
uploadId: await sessions.sign(session),
|
|
183
128
|
partSize: session.partSize,
|
|
184
129
|
totalParts: session.totalParts,
|
|
185
|
-
parts: await signParts(
|
|
186
|
-
session,
|
|
187
|
-
plan.partNumbers.slice(0, INITIAL_MULTIPART_PART_URLS),
|
|
188
|
-
),
|
|
130
|
+
parts: await signParts(session, initialPartNumbers(plan)),
|
|
189
131
|
};
|
|
190
132
|
} catch (error) {
|
|
191
133
|
await abort(session).catch(() => undefined);
|
|
@@ -3,7 +3,8 @@ import type {
|
|
|
3
3
|
S3Client,
|
|
4
4
|
S3ClientConfig,
|
|
5
5
|
} from '@aws-sdk/client-s3';
|
|
6
|
-
import type { MaybePromise
|
|
6
|
+
import type { MaybePromise } from '@edgestore/shared';
|
|
7
|
+
import type { ObjectPathFn, ObjectPathFnArgs } from '../storage/objectKeys';
|
|
7
8
|
|
|
8
9
|
export type S3ObjectOptions = Pick<
|
|
9
10
|
PutObjectCommandInput,
|
|
@@ -16,46 +17,22 @@ export type S3ObjectOptions = Pick<
|
|
|
16
17
|
| 'SSEKMSKeyId'
|
|
17
18
|
>;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
type
|
|
20
|
+
/** Arguments passed to the `path` callback. */
|
|
21
|
+
export type S3PathFnArgs = ObjectPathFnArgs;
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
edgestoreBucketName: string;
|
|
25
|
-
/** File info after EdgeStore path and metadata generation. */
|
|
26
|
-
fileInfo: ProviderUploadedFileInfo;
|
|
27
|
-
/** Default object path relative to the logical bucket prefix. */
|
|
28
|
-
defaultPath: string;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type S3PathFn = (args: S3PathFnArgs) => MaybePromise<string>;
|
|
23
|
+
/** Returns an object path relative to the logical bucket prefix. */
|
|
24
|
+
export type S3PathFn = ObjectPathFn;
|
|
32
25
|
|
|
33
26
|
export type S3ProviderOptions = {
|
|
34
27
|
/**
|
|
35
28
|
* AWS SDK credentials (or credentials provider) to use for S3 requests.
|
|
36
29
|
*
|
|
37
|
-
* If unset,
|
|
38
|
-
*
|
|
30
|
+
* If unset, `ES_AWS_ACCESS_KEY_ID` and `ES_AWS_SECRET_ACCESS_KEY` are used
|
|
31
|
+
* when both are set. Otherwise the AWS SDK uses its default credential
|
|
32
|
+
* provider chain (environment variables, shared config files, instance/task
|
|
33
|
+
* roles, etc).
|
|
39
34
|
*/
|
|
40
35
|
credentials?: S3ClientConfig['credentials'];
|
|
41
|
-
/**
|
|
42
|
-
* Access key for AWS credentials.
|
|
43
|
-
* Can also be set via the `ES_AWS_ACCESS_KEY_ID` environment variable.
|
|
44
|
-
*
|
|
45
|
-
* If unset, the SDK will attempt to use the default credentials provider chain.
|
|
46
|
-
*
|
|
47
|
-
* @deprecated Pass `credentials` instead.
|
|
48
|
-
*/
|
|
49
|
-
accessKeyId?: string;
|
|
50
|
-
/**
|
|
51
|
-
* Secret access key for AWS credentials.
|
|
52
|
-
* Can also be set via the `ES_AWS_SECRET_ACCESS_KEY` environment variable.
|
|
53
|
-
*
|
|
54
|
-
* If unset, the SDK will attempt to use the default credentials provider chain.
|
|
55
|
-
*
|
|
56
|
-
* @deprecated Pass `credentials` instead.
|
|
57
|
-
*/
|
|
58
|
-
secretAccessKey?: string;
|
|
59
36
|
/**
|
|
60
37
|
* AWS region to use.
|
|
61
38
|
* Can also be set via the `ES_AWS_REGION` environment variable.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { type MultipartUploadPlan } from '@edgestore/sdk';
|
|
2
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
3
|
+
import { jwtVerify, SignJWT } from 'jose';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { INITIAL_MULTIPART_PART_URLS } from '../../core/provider';
|
|
6
|
+
import { getEnv } from '../../libs/env';
|
|
7
|
+
|
|
8
|
+
const sessionSchema = z.object({
|
|
9
|
+
key: z.string(),
|
|
10
|
+
/** Storage upload ID (S3) or block ID prefix (Azure). */
|
|
11
|
+
id: z.string(),
|
|
12
|
+
size: z.number().int().nonnegative(),
|
|
13
|
+
partSize: z.number().int().positive(),
|
|
14
|
+
totalParts: z.number().int().min(1).max(10_000),
|
|
15
|
+
/** Provider-specific settings applied when the upload completes. */
|
|
16
|
+
data: z.record(z.string(), z.unknown()).optional(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export type MultipartSession = z.infer<typeof sessionSchema>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Browser multipart sessions are HS256 JWTs. The token is the `uploadId` the
|
|
23
|
+
* browser sends back, and acts as a bearer capability for one object key.
|
|
24
|
+
*/
|
|
25
|
+
export function createMultipartSessions({
|
|
26
|
+
providerName,
|
|
27
|
+
audience,
|
|
28
|
+
secret,
|
|
29
|
+
expiresIn,
|
|
30
|
+
}: {
|
|
31
|
+
providerName: string;
|
|
32
|
+
audience: string;
|
|
33
|
+
secret: string | undefined;
|
|
34
|
+
expiresIn: number;
|
|
35
|
+
}) {
|
|
36
|
+
if (!Number.isSafeInteger(expiresIn) || expiresIn < 1) {
|
|
37
|
+
throw new RangeError(
|
|
38
|
+
`${providerName} multipart session lifetime must be a positive integer number of seconds.`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function signingKey() {
|
|
43
|
+
const value =
|
|
44
|
+
secret ??
|
|
45
|
+
getEnv('EDGE_STORE_JWT_SECRET') ??
|
|
46
|
+
getEnv('EDGE_STORE_SECRET_KEY');
|
|
47
|
+
if (!value)
|
|
48
|
+
throw new Error(
|
|
49
|
+
`${providerName} multipart uploads require jwtSecret or EDGE_STORE_JWT_SECRET.`,
|
|
50
|
+
);
|
|
51
|
+
return new TextEncoder().encode(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
/** Fails before storage resources are allocated when no secret is set. */
|
|
56
|
+
assertConfigured() {
|
|
57
|
+
signingKey();
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
sign(session: MultipartSession) {
|
|
61
|
+
return new SignJWT(session)
|
|
62
|
+
.setProtectedHeader({ alg: 'HS256' })
|
|
63
|
+
.setAudience(audience)
|
|
64
|
+
.setIssuedAt()
|
|
65
|
+
.setExpirationTime(`${expiresIn}s`)
|
|
66
|
+
.sign(signingKey());
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
async read(token: string, key: string) {
|
|
70
|
+
try {
|
|
71
|
+
const { payload } = await jwtVerify(token, signingKey(), {
|
|
72
|
+
algorithms: ['HS256'],
|
|
73
|
+
audience,
|
|
74
|
+
});
|
|
75
|
+
const session = sessionSchema.parse(payload);
|
|
76
|
+
if (session.key !== key) throw new Error('Wrong object key');
|
|
77
|
+
return session;
|
|
78
|
+
} catch {
|
|
79
|
+
throw new EdgeStoreError({
|
|
80
|
+
code: 'BAD_REQUEST',
|
|
81
|
+
message: `Invalid or expired ${providerName} multipart upload session.`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Builds a session from an upload plan. */
|
|
89
|
+
export function sessionFromPlan({
|
|
90
|
+
key,
|
|
91
|
+
id,
|
|
92
|
+
size,
|
|
93
|
+
plan,
|
|
94
|
+
data,
|
|
95
|
+
}: {
|
|
96
|
+
key: string;
|
|
97
|
+
id: string;
|
|
98
|
+
size: number;
|
|
99
|
+
plan: MultipartUploadPlan;
|
|
100
|
+
data?: Record<string, unknown>;
|
|
101
|
+
}): MultipartSession {
|
|
102
|
+
return {
|
|
103
|
+
key,
|
|
104
|
+
id,
|
|
105
|
+
size,
|
|
106
|
+
partSize: plan.partSizeBytes,
|
|
107
|
+
totalParts: plan.totalParts,
|
|
108
|
+
...(data ? { data } : {}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Part numbers whose URLs are signed when the upload starts. */
|
|
113
|
+
export function initialPartNumbers(plan: MultipartUploadPlan) {
|
|
114
|
+
return plan.partNumbers.slice(0, INITIAL_MULTIPART_PART_URLS);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Rejects empty, duplicate, or out-of-range part lists. */
|
|
118
|
+
export function assertValidParts(
|
|
119
|
+
session: MultipartSession,
|
|
120
|
+
parts: number[],
|
|
121
|
+
{ complete = false } = {},
|
|
122
|
+
) {
|
|
123
|
+
if (
|
|
124
|
+
!parts.length ||
|
|
125
|
+
new Set(parts).size !== parts.length ||
|
|
126
|
+
parts.some(
|
|
127
|
+
(part) =>
|
|
128
|
+
!Number.isInteger(part) || part < 1 || part > session.totalParts,
|
|
129
|
+
) ||
|
|
130
|
+
(complete && parts.length !== session.totalParts)
|
|
131
|
+
) {
|
|
132
|
+
throw new EdgeStoreError({
|
|
133
|
+
code: 'BAD_REQUEST',
|
|
134
|
+
message: 'Invalid multipart part numbers.',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Byte length of one part; the last part may be shorter. */
|
|
140
|
+
export function partLength(session: MultipartSession, partNumber: number) {
|
|
141
|
+
return Math.min(
|
|
142
|
+
session.partSize,
|
|
143
|
+
session.size - (partNumber - 1) * session.partSize,
|
|
144
|
+
);
|
|
145
|
+
}
|