@edgestore/server 1.0.0-next.4 → 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/README.md +2 -8
- package/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +3 -9
- package/agent-docs/backend-client.md +16 -29
- package/agent-docs/configuration.md +41 -10
- package/agent-docs/express.md +3 -9
- package/agent-docs/fastify.md +3 -9
- package/agent-docs/hono.md +3 -9
- package/agent-docs/next.md +5 -17
- package/agent-docs/provider-azure-blob.md +150 -59
- package/agent-docs/provider-custom.md +42 -13
- package/agent-docs/provider-edgestore.md +9 -9
- package/agent-docs/provider-s3.md +224 -119
- package/agent-docs/remix.md +3 -9
- package/agent-docs/tanstack-start.md +3 -9
- 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 +31 -143
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +115 -197
- 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 +25 -26
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +2 -3
- package/dist/core/provider.d.ts +11 -2
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/provider.js +20 -3
- 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 +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- 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 +81 -49
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +194 -105
- package/dist/providers/edgestore/index.d.ts +12 -32
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +34 -31
- package/dist/providers/s3/backendUpload.d.ts +10 -0
- package/dist/providers/s3/backendUpload.d.ts.map +1 -0
- package/dist/providers/s3/backendUpload.js +59 -0
- package/dist/providers/s3/index.d.ts +52 -97
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +221 -91
- package/dist/providers/s3/multipart.d.ts +23 -0
- package/dist/providers/s3/multipart.d.ts.map +1 -0
- package/dist/providers/s3/multipart.js +96 -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 +85 -0
- package/dist/providers/s3/options.d.ts.map +1 -0
- 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/storage/objectKeys.js +67 -0
- 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 +5 -12
- 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 +166 -285
- package/src/adapters/start/index.ts +3 -4
- package/src/core/client/bucketClient.ts +10 -41
- package/src/core/client/index.ts +29 -49
- package/src/core/provider.ts +25 -10
- package/src/core/router.ts +97 -0
- package/src/index.ts +4 -11
- package/src/providers/azure-blob/blocks.ts +201 -0
- package/src/providers/azure-blob/index.ts +290 -170
- package/src/providers/edgestore/index.ts +38 -35
- package/src/providers/s3/backendUpload.ts +82 -0
- package/src/providers/s3/index.ts +279 -227
- package/src/providers/s3/multipart.ts +138 -0
- package/src/providers/s3/objectHeaders.ts +23 -0
- package/src/providers/s3/options.ts +102 -0
- 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 -22
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -14
- package/src/core/index.ts +0 -43
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbortMultipartUploadCommand,
|
|
3
|
+
CompleteMultipartUploadCommand,
|
|
4
|
+
CreateMultipartUploadCommand,
|
|
5
|
+
UploadPartCommand,
|
|
6
|
+
type PutObjectCommandInput,
|
|
7
|
+
type S3Client,
|
|
8
|
+
} from '@aws-sdk/client-s3';
|
|
9
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
10
|
+
import { type MultipartUploadPlan } from '@edgestore/sdk';
|
|
11
|
+
import {
|
|
12
|
+
EdgeStoreError,
|
|
13
|
+
type ProviderMultipartUploads,
|
|
14
|
+
} from '@edgestore/shared';
|
|
15
|
+
import {
|
|
16
|
+
assertValidParts,
|
|
17
|
+
initialPartNumbers,
|
|
18
|
+
partLength,
|
|
19
|
+
sessionFromPlan,
|
|
20
|
+
type createMultipartSessions,
|
|
21
|
+
type MultipartSession,
|
|
22
|
+
} from '../storage/multipartSession';
|
|
23
|
+
|
|
24
|
+
export function createMultipartUploads({
|
|
25
|
+
client,
|
|
26
|
+
bucket,
|
|
27
|
+
sessions,
|
|
28
|
+
partUrlExpiresIn,
|
|
29
|
+
}: {
|
|
30
|
+
client: S3Client;
|
|
31
|
+
bucket: () => string;
|
|
32
|
+
sessions: ReturnType<typeof createMultipartSessions>;
|
|
33
|
+
partUrlExpiresIn: number;
|
|
34
|
+
}) {
|
|
35
|
+
async function abort(session: MultipartSession) {
|
|
36
|
+
await client.send(
|
|
37
|
+
new AbortMultipartUploadCommand({
|
|
38
|
+
Bucket: bucket(),
|
|
39
|
+
Key: session.key,
|
|
40
|
+
UploadId: session.id,
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function signParts(session: MultipartSession, parts: number[]) {
|
|
46
|
+
assertValidParts(session, parts);
|
|
47
|
+
return Promise.all(
|
|
48
|
+
parts.map(async (partNumber) => ({
|
|
49
|
+
partNumber,
|
|
50
|
+
uploadUrl: await getSignedUrl(
|
|
51
|
+
client,
|
|
52
|
+
new UploadPartCommand({
|
|
53
|
+
Bucket: bucket(),
|
|
54
|
+
Key: session.key,
|
|
55
|
+
UploadId: session.id,
|
|
56
|
+
PartNumber: partNumber,
|
|
57
|
+
ContentLength: partLength(session, partNumber),
|
|
58
|
+
}),
|
|
59
|
+
{ expiresIn: partUrlExpiresIn },
|
|
60
|
+
),
|
|
61
|
+
})),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const operations: ProviderMultipartUploads = {
|
|
66
|
+
async requestParts({ uploadId, key, parts }) {
|
|
67
|
+
return {
|
|
68
|
+
parts: await signParts(await sessions.read(uploadId, key), parts),
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
async complete({ uploadId, key, parts }) {
|
|
72
|
+
const session = await sessions.read(uploadId, key);
|
|
73
|
+
assertValidParts(
|
|
74
|
+
session,
|
|
75
|
+
parts.map((part) => part.partNumber),
|
|
76
|
+
{ complete: true },
|
|
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
|
+
}
|
|
85
|
+
await client.send(
|
|
86
|
+
new CompleteMultipartUploadCommand({
|
|
87
|
+
Bucket: bucket(),
|
|
88
|
+
Key: key,
|
|
89
|
+
UploadId: session.id,
|
|
90
|
+
MultipartUpload: {
|
|
91
|
+
Parts: [...parts]
|
|
92
|
+
.sort((a, b) => a.partNumber - b.partNumber)
|
|
93
|
+
.map((part) => ({
|
|
94
|
+
PartNumber: part.partNumber,
|
|
95
|
+
ETag: part.eTag,
|
|
96
|
+
})),
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
async abort({ uploadId, key }) {
|
|
102
|
+
await abort(await sessions.read(uploadId, key));
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
operations,
|
|
108
|
+
async request(input: PutObjectCommandInput, plan: MultipartUploadPlan) {
|
|
109
|
+
// Validate signing configuration before allocating storage resources.
|
|
110
|
+
sessions.assertConfigured();
|
|
111
|
+
const { ContentLength, ...objectInput } = input;
|
|
112
|
+
const result = await client.send(
|
|
113
|
+
new CreateMultipartUploadCommand(objectInput),
|
|
114
|
+
);
|
|
115
|
+
if (!result.UploadId || !input.Key || ContentLength === undefined) {
|
|
116
|
+
throw new Error('S3 did not return a multipart upload ID.');
|
|
117
|
+
}
|
|
118
|
+
const session = sessionFromPlan({
|
|
119
|
+
key: input.Key,
|
|
120
|
+
id: result.UploadId,
|
|
121
|
+
size: ContentLength,
|
|
122
|
+
plan,
|
|
123
|
+
});
|
|
124
|
+
try {
|
|
125
|
+
return {
|
|
126
|
+
key: session.key,
|
|
127
|
+
uploadId: await sessions.sign(session),
|
|
128
|
+
partSize: session.partSize,
|
|
129
|
+
totalParts: session.totalParts,
|
|
130
|
+
parts: await signParts(session, initialPartNumbers(plan)),
|
|
131
|
+
};
|
|
132
|
+
} catch (error) {
|
|
133
|
+
await abort(session).catch(() => undefined);
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PutObjectCommandInput } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
/** Keep signed object settings and the browser's actual PUT headers aligned. */
|
|
4
|
+
export function objectUploadHeaders(input: PutObjectCommandInput) {
|
|
5
|
+
const headers: Record<string, string> = {};
|
|
6
|
+
const fields = {
|
|
7
|
+
ContentType: 'Content-Type',
|
|
8
|
+
CacheControl: 'Cache-Control',
|
|
9
|
+
ContentDisposition: 'Content-Disposition',
|
|
10
|
+
StorageClass: 'x-amz-storage-class',
|
|
11
|
+
ServerSideEncryption: 'x-amz-server-side-encryption',
|
|
12
|
+
SSEKMSKeyId: 'x-amz-server-side-encryption-aws-kms-key-id',
|
|
13
|
+
Tagging: 'x-amz-tagging',
|
|
14
|
+
} as const;
|
|
15
|
+
for (const field of Object.keys(fields) as (keyof typeof fields)[]) {
|
|
16
|
+
const value = input[field];
|
|
17
|
+
if (value !== undefined) headers[fields[field]] = value;
|
|
18
|
+
}
|
|
19
|
+
for (const [key, value] of Object.entries(input.Metadata ?? {})) {
|
|
20
|
+
headers[`x-amz-meta-${key}`] = value;
|
|
21
|
+
}
|
|
22
|
+
return headers;
|
|
23
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PutObjectCommandInput,
|
|
3
|
+
S3Client,
|
|
4
|
+
S3ClientConfig,
|
|
5
|
+
} from '@aws-sdk/client-s3';
|
|
6
|
+
import type { MaybePromise } from '@edgestore/shared';
|
|
7
|
+
import type { ObjectPathFn, ObjectPathFnArgs } from '../storage/objectKeys';
|
|
8
|
+
|
|
9
|
+
export type S3ObjectOptions = Pick<
|
|
10
|
+
PutObjectCommandInput,
|
|
11
|
+
| 'CacheControl'
|
|
12
|
+
| 'ContentDisposition'
|
|
13
|
+
| 'Metadata'
|
|
14
|
+
| 'Tagging'
|
|
15
|
+
| 'StorageClass'
|
|
16
|
+
| 'ServerSideEncryption'
|
|
17
|
+
| 'SSEKMSKeyId'
|
|
18
|
+
>;
|
|
19
|
+
|
|
20
|
+
/** Arguments passed to the `path` callback. */
|
|
21
|
+
export type S3PathFnArgs = ObjectPathFnArgs;
|
|
22
|
+
|
|
23
|
+
/** Returns an object path relative to the logical bucket prefix. */
|
|
24
|
+
export type S3PathFn = ObjectPathFn;
|
|
25
|
+
|
|
26
|
+
export type S3ProviderOptions = {
|
|
27
|
+
/**
|
|
28
|
+
* AWS SDK credentials (or credentials provider) to use for S3 requests.
|
|
29
|
+
*
|
|
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).
|
|
34
|
+
*/
|
|
35
|
+
credentials?: S3ClientConfig['credentials'];
|
|
36
|
+
/**
|
|
37
|
+
* AWS region to use.
|
|
38
|
+
* Can also be set via the `ES_AWS_REGION` environment variable.
|
|
39
|
+
*/
|
|
40
|
+
region?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Name of the S3 bucket to use.
|
|
43
|
+
* Can also be set via the `ES_AWS_BUCKET_NAME` environment variable.
|
|
44
|
+
*/
|
|
45
|
+
bucketName?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Custom endpoint for S3-compatible storage providers (e.g., MinIO).
|
|
48
|
+
* Can also be set via the `ES_AWS_ENDPOINT` environment variable.
|
|
49
|
+
*/
|
|
50
|
+
endpoint?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Force path style for S3-compatible storage providers.
|
|
53
|
+
* Can also be set via the `ES_AWS_FORCE_PATH_STYLE` environment variable.
|
|
54
|
+
* Defaults to false for AWS S3, but should be true for most S3-compatible providers.
|
|
55
|
+
*/
|
|
56
|
+
forcePathStyle?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Base URL to use for accessing files.
|
|
59
|
+
* Only needed if you are using a custom domain or cloudfront.
|
|
60
|
+
*
|
|
61
|
+
* It can also be set via the `EDGE_STORE_BASE_URL` environment variable.
|
|
62
|
+
*/
|
|
63
|
+
baseUrl?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Secret used to sign multipart upload sessions. The adapter context cookie
|
|
66
|
+
* still requires EDGE_STORE_JWT_SECRET (or EDGE_STORE_SECRET_KEY).
|
|
67
|
+
* Can be generated with `openssl rand -base64 32`.
|
|
68
|
+
*
|
|
69
|
+
* It can also be set via the `EDGE_STORE_JWT_SECRET` environment variable.
|
|
70
|
+
*/
|
|
71
|
+
jwtSecret?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Customizes the object path beneath the logical EdgeStore bucket prefix.
|
|
74
|
+
*
|
|
75
|
+
* The logical bucket prefix is always preserved so router authorization for
|
|
76
|
+
* one bucket cannot access objects from another.
|
|
77
|
+
*/
|
|
78
|
+
path?: S3PathFn;
|
|
79
|
+
/** An existing client, for custom retry, transport, or endpoint configuration.
|
|
80
|
+
* Browser uploads require requestChecksumCalculation: "WHEN_REQUIRED".
|
|
81
|
+
* Set baseUrl when this client uses a custom endpoint. */
|
|
82
|
+
client?: S3Client;
|
|
83
|
+
/** Presigned upload URL lifetime in seconds. Default: 3600. */
|
|
84
|
+
uploadUrlExpiresIn?: number;
|
|
85
|
+
/** Presigned download URL lifetime in seconds. Default: 3600. */
|
|
86
|
+
signedUrlExpiresIn?: number;
|
|
87
|
+
/** Automatic multipart upload configuration. */
|
|
88
|
+
multipart?: {
|
|
89
|
+
/** Size above which uploads use multipart transfers. Default: 100 MiB. */
|
|
90
|
+
thresholdBytes?: number;
|
|
91
|
+
/** Preferred part size. Default: 16 MiB. */
|
|
92
|
+
partSizeBytes?: number;
|
|
93
|
+
/**
|
|
94
|
+
* How long a browser multipart session can request part URLs, complete, or
|
|
95
|
+
* abort, in seconds. Default: 86400 (24 hours).
|
|
96
|
+
*/
|
|
97
|
+
sessionExpiresIn?: number;
|
|
98
|
+
};
|
|
99
|
+
/** Object settings shared by browser and backend uploads. */
|
|
100
|
+
objectOptions?:
|
|
101
|
+
S3ObjectOptions | ((args: S3PathFnArgs) => MaybePromise<S3ObjectOptions>);
|
|
102
|
+
};
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { MaybePromise, RequestUploadParams } from '@edgestore/shared';
|
|
2
|
+
|
|
3
|
+
export type ObjectPathFnArgs = {
|
|
4
|
+
/** Logical EdgeStore router bucket name. */
|
|
5
|
+
edgestoreBucketName: string;
|
|
6
|
+
/** File info after EdgeStore path and metadata generation. */
|
|
7
|
+
fileInfo: RequestUploadParams['fileInfo'];
|
|
8
|
+
/** Default object path relative to the logical bucket prefix. */
|
|
9
|
+
defaultPath: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Returns an object path relative to the logical bucket prefix. */
|
|
13
|
+
export type ObjectPathFn = (args: ObjectPathFnArgs) => MaybePromise<string>;
|
|
14
|
+
|
|
15
|
+
export function createObjectKeys(providerName: string, baseUrl: string) {
|
|
16
|
+
const normalizedBaseUrl = baseUrl.replace(/\/+$/, '');
|
|
17
|
+
|
|
18
|
+
function objectKeyToUrl(objectKey: string) {
|
|
19
|
+
const encodedKey = objectKey
|
|
20
|
+
.split('/')
|
|
21
|
+
.map((segment) => encodeURIComponent(segment))
|
|
22
|
+
.join('/');
|
|
23
|
+
return `${normalizedBaseUrl}/${encodedKey}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function urlToObjectKey(url: string) {
|
|
27
|
+
try {
|
|
28
|
+
const fileUrl = new URL(url);
|
|
29
|
+
const providerUrl = new URL(`${normalizedBaseUrl}/`);
|
|
30
|
+
if (
|
|
31
|
+
fileUrl.origin !== providerUrl.origin ||
|
|
32
|
+
!fileUrl.pathname.startsWith(providerUrl.pathname)
|
|
33
|
+
) {
|
|
34
|
+
throw new Error();
|
|
35
|
+
}
|
|
36
|
+
return decodeURIComponent(
|
|
37
|
+
fileUrl.pathname.slice(providerUrl.pathname.length),
|
|
38
|
+
);
|
|
39
|
+
} catch {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`File URL does not belong to this ${providerName} provider.`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function normalizeRelativePath(value: string) {
|
|
47
|
+
const path = value.replace(/^\/+|\/+$/g, '');
|
|
48
|
+
if (
|
|
49
|
+
path.length === 0 ||
|
|
50
|
+
path.split('/').some((segment) => segment === '.' || segment === '..')
|
|
51
|
+
) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`${providerName} paths must stay within the EdgeStore bucket prefix.`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return path;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Keys from both URLs and key references must stay inside the logical bucket. */
|
|
60
|
+
function fromReference(
|
|
61
|
+
bucketName: string,
|
|
62
|
+
file: { url: string } | { key: string },
|
|
63
|
+
) {
|
|
64
|
+
const key = 'url' in file ? urlToObjectKey(file.url) : file.key;
|
|
65
|
+
if (
|
|
66
|
+
normalizeRelativePath(key) !== key ||
|
|
67
|
+
!key.startsWith(`${bucketName}/`)
|
|
68
|
+
) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`File does not belong to EdgeStore bucket "${bucketName}".`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return key;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Resolves the object key for an upload. The logical bucket is always the
|
|
78
|
+
* first key segment, so router authorization for one bucket cannot reach
|
|
79
|
+
* objects from another.
|
|
80
|
+
*/
|
|
81
|
+
async function forUpload(
|
|
82
|
+
edgestoreBucketName: string,
|
|
83
|
+
fileInfo: RequestUploadParams['fileInfo'],
|
|
84
|
+
path?: ObjectPathFn,
|
|
85
|
+
) {
|
|
86
|
+
const extension = fileInfo.extension
|
|
87
|
+
? `.${fileInfo.extension.replace(/^\./, '')}`
|
|
88
|
+
: '';
|
|
89
|
+
const defaultPath = [
|
|
90
|
+
...(fileInfo.isPublic ? ['_public'] : []),
|
|
91
|
+
...fileInfo.path.map((part) => part.value),
|
|
92
|
+
fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`,
|
|
93
|
+
].join('/');
|
|
94
|
+
const relativePath = normalizeRelativePath(
|
|
95
|
+
path
|
|
96
|
+
? await path({ edgestoreBucketName, fileInfo, defaultPath })
|
|
97
|
+
: defaultPath,
|
|
98
|
+
);
|
|
99
|
+
return {
|
|
100
|
+
key: `${edgestoreBucketName}/${relativePath}`,
|
|
101
|
+
pathArgs: { edgestoreBucketName, fileInfo, defaultPath },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { toUrl: objectKeyToUrl, fromReference, forUpload };
|
|
106
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { DEFAULT_MULTIPART_CONCURRENCY } from '@edgestore/sdk';
|
|
2
|
+
import type {
|
|
3
|
+
AnyContext,
|
|
4
|
+
BackendUploadParams,
|
|
5
|
+
EdgeStoreRouter,
|
|
6
|
+
} from '@edgestore/shared';
|
|
7
|
+
|
|
8
|
+
/** Validates a signed URL lifetime against the storage service's maximum. */
|
|
9
|
+
export function signedUrlLifetime(
|
|
10
|
+
providerName: string,
|
|
11
|
+
value: number,
|
|
12
|
+
maxSeconds = 604800,
|
|
13
|
+
) {
|
|
14
|
+
if (!Number.isInteger(value) || value < 1 || value > maxSeconds) {
|
|
15
|
+
throw new RangeError(
|
|
16
|
+
`${providerName} signed URL expiration must be an integer between 1 and ${maxSeconds} seconds.`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Direct-storage providers authorize private reads with backend-issued signed
|
|
24
|
+
* URLs. Cookie-based rules would silently not be enforced, so reject them.
|
|
25
|
+
*/
|
|
26
|
+
export function assertSignedUrlAccessControl<TCtx extends AnyContext>(
|
|
27
|
+
providerName: string,
|
|
28
|
+
router: EdgeStoreRouter<TCtx>,
|
|
29
|
+
) {
|
|
30
|
+
for (const bucket of Object.values(router.buckets)) {
|
|
31
|
+
const { accessControl } = bucket._def;
|
|
32
|
+
if (accessControl !== undefined && accessControl !== 'private') {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`${providerName} supports accessControl("private") with backend signed URLs, not cookie-based access-control rules.`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Reports cumulative backend upload progress. */
|
|
41
|
+
export function createProgress(
|
|
42
|
+
totalBytes: number,
|
|
43
|
+
onProgress: BackendUploadParams['onProgress'],
|
|
44
|
+
) {
|
|
45
|
+
let transferredBytes = 0;
|
|
46
|
+
const report = (bytes: number) => {
|
|
47
|
+
transferredBytes += bytes;
|
|
48
|
+
onProgress?.({
|
|
49
|
+
transferredBytes,
|
|
50
|
+
totalBytes,
|
|
51
|
+
percentage: totalBytes ? (transferredBytes / totalBytes) * 100 : 100,
|
|
52
|
+
phase: 'uploading',
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
report(0);
|
|
56
|
+
return report;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Runs tasks a few at a time to bound memory use. The first failure, or
|
|
61
|
+
* the caller's signal, stops the remaining parts before the error is thrown.
|
|
62
|
+
*/
|
|
63
|
+
export async function runConcurrently<TItem, TResult>(
|
|
64
|
+
items: TItem[],
|
|
65
|
+
signal: AbortSignal | undefined,
|
|
66
|
+
task: (item: TItem, signal: AbortSignal) => Promise<TResult>,
|
|
67
|
+
) {
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const partSignal = signal
|
|
70
|
+
? AbortSignal.any([signal, controller.signal])
|
|
71
|
+
: controller.signal;
|
|
72
|
+
const results = new Array<TResult>(items.length);
|
|
73
|
+
let next = 0;
|
|
74
|
+
const workers = Array.from(
|
|
75
|
+
{ length: Math.min(DEFAULT_MULTIPART_CONCURRENCY, items.length) },
|
|
76
|
+
async () => {
|
|
77
|
+
while (next < items.length) {
|
|
78
|
+
partSignal.throwIfAborted();
|
|
79
|
+
const index = next++;
|
|
80
|
+
results[index] = await task(items[index]!, partSignal);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
try {
|
|
85
|
+
await Promise.all(workers);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
controller.abort(error);
|
|
88
|
+
await Promise.allSettled(workers);
|
|
89
|
+
throw signal?.aborted ? signal.reason : error;
|
|
90
|
+
}
|
|
91
|
+
signal?.throwIfAborted();
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Reads one part of the source into memory. */
|
|
96
|
+
export async function readPart(
|
|
97
|
+
source: Blob,
|
|
98
|
+
partNumber: number,
|
|
99
|
+
partSizeBytes: number,
|
|
100
|
+
) {
|
|
101
|
+
const start = (partNumber - 1) * partSizeBytes;
|
|
102
|
+
return new Uint8Array(
|
|
103
|
+
await source.slice(start, start + partSizeBytes).arrayBuffer(),
|
|
104
|
+
);
|
|
105
|
+
}
|
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.0-
|
|
2
|
+
export const EDGE_STORE_PACKAGE_VERSION = '1.0.0-rc.6';
|
package/core/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../dist/core/index.js';
|
package/core/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../dist/core/index.js';
|
package/dist/core/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { AnyEdgeStoreProvider, AnyRouter } from '@edgestore/shared';
|
|
2
|
-
import { type EdgeStoreClient } from './client/index.js';
|
|
3
|
-
export * from './client/index.js';
|
|
4
|
-
export { defineProvider, getProviderBaseUrl, referenceFromUrl, validateProviderCursor, validateProviderReference, } from './provider.js';
|
|
5
|
-
export type ConfiguredEdgeStore<TRouter extends AnyRouter, TProvider extends AnyEdgeStoreProvider> = {
|
|
6
|
-
router: TRouter;
|
|
7
|
-
provider: TProvider;
|
|
8
|
-
baseUrl?: string;
|
|
9
|
-
client: EdgeStoreClient<TRouter, TProvider>;
|
|
10
|
-
};
|
|
11
|
-
export declare function createEdgeStore<TRouter extends AnyRouter, TProvider extends AnyEdgeStoreProvider>(config: {
|
|
12
|
-
router: TRouter;
|
|
13
|
-
provider: TProvider;
|
|
14
|
-
/**
|
|
15
|
-
* Application EdgeStore handler URL used to proxy protected files during
|
|
16
|
-
* local development.
|
|
17
|
-
*
|
|
18
|
-
* @example http://localhost:3000/api/edgestore
|
|
19
|
-
*/
|
|
20
|
-
baseUrl?: string;
|
|
21
|
-
}): ConfiguredEdgeStore<TRouter, TProvider>;
|
|
22
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/core/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAuB,KAAK,eAAe,EAAE,MAAM,UAAU,CAAC;AAErE,cAAc,UAAU,CAAC;AACzB,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,mBAAmB,CAC7B,OAAO,SAAS,SAAS,EACzB,SAAS,SAAS,oBAAoB,IACpC;IACF,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,SAAS,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,eAAe,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;CAC7C,CAAC;AAEF,wBAAgB,eAAe,CAC7B,OAAO,SAAS,SAAS,EACzB,SAAS,SAAS,oBAAoB,EACtC,MAAM,EAAE;IACR,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,SAAS,CAAC;IACpB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAO1C"}
|
package/dist/core/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createBackendClient } from './client/index.js';
|
|
2
|
-
export { defineProvider, getProviderBaseUrl, referenceFromUrl, validateProviderCursor, validateProviderReference } from './provider.js';
|
|
3
|
-
export { EdgeStoreFileMutationError } from '@edgestore/sdk';
|
|
4
|
-
|
|
5
|
-
function createEdgeStore(config) {
|
|
6
|
-
return {
|
|
7
|
-
router: config.router,
|
|
8
|
-
provider: config.provider,
|
|
9
|
-
baseUrl: config.baseUrl,
|
|
10
|
-
client: createBackendClient(config.router, config.provider, config.baseUrl)
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export { createBackendClient, createEdgeStore };
|