@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
package/src/adapters/shared.ts
CHANGED
|
@@ -14,47 +14,23 @@ import { hkdf } from '@panva/hkdf';
|
|
|
14
14
|
import { stringifySetCookie } from 'cookie';
|
|
15
15
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
16
16
|
import { z } from 'zod';
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
assertSupportedUploadOptions,
|
|
19
|
+
getProviderBaseUrl,
|
|
20
|
+
referenceFromUrl,
|
|
21
|
+
} from '../core/provider';
|
|
18
22
|
import { buildPath, parseBucketInput, parsePath } from '../core/routerRules';
|
|
19
23
|
import { validateFileForBucket } from '../core/validateFile';
|
|
20
|
-
import { getEnv
|
|
24
|
+
import { getEnv } from '../libs/env';
|
|
21
25
|
import type { LoggerLike } from '../libs/logger';
|
|
22
26
|
|
|
23
27
|
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
24
28
|
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
|
|
25
29
|
|
|
26
|
-
export type
|
|
27
|
-
provider: AnyEdgeStoreProvider;
|
|
28
|
-
router: EdgeStoreRouter<TCtx>;
|
|
30
|
+
export type HandlerRouter<TCtx extends AnyContext> = EdgeStoreRouter<TCtx> & {
|
|
31
|
+
readonly _def: { readonly provider: AnyEdgeStoreProvider };
|
|
29
32
|
};
|
|
30
33
|
|
|
31
|
-
const NO_BODY_STATUSES = new Set([204, 205, 304]);
|
|
32
|
-
|
|
33
|
-
export async function fetchProxyFile({
|
|
34
|
-
cookieHeader,
|
|
35
|
-
url,
|
|
36
|
-
}: {
|
|
37
|
-
cookieHeader?: string;
|
|
38
|
-
url: string;
|
|
39
|
-
}) {
|
|
40
|
-
const proxyRes = await fetch(url, {
|
|
41
|
-
headers: {
|
|
42
|
-
cookie: cookieHeader ?? '',
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const body = NO_BODY_STATUSES.has(proxyRes.status)
|
|
47
|
-
? null
|
|
48
|
-
: await proxyRes.arrayBuffer();
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
body,
|
|
52
|
-
contentType:
|
|
53
|
-
proxyRes.headers.get('Content-Type') ?? 'application/octet-stream',
|
|
54
|
-
status: proxyRes.status,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
34
|
export type CookieOptions = {
|
|
59
35
|
/**
|
|
60
36
|
* Cookie path
|
|
@@ -99,20 +75,6 @@ export type CookieConfig = {
|
|
|
99
75
|
*/
|
|
100
76
|
options?: CookieOptions;
|
|
101
77
|
};
|
|
102
|
-
/**
|
|
103
|
-
* Token cookie configuration
|
|
104
|
-
*/
|
|
105
|
-
token?: {
|
|
106
|
-
/**
|
|
107
|
-
* Name of the token cookie
|
|
108
|
-
* @default "edgestore-token"
|
|
109
|
-
*/
|
|
110
|
-
name?: string;
|
|
111
|
-
/**
|
|
112
|
-
* Cookie options for token cookie
|
|
113
|
-
*/
|
|
114
|
-
options?: CookieOptions;
|
|
115
|
-
};
|
|
116
78
|
};
|
|
117
79
|
|
|
118
80
|
type ResolvedCookieConfig = {
|
|
@@ -120,10 +82,6 @@ type ResolvedCookieConfig = {
|
|
|
120
82
|
name: string;
|
|
121
83
|
options: CookieOptions;
|
|
122
84
|
};
|
|
123
|
-
token: {
|
|
124
|
-
name: string;
|
|
125
|
-
options: CookieOptions;
|
|
126
|
-
};
|
|
127
85
|
};
|
|
128
86
|
|
|
129
87
|
/**
|
|
@@ -158,10 +116,6 @@ export function getCookieConfig(
|
|
|
158
116
|
name: cookieConfig?.ctx?.name ?? 'edgestore-ctx',
|
|
159
117
|
options: mergeOptions(cookieConfig?.ctx?.options),
|
|
160
118
|
},
|
|
161
|
-
token: {
|
|
162
|
-
name: cookieConfig?.token?.name ?? 'edgestore-token',
|
|
163
|
-
options: mergeOptions(cookieConfig?.token?.options),
|
|
164
|
-
},
|
|
165
119
|
};
|
|
166
120
|
}
|
|
167
121
|
|
|
@@ -186,16 +140,7 @@ export async function init<TCtx extends AnyContext>(params: {
|
|
|
186
140
|
...resolvedCookieConfig.ctx.options,
|
|
187
141
|
}),
|
|
188
142
|
];
|
|
189
|
-
|
|
190
|
-
newCookies.push(
|
|
191
|
-
stringifySetCookie({
|
|
192
|
-
name: resolvedCookieConfig.token.name,
|
|
193
|
-
value: initRes.token,
|
|
194
|
-
...resolvedCookieConfig.token.options,
|
|
195
|
-
}),
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
const baseUrl = await getProviderBaseUrl(provider);
|
|
143
|
+
const baseUrl = initRes.baseUrl ?? (await getProviderBaseUrl(provider));
|
|
199
144
|
|
|
200
145
|
logger.debug('Finished [init]', {
|
|
201
146
|
ctx,
|
|
@@ -263,6 +208,7 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
263
208
|
code: 'BAD_REQUEST',
|
|
264
209
|
});
|
|
265
210
|
}
|
|
211
|
+
assertSupportedUploadOptions(provider, fileInfo);
|
|
266
212
|
const parsedInput = await parseBucketInput(bucket, input);
|
|
267
213
|
if (bucket._def.beforeUpload) {
|
|
268
214
|
logger.debug('Running [beforeUpload]');
|
|
@@ -333,96 +279,59 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
333
279
|
};
|
|
334
280
|
}
|
|
335
281
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}),
|
|
341
|
-
path: nonEmptyStringSchema,
|
|
282
|
+
const multipartSessionBodySchema = z.object({
|
|
283
|
+
bucketName: nonEmptyStringSchema,
|
|
284
|
+
uploadId: nonEmptyStringSchema,
|
|
285
|
+
key: nonEmptyStringSchema,
|
|
342
286
|
});
|
|
343
287
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
>;
|
|
288
|
+
/** Bounds the presigning work one request can trigger. Clients ask for 10. */
|
|
289
|
+
const MAX_PART_URLS_PER_REQUEST = 100;
|
|
347
290
|
|
|
348
|
-
export
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}): Promise<SharedRequestUploadPartsRes> {
|
|
355
|
-
const {
|
|
356
|
-
provider,
|
|
357
|
-
ctxToken,
|
|
358
|
-
logger,
|
|
359
|
-
body: { multipart, path },
|
|
360
|
-
} = params;
|
|
361
|
-
|
|
362
|
-
logger.debug('Running [requestUploadParts]', { multipart, path });
|
|
363
|
-
|
|
364
|
-
if (!ctxToken) {
|
|
365
|
-
throw new EdgeStoreError({
|
|
366
|
-
message: 'Missing edgestore-ctx cookie',
|
|
367
|
-
code: 'UNAUTHORIZED',
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
await getContext(ctxToken); // just to check if the token is valid
|
|
291
|
+
export const requestUploadPartsBodySchema = multipartSessionBodySchema.extend({
|
|
292
|
+
parts: z
|
|
293
|
+
.array(z.number().int().positive())
|
|
294
|
+
.min(1)
|
|
295
|
+
.max(MAX_PART_URLS_PER_REQUEST),
|
|
296
|
+
});
|
|
371
297
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
multipart,
|
|
381
|
-
path,
|
|
298
|
+
export const completeMultipartUploadBodySchema =
|
|
299
|
+
multipartSessionBodySchema.extend({
|
|
300
|
+
parts: z.array(
|
|
301
|
+
z.object({
|
|
302
|
+
partNumber: z.number().int().positive(),
|
|
303
|
+
eTag: nonEmptyStringSchema,
|
|
304
|
+
}),
|
|
305
|
+
),
|
|
382
306
|
});
|
|
383
307
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
return res;
|
|
387
|
-
}
|
|
388
|
-
|
|
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
|
-
});
|
|
308
|
+
export const abortMultipartUploadBodySchema = multipartSessionBodySchema;
|
|
400
309
|
|
|
310
|
+
export type RequestUploadPartsBody = z.infer<
|
|
311
|
+
typeof requestUploadPartsBodySchema
|
|
312
|
+
>;
|
|
401
313
|
export type CompleteMultipartUploadBody = z.infer<
|
|
402
314
|
typeof completeMultipartUploadBodySchema
|
|
403
315
|
>;
|
|
316
|
+
export type AbortMultipartUploadBody = z.infer<
|
|
317
|
+
typeof abortMultipartUploadBodySchema
|
|
318
|
+
>;
|
|
404
319
|
|
|
405
|
-
|
|
320
|
+
type MultipartRequest<TCtx extends AnyContext, TBody> = {
|
|
406
321
|
provider: AnyEdgeStoreProvider;
|
|
407
322
|
router: EdgeStoreRouter<TCtx>;
|
|
408
323
|
ctxToken: string | undefined;
|
|
409
|
-
body:
|
|
324
|
+
body: TBody;
|
|
410
325
|
logger: LoggerLike;
|
|
411
|
-
}
|
|
412
|
-
const {
|
|
413
|
-
provider,
|
|
414
|
-
router,
|
|
415
|
-
ctxToken,
|
|
416
|
-
logger,
|
|
417
|
-
body: { bucketName, uploadId, key, parts },
|
|
418
|
-
} = params;
|
|
419
|
-
|
|
420
|
-
logger.debug('Running [completeMultipartUpload]', {
|
|
421
|
-
bucketName,
|
|
422
|
-
uploadId,
|
|
423
|
-
key,
|
|
424
|
-
});
|
|
326
|
+
};
|
|
425
327
|
|
|
328
|
+
/** Authorizes a multipart session request and returns the provider operations. */
|
|
329
|
+
async function getMultipartUploads<TCtx extends AnyContext>({
|
|
330
|
+
provider,
|
|
331
|
+
router,
|
|
332
|
+
ctxToken,
|
|
333
|
+
body: { bucketName },
|
|
334
|
+
}: MultipartRequest<TCtx, { bucketName: string }>) {
|
|
426
335
|
if (!ctxToken) {
|
|
427
336
|
throw new EdgeStoreError({
|
|
428
337
|
message: 'Missing edgestore-ctx cookie',
|
|
@@ -430,14 +339,12 @@ export async function completeMultipartUpload<TCtx extends AnyContext>(params: {
|
|
|
430
339
|
});
|
|
431
340
|
}
|
|
432
341
|
await getContext(ctxToken); // just to check if the token is valid
|
|
433
|
-
|
|
434
|
-
if (!bucket) {
|
|
342
|
+
if (!router.buckets[bucketName]) {
|
|
435
343
|
throw new EdgeStoreError({
|
|
436
344
|
message: `Bucket ${bucketName} not found`,
|
|
437
345
|
code: 'BAD_REQUEST',
|
|
438
346
|
});
|
|
439
347
|
}
|
|
440
|
-
|
|
441
348
|
const multipartUploads = provider.uploads.multipart;
|
|
442
349
|
if (!multipartUploads) {
|
|
443
350
|
throw new EdgeStoreError({
|
|
@@ -445,13 +352,51 @@ export async function completeMultipartUpload<TCtx extends AnyContext>(params: {
|
|
|
445
352
|
code: 'BAD_REQUEST',
|
|
446
353
|
});
|
|
447
354
|
}
|
|
448
|
-
|
|
355
|
+
return multipartUploads;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export async function requestUploadParts<TCtx extends AnyContext>(
|
|
359
|
+
params: MultipartRequest<TCtx, RequestUploadPartsBody>,
|
|
360
|
+
): Promise<SharedRequestUploadPartsRes> {
|
|
361
|
+
const { bucketName, uploadId, key, parts } = params.body;
|
|
362
|
+
params.logger.debug('Running [requestUploadParts]', {
|
|
363
|
+
bucketName,
|
|
449
364
|
uploadId,
|
|
450
365
|
key,
|
|
451
366
|
parts,
|
|
452
367
|
});
|
|
368
|
+
const multipartUploads = await getMultipartUploads(params);
|
|
369
|
+
const res = await multipartUploads.requestParts({ uploadId, key, parts });
|
|
370
|
+
params.logger.debug('Finished [requestUploadParts]');
|
|
371
|
+
return res;
|
|
372
|
+
}
|
|
453
373
|
|
|
454
|
-
|
|
374
|
+
export async function completeMultipartUpload<TCtx extends AnyContext>(
|
|
375
|
+
params: MultipartRequest<TCtx, CompleteMultipartUploadBody>,
|
|
376
|
+
) {
|
|
377
|
+
const { bucketName, uploadId, key, parts } = params.body;
|
|
378
|
+
params.logger.debug('Running [completeMultipartUpload]', {
|
|
379
|
+
bucketName,
|
|
380
|
+
uploadId,
|
|
381
|
+
key,
|
|
382
|
+
});
|
|
383
|
+
const multipartUploads = await getMultipartUploads(params);
|
|
384
|
+
await multipartUploads.complete({ uploadId, key, parts });
|
|
385
|
+
params.logger.debug('Finished [completeMultipartUpload]');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export async function abortMultipartUpload<TCtx extends AnyContext>(
|
|
389
|
+
params: MultipartRequest<TCtx, AbortMultipartUploadBody>,
|
|
390
|
+
) {
|
|
391
|
+
const { bucketName, uploadId, key } = params.body;
|
|
392
|
+
params.logger.debug('Running [abortMultipartUpload]', {
|
|
393
|
+
bucketName,
|
|
394
|
+
uploadId,
|
|
395
|
+
key,
|
|
396
|
+
});
|
|
397
|
+
const multipartUploads = await getMultipartUploads(params);
|
|
398
|
+
await multipartUploads.abort({ uploadId, key });
|
|
399
|
+
params.logger.debug('Finished [abortMultipartUpload]');
|
|
455
400
|
}
|
|
456
401
|
|
|
457
402
|
export const confirmUploadsBodySchema = z.object({
|
|
@@ -500,7 +445,7 @@ export async function confirmUploads<TCtx extends AnyContext>(params: {
|
|
|
500
445
|
});
|
|
501
446
|
}
|
|
502
447
|
const files = await Promise.all(
|
|
503
|
-
urls.map((url) => referenceFromUrl(provider,
|
|
448
|
+
urls.map((url) => referenceFromUrl(provider, url)),
|
|
504
449
|
);
|
|
505
450
|
const result = await provider.files.confirm({
|
|
506
451
|
bucketName,
|
|
@@ -565,7 +510,7 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
565
510
|
});
|
|
566
511
|
}
|
|
567
512
|
const files = await Promise.all(
|
|
568
|
-
urls.map((url) => referenceFromUrl(provider,
|
|
513
|
+
urls.map((url) => referenceFromUrl(provider, url)),
|
|
569
514
|
);
|
|
570
515
|
const fileRecords = await Promise.all(
|
|
571
516
|
files.map((file) =>
|
|
@@ -690,21 +635,3 @@ async function getDerivedEncryptionKey(secret: string) {
|
|
|
690
635
|
async function getContext(token: string) {
|
|
691
636
|
return await decryptJWT(token);
|
|
692
637
|
}
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* On local development, protected files are proxied to the server,
|
|
696
|
-
* which changes the original URL.
|
|
697
|
-
*
|
|
698
|
-
* This function is used to get the original URL,
|
|
699
|
-
* so that we can delete or confirm the upload.
|
|
700
|
-
*/
|
|
701
|
-
function unproxyUrl(url: string) {
|
|
702
|
-
if (isDev() && url.startsWith('http://')) {
|
|
703
|
-
// get the url param from the query string
|
|
704
|
-
const urlParam = new URL(url).searchParams.get('url');
|
|
705
|
-
if (urlParam) {
|
|
706
|
-
return urlParam;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
return url;
|
|
710
|
-
}
|
|
@@ -5,14 +5,14 @@ import {
|
|
|
5
5
|
resolveContext,
|
|
6
6
|
type CreateContextConfig,
|
|
7
7
|
} from '../dispatcher';
|
|
8
|
-
import type { CookieConfig,
|
|
8
|
+
import type { CookieConfig, HandlerRouter } from '../shared';
|
|
9
9
|
|
|
10
10
|
export type CreateContextOptions = {
|
|
11
11
|
req: Request;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export type Config<TCtx extends AnyContext> = {
|
|
15
|
-
|
|
15
|
+
router: HandlerRouter<TCtx>;
|
|
16
16
|
logLevel?: LogLevel;
|
|
17
17
|
cookieConfig?: CookieConfig;
|
|
18
18
|
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
@@ -26,13 +26,12 @@ export function createEdgeStoreStartHandler<TCtx extends AnyContext>(
|
|
|
26
26
|
return async ({ request }: { request: Request }) => {
|
|
27
27
|
const url = new URL(request.url);
|
|
28
28
|
return await dispatchEdgeStoreRequest<TCtx>({
|
|
29
|
-
|
|
29
|
+
router: config.router,
|
|
30
30
|
logger: log,
|
|
31
31
|
cookieConfig: config.cookieConfig,
|
|
32
32
|
request: {
|
|
33
33
|
pathname: url.pathname,
|
|
34
34
|
readJson: () => request.json(),
|
|
35
|
-
getQuery: (name) => url.searchParams.get(name) ?? undefined,
|
|
36
35
|
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
37
36
|
createContext: () =>
|
|
38
37
|
resolveContext<TCtx, CreateContextOptions>(config, {
|
|
@@ -24,8 +24,11 @@ import type {
|
|
|
24
24
|
UploadContent,
|
|
25
25
|
UploadFileRequest,
|
|
26
26
|
} from '.';
|
|
27
|
-
import {
|
|
28
|
-
|
|
27
|
+
import {
|
|
28
|
+
assertSupportedUploadOptions,
|
|
29
|
+
validateProviderCursor,
|
|
30
|
+
validateProviderReference,
|
|
31
|
+
} from '../provider';
|
|
29
32
|
import { buildPath, parseBucketInput, parsePath } from '../routerRules';
|
|
30
33
|
import { validateFileForBucket } from '../validateFile';
|
|
31
34
|
|
|
@@ -36,7 +39,6 @@ type BucketContext<
|
|
|
36
39
|
bucket: TBucket;
|
|
37
40
|
bucketName: string;
|
|
38
41
|
provider: TProvider;
|
|
39
|
-
baseUrl?: string;
|
|
40
42
|
};
|
|
41
43
|
|
|
42
44
|
type UploadImplementationParams = {
|
|
@@ -54,7 +56,6 @@ export function createBucketClient<
|
|
|
54
56
|
bucketName: TName,
|
|
55
57
|
options: {
|
|
56
58
|
provider: TProvider;
|
|
57
|
-
baseUrl?: string;
|
|
58
59
|
},
|
|
59
60
|
): BucketClient<TRouter['buckets'][TName], TProvider> {
|
|
60
61
|
type TBucket = TRouter['buckets'][TName];
|
|
@@ -94,6 +95,7 @@ function createUploadMethods<
|
|
|
94
95
|
return {
|
|
95
96
|
upload: async (params: Prettify<UploadFileRequest<TBucket>>) => {
|
|
96
97
|
const { content, ctx = {}, input }: UploadImplementationParams = params;
|
|
98
|
+
assertSupportedUploadOptions(context.provider, params.options ?? {});
|
|
97
99
|
let { blob, extension } = await resolveUploadContent(
|
|
98
100
|
content,
|
|
99
101
|
params.signal,
|
|
@@ -146,7 +148,7 @@ function createUploadMethods<
|
|
|
146
148
|
|
|
147
149
|
const { parsedPath, pathOrder } = parsePath<TBucket>(path);
|
|
148
150
|
return {
|
|
149
|
-
...mapFileRecord(uploadResult.file
|
|
151
|
+
...mapFileRecord(uploadResult.file),
|
|
150
152
|
...mapSignedReadAccess(uploadResult.signedReadUrl),
|
|
151
153
|
metadata,
|
|
152
154
|
path: parsedPath,
|
|
@@ -169,7 +171,7 @@ function createGetMethods<
|
|
|
169
171
|
bucketName: context.bucketName,
|
|
170
172
|
file: await validateProviderReference(context.provider, ref),
|
|
171
173
|
});
|
|
172
|
-
return
|
|
174
|
+
return mapFileRecord(file);
|
|
173
175
|
},
|
|
174
176
|
};
|
|
175
177
|
}
|
|
@@ -236,9 +238,7 @@ function createListMethods<
|
|
|
236
238
|
});
|
|
237
239
|
return {
|
|
238
240
|
...result,
|
|
239
|
-
items: result.items.map((file) =>
|
|
240
|
-
mapBucketFileRecord(file, context.baseUrl),
|
|
241
|
-
),
|
|
241
|
+
items: result.items.map((file) => mapFileRecord(file)),
|
|
242
242
|
};
|
|
243
243
|
};
|
|
244
244
|
|
|
@@ -391,45 +391,14 @@ function mapSignedUrl<TSignedUrl extends { expiresAt: Date | string }>(
|
|
|
391
391
|
};
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
function mapFileRecord<TFile extends BackendFile>(
|
|
395
|
-
file: TFile,
|
|
396
|
-
baseUrl?: string,
|
|
397
|
-
) {
|
|
394
|
+
function mapFileRecord<TFile extends BackendFile>(file: TFile) {
|
|
398
395
|
return {
|
|
399
396
|
...file,
|
|
400
|
-
url: getUrl(file.url, baseUrl),
|
|
401
397
|
uploadedAt: new Date(file.uploadedAt),
|
|
402
398
|
updatedAt: new Date(file.updatedAt),
|
|
403
399
|
};
|
|
404
400
|
}
|
|
405
401
|
|
|
406
|
-
function mapBucketFileRecord<TFile extends BackendFile>(
|
|
407
|
-
file: TFile,
|
|
408
|
-
baseUrl?: string,
|
|
409
|
-
) {
|
|
410
|
-
return mapFileRecord(file, baseUrl);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Protected files need third-party cookies to work.
|
|
415
|
-
* Since third party cookies don't work on localhost,
|
|
416
|
-
* we need to proxy the file through the server.
|
|
417
|
-
*/
|
|
418
|
-
function getUrl(url: string, baseUrl?: string) {
|
|
419
|
-
if (isDev() && !url.includes('/_public/')) {
|
|
420
|
-
if (!baseUrl) {
|
|
421
|
-
throw new Error(
|
|
422
|
-
'Missing baseUrl. Pass the baseUrl to `createEdgeStore` to get protected files in development.',
|
|
423
|
-
);
|
|
424
|
-
}
|
|
425
|
-
const proxyUrl = new URL(baseUrl);
|
|
426
|
-
proxyUrl.pathname = `${proxyUrl.pathname}/proxy-file`;
|
|
427
|
-
proxyUrl.search = new URLSearchParams({ url }).toString();
|
|
428
|
-
return proxyUrl.toString();
|
|
429
|
-
}
|
|
430
|
-
return url;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
402
|
function mapMutationResult<TFileReference, TErrorCode extends string>(
|
|
434
403
|
files: TFileReference[],
|
|
435
404
|
result: ProviderFileMutationResult<TErrorCode>,
|
package/src/core/client/index.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
type AnyEdgeStoreProvider,
|
|
4
4
|
type AnyRouter,
|
|
5
5
|
type BackendFile,
|
|
6
|
-
type DefaultEdgeStoreProvider,
|
|
7
6
|
type FileReference,
|
|
8
7
|
type InferBucketPathKeys,
|
|
9
8
|
type InferBucketPathObject,
|
|
@@ -21,6 +20,7 @@ import {
|
|
|
21
20
|
type ProviderMutationError,
|
|
22
21
|
type ProviderReference,
|
|
23
22
|
type ProviderReferenceInput,
|
|
23
|
+
type ProviderUploadOptions,
|
|
24
24
|
type Simplify,
|
|
25
25
|
} from '@edgestore/shared';
|
|
26
26
|
import { createBucketClient } from './bucketClient';
|
|
@@ -96,16 +96,16 @@ export type UploadOptions = {
|
|
|
96
96
|
* But it might take some time for the CDN cache to be cleared.
|
|
97
97
|
* So maybe you will keep seeing the old file for a while.
|
|
98
98
|
*
|
|
99
|
-
*
|
|
99
|
+
* For providers that support managed replacement, leave `manualFileName` empty and use `replaceTargetUrl`.
|
|
100
100
|
*/
|
|
101
101
|
manualFileName?: string;
|
|
102
102
|
/**
|
|
103
|
-
*
|
|
103
|
+
* Replace an existing file, when supported by the provider.
|
|
104
104
|
* It will automatically delete the existing file when the upload is complete.
|
|
105
105
|
*/
|
|
106
106
|
replaceTargetUrl?: string;
|
|
107
107
|
/**
|
|
108
|
-
*
|
|
108
|
+
* When supported by the provider, the file needs to be confirmed by using the `confirm` function.
|
|
109
109
|
* If the file is not confirmed within 24 hours, it will be deleted.
|
|
110
110
|
*
|
|
111
111
|
* This is useful for pages where the file is uploaded as soon as it is selected,
|
|
@@ -145,7 +145,10 @@ export type ServerUploadTransform = (params: {
|
|
|
145
145
|
extension: string;
|
|
146
146
|
}>;
|
|
147
147
|
|
|
148
|
-
export type UploadFileRequest<
|
|
148
|
+
export type UploadFileRequest<
|
|
149
|
+
TBucket extends AnyBuilder,
|
|
150
|
+
TProvider = unknown,
|
|
151
|
+
> = {
|
|
149
152
|
/**
|
|
150
153
|
* Can be a string, a blob or an url.
|
|
151
154
|
*
|
|
@@ -170,7 +173,7 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
170
173
|
* }
|
|
171
174
|
*/
|
|
172
175
|
content: UploadContent;
|
|
173
|
-
options?: UploadOptions
|
|
176
|
+
options?: ProviderUploadOptions<UploadOptions, TProvider>;
|
|
174
177
|
signal?: AbortSignal;
|
|
175
178
|
onProgress?: (progress: {
|
|
176
179
|
transferredBytes: number;
|
|
@@ -279,7 +282,7 @@ type UploadBucketClient<TBucket extends AnyBuilder, TProvider> = [
|
|
|
279
282
|
: {
|
|
280
283
|
/** Upload a file directly from the backend. */
|
|
281
284
|
upload: (
|
|
282
|
-
params: Prettify<UploadFileRequest<TBucket>>,
|
|
285
|
+
params: Prettify<UploadFileRequest<TBucket, TProvider>>,
|
|
283
286
|
) => Promise<
|
|
284
287
|
Prettify<
|
|
285
288
|
UploadFileRes<TBucket, ProviderCapabilityFile<TProvider, 'upload'>>
|
|
@@ -367,9 +370,9 @@ export type BucketClient<
|
|
|
367
370
|
ListBucketClient<TBucket, TProvider> &
|
|
368
371
|
SignedUrlBucketClient<TBucket, TProvider>;
|
|
369
372
|
|
|
370
|
-
|
|
373
|
+
type BackendClient<
|
|
371
374
|
TRouter extends AnyRouter,
|
|
372
|
-
TProvider extends AnyBackendProvider
|
|
375
|
+
TProvider extends AnyBackendProvider,
|
|
373
376
|
> = {
|
|
374
377
|
[K in keyof TRouter['buckets']]: BucketClient<
|
|
375
378
|
TRouter['buckets'][K],
|
|
@@ -380,19 +383,15 @@ export type EdgeStoreClient<
|
|
|
380
383
|
export function createBackendClient<
|
|
381
384
|
TRouter extends AnyRouter,
|
|
382
385
|
TProvider extends AnyBackendProvider,
|
|
383
|
-
>(
|
|
384
|
-
router: TRouter,
|
|
385
|
-
provider: TProvider,
|
|
386
|
-
baseUrl?: string,
|
|
387
|
-
): EdgeStoreClient<TRouter, TProvider> {
|
|
386
|
+
>(router: TRouter, provider: TProvider): BackendClient<TRouter, TProvider> {
|
|
388
387
|
const bucketNames = Object.keys(router.buckets) as (keyof TRouter['buckets'] &
|
|
389
388
|
string)[];
|
|
390
389
|
const entries = bucketNames.map((bucketName) => [
|
|
391
390
|
bucketName,
|
|
392
|
-
createBucketClient(router, bucketName, { provider
|
|
391
|
+
createBucketClient(router, bucketName, { provider }),
|
|
393
392
|
]);
|
|
394
393
|
|
|
395
|
-
return Object.fromEntries(entries) as
|
|
394
|
+
return Object.fromEntries(entries) as BackendClient<TRouter, TProvider>;
|
|
396
395
|
}
|
|
397
396
|
|
|
398
397
|
type ClientMethodInput<TMethod> = TMethod extends (
|
|
@@ -407,43 +406,28 @@ type ClientMethodOutput<TMethod> = TMethod extends (
|
|
|
407
406
|
? Simplify<Awaited<TResult>>
|
|
408
407
|
: never;
|
|
409
408
|
|
|
410
|
-
/**
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
TClientFn in keyof EdgeStoreClient<TRouter, TProvider>[TBucketName]
|
|
420
|
-
]: ClientMethodInput<
|
|
421
|
-
EdgeStoreClient<TRouter, TProvider>[TBucketName][TClientFn]
|
|
409
|
+
/** The backend client exposed by a configured router. */
|
|
410
|
+
export type EdgeStoreClient<TRouter extends { readonly client: object }> =
|
|
411
|
+
TRouter['client'];
|
|
412
|
+
|
|
413
|
+
/** Infers the input accepted by every method on the router's backend client. */
|
|
414
|
+
export type InferClientInputs<TRouter extends { readonly client: object }> = {
|
|
415
|
+
[TBucket in keyof TRouter['client']]: {
|
|
416
|
+
[TMethod in keyof TRouter['client'][TBucket]]: ClientMethodInput<
|
|
417
|
+
TRouter['client'][TBucket][TMethod]
|
|
422
418
|
>;
|
|
423
419
|
};
|
|
424
420
|
};
|
|
425
421
|
|
|
426
|
-
/**
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
TRouter extends AnyRouter,
|
|
432
|
-
TProvider extends AnyBackendProvider = DefaultEdgeStoreProvider,
|
|
433
|
-
> = {
|
|
434
|
-
[TBucketName in keyof TRouter['buckets']]: {
|
|
435
|
-
[
|
|
436
|
-
TClientFn in keyof EdgeStoreClient<TRouter, TProvider>[TBucketName]
|
|
437
|
-
]: ClientMethodOutput<
|
|
438
|
-
EdgeStoreClient<TRouter, TProvider>[TBucketName][TClientFn]
|
|
422
|
+
/** Infers the resolved output of every method on the router's backend client. */
|
|
423
|
+
export type InferClientOutputs<TRouter extends { readonly client: object }> = {
|
|
424
|
+
[TBucket in keyof TRouter['client']]: {
|
|
425
|
+
[TMethod in keyof TRouter['client'][TBucket]]: ClientMethodOutput<
|
|
426
|
+
TRouter['client'][TBucket][TMethod]
|
|
439
427
|
>;
|
|
440
428
|
};
|
|
441
429
|
};
|
|
442
430
|
|
|
443
|
-
/**
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
export type InferClientResponse<
|
|
447
|
-
TRouter extends AnyRouter,
|
|
448
|
-
TProvider extends AnyBackendProvider = DefaultEdgeStoreProvider,
|
|
449
|
-
> = InferClientOutputs<TRouter, TProvider>;
|
|
431
|
+
/** @deprecated Use {@link InferClientOutputs} instead. */
|
|
432
|
+
export type InferClientResponse<TRouter extends { readonly client: object }> =
|
|
433
|
+
InferClientOutputs<TRouter>;
|