@edgestore/server 1.0.0-next.0 → 1.0.0-next.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/README.md +10 -4
- package/dist/adapters/astro/index.d.ts +7 -12
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +14 -161
- package/dist/adapters/dispatcher.d.ts +30 -0
- package/dist/adapters/dispatcher.d.ts.map +1 -0
- package/dist/adapters/dispatcher.js +172 -0
- package/dist/adapters/express/index.d.ts +7 -12
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +24 -94
- package/dist/adapters/fastify/index.d.ts +7 -12
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +23 -121
- package/dist/adapters/hono/index.d.ts +7 -12
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +14 -121
- package/dist/adapters/next/app/index.d.ts +7 -12
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +21 -148
- package/dist/adapters/next/pages/index.d.ts +7 -12
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +34 -95
- package/dist/adapters/remix/index.d.ts +7 -12
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +14 -136
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +188 -179
- package/dist/adapters/start/index.d.ts +7 -12
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +14 -169
- package/dist/core/client/bucketClient.d.ts +7 -0
- package/dist/core/client/bucketClient.d.ts.map +1 -0
- package/dist/core/client/bucketClient.js +312 -0
- package/dist/core/client/index.d.ts +119 -140
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +13 -218
- package/dist/core/index.d.ts +20 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -2
- package/dist/core/provider.d.ts +15 -0
- package/dist/core/provider.d.ts.map +1 -0
- package/dist/core/provider.js +25 -0
- package/dist/core/routerRules.d.ts +21 -0
- package/dist/core/routerRules.d.ts.map +1 -0
- package/dist/core/routerRules.js +54 -0
- package/dist/core/validateFile.d.ts +9 -0
- package/dist/core/validateFile.d.ts.map +1 -0
- package/dist/core/validateFile.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/libs/env.d.ts +3 -0
- package/dist/libs/env.d.ts.map +1 -0
- package/dist/libs/env.js +14 -0
- package/dist/libs/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +1 -1
- package/dist/providers/azure-blob/index.d.ts +119 -0
- package/dist/providers/azure-blob/index.d.ts.map +1 -0
- package/dist/providers/azure-blob/index.js +166 -0
- package/dist/providers/edgestore/index.d.ts +171 -3
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +304 -136
- package/dist/providers/{aws → s3}/index.d.ts +68 -11
- package/dist/providers/s3/index.d.ts.map +1 -0
- package/dist/providers/s3/index.js +146 -0
- package/dist/test-utils/edgestoreSmoke.d.ts +0 -2
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/package.json +13 -12
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- package/providers/s3/index.d.ts +1 -0
- package/providers/s3/index.js +1 -0
- package/src/adapters/astro/index.ts +27 -211
- package/src/adapters/dispatcher.ts +261 -0
- package/src/adapters/express/index.ts +43 -149
- package/src/adapters/fastify/index.ts +43 -191
- package/src/adapters/hono/index.ts +28 -183
- package/src/adapters/next/app/index.ts +37 -201
- package/src/adapters/next/pages/index.ts +55 -156
- package/src/adapters/remix/index.ts +27 -190
- package/src/adapters/shared.ts +237 -261
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +480 -0
- package/src/core/client/index.ts +300 -427
- package/src/core/index.ts +42 -1
- package/src/core/provider.ts +80 -0
- package/src/core/routerRules.ts +75 -0
- package/src/core/validateFile.ts +50 -0
- package/src/index.ts +10 -0
- package/src/libs/env.ts +16 -0
- package/src/libs/logger.ts +4 -2
- package/src/providers/azure-blob/index.ts +265 -0
- package/src/providers/edgestore/index.ts +426 -154
- package/src/providers/s3/index.ts +289 -0
- package/src/test-utils/edgestoreSmoke.ts +0 -11
- package/src/version.ts +1 -1
- package/dist/core/sdk/index.d.ts +0 -282
- package/dist/core/sdk/index.d.ts.map +0 -1
- package/dist/core/sdk/index.js +0 -253
- package/dist/providers/aws/index.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -102
- package/dist/providers/azure/index.d.ts +0 -39
- package/dist/providers/azure/index.d.ts.map +0 -1
- package/dist/providers/azure/index.js +0 -84
- package/dist/version.js +0 -4
- package/providers/aws/index.d.ts +0 -1
- package/providers/aws/index.js +0 -1
- package/providers/azure/index.d.ts +0 -1
- package/providers/azure/index.js +0 -1
- package/src/core/sdk/index.ts +0 -542
- package/src/providers/aws/index.ts +0 -239
- package/src/providers/azure/index.ts +0 -141
package/dist/adapters/shared.js
CHANGED
|
@@ -2,7 +2,11 @@ import { EdgeStoreError } from '@edgestore/shared';
|
|
|
2
2
|
import { hkdf } from '@panva/hkdf';
|
|
3
3
|
import { stringifySetCookie } from 'cookie';
|
|
4
4
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
5
|
-
import {
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { getProviderBaseUrl, referenceFromUrl } from '../core/provider.js';
|
|
7
|
+
import { parseBucketInput, buildPath, parsePath } from '../core/routerRules.js';
|
|
8
|
+
import { validateFileForBucket } from '../core/validateFile.js';
|
|
9
|
+
import { getEnv, isDev } from '../libs/env.js';
|
|
6
10
|
|
|
7
11
|
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
8
12
|
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
|
|
@@ -58,23 +62,16 @@ async function fetchProxyFile({ cookieHeader, url }) {
|
|
|
58
62
|
};
|
|
59
63
|
}
|
|
60
64
|
async function init(params) {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
log.debug('Running [init]', {
|
|
65
|
+
const { ctx, provider, router, logger, cookieConfig } = params;
|
|
66
|
+
logger.debug('Running [init]', {
|
|
64
67
|
ctx
|
|
65
68
|
});
|
|
66
69
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
67
70
|
const ctxToken = await encryptJWT(ctx);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const initRes = await provider.init({
|
|
73
|
-
ctx,
|
|
74
|
-
router: router
|
|
75
|
-
});
|
|
76
|
-
token = initRes.token;
|
|
77
|
-
}
|
|
71
|
+
const initRes = await provider.init({
|
|
72
|
+
ctx,
|
|
73
|
+
router
|
|
74
|
+
});
|
|
78
75
|
const newCookies = [
|
|
79
76
|
stringifySetCookie({
|
|
80
77
|
name: resolvedCookieConfig.ctx.name,
|
|
@@ -82,34 +79,44 @@ async function init(params) {
|
|
|
82
79
|
...resolvedCookieConfig.ctx.options
|
|
83
80
|
})
|
|
84
81
|
];
|
|
85
|
-
if (token) {
|
|
82
|
+
if (initRes.token) {
|
|
86
83
|
newCookies.push(stringifySetCookie({
|
|
87
84
|
name: resolvedCookieConfig.token.name,
|
|
88
|
-
value: token,
|
|
85
|
+
value: initRes.token,
|
|
89
86
|
...resolvedCookieConfig.token.options
|
|
90
87
|
}));
|
|
91
88
|
}
|
|
92
|
-
const baseUrl = await provider
|
|
93
|
-
|
|
89
|
+
const baseUrl = await getProviderBaseUrl(provider);
|
|
90
|
+
logger.debug('Finished [init]', {
|
|
94
91
|
ctx,
|
|
95
92
|
newCookies,
|
|
96
|
-
token,
|
|
97
93
|
baseUrl,
|
|
98
94
|
providerName: provider.name,
|
|
99
|
-
|
|
95
|
+
clientInit: initRes.clientInit
|
|
100
96
|
});
|
|
101
97
|
return {
|
|
102
98
|
newCookies,
|
|
103
|
-
token,
|
|
104
99
|
baseUrl,
|
|
105
100
|
providerName: provider.name,
|
|
106
|
-
|
|
101
|
+
clientInit: initRes.clientInit
|
|
107
102
|
};
|
|
108
103
|
}
|
|
104
|
+
const nonEmptyStringSchema = z.string().min(1);
|
|
105
|
+
const requestUploadBodySchema = z.object({
|
|
106
|
+
bucketName: nonEmptyStringSchema,
|
|
107
|
+
input: z.unknown().default({}),
|
|
108
|
+
fileInfo: z.object({
|
|
109
|
+
size: z.number().finite().nonnegative(),
|
|
110
|
+
type: z.string(),
|
|
111
|
+
extension: z.string(),
|
|
112
|
+
fileName: z.string().optional(),
|
|
113
|
+
replaceTargetUrl: nonEmptyStringSchema.optional(),
|
|
114
|
+
temporary: z.boolean().default(false)
|
|
115
|
+
})
|
|
116
|
+
});
|
|
109
117
|
async function requestUpload(params) {
|
|
110
|
-
const { provider, router, ctxToken, body: { bucketName, input, fileInfo } } = params;
|
|
111
|
-
|
|
112
|
-
log.debug('Running [requestUpload]', {
|
|
118
|
+
const { provider, router, ctxToken, logger, body: { bucketName, input, fileInfo } } = params;
|
|
119
|
+
logger.debug('Running [requestUpload]', {
|
|
113
120
|
bucketName,
|
|
114
121
|
input,
|
|
115
122
|
fileInfo
|
|
@@ -121,7 +128,7 @@ async function requestUpload(params) {
|
|
|
121
128
|
});
|
|
122
129
|
}
|
|
123
130
|
const ctx = await getContext(ctxToken);
|
|
124
|
-
|
|
131
|
+
logger.debug('Decrypted Context', {
|
|
125
132
|
ctx
|
|
126
133
|
});
|
|
127
134
|
const bucket = router.buckets[bucketName];
|
|
@@ -131,11 +138,12 @@ async function requestUpload(params) {
|
|
|
131
138
|
code: 'BAD_REQUEST'
|
|
132
139
|
});
|
|
133
140
|
}
|
|
141
|
+
const parsedInput = await parseBucketInput(bucket, input);
|
|
134
142
|
if (bucket._def.beforeUpload) {
|
|
135
|
-
|
|
143
|
+
logger.debug('Running [beforeUpload]');
|
|
136
144
|
const canUpload = await bucket._def.beforeUpload?.({
|
|
137
145
|
ctx,
|
|
138
|
-
input,
|
|
146
|
+
input: parsedInput,
|
|
139
147
|
fileInfo: {
|
|
140
148
|
size: fileInfo.size,
|
|
141
149
|
type: fileInfo.type,
|
|
@@ -145,7 +153,7 @@ async function requestUpload(params) {
|
|
|
145
153
|
temporary: fileInfo.temporary
|
|
146
154
|
}
|
|
147
155
|
});
|
|
148
|
-
|
|
156
|
+
logger.debug('Finished [beforeUpload]', {
|
|
149
157
|
canUpload
|
|
150
158
|
});
|
|
151
159
|
if (!canUpload) {
|
|
@@ -155,76 +163,30 @@ async function requestUpload(params) {
|
|
|
155
163
|
});
|
|
156
164
|
}
|
|
157
165
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
message: 'Only images are allowed in this bucket',
|
|
163
|
-
details: {
|
|
164
|
-
allowedMimeTypes: IMAGE_MIME_TYPES,
|
|
165
|
-
mimeType: fileInfo.type
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (bucket._def.bucketConfig?.maxSize) {
|
|
171
|
-
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
172
|
-
throw new EdgeStoreError({
|
|
173
|
-
code: 'FILE_TOO_LARGE',
|
|
174
|
-
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
175
|
-
details: {
|
|
176
|
-
maxFileSize: bucket._def.bucketConfig.maxSize,
|
|
177
|
-
fileSize: fileInfo.size
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (bucket._def.bucketConfig?.accept) {
|
|
183
|
-
const accept = bucket._def.bucketConfig.accept;
|
|
184
|
-
let accepted = false;
|
|
185
|
-
for (const acceptedMimeType of accept){
|
|
186
|
-
if (acceptedMimeType.endsWith('/*')) {
|
|
187
|
-
const mimeType = acceptedMimeType.replace('/*', '');
|
|
188
|
-
if (fileInfo.type.startsWith(mimeType)) {
|
|
189
|
-
accepted = true;
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
} else if (fileInfo.type === acceptedMimeType) {
|
|
193
|
-
accepted = true;
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
if (!accepted) {
|
|
198
|
-
throw new EdgeStoreError({
|
|
199
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
200
|
-
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
201
|
-
details: {
|
|
202
|
-
allowedMimeTypes: accept,
|
|
203
|
-
mimeType: fileInfo.type
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
}
|
|
166
|
+
validateFileForBucket({
|
|
167
|
+
bucket,
|
|
168
|
+
fileInfo
|
|
169
|
+
});
|
|
208
170
|
const path = buildPath({
|
|
209
171
|
bucket,
|
|
210
172
|
pathAttrs: {
|
|
211
173
|
ctx,
|
|
212
|
-
input
|
|
174
|
+
input: parsedInput
|
|
213
175
|
}
|
|
214
176
|
});
|
|
215
177
|
const metadata = await bucket._def.metadata?.({
|
|
216
178
|
ctx,
|
|
217
|
-
input
|
|
218
|
-
});
|
|
179
|
+
input: parsedInput
|
|
180
|
+
}) ?? {};
|
|
219
181
|
const isPublic = bucket._def.accessControl === undefined;
|
|
220
182
|
const autoSignedUrls = bucket._def.autoSignedUrls;
|
|
221
|
-
|
|
183
|
+
logger.debug('upload info', {
|
|
222
184
|
path,
|
|
223
185
|
metadata,
|
|
224
186
|
isPublic,
|
|
225
187
|
bucketType: bucket._def.type
|
|
226
188
|
});
|
|
227
|
-
const requestUploadRes = await provider.
|
|
189
|
+
const requestUploadRes = await provider.uploads.request({
|
|
228
190
|
bucketName,
|
|
229
191
|
bucketType: bucket._def.type,
|
|
230
192
|
fileInfo: {
|
|
@@ -236,7 +198,7 @@ async function requestUpload(params) {
|
|
|
236
198
|
autoSignedUrls
|
|
237
199
|
});
|
|
238
200
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
239
|
-
|
|
201
|
+
logger.debug('Finished [requestUpload]');
|
|
240
202
|
return {
|
|
241
203
|
...requestUploadRes,
|
|
242
204
|
size: fileInfo.size,
|
|
@@ -246,10 +208,16 @@ async function requestUpload(params) {
|
|
|
246
208
|
metadata
|
|
247
209
|
};
|
|
248
210
|
}
|
|
211
|
+
const requestUploadPartsBodySchema = z.object({
|
|
212
|
+
multipart: z.object({
|
|
213
|
+
uploadId: nonEmptyStringSchema,
|
|
214
|
+
parts: z.array(z.number().int().positive())
|
|
215
|
+
}),
|
|
216
|
+
path: nonEmptyStringSchema
|
|
217
|
+
});
|
|
249
218
|
async function requestUploadParts(params) {
|
|
250
|
-
const { provider, ctxToken, body: { multipart, path } } = params;
|
|
251
|
-
|
|
252
|
-
log.debug('Running [requestUploadParts]', {
|
|
219
|
+
const { provider, ctxToken, logger, body: { multipart, path } } = params;
|
|
220
|
+
logger.debug('Running [requestUploadParts]', {
|
|
253
221
|
multipart,
|
|
254
222
|
path
|
|
255
223
|
});
|
|
@@ -260,17 +228,32 @@ async function requestUploadParts(params) {
|
|
|
260
228
|
});
|
|
261
229
|
}
|
|
262
230
|
await getContext(ctxToken); // just to check if the token is valid
|
|
263
|
-
const
|
|
231
|
+
const multipartUploads = provider.uploads.multipart;
|
|
232
|
+
if (!multipartUploads) {
|
|
233
|
+
throw new EdgeStoreError({
|
|
234
|
+
message: `Provider ${provider.name} does not support multipart uploads.`,
|
|
235
|
+
code: 'BAD_REQUEST'
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
const res = await multipartUploads.requestParts({
|
|
264
239
|
multipart,
|
|
265
240
|
path
|
|
266
241
|
});
|
|
267
|
-
|
|
242
|
+
logger.debug('Finished [requestUploadParts]');
|
|
268
243
|
return res;
|
|
269
244
|
}
|
|
245
|
+
const completeMultipartUploadBodySchema = z.object({
|
|
246
|
+
bucketName: nonEmptyStringSchema,
|
|
247
|
+
uploadId: nonEmptyStringSchema,
|
|
248
|
+
key: nonEmptyStringSchema,
|
|
249
|
+
parts: z.array(z.object({
|
|
250
|
+
partNumber: z.number().int().positive(),
|
|
251
|
+
eTag: nonEmptyStringSchema
|
|
252
|
+
}))
|
|
253
|
+
});
|
|
270
254
|
async function completeMultipartUpload(params) {
|
|
271
|
-
const { provider, router, ctxToken, body: { bucketName, uploadId, key, parts } } = params;
|
|
272
|
-
|
|
273
|
-
log.debug('Running [completeMultipartUpload]', {
|
|
255
|
+
const { provider, router, ctxToken, logger, body: { bucketName, uploadId, key, parts } } = params;
|
|
256
|
+
logger.debug('Running [completeMultipartUpload]', {
|
|
274
257
|
bucketName,
|
|
275
258
|
uploadId,
|
|
276
259
|
key
|
|
@@ -289,20 +272,29 @@ async function completeMultipartUpload(params) {
|
|
|
289
272
|
code: 'BAD_REQUEST'
|
|
290
273
|
});
|
|
291
274
|
}
|
|
292
|
-
const
|
|
275
|
+
const multipartUploads = provider.uploads.multipart;
|
|
276
|
+
if (!multipartUploads) {
|
|
277
|
+
throw new EdgeStoreError({
|
|
278
|
+
message: `Provider ${provider.name} does not support multipart uploads.`,
|
|
279
|
+
code: 'BAD_REQUEST'
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
await multipartUploads.complete({
|
|
293
283
|
uploadId,
|
|
294
284
|
key,
|
|
295
285
|
parts
|
|
296
286
|
});
|
|
297
|
-
|
|
298
|
-
return res;
|
|
287
|
+
logger.debug('Finished [completeMultipartUpload]');
|
|
299
288
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
289
|
+
const confirmUploadsBodySchema = z.object({
|
|
290
|
+
bucketName: nonEmptyStringSchema,
|
|
291
|
+
urls: z.array(nonEmptyStringSchema)
|
|
292
|
+
});
|
|
293
|
+
async function confirmUploads(params) {
|
|
294
|
+
const { provider, router, ctxToken, logger, body: { bucketName, urls } } = params;
|
|
295
|
+
logger.debug('Running [confirmUploads]', {
|
|
304
296
|
bucketName,
|
|
305
|
-
|
|
297
|
+
urls
|
|
306
298
|
});
|
|
307
299
|
if (!ctxToken) {
|
|
308
300
|
throw new EdgeStoreError({
|
|
@@ -318,19 +310,29 @@ async function confirmUpload(params) {
|
|
|
318
310
|
code: 'BAD_REQUEST'
|
|
319
311
|
});
|
|
320
312
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
313
|
+
if (!provider.files.confirm) {
|
|
314
|
+
throw new EdgeStoreError({
|
|
315
|
+
message: `Provider ${provider.name} does not support file confirmation.`,
|
|
316
|
+
code: 'SERVER_ERROR'
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
const files = await Promise.all(urls.map((url)=>referenceFromUrl(provider, unproxyUrl(url))));
|
|
320
|
+
const result = await provider.files.confirm({
|
|
321
|
+
bucketName,
|
|
322
|
+
files
|
|
324
323
|
});
|
|
325
|
-
|
|
326
|
-
return
|
|
324
|
+
logger.debug('Finished [confirmUploads]');
|
|
325
|
+
return mapFrontendMutationResult(urls, result);
|
|
327
326
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
327
|
+
const deleteFilesBodySchema = z.object({
|
|
328
|
+
bucketName: nonEmptyStringSchema,
|
|
329
|
+
urls: z.array(nonEmptyStringSchema)
|
|
330
|
+
});
|
|
331
|
+
async function deleteFiles(params) {
|
|
332
|
+
const { provider, router, ctxToken, logger, body: { bucketName, urls } } = params;
|
|
333
|
+
logger.debug('Running [deleteFiles]', {
|
|
332
334
|
bucketName,
|
|
333
|
-
|
|
335
|
+
urls
|
|
334
336
|
});
|
|
335
337
|
if (!ctxToken) {
|
|
336
338
|
throw new EdgeStoreError({
|
|
@@ -352,25 +354,72 @@ async function deleteFile(params) {
|
|
|
352
354
|
code: 'SERVER_ERROR'
|
|
353
355
|
});
|
|
354
356
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
357
|
+
if (!provider.files.delete) {
|
|
358
|
+
throw new EdgeStoreError({
|
|
359
|
+
message: `Provider ${provider.name} does not support file deletion.`,
|
|
360
|
+
code: 'SERVER_ERROR'
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
const files = await Promise.all(urls.map((url)=>referenceFromUrl(provider, unproxyUrl(url))));
|
|
364
|
+
const fileRecords = await Promise.all(files.map((file)=>Promise.resolve(provider.files.get({
|
|
365
|
+
bucketName,
|
|
366
|
+
file
|
|
367
|
+
}))));
|
|
368
|
+
const authorizations = await Promise.all(fileRecords.map((file)=>{
|
|
369
|
+
if (file.path === undefined && bucket._def.path.length > 0) {
|
|
370
|
+
throw new EdgeStoreError({
|
|
371
|
+
message: `Provider ${provider.name} must return path from files.get to authorize frontend deletion for a bucket with configured path fields.`,
|
|
372
|
+
code: 'SERVER_ERROR'
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
if (file.metadata === undefined && bucket._def.metadata !== undefined) {
|
|
376
|
+
throw new EdgeStoreError({
|
|
377
|
+
message: `Provider ${provider.name} must return metadata from files.get to authorize frontend deletion for a bucket with configured metadata fields.`,
|
|
378
|
+
code: 'SERVER_ERROR'
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
return Promise.resolve(bucket._def.beforeDelete({
|
|
382
|
+
ctx,
|
|
383
|
+
fileInfo: {
|
|
384
|
+
url: file.url,
|
|
385
|
+
size: file.sizeBytes,
|
|
386
|
+
uploadedAt: new Date(file.uploadedAt),
|
|
387
|
+
path: file.path ?? {},
|
|
388
|
+
metadata: file.metadata ?? {}
|
|
389
|
+
}
|
|
390
|
+
}));
|
|
391
|
+
}));
|
|
392
|
+
if (authorizations.some((allowed)=>!allowed)) {
|
|
363
393
|
throw new EdgeStoreError({
|
|
364
394
|
message: 'Delete not allowed for the current context',
|
|
365
395
|
code: 'DELETE_NOT_ALLOWED'
|
|
366
396
|
});
|
|
367
397
|
}
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
398
|
+
const result = await provider.files.delete({
|
|
399
|
+
bucketName,
|
|
400
|
+
files
|
|
371
401
|
});
|
|
372
|
-
|
|
373
|
-
return
|
|
402
|
+
logger.debug('Finished [deleteFiles]');
|
|
403
|
+
return mapFrontendMutationResult(urls, result);
|
|
404
|
+
}
|
|
405
|
+
function mapFrontendMutationResult(urls, result) {
|
|
406
|
+
if (result.results.length !== urls.length) {
|
|
407
|
+
throw new Error(`The provider returned ${result.results.length} mutation results for ${urls.length} files.`);
|
|
408
|
+
}
|
|
409
|
+
const succeeded = [];
|
|
410
|
+
const failed = [];
|
|
411
|
+
result.results.forEach((item, index)=>{
|
|
412
|
+
const url = urls[index];
|
|
413
|
+
if (item.success) succeeded.push(url);
|
|
414
|
+
else failed.push({
|
|
415
|
+
url,
|
|
416
|
+
error: item.error
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
return {
|
|
420
|
+
succeeded,
|
|
421
|
+
failed
|
|
422
|
+
};
|
|
374
423
|
}
|
|
375
424
|
async function encryptJWT(ctx) {
|
|
376
425
|
const secret = getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
@@ -381,7 +430,9 @@ async function encryptJWT(ctx) {
|
|
|
381
430
|
});
|
|
382
431
|
}
|
|
383
432
|
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
384
|
-
return await new EncryptJWT(
|
|
433
|
+
return await new EncryptJWT({
|
|
434
|
+
ctx
|
|
435
|
+
}).setProtectedHeader({
|
|
385
436
|
alg: 'dir',
|
|
386
437
|
enc: 'A256GCM'
|
|
387
438
|
}).setIssuedAt().setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE).setJti(crypto.randomUUID()).encrypt(encryptionSecret);
|
|
@@ -398,51 +449,21 @@ async function decryptJWT(token) {
|
|
|
398
449
|
const { payload } = await jwtDecrypt(token, encryptionSecret, {
|
|
399
450
|
clockTolerance: 15
|
|
400
451
|
});
|
|
401
|
-
|
|
452
|
+
const result = z.object({
|
|
453
|
+
ctx: z.record(z.string())
|
|
454
|
+
}).safeParse(payload);
|
|
455
|
+
if (!result.success) {
|
|
456
|
+
throw new EdgeStoreError({
|
|
457
|
+
message: 'Invalid edgestore-ctx cookie',
|
|
458
|
+
code: 'UNAUTHORIZED',
|
|
459
|
+
cause: result.error
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return result.data.ctx;
|
|
402
463
|
}
|
|
403
464
|
async function getDerivedEncryptionKey(secret) {
|
|
404
465
|
return await hkdf('sha256', secret, '', 'EdgeStore Generated Encryption Key', 32);
|
|
405
466
|
}
|
|
406
|
-
function buildPath(params) {
|
|
407
|
-
const { bucket } = params;
|
|
408
|
-
const pathParams = bucket._def.path;
|
|
409
|
-
const path = pathParams.map((param)=>{
|
|
410
|
-
const paramEntries = Object.entries(param);
|
|
411
|
-
if (paramEntries[0] === undefined) {
|
|
412
|
-
throw new EdgeStoreError({
|
|
413
|
-
message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
|
|
414
|
-
code: 'SERVER_ERROR'
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
const [key, value] = paramEntries[0];
|
|
418
|
-
// this is a string like: "ctx.xxx" or "input.yyy.zzz"
|
|
419
|
-
const currParamVal = value().split('.').reduce((acc2, key)=>{
|
|
420
|
-
if (acc2[key] === undefined) {
|
|
421
|
-
throw new EdgeStoreError({
|
|
422
|
-
message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
|
|
423
|
-
code: 'BAD_REQUEST'
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
return acc2[key];
|
|
427
|
-
}, params.pathAttrs);
|
|
428
|
-
return {
|
|
429
|
-
key,
|
|
430
|
-
value: currParamVal
|
|
431
|
-
};
|
|
432
|
-
});
|
|
433
|
-
return path;
|
|
434
|
-
}
|
|
435
|
-
function parsePath(path) {
|
|
436
|
-
const parsedPath = path.reduce((acc, curr)=>{
|
|
437
|
-
acc[curr.key] = curr.value;
|
|
438
|
-
return acc;
|
|
439
|
-
}, {});
|
|
440
|
-
const pathOrder = path.map((p)=>p.key);
|
|
441
|
-
return {
|
|
442
|
-
parsedPath,
|
|
443
|
-
pathOrder
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
467
|
async function getContext(token) {
|
|
447
468
|
return await decryptJWT(token);
|
|
448
469
|
}
|
|
@@ -462,17 +483,5 @@ async function getContext(token) {
|
|
|
462
483
|
}
|
|
463
484
|
return url;
|
|
464
485
|
}
|
|
465
|
-
function getEnv(key) {
|
|
466
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
467
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
468
|
-
return process.env[key] ?? import.meta.env?.[key];
|
|
469
|
-
}
|
|
470
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
471
|
-
return import.meta.env?.[key];
|
|
472
|
-
}
|
|
473
|
-
function isDev() {
|
|
474
|
-
return process?.env?.NODE_ENV === 'development' || // @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
475
|
-
import.meta.env?.DEV;
|
|
476
|
-
}
|
|
477
486
|
|
|
478
|
-
export {
|
|
487
|
+
export { completeMultipartUpload, completeMultipartUploadBodySchema, confirmUploads, confirmUploadsBodySchema, deleteFiles, deleteFilesBodySchema, fetchProxyFile, getCookieConfig, init, requestUpload, requestUploadBodySchema, requestUploadParts, requestUploadPartsBodySchema };
|
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
2
|
import { type LogLevel } from '../../libs/logger.js';
|
|
3
|
-
import { type
|
|
3
|
+
import { type CreateContextConfig } from '../dispatcher.js';
|
|
4
|
+
import type { CookieConfig, HandlerEdgeStore } from '../shared.js';
|
|
4
5
|
export type CreateContextOptions = {
|
|
5
6
|
req: Request;
|
|
6
7
|
};
|
|
7
|
-
export type Config<TCtx> = {
|
|
8
|
-
|
|
9
|
-
router: EdgeStoreRouter<TCtx>;
|
|
8
|
+
export type Config<TCtx extends AnyContext> = {
|
|
9
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
10
10
|
logLevel?: LogLevel;
|
|
11
11
|
cookieConfig?: CookieConfig;
|
|
12
|
-
} &
|
|
13
|
-
|
|
14
|
-
router: EdgeStoreRouter<TCtx>;
|
|
15
|
-
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
16
|
-
cookieConfig?: CookieConfig;
|
|
17
|
-
});
|
|
18
|
-
export declare function createEdgeStoreStartHandler<TCtx>(config: Config<TCtx>): ({ request }: {
|
|
12
|
+
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
13
|
+
export declare function createEdgeStoreStartHandler<TCtx extends AnyContext>(config: Config<TCtx>): ({ request }: {
|
|
19
14
|
request: Request;
|
|
20
15
|
}) => Promise<Response>;
|
|
21
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/start/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/start/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEhE,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,OAAO,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,UAAU,IAAI;IAC5C,SAAS,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,GAAG,mBAAmB,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;AAEpD,wBAAgB,2BAA2B,CAAC,IAAI,SAAS,UAAU,EACjE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,IAKN,aAAa;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,uBAkBhD"}
|