@edgestore/server 1.0.0-next.5 → 1.0.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +1 -1
- package/agent-docs/backend-client.md +5 -8
- package/agent-docs/express.md +1 -1
- package/agent-docs/fastify.md +1 -1
- package/agent-docs/hono.md +1 -1
- package/agent-docs/next.md +1 -1
- package/agent-docs/provider-azure-blob.md +150 -57
- package/agent-docs/provider-custom.md +17 -3
- package/agent-docs/provider-s3.md +4 -3
- package/agent-docs/remix.md +1 -1
- package/agent-docs/tanstack-start.md +1 -1
- package/dist/adapters/shared.d.ts +12 -102
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +63 -108
- package/dist/core/client/index.d.ts +0 -4
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/provider.d.ts +0 -1
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/provider.js +1 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/providers/azure-blob/blocks.d.ts +48 -0
- package/dist/providers/azure-blob/blocks.d.ts.map +1 -0
- package/dist/providers/azure-blob/blocks.js +126 -0
- package/dist/providers/azure-blob/index.d.ts +76 -51
- package/dist/providers/azure-blob/index.d.ts.map +1 -1
- package/dist/providers/azure-blob/index.js +189 -107
- package/dist/providers/edgestore/index.d.ts +6 -22
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +19 -18
- package/dist/providers/s3/backendUpload.d.ts +2 -5
- package/dist/providers/s3/backendUpload.d.ts.map +1 -1
- package/dist/providers/s3/backendUpload.js +19 -57
- package/dist/providers/s3/index.d.ts +3 -12
- package/dist/providers/s3/index.d.ts.map +1 -1
- package/dist/providers/s3/index.js +23 -44
- package/dist/providers/s3/multipart.d.ts +3 -5
- package/dist/providers/s3/multipart.d.ts.map +1 -1
- package/dist/providers/s3/multipart.js +26 -62
- package/dist/providers/s3/options.d.ts +10 -32
- package/dist/providers/s3/options.d.ts.map +1 -1
- package/dist/providers/storage/multipartSession.d.ts +51 -0
- package/dist/providers/storage/multipartSession.d.ts.map +1 -0
- package/dist/providers/storage/multipartSession.js +83 -0
- package/dist/providers/storage/objectKeys.d.ts +41 -0
- package/dist/providers/storage/objectKeys.d.ts.map +1 -0
- package/dist/providers/{s3/keys.js → storage/objectKeys.js} +31 -4
- package/dist/providers/storage/transfer.d.ts +18 -0
- package/dist/providers/storage/transfer.d.ts.map +1 -0
- package/dist/providers/storage/transfer.js +69 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +4 -11
- package/src/adapters/shared.ts +81 -127
- package/src/core/client/index.ts +0 -4
- package/src/core/provider.ts +0 -8
- package/src/index.ts +3 -8
- package/src/providers/azure-blob/blocks.ts +201 -0
- package/src/providers/azure-blob/index.ts +289 -171
- package/src/providers/edgestore/index.ts +19 -22
- package/src/providers/s3/backendUpload.ts +20 -55
- package/src/providers/s3/index.ts +31 -58
- package/src/providers/s3/multipart.ts +36 -94
- package/src/providers/s3/options.ts +10 -33
- package/src/providers/storage/multipartSession.ts +145 -0
- package/src/providers/storage/objectKeys.ts +106 -0
- package/src/providers/storage/transfer.ts +105 -0
- package/src/version.ts +1 -1
- package/core/index.d.ts +0 -1
- package/core/index.js +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -3
- package/dist/providers/s3/keys.d.ts +0 -10
- package/dist/providers/s3/keys.d.ts.map +0 -1
- package/src/core/index.ts +0 -9
- package/src/providers/s3/keys.ts +0 -59
|
@@ -6,247 +6,347 @@ import {
|
|
|
6
6
|
SASProtocol,
|
|
7
7
|
StorageSharedKeyCredential,
|
|
8
8
|
} from '@azure/storage-blob';
|
|
9
|
-
import {
|
|
9
|
+
import { planMultipartUpload } from '@edgestore/sdk';
|
|
10
|
+
import type {
|
|
11
|
+
MaybePromise,
|
|
12
|
+
RequestUploadParams,
|
|
13
|
+
RequestUploadRes,
|
|
14
|
+
} from '@edgestore/shared';
|
|
10
15
|
import { z } from 'zod';
|
|
11
16
|
import { defineProvider } from '../../core/provider';
|
|
12
17
|
import { getEnv } from '../../libs/env';
|
|
18
|
+
import { createMultipartSessions } from '../storage/multipartSession';
|
|
19
|
+
import {
|
|
20
|
+
createObjectKeys,
|
|
21
|
+
type ObjectPathFn,
|
|
22
|
+
type ObjectPathFnArgs,
|
|
23
|
+
} from '../storage/objectKeys';
|
|
24
|
+
import {
|
|
25
|
+
assertSignedUrlAccessControl,
|
|
26
|
+
runConcurrently,
|
|
27
|
+
signedUrlLifetime,
|
|
28
|
+
} from '../storage/transfer';
|
|
29
|
+
import {
|
|
30
|
+
createBlockUploads,
|
|
31
|
+
uploadBlob,
|
|
32
|
+
type BlobCommitOptions,
|
|
33
|
+
} from './blocks';
|
|
34
|
+
|
|
35
|
+
/** Blob settings shared by browser and backend uploads. */
|
|
36
|
+
export type AzureBlobObjectOptions = {
|
|
37
|
+
cacheControl?: string;
|
|
38
|
+
contentDisposition?: string;
|
|
39
|
+
metadata?: Record<string, string>;
|
|
40
|
+
};
|
|
13
41
|
|
|
14
|
-
/**
|
|
15
|
-
* Options for the Azure provider. Compatible with Azure Blob Storage and Azurite.
|
|
16
|
-
* Use Azure Storage Explorer for local development with Azurite.
|
|
17
|
-
* @see https://azure.microsoft.com/de-de/products/storage/storage-explorer
|
|
18
|
-
* @category Providers
|
|
19
|
-
* @example
|
|
20
|
-
* azureBlob({
|
|
21
|
-
* storageAccountName: 'devstoreaccount1',
|
|
22
|
-
* storageAccountKey: 'some-account-key',
|
|
23
|
-
* containerName: 'some-container-name',
|
|
24
|
-
* customBaseUrl: 'http://localhost:10000/devstoreaccount1',
|
|
25
|
-
* })
|
|
26
|
-
*/
|
|
27
42
|
export type AzureBlobProviderOptions = {
|
|
28
43
|
/**
|
|
29
|
-
*
|
|
30
|
-
* Can also be set via the `ES_AZURE_ACCOUNT_NAME` environment variable.
|
|
44
|
+
* Storage account name. Can also be set via `ES_AZURE_ACCOUNT_NAME`.
|
|
31
45
|
*/
|
|
32
46
|
storageAccountName?: string;
|
|
33
47
|
/**
|
|
34
|
-
* Account key used
|
|
35
|
-
*
|
|
36
|
-
* Can also be set via the `ES_AZURE_ACCOUNT_KEY` environment variable.
|
|
48
|
+
* Account key used for server-side operations and for signing short-lived,
|
|
49
|
+
* blob-scoped URLs. Can also be set via `ES_AZURE_ACCOUNT_KEY`.
|
|
37
50
|
*/
|
|
38
51
|
storageAccountKey?: string;
|
|
52
|
+
/** Container name. Can also be set via `ES_AZURE_CONTAINER_NAME`. */
|
|
53
|
+
containerName?: string;
|
|
39
54
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
55
|
+
* Blob service endpoint, for example Azurite at
|
|
56
|
+
* `http://127.0.0.1:10000/devstoreaccount1`. Defaults to
|
|
57
|
+
* `https://<account>.blob.core.windows.net`. Can also be set via
|
|
58
|
+
* `ES_AZURE_ENDPOINT`.
|
|
42
59
|
*/
|
|
43
|
-
|
|
60
|
+
endpoint?: string;
|
|
44
61
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
62
|
+
* Base URL for file URLs, such as a CDN in front of the container. Defaults
|
|
63
|
+
* to `<endpoint>/<container>`. Can also be set via `EDGE_STORE_BASE_URL`.
|
|
64
|
+
* Signed URLs always use the endpoint.
|
|
48
65
|
*/
|
|
49
|
-
|
|
66
|
+
baseUrl?: string;
|
|
50
67
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
68
|
+
* Secret used to sign multipart upload sessions. Defaults to
|
|
69
|
+
* `EDGE_STORE_JWT_SECRET` or `EDGE_STORE_SECRET_KEY`.
|
|
53
70
|
*/
|
|
54
|
-
|
|
71
|
+
jwtSecret?: string;
|
|
55
72
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
73
|
+
* Customizes the blob path beneath the logical EdgeStore bucket prefix.
|
|
74
|
+
* The logical bucket prefix is always preserved.
|
|
58
75
|
*/
|
|
76
|
+
path?: ObjectPathFn;
|
|
77
|
+
/** Blob settings shared by browser and backend uploads. */
|
|
78
|
+
objectOptions?:
|
|
79
|
+
| AzureBlobObjectOptions
|
|
80
|
+
| ((args: ObjectPathFnArgs) => MaybePromise<AzureBlobObjectOptions>);
|
|
81
|
+
/** Upload URL lifetime in seconds. Default: 3600. */
|
|
82
|
+
uploadUrlExpiresIn?: number;
|
|
83
|
+
/** Signed read URL lifetime in seconds. Default: 3600. */
|
|
59
84
|
signedUrlExpiresIn?: number;
|
|
85
|
+
/** Automatic multipart upload configuration. */
|
|
86
|
+
multipart?: {
|
|
87
|
+
/** Size above which uploads use blocks. Default: 100 MiB. */
|
|
88
|
+
thresholdBytes?: number;
|
|
89
|
+
/** Preferred block size. Default: 16 MiB; maximum 4000 MiB. */
|
|
90
|
+
partSizeBytes?: number;
|
|
91
|
+
/**
|
|
92
|
+
* How long a browser multipart session can request URLs and complete, in
|
|
93
|
+
* seconds. Default: 86400 (24 hours).
|
|
94
|
+
*/
|
|
95
|
+
sessionExpiresIn?: number;
|
|
96
|
+
};
|
|
60
97
|
};
|
|
61
98
|
|
|
62
|
-
|
|
99
|
+
const MiB = 1024 ** 2;
|
|
100
|
+
/** Largest single Put Blob request. */
|
|
101
|
+
const MAX_SINGLE_UPLOAD_BYTES = 5000 * MiB;
|
|
102
|
+
/** Largest Put Block request. */
|
|
103
|
+
const MAX_BLOCK_BYTES = 4000 * MiB;
|
|
104
|
+
|
|
105
|
+
const expiration = (value: number) => signedUrlLifetime('Azure Blob', value);
|
|
106
|
+
|
|
107
|
+
export function azureBlob(options: AzureBlobProviderOptions = {}) {
|
|
63
108
|
const {
|
|
64
109
|
storageAccountName = getEnv('ES_AZURE_ACCOUNT_NAME'),
|
|
65
110
|
storageAccountKey = getEnv('ES_AZURE_ACCOUNT_KEY'),
|
|
66
111
|
containerName = getEnv('ES_AZURE_CONTAINER_NAME'),
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
112
|
+
} = options;
|
|
113
|
+
const endpoint = (
|
|
114
|
+
options.endpoint ??
|
|
115
|
+
getEnv('ES_AZURE_ENDPOINT') ??
|
|
116
|
+
`https://${storageAccountName}.blob.core.windows.net`
|
|
117
|
+
).replace(/\/+$/, '');
|
|
118
|
+
const baseUrl =
|
|
119
|
+
options.baseUrl ??
|
|
120
|
+
getEnv('EDGE_STORE_BASE_URL') ??
|
|
121
|
+
`${endpoint}/${containerName}`;
|
|
122
|
+
const keys = createObjectKeys('Azure Blob', baseUrl);
|
|
123
|
+
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|
|
124
|
+
const readExpiresIn = expiration(options.signedUrlExpiresIn ?? 3600);
|
|
71
125
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
'Azure
|
|
85
|
-
|
|
126
|
+
function plan(sizeBytes: number) {
|
|
127
|
+
if (!Number.isSafeInteger(sizeBytes) || sizeBytes < 0)
|
|
128
|
+
throw new RangeError(
|
|
129
|
+
'Azure Blob upload size must be a nonnegative safe integer.',
|
|
130
|
+
);
|
|
131
|
+
const result = planMultipartUpload({
|
|
132
|
+
sizeBytes,
|
|
133
|
+
thresholdBytes: options.multipart?.thresholdBytes,
|
|
134
|
+
preferredPartSizeBytes: options.multipart?.partSizeBytes,
|
|
135
|
+
forceMultipart: sizeBytes > MAX_SINGLE_UPLOAD_BYTES,
|
|
136
|
+
});
|
|
137
|
+
if (result && result.partSizeBytes > MAX_BLOCK_BYTES)
|
|
138
|
+
throw new RangeError('Azure blocks cannot exceed 4000 MiB.');
|
|
139
|
+
return result;
|
|
86
140
|
}
|
|
87
|
-
|
|
141
|
+
// Validate multipart settings even before the first large upload.
|
|
142
|
+
plan(0);
|
|
88
143
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
144
|
+
// Credentials are resolved on first use, so defining a router needs none.
|
|
145
|
+
let resolved:
|
|
146
|
+
| {
|
|
147
|
+
containerName: string;
|
|
148
|
+
credential: StorageSharedKeyCredential;
|
|
149
|
+
container: ReturnType<BlobServiceClient['getContainerClient']>;
|
|
150
|
+
}
|
|
151
|
+
| undefined;
|
|
152
|
+
function storage() {
|
|
153
|
+
if (!storageAccountName || !storageAccountKey || !containerName) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
'Azure Blob requires storageAccountName, storageAccountKey, and containerName (or ES_AZURE_ACCOUNT_NAME, ES_AZURE_ACCOUNT_KEY, and ES_AZURE_CONTAINER_NAME).',
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
if (!resolved) {
|
|
159
|
+
const credential = new StorageSharedKeyCredential(
|
|
160
|
+
storageAccountName,
|
|
161
|
+
storageAccountKey,
|
|
162
|
+
);
|
|
163
|
+
resolved = {
|
|
164
|
+
containerName,
|
|
165
|
+
credential,
|
|
166
|
+
container: new BlobServiceClient(
|
|
167
|
+
endpoint,
|
|
168
|
+
credential,
|
|
169
|
+
).getContainerClient(containerName),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return resolved;
|
|
173
|
+
}
|
|
174
|
+
const blob = (key: string) => storage().container.getBlockBlobClient(key);
|
|
99
175
|
|
|
100
|
-
function
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}) {
|
|
105
|
-
const expiresAt = new Date(Date.now() + params.expiresIn * 1000);
|
|
106
|
-
const sas = generateBlobSASQueryParameters(
|
|
176
|
+
function sas(key: string, permissions: string, expiresIn: number) {
|
|
177
|
+
const expiresAt = new Date(Date.now() + expiresIn * 1000);
|
|
178
|
+
const { containerName: container, credential } = storage();
|
|
179
|
+
const query = generateBlobSASQueryParameters(
|
|
107
180
|
{
|
|
108
|
-
containerName:
|
|
109
|
-
blobName:
|
|
110
|
-
permissions: BlobSASPermissions.parse(
|
|
111
|
-
protocol:
|
|
181
|
+
containerName: container,
|
|
182
|
+
blobName: key,
|
|
183
|
+
permissions: BlobSASPermissions.parse(permissions),
|
|
184
|
+
protocol: endpoint.startsWith('https:')
|
|
112
185
|
? SASProtocol.Https
|
|
113
186
|
: SASProtocol.HttpsAndHttp,
|
|
187
|
+
// Tolerate clock skew between this server and Azure.
|
|
114
188
|
startsOn: new Date(Date.now() - 5 * 60 * 1000),
|
|
115
189
|
expiresOn: expiresAt,
|
|
116
190
|
},
|
|
117
|
-
|
|
191
|
+
credential,
|
|
118
192
|
).toString();
|
|
119
|
-
|
|
193
|
+
return { query, expiresAt };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function signedRead(key: string, expiresIn = readExpiresIn) {
|
|
197
|
+
const ttl = expiration(expiresIn);
|
|
198
|
+
const { query, expiresAt } = sas(key, 'r', ttl);
|
|
120
199
|
return {
|
|
121
|
-
url:
|
|
122
|
-
signedUrl: `${
|
|
200
|
+
url: keys.toUrl(key),
|
|
201
|
+
signedUrl: `${blob(key).url}?${query}`,
|
|
123
202
|
expiresAt,
|
|
124
|
-
expiresIn:
|
|
203
|
+
expiresIn: ttl,
|
|
125
204
|
};
|
|
126
205
|
}
|
|
127
206
|
|
|
128
|
-
function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const containerUrl = new URL(containerClient.url);
|
|
132
|
-
const containerPath = containerUrl.pathname.replace(/\/$/, '');
|
|
133
|
-
|
|
134
|
-
if (blobUrl.origin !== containerUrl.origin) {
|
|
135
|
-
throw new Error();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (!blobUrl.pathname.startsWith(`${containerPath}/`)) {
|
|
139
|
-
throw new Error();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return decodeURIComponent(
|
|
143
|
-
blobUrl.pathname.slice(containerPath.length + 1),
|
|
144
|
-
);
|
|
145
|
-
} catch {
|
|
146
|
-
throw new Error('File URL does not belong to this Azure Blob provider.');
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function getBucketBlobName(edgestoreBucketName: string, url: string) {
|
|
151
|
-
const blobName = getBlobNameFromUrl(url);
|
|
152
|
-
if (!blobName.startsWith(`${edgestoreBucketName}/`)) {
|
|
153
|
-
throw new Error(
|
|
154
|
-
`File does not belong to EdgeStore bucket "${edgestoreBucketName}".`,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
return blobName;
|
|
207
|
+
function readForUpload(key: string, params: RequestUploadParams) {
|
|
208
|
+
if (params.fileInfo.isPublic || !params.autoSignedUrls) return undefined;
|
|
209
|
+
return signedRead(key, params.autoSignedUrls.expiresIn);
|
|
158
210
|
}
|
|
159
211
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
:
|
|
165
|
-
|
|
212
|
+
const blocks = createBlockUploads({
|
|
213
|
+
blob,
|
|
214
|
+
sessions: createMultipartSessions({
|
|
215
|
+
providerName: 'Azure Blob',
|
|
216
|
+
audience: `edgestore:azure-blob:${endpoint}:${containerName}`,
|
|
217
|
+
secret: options.jwtSecret,
|
|
218
|
+
expiresIn: options.multipart?.sessionExpiresIn ?? 86400,
|
|
219
|
+
}),
|
|
220
|
+
signPartUrl: (key, id) =>
|
|
221
|
+
`${blob(key).url}?comp=block&blockid=${encodeURIComponent(id)}&${
|
|
222
|
+
sas(key, 'w', uploadExpiresIn).query
|
|
223
|
+
}`,
|
|
224
|
+
});
|
|
166
225
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
226
|
+
async function prepare({ bucketName, fileInfo }: RequestUploadParams) {
|
|
227
|
+
const { key, pathArgs } = await keys.forUpload(
|
|
228
|
+
bucketName,
|
|
229
|
+
fileInfo,
|
|
230
|
+
options.path,
|
|
231
|
+
);
|
|
232
|
+
const objectOptions =
|
|
233
|
+
typeof options.objectOptions === 'function'
|
|
234
|
+
? await options.objectOptions(pathArgs)
|
|
235
|
+
: options.objectOptions;
|
|
236
|
+
const commit: BlobCommitOptions = {
|
|
237
|
+
blobHTTPHeaders: {
|
|
238
|
+
blobContentType: fileInfo.type || 'application/octet-stream',
|
|
239
|
+
blobCacheControl: objectOptions?.cacheControl,
|
|
240
|
+
blobContentDisposition: objectOptions?.contentDisposition,
|
|
241
|
+
},
|
|
242
|
+
metadata: objectOptions?.metadata,
|
|
243
|
+
};
|
|
244
|
+
return { key, commit, plan: plan(fileInfo.size) };
|
|
173
245
|
}
|
|
174
246
|
|
|
175
247
|
return defineProvider({
|
|
176
248
|
name: 'azure-blob',
|
|
177
|
-
baseUrl,
|
|
178
249
|
reference: {
|
|
179
|
-
schema: z.
|
|
250
|
+
schema: z.union([
|
|
251
|
+
z.object({ key: z.string().min(1) }),
|
|
252
|
+
z.object({ url: z.string().url() }),
|
|
253
|
+
]),
|
|
180
254
|
fromUrl: (url) => ({ url }),
|
|
181
255
|
},
|
|
182
|
-
async init() {
|
|
256
|
+
async init({ router }) {
|
|
257
|
+
storage();
|
|
258
|
+
assertSignedUrlAccessControl('Azure Blob', router);
|
|
183
259
|
return {};
|
|
184
260
|
},
|
|
185
261
|
uploads: {
|
|
186
262
|
supportedOptions: { temporary: false, replaceTargetUrl: false },
|
|
187
|
-
async request(params) {
|
|
188
|
-
const
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
263
|
+
async request(params): Promise<RequestUploadRes> {
|
|
264
|
+
const { key, commit, plan } = await prepare(params);
|
|
265
|
+
const signedReadUrl = readForUpload(key, params);
|
|
266
|
+
const access = {
|
|
267
|
+
key,
|
|
268
|
+
url: keys.toUrl(key),
|
|
269
|
+
...(signedReadUrl ? { signedReadUrl } : {}),
|
|
270
|
+
};
|
|
271
|
+
if (plan) {
|
|
272
|
+
return {
|
|
273
|
+
...access,
|
|
274
|
+
multipart: await blocks.request({
|
|
275
|
+
key,
|
|
276
|
+
size: params.fileInfo.size,
|
|
277
|
+
plan,
|
|
278
|
+
commit,
|
|
279
|
+
}),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
...access,
|
|
284
|
+
uploadUrl: `${blob(key).url}?${sas(key, 'cw', uploadExpiresIn).query}`,
|
|
285
|
+
uploadHeaders: blobUploadHeaders(commit),
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
multipart: blocks.operations,
|
|
289
|
+
async upload(params) {
|
|
290
|
+
const { key, commit, plan } = await prepare({
|
|
291
|
+
...params,
|
|
292
|
+
fileInfo: {
|
|
293
|
+
...params.fileInfo,
|
|
294
|
+
size: params.source.size,
|
|
295
|
+
type: params.source.type || params.fileInfo.type,
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
// Sign before sending bytes: nothing fallible runs after the commit.
|
|
299
|
+
const signedReadUrl = readForUpload(key, params);
|
|
300
|
+
await uploadBlob({
|
|
301
|
+
client: blob(key),
|
|
302
|
+
commit,
|
|
303
|
+
plan,
|
|
304
|
+
source: params.source,
|
|
305
|
+
signal: params.signal,
|
|
306
|
+
onProgress: params.onProgress,
|
|
193
307
|
});
|
|
194
|
-
const
|
|
195
|
-
? undefined
|
|
196
|
-
: createSignedBlobUrl({
|
|
197
|
-
blobName,
|
|
198
|
-
permissions: 'r',
|
|
199
|
-
expiresIn: signedUrlExpiresIn,
|
|
200
|
-
});
|
|
308
|
+
const uploadedAt = new Date();
|
|
201
309
|
return {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
310
|
+
file: {
|
|
311
|
+
key,
|
|
312
|
+
url: keys.toUrl(key),
|
|
313
|
+
sizeBytes: params.source.size,
|
|
314
|
+
uploadedAt,
|
|
315
|
+
updatedAt: uploadedAt,
|
|
316
|
+
},
|
|
317
|
+
signedReadUrl,
|
|
208
318
|
};
|
|
209
319
|
},
|
|
210
320
|
},
|
|
211
321
|
files: {
|
|
212
322
|
async get({ bucketName, file }) {
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
await blobClient.getProperties();
|
|
218
|
-
const timestamp = lastModified ?? new Date();
|
|
323
|
+
const key = keys.fromReference(bucketName, file);
|
|
324
|
+
const { contentLength, lastModified } = await blob(key).getProperties();
|
|
325
|
+
if (contentLength === undefined || !lastModified)
|
|
326
|
+
throw new Error('File not found');
|
|
219
327
|
return {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
328
|
+
key,
|
|
329
|
+
url: keys.toUrl(key),
|
|
330
|
+
sizeBytes: contentLength,
|
|
331
|
+
uploadedAt: lastModified,
|
|
332
|
+
updatedAt: lastModified,
|
|
224
333
|
};
|
|
225
334
|
},
|
|
226
335
|
async getSignedUrls({ bucketName, files, expiresIn }) {
|
|
227
|
-
return files.map((file) =>
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
permissions: 'r',
|
|
231
|
-
expiresIn: expiresIn ?? signedUrlExpiresIn,
|
|
232
|
-
});
|
|
233
|
-
return {
|
|
234
|
-
url: access.url,
|
|
235
|
-
signedUrl: access.signedUrl,
|
|
236
|
-
expiresAt: access.expiresAt,
|
|
237
|
-
expiresIn: access.expiresIn,
|
|
238
|
-
};
|
|
239
|
-
});
|
|
336
|
+
return files.map((file) =>
|
|
337
|
+
signedRead(keys.fromReference(bucketName, file), expiresIn),
|
|
338
|
+
);
|
|
240
339
|
},
|
|
241
340
|
async delete({ bucketName, files }) {
|
|
242
|
-
const
|
|
243
|
-
|
|
341
|
+
const blobKeys = files.map((file) =>
|
|
342
|
+
keys.fromReference(bucketName, file),
|
|
244
343
|
);
|
|
245
|
-
const results = await
|
|
246
|
-
|
|
344
|
+
const results = await runConcurrently(
|
|
345
|
+
blobKeys,
|
|
346
|
+
undefined,
|
|
347
|
+
async (key) => {
|
|
247
348
|
try {
|
|
248
|
-
|
|
249
|
-
await blobClient.delete();
|
|
349
|
+
await blob(key).deleteIfExists();
|
|
250
350
|
return { success: true as const };
|
|
251
351
|
} catch (error) {
|
|
252
352
|
return {
|
|
@@ -258,10 +358,28 @@ export function azureBlob(options?: AzureBlobProviderOptions) {
|
|
|
258
358
|
},
|
|
259
359
|
};
|
|
260
360
|
}
|
|
261
|
-
}
|
|
361
|
+
},
|
|
262
362
|
);
|
|
263
363
|
return { results };
|
|
264
364
|
},
|
|
265
365
|
},
|
|
266
366
|
});
|
|
267
367
|
}
|
|
368
|
+
|
|
369
|
+
/** Headers a browser Put Blob request must send to apply the blob settings. */
|
|
370
|
+
function blobUploadHeaders({ blobHTTPHeaders, metadata }: BlobCommitOptions) {
|
|
371
|
+
const headers: Record<string, string> = { 'x-ms-blob-type': 'BlockBlob' };
|
|
372
|
+
const fields = {
|
|
373
|
+
blobContentType: 'x-ms-blob-content-type',
|
|
374
|
+
blobCacheControl: 'x-ms-blob-cache-control',
|
|
375
|
+
blobContentDisposition: 'x-ms-blob-content-disposition',
|
|
376
|
+
} as const;
|
|
377
|
+
for (const [field, header] of Object.entries(fields)) {
|
|
378
|
+
const value = blobHTTPHeaders[field as keyof typeof fields];
|
|
379
|
+
if (value !== undefined) headers[header] = value;
|
|
380
|
+
}
|
|
381
|
+
for (const [name, value] of Object.entries(metadata ?? {})) {
|
|
382
|
+
headers[`x-ms-meta-${name}`] = value;
|
|
383
|
+
}
|
|
384
|
+
return headers;
|
|
385
|
+
}
|
|
@@ -68,12 +68,10 @@ export type EdgeStoreProviderOptions = EdgeStoreProviderCommonOptions &
|
|
|
68
68
|
(EdgeStoreProjectProviderOptions | EdgeStoreBearerProviderOptions);
|
|
69
69
|
|
|
70
70
|
export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
71
|
-
const baseUrl = getEnv('EDGE_STORE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
72
71
|
const runtime = createProviderRuntime(options);
|
|
73
72
|
|
|
74
73
|
const provider = defineProvider({
|
|
75
74
|
name: 'edgestore',
|
|
76
|
-
baseUrl,
|
|
77
75
|
reference: {
|
|
78
76
|
schema: fileReferenceSchema,
|
|
79
77
|
fromUrl: (url) => ({ url }),
|
|
@@ -114,10 +112,13 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
|
114
112
|
});
|
|
115
113
|
const overrideBaseUrl = getEnv('EDGE_STORE_BASE_URL');
|
|
116
114
|
return {
|
|
117
|
-
baseUrl: overrideBaseUrl ?? delivery?.baseUrl ?? baseUrl,
|
|
118
115
|
clientInit: {
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
urls:
|
|
117
|
+
delivery && !overrideBaseUrl
|
|
118
|
+
? delivery.initUrls
|
|
119
|
+
: [
|
|
120
|
+
`${(overrideBaseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '')}/_init`,
|
|
121
|
+
],
|
|
121
122
|
headers: {
|
|
122
123
|
'x-edgestore-token': token,
|
|
123
124
|
},
|
|
@@ -175,7 +176,15 @@ export function edgestore(options?: EdgeStoreProviderOptions) {
|
|
|
175
176
|
complete: async ({ uploadId, parts }) => {
|
|
176
177
|
await runtime.uploads.completeMultipart({
|
|
177
178
|
uploadId,
|
|
178
|
-
parts,
|
|
179
|
+
parts: parts.map(({ partNumber, eTag }) => {
|
|
180
|
+
if (!eTag) {
|
|
181
|
+
throw new EdgeStoreError({
|
|
182
|
+
message: `Multipart part ${partNumber} is missing its ETag.`,
|
|
183
|
+
code: 'BAD_REQUEST',
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return { partNumber, eTag };
|
|
187
|
+
}),
|
|
179
188
|
});
|
|
180
189
|
},
|
|
181
190
|
abort: async ({ uploadId }) => {
|
|
@@ -342,16 +351,8 @@ function resolveProviderAuthentication(
|
|
|
342
351
|
);
|
|
343
352
|
}
|
|
344
353
|
|
|
345
|
-
const accessKey =
|
|
346
|
-
|
|
347
|
-
getEnv('EDGE_STORE_ACCESS_KEY') ??
|
|
348
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
349
|
-
import.meta.env?.EDGE_STORE_ACCESS_KEY;
|
|
350
|
-
const secretKey =
|
|
351
|
-
configuredSecretKey ??
|
|
352
|
-
getEnv('EDGE_STORE_SECRET_KEY') ??
|
|
353
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
354
|
-
import.meta.env?.EDGE_STORE_SECRET_KEY;
|
|
354
|
+
const accessKey = configuredAccessKey ?? getEnv('EDGE_STORE_ACCESS_KEY');
|
|
355
|
+
const secretKey = configuredSecretKey ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
355
356
|
|
|
356
357
|
if (
|
|
357
358
|
typeof accessKey !== 'string' ||
|
|
@@ -451,15 +452,11 @@ function mapUploadResponse(
|
|
|
451
452
|
res: Awaited<ReturnType<ProjectRuntimeClient['uploads']['request']>>,
|
|
452
453
|
multipartConfig?: { partSize: number; totalParts: number },
|
|
453
454
|
): RequestUploadRes {
|
|
454
|
-
const signed = res.signedReadUrl;
|
|
455
455
|
const access = {
|
|
456
456
|
key: res.file.key,
|
|
457
|
-
|
|
457
|
+
url: res.file.url,
|
|
458
458
|
thumbnailUrl: res.file.thumbnailUrl,
|
|
459
|
-
|
|
460
|
-
accessSignedThumbnailUrl: signed?.signedThumbnailUrl,
|
|
461
|
-
accessSignedUrlExpiresAt: signed?.expiresAt,
|
|
462
|
-
accessSignedUrlExpiresIn: signed?.expiresIn,
|
|
459
|
+
signedReadUrl: res.signedReadUrl,
|
|
463
460
|
};
|
|
464
461
|
if (res.upload.kind === 'single') {
|
|
465
462
|
return { ...access, uploadUrl: res.upload.signedUrl };
|