@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
|
@@ -6,245 +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
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
262
|
+
supportedOptions: { temporary: false, replaceTargetUrl: false },
|
|
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,
|
|
192
307
|
});
|
|
193
|
-
const
|
|
194
|
-
? undefined
|
|
195
|
-
: createSignedBlobUrl({
|
|
196
|
-
blobName,
|
|
197
|
-
permissions: 'r',
|
|
198
|
-
expiresIn: signedUrlExpiresIn,
|
|
199
|
-
});
|
|
308
|
+
const uploadedAt = new Date();
|
|
200
309
|
return {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
310
|
+
file: {
|
|
311
|
+
key,
|
|
312
|
+
url: keys.toUrl(key),
|
|
313
|
+
sizeBytes: params.source.size,
|
|
314
|
+
uploadedAt,
|
|
315
|
+
updatedAt: uploadedAt,
|
|
316
|
+
},
|
|
317
|
+
signedReadUrl,
|
|
206
318
|
};
|
|
207
319
|
},
|
|
208
320
|
},
|
|
209
321
|
files: {
|
|
210
322
|
async get({ bucketName, file }) {
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
await blobClient.getProperties();
|
|
216
|
-
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');
|
|
217
327
|
return {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
328
|
+
key,
|
|
329
|
+
url: keys.toUrl(key),
|
|
330
|
+
sizeBytes: contentLength,
|
|
331
|
+
uploadedAt: lastModified,
|
|
332
|
+
updatedAt: lastModified,
|
|
222
333
|
};
|
|
223
334
|
},
|
|
224
335
|
async getSignedUrls({ bucketName, files, expiresIn }) {
|
|
225
|
-
return files.map((file) =>
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
permissions: 'r',
|
|
229
|
-
expiresIn: expiresIn ?? signedUrlExpiresIn,
|
|
230
|
-
});
|
|
231
|
-
return {
|
|
232
|
-
url: access.url,
|
|
233
|
-
signedUrl: access.signedUrl,
|
|
234
|
-
expiresAt: access.expiresAt,
|
|
235
|
-
expiresIn: access.expiresIn,
|
|
236
|
-
};
|
|
237
|
-
});
|
|
336
|
+
return files.map((file) =>
|
|
337
|
+
signedRead(keys.fromReference(bucketName, file), expiresIn),
|
|
338
|
+
);
|
|
238
339
|
},
|
|
239
340
|
async delete({ bucketName, files }) {
|
|
240
|
-
const
|
|
241
|
-
|
|
341
|
+
const blobKeys = files.map((file) =>
|
|
342
|
+
keys.fromReference(bucketName, file),
|
|
242
343
|
);
|
|
243
|
-
const results = await
|
|
244
|
-
|
|
344
|
+
const results = await runConcurrently(
|
|
345
|
+
blobKeys,
|
|
346
|
+
undefined,
|
|
347
|
+
async (key) => {
|
|
245
348
|
try {
|
|
246
|
-
|
|
247
|
-
await blobClient.delete();
|
|
349
|
+
await blob(key).deleteIfExists();
|
|
248
350
|
return { success: true as const };
|
|
249
351
|
} catch (error) {
|
|
250
352
|
return {
|
|
@@ -256,10 +358,28 @@ export function azureBlob(options?: AzureBlobProviderOptions) {
|
|
|
256
358
|
},
|
|
257
359
|
};
|
|
258
360
|
}
|
|
259
|
-
}
|
|
361
|
+
},
|
|
260
362
|
);
|
|
261
363
|
return { results };
|
|
262
364
|
},
|
|
263
365
|
},
|
|
264
366
|
});
|
|
265
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
|
+
}
|