@edgestore/server 1.0.0-rc.6 → 1.0.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/README.md +2 -2
- package/agent-docs/README.md +1 -1
- package/agent-docs/astro.md +4 -4
- package/agent-docs/backend-client.md +5 -2
- package/agent-docs/configuration.md +3 -3
- package/agent-docs/express.md +5 -5
- package/agent-docs/fastify.md +5 -5
- package/agent-docs/hono.md +5 -5
- package/agent-docs/next.md +4 -4
- package/agent-docs/provider-azure-blob.md +15 -15
- package/agent-docs/provider-custom.md +13 -1
- package/agent-docs/provider-edgestore.md +3 -3
- package/agent-docs/provider-s3.md +120 -21
- package/agent-docs/remix.md +5 -5
- package/agent-docs/tanstack-start.md +5 -5
- package/agent-docs/troubleshooting.md +4 -4
- package/dist/adapters/dispatcher.d.ts.map +1 -1
- package/dist/adapters/dispatcher.js +10 -1
- package/dist/adapters/shared.d.ts +13 -1
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +96 -4
- package/dist/libs/env.d.ts.map +1 -1
- package/dist/libs/env.js +3 -0
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -1
- package/dist/providers/azure-blob/index.d.ts +2 -2
- package/dist/providers/azure-blob/index.js +1 -1
- package/dist/providers/edgestore/index.d.ts +35 -2
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +16 -4
- package/dist/providers/s3/index.js +1 -1
- package/dist/providers/s3/options.d.ts +3 -3
- package/dist/providers/storage/multipartSession.js +2 -2
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/adapters/dispatcher.ts +14 -0
- package/src/adapters/shared.ts +100 -2
- package/src/libs/env.ts +9 -0
- package/src/libs/errors/EdgeStoreCredentialsError.ts +1 -1
- package/src/providers/azure-blob/index.ts +3 -3
- package/src/providers/edgestore/index.ts +13 -6
- package/src/providers/s3/index.ts +1 -1
- package/src/providers/s3/options.ts +3 -3
- package/src/providers/storage/multipartSession.ts +3 -3
- package/src/test-utils/edgestoreSmoke.ts +3 -5
- package/src/version.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type AnyContext, type AnyEdgeStoreProvider, type EdgeStoreRouter, type SharedConfirmUploadsRes, type SharedDeleteFilesRes, type SharedInitRes, type SharedRequestUploadPartsRes, type SharedRequestUploadRes } from '@edgestore/shared';
|
|
1
|
+
import { type AnyContext, type AnyEdgeStoreProvider, type EdgeStoreRouter, type SharedConfirmUploadsRes, type SharedDeleteFilesRes, type SharedInitRes, type SharedRequestUploadPartsRes, type SharedRequestUploadRes, type SharedUploadStatusRes } from '@edgestore/shared';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import type { LoggerLike } from '../libs/logger.js';
|
|
4
4
|
export type HandlerRouter<TCtx extends AnyContext> = EdgeStoreRouter<TCtx> & {
|
|
@@ -87,6 +87,18 @@ export declare function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
87
87
|
body: RequestUploadBody;
|
|
88
88
|
logger: LoggerLike;
|
|
89
89
|
}): Promise<SharedRequestUploadRes>;
|
|
90
|
+
export declare const uploadStatusBodySchema: z.ZodObject<{
|
|
91
|
+
statusToken: z.ZodString;
|
|
92
|
+
}, z.core.$strip>;
|
|
93
|
+
export type UploadStatusBody = z.infer<typeof uploadStatusBodySchema>;
|
|
94
|
+
/** Reports processing state for an upload authorized by its status token. */
|
|
95
|
+
export declare function getUploadStatus<TCtx extends AnyContext>(params: {
|
|
96
|
+
provider: AnyEdgeStoreProvider;
|
|
97
|
+
router: EdgeStoreRouter<TCtx>;
|
|
98
|
+
ctxToken: string | undefined;
|
|
99
|
+
body: UploadStatusBody;
|
|
100
|
+
logger: LoggerLike;
|
|
101
|
+
}): Promise<SharedUploadStatusRes>;
|
|
90
102
|
export declare const requestUploadPartsBodySchema: z.ZodObject<{
|
|
91
103
|
bucketName: z.ZodString;
|
|
92
104
|
uploadId: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/adapters/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,oBAAoB,EACzB,KAAK,eAAe,EAEpB,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,2BAA2B,EAChC,KAAK,sBAAsB,
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/adapters/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,oBAAoB,EACzB,KAAK,eAAe,EAEpB,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,2BAA2B,EAChC,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC3B,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAQxB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAKjD,MAAM,MAAM,aAAa,CAAC,IAAI,SAAS,UAAU,IAAI,eAAe,CAAC,IAAI,CAAC,GAAG;IAC3E,QAAQ,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,oBAAoB,CAAA;KAAE,CAAC;CAC5D,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;IACrC;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;OAEG;IAC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AM,EAAE,gBAAgB,CAAC,IAAI,EAAE,2BAA2B,CAAC,iBAW5D;AAED,wBAAsB,oBAAoB,CAAC,IAAI,SAAS,UAAU,EAChE,MAAM,EAAE,gBAAgB,CAAC,IAAI,EAAE,wBAAwB,CAAC,iBAWzD;AAED,eAAO,MAAM,wBAAwB;;;iBAGnC,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAE1E,wBAAsB,cAAc,CAAC,IAAI,SAAS,UAAU,EAAE,MAAM,EAAE;IACpE,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,MAAM,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,IAAI,EAAE,kBAAkB,CAAC;IACzB,MAAM,EAAE,UAAU,CAAC;CACpB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA8BnC;AAED,eAAO,MAAM,qBAAqB;;;iBAGhC,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,wBAAsB,WAAW,CAAC,IAAI,SAAS,UAAU,EAAE,MAAM,EAAE;IACjE,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,MAAM,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,UAAU,CAAC;CACpB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA8EhC"}
|
package/dist/adapters/shared.js
CHANGED
|
@@ -139,13 +139,74 @@ async function requestUpload(params) {
|
|
|
139
139
|
autoSignedUrls
|
|
140
140
|
});
|
|
141
141
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
142
|
+
const statusToken = provider.uploads.getStatus && requestUploadRes.id ? await encryptStatusToken({
|
|
143
|
+
id: requestUploadRes.id,
|
|
144
|
+
bucketName
|
|
145
|
+
}) : undefined;
|
|
142
146
|
logger.debug('Finished [requestUpload]');
|
|
143
147
|
return {
|
|
144
148
|
...requestUploadRes,
|
|
145
149
|
size: fileInfo.size,
|
|
146
150
|
path: parsedPath,
|
|
147
151
|
pathOrder,
|
|
148
|
-
metadata
|
|
152
|
+
metadata,
|
|
153
|
+
statusToken
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const uploadStatusBodySchema = z.object({
|
|
157
|
+
statusToken: nonEmptyStringSchema
|
|
158
|
+
});
|
|
159
|
+
/** Reports processing state for an upload authorized by its status token. */ async function getUploadStatus(params) {
|
|
160
|
+
const { provider, router, ctxToken, logger, body } = params;
|
|
161
|
+
await getContext(ctxToken);
|
|
162
|
+
const { id, bucketName } = await decryptStatusToken(body.statusToken);
|
|
163
|
+
logger.debug('Running [getUploadStatus]', {
|
|
164
|
+
bucketName,
|
|
165
|
+
id
|
|
166
|
+
});
|
|
167
|
+
const bucket = getBucket(router, bucketName);
|
|
168
|
+
if (!provider.uploads.getStatus) {
|
|
169
|
+
throw new EdgeStoreError({
|
|
170
|
+
message: `Provider ${provider.name} does not report upload status.`,
|
|
171
|
+
code: 'BAD_REQUEST'
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
const result = await provider.uploads.getStatus({
|
|
175
|
+
bucketName,
|
|
176
|
+
id
|
|
177
|
+
});
|
|
178
|
+
logger.debug('Finished [getUploadStatus]', {
|
|
179
|
+
status: result.status
|
|
180
|
+
});
|
|
181
|
+
if (result.status !== 'completed') return {
|
|
182
|
+
status: result.status
|
|
183
|
+
};
|
|
184
|
+
const { url, key, thumbnailUrl, sizeBytes } = result.file;
|
|
185
|
+
// Processing may add a thumbnail that the upload response could not sign.
|
|
186
|
+
const { autoSignedUrls } = bucket._def;
|
|
187
|
+
const [signed] = autoSignedUrls && provider.files.getSignedUrls ? await provider.files.getSignedUrls({
|
|
188
|
+
bucketName,
|
|
189
|
+
files: [
|
|
190
|
+
await referenceFromUrl(provider, url)
|
|
191
|
+
],
|
|
192
|
+
...autoSignedUrls
|
|
193
|
+
}) : [];
|
|
194
|
+
return {
|
|
195
|
+
status: 'completed',
|
|
196
|
+
file: {
|
|
197
|
+
url,
|
|
198
|
+
key,
|
|
199
|
+
thumbnailUrl: thumbnailUrl ?? null,
|
|
200
|
+
size: sizeBytes
|
|
201
|
+
},
|
|
202
|
+
...signed && {
|
|
203
|
+
signedReadUrl: {
|
|
204
|
+
signedUrl: signed.signedUrl,
|
|
205
|
+
signedThumbnailUrl: signed.signedThumbnailUrl ?? null,
|
|
206
|
+
expiresAt: signed.expiresAt,
|
|
207
|
+
expiresIn: signed.expiresIn
|
|
208
|
+
}
|
|
209
|
+
}
|
|
149
210
|
};
|
|
150
211
|
}
|
|
151
212
|
const multipartSessionBodySchema = z.object({
|
|
@@ -351,6 +412,37 @@ async function encryptJWT(ctx) {
|
|
|
351
412
|
enc: 'A256GCM'
|
|
352
413
|
}).setIssuedAt().setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE).setJti(crypto.randomUUID()).encrypt(await getEncryptionKey());
|
|
353
414
|
}
|
|
415
|
+
/**
|
|
416
|
+
* Status tokens bind one upload to the browser that requested it. They never
|
|
417
|
+
* reach application code, so they carry no expiry: uploads can take arbitrarily
|
|
418
|
+
* long, and the client bounds how long it waits for processing.
|
|
419
|
+
*/ async function encryptStatusToken(upload) {
|
|
420
|
+
return await new EncryptJWT({
|
|
421
|
+
upload
|
|
422
|
+
}).setProtectedHeader({
|
|
423
|
+
alg: 'dir',
|
|
424
|
+
enc: 'A256GCM'
|
|
425
|
+
}).encrypt(await getEncryptionKey());
|
|
426
|
+
}
|
|
427
|
+
const statusTokenPayloadSchema = z.object({
|
|
428
|
+
upload: z.object({
|
|
429
|
+
id: nonEmptyStringSchema,
|
|
430
|
+
bucketName: nonEmptyStringSchema
|
|
431
|
+
})
|
|
432
|
+
});
|
|
433
|
+
async function decryptStatusToken(token) {
|
|
434
|
+
const invalid = (cause)=>new EdgeStoreError({
|
|
435
|
+
message: 'Invalid upload status token',
|
|
436
|
+
code: 'BAD_REQUEST',
|
|
437
|
+
cause
|
|
438
|
+
});
|
|
439
|
+
const { payload } = await jwtDecrypt(token, await getEncryptionKey()).catch((error)=>{
|
|
440
|
+
throw invalid(error instanceof Error ? error : undefined);
|
|
441
|
+
});
|
|
442
|
+
const result = statusTokenPayloadSchema.safeParse(payload);
|
|
443
|
+
if (!result.success) throw invalid(result.error);
|
|
444
|
+
return result.data.upload;
|
|
445
|
+
}
|
|
354
446
|
const contextPayloadSchema = z.object({
|
|
355
447
|
ctx: z.record(z.string(), z.string())
|
|
356
448
|
});
|
|
@@ -382,10 +474,10 @@ const contextPayloadSchema = z.object({
|
|
|
382
474
|
return result.data.ctx;
|
|
383
475
|
}
|
|
384
476
|
/** Derives the context-cookie encryption key from the configured secret. */ async function getEncryptionKey() {
|
|
385
|
-
const secret = getEnv('
|
|
477
|
+
const secret = getEnv('EDGESTORE_JWT_SECRET') ?? getEnv('EDGESTORE_SECRET_KEY');
|
|
386
478
|
if (!secret) {
|
|
387
479
|
throw new EdgeStoreError({
|
|
388
|
-
message: '
|
|
480
|
+
message: 'EDGESTORE_JWT_SECRET or EDGESTORE_SECRET_KEY is not defined',
|
|
389
481
|
code: 'SERVER_ERROR'
|
|
390
482
|
});
|
|
391
483
|
}
|
|
@@ -402,4 +494,4 @@ const contextPayloadSchema = z.object({
|
|
|
402
494
|
return new Uint8Array(bits);
|
|
403
495
|
}
|
|
404
496
|
|
|
405
|
-
export { abortMultipartUpload, abortMultipartUploadBodySchema, completeMultipartUpload, completeMultipartUploadBodySchema, confirmUploads, confirmUploadsBodySchema, deleteFiles, deleteFilesBodySchema, getCookieConfig, init, requestUpload, requestUploadBodySchema, requestUploadParts, requestUploadPartsBodySchema };
|
|
497
|
+
export { abortMultipartUpload, abortMultipartUploadBodySchema, completeMultipartUpload, completeMultipartUploadBodySchema, confirmUploads, confirmUploadsBodySchema, deleteFiles, deleteFilesBodySchema, getCookieConfig, getUploadStatus, init, requestUpload, requestUploadBodySchema, requestUploadParts, requestUploadPartsBodySchema, uploadStatusBodySchema };
|
package/dist/libs/env.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/libs/env.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOtD;
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/libs/env.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOtD;AAWD,wBAAgB,KAAK,IAAI,OAAO,CAM/B"}
|
package/dist/libs/env.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
function getEnv(key) {
|
|
2
|
+
return readEnv(key) ?? (key.startsWith('EDGESTORE_') ? readEnv(key.replace('EDGESTORE_', 'EDGE_STORE_')) : undefined);
|
|
3
|
+
}
|
|
4
|
+
function readEnv(key) {
|
|
2
5
|
if (typeof process !== 'undefined' && process.env) {
|
|
3
6
|
// @ts-expect-error - In Vite/Astro, env variables are available on import.meta.
|
|
4
7
|
return process.env[key] ?? import.meta.env?.[key];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = `Missing
|
|
1
|
+
const DEFAULT_MESSAGE = `Missing EDGESTORE_ACCESS_KEY or EDGESTORE_SECRET_KEY.
|
|
2
2
|
This can happen if you are trying to import something related to the backend of EdgeStore in a client component.`;
|
|
3
3
|
class EdgeStoreCredentialsError extends Error {
|
|
4
4
|
constructor(message = DEFAULT_MESSAGE){
|
|
@@ -28,13 +28,13 @@ export type AzureBlobProviderOptions = {
|
|
|
28
28
|
endpoint?: string;
|
|
29
29
|
/**
|
|
30
30
|
* Base URL for file URLs, such as a CDN in front of the container. Defaults
|
|
31
|
-
* to `<endpoint>/<container>`. Can also be set via `
|
|
31
|
+
* to `<endpoint>/<container>`. Can also be set via `EDGESTORE_BASE_URL`.
|
|
32
32
|
* Signed URLs always use the endpoint.
|
|
33
33
|
*/
|
|
34
34
|
baseUrl?: string;
|
|
35
35
|
/**
|
|
36
36
|
* Secret used to sign multipart upload sessions. Defaults to
|
|
37
|
-
* `
|
|
37
|
+
* `EDGESTORE_JWT_SECRET` or `EDGESTORE_SECRET_KEY`.
|
|
38
38
|
*/
|
|
39
39
|
jwtSecret?: string;
|
|
40
40
|
/**
|
|
@@ -15,7 +15,7 @@ const expiration = (value)=>signedUrlLifetime('Azure Blob', value);
|
|
|
15
15
|
function azureBlob(options = {}) {
|
|
16
16
|
const { storageAccountName = getEnv('ES_AZURE_ACCOUNT_NAME'), storageAccountKey = getEnv('ES_AZURE_ACCOUNT_KEY'), containerName = getEnv('ES_AZURE_CONTAINER_NAME') } = options;
|
|
17
17
|
const endpoint = (options.endpoint ?? getEnv('ES_AZURE_ENDPOINT') ?? `https://${storageAccountName}.blob.core.windows.net`).replace(/\/+$/, '');
|
|
18
|
-
const baseUrl = options.baseUrl ?? getEnv('
|
|
18
|
+
const baseUrl = options.baseUrl ?? getEnv('EDGESTORE_BASE_URL') ?? `${endpoint}/${containerName}`;
|
|
19
19
|
const keys = createObjectKeys('Azure Blob', baseUrl);
|
|
20
20
|
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|
|
21
21
|
const readExpiresIn = expiration(options.signedUrlExpiresIn ?? 3600);
|
|
@@ -9,14 +9,14 @@ type EdgeStoreProjectProviderOptions = {
|
|
|
9
9
|
* Access key for your EdgeStore project.
|
|
10
10
|
* Can be found in the EdgeStore dashboard.
|
|
11
11
|
*
|
|
12
|
-
* This can be omitted if the `
|
|
12
|
+
* This can be omitted if the `EDGESTORE_ACCESS_KEY` environment variable is set.
|
|
13
13
|
*/
|
|
14
14
|
accessKey?: string;
|
|
15
15
|
/**
|
|
16
16
|
* Secret key for your EdgeStore project.
|
|
17
17
|
* Can be found in the EdgeStore dashboard.
|
|
18
18
|
*
|
|
19
|
-
* This can be omitted if the `
|
|
19
|
+
* This can be omitted if the `EDGESTORE_SECRET_KEY` environment variable is set.
|
|
20
20
|
*/
|
|
21
21
|
secretKey?: string;
|
|
22
22
|
token?: never;
|
|
@@ -79,6 +79,39 @@ export declare function edgestore(options?: EdgeStoreProviderOptions): {
|
|
|
79
79
|
readonly complete: ({ uploadId, parts }: import("@edgestore/shared").CompleteMultipartUploadParams) => Promise<void>;
|
|
80
80
|
readonly abort: ({ uploadId }: import("@edgestore/shared").MultipartUploadSession) => Promise<void>;
|
|
81
81
|
};
|
|
82
|
+
readonly getStatus: ({ id }: {
|
|
83
|
+
bucketName: string;
|
|
84
|
+
id: string;
|
|
85
|
+
}) => Promise<{
|
|
86
|
+
status: "completed";
|
|
87
|
+
file: {
|
|
88
|
+
id: string;
|
|
89
|
+
url: string;
|
|
90
|
+
key: string;
|
|
91
|
+
thumbnailUrl: string | null;
|
|
92
|
+
thumbnailKey: string | null;
|
|
93
|
+
bucketId: string;
|
|
94
|
+
bucketName: string;
|
|
95
|
+
projectId: string;
|
|
96
|
+
accountId: string;
|
|
97
|
+
name: string;
|
|
98
|
+
path: {
|
|
99
|
+
[key: string]: string;
|
|
100
|
+
};
|
|
101
|
+
metadata: {
|
|
102
|
+
[key: string]: string;
|
|
103
|
+
};
|
|
104
|
+
sizeBytes: number;
|
|
105
|
+
mimeType: string | null;
|
|
106
|
+
state: "requested" | "uploaded" | "deleted" | "replace_requested";
|
|
107
|
+
temporary: boolean;
|
|
108
|
+
uploadedAt: string;
|
|
109
|
+
updatedAt: string;
|
|
110
|
+
};
|
|
111
|
+
} | {
|
|
112
|
+
status: "processing" | "canceled";
|
|
113
|
+
file?: undefined;
|
|
114
|
+
}>;
|
|
82
115
|
readonly upload: ({ bucketName, bucketType, fileInfo, autoSignedUrls, source, signal, onProgress, }: import("@edgestore/shared").BackendUploadParams) => Promise<{
|
|
83
116
|
file: ProviderFile;
|
|
84
117
|
signedReadUrl: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/edgestore/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAqBxB,KAAK,8BAA8B,GAAG;IACpC,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,+BAA+B,GAAG;IACrC;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,OAAO,CAAC,EAAE,KAAK,CAAC;CACjB,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,KAAK,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG,8BAA8B,GACnE,CAAC,+BAA+B,GAAG,8BAA8B,CAAC,CAAC;AAErE,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0GAgElD,OAAO,CAAC,gBAAgB,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/edgestore/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,K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61
|
clientInit: {
|
|
62
62
|
urls: delivery && !overrideBaseUrl ? delivery.initUrls : [
|
|
@@ -126,6 +126,17 @@ function edgestore(options) {
|
|
|
126
126
|
});
|
|
127
127
|
}
|
|
128
128
|
},
|
|
129
|
+
getStatus: async ({ id })=>{
|
|
130
|
+
const result = await runtime.uploads.get({
|
|
131
|
+
uploadId: id
|
|
132
|
+
});
|
|
133
|
+
return 'file' in result ? {
|
|
134
|
+
status: 'completed',
|
|
135
|
+
file: result.file
|
|
136
|
+
} : {
|
|
137
|
+
status: result.upload.status
|
|
138
|
+
};
|
|
139
|
+
},
|
|
129
140
|
upload: async ({ bucketName, bucketType, fileInfo, autoSignedUrls, source, signal, onProgress })=>{
|
|
130
141
|
const result = await runtime.uploads.upload({
|
|
131
142
|
bucket: bucketName,
|
|
@@ -244,8 +255,8 @@ function resolveProviderAuthentication(options) {
|
|
|
244
255
|
if (project !== undefined) {
|
|
245
256
|
throw new TypeError('EdgeStore provider option `project` requires a Bearer token.');
|
|
246
257
|
}
|
|
247
|
-
const accessKey = configuredAccessKey ?? getEnv('
|
|
248
|
-
const secretKey = configuredSecretKey ?? getEnv('
|
|
258
|
+
const accessKey = configuredAccessKey ?? getEnv('EDGESTORE_ACCESS_KEY');
|
|
259
|
+
const secretKey = configuredSecretKey ?? getEnv('EDGESTORE_SECRET_KEY');
|
|
249
260
|
if (typeof accessKey !== 'string' || !accessKey.trim() || typeof secretKey !== 'string' || !secretKey.trim()) {
|
|
250
261
|
throw new EdgeStoreCredentialsError();
|
|
251
262
|
}
|
|
@@ -271,7 +282,7 @@ function mapMutationResult(result) {
|
|
|
271
282
|
};
|
|
272
283
|
}
|
|
273
284
|
function getApiUrl() {
|
|
274
|
-
const configured = getEnv('
|
|
285
|
+
const configured = getEnv('EDGESTORE_API_ENDPOINT');
|
|
275
286
|
if (!configured) return undefined;
|
|
276
287
|
const base = configured.replace(/\/+$/, '');
|
|
277
288
|
return base.endsWith('/v2') ? base : `${base}/v2`;
|
|
@@ -303,6 +314,7 @@ function mapRawUploadRequest(bucketType, fileInfo, signedReadUrl) {
|
|
|
303
314
|
}
|
|
304
315
|
function mapUploadResponse(res, multipartConfig) {
|
|
305
316
|
const access = {
|
|
317
|
+
id: res.file.id,
|
|
306
318
|
key: res.file.key,
|
|
307
319
|
url: res.file.url,
|
|
308
320
|
thumbnailUrl: res.file.thumbnailUrl,
|
|
@@ -27,7 +27,7 @@ function s3(options = {}) {
|
|
|
27
27
|
// Presigning without a body must not sign an empty-body optional checksum.
|
|
28
28
|
requestChecksumCalculation: 'WHEN_REQUIRED'
|
|
29
29
|
});
|
|
30
|
-
const baseUrl = options.baseUrl ?? getEnv('
|
|
30
|
+
const baseUrl = options.baseUrl ?? getEnv('EDGESTORE_BASE_URL') ?? (endpoint ? `${endpoint.replace(/\/+$/, '')}/${bucketName}` : region ? `https://${bucketName}.s3.${region}.amazonaws.com` : async ()=>`https://${bucketName}.s3.${await client.config.region()}.amazonaws.com`);
|
|
31
31
|
const keys = async ()=>createObjectKeys('S3', typeof baseUrl === 'function' ? await baseUrl() : baseUrl);
|
|
32
32
|
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|
|
33
33
|
const readExpiresIn = expiration(options.signedUrlExpiresIn ?? 3600);
|
|
@@ -41,15 +41,15 @@ export type S3ProviderOptions = {
|
|
|
41
41
|
* Base URL to use for accessing files.
|
|
42
42
|
* Only needed if you are using a custom domain or cloudfront.
|
|
43
43
|
*
|
|
44
|
-
* It can also be set via the `
|
|
44
|
+
* It can also be set via the `EDGESTORE_BASE_URL` environment variable.
|
|
45
45
|
*/
|
|
46
46
|
baseUrl?: string;
|
|
47
47
|
/**
|
|
48
48
|
* Secret used to sign multipart upload sessions. The adapter context cookie
|
|
49
|
-
* still requires
|
|
49
|
+
* still requires EDGESTORE_JWT_SECRET (or EDGESTORE_SECRET_KEY).
|
|
50
50
|
* Can be generated with `openssl rand -base64 32`.
|
|
51
51
|
*
|
|
52
|
-
* It can also be set via the `
|
|
52
|
+
* It can also be set via the `EDGESTORE_JWT_SECRET` environment variable.
|
|
53
53
|
*/
|
|
54
54
|
jwtSecret?: string;
|
|
55
55
|
/**
|
|
@@ -20,8 +20,8 @@ const sessionSchema = z.object({
|
|
|
20
20
|
throw new RangeError(`${providerName} multipart session lifetime must be a positive integer number of seconds.`);
|
|
21
21
|
}
|
|
22
22
|
function signingKey() {
|
|
23
|
-
const value = secret ?? getEnv('
|
|
24
|
-
if (!value) throw new Error(`${providerName} multipart uploads require jwtSecret or
|
|
23
|
+
const value = secret ?? getEnv('EDGESTORE_JWT_SECRET') ?? getEnv('EDGESTORE_SECRET_KEY');
|
|
24
|
+
if (!value) throw new Error(`${providerName} multipart uploads require jwtSecret or EDGESTORE_JWT_SECRET.`);
|
|
25
25
|
return new TextEncoder().encode(value);
|
|
26
26
|
}
|
|
27
27
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edgestoreSmoke.d.ts","sourceRoot":"","sources":["../../src/test-utils/edgestoreSmoke.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"edgestoreSmoke.d.ts","sourceRoot":"","sources":["../../src/test-utils/edgestoreSmoke.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,aAAa,yBAAyB,CAAC;AAEpD,wBAAgB,kBAAkB,WAEjC;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,UAEjD;AAED,wBAAgB,uBAAuB,SAMtC;AAMD,wBAAsB,iBAAiB,CAAC,MAAM,EAAE;IAC9C,MAAM,EAAE,MAAM,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC5C,WAAW,EAAE,MAAM,CAAC;CACrB;aAE4B,OAAO;GAkCnC;AAED,KAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAuBF,wBAAsB,uBAAuB,CAAC,MAAM,EAAE;IACpD,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC9D,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC3D,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;IACpD,MAAM,EAAE,MAAM,OAAO,CAAC,eAAe,CAAC,CAAC;CACxC,4BA4BA"}
|
package/dist/version.d.ts
CHANGED
package/dist/version.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAC3D,eAAO,MAAM,0BAA0B,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAC3D,eAAO,MAAM,0BAA0B,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgestore/server",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Upload files with ease from React/Next.js",
|
|
5
5
|
"homepage": "https://edgestore.dev",
|
|
6
6
|
"license": "MIT",
|
|
@@ -107,8 +107,8 @@
|
|
|
107
107
|
"!**/__tests__"
|
|
108
108
|
],
|
|
109
109
|
"dependencies": {
|
|
110
|
-
"@edgestore/sdk": "1.0.0
|
|
111
|
-
"@edgestore/shared": "1.0.0
|
|
110
|
+
"@edgestore/sdk": "1.0.0",
|
|
111
|
+
"@edgestore/shared": "1.0.0",
|
|
112
112
|
"@standard-schema/spec": "^1.1.0",
|
|
113
113
|
"cookie": "^2.0.1",
|
|
114
114
|
"jose": "^6.2.3",
|
|
@@ -19,11 +19,13 @@ import {
|
|
|
19
19
|
deleteFiles,
|
|
20
20
|
deleteFilesBodySchema,
|
|
21
21
|
getCookieConfig,
|
|
22
|
+
getUploadStatus,
|
|
22
23
|
init,
|
|
23
24
|
requestUpload,
|
|
24
25
|
requestUploadBodySchema,
|
|
25
26
|
requestUploadParts,
|
|
26
27
|
requestUploadPartsBodySchema,
|
|
28
|
+
uploadStatusBodySchema,
|
|
27
29
|
type CookieConfig,
|
|
28
30
|
type HandlerRouter,
|
|
29
31
|
} from './shared';
|
|
@@ -159,6 +161,18 @@ export async function dispatchEdgeStoreRequest<
|
|
|
159
161
|
return new Response(null, { status: 200 });
|
|
160
162
|
}
|
|
161
163
|
|
|
164
|
+
if (matchPath(request.pathname, '/upload-status')) {
|
|
165
|
+
return jsonResponse(
|
|
166
|
+
await getUploadStatus({
|
|
167
|
+
provider,
|
|
168
|
+
router,
|
|
169
|
+
body: await parseRequestBody(request, uploadStatusBodySchema),
|
|
170
|
+
ctxToken,
|
|
171
|
+
logger,
|
|
172
|
+
}),
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
162
176
|
if (matchPath(request.pathname, '/confirm-uploads')) {
|
|
163
177
|
return jsonResponse(
|
|
164
178
|
await confirmUploads({
|
package/src/adapters/shared.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type SharedInitRes,
|
|
10
10
|
type SharedRequestUploadPartsRes,
|
|
11
11
|
type SharedRequestUploadRes,
|
|
12
|
+
type SharedUploadStatusRes,
|
|
12
13
|
} from '@edgestore/shared';
|
|
13
14
|
import { stringifySetCookie } from 'cookie';
|
|
14
15
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
@@ -224,6 +225,10 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
224
225
|
autoSignedUrls,
|
|
225
226
|
});
|
|
226
227
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
228
|
+
const statusToken =
|
|
229
|
+
provider.uploads.getStatus && requestUploadRes.id
|
|
230
|
+
? await encryptStatusToken({ id: requestUploadRes.id, bucketName })
|
|
231
|
+
: undefined;
|
|
227
232
|
|
|
228
233
|
logger.debug('Finished [requestUpload]');
|
|
229
234
|
|
|
@@ -233,6 +238,64 @@ export async function requestUpload<TCtx extends AnyContext>(params: {
|
|
|
233
238
|
path: parsedPath,
|
|
234
239
|
pathOrder,
|
|
235
240
|
metadata,
|
|
241
|
+
statusToken,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export const uploadStatusBodySchema = z.object({
|
|
246
|
+
statusToken: nonEmptyStringSchema,
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
export type UploadStatusBody = z.infer<typeof uploadStatusBodySchema>;
|
|
250
|
+
|
|
251
|
+
/** Reports processing state for an upload authorized by its status token. */
|
|
252
|
+
export async function getUploadStatus<TCtx extends AnyContext>(params: {
|
|
253
|
+
provider: AnyEdgeStoreProvider;
|
|
254
|
+
router: EdgeStoreRouter<TCtx>;
|
|
255
|
+
ctxToken: string | undefined;
|
|
256
|
+
body: UploadStatusBody;
|
|
257
|
+
logger: LoggerLike;
|
|
258
|
+
}): Promise<SharedUploadStatusRes> {
|
|
259
|
+
const { provider, router, ctxToken, logger, body } = params;
|
|
260
|
+
|
|
261
|
+
await getContext(ctxToken);
|
|
262
|
+
const { id, bucketName } = await decryptStatusToken(body.statusToken);
|
|
263
|
+
logger.debug('Running [getUploadStatus]', { bucketName, id });
|
|
264
|
+
const bucket = getBucket(router, bucketName);
|
|
265
|
+
|
|
266
|
+
if (!provider.uploads.getStatus) {
|
|
267
|
+
throw new EdgeStoreError({
|
|
268
|
+
message: `Provider ${provider.name} does not report upload status.`,
|
|
269
|
+
code: 'BAD_REQUEST',
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const result = await provider.uploads.getStatus({ bucketName, id });
|
|
273
|
+
|
|
274
|
+
logger.debug('Finished [getUploadStatus]', { status: result.status });
|
|
275
|
+
|
|
276
|
+
if (result.status !== 'completed') return { status: result.status };
|
|
277
|
+
const { url, key, thumbnailUrl, sizeBytes } = result.file;
|
|
278
|
+
// Processing may add a thumbnail that the upload response could not sign.
|
|
279
|
+
const { autoSignedUrls } = bucket._def;
|
|
280
|
+
const [signed] =
|
|
281
|
+
autoSignedUrls && provider.files.getSignedUrls
|
|
282
|
+
? await provider.files.getSignedUrls({
|
|
283
|
+
bucketName,
|
|
284
|
+
files: [await referenceFromUrl(provider, url)],
|
|
285
|
+
...autoSignedUrls,
|
|
286
|
+
})
|
|
287
|
+
: [];
|
|
288
|
+
return {
|
|
289
|
+
status: 'completed',
|
|
290
|
+
file: { url, key, thumbnailUrl: thumbnailUrl ?? null, size: sizeBytes },
|
|
291
|
+
...(signed && {
|
|
292
|
+
signedReadUrl: {
|
|
293
|
+
signedUrl: signed.signedUrl,
|
|
294
|
+
signedThumbnailUrl: signed.signedThumbnailUrl ?? null,
|
|
295
|
+
expiresAt: signed.expiresAt,
|
|
296
|
+
expiresIn: signed.expiresIn,
|
|
297
|
+
},
|
|
298
|
+
}),
|
|
236
299
|
};
|
|
237
300
|
}
|
|
238
301
|
|
|
@@ -525,6 +588,41 @@ async function encryptJWT(ctx: AnyContext) {
|
|
|
525
588
|
.encrypt(await getEncryptionKey());
|
|
526
589
|
}
|
|
527
590
|
|
|
591
|
+
/**
|
|
592
|
+
* Status tokens bind one upload to the browser that requested it. They never
|
|
593
|
+
* reach application code, so they carry no expiry: uploads can take arbitrarily
|
|
594
|
+
* long, and the client bounds how long it waits for processing.
|
|
595
|
+
*/
|
|
596
|
+
async function encryptStatusToken(upload: { id: string; bucketName: string }) {
|
|
597
|
+
return await new EncryptJWT({ upload })
|
|
598
|
+
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
|
599
|
+
.encrypt(await getEncryptionKey());
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const statusTokenPayloadSchema = z.object({
|
|
603
|
+
upload: z.object({
|
|
604
|
+
id: nonEmptyStringSchema,
|
|
605
|
+
bucketName: nonEmptyStringSchema,
|
|
606
|
+
}),
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
async function decryptStatusToken(token: string) {
|
|
610
|
+
const invalid = (cause?: Error) =>
|
|
611
|
+
new EdgeStoreError({
|
|
612
|
+
message: 'Invalid upload status token',
|
|
613
|
+
code: 'BAD_REQUEST',
|
|
614
|
+
cause,
|
|
615
|
+
});
|
|
616
|
+
const { payload } = await jwtDecrypt(token, await getEncryptionKey()).catch(
|
|
617
|
+
(error: unknown) => {
|
|
618
|
+
throw invalid(error instanceof Error ? error : undefined);
|
|
619
|
+
},
|
|
620
|
+
);
|
|
621
|
+
const result = statusTokenPayloadSchema.safeParse(payload);
|
|
622
|
+
if (!result.success) throw invalid(result.error);
|
|
623
|
+
return result.data.upload;
|
|
624
|
+
}
|
|
625
|
+
|
|
528
626
|
const contextPayloadSchema = z.object({
|
|
529
627
|
ctx: z.record(z.string(), z.string()),
|
|
530
628
|
});
|
|
@@ -562,10 +660,10 @@ async function getContext(token: string | undefined) {
|
|
|
562
660
|
/** Derives the context-cookie encryption key from the configured secret. */
|
|
563
661
|
async function getEncryptionKey() {
|
|
564
662
|
const secret =
|
|
565
|
-
getEnv('
|
|
663
|
+
getEnv('EDGESTORE_JWT_SECRET') ?? getEnv('EDGESTORE_SECRET_KEY');
|
|
566
664
|
if (!secret) {
|
|
567
665
|
throw new EdgeStoreError({
|
|
568
|
-
message: '
|
|
666
|
+
message: 'EDGESTORE_JWT_SECRET or EDGESTORE_SECRET_KEY is not defined',
|
|
569
667
|
code: 'SERVER_ERROR',
|
|
570
668
|
});
|
|
571
669
|
}
|
package/src/libs/env.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export function getEnv(key: string): string | undefined {
|
|
2
|
+
return (
|
|
3
|
+
readEnv(key) ??
|
|
4
|
+
(key.startsWith('EDGESTORE_')
|
|
5
|
+
? readEnv(key.replace('EDGESTORE_', 'EDGE_STORE_'))
|
|
6
|
+
: undefined)
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function readEnv(key: string): string | undefined {
|
|
2
11
|
if (typeof process !== 'undefined' && process.env) {
|
|
3
12
|
// @ts-expect-error - In Vite/Astro, env variables are available on import.meta.
|
|
4
13
|
return process.env[key] ?? import.meta.env?.[key];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = `Missing
|
|
1
|
+
const DEFAULT_MESSAGE = `Missing EDGESTORE_ACCESS_KEY or EDGESTORE_SECRET_KEY.
|
|
2
2
|
This can happen if you are trying to import something related to the backend of EdgeStore in a client component.`;
|
|
3
3
|
|
|
4
4
|
class EdgeStoreCredentialsError extends Error {
|
|
@@ -60,13 +60,13 @@ export type AzureBlobProviderOptions = {
|
|
|
60
60
|
endpoint?: string;
|
|
61
61
|
/**
|
|
62
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 `
|
|
63
|
+
* to `<endpoint>/<container>`. Can also be set via `EDGESTORE_BASE_URL`.
|
|
64
64
|
* Signed URLs always use the endpoint.
|
|
65
65
|
*/
|
|
66
66
|
baseUrl?: string;
|
|
67
67
|
/**
|
|
68
68
|
* Secret used to sign multipart upload sessions. Defaults to
|
|
69
|
-
* `
|
|
69
|
+
* `EDGESTORE_JWT_SECRET` or `EDGESTORE_SECRET_KEY`.
|
|
70
70
|
*/
|
|
71
71
|
jwtSecret?: string;
|
|
72
72
|
/**
|
|
@@ -117,7 +117,7 @@ export function azureBlob(options: AzureBlobProviderOptions = {}) {
|
|
|
117
117
|
).replace(/\/+$/, '');
|
|
118
118
|
const baseUrl =
|
|
119
119
|
options.baseUrl ??
|
|
120
|
-
getEnv('
|
|
120
|
+
getEnv('EDGESTORE_BASE_URL') ??
|
|
121
121
|
`${endpoint}/${containerName}`;
|
|
122
122
|
const keys = createObjectKeys('Azure Blob', baseUrl);
|
|
123
123
|
const uploadExpiresIn = expiration(options.uploadUrlExpiresIn ?? 3600);
|