@edgestore/server 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +2 -26
- package/dist/adapters/astro/index.mjs +2 -26
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +2 -20
- package/dist/adapters/express/index.mjs +2 -20
- package/dist/adapters/fastify/index.d.ts +1 -1
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +2 -21
- package/dist/adapters/fastify/index.mjs +2 -21
- package/dist/adapters/hono/index.d.ts +4 -70
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +5 -22
- package/dist/adapters/hono/index.mjs +5 -22
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +2 -26
- package/dist/adapters/next/app/index.mjs +2 -26
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +2 -20
- package/dist/adapters/next/pages/index.mjs +2 -20
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +2 -25
- package/dist/adapters/remix/index.mjs +2 -25
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +20 -12
- package/dist/adapters/shared.mjs +20 -12
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +2 -26
- package/dist/adapters/start/index.mjs +2 -26
- package/dist/core/client/index.d.ts +114 -74
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +54 -24
- package/dist/core/client/index.mjs +55 -25
- 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 +36 -6
- package/dist/core/sdk/index.mjs +36 -6
- 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 +3 -3
- package/dist/providers/aws/index.mjs +3 -3
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +33 -7
- package/dist/providers/azure/index.mjs +33 -7
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +24 -4
- package/dist/providers/edgestore/index.mjs +24 -4
- 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 +7 -0
- package/dist/version.mjs +4 -0
- package/package.json +11 -2
- package/src/adapters/astro/index.ts +2 -32
- package/src/adapters/express/index.ts +2 -25
- package/src/adapters/fastify/index.ts +3 -31
- package/src/adapters/hono/index.ts +7 -33
- package/src/adapters/next/app/index.ts +6 -36
- package/src/adapters/next/pages/index.ts +2 -25
- package/src/adapters/remix/index.ts +2 -22
- package/src/adapters/shared.ts +25 -9
- package/src/adapters/start/index.ts +2 -20
- package/src/core/client/index.ts +204 -104
- package/src/core/sdk/index.ts +75 -0
- package/src/providers/aws/index.ts +4 -4
- package/src/providers/azure/index.ts +40 -10
- package/src/providers/edgestore/index.ts +29 -0
- package/src/test-utils/edgestoreSmoke.ts +141 -0
- package/src/version.ts +2 -0
package/src/core/client/index.ts
CHANGED
|
@@ -4,13 +4,15 @@ import {
|
|
|
4
4
|
type AnyRouter,
|
|
5
5
|
type InferBucketPathKeys,
|
|
6
6
|
type InferBucketPathObject,
|
|
7
|
+
type InferBucketPathOrder,
|
|
7
8
|
type InferMetadataObject,
|
|
9
|
+
type MaybePromise,
|
|
8
10
|
type Prettify,
|
|
9
11
|
type Simplify,
|
|
10
12
|
} from '@edgestore/shared';
|
|
11
13
|
import { type z, type ZodNever } from 'zod';
|
|
12
14
|
import { type Comparison } from '..';
|
|
13
|
-
import { buildPath,
|
|
15
|
+
import { buildPath, parsePath } from '../../adapters/shared';
|
|
14
16
|
import { initEdgeStoreSdk } from '../sdk';
|
|
15
17
|
|
|
16
18
|
export type GetFileRes<TBucket extends AnyBuilder> = {
|
|
@@ -49,6 +51,14 @@ export type UploadOptions = {
|
|
|
49
51
|
* This avoids unnecessary zombie files in the bucket.
|
|
50
52
|
*/
|
|
51
53
|
temporary?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Transform the file before it is validated and uploaded.
|
|
56
|
+
*
|
|
57
|
+
* This can be used to compress images, convert formats, encrypt files, etc.
|
|
58
|
+
* The transformed blob's size, MIME type, and extension will be used for the
|
|
59
|
+
* upload request.
|
|
60
|
+
*/
|
|
61
|
+
transform?: ServerUploadTransform;
|
|
52
62
|
};
|
|
53
63
|
|
|
54
64
|
type TextContent = string;
|
|
@@ -61,6 +71,17 @@ type UrlContent = {
|
|
|
61
71
|
extension: string;
|
|
62
72
|
};
|
|
63
73
|
|
|
74
|
+
export type UploadContent = TextContent | BlobContent | UrlContent;
|
|
75
|
+
|
|
76
|
+
export type ServerUploadTransform = (params: {
|
|
77
|
+
blob: Blob;
|
|
78
|
+
extension: string;
|
|
79
|
+
type: string;
|
|
80
|
+
}) => MaybePromise<{
|
|
81
|
+
blob: Blob;
|
|
82
|
+
extension: string;
|
|
83
|
+
}>;
|
|
84
|
+
|
|
64
85
|
// type guard for `content`
|
|
65
86
|
function isTextContent(
|
|
66
87
|
content: TextContent | BlobContent | UrlContent,
|
|
@@ -98,7 +119,7 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
98
119
|
* extension: "csv",
|
|
99
120
|
* }
|
|
100
121
|
*/
|
|
101
|
-
content:
|
|
122
|
+
content: UploadContent;
|
|
102
123
|
options?: UploadOptions;
|
|
103
124
|
} & (TBucket['$config']['ctx'] extends Record<string, never>
|
|
104
125
|
? {}
|
|
@@ -111,23 +132,37 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
111
132
|
input: z.infer<TBucket['_def']['input']>;
|
|
112
133
|
});
|
|
113
134
|
|
|
135
|
+
type UploadImplementationParams = {
|
|
136
|
+
content: UploadContent;
|
|
137
|
+
ctx?: Record<string, unknown>;
|
|
138
|
+
input?: Record<string, unknown>;
|
|
139
|
+
};
|
|
140
|
+
|
|
114
141
|
export type UploadFileRes<TBucket extends AnyBuilder> =
|
|
115
|
-
TBucket['_def']['type'] extends 'IMAGE'
|
|
142
|
+
(TBucket['_def']['type'] extends 'IMAGE'
|
|
116
143
|
? {
|
|
117
144
|
url: string;
|
|
118
145
|
thumbnailUrl: string | null;
|
|
119
146
|
size: number;
|
|
120
147
|
metadata: InferMetadataObject<TBucket>;
|
|
121
148
|
path: InferBucketPathObject<TBucket>;
|
|
122
|
-
pathOrder:
|
|
149
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
123
150
|
}
|
|
124
151
|
: {
|
|
125
152
|
url: string;
|
|
126
153
|
size: number;
|
|
127
154
|
metadata: InferMetadataObject<TBucket>;
|
|
128
155
|
path: InferBucketPathObject<TBucket>;
|
|
129
|
-
pathOrder:
|
|
130
|
-
}
|
|
156
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
157
|
+
}) &
|
|
158
|
+
(undefined extends TBucket['_def']['autoSignedUrls']
|
|
159
|
+
? unknown
|
|
160
|
+
: {
|
|
161
|
+
signedUrl: string;
|
|
162
|
+
expiresAt: Date;
|
|
163
|
+
expiresIn: number;
|
|
164
|
+
signedThumbnailUrl?: string | null;
|
|
165
|
+
});
|
|
131
166
|
|
|
132
167
|
type Filter<TBucket extends AnyBuilder> = {
|
|
133
168
|
AND?: Filter<TBucket>[];
|
|
@@ -173,69 +208,96 @@ export type ListFilesResponse<TBucket extends AnyBuilder> = {
|
|
|
173
208
|
};
|
|
174
209
|
};
|
|
175
210
|
|
|
211
|
+
type GetSignedUrlRes = {
|
|
212
|
+
url: string;
|
|
213
|
+
signedUrl: string;
|
|
214
|
+
expiresAt: Date;
|
|
215
|
+
expiresIn: number;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
type GetSignedUrlsRes<TBucket extends AnyBuilder> =
|
|
219
|
+
TBucket['_def']['type'] extends 'IMAGE'
|
|
220
|
+
? (GetSignedUrlRes & {
|
|
221
|
+
thumbnailUrl?: string | null;
|
|
222
|
+
signedThumbnailUrl?: string | null;
|
|
223
|
+
})[]
|
|
224
|
+
: GetSignedUrlRes[];
|
|
225
|
+
|
|
226
|
+
type BucketClient<TBucket extends AnyBuilder> = {
|
|
227
|
+
getFile: (params: { url: string }) => Promise<Prettify<GetFileRes<TBucket>>>;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Use this function to upload a file to the bucket directly from your backend.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* // simple example
|
|
235
|
+
* await backendClient.myBucket.upload({
|
|
236
|
+
* content: "some text",
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* // complete example
|
|
243
|
+
* await backendClient.myBucket.upload({
|
|
244
|
+
* content: {
|
|
245
|
+
* blob: new Blob([text], { type: "text/csv" }),
|
|
246
|
+
* extension: "csv",
|
|
247
|
+
* },
|
|
248
|
+
* options: {
|
|
249
|
+
* temporary: true,
|
|
250
|
+
* replaceTargetUrl: replaceUrl,
|
|
251
|
+
* manualFileName: "test.csv",
|
|
252
|
+
* },
|
|
253
|
+
* ctx: {
|
|
254
|
+
* userId: "123",
|
|
255
|
+
* userRole: "admin",
|
|
256
|
+
* },
|
|
257
|
+
* input: {
|
|
258
|
+
* type: "post",
|
|
259
|
+
* },
|
|
260
|
+
* });
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
upload: (
|
|
264
|
+
params: Prettify<UploadFileRequest<TBucket>>,
|
|
265
|
+
) => Promise<Prettify<UploadFileRes<TBucket>>>;
|
|
266
|
+
/**
|
|
267
|
+
* Confirm a temporary file upload directly from your backend.
|
|
268
|
+
*/
|
|
269
|
+
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
270
|
+
/**
|
|
271
|
+
* Programmatically delete a file directly from your backend.
|
|
272
|
+
*/
|
|
273
|
+
deleteFile: (params: { url: string }) => Promise<{
|
|
274
|
+
success: boolean;
|
|
275
|
+
}>;
|
|
276
|
+
/**
|
|
277
|
+
* List files in a bucket.
|
|
278
|
+
*
|
|
279
|
+
* You can also filter the results by passing a filter object.
|
|
280
|
+
* The results are paginated.
|
|
281
|
+
*/
|
|
282
|
+
listFiles: (
|
|
283
|
+
params?: ListFilesRequest<TBucket>,
|
|
284
|
+
) => Promise<Prettify<ListFilesResponse<TBucket>>>;
|
|
285
|
+
} & (undefined extends TBucket['_def']['accessControl']
|
|
286
|
+
? unknown
|
|
287
|
+
: {
|
|
288
|
+
getSignedUrl: (params: {
|
|
289
|
+
url: string;
|
|
290
|
+
expiresIn?: number;
|
|
291
|
+
}) => Promise<Prettify<GetSignedUrlRes>>;
|
|
292
|
+
getSignedUrls: (params: {
|
|
293
|
+
urls: string[];
|
|
294
|
+
expiresIn?: number;
|
|
295
|
+
includeThumbnails?: boolean;
|
|
296
|
+
}) => Promise<Prettify<GetSignedUrlsRes<TBucket>[number]>[]>;
|
|
297
|
+
});
|
|
298
|
+
|
|
176
299
|
type EdgeStoreClient<TRouter extends AnyRouter> = {
|
|
177
|
-
[K in keyof TRouter['buckets']]:
|
|
178
|
-
getFile: (params: {
|
|
179
|
-
url: string;
|
|
180
|
-
}) => Promise<GetFileRes<TRouter['buckets'][K]>>;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Use this function to upload a file to the bucket directly from your backend.
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```ts
|
|
187
|
-
* // simple example
|
|
188
|
-
* await backendClient.myBucket.upload({
|
|
189
|
-
* content: "some text",
|
|
190
|
-
* });
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```ts
|
|
195
|
-
* // complete example
|
|
196
|
-
* await backendClient.myBucket.upload({
|
|
197
|
-
* content: {
|
|
198
|
-
* blob: new Blob([text], { type: "text/csv" }),
|
|
199
|
-
* extension: "csv",
|
|
200
|
-
* },
|
|
201
|
-
* options: {
|
|
202
|
-
* temporary: true,
|
|
203
|
-
* replaceTargetUrl: replaceUrl,
|
|
204
|
-
* manualFileName: "test.csv",
|
|
205
|
-
* },
|
|
206
|
-
* ctx: {
|
|
207
|
-
* userId: "123",
|
|
208
|
-
* userRole: "admin",
|
|
209
|
-
* },
|
|
210
|
-
* input: {
|
|
211
|
-
* type: "post",
|
|
212
|
-
* },
|
|
213
|
-
* });
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
upload: (
|
|
217
|
-
params: UploadFileRequest<TRouter['buckets'][K]>,
|
|
218
|
-
) => Promise<Prettify<UploadFileRes<TRouter['buckets'][K]>>>;
|
|
219
|
-
/**
|
|
220
|
-
* Confirm a temporary file upload directly from your backend.
|
|
221
|
-
*/
|
|
222
|
-
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
223
|
-
/**
|
|
224
|
-
* Programmatically delete a file directly from your backend.
|
|
225
|
-
*/
|
|
226
|
-
deleteFile: (params: { url: string }) => Promise<{
|
|
227
|
-
success: boolean;
|
|
228
|
-
}>;
|
|
229
|
-
/**
|
|
230
|
-
* List files in a bucket.
|
|
231
|
-
*
|
|
232
|
-
* You can also filter the results by passing a filter object.
|
|
233
|
-
* The results are paginated.
|
|
234
|
-
*/
|
|
235
|
-
listFiles: (
|
|
236
|
-
params?: ListFilesRequest<TRouter['buckets'][K]>,
|
|
237
|
-
) => Promise<Prettify<ListFilesResponse<TRouter['buckets'][K]>>>;
|
|
238
|
-
};
|
|
300
|
+
[K in keyof TRouter['buckets']]: BucketClient<TRouter['buckets'][K]>;
|
|
239
301
|
};
|
|
240
302
|
|
|
241
303
|
export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
@@ -243,11 +305,8 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
243
305
|
accessKey?: string;
|
|
244
306
|
secretKey?: string;
|
|
245
307
|
/**
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* This is only needed for getting protected files in a development environment.
|
|
249
|
-
*
|
|
250
|
-
* @example http://localhost:3000/api/edgestore
|
|
308
|
+
* @deprecated No longer used. Protected files load directly from their file
|
|
309
|
+
* URL in every environment.
|
|
251
310
|
*/
|
|
252
311
|
baseUrl?: string;
|
|
253
312
|
}) {
|
|
@@ -264,11 +323,13 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
264
323
|
}
|
|
265
324
|
const client: EdgeStoreClient<TRouter>[string] = {
|
|
266
325
|
async upload(params) {
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
326
|
+
const {
|
|
327
|
+
content,
|
|
328
|
+
ctx = {},
|
|
329
|
+
input = {},
|
|
330
|
+
}: UploadImplementationParams = params;
|
|
270
331
|
|
|
271
|
-
|
|
332
|
+
let { blob, extension } = await (async () => {
|
|
272
333
|
if (isTextContent(content)) {
|
|
273
334
|
return {
|
|
274
335
|
blob: new Blob([content], { type: 'text/plain' }),
|
|
@@ -287,6 +348,16 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
287
348
|
}
|
|
288
349
|
})();
|
|
289
350
|
|
|
351
|
+
if (params.options?.transform) {
|
|
352
|
+
const transformed = await params.options.transform({
|
|
353
|
+
blob,
|
|
354
|
+
extension,
|
|
355
|
+
type: blob.type,
|
|
356
|
+
});
|
|
357
|
+
blob = transformed.blob;
|
|
358
|
+
extension = transformed.extension;
|
|
359
|
+
}
|
|
360
|
+
|
|
290
361
|
const path = buildPath({
|
|
291
362
|
bucket,
|
|
292
363
|
pathAttrs: {
|
|
@@ -310,6 +381,7 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
310
381
|
const requestUploadRes = await sdk.requestUpload({
|
|
311
382
|
bucketName,
|
|
312
383
|
bucketType: bucket._def.type,
|
|
384
|
+
autoSignedUrls: bucket._def.autoSignedUrls,
|
|
313
385
|
fileInfo: {
|
|
314
386
|
fileName: params.options?.manualFileName,
|
|
315
387
|
replaceTargetUrl: params.options?.replaceTargetUrl,
|
|
@@ -348,19 +420,18 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
348
420
|
...{
|
|
349
421
|
thumbnailUrl: requestUploadRes.thumbnailUrl,
|
|
350
422
|
},
|
|
423
|
+
...mapSignedUploadAccess(requestUploadRes),
|
|
351
424
|
size: blob.size,
|
|
352
425
|
metadata,
|
|
353
426
|
path: parsedPath,
|
|
354
427
|
pathOrder,
|
|
355
|
-
}
|
|
356
|
-
TRouter['buckets'][string]
|
|
357
|
-
>;
|
|
428
|
+
} as unknown as UploadFileRes<TRouter['buckets'][string]>;
|
|
358
429
|
},
|
|
359
430
|
|
|
360
431
|
async getFile(params) {
|
|
361
432
|
const res = await sdk.getFile(params);
|
|
362
433
|
return {
|
|
363
|
-
url:
|
|
434
|
+
url: res.url,
|
|
364
435
|
size: res.size,
|
|
365
436
|
uploadedAt: new Date(res.uploadedAt),
|
|
366
437
|
metadata: res.metadata,
|
|
@@ -386,7 +457,7 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
386
457
|
|
|
387
458
|
const files = res.data.map((file) => {
|
|
388
459
|
return {
|
|
389
|
-
url:
|
|
460
|
+
url: file.url,
|
|
390
461
|
thumbnailUrl: file.thumbnailUrl,
|
|
391
462
|
size: file.size,
|
|
392
463
|
uploadedAt: new Date(file.uploadedAt),
|
|
@@ -402,39 +473,68 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
402
473
|
pagination: res.pagination,
|
|
403
474
|
};
|
|
404
475
|
},
|
|
476
|
+
|
|
477
|
+
async getSignedUrl(params: { url: string; expiresIn?: number }) {
|
|
478
|
+
const [signedUrl] = await sdk.getSignedUrls({
|
|
479
|
+
bucketName,
|
|
480
|
+
urls: [params.url],
|
|
481
|
+
expiresIn: params.expiresIn,
|
|
482
|
+
});
|
|
483
|
+
if (!signedUrl) {
|
|
484
|
+
throw new Error('Missing signed URL response');
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
...signedUrl,
|
|
488
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
489
|
+
};
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
async getSignedUrls(params: {
|
|
493
|
+
urls: string[];
|
|
494
|
+
expiresIn?: number;
|
|
495
|
+
includeThumbnails?: boolean;
|
|
496
|
+
}) {
|
|
497
|
+
const signedUrls = await sdk.getSignedUrls({
|
|
498
|
+
bucketName,
|
|
499
|
+
urls: params.urls,
|
|
500
|
+
expiresIn: params.expiresIn,
|
|
501
|
+
includeThumbnails: params.includeThumbnails,
|
|
502
|
+
});
|
|
503
|
+
return signedUrls.map((signedUrl) => ({
|
|
504
|
+
...signedUrl,
|
|
505
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
506
|
+
})) as any;
|
|
507
|
+
},
|
|
405
508
|
};
|
|
406
509
|
return client;
|
|
407
510
|
},
|
|
408
511
|
});
|
|
409
512
|
}
|
|
410
513
|
|
|
411
|
-
/**
|
|
412
|
-
* Protected files need third-party cookies to work.
|
|
413
|
-
* Since third party cookies don't work on localhost,
|
|
414
|
-
* we need to proxy the file through the server.
|
|
415
|
-
*/
|
|
416
|
-
function getUrl(url: string, baseUrl?: string) {
|
|
417
|
-
if (isDev() && !url.includes('/_public/')) {
|
|
418
|
-
if (!baseUrl) {
|
|
419
|
-
throw new Error(
|
|
420
|
-
'Missing baseUrl. You need to pass the baseUrl to `initEdgeStoreClient` to get protected files in development.',
|
|
421
|
-
);
|
|
422
|
-
}
|
|
423
|
-
const proxyUrl = new URL(baseUrl);
|
|
424
|
-
proxyUrl.pathname = `${proxyUrl.pathname}/proxy-file`;
|
|
425
|
-
proxyUrl.search = new URLSearchParams({
|
|
426
|
-
url,
|
|
427
|
-
}).toString();
|
|
428
|
-
return proxyUrl.toString();
|
|
429
|
-
}
|
|
430
|
-
return url;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
514
|
async function getBlobFromUrl(url: string) {
|
|
434
515
|
const res = await fetch(url);
|
|
435
516
|
return await res.blob();
|
|
436
517
|
}
|
|
437
518
|
|
|
519
|
+
function mapSignedUploadAccess(res: {
|
|
520
|
+
accessSignedUrl?: string;
|
|
521
|
+
accessSignedThumbnailUrl?: string | null;
|
|
522
|
+
accessSignedUrlExpiresAt?: Date | string;
|
|
523
|
+
accessSignedUrlExpiresIn?: number;
|
|
524
|
+
}) {
|
|
525
|
+
if (!res.accessSignedUrl) {
|
|
526
|
+
return {};
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
signedUrl: res.accessSignedUrl,
|
|
530
|
+
expiresAt: res.accessSignedUrlExpiresAt
|
|
531
|
+
? new Date(res.accessSignedUrlExpiresAt)
|
|
532
|
+
: new Date(),
|
|
533
|
+
expiresIn: res.accessSignedUrlExpiresIn ?? 0,
|
|
534
|
+
signedThumbnailUrl: res.accessSignedThumbnailUrl ?? null,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
438
538
|
export type InferClientResponse<TRouter extends AnyRouter> = {
|
|
439
539
|
[TBucketName in keyof TRouter['buckets']]: {
|
|
440
540
|
[TClienFn in keyof EdgeStoreClient<TRouter>[TBucketName]]: Simplify<
|
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,6 +29,11 @@ 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
38
|
| 'eq'
|
|
30
39
|
| 'neq'
|
|
@@ -77,6 +86,8 @@ async function makeRequest<TOutput>(params: {
|
|
|
77
86
|
Authorization: `Basic ${Buffer.from(`${accessKey}:${secretKey}`).toString(
|
|
78
87
|
'base64',
|
|
79
88
|
)}`,
|
|
89
|
+
'x-edgestore-package-name': EDGE_STORE_PACKAGE_NAME,
|
|
90
|
+
'x-edgestore-package-version': EDGE_STORE_PACKAGE_VERSION,
|
|
80
91
|
},
|
|
81
92
|
});
|
|
82
93
|
if (!res.ok) {
|
|
@@ -161,12 +172,14 @@ export const edgeStoreRawSdk = {
|
|
|
161
172
|
bucketType,
|
|
162
173
|
fileInfo,
|
|
163
174
|
multipart,
|
|
175
|
+
autoSignedUrls,
|
|
164
176
|
}: {
|
|
165
177
|
accessKey: string;
|
|
166
178
|
secretKey: string;
|
|
167
179
|
bucketName: string;
|
|
168
180
|
bucketType: string;
|
|
169
181
|
fileInfo: FileInfoForUpload;
|
|
182
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
170
183
|
multipart?: {
|
|
171
184
|
parts: number[];
|
|
172
185
|
};
|
|
@@ -184,6 +197,10 @@ export const edgeStoreRawSdk = {
|
|
|
184
197
|
url: string;
|
|
185
198
|
path: string;
|
|
186
199
|
thumbnailUrl: string | null;
|
|
200
|
+
accessSignedUrl?: string;
|
|
201
|
+
accessSignedThumbnailUrl?: string | null;
|
|
202
|
+
accessSignedUrlExpiresAt?: string;
|
|
203
|
+
accessSignedUrlExpiresIn?: number;
|
|
187
204
|
}>({
|
|
188
205
|
path: '/request-upload',
|
|
189
206
|
accessKey,
|
|
@@ -201,6 +218,7 @@ export const edgeStoreRawSdk = {
|
|
|
201
218
|
fileName: fileInfo.fileName,
|
|
202
219
|
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
203
220
|
isTemporary: fileInfo.temporary,
|
|
221
|
+
autoSignedUrls,
|
|
204
222
|
},
|
|
205
223
|
});
|
|
206
224
|
return {
|
|
@@ -209,9 +227,51 @@ export const edgeStoreRawSdk = {
|
|
|
209
227
|
accessUrl: res.url,
|
|
210
228
|
path: res.path,
|
|
211
229
|
thumbnailUrl: res.thumbnailUrl,
|
|
230
|
+
accessSignedUrl: res.accessSignedUrl,
|
|
231
|
+
accessSignedThumbnailUrl: res.accessSignedThumbnailUrl,
|
|
232
|
+
accessSignedUrlExpiresAt: res.accessSignedUrlExpiresAt,
|
|
233
|
+
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn,
|
|
212
234
|
};
|
|
213
235
|
},
|
|
214
236
|
|
|
237
|
+
async getSignedUrls({
|
|
238
|
+
accessKey,
|
|
239
|
+
secretKey,
|
|
240
|
+
bucketName,
|
|
241
|
+
urls,
|
|
242
|
+
expiresIn,
|
|
243
|
+
includeThumbnails,
|
|
244
|
+
}: {
|
|
245
|
+
accessKey: string;
|
|
246
|
+
secretKey: string;
|
|
247
|
+
bucketName: string;
|
|
248
|
+
urls: string[];
|
|
249
|
+
expiresIn?: number;
|
|
250
|
+
includeThumbnails?: boolean;
|
|
251
|
+
}) {
|
|
252
|
+
const res = await makeRequest<{
|
|
253
|
+
signedUrls: {
|
|
254
|
+
url: string;
|
|
255
|
+
signedUrl: string;
|
|
256
|
+
expiresAt: string;
|
|
257
|
+
expiresIn: number;
|
|
258
|
+
thumbnailUrl?: string | null;
|
|
259
|
+
signedThumbnailUrl?: string | null;
|
|
260
|
+
}[];
|
|
261
|
+
}>({
|
|
262
|
+
path: '/get-signed-urls',
|
|
263
|
+
accessKey,
|
|
264
|
+
secretKey,
|
|
265
|
+
body: {
|
|
266
|
+
bucketName,
|
|
267
|
+
urls,
|
|
268
|
+
expiresIn,
|
|
269
|
+
includeThumbnails,
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
return res.signedUrls;
|
|
273
|
+
},
|
|
274
|
+
|
|
215
275
|
async requestUploadParts({
|
|
216
276
|
accessKey,
|
|
217
277
|
secretKey,
|
|
@@ -389,10 +449,12 @@ export function initEdgeStoreSdk(params: {
|
|
|
389
449
|
bucketType,
|
|
390
450
|
fileInfo,
|
|
391
451
|
multipart,
|
|
452
|
+
autoSignedUrls,
|
|
392
453
|
}: {
|
|
393
454
|
bucketName: string;
|
|
394
455
|
bucketType: string;
|
|
395
456
|
fileInfo: FileInfoForUpload;
|
|
457
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
396
458
|
multipart?: {
|
|
397
459
|
parts: number[];
|
|
398
460
|
};
|
|
@@ -404,6 +466,19 @@ export function initEdgeStoreSdk(params: {
|
|
|
404
466
|
bucketType,
|
|
405
467
|
fileInfo,
|
|
406
468
|
multipart,
|
|
469
|
+
autoSignedUrls,
|
|
470
|
+
});
|
|
471
|
+
},
|
|
472
|
+
async getSignedUrls(params: {
|
|
473
|
+
bucketName: string;
|
|
474
|
+
urls: string[];
|
|
475
|
+
expiresIn?: number;
|
|
476
|
+
includeThumbnails?: boolean;
|
|
477
|
+
}) {
|
|
478
|
+
return await edgeStoreRawSdk.getSignedUrls({
|
|
479
|
+
accessKey,
|
|
480
|
+
secretKey,
|
|
481
|
+
...params,
|
|
407
482
|
});
|
|
408
483
|
},
|
|
409
484
|
async requestUploadParts({
|
|
@@ -7,8 +7,8 @@ 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
14
|
import { v4 as uuidv4 } from 'uuid';
|
|
@@ -175,12 +175,11 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
175
175
|
const pathPrefix = `${esBucketName}${
|
|
176
176
|
fileInfo.isPublic ? '/_public' : ''
|
|
177
177
|
}`;
|
|
178
|
-
const nameId = uuidv4();
|
|
179
178
|
const extension = fileInfo.extension
|
|
180
179
|
? `.${fileInfo.extension.replace('.', '')}`
|
|
181
180
|
: '';
|
|
182
181
|
const defaultResolvedFileName =
|
|
183
|
-
fileInfo.fileName ?? `${
|
|
182
|
+
fileInfo.fileName ?? `${uuidv4()}${extension}`;
|
|
184
183
|
const defaultFilePathFromMetadata = fileInfo.path.reduce((acc, item) => {
|
|
185
184
|
return `${acc}/${item.value}`;
|
|
186
185
|
}, '');
|
|
@@ -194,6 +193,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
194
193
|
defaultAccessPath: accessPath,
|
|
195
194
|
});
|
|
196
195
|
}
|
|
196
|
+
accessPath = accessPath.replace(/^\/+/, '');
|
|
197
197
|
|
|
198
198
|
const command = new PutObjectCommand({
|
|
199
199
|
Bucket: bucketName,
|
|
@@ -204,7 +204,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
204
204
|
expiresIn: 60 * 60, // 1 hour
|
|
205
205
|
});
|
|
206
206
|
|
|
207
|
-
const finalAccessUrl = `${baseUrl}/${accessPath
|
|
207
|
+
const finalAccessUrl = `${baseUrl}/${accessPath}`;
|
|
208
208
|
return {
|
|
209
209
|
uploadUrl: signedUrl,
|
|
210
210
|
accessUrl: finalAccessUrl,
|
|
@@ -54,6 +54,42 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
54
54
|
const containerClient = blobServiceClient.getContainerClient(
|
|
55
55
|
containerName ?? '',
|
|
56
56
|
);
|
|
57
|
+
function getBlobNameFromUrl(url: string) {
|
|
58
|
+
try {
|
|
59
|
+
const blobUrl = new URL(url);
|
|
60
|
+
const containerUrl = new URL(containerClient.url);
|
|
61
|
+
const containerPath = containerUrl.pathname.replace(/\/$/, '');
|
|
62
|
+
|
|
63
|
+
if (blobUrl.origin !== containerUrl.origin) {
|
|
64
|
+
return url;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!blobUrl.pathname.startsWith(`${containerPath}/`)) {
|
|
68
|
+
return url;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return decodeURIComponent(
|
|
72
|
+
blobUrl.pathname.slice(containerPath.length + 1),
|
|
73
|
+
);
|
|
74
|
+
} catch {
|
|
75
|
+
return url;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getBlobName(params: Parameters<Provider['requestUpload']>[0]) {
|
|
80
|
+
const { bucketName: esBucketName, fileInfo } = params;
|
|
81
|
+
const extension = fileInfo.extension
|
|
82
|
+
? `.${fileInfo.extension.replace('.', '')}`
|
|
83
|
+
: '';
|
|
84
|
+
const fileName = fileInfo.fileName ?? `${uuidv4()}${extension}`;
|
|
85
|
+
|
|
86
|
+
return [
|
|
87
|
+
esBucketName,
|
|
88
|
+
...(fileInfo.isPublic ? ['_public'] : []),
|
|
89
|
+
...fileInfo.path.map((item) => item.value),
|
|
90
|
+
fileName,
|
|
91
|
+
].join('/');
|
|
92
|
+
}
|
|
57
93
|
|
|
58
94
|
return {
|
|
59
95
|
name: 'azure',
|
|
@@ -64,7 +100,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
64
100
|
return baseUrl;
|
|
65
101
|
},
|
|
66
102
|
async getFile({ url }) {
|
|
67
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
103
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
68
104
|
|
|
69
105
|
const { contentLength, lastModified } = await blobClient.getProperties();
|
|
70
106
|
|
|
@@ -76,14 +112,8 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
76
112
|
uploadedAt: lastModified ?? new Date(),
|
|
77
113
|
};
|
|
78
114
|
},
|
|
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);
|
|
115
|
+
async requestUpload(params) {
|
|
116
|
+
const blobClient = containerClient.getBlobClient(getBlobName(params));
|
|
87
117
|
|
|
88
118
|
const url = blobClient.url;
|
|
89
119
|
|
|
@@ -102,7 +132,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
102
132
|
throw new Error('Not implemented');
|
|
103
133
|
},
|
|
104
134
|
async deleteFile({ url }) {
|
|
105
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
135
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
106
136
|
await blobClient.delete();
|
|
107
137
|
return {
|
|
108
138
|
success: true,
|