@edgestore/server 0.8.0 → 1.0.0-next.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 +10 -4
- package/adapters/astro/index.d.ts +1 -1
- package/adapters/astro/index.js +1 -1
- package/adapters/express/index.d.ts +1 -1
- package/adapters/express/index.js +1 -1
- package/adapters/fastify/index.d.ts +1 -1
- package/adapters/fastify/index.js +1 -1
- package/adapters/hono/index.d.ts +1 -1
- package/adapters/hono/index.js +1 -1
- package/adapters/next/app/index.d.ts +1 -1
- package/adapters/next/app/index.js +1 -1
- package/adapters/next/pages/index.d.ts +1 -1
- package/adapters/next/pages/index.js +1 -1
- package/adapters/remix/index.d.ts +1 -1
- package/adapters/remix/index.js +1 -1
- package/adapters/start/index.d.ts +1 -1
- package/adapters/start/index.js +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.js +1 -1
- package/dist/adapters/astro/index.d.ts +8 -13
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +17 -166
- package/dist/adapters/dispatcher.d.ts +30 -0
- package/dist/adapters/dispatcher.d.ts.map +1 -0
- package/dist/adapters/dispatcher.js +172 -0
- package/dist/adapters/express/index.d.ts +8 -13
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +27 -99
- package/dist/adapters/fastify/index.d.ts +8 -13
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +26 -126
- package/dist/adapters/hono/index.d.ts +8 -13
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +17 -126
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +8 -13
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +24 -153
- package/dist/adapters/next/pages/index.d.ts +8 -13
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +37 -100
- package/dist/adapters/remix/index.d.ts +8 -13
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +17 -141
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +218 -215
- package/dist/adapters/start/index.d.ts +8 -13
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +17 -174
- package/dist/core/client/bucketClient.d.ts +7 -0
- package/dist/core/client/bucketClient.d.ts.map +1 -0
- package/dist/core/client/bucketClient.js +312 -0
- package/dist/core/client/index.d.ts +119 -140
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +13 -220
- package/dist/core/index.d.ts +21 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -10
- package/dist/core/provider.d.ts +15 -0
- package/dist/core/provider.d.ts.map +1 -0
- package/dist/core/provider.js +25 -0
- package/dist/core/routerRules.d.ts +21 -0
- package/dist/core/routerRules.d.ts.map +1 -0
- package/dist/core/routerRules.js +54 -0
- package/dist/core/validateFile.d.ts +9 -0
- package/dist/core/validateFile.d.ts.map +1 -0
- package/dist/core/validateFile.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -10
- package/dist/libs/env.d.ts +3 -0
- package/dist/libs/env.d.ts.map +1 -0
- package/dist/libs/env.js +14 -0
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- package/dist/providers/azure-blob/index.d.ts +119 -0
- package/dist/providers/azure-blob/index.d.ts.map +1 -0
- package/dist/providers/azure-blob/index.js +166 -0
- package/dist/providers/edgestore/index.d.ts +171 -3
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +307 -141
- package/dist/providers/{aws → s3}/index.d.ts +68 -11
- package/dist/providers/s3/index.d.ts.map +1 -0
- package/dist/providers/s3/index.js +146 -0
- package/dist/test-utils/edgestoreSmoke.d.ts +0 -2
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +58 -56
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- package/providers/s3/index.d.ts +1 -0
- package/providers/s3/index.js +1 -0
- package/src/adapters/astro/index.ts +27 -211
- package/src/adapters/dispatcher.ts +261 -0
- package/src/adapters/express/index.ts +43 -149
- package/src/adapters/fastify/index.ts +43 -191
- package/src/adapters/hono/index.ts +28 -183
- package/src/adapters/next/app/index.ts +37 -201
- package/src/adapters/next/pages/index.ts +55 -156
- package/src/adapters/remix/index.ts +27 -190
- package/src/adapters/shared.ts +249 -274
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +480 -0
- package/src/core/client/index.ts +300 -428
- package/src/core/index.ts +42 -1
- package/src/core/provider.ts +80 -0
- package/src/core/routerRules.ts +75 -0
- package/src/core/validateFile.ts +50 -0
- package/src/index.ts +10 -0
- package/src/libs/env.ts +16 -0
- package/src/libs/logger.ts +4 -2
- package/src/providers/azure-blob/index.ts +265 -0
- package/src/providers/edgestore/index.ts +426 -154
- package/src/providers/s3/index.ts +289 -0
- package/src/test-utils/edgestoreSmoke.ts +0 -11
- package/src/version.ts +1 -1
- package/dist/adapters/astro/index.mjs +0 -172
- package/dist/adapters/express/index.mjs +0 -108
- package/dist/adapters/fastify/index.mjs +0 -135
- package/dist/adapters/hono/index.mjs +0 -131
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -163
- package/dist/adapters/next/pages/index.mjs +0 -112
- package/dist/adapters/remix/index.mjs +0 -148
- package/dist/adapters/shared.mjs +0 -471
- package/dist/adapters/start/index.mjs +0 -181
- package/dist/core/client/index.mjs +0 -221
- package/dist/core/index.mjs +0 -2
- package/dist/core/sdk/index.d.ts +0 -282
- package/dist/core/sdk/index.d.ts.map +0 -1
- package/dist/core/sdk/index.js +0 -256
- package/dist/core/sdk/index.mjs +0 -253
- package/dist/index.mjs +0 -1
- package/dist/libs/errors/EdgeStoreCredentialsError.mjs +0 -10
- package/dist/libs/logger.mjs +0 -53
- package/dist/libs/utils.mjs +0 -10
- package/dist/providers/aws/index.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -105
- package/dist/providers/aws/index.mjs +0 -103
- package/dist/providers/azure/index.d.ts +0 -39
- package/dist/providers/azure/index.d.ts.map +0 -1
- package/dist/providers/azure/index.js +0 -87
- package/dist/providers/azure/index.mjs +0 -85
- package/dist/providers/edgestore/index.mjs +0 -164
- package/dist/version.js +0 -7
- package/dist/version.mjs +0 -4
- package/providers/aws/index.d.ts +0 -1
- package/providers/aws/index.js +0 -1
- package/providers/azure/index.d.ts +0 -1
- package/providers/azure/index.js +0 -1
- package/src/core/sdk/index.ts +0 -549
- package/src/providers/aws/index.ts +0 -240
- package/src/providers/azure/index.ts +0 -142
package/src/adapters/shared.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EdgeStoreError,
|
|
3
|
-
type
|
|
3
|
+
type AnyContext,
|
|
4
|
+
type AnyEdgeStoreProvider,
|
|
4
5
|
type EdgeStoreRouter,
|
|
5
|
-
type
|
|
6
|
-
type
|
|
6
|
+
type ProviderFileMutationResult,
|
|
7
|
+
type SharedConfirmUploadsRes,
|
|
8
|
+
type SharedDeleteFilesRes,
|
|
7
9
|
type SharedInitRes,
|
|
8
10
|
type SharedRequestUploadPartsRes,
|
|
9
11
|
type SharedRequestUploadRes,
|
|
10
12
|
} from '@edgestore/shared';
|
|
11
13
|
import { hkdf } from '@panva/hkdf';
|
|
12
|
-
import {
|
|
14
|
+
import { stringifySetCookie } from 'cookie';
|
|
13
15
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
14
|
-
import {
|
|
15
|
-
import
|
|
16
|
-
import {
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { getProviderBaseUrl, referenceFromUrl } from '../core/provider';
|
|
18
|
+
import { buildPath, parseBucketInput, parsePath } from '../core/routerRules';
|
|
19
|
+
import { validateFileForBucket } from '../core/validateFile';
|
|
20
|
+
import { getEnv, isDev } from '../libs/env';
|
|
21
|
+
import type { LoggerLike } from '../libs/logger';
|
|
17
22
|
|
|
18
23
|
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
19
24
|
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
export type HandlerEdgeStore<TCtx extends AnyContext> = {
|
|
27
|
+
provider: AnyEdgeStoreProvider;
|
|
28
|
+
router: EdgeStoreRouter<TCtx>;
|
|
23
29
|
};
|
|
24
30
|
|
|
25
31
|
const NO_BODY_STATUSES = new Set([204, 205, 304]);
|
|
@@ -159,100 +165,86 @@ export function getCookieConfig(
|
|
|
159
165
|
};
|
|
160
166
|
}
|
|
161
167
|
|
|
162
|
-
export async function init<TCtx>(params: {
|
|
163
|
-
provider:
|
|
168
|
+
export async function init<TCtx extends AnyContext>(params: {
|
|
169
|
+
provider: AnyEdgeStoreProvider;
|
|
164
170
|
router: EdgeStoreRouter<TCtx>;
|
|
165
171
|
ctx: TCtx;
|
|
172
|
+
logger: LoggerLike;
|
|
166
173
|
cookieConfig?: CookieConfig;
|
|
167
174
|
}): Promise<SharedInitRes> {
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
log.debug('Running [init]', { ctx });
|
|
175
|
+
const { ctx, provider, router, logger, cookieConfig } = params;
|
|
176
|
+
logger.debug('Running [init]', { ctx });
|
|
171
177
|
|
|
172
178
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
173
179
|
|
|
174
180
|
const ctxToken = await encryptJWT(ctx);
|
|
175
|
-
const
|
|
176
|
-
provider.name === 'edgestore' &&
|
|
177
|
-
Object.values(router.buckets).some(
|
|
178
|
-
(bucket) =>
|
|
179
|
-
bucket._def.accessControl !== undefined &&
|
|
180
|
-
bucket._def.accessControl !== 'private',
|
|
181
|
-
);
|
|
182
|
-
const shouldRunProviderInit =
|
|
183
|
-
provider.name !== 'edgestore' || requiresFileAccessCookie;
|
|
184
|
-
|
|
185
|
-
let token: string | undefined;
|
|
186
|
-
if (shouldRunProviderInit) {
|
|
187
|
-
const initRes = await provider.init({
|
|
188
|
-
ctx,
|
|
189
|
-
router: router,
|
|
190
|
-
});
|
|
191
|
-
token = initRes.token;
|
|
192
|
-
}
|
|
181
|
+
const initRes = await provider.init({ ctx, router });
|
|
193
182
|
const newCookies = [
|
|
194
|
-
|
|
195
|
-
resolvedCookieConfig.ctx.name,
|
|
196
|
-
ctxToken,
|
|
197
|
-
resolvedCookieConfig.ctx.options,
|
|
198
|
-
),
|
|
183
|
+
stringifySetCookie({
|
|
184
|
+
name: resolvedCookieConfig.ctx.name,
|
|
185
|
+
value: ctxToken,
|
|
186
|
+
...resolvedCookieConfig.ctx.options,
|
|
187
|
+
}),
|
|
199
188
|
];
|
|
200
|
-
if (token) {
|
|
189
|
+
if (initRes.token) {
|
|
201
190
|
newCookies.push(
|
|
202
|
-
|
|
203
|
-
resolvedCookieConfig.token.name,
|
|
204
|
-
token,
|
|
205
|
-
resolvedCookieConfig.token.options,
|
|
206
|
-
),
|
|
191
|
+
stringifySetCookie({
|
|
192
|
+
name: resolvedCookieConfig.token.name,
|
|
193
|
+
value: initRes.token,
|
|
194
|
+
...resolvedCookieConfig.token.options,
|
|
195
|
+
}),
|
|
207
196
|
);
|
|
208
197
|
}
|
|
209
|
-
const baseUrl = await provider
|
|
198
|
+
const baseUrl = await getProviderBaseUrl(provider);
|
|
210
199
|
|
|
211
|
-
|
|
200
|
+
logger.debug('Finished [init]', {
|
|
212
201
|
ctx,
|
|
213
202
|
newCookies,
|
|
214
|
-
token,
|
|
215
203
|
baseUrl,
|
|
216
204
|
providerName: provider.name,
|
|
217
|
-
|
|
205
|
+
clientInit: initRes.clientInit,
|
|
218
206
|
});
|
|
219
207
|
|
|
220
208
|
return {
|
|
221
209
|
newCookies,
|
|
222
|
-
token,
|
|
223
210
|
baseUrl,
|
|
224
211
|
providerName: provider.name,
|
|
225
|
-
|
|
212
|
+
clientInit: initRes.clientInit,
|
|
226
213
|
};
|
|
227
214
|
}
|
|
228
215
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
216
|
+
const nonEmptyStringSchema = z.string().min(1);
|
|
217
|
+
|
|
218
|
+
export const requestUploadBodySchema = z.object({
|
|
219
|
+
bucketName: nonEmptyStringSchema,
|
|
220
|
+
input: z.unknown().default({}),
|
|
221
|
+
fileInfo: z.object({
|
|
222
|
+
size: z.number().finite().nonnegative(),
|
|
223
|
+
type: z.string(),
|
|
224
|
+
extension: z.string(),
|
|
225
|
+
fileName: z.string().optional(),
|
|
226
|
+
replaceTargetUrl: nonEmptyStringSchema.optional(),
|
|
227
|
+
temporary: z.boolean().default(false),
|
|
228
|
+
}),
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
export type RequestUploadBody = z.infer<typeof requestUploadBodySchema>;
|
|
232
|
+
|
|
233
|
+
export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
234
|
+
provider: AnyEdgeStoreProvider;
|
|
244
235
|
router: EdgeStoreRouter<TCtx>;
|
|
245
236
|
ctxToken: string | undefined;
|
|
246
237
|
body: RequestUploadBody;
|
|
238
|
+
logger: LoggerLike;
|
|
247
239
|
}): Promise<SharedRequestUploadRes> {
|
|
248
240
|
const {
|
|
249
241
|
provider,
|
|
250
242
|
router,
|
|
251
243
|
ctxToken,
|
|
244
|
+
logger,
|
|
252
245
|
body: { bucketName, input, fileInfo },
|
|
253
246
|
} = params;
|
|
254
|
-
|
|
255
|
-
log.debug('Running [requestUpload]', { bucketName, input, fileInfo });
|
|
247
|
+
logger.debug('Running [requestUpload]', { bucketName, input, fileInfo });
|
|
256
248
|
|
|
257
249
|
if (!ctxToken) {
|
|
258
250
|
throw new EdgeStoreError({
|
|
@@ -262,7 +254,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
262
254
|
}
|
|
263
255
|
const ctx = await getContext(ctxToken);
|
|
264
256
|
|
|
265
|
-
|
|
257
|
+
logger.debug('Decrypted Context', { ctx });
|
|
266
258
|
|
|
267
259
|
const bucket = router.buckets[bucketName];
|
|
268
260
|
if (!bucket) {
|
|
@@ -271,11 +263,12 @@ export async function requestUpload<TCtx>(params: {
|
|
|
271
263
|
code: 'BAD_REQUEST',
|
|
272
264
|
});
|
|
273
265
|
}
|
|
266
|
+
const parsedInput = await parseBucketInput(bucket, input);
|
|
274
267
|
if (bucket._def.beforeUpload) {
|
|
275
|
-
|
|
268
|
+
logger.debug('Running [beforeUpload]');
|
|
276
269
|
const canUpload = await bucket._def.beforeUpload?.({
|
|
277
270
|
ctx,
|
|
278
|
-
input,
|
|
271
|
+
input: parsedInput,
|
|
279
272
|
fileInfo: {
|
|
280
273
|
size: fileInfo.size,
|
|
281
274
|
type: fileInfo.type,
|
|
@@ -285,7 +278,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
285
278
|
temporary: fileInfo.temporary,
|
|
286
279
|
},
|
|
287
280
|
});
|
|
288
|
-
|
|
281
|
+
logger.debug('Finished [beforeUpload]', { canUpload });
|
|
289
282
|
if (!canUpload) {
|
|
290
283
|
throw new EdgeStoreError({
|
|
291
284
|
message: 'Upload not allowed for the current context',
|
|
@@ -294,78 +287,28 @@ export async function requestUpload<TCtx>(params: {
|
|
|
294
287
|
}
|
|
295
288
|
}
|
|
296
289
|
|
|
297
|
-
|
|
298
|
-
if (!IMAGE_MIME_TYPES.includes(fileInfo.type)) {
|
|
299
|
-
throw new EdgeStoreError({
|
|
300
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
301
|
-
message: 'Only images are allowed in this bucket',
|
|
302
|
-
details: {
|
|
303
|
-
allowedMimeTypes: IMAGE_MIME_TYPES,
|
|
304
|
-
mimeType: fileInfo.type,
|
|
305
|
-
},
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (bucket._def.bucketConfig?.maxSize) {
|
|
311
|
-
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
312
|
-
throw new EdgeStoreError({
|
|
313
|
-
code: 'FILE_TOO_LARGE',
|
|
314
|
-
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
315
|
-
details: {
|
|
316
|
-
maxFileSize: bucket._def.bucketConfig.maxSize,
|
|
317
|
-
fileSize: fileInfo.size,
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (bucket._def.bucketConfig?.accept) {
|
|
324
|
-
const accept = bucket._def.bucketConfig.accept;
|
|
325
|
-
let accepted = false;
|
|
326
|
-
for (const acceptedMimeType of accept) {
|
|
327
|
-
if (acceptedMimeType.endsWith('/*')) {
|
|
328
|
-
const mimeType = acceptedMimeType.replace('/*', '');
|
|
329
|
-
if (fileInfo.type.startsWith(mimeType)) {
|
|
330
|
-
accepted = true;
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
333
|
-
} else if (fileInfo.type === acceptedMimeType) {
|
|
334
|
-
accepted = true;
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (!accepted) {
|
|
339
|
-
throw new EdgeStoreError({
|
|
340
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
341
|
-
message: `"${
|
|
342
|
-
fileInfo.type
|
|
343
|
-
}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
344
|
-
details: {
|
|
345
|
-
allowedMimeTypes: accept,
|
|
346
|
-
mimeType: fileInfo.type,
|
|
347
|
-
},
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
}
|
|
290
|
+
validateFileForBucket({ bucket, fileInfo });
|
|
351
291
|
|
|
352
292
|
const path = buildPath({
|
|
353
|
-
fileInfo,
|
|
354
293
|
bucket,
|
|
355
|
-
pathAttrs: { ctx, input },
|
|
294
|
+
pathAttrs: { ctx, input: parsedInput },
|
|
356
295
|
});
|
|
357
|
-
const metadata =
|
|
296
|
+
const metadata =
|
|
297
|
+
(await bucket._def.metadata?.({
|
|
298
|
+
ctx,
|
|
299
|
+
input: parsedInput,
|
|
300
|
+
})) ?? {};
|
|
358
301
|
const isPublic = bucket._def.accessControl === undefined;
|
|
359
302
|
const autoSignedUrls = bucket._def.autoSignedUrls;
|
|
360
303
|
|
|
361
|
-
|
|
304
|
+
logger.debug('upload info', {
|
|
362
305
|
path,
|
|
363
306
|
metadata,
|
|
364
307
|
isPublic,
|
|
365
308
|
bucketType: bucket._def.type,
|
|
366
309
|
});
|
|
367
310
|
|
|
368
|
-
const requestUploadRes = await provider.
|
|
311
|
+
const requestUploadRes = await provider.uploads.request({
|
|
369
312
|
bucketName,
|
|
370
313
|
bucketType: bucket._def.type,
|
|
371
314
|
fileInfo: {
|
|
@@ -378,7 +321,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
378
321
|
});
|
|
379
322
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
380
323
|
|
|
381
|
-
|
|
324
|
+
logger.debug('Finished [requestUpload]');
|
|
382
325
|
|
|
383
326
|
return {
|
|
384
327
|
...requestUploadRes,
|
|
@@ -390,28 +333,33 @@ export async function requestUpload<TCtx>(params: {
|
|
|
390
333
|
};
|
|
391
334
|
}
|
|
392
335
|
|
|
393
|
-
export
|
|
394
|
-
multipart: {
|
|
395
|
-
uploadId:
|
|
396
|
-
parts: number
|
|
397
|
-
}
|
|
398
|
-
path:
|
|
399
|
-
};
|
|
336
|
+
export const requestUploadPartsBodySchema = z.object({
|
|
337
|
+
multipart: z.object({
|
|
338
|
+
uploadId: nonEmptyStringSchema,
|
|
339
|
+
parts: z.array(z.number().int().positive()),
|
|
340
|
+
}),
|
|
341
|
+
path: nonEmptyStringSchema,
|
|
342
|
+
});
|
|
400
343
|
|
|
401
|
-
export
|
|
402
|
-
|
|
344
|
+
export type RequestUploadPartsParams = z.infer<
|
|
345
|
+
typeof requestUploadPartsBodySchema
|
|
346
|
+
>;
|
|
347
|
+
|
|
348
|
+
export async function requestUploadParts<TCtx extends AnyContext>(params: {
|
|
349
|
+
provider: AnyEdgeStoreProvider;
|
|
403
350
|
router: EdgeStoreRouter<TCtx>;
|
|
404
351
|
ctxToken: string | undefined;
|
|
405
352
|
body: RequestUploadPartsParams;
|
|
353
|
+
logger: LoggerLike;
|
|
406
354
|
}): Promise<SharedRequestUploadPartsRes> {
|
|
407
355
|
const {
|
|
408
356
|
provider,
|
|
409
357
|
ctxToken,
|
|
358
|
+
logger,
|
|
410
359
|
body: { multipart, path },
|
|
411
360
|
} = params;
|
|
412
361
|
|
|
413
|
-
|
|
414
|
-
log.debug('Running [requestUploadParts]', { multipart, path });
|
|
362
|
+
logger.debug('Running [requestUploadParts]', { multipart, path });
|
|
415
363
|
|
|
416
364
|
if (!ctxToken) {
|
|
417
365
|
throw new EdgeStoreError({
|
|
@@ -421,41 +369,55 @@ export async function requestUploadParts<TCtx>(params: {
|
|
|
421
369
|
}
|
|
422
370
|
await getContext(ctxToken); // just to check if the token is valid
|
|
423
371
|
|
|
424
|
-
const
|
|
372
|
+
const multipartUploads = provider.uploads.multipart;
|
|
373
|
+
if (!multipartUploads) {
|
|
374
|
+
throw new EdgeStoreError({
|
|
375
|
+
message: `Provider ${provider.name} does not support multipart uploads.`,
|
|
376
|
+
code: 'BAD_REQUEST',
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
const res = await multipartUploads.requestParts({
|
|
425
380
|
multipart,
|
|
426
381
|
path,
|
|
427
382
|
});
|
|
428
383
|
|
|
429
|
-
|
|
384
|
+
logger.debug('Finished [requestUploadParts]');
|
|
430
385
|
|
|
431
386
|
return res;
|
|
432
387
|
}
|
|
433
388
|
|
|
434
|
-
export
|
|
435
|
-
bucketName:
|
|
436
|
-
uploadId:
|
|
437
|
-
key:
|
|
438
|
-
parts:
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
389
|
+
export const completeMultipartUploadBodySchema = z.object({
|
|
390
|
+
bucketName: nonEmptyStringSchema,
|
|
391
|
+
uploadId: nonEmptyStringSchema,
|
|
392
|
+
key: nonEmptyStringSchema,
|
|
393
|
+
parts: z.array(
|
|
394
|
+
z.object({
|
|
395
|
+
partNumber: z.number().int().positive(),
|
|
396
|
+
eTag: nonEmptyStringSchema,
|
|
397
|
+
}),
|
|
398
|
+
),
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
export type CompleteMultipartUploadBody = z.infer<
|
|
402
|
+
typeof completeMultipartUploadBodySchema
|
|
403
|
+
>;
|
|
404
|
+
|
|
405
|
+
export async function completeMultipartUpload<TCtx extends AnyContext>(params: {
|
|
406
|
+
provider: AnyEdgeStoreProvider;
|
|
446
407
|
router: EdgeStoreRouter<TCtx>;
|
|
447
408
|
ctxToken: string | undefined;
|
|
448
409
|
body: CompleteMultipartUploadBody;
|
|
410
|
+
logger: LoggerLike;
|
|
449
411
|
}) {
|
|
450
412
|
const {
|
|
451
413
|
provider,
|
|
452
414
|
router,
|
|
453
415
|
ctxToken,
|
|
416
|
+
logger,
|
|
454
417
|
body: { bucketName, uploadId, key, parts },
|
|
455
418
|
} = params;
|
|
456
419
|
|
|
457
|
-
|
|
458
|
-
log.debug('Running [completeMultipartUpload]', {
|
|
420
|
+
logger.debug('Running [completeMultipartUpload]', {
|
|
459
421
|
bucketName,
|
|
460
422
|
uploadId,
|
|
461
423
|
key,
|
|
@@ -476,37 +438,45 @@ export async function completeMultipartUpload<TCtx>(params: {
|
|
|
476
438
|
});
|
|
477
439
|
}
|
|
478
440
|
|
|
479
|
-
const
|
|
441
|
+
const multipartUploads = provider.uploads.multipart;
|
|
442
|
+
if (!multipartUploads) {
|
|
443
|
+
throw new EdgeStoreError({
|
|
444
|
+
message: `Provider ${provider.name} does not support multipart uploads.`,
|
|
445
|
+
code: 'BAD_REQUEST',
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
await multipartUploads.complete({
|
|
480
449
|
uploadId,
|
|
481
450
|
key,
|
|
482
451
|
parts,
|
|
483
452
|
});
|
|
484
453
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
return res;
|
|
454
|
+
logger.debug('Finished [completeMultipartUpload]');
|
|
488
455
|
}
|
|
489
456
|
|
|
490
|
-
export
|
|
491
|
-
bucketName:
|
|
492
|
-
|
|
493
|
-
};
|
|
457
|
+
export const confirmUploadsBodySchema = z.object({
|
|
458
|
+
bucketName: nonEmptyStringSchema,
|
|
459
|
+
urls: z.array(nonEmptyStringSchema),
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
export type ConfirmUploadsBody = z.infer<typeof confirmUploadsBodySchema>;
|
|
494
463
|
|
|
495
|
-
export async function
|
|
496
|
-
provider:
|
|
464
|
+
export async function confirmUploads<TCtx extends AnyContext>(params: {
|
|
465
|
+
provider: AnyEdgeStoreProvider;
|
|
497
466
|
router: EdgeStoreRouter<TCtx>;
|
|
498
467
|
ctxToken: string | undefined;
|
|
499
|
-
body:
|
|
500
|
-
|
|
468
|
+
body: ConfirmUploadsBody;
|
|
469
|
+
logger: LoggerLike;
|
|
470
|
+
}): Promise<SharedConfirmUploadsRes> {
|
|
501
471
|
const {
|
|
502
472
|
provider,
|
|
503
473
|
router,
|
|
504
474
|
ctxToken,
|
|
505
|
-
|
|
475
|
+
logger,
|
|
476
|
+
body: { bucketName, urls },
|
|
506
477
|
} = params;
|
|
507
478
|
|
|
508
|
-
|
|
509
|
-
log.debug('Running [confirmUpload]', { bucketName, url });
|
|
479
|
+
logger.debug('Running [confirmUploads]', { bucketName, urls });
|
|
510
480
|
|
|
511
481
|
if (!ctxToken) {
|
|
512
482
|
throw new EdgeStoreError({
|
|
@@ -523,35 +493,47 @@ export async function confirmUpload<TCtx>(params: {
|
|
|
523
493
|
});
|
|
524
494
|
}
|
|
525
495
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
496
|
+
if (!provider.files.confirm) {
|
|
497
|
+
throw new EdgeStoreError({
|
|
498
|
+
message: `Provider ${provider.name} does not support file confirmation.`,
|
|
499
|
+
code: 'SERVER_ERROR',
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
const files = await Promise.all(
|
|
503
|
+
urls.map((url) => referenceFromUrl(provider, unproxyUrl(url))),
|
|
504
|
+
);
|
|
505
|
+
const result = await provider.files.confirm({
|
|
506
|
+
bucketName,
|
|
507
|
+
files,
|
|
529
508
|
});
|
|
530
509
|
|
|
531
|
-
|
|
532
|
-
return
|
|
510
|
+
logger.debug('Finished [confirmUploads]');
|
|
511
|
+
return mapFrontendMutationResult(urls, result);
|
|
533
512
|
}
|
|
534
513
|
|
|
535
|
-
export
|
|
536
|
-
bucketName:
|
|
537
|
-
|
|
538
|
-
};
|
|
514
|
+
export const deleteFilesBodySchema = z.object({
|
|
515
|
+
bucketName: nonEmptyStringSchema,
|
|
516
|
+
urls: z.array(nonEmptyStringSchema),
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
export type DeleteFilesBody = z.infer<typeof deleteFilesBodySchema>;
|
|
539
520
|
|
|
540
|
-
export async function
|
|
541
|
-
provider:
|
|
521
|
+
export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
522
|
+
provider: AnyEdgeStoreProvider;
|
|
542
523
|
router: EdgeStoreRouter<TCtx>;
|
|
543
524
|
ctxToken: string | undefined;
|
|
544
|
-
body:
|
|
545
|
-
|
|
525
|
+
body: DeleteFilesBody;
|
|
526
|
+
logger: LoggerLike;
|
|
527
|
+
}): Promise<SharedDeleteFilesRes> {
|
|
546
528
|
const {
|
|
547
529
|
provider,
|
|
548
530
|
router,
|
|
549
531
|
ctxToken,
|
|
550
|
-
|
|
532
|
+
logger,
|
|
533
|
+
body: { bucketName, urls },
|
|
551
534
|
} = params;
|
|
552
535
|
|
|
553
|
-
|
|
554
|
-
log.debug('Running [deleteFile]', { bucketName, url });
|
|
536
|
+
logger.debug('Running [deleteFiles]', { bucketName, urls });
|
|
555
537
|
|
|
556
538
|
if (!ctxToken) {
|
|
557
539
|
throw new EdgeStoreError({
|
|
@@ -576,31 +558,84 @@ export async function deleteFile<TCtx>(params: {
|
|
|
576
558
|
});
|
|
577
559
|
}
|
|
578
560
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
561
|
+
if (!provider.files.delete) {
|
|
562
|
+
throw new EdgeStoreError({
|
|
563
|
+
message: `Provider ${provider.name} does not support file deletion.`,
|
|
564
|
+
code: 'SERVER_ERROR',
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
const files = await Promise.all(
|
|
568
|
+
urls.map((url) => referenceFromUrl(provider, unproxyUrl(url))),
|
|
569
|
+
);
|
|
570
|
+
const fileRecords = await Promise.all(
|
|
571
|
+
files.map((file) =>
|
|
572
|
+
Promise.resolve(provider.files.get({ bucketName, file })),
|
|
573
|
+
),
|
|
574
|
+
);
|
|
575
|
+
const authorizations = await Promise.all(
|
|
576
|
+
fileRecords.map((file) => {
|
|
577
|
+
if (file.path === undefined && bucket._def.path.length > 0) {
|
|
578
|
+
throw new EdgeStoreError({
|
|
579
|
+
message: `Provider ${provider.name} must return path from files.get to authorize frontend deletion for a bucket with configured path fields.`,
|
|
580
|
+
code: 'SERVER_ERROR',
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
if (file.metadata === undefined && bucket._def.metadata !== undefined) {
|
|
584
|
+
throw new EdgeStoreError({
|
|
585
|
+
message: `Provider ${provider.name} must return metadata from files.get to authorize frontend deletion for a bucket with configured metadata fields.`,
|
|
586
|
+
code: 'SERVER_ERROR',
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
return Promise.resolve(
|
|
590
|
+
bucket._def.beforeDelete!({
|
|
591
|
+
ctx,
|
|
592
|
+
fileInfo: {
|
|
593
|
+
url: file.url,
|
|
594
|
+
size: file.sizeBytes,
|
|
595
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
596
|
+
path: file.path ?? {},
|
|
597
|
+
metadata: file.metadata ?? {},
|
|
598
|
+
},
|
|
599
|
+
}),
|
|
600
|
+
);
|
|
601
|
+
}),
|
|
602
|
+
);
|
|
603
|
+
if (authorizations.some((allowed) => !allowed)) {
|
|
588
604
|
throw new EdgeStoreError({
|
|
589
605
|
message: 'Delete not allowed for the current context',
|
|
590
606
|
code: 'DELETE_NOT_ALLOWED',
|
|
591
607
|
});
|
|
592
608
|
}
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
609
|
+
const result = await provider.files.delete({
|
|
610
|
+
bucketName,
|
|
611
|
+
files,
|
|
596
612
|
});
|
|
597
613
|
|
|
598
|
-
|
|
614
|
+
logger.debug('Finished [deleteFiles]');
|
|
599
615
|
|
|
600
|
-
return
|
|
616
|
+
return mapFrontendMutationResult(urls, result);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function mapFrontendMutationResult(
|
|
620
|
+
urls: string[],
|
|
621
|
+
result: ProviderFileMutationResult<string>,
|
|
622
|
+
): SharedDeleteFilesRes {
|
|
623
|
+
if (result.results.length !== urls.length) {
|
|
624
|
+
throw new Error(
|
|
625
|
+
`The provider returned ${result.results.length} mutation results for ${urls.length} files.`,
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
const succeeded: string[] = [];
|
|
629
|
+
const failed: SharedDeleteFilesRes['failed'] = [];
|
|
630
|
+
result.results.forEach((item, index) => {
|
|
631
|
+
const url = urls[index]!;
|
|
632
|
+
if (item.success) succeeded.push(url);
|
|
633
|
+
else failed.push({ url, error: item.error });
|
|
634
|
+
});
|
|
635
|
+
return { succeeded, failed };
|
|
601
636
|
}
|
|
602
637
|
|
|
603
|
-
async function encryptJWT(ctx:
|
|
638
|
+
async function encryptJWT(ctx: AnyContext) {
|
|
604
639
|
const secret =
|
|
605
640
|
getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
606
641
|
if (!secret) {
|
|
@@ -610,11 +645,11 @@ async function encryptJWT(ctx: any) {
|
|
|
610
645
|
});
|
|
611
646
|
}
|
|
612
647
|
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
613
|
-
return await new EncryptJWT(ctx)
|
|
648
|
+
return await new EncryptJWT({ ctx })
|
|
614
649
|
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
|
615
650
|
.setIssuedAt()
|
|
616
651
|
.setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE)
|
|
617
|
-
.setJti(
|
|
652
|
+
.setJti(crypto.randomUUID())
|
|
618
653
|
.encrypt(encryptionSecret);
|
|
619
654
|
}
|
|
620
655
|
|
|
@@ -631,7 +666,15 @@ async function decryptJWT(token: string) {
|
|
|
631
666
|
const { payload } = await jwtDecrypt(token, encryptionSecret, {
|
|
632
667
|
clockTolerance: 15,
|
|
633
668
|
});
|
|
634
|
-
|
|
669
|
+
const result = z.object({ ctx: z.record(z.string()) }).safeParse(payload);
|
|
670
|
+
if (!result.success) {
|
|
671
|
+
throw new EdgeStoreError({
|
|
672
|
+
message: 'Invalid edgestore-ctx cookie',
|
|
673
|
+
code: 'UNAUTHORIZED',
|
|
674
|
+
cause: result.error,
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
return result.data.ctx;
|
|
635
678
|
}
|
|
636
679
|
|
|
637
680
|
async function getDerivedEncryptionKey(secret: string) {
|
|
@@ -644,57 +687,6 @@ async function getDerivedEncryptionKey(secret: string) {
|
|
|
644
687
|
);
|
|
645
688
|
}
|
|
646
689
|
|
|
647
|
-
export function buildPath(params: {
|
|
648
|
-
fileInfo: RequestUploadBody['fileInfo'];
|
|
649
|
-
bucket: AnyBuilder;
|
|
650
|
-
pathAttrs: {
|
|
651
|
-
ctx: any;
|
|
652
|
-
input: any;
|
|
653
|
-
};
|
|
654
|
-
}) {
|
|
655
|
-
const { bucket } = params;
|
|
656
|
-
const pathParams = bucket._def.path;
|
|
657
|
-
const path = pathParams.map((param) => {
|
|
658
|
-
const paramEntries = Object.entries(param);
|
|
659
|
-
if (paramEntries[0] === undefined) {
|
|
660
|
-
throw new EdgeStoreError({
|
|
661
|
-
message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
|
|
662
|
-
code: 'SERVER_ERROR',
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
const [key, value] = paramEntries[0];
|
|
666
|
-
// this is a string like: "ctx.xxx" or "input.yyy.zzz"
|
|
667
|
-
const currParamVal = value()
|
|
668
|
-
.split('.')
|
|
669
|
-
.reduce((acc2: any, key: string) => {
|
|
670
|
-
if (acc2[key] === undefined) {
|
|
671
|
-
throw new EdgeStoreError({
|
|
672
|
-
message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
|
|
673
|
-
code: 'BAD_REQUEST',
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
return acc2[key];
|
|
677
|
-
}, params.pathAttrs as any) as string;
|
|
678
|
-
return {
|
|
679
|
-
key,
|
|
680
|
-
value: currParamVal,
|
|
681
|
-
};
|
|
682
|
-
});
|
|
683
|
-
return path;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
export function parsePath(path: { key: string; value: string }[]) {
|
|
687
|
-
const parsedPath = path.reduce<Record<string, string>>((acc, curr) => {
|
|
688
|
-
acc[curr.key] = curr.value;
|
|
689
|
-
return acc;
|
|
690
|
-
}, {});
|
|
691
|
-
const pathOrder = path.map((p) => p.key);
|
|
692
|
-
return {
|
|
693
|
-
parsedPath,
|
|
694
|
-
pathOrder,
|
|
695
|
-
};
|
|
696
|
-
}
|
|
697
|
-
|
|
698
690
|
async function getContext(token: string) {
|
|
699
691
|
return await decryptJWT(token);
|
|
700
692
|
}
|
|
@@ -716,20 +708,3 @@ function unproxyUrl(url: string) {
|
|
|
716
708
|
}
|
|
717
709
|
return url;
|
|
718
710
|
}
|
|
719
|
-
|
|
720
|
-
export function getEnv(key: string): string | undefined {
|
|
721
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
722
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
723
|
-
return process.env[key] ?? import.meta.env?.[key];
|
|
724
|
-
}
|
|
725
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
726
|
-
return import.meta.env?.[key];
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
export function isDev(): boolean {
|
|
730
|
-
return (
|
|
731
|
-
process?.env?.NODE_ENV === 'development' ||
|
|
732
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
733
|
-
import.meta.env?.DEV
|
|
734
|
-
);
|
|
735
|
-
}
|