@edgestore/server 0.7.0 → 1.0.0-next.0
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/adapters/astro/index.d.ts +1 -1
- package/adapters/astro/index.js +1 -1
- package/adapters/express/index.d.ts +1 -1
- package/adapters/express/index.js +1 -1
- package/adapters/fastify/index.d.ts +1 -1
- package/adapters/fastify/index.js +1 -1
- package/adapters/hono/index.d.ts +1 -1
- package/adapters/hono/index.js +1 -1
- package/adapters/next/app/index.d.ts +1 -1
- package/adapters/next/app/index.js +1 -1
- package/adapters/next/pages/index.d.ts +1 -1
- package/adapters/next/pages/index.js +1 -1
- package/adapters/remix/index.d.ts +1 -1
- package/adapters/remix/index.js +1 -1
- package/adapters/start/index.d.ts +1 -1
- package/adapters/start/index.js +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.js +1 -1
- 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 +35 -43
- 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 +34 -41
- package/dist/adapters/fastify/index.d.ts +3 -3
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +33 -42
- package/dist/adapters/hono/index.d.ts +6 -72
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +38 -41
- package/dist/adapters/imageTypes.js +1 -3
- 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 +34 -42
- 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 +35 -42
- 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 +35 -42
- package/dist/adapters/shared.d.ts +8 -0
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +79 -56
- 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 +35 -43
- package/dist/core/client/index.d.ts +112 -69
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +59 -13
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -10
- package/dist/core/sdk/index.d.ts +44 -2
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/core/sdk/index.js +43 -16
- package/dist/index.js +1 -10
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- package/dist/providers/aws/index.d.ts +1 -1
- package/dist/providers/aws/index.d.ts.map +1 -1
- package/dist/providers/aws/index.js +14 -17
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +38 -15
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +36 -18
- package/dist/test-utils/edgestoreSmoke.d.ts +32 -0
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +4 -0
- package/package.json +59 -49
- package/providers/aws/index.d.ts +1 -1
- package/providers/aws/index.js +1 -1
- package/providers/azure/index.d.ts +1 -1
- package/providers/azure/index.js +1 -1
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- package/src/adapters/astro/index.ts +11 -21
- package/src/adapters/express/index.ts +10 -16
- package/src/adapters/fastify/index.ts +13 -19
- package/src/adapters/hono/index.ts +19 -23
- package/src/adapters/next/app/index.ts +12 -22
- package/src/adapters/next/pages/index.ts +11 -16
- package/src/adapters/remix/index.ts +11 -12
- package/src/adapters/shared.ts +61 -17
- package/src/adapters/start/index.ts +11 -12
- package/src/core/client/index.ts +199 -75
- package/src/core/sdk/index.ts +76 -8
- package/src/providers/aws/index.ts +4 -5
- package/src/providers/azure/index.ts +40 -11
- package/src/providers/edgestore/index.ts +29 -0
- package/src/test-utils/edgestoreSmoke.ts +141 -0
- package/src/version.ts +2 -0
- package/dist/adapters/astro/index.mjs +0 -178
- package/dist/adapters/express/index.mjs +0 -113
- package/dist/adapters/fastify/index.mjs +0 -142
- package/dist/adapters/hono/index.mjs +0 -132
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -169
- package/dist/adapters/next/pages/index.mjs +0 -117
- package/dist/adapters/remix/index.mjs +0 -153
- package/dist/adapters/shared.mjs +0 -443
- package/dist/adapters/start/index.mjs +0 -187
- package/dist/core/client/index.mjs +0 -173
- package/dist/core/index.mjs +0 -2
- package/dist/core/sdk/index.mjs +0 -223
- package/dist/index.mjs +0 -1
- package/dist/libs/errors/EdgeStoreCredentialsError.mjs +0 -10
- package/dist/libs/logger.mjs +0 -53
- package/dist/libs/utils.mjs +0 -10
- package/dist/providers/aws/index.mjs +0 -103
- package/dist/providers/azure/index.mjs +0 -59
- package/dist/providers/edgestore/index.mjs +0 -144
package/src/core/sdk/index.ts
CHANGED
|
@@ -6,6 +6,10 @@ import {
|
|
|
6
6
|
} from '@edgestore/shared';
|
|
7
7
|
import { getEnv } from '../../adapters/shared';
|
|
8
8
|
import EdgeStoreCredentialsError from '../../libs/errors/EdgeStoreCredentialsError';
|
|
9
|
+
import {
|
|
10
|
+
EDGE_STORE_PACKAGE_NAME,
|
|
11
|
+
EDGE_STORE_PACKAGE_VERSION,
|
|
12
|
+
} from '../../version';
|
|
9
13
|
|
|
10
14
|
const API_ENDPOINT =
|
|
11
15
|
getEnv('EDGE_STORE_API_ENDPOINT') ?? 'https://api.edgestore.dev';
|
|
@@ -25,15 +29,13 @@ type FileInfoForUpload = {
|
|
|
25
29
|
temporary: boolean;
|
|
26
30
|
};
|
|
27
31
|
|
|
32
|
+
type AutoSignedUrlsForUpload = {
|
|
33
|
+
expiresIn?: number;
|
|
34
|
+
includeThumbnails?: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
28
37
|
export type SimpleOperator =
|
|
29
|
-
| '
|
|
30
|
-
| 'neq'
|
|
31
|
-
| 'gt'
|
|
32
|
-
| 'gte'
|
|
33
|
-
| 'lt'
|
|
34
|
-
| 'lte'
|
|
35
|
-
| 'startsWith'
|
|
36
|
-
| 'endsWith';
|
|
38
|
+
'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'startsWith' | 'endsWith';
|
|
37
39
|
|
|
38
40
|
export type Comparison<TType = string> =
|
|
39
41
|
| TType
|
|
@@ -77,6 +79,8 @@ async function makeRequest<TOutput>(params: {
|
|
|
77
79
|
Authorization: `Basic ${Buffer.from(`${accessKey}:${secretKey}`).toString(
|
|
78
80
|
'base64',
|
|
79
81
|
)}`,
|
|
82
|
+
'x-edgestore-package-name': EDGE_STORE_PACKAGE_NAME,
|
|
83
|
+
'x-edgestore-package-version': EDGE_STORE_PACKAGE_VERSION,
|
|
80
84
|
},
|
|
81
85
|
});
|
|
82
86
|
if (!res.ok) {
|
|
@@ -161,12 +165,14 @@ export const edgeStoreRawSdk = {
|
|
|
161
165
|
bucketType,
|
|
162
166
|
fileInfo,
|
|
163
167
|
multipart,
|
|
168
|
+
autoSignedUrls,
|
|
164
169
|
}: {
|
|
165
170
|
accessKey: string;
|
|
166
171
|
secretKey: string;
|
|
167
172
|
bucketName: string;
|
|
168
173
|
bucketType: string;
|
|
169
174
|
fileInfo: FileInfoForUpload;
|
|
175
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
170
176
|
multipart?: {
|
|
171
177
|
parts: number[];
|
|
172
178
|
};
|
|
@@ -184,6 +190,10 @@ export const edgeStoreRawSdk = {
|
|
|
184
190
|
url: string;
|
|
185
191
|
path: string;
|
|
186
192
|
thumbnailUrl: string | null;
|
|
193
|
+
accessSignedUrl?: string;
|
|
194
|
+
accessSignedThumbnailUrl?: string | null;
|
|
195
|
+
accessSignedUrlExpiresAt?: string;
|
|
196
|
+
accessSignedUrlExpiresIn?: number;
|
|
187
197
|
}>({
|
|
188
198
|
path: '/request-upload',
|
|
189
199
|
accessKey,
|
|
@@ -201,6 +211,7 @@ export const edgeStoreRawSdk = {
|
|
|
201
211
|
fileName: fileInfo.fileName,
|
|
202
212
|
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
203
213
|
isTemporary: fileInfo.temporary,
|
|
214
|
+
autoSignedUrls,
|
|
204
215
|
},
|
|
205
216
|
});
|
|
206
217
|
return {
|
|
@@ -209,9 +220,51 @@ export const edgeStoreRawSdk = {
|
|
|
209
220
|
accessUrl: res.url,
|
|
210
221
|
path: res.path,
|
|
211
222
|
thumbnailUrl: res.thumbnailUrl,
|
|
223
|
+
accessSignedUrl: res.accessSignedUrl,
|
|
224
|
+
accessSignedThumbnailUrl: res.accessSignedThumbnailUrl,
|
|
225
|
+
accessSignedUrlExpiresAt: res.accessSignedUrlExpiresAt,
|
|
226
|
+
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn,
|
|
212
227
|
};
|
|
213
228
|
},
|
|
214
229
|
|
|
230
|
+
async getSignedUrls({
|
|
231
|
+
accessKey,
|
|
232
|
+
secretKey,
|
|
233
|
+
bucketName,
|
|
234
|
+
urls,
|
|
235
|
+
expiresIn,
|
|
236
|
+
includeThumbnails,
|
|
237
|
+
}: {
|
|
238
|
+
accessKey: string;
|
|
239
|
+
secretKey: string;
|
|
240
|
+
bucketName: string;
|
|
241
|
+
urls: string[];
|
|
242
|
+
expiresIn?: number;
|
|
243
|
+
includeThumbnails?: boolean;
|
|
244
|
+
}) {
|
|
245
|
+
const res = await makeRequest<{
|
|
246
|
+
signedUrls: {
|
|
247
|
+
url: string;
|
|
248
|
+
signedUrl: string;
|
|
249
|
+
expiresAt: string;
|
|
250
|
+
expiresIn: number;
|
|
251
|
+
thumbnailUrl?: string | null;
|
|
252
|
+
signedThumbnailUrl?: string | null;
|
|
253
|
+
}[];
|
|
254
|
+
}>({
|
|
255
|
+
path: '/get-signed-urls',
|
|
256
|
+
accessKey,
|
|
257
|
+
secretKey,
|
|
258
|
+
body: {
|
|
259
|
+
bucketName,
|
|
260
|
+
urls,
|
|
261
|
+
expiresIn,
|
|
262
|
+
includeThumbnails,
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
return res.signedUrls;
|
|
266
|
+
},
|
|
267
|
+
|
|
215
268
|
async requestUploadParts({
|
|
216
269
|
accessKey,
|
|
217
270
|
secretKey,
|
|
@@ -389,10 +442,12 @@ export function initEdgeStoreSdk(params: {
|
|
|
389
442
|
bucketType,
|
|
390
443
|
fileInfo,
|
|
391
444
|
multipart,
|
|
445
|
+
autoSignedUrls,
|
|
392
446
|
}: {
|
|
393
447
|
bucketName: string;
|
|
394
448
|
bucketType: string;
|
|
395
449
|
fileInfo: FileInfoForUpload;
|
|
450
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
396
451
|
multipart?: {
|
|
397
452
|
parts: number[];
|
|
398
453
|
};
|
|
@@ -404,6 +459,19 @@ export function initEdgeStoreSdk(params: {
|
|
|
404
459
|
bucketType,
|
|
405
460
|
fileInfo,
|
|
406
461
|
multipart,
|
|
462
|
+
autoSignedUrls,
|
|
463
|
+
});
|
|
464
|
+
},
|
|
465
|
+
async getSignedUrls(params: {
|
|
466
|
+
bucketName: string;
|
|
467
|
+
urls: string[];
|
|
468
|
+
expiresIn?: number;
|
|
469
|
+
includeThumbnails?: boolean;
|
|
470
|
+
}) {
|
|
471
|
+
return await edgeStoreRawSdk.getSignedUrls({
|
|
472
|
+
accessKey,
|
|
473
|
+
secretKey,
|
|
474
|
+
...params,
|
|
407
475
|
});
|
|
408
476
|
},
|
|
409
477
|
async requestUploadParts({
|
|
@@ -7,11 +7,10 @@ import {
|
|
|
7
7
|
} from '@aws-sdk/client-s3';
|
|
8
8
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
9
9
|
import {
|
|
10
|
-
type MaybePromise,
|
|
11
10
|
type Provider as EdgeStoreProvider,
|
|
11
|
+
type MaybePromise,
|
|
12
12
|
type RequestUploadParams,
|
|
13
13
|
} from '@edgestore/shared';
|
|
14
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
15
14
|
import { getEnv } from '../../adapters/shared';
|
|
16
15
|
|
|
17
16
|
// FileInfo type as received by the provider's requestUpload, part of RequestUploadParams
|
|
@@ -175,12 +174,11 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
175
174
|
const pathPrefix = `${esBucketName}${
|
|
176
175
|
fileInfo.isPublic ? '/_public' : ''
|
|
177
176
|
}`;
|
|
178
|
-
const nameId = uuidv4();
|
|
179
177
|
const extension = fileInfo.extension
|
|
180
178
|
? `.${fileInfo.extension.replace('.', '')}`
|
|
181
179
|
: '';
|
|
182
180
|
const defaultResolvedFileName =
|
|
183
|
-
fileInfo.fileName ?? `${
|
|
181
|
+
fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`;
|
|
184
182
|
const defaultFilePathFromMetadata = fileInfo.path.reduce((acc, item) => {
|
|
185
183
|
return `${acc}/${item.value}`;
|
|
186
184
|
}, '');
|
|
@@ -194,6 +192,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
194
192
|
defaultAccessPath: accessPath,
|
|
195
193
|
});
|
|
196
194
|
}
|
|
195
|
+
accessPath = accessPath.replace(/^\/+/, '');
|
|
197
196
|
|
|
198
197
|
const command = new PutObjectCommand({
|
|
199
198
|
Bucket: bucketName,
|
|
@@ -204,7 +203,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
204
203
|
expiresIn: 60 * 60, // 1 hour
|
|
205
204
|
});
|
|
206
205
|
|
|
207
|
-
const finalAccessUrl = `${baseUrl}/${accessPath
|
|
206
|
+
const finalAccessUrl = `${baseUrl}/${accessPath}`;
|
|
208
207
|
return {
|
|
209
208
|
uploadUrl: signedUrl,
|
|
210
209
|
accessUrl: finalAccessUrl,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { BlobServiceClient } from '@azure/storage-blob';
|
|
2
2
|
import { type Provider } from '@edgestore/shared';
|
|
3
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
4
3
|
import { getEnv } from '../../adapters/shared';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -54,6 +53,42 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
54
53
|
const containerClient = blobServiceClient.getContainerClient(
|
|
55
54
|
containerName ?? '',
|
|
56
55
|
);
|
|
56
|
+
function getBlobNameFromUrl(url: string) {
|
|
57
|
+
try {
|
|
58
|
+
const blobUrl = new URL(url);
|
|
59
|
+
const containerUrl = new URL(containerClient.url);
|
|
60
|
+
const containerPath = containerUrl.pathname.replace(/\/$/, '');
|
|
61
|
+
|
|
62
|
+
if (blobUrl.origin !== containerUrl.origin) {
|
|
63
|
+
return url;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!blobUrl.pathname.startsWith(`${containerPath}/`)) {
|
|
67
|
+
return url;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return decodeURIComponent(
|
|
71
|
+
blobUrl.pathname.slice(containerPath.length + 1),
|
|
72
|
+
);
|
|
73
|
+
} catch {
|
|
74
|
+
return url;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getBlobName(params: Parameters<Provider['requestUpload']>[0]) {
|
|
79
|
+
const { bucketName: esBucketName, fileInfo } = params;
|
|
80
|
+
const extension = fileInfo.extension
|
|
81
|
+
? `.${fileInfo.extension.replace('.', '')}`
|
|
82
|
+
: '';
|
|
83
|
+
const fileName = fileInfo.fileName ?? `${crypto.randomUUID()}${extension}`;
|
|
84
|
+
|
|
85
|
+
return [
|
|
86
|
+
esBucketName,
|
|
87
|
+
...(fileInfo.isPublic ? ['_public'] : []),
|
|
88
|
+
...fileInfo.path.map((item) => item.value),
|
|
89
|
+
fileName,
|
|
90
|
+
].join('/');
|
|
91
|
+
}
|
|
57
92
|
|
|
58
93
|
return {
|
|
59
94
|
name: 'azure',
|
|
@@ -64,7 +99,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
64
99
|
return baseUrl;
|
|
65
100
|
},
|
|
66
101
|
async getFile({ url }) {
|
|
67
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
102
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
68
103
|
|
|
69
104
|
const { contentLength, lastModified } = await blobClient.getProperties();
|
|
70
105
|
|
|
@@ -76,14 +111,8 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
76
111
|
uploadedAt: lastModified ?? new Date(),
|
|
77
112
|
};
|
|
78
113
|
},
|
|
79
|
-
async requestUpload(
|
|
80
|
-
const
|
|
81
|
-
const extension = fileInfo.extension
|
|
82
|
-
? `.${fileInfo.extension.replace('.', '')}`
|
|
83
|
-
: '';
|
|
84
|
-
const fileName = fileInfo.fileName ?? `${nameId}${extension}`;
|
|
85
|
-
|
|
86
|
-
const blobClient = containerClient.getBlobClient(fileName);
|
|
114
|
+
async requestUpload(params) {
|
|
115
|
+
const blobClient = containerClient.getBlobClient(getBlobName(params));
|
|
87
116
|
|
|
88
117
|
const url = blobClient.url;
|
|
89
118
|
|
|
@@ -102,7 +131,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
102
131
|
throw new Error('Not implemented');
|
|
103
132
|
},
|
|
104
133
|
async deleteFile({ url }) {
|
|
105
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
134
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
106
135
|
await blobClient.delete();
|
|
107
136
|
return {
|
|
108
137
|
success: true,
|
|
@@ -75,6 +75,7 @@ export function EdgeStoreProvider(
|
|
|
75
75
|
bucketName,
|
|
76
76
|
bucketType,
|
|
77
77
|
fileInfo,
|
|
78
|
+
autoSignedUrls,
|
|
78
79
|
}): Promise<RequestUploadRes> {
|
|
79
80
|
// multipart upload if file is bigger than a certain size
|
|
80
81
|
const MULTIPART_THRESHOLD = 10 * 1024 * 1024; // 10MB
|
|
@@ -90,6 +91,7 @@ export function EdgeStoreProvider(
|
|
|
90
91
|
bucketName,
|
|
91
92
|
bucketType,
|
|
92
93
|
fileInfo,
|
|
94
|
+
autoSignedUrls,
|
|
93
95
|
multipart: {
|
|
94
96
|
parts: Array.from({ length: totalParts }).map(
|
|
95
97
|
(_, index) => index + 1,
|
|
@@ -112,6 +114,7 @@ export function EdgeStoreProvider(
|
|
|
112
114
|
return {
|
|
113
115
|
accessUrl: res.accessUrl,
|
|
114
116
|
thumbnailUrl: res.thumbnailUrl,
|
|
117
|
+
...mapSignedUploadAccess(res),
|
|
115
118
|
multipart,
|
|
116
119
|
};
|
|
117
120
|
} else if (res.signedUrl) {
|
|
@@ -119,6 +122,7 @@ export function EdgeStoreProvider(
|
|
|
119
122
|
accessUrl: res.accessUrl,
|
|
120
123
|
uploadUrl: res.signedUrl,
|
|
121
124
|
thumbnailUrl: res.thumbnailUrl,
|
|
125
|
+
...mapSignedUploadAccess(res),
|
|
122
126
|
};
|
|
123
127
|
} else {
|
|
124
128
|
throw new EdgeStoreError({
|
|
@@ -131,12 +135,14 @@ export function EdgeStoreProvider(
|
|
|
131
135
|
bucketName,
|
|
132
136
|
bucketType,
|
|
133
137
|
fileInfo,
|
|
138
|
+
autoSignedUrls,
|
|
134
139
|
});
|
|
135
140
|
if (res.signedUrl) {
|
|
136
141
|
return {
|
|
137
142
|
accessUrl: res.accessUrl,
|
|
138
143
|
uploadUrl: res.signedUrl,
|
|
139
144
|
thumbnailUrl: res.thumbnailUrl,
|
|
145
|
+
...mapSignedUploadAccess(res),
|
|
140
146
|
};
|
|
141
147
|
}
|
|
142
148
|
throw new EdgeStoreError({
|
|
@@ -159,6 +165,13 @@ export function EdgeStoreProvider(
|
|
|
159
165
|
},
|
|
160
166
|
};
|
|
161
167
|
},
|
|
168
|
+
getSignedUrls: async (params) => {
|
|
169
|
+
const res = await edgeStoreSdk.getSignedUrls(params);
|
|
170
|
+
return res.map((item) => ({
|
|
171
|
+
...item,
|
|
172
|
+
expiresAt: new Date(item.expiresAt),
|
|
173
|
+
}));
|
|
174
|
+
},
|
|
162
175
|
completeMultipartUpload: async ({ uploadId, key, parts }) => {
|
|
163
176
|
return await edgeStoreSdk.completeMultipartUpload({
|
|
164
177
|
uploadId,
|
|
@@ -178,3 +191,19 @@ export function EdgeStoreProvider(
|
|
|
178
191
|
},
|
|
179
192
|
};
|
|
180
193
|
}
|
|
194
|
+
|
|
195
|
+
function mapSignedUploadAccess(res: {
|
|
196
|
+
accessSignedUrl?: string;
|
|
197
|
+
accessSignedThumbnailUrl?: string | null;
|
|
198
|
+
accessSignedUrlExpiresAt?: string;
|
|
199
|
+
accessSignedUrlExpiresIn?: number;
|
|
200
|
+
}) {
|
|
201
|
+
return {
|
|
202
|
+
accessSignedUrl: res.accessSignedUrl,
|
|
203
|
+
accessSignedThumbnailUrl: res.accessSignedThumbnailUrl,
|
|
204
|
+
accessSignedUrlExpiresAt: res.accessSignedUrlExpiresAt
|
|
205
|
+
? new Date(res.accessSignedUrlExpiresAt)
|
|
206
|
+
: undefined,
|
|
207
|
+
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export const SMOKE_CONTENT = 'edgestore smoke test';
|
|
4
|
+
|
|
5
|
+
export function getSmokeApiEndpoint() {
|
|
6
|
+
return process.env.EDGE_STORE_API_ENDPOINT ?? 'https://api.edgestore.dev';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getSmokeBucketName() {
|
|
10
|
+
return process.env.EDGESTORE_SMOKE_BUCKET_NAME ?? 'publicFiles';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getSmokeBasicAuthHeader() {
|
|
14
|
+
requireSmokeCredentials();
|
|
15
|
+
return `Basic ${Buffer.from(
|
|
16
|
+
`${process.env.EDGE_STORE_ACCESS_KEY}:${process.env.EDGE_STORE_SECRET_KEY}`,
|
|
17
|
+
).toString('base64')}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createSmokeFileName(prefix: string) {
|
|
21
|
+
return `${prefix}-smoke-${randomUUID()}.txt`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function requireSmokeCredentials() {
|
|
25
|
+
if (
|
|
26
|
+
!process.env.EDGE_STORE_ACCESS_KEY ||
|
|
27
|
+
!process.env.EDGE_STORE_SECRET_KEY
|
|
28
|
+
) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
'EDGE_STORE_ACCESS_KEY and EDGE_STORE_SECRET_KEY are required',
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function wait(ms: number) {
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function retryUntilSuccess(params: {
|
|
40
|
+
action: () => Promise<{ success: boolean }>;
|
|
41
|
+
description: string;
|
|
42
|
+
}) {
|
|
43
|
+
const delays = [250, 500, 1000, 2000, 4000];
|
|
44
|
+
let lastResult: { success: boolean } | undefined;
|
|
45
|
+
let lastError: unknown;
|
|
46
|
+
|
|
47
|
+
for (const delay of delays) {
|
|
48
|
+
try {
|
|
49
|
+
lastResult = await params.action();
|
|
50
|
+
lastError = undefined;
|
|
51
|
+
if (lastResult.success) {
|
|
52
|
+
return lastResult;
|
|
53
|
+
}
|
|
54
|
+
} catch (err) {
|
|
55
|
+
lastResult = undefined;
|
|
56
|
+
lastError = err;
|
|
57
|
+
}
|
|
58
|
+
await wait(delay);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
lastResult = await params.action();
|
|
63
|
+
lastError = undefined;
|
|
64
|
+
if (lastResult.success) {
|
|
65
|
+
return lastResult;
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
lastResult = undefined;
|
|
69
|
+
lastError = err;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const lastFailure =
|
|
73
|
+
lastError instanceof Error
|
|
74
|
+
? `${lastError.name}: ${lastError.message}`
|
|
75
|
+
: JSON.stringify(lastError ?? lastResult);
|
|
76
|
+
|
|
77
|
+
throw new Error(`${params.description} did not succeed: ${lastFailure}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type SmokeFileResult = {
|
|
81
|
+
size: number;
|
|
82
|
+
url: string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
function assertSmokeFileResult(
|
|
86
|
+
description: string,
|
|
87
|
+
result: SmokeFileResult,
|
|
88
|
+
expectedSize: number,
|
|
89
|
+
) {
|
|
90
|
+
if (!result.url) {
|
|
91
|
+
throw new Error(`${description} did not return a file URL`);
|
|
92
|
+
}
|
|
93
|
+
if (result.size !== expectedSize) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`${description} returned size ${result.size}, expected ${expectedSize}`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function assertSuccess(description: string, result: { success: boolean }) {
|
|
101
|
+
if (!result.success) {
|
|
102
|
+
throw new Error(`${description} did not succeed`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function runSmokeUploadLifecycle(params: {
|
|
107
|
+
confirmUpload: (url: string) => Promise<{ success: boolean }>;
|
|
108
|
+
deleteFile: (url: string) => Promise<{ success: boolean }>;
|
|
109
|
+
deleteDescription: string;
|
|
110
|
+
expectedSize: number;
|
|
111
|
+
getFile?: (url: string) => Promise<SmokeFileResult>;
|
|
112
|
+
upload: () => Promise<SmokeFileResult>;
|
|
113
|
+
}) {
|
|
114
|
+
let accessUrl: string | undefined;
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const uploadRes = await params.upload();
|
|
118
|
+
assertSmokeFileResult('upload', uploadRes, params.expectedSize);
|
|
119
|
+
accessUrl = uploadRes.url;
|
|
120
|
+
|
|
121
|
+
assertSuccess('confirmUpload', await params.confirmUpload(uploadRes.url));
|
|
122
|
+
|
|
123
|
+
if (params.getFile) {
|
|
124
|
+
assertSmokeFileResult(
|
|
125
|
+
'getFile',
|
|
126
|
+
await params.getFile(uploadRes.url),
|
|
127
|
+
params.expectedSize,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return uploadRes;
|
|
132
|
+
} finally {
|
|
133
|
+
if (accessUrl) {
|
|
134
|
+
const url = accessUrl;
|
|
135
|
+
await retryUntilSuccess({
|
|
136
|
+
description: params.deleteDescription,
|
|
137
|
+
action: () => params.deleteFile(url),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
package/src/version.ts
ADDED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile } from '../shared.mjs';
|
|
6
|
-
|
|
7
|
-
// Helper to safely get cookies from Astro request
|
|
8
|
-
function getCookie(request, name) {
|
|
9
|
-
const cookieHeader = request.headers.get('cookie');
|
|
10
|
-
if (!cookieHeader) return undefined;
|
|
11
|
-
const cookies = cookieHeader.split(';').reduce((acc, cookie)=>{
|
|
12
|
-
const [key, value] = cookie.trim().split('=');
|
|
13
|
-
if (key && value) acc[key] = value;
|
|
14
|
-
return acc;
|
|
15
|
-
}, {});
|
|
16
|
-
return cookies[name];
|
|
17
|
-
}
|
|
18
|
-
function createEdgeStoreAstroHandler(config) {
|
|
19
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
20
|
-
const log = new Logger(config.logLevel);
|
|
21
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
22
|
-
log.debug('Creating EdgeStore Astro handler');
|
|
23
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
24
|
-
return async (context)=>{
|
|
25
|
-
try {
|
|
26
|
-
const { request } = context;
|
|
27
|
-
const url = new URL(request.url);
|
|
28
|
-
if (matchPath(url.pathname, 'health')) {
|
|
29
|
-
return new Response('OK');
|
|
30
|
-
} else if (matchPath(url.pathname, 'init')) {
|
|
31
|
-
let ctx = {};
|
|
32
|
-
try {
|
|
33
|
-
ctx = 'createContext' in config ? await config.createContext(context) : {};
|
|
34
|
-
} catch (err) {
|
|
35
|
-
throw new EdgeStoreError({
|
|
36
|
-
message: 'Error creating context',
|
|
37
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
38
|
-
cause: err instanceof Error ? err : undefined
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
const { newCookies, token, baseUrl, providerName } = await init({
|
|
42
|
-
ctx,
|
|
43
|
-
provider,
|
|
44
|
-
router: config.router,
|
|
45
|
-
cookieConfig
|
|
46
|
-
});
|
|
47
|
-
const headers = new Headers();
|
|
48
|
-
headers.set('Content-Type', 'application/json');
|
|
49
|
-
// Set cookies
|
|
50
|
-
if (Array.isArray(newCookies)) {
|
|
51
|
-
for (const cookie of newCookies){
|
|
52
|
-
headers.append('Set-Cookie', cookie);
|
|
53
|
-
}
|
|
54
|
-
} else if (newCookies) {
|
|
55
|
-
headers.append('Set-Cookie', newCookies);
|
|
56
|
-
}
|
|
57
|
-
return new Response(JSON.stringify({
|
|
58
|
-
token,
|
|
59
|
-
baseUrl,
|
|
60
|
-
providerName
|
|
61
|
-
}), {
|
|
62
|
-
headers
|
|
63
|
-
});
|
|
64
|
-
} else if (matchPath(url.pathname, 'request-upload')) {
|
|
65
|
-
const body = await request.json();
|
|
66
|
-
const result = await requestUpload({
|
|
67
|
-
provider,
|
|
68
|
-
router: config.router,
|
|
69
|
-
body,
|
|
70
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name)
|
|
71
|
-
});
|
|
72
|
-
return new Response(JSON.stringify(result), {
|
|
73
|
-
headers: {
|
|
74
|
-
'Content-Type': 'application/json'
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
} else if (matchPath(url.pathname, 'request-upload-parts')) {
|
|
78
|
-
const body = await request.json();
|
|
79
|
-
const result = await requestUploadParts({
|
|
80
|
-
provider,
|
|
81
|
-
router: config.router,
|
|
82
|
-
body,
|
|
83
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name)
|
|
84
|
-
});
|
|
85
|
-
return new Response(JSON.stringify(result), {
|
|
86
|
-
headers: {
|
|
87
|
-
'Content-Type': 'application/json'
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
} else if (matchPath(url.pathname, 'complete-multipart-upload')) {
|
|
91
|
-
const body = await request.json();
|
|
92
|
-
await completeMultipartUpload({
|
|
93
|
-
provider,
|
|
94
|
-
router: config.router,
|
|
95
|
-
body,
|
|
96
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name)
|
|
97
|
-
});
|
|
98
|
-
return new Response(null, {
|
|
99
|
-
status: 200
|
|
100
|
-
});
|
|
101
|
-
} else if (matchPath(url.pathname, 'confirm-upload')) {
|
|
102
|
-
const body = await request.json();
|
|
103
|
-
const result = await confirmUpload({
|
|
104
|
-
provider,
|
|
105
|
-
router: config.router,
|
|
106
|
-
body,
|
|
107
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name)
|
|
108
|
-
});
|
|
109
|
-
return new Response(JSON.stringify(result), {
|
|
110
|
-
headers: {
|
|
111
|
-
'Content-Type': 'application/json'
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
} else if (matchPath(url.pathname, 'delete-file')) {
|
|
115
|
-
const body = await request.json();
|
|
116
|
-
const result = await deleteFile({
|
|
117
|
-
provider,
|
|
118
|
-
router: config.router,
|
|
119
|
-
body,
|
|
120
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name)
|
|
121
|
-
});
|
|
122
|
-
return new Response(JSON.stringify(result), {
|
|
123
|
-
headers: {
|
|
124
|
-
'Content-Type': 'application/json'
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
} else if (matchPath(url.pathname, 'proxy-file')) {
|
|
128
|
-
const url = new URL(request.url).searchParams.get('url');
|
|
129
|
-
if (typeof url === 'string') {
|
|
130
|
-
const cookieHeader = request.headers.get('cookie') ?? '';
|
|
131
|
-
const proxyRes = await fetch(url, {
|
|
132
|
-
headers: {
|
|
133
|
-
cookie: cookieHeader
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
const data = await proxyRes.arrayBuffer();
|
|
137
|
-
const headers = new Headers();
|
|
138
|
-
headers.set('Content-Type', proxyRes.headers.get('Content-Type') ?? 'application/octet-stream');
|
|
139
|
-
return new Response(data, {
|
|
140
|
-
headers
|
|
141
|
-
});
|
|
142
|
-
} else {
|
|
143
|
-
return new Response(null, {
|
|
144
|
-
status: 400
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
return new Response(null, {
|
|
149
|
-
status: 404
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
} catch (err) {
|
|
153
|
-
if (err instanceof EdgeStoreError) {
|
|
154
|
-
log[err.level](err.formattedMessage());
|
|
155
|
-
if (err.cause) log[err.level](err.cause);
|
|
156
|
-
return new Response(JSON.stringify(err.formattedJson()), {
|
|
157
|
-
status: EDGE_STORE_ERROR_CODES[err.code],
|
|
158
|
-
headers: {
|
|
159
|
-
'Content-Type': 'application/json'
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
} else {
|
|
163
|
-
log.error(err);
|
|
164
|
-
return new Response(JSON.stringify(new EdgeStoreError({
|
|
165
|
-
message: 'Internal Server Error',
|
|
166
|
-
code: 'SERVER_ERROR'
|
|
167
|
-
}).formattedJson()), {
|
|
168
|
-
status: 500,
|
|
169
|
-
headers: {
|
|
170
|
-
'Content-Type': 'application/json'
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export { createEdgeStoreAstroHandler };
|