@edgestore/server 1.0.0-rc.7 → 1.0.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 +2 -0
- package/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +2 -2
- package/agent-docs/backend-client.md +5 -2
- package/agent-docs/configuration.md +6 -5
- package/agent-docs/express.md +3 -3
- package/agent-docs/fastify.md +3 -3
- package/agent-docs/hono.md +3 -3
- package/agent-docs/next.md +2 -2
- package/agent-docs/provider-azure-blob.md +1 -1
- package/agent-docs/provider-custom.md +6 -1
- package/agent-docs/provider-edgestore.md +1 -1
- package/agent-docs/provider-s3.md +1 -1
- package/agent-docs/remix.md +3 -3
- package/agent-docs/tanstack-start.md +3 -3
- package/agent-docs/troubleshooting.md +4 -4
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +1 -0
- package/dist/adapters/dispatcher.d.ts +1 -0
- package/dist/adapters/dispatcher.d.ts.map +1 -1
- package/dist/adapters/dispatcher.js +11 -1
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +1 -0
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +1 -0
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +1 -0
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +1 -0
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +1 -0
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +1 -0
- package/dist/adapters/shared.d.ts +11 -4
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +75 -27
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +1 -0
- package/dist/libs/env.d.ts +1 -0
- package/dist/libs/env.d.ts.map +1 -1
- package/dist/libs/env.js +4 -1
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +27 -12
- package/dist/providers/edgestore/index.d.ts +1 -1
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +37 -14
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +34 -16
- package/dist/providers/storage/objectKeys.d.ts +8 -0
- package/dist/providers/storage/objectKeys.d.ts.map +1 -1
- package/dist/providers/storage/objectKeys.js +18 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/adapters/astro/index.ts +1 -0
- package/src/adapters/dispatcher.ts +12 -0
- package/src/adapters/express/index.ts +1 -0
- package/src/adapters/fastify/index.ts +1 -0
- package/src/adapters/hono/index.ts +1 -0
- package/src/adapters/next/app/index.ts +1 -0
- package/src/adapters/next/pages/index.ts +1 -0
- package/src/adapters/remix/index.ts +1 -0
- package/src/adapters/shared.ts +125 -34
- package/src/adapters/start/index.ts +1 -0
- package/src/libs/env.ts +4 -0
- package/src/providers/azure-blob/index.ts +32 -11
- package/src/providers/edgestore/index.ts +42 -20
- package/src/providers/s3/index.ts +41 -14
- package/src/providers/storage/objectKeys.ts +21 -0
- package/src/version.ts +1 -1
|
@@ -45,6 +45,7 @@ export function createEdgeStoreNextHandler<TCtx extends AnyContext>(
|
|
|
45
45
|
request: {
|
|
46
46
|
pathname: url.pathname,
|
|
47
47
|
readJson: async () => req.body,
|
|
48
|
+
contentType: req.headers['content-type'],
|
|
48
49
|
cookieHeader: req.headers.cookie,
|
|
49
50
|
cookies: req.cookies,
|
|
50
51
|
createContext: () =>
|
|
@@ -32,6 +32,7 @@ export function createEdgeStoreRemixHandler<TCtx extends AnyContext>(
|
|
|
32
32
|
request: {
|
|
33
33
|
pathname: url.pathname,
|
|
34
34
|
readJson: () => req.json(),
|
|
35
|
+
contentType: req.headers.get('content-type') ?? undefined,
|
|
35
36
|
cookieHeader: req.headers.get('cookie') ?? undefined,
|
|
36
37
|
createContext: () =>
|
|
37
38
|
resolveContext<TCtx, CreateContextOptions>(config, {
|
package/src/adapters/shared.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
EdgeStoreError,
|
|
3
3
|
type AnyContext,
|
|
4
4
|
type AnyEdgeStoreProvider,
|
|
5
|
+
type BackendFile,
|
|
5
6
|
type EdgeStoreRouter,
|
|
6
7
|
type ProviderFileMutationResult,
|
|
7
8
|
type SharedConfirmUploadsRes,
|
|
@@ -20,7 +21,7 @@ import {
|
|
|
20
21
|
} from '../core/provider';
|
|
21
22
|
import { buildPath, parseBucketInput, parsePath } from '../core/routerRules';
|
|
22
23
|
import { validateFileForBucket } from '../core/validateFile';
|
|
23
|
-
import { getEnv } from '../libs/env';
|
|
24
|
+
import { getEnv, isProduction } from '../libs/env';
|
|
24
25
|
import type { LoggerLike } from '../libs/logger';
|
|
25
26
|
|
|
26
27
|
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
@@ -37,7 +38,8 @@ export type CookieOptions = {
|
|
|
37
38
|
*/
|
|
38
39
|
path?: string;
|
|
39
40
|
/**
|
|
40
|
-
* Cookie max age in seconds
|
|
41
|
+
* Cookie max age in seconds. The encrypted context token expires at the
|
|
42
|
+
* same time.
|
|
41
43
|
* @default 2592000 (30 days)
|
|
42
44
|
*/
|
|
43
45
|
maxAge?: number;
|
|
@@ -46,15 +48,19 @@ export type CookieOptions = {
|
|
|
46
48
|
*/
|
|
47
49
|
domain?: string;
|
|
48
50
|
/**
|
|
49
|
-
* Cookie same site policy
|
|
51
|
+
* Cookie same site policy. Use `'none'` (with `secure: true`) only when the
|
|
52
|
+
* frontend calls the EdgeStore API from a different site.
|
|
53
|
+
* @default "lax"
|
|
50
54
|
*/
|
|
51
55
|
sameSite?: 'strict' | 'lax' | 'none';
|
|
52
56
|
/**
|
|
53
57
|
* Cookie secure flag
|
|
58
|
+
* @default true when NODE_ENV is "production", otherwise false
|
|
54
59
|
*/
|
|
55
60
|
secure?: boolean;
|
|
56
61
|
/**
|
|
57
62
|
* Cookie http only flag
|
|
63
|
+
* @default true
|
|
58
64
|
*/
|
|
59
65
|
httpOnly?: boolean;
|
|
60
66
|
};
|
|
@@ -79,12 +85,12 @@ export type CookieConfig = {
|
|
|
79
85
|
type ResolvedCookieConfig = {
|
|
80
86
|
ctx: {
|
|
81
87
|
name: string;
|
|
82
|
-
options: CookieOptions;
|
|
88
|
+
options: CookieOptions & { maxAge: number };
|
|
83
89
|
};
|
|
84
90
|
};
|
|
85
91
|
|
|
86
92
|
/**
|
|
87
|
-
* Merges the provided cookie configuration with default values
|
|
93
|
+
* Merges the provided cookie configuration with default values.
|
|
88
94
|
*/
|
|
89
95
|
export function getCookieConfig(
|
|
90
96
|
cookieConfig?: CookieConfig,
|
|
@@ -99,7 +105,14 @@ export function getCookieConfig(
|
|
|
99
105
|
return {
|
|
100
106
|
ctx: {
|
|
101
107
|
name: cookieConfig?.ctx?.name ?? 'edgestore-ctx',
|
|
102
|
-
options: {
|
|
108
|
+
options: {
|
|
109
|
+
path: '/',
|
|
110
|
+
maxAge: DEFAULT_MAX_AGE,
|
|
111
|
+
sameSite: 'lax',
|
|
112
|
+
httpOnly: true,
|
|
113
|
+
secure: isProduction(),
|
|
114
|
+
...configured,
|
|
115
|
+
},
|
|
103
116
|
},
|
|
104
117
|
};
|
|
105
118
|
}
|
|
@@ -116,7 +129,10 @@ export async function init<TCtx extends AnyContext>(params: {
|
|
|
116
129
|
|
|
117
130
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
118
131
|
|
|
119
|
-
const ctxToken = await encryptJWT(
|
|
132
|
+
const ctxToken = await encryptJWT(
|
|
133
|
+
ctx,
|
|
134
|
+
resolvedCookieConfig.ctx.options.maxAge,
|
|
135
|
+
);
|
|
120
136
|
const { clientInit } = await provider.init({ ctx, router });
|
|
121
137
|
const newCookies = [
|
|
122
138
|
stringifySetCookie({
|
|
@@ -194,6 +210,35 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
194
210
|
|
|
195
211
|
validateFileForBucket({ bucket, fileInfo });
|
|
196
212
|
|
|
213
|
+
// Replacing or overwriting a file deletes it, so it needs the same
|
|
214
|
+
// authorization as deleting it.
|
|
215
|
+
const authorizeReplacement =
|
|
216
|
+
(action: string, refusal: string) => async (file: BackendFile) => {
|
|
217
|
+
const beforeDelete = requireBeforeDelete(bucket, action);
|
|
218
|
+
if (
|
|
219
|
+
!(await canDelete({
|
|
220
|
+
provider,
|
|
221
|
+
bucket,
|
|
222
|
+
ctx,
|
|
223
|
+
beforeDelete,
|
|
224
|
+
files: [file],
|
|
225
|
+
}))
|
|
226
|
+
) {
|
|
227
|
+
throw new EdgeStoreError({
|
|
228
|
+
message: refusal,
|
|
229
|
+
code: 'UPLOAD_NOT_ALLOWED',
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
if (fileInfo.replaceTargetUrl) {
|
|
234
|
+
// The lookup is scoped to this bucket, so targets elsewhere are rejected.
|
|
235
|
+
const file = await referenceFromUrl(provider, fileInfo.replaceTargetUrl);
|
|
236
|
+
await authorizeReplacement(
|
|
237
|
+
'replace files',
|
|
238
|
+
'Replacing this file is not allowed for the current context',
|
|
239
|
+
)(await provider.files.get({ bucketName, file }));
|
|
240
|
+
}
|
|
241
|
+
|
|
197
242
|
const path = buildPath({
|
|
198
243
|
bucket,
|
|
199
244
|
pathAttrs: { ctx, input: parsedInput },
|
|
@@ -223,6 +268,13 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
223
268
|
metadata,
|
|
224
269
|
},
|
|
225
270
|
autoSignedUrls,
|
|
271
|
+
...(fileInfo.fileName !== undefined && {
|
|
272
|
+
// A client-chosen name can match an existing file.
|
|
273
|
+
authorizeOverwrite: authorizeReplacement(
|
|
274
|
+
`overwrite the existing file "${fileInfo.fileName}"`,
|
|
275
|
+
`A file named "${fileInfo.fileName}" already exists, and the current context may not overwrite it`,
|
|
276
|
+
),
|
|
277
|
+
}),
|
|
226
278
|
});
|
|
227
279
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
228
280
|
const statusToken =
|
|
@@ -479,14 +531,7 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
479
531
|
|
|
480
532
|
const ctx = await getContext(ctxToken);
|
|
481
533
|
const bucket = getBucket(router, bucketName);
|
|
482
|
-
|
|
483
|
-
if (!bucket._def.beforeDelete) {
|
|
484
|
-
throw new EdgeStoreError({
|
|
485
|
-
message:
|
|
486
|
-
'You need to define beforeDelete if you want to delete files directly from the frontend.',
|
|
487
|
-
code: 'SERVER_ERROR',
|
|
488
|
-
});
|
|
489
|
-
}
|
|
534
|
+
const beforeDelete = requireBeforeDelete(bucket, 'delete files directly');
|
|
490
535
|
|
|
491
536
|
if (!provider.files.delete) {
|
|
492
537
|
throw new EdgeStoreError({
|
|
@@ -502,8 +547,65 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
502
547
|
Promise.resolve(provider.files.get({ bucketName, file })),
|
|
503
548
|
),
|
|
504
549
|
);
|
|
550
|
+
if (
|
|
551
|
+
!(await canDelete({
|
|
552
|
+
provider,
|
|
553
|
+
bucket,
|
|
554
|
+
ctx,
|
|
555
|
+
beforeDelete,
|
|
556
|
+
files: fileRecords,
|
|
557
|
+
}))
|
|
558
|
+
) {
|
|
559
|
+
throw new EdgeStoreError({
|
|
560
|
+
message: 'Delete not allowed for the current context',
|
|
561
|
+
code: 'DELETE_NOT_ALLOWED',
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
const result = await provider.files.delete({
|
|
565
|
+
bucketName,
|
|
566
|
+
files,
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
logger.debug('Finished [deleteFiles]');
|
|
570
|
+
|
|
571
|
+
return mapFrontendMutationResult(urls, result);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
type Bucket<TCtx extends AnyContext> = EdgeStoreRouter<TCtx>['buckets'][string];
|
|
575
|
+
type BeforeDelete<TCtx extends AnyContext> = NonNullable<
|
|
576
|
+
Bucket<TCtx>['_def']['beforeDelete']
|
|
577
|
+
>;
|
|
578
|
+
|
|
579
|
+
function requireBeforeDelete<TCtx extends AnyContext>(
|
|
580
|
+
bucket: Bucket<TCtx>,
|
|
581
|
+
action: string,
|
|
582
|
+
) {
|
|
583
|
+
const { beforeDelete } = bucket._def;
|
|
584
|
+
if (!beforeDelete) {
|
|
585
|
+
throw new EdgeStoreError({
|
|
586
|
+
message: `You need to define beforeDelete if you want to ${action} from the frontend.`,
|
|
587
|
+
code: 'SERVER_ERROR',
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
return beforeDelete;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** Runs `beforeDelete` for every file and reports whether all are allowed. */
|
|
594
|
+
async function canDelete<TCtx extends AnyContext>({
|
|
595
|
+
provider,
|
|
596
|
+
bucket,
|
|
597
|
+
ctx,
|
|
598
|
+
beforeDelete,
|
|
599
|
+
files,
|
|
600
|
+
}: {
|
|
601
|
+
provider: AnyEdgeStoreProvider;
|
|
602
|
+
bucket: Bucket<TCtx>;
|
|
603
|
+
ctx: AnyContext;
|
|
604
|
+
beforeDelete: BeforeDelete<TCtx>;
|
|
605
|
+
files: BackendFile[];
|
|
606
|
+
}) {
|
|
505
607
|
const authorizations = await Promise.all(
|
|
506
|
-
|
|
608
|
+
files.map((file) => {
|
|
507
609
|
if (file.path === undefined && bucket._def.path.length > 0) {
|
|
508
610
|
throw new EdgeStoreError({
|
|
509
611
|
message: `Provider ${provider.name} must return path from files.get to authorize frontend deletion for a bucket with configured path fields.`,
|
|
@@ -517,7 +619,7 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
517
619
|
});
|
|
518
620
|
}
|
|
519
621
|
return Promise.resolve(
|
|
520
|
-
|
|
622
|
+
beforeDelete({
|
|
521
623
|
ctx,
|
|
522
624
|
fileInfo: {
|
|
523
625
|
url: file.url,
|
|
@@ -530,20 +632,7 @@ export async function deleteFiles<TCtx extends AnyContext>(params: {
|
|
|
530
632
|
);
|
|
531
633
|
}),
|
|
532
634
|
);
|
|
533
|
-
|
|
534
|
-
throw new EdgeStoreError({
|
|
535
|
-
message: 'Delete not allowed for the current context',
|
|
536
|
-
code: 'DELETE_NOT_ALLOWED',
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
const result = await provider.files.delete({
|
|
540
|
-
bucketName,
|
|
541
|
-
files,
|
|
542
|
-
});
|
|
543
|
-
|
|
544
|
-
logger.debug('Finished [deleteFiles]');
|
|
545
|
-
|
|
546
|
-
return mapFrontendMutationResult(urls, result);
|
|
635
|
+
return authorizations.every(Boolean);
|
|
547
636
|
}
|
|
548
637
|
|
|
549
638
|
function mapFrontendMutationResult(
|
|
@@ -579,11 +668,12 @@ function getBucket<TCtx extends AnyContext>(
|
|
|
579
668
|
return bucket;
|
|
580
669
|
}
|
|
581
670
|
|
|
582
|
-
|
|
671
|
+
/** Encrypts the context into a token that expires with its cookie. */
|
|
672
|
+
async function encryptJWT(ctx: AnyContext, maxAge: number) {
|
|
583
673
|
return await new EncryptJWT({ ctx })
|
|
584
674
|
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
|
585
675
|
.setIssuedAt()
|
|
586
|
-
.setExpirationTime(Date.now() / 1000 +
|
|
676
|
+
.setExpirationTime(Date.now() / 1000 + maxAge)
|
|
587
677
|
.setJti(crypto.randomUUID())
|
|
588
678
|
.encrypt(await getEncryptionKey());
|
|
589
679
|
}
|
|
@@ -631,7 +721,8 @@ const contextPayloadSchema = z.object({
|
|
|
631
721
|
async function getContext(token: string | undefined) {
|
|
632
722
|
if (!token) {
|
|
633
723
|
throw new EdgeStoreError({
|
|
634
|
-
message:
|
|
724
|
+
message:
|
|
725
|
+
'Missing edgestore-ctx cookie. When NODE_ENV is production the cookie is Secure, so browsers drop it over plain HTTP (Safari even on localhost). To test a production build over HTTP, set cookieConfig.ctx.options.secure to false.',
|
|
635
726
|
code: 'UNAUTHORIZED',
|
|
636
727
|
});
|
|
637
728
|
}
|
|
@@ -32,6 +32,7 @@ export function createEdgeStoreStartHandler<TCtx extends AnyContext>(
|
|
|
32
32
|
request: {
|
|
33
33
|
pathname: url.pathname,
|
|
34
34
|
readJson: () => request.json(),
|
|
35
|
+
contentType: request.headers.get('content-type') ?? undefined,
|
|
35
36
|
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
36
37
|
createContext: () =>
|
|
37
38
|
resolveContext<TCtx, CreateContextOptions>(config, {
|
package/src/libs/env.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
BlobSASPermissions,
|
|
4
4
|
BlobServiceClient,
|
|
5
5
|
generateBlobSASQueryParameters,
|
|
6
|
+
RestError,
|
|
6
7
|
SASProtocol,
|
|
7
8
|
StorageSharedKeyCredential,
|
|
8
9
|
} from '@azure/storage-blob';
|
|
@@ -18,6 +19,7 @@ import { getEnv } from '../../libs/env';
|
|
|
18
19
|
import { createMultipartSessions } from '../storage/multipartSession';
|
|
19
20
|
import {
|
|
20
21
|
createObjectKeys,
|
|
22
|
+
pathFromDefaultKey,
|
|
21
23
|
type ObjectPathFn,
|
|
22
24
|
type ObjectPathFnArgs,
|
|
23
25
|
} from '../storage/objectKeys';
|
|
@@ -204,6 +206,34 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
|
|
|
204
206
|
};
|
|
205
207
|
}
|
|
206
208
|
|
|
209
|
+
async function describeBlob(key: string) {
|
|
210
|
+
const { contentLength, lastModified } = await blob(key).getProperties();
|
|
211
|
+
if (contentLength === undefined || !lastModified)
|
|
212
|
+
throw new Error('File not found');
|
|
213
|
+
return {
|
|
214
|
+
key,
|
|
215
|
+
url: keys.toUrl(key),
|
|
216
|
+
sizeBytes: contentLength,
|
|
217
|
+
uploadedAt: lastModified,
|
|
218
|
+
updatedAt: lastModified,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Authorizes overwriting the blob at `key`, if one exists. */
|
|
223
|
+
async function authorizeOverwrite(key: string, params: RequestUploadParams) {
|
|
224
|
+
if (!params.authorizeOverwrite) return;
|
|
225
|
+
const existing = await describeBlob(key).catch((error: unknown) => {
|
|
226
|
+
if (error instanceof RestError && error.statusCode === 404)
|
|
227
|
+
return undefined;
|
|
228
|
+
throw error;
|
|
229
|
+
});
|
|
230
|
+
if (!existing) return;
|
|
231
|
+
await params.authorizeOverwrite({
|
|
232
|
+
...existing,
|
|
233
|
+
path: pathFromDefaultKey(params.fileInfo, options.path),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
207
237
|
function readForUpload(key: string, params: RequestUploadParams) {
|
|
208
238
|
if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
|
|
209
239
|
return signedRead(key, params.autoSignedUrls.expiresIn);
|
|
@@ -262,6 +292,7 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
|
|
|
262
292
|
supportedOptions: { temporary: false, replaceTargetUrl: false },
|
|
263
293
|
async request(params): Promise<RequestUploadRes> {
|
|
264
294
|
const { key, commit, plan } = await prepare(params);
|
|
295
|
+
await authorizeOverwrite(key, params);
|
|
265
296
|
const signedReadUrl = readForUpload(key, params);
|
|
266
297
|
const access = {
|
|
267
298
|
key,
|
|
@@ -320,17 +351,7 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
|
|
|
320
351
|
},
|
|
321
352
|
files: {
|
|
322
353
|
async get({ bucketName, file }) {
|
|
323
|
-
|
|
324
|
-
const { contentLength, lastModified } = await blob(key).getProperties();
|
|
325
|
-
if (contentLength === undefined || !lastModified)
|
|
326
|
-
throw new Error('File not found');
|
|
327
|
-
return {
|
|
328
|
-
key,
|
|
329
|
-
url: keys.toUrl(key),
|
|
330
|
-
sizeBytes: contentLength,
|
|
331
|
-
uploadedAt: lastModified,
|
|
332
|
-
updatedAt: lastModified,
|
|
333
|
-
};
|
|
354
|
+
return describeBlob(keys.fromReference(bucketName, file));
|
|
334
355
|
},
|
|
335
356
|
async getSignedUrls({ bucketName, files, expiresIn }) {
|
|
336
357
|
return files.map((file) =>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createEdgeStoreSdk,
|
|
3
|
+
EdgeStoreApiError,
|
|
3
4
|
planMultipartUpload,
|
|
4
5
|
type ProjectRuntimeClient,
|
|
5
6
|
} from '@edgestore/sdk';
|
|
@@ -131,33 +132,44 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
|
131
132
|
bucketType,
|
|
132
133
|
fileInfo,
|
|
133
134
|
autoSignedUrls,
|
|
135
|
+
authorizeOverwrite,
|
|
134
136
|
}): Promise<RequestUploadRes> {
|
|
135
137
|
const multipartPlan = planMultipartUpload({
|
|
136
138
|
sizeBytes: fileInfo.size,
|
|
137
139
|
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
),
|
|
148
|
-
},
|
|
149
|
-
}),
|
|
150
|
-
{
|
|
151
|
-
partSize: multipartPlan.partSizeBytes,
|
|
152
|
-
totalParts: multipartPlan.totalParts,
|
|
140
|
+
const res = await runtime.uploads.request({
|
|
141
|
+
bucket: bucketName,
|
|
142
|
+
...mapRawUploadRequest(bucketType, fileInfo, autoSignedUrls),
|
|
143
|
+
...(multipartPlan && {
|
|
144
|
+
multipart: {
|
|
145
|
+
partNumbers: multipartPlan.partNumbers.slice(
|
|
146
|
+
0,
|
|
147
|
+
INITIAL_MULTIPART_PART_URLS,
|
|
148
|
+
),
|
|
153
149
|
},
|
|
154
|
-
)
|
|
150
|
+
}),
|
|
151
|
+
});
|
|
152
|
+
if (authorizeOverwrite) {
|
|
153
|
+
// The upload supersedes an uploaded file at the same key once it
|
|
154
|
+
// completes, so cancel it unless the requester may delete that file.
|
|
155
|
+
try {
|
|
156
|
+
const existing = await findFile(bucketName, res.file.key);
|
|
157
|
+
if (existing) await authorizeOverwrite(mapFile(existing));
|
|
158
|
+
} catch (error) {
|
|
159
|
+
await runtime.uploads
|
|
160
|
+
.cancel({ uploadId: res.file.id })
|
|
161
|
+
.catch(() => undefined);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
155
164
|
}
|
|
156
165
|
return mapUploadResponse(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
166
|
+
res,
|
|
167
|
+
multipartPlan
|
|
168
|
+
? {
|
|
169
|
+
partSize: multipartPlan.partSizeBytes,
|
|
170
|
+
totalParts: multipartPlan.totalParts,
|
|
171
|
+
}
|
|
172
|
+
: undefined,
|
|
161
173
|
);
|
|
162
174
|
},
|
|
163
175
|
multipart: {
|
|
@@ -287,6 +299,16 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
|
287
299
|
},
|
|
288
300
|
});
|
|
289
301
|
|
|
302
|
+
async function findFile(bucketName: string, key: string) {
|
|
303
|
+
try {
|
|
304
|
+
return (await runtime.files.lookup({ bucketName, file: { key } })).file;
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (error instanceof EdgeStoreApiError && error.code === 'file_not_found')
|
|
307
|
+
return undefined;
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
290
312
|
return provider;
|
|
291
313
|
}
|
|
292
314
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
HeadObjectCommand,
|
|
5
5
|
PutObjectCommand,
|
|
6
6
|
S3Client,
|
|
7
|
+
S3ServiceException,
|
|
7
8
|
type PutObjectCommandInput,
|
|
8
9
|
} from '@aws-sdk/client-s3';
|
|
9
10
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
@@ -16,7 +17,7 @@ import { z } from 'zod';
|
|
|
16
17
|
import { defineProvider } from '../../core/provider';
|
|
17
18
|
import { getEnv } from '../../libs/env';
|
|
18
19
|
import { createMultipartSessions } from '../storage/multipartSession';
|
|
19
|
-
import { createObjectKeys } from '../storage/objectKeys';
|
|
20
|
+
import { createObjectKeys, pathFromDefaultKey } from '../storage/objectKeys';
|
|
20
21
|
import {
|
|
21
22
|
assertSignedUrlAccessControl,
|
|
22
23
|
signedUrlLifetime,
|
|
@@ -169,6 +170,42 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
169
170
|
})());
|
|
170
171
|
}
|
|
171
172
|
|
|
173
|
+
async function describeObject(key: string) {
|
|
174
|
+
const { ContentLength, LastModified } = await client.send(
|
|
175
|
+
new HeadObjectCommand({ Bucket: bucket(), Key: key }),
|
|
176
|
+
);
|
|
177
|
+
if (ContentLength === undefined || !LastModified)
|
|
178
|
+
throw new Error('File not found');
|
|
179
|
+
return {
|
|
180
|
+
key,
|
|
181
|
+
url: (await keys()).toUrl(key),
|
|
182
|
+
sizeBytes: ContentLength,
|
|
183
|
+
uploadedAt: LastModified,
|
|
184
|
+
updatedAt: LastModified,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Authorizes overwriting the object at `key`, if one exists. */
|
|
189
|
+
async function authorizeOverwrite(key: string, params: RequestUploadParams) {
|
|
190
|
+
if (!params.authorizeOverwrite) return;
|
|
191
|
+
const existing = await describeObject(key).catch((error: unknown) => {
|
|
192
|
+
// Without s3:ListBucket, S3 reports a missing key as 403, not 404.
|
|
193
|
+
// Credentials without s3:GetObject can't detect overwrites; the
|
|
194
|
+
// documented policy grants it.
|
|
195
|
+
const status =
|
|
196
|
+
error instanceof S3ServiceException
|
|
197
|
+
? error.$metadata.httpStatusCode
|
|
198
|
+
: undefined;
|
|
199
|
+
if (status === 403 || status === 404) return undefined;
|
|
200
|
+
throw error;
|
|
201
|
+
});
|
|
202
|
+
if (!existing) return;
|
|
203
|
+
await params.authorizeOverwrite({
|
|
204
|
+
...existing,
|
|
205
|
+
path: pathFromDefaultKey(params.fileInfo, options.path),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
172
209
|
async function readForUpload(key: string, params: RequestUploadParams) {
|
|
173
210
|
if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
|
|
174
211
|
return signedRead(key, params.autoSignedUrls.expiresIn);
|
|
@@ -193,6 +230,7 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
193
230
|
async request(params): Promise<RequestUploadRes> {
|
|
194
231
|
await assertPresignableClient();
|
|
195
232
|
const prepared = await prepare(params);
|
|
233
|
+
await authorizeOverwrite(prepared.key, params);
|
|
196
234
|
const read = await readForUpload(prepared.key, params);
|
|
197
235
|
const access = {
|
|
198
236
|
key: prepared.key,
|
|
@@ -268,20 +306,9 @@ export function s3(options: S3ProviderOptions = {}) {
|
|
|
268
306
|
},
|
|
269
307
|
files: {
|
|
270
308
|
async get({ bucketName: logicalBucket, file }) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const { ContentLength, LastModified } = await client.send(
|
|
274
|
-
new HeadObjectCommand({ Bucket: bucket(), Key: key }),
|
|
309
|
+
return describeObject(
|
|
310
|
+
(await keys()).fromReference(logicalBucket, file),
|
|
275
311
|
);
|
|
276
|
-
if (ContentLength === undefined || !LastModified)
|
|
277
|
-
throw new Error('File not found');
|
|
278
|
-
return {
|
|
279
|
-
key,
|
|
280
|
-
url: objectKeys.toUrl(key),
|
|
281
|
-
sizeBytes: ContentLength,
|
|
282
|
-
uploadedAt: LastModified,
|
|
283
|
-
updatedAt: LastModified,
|
|
284
|
-
};
|
|
285
312
|
},
|
|
286
313
|
async getSignedUrls({ bucketName: logicalBucket, files, expiresIn }) {
|
|
287
314
|
const objectKeys = await keys();
|
|
@@ -104,3 +104,24 @@ export function createObjectKeys(providerName: string, baseUrl: string) {
|
|
|
104
104
|
|
|
105
105
|
return { toUrl: objectKeyToUrl, fromReference, forUpload };
|
|
106
106
|
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Path values of an existing object at an upload's key, when the key proves
|
|
110
|
+
* them. Only the default layout encodes the path, and a `/` inside a path
|
|
111
|
+
* value or the file name makes its segment boundaries ambiguous.
|
|
112
|
+
*/
|
|
113
|
+
export function pathFromDefaultKey(
|
|
114
|
+
fileInfo: RequestUploadParams['fileInfo'],
|
|
115
|
+
customPath: ObjectPathFn | undefined,
|
|
116
|
+
) {
|
|
117
|
+
const segments = [
|
|
118
|
+
...fileInfo.path.map(({ value }) => value),
|
|
119
|
+
fileInfo.fileName ?? '',
|
|
120
|
+
];
|
|
121
|
+
if (customPath || segments.some((segment) => segment.includes('/'))) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return Object.fromEntries(
|
|
125
|
+
fileInfo.path.map(({ key, value }) => [key, value]),
|
|
126
|
+
);
|
|
127
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const EDGE_STORE_PACKAGE_NAME = '@edgestore/server';
|
|
2
|
-
export const EDGE_STORE_PACKAGE_VERSION = '1.0.
|
|
2
|
+
export const EDGE_STORE_PACKAGE_VERSION = '1.0.1';
|