@edgestore/server 0.8.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/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 +8 -13
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +17 -166
- 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 +8 -13
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +27 -99
- package/dist/adapters/fastify/index.d.ts +8 -13
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +26 -126
- package/dist/adapters/hono/index.d.ts +8 -13
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +17 -126
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +8 -13
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +24 -153
- package/dist/adapters/next/pages/index.d.ts +8 -13
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +37 -100
- package/dist/adapters/remix/index.d.ts +8 -13
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +17 -141
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +218 -215
- package/dist/adapters/start/index.d.ts +8 -13
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +17 -174
- 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 -220
- package/dist/core/index.d.ts +21 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -10
- 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 +4 -10
- 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/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- 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 +307 -141
- 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/dist/version.d.ts.map +1 -1
- package/package.json +58 -56
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- 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 +249 -274
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +480 -0
- package/src/core/client/index.ts +300 -428
- 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/adapters/astro/index.mjs +0 -172
- package/dist/adapters/express/index.mjs +0 -108
- package/dist/adapters/fastify/index.mjs +0 -135
- package/dist/adapters/hono/index.mjs +0 -131
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -163
- package/dist/adapters/next/pages/index.mjs +0 -112
- package/dist/adapters/remix/index.mjs +0 -148
- package/dist/adapters/shared.mjs +0 -471
- package/dist/adapters/start/index.mjs +0 -181
- package/dist/core/client/index.mjs +0 -221
- package/dist/core/index.mjs +0 -2
- 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 -256
- package/dist/core/sdk/index.mjs +0 -253
- 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.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -105
- package/dist/providers/aws/index.mjs +0 -103
- 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 -87
- package/dist/providers/azure/index.mjs +0 -85
- package/dist/providers/edgestore/index.mjs +0 -164
- package/dist/version.js +0 -7
- package/dist/version.mjs +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 -549
- package/src/providers/aws/index.ts +0 -240
- package/src/providers/azure/index.ts +0 -142
package/dist/adapters/shared.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
|
+
import { hkdf } from '@panva/hkdf';
|
|
3
|
+
import { stringifySetCookie } from 'cookie';
|
|
4
|
+
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
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';
|
|
9
10
|
|
|
10
11
|
// TODO: change it to 1 hour when we have a way to refresh the token
|
|
11
12
|
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
|
|
@@ -61,76 +62,88 @@ async function fetchProxyFile({ cookieHeader, url }) {
|
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
64
|
async function init(params) {
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
log.debug('Running [init]', {
|
|
65
|
+
const { ctx, provider, router, logger, cookieConfig } = params;
|
|
66
|
+
logger.debug('Running [init]', {
|
|
67
67
|
ctx
|
|
68
68
|
});
|
|
69
69
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
70
70
|
const ctxToken = await encryptJWT(ctx);
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const initRes = await provider.init({
|
|
76
|
-
ctx,
|
|
77
|
-
router: router
|
|
78
|
-
});
|
|
79
|
-
token = initRes.token;
|
|
80
|
-
}
|
|
71
|
+
const initRes = await provider.init({
|
|
72
|
+
ctx,
|
|
73
|
+
router
|
|
74
|
+
});
|
|
81
75
|
const newCookies = [
|
|
82
|
-
|
|
76
|
+
stringifySetCookie({
|
|
77
|
+
name: resolvedCookieConfig.ctx.name,
|
|
78
|
+
value: ctxToken,
|
|
79
|
+
...resolvedCookieConfig.ctx.options
|
|
80
|
+
})
|
|
83
81
|
];
|
|
84
|
-
if (token) {
|
|
85
|
-
newCookies.push(
|
|
82
|
+
if (initRes.token) {
|
|
83
|
+
newCookies.push(stringifySetCookie({
|
|
84
|
+
name: resolvedCookieConfig.token.name,
|
|
85
|
+
value: initRes.token,
|
|
86
|
+
...resolvedCookieConfig.token.options
|
|
87
|
+
}));
|
|
86
88
|
}
|
|
87
|
-
const baseUrl = await provider
|
|
88
|
-
|
|
89
|
+
const baseUrl = await getProviderBaseUrl(provider);
|
|
90
|
+
logger.debug('Finished [init]', {
|
|
89
91
|
ctx,
|
|
90
92
|
newCookies,
|
|
91
|
-
token,
|
|
92
93
|
baseUrl,
|
|
93
94
|
providerName: provider.name,
|
|
94
|
-
|
|
95
|
+
clientInit: initRes.clientInit
|
|
95
96
|
});
|
|
96
97
|
return {
|
|
97
98
|
newCookies,
|
|
98
|
-
token,
|
|
99
99
|
baseUrl,
|
|
100
100
|
providerName: provider.name,
|
|
101
|
-
|
|
101
|
+
clientInit: initRes.clientInit
|
|
102
102
|
};
|
|
103
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
|
+
});
|
|
104
117
|
async function requestUpload(params) {
|
|
105
|
-
const { provider, router, ctxToken, body: { bucketName, input, fileInfo } } = params;
|
|
106
|
-
|
|
107
|
-
log.debug('Running [requestUpload]', {
|
|
118
|
+
const { provider, router, ctxToken, logger, body: { bucketName, input, fileInfo } } = params;
|
|
119
|
+
logger.debug('Running [requestUpload]', {
|
|
108
120
|
bucketName,
|
|
109
121
|
input,
|
|
110
122
|
fileInfo
|
|
111
123
|
});
|
|
112
124
|
if (!ctxToken) {
|
|
113
|
-
throw new
|
|
125
|
+
throw new EdgeStoreError({
|
|
114
126
|
message: 'Missing edgestore-ctx cookie',
|
|
115
127
|
code: 'UNAUTHORIZED'
|
|
116
128
|
});
|
|
117
129
|
}
|
|
118
130
|
const ctx = await getContext(ctxToken);
|
|
119
|
-
|
|
131
|
+
logger.debug('Decrypted Context', {
|
|
120
132
|
ctx
|
|
121
133
|
});
|
|
122
134
|
const bucket = router.buckets[bucketName];
|
|
123
135
|
if (!bucket) {
|
|
124
|
-
throw new
|
|
136
|
+
throw new EdgeStoreError({
|
|
125
137
|
message: `Bucket ${bucketName} not found`,
|
|
126
138
|
code: 'BAD_REQUEST'
|
|
127
139
|
});
|
|
128
140
|
}
|
|
141
|
+
const parsedInput = await parseBucketInput(bucket, input);
|
|
129
142
|
if (bucket._def.beforeUpload) {
|
|
130
|
-
|
|
143
|
+
logger.debug('Running [beforeUpload]');
|
|
131
144
|
const canUpload = await bucket._def.beforeUpload?.({
|
|
132
145
|
ctx,
|
|
133
|
-
input,
|
|
146
|
+
input: parsedInput,
|
|
134
147
|
fileInfo: {
|
|
135
148
|
size: fileInfo.size,
|
|
136
149
|
type: fileInfo.type,
|
|
@@ -140,86 +153,40 @@ async function requestUpload(params) {
|
|
|
140
153
|
temporary: fileInfo.temporary
|
|
141
154
|
}
|
|
142
155
|
});
|
|
143
|
-
|
|
156
|
+
logger.debug('Finished [beforeUpload]', {
|
|
144
157
|
canUpload
|
|
145
158
|
});
|
|
146
159
|
if (!canUpload) {
|
|
147
|
-
throw new
|
|
160
|
+
throw new EdgeStoreError({
|
|
148
161
|
message: 'Upload not allowed for the current context',
|
|
149
162
|
code: 'UPLOAD_NOT_ALLOWED'
|
|
150
163
|
});
|
|
151
164
|
}
|
|
152
165
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
message: 'Only images are allowed in this bucket',
|
|
158
|
-
details: {
|
|
159
|
-
allowedMimeTypes: imageTypes.IMAGE_MIME_TYPES,
|
|
160
|
-
mimeType: fileInfo.type
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (bucket._def.bucketConfig?.maxSize) {
|
|
166
|
-
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
167
|
-
throw new shared.EdgeStoreError({
|
|
168
|
-
code: 'FILE_TOO_LARGE',
|
|
169
|
-
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
170
|
-
details: {
|
|
171
|
-
maxFileSize: bucket._def.bucketConfig.maxSize,
|
|
172
|
-
fileSize: fileInfo.size
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
if (bucket._def.bucketConfig?.accept) {
|
|
178
|
-
const accept = bucket._def.bucketConfig.accept;
|
|
179
|
-
let accepted = false;
|
|
180
|
-
for (const acceptedMimeType of accept){
|
|
181
|
-
if (acceptedMimeType.endsWith('/*')) {
|
|
182
|
-
const mimeType = acceptedMimeType.replace('/*', '');
|
|
183
|
-
if (fileInfo.type.startsWith(mimeType)) {
|
|
184
|
-
accepted = true;
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
} else if (fileInfo.type === acceptedMimeType) {
|
|
188
|
-
accepted = true;
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
if (!accepted) {
|
|
193
|
-
throw new shared.EdgeStoreError({
|
|
194
|
-
code: 'MIME_TYPE_NOT_ALLOWED',
|
|
195
|
-
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
196
|
-
details: {
|
|
197
|
-
allowedMimeTypes: accept,
|
|
198
|
-
mimeType: fileInfo.type
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
166
|
+
validateFileForBucket({
|
|
167
|
+
bucket,
|
|
168
|
+
fileInfo
|
|
169
|
+
});
|
|
203
170
|
const path = buildPath({
|
|
204
171
|
bucket,
|
|
205
172
|
pathAttrs: {
|
|
206
173
|
ctx,
|
|
207
|
-
input
|
|
174
|
+
input: parsedInput
|
|
208
175
|
}
|
|
209
176
|
});
|
|
210
177
|
const metadata = await bucket._def.metadata?.({
|
|
211
178
|
ctx,
|
|
212
|
-
input
|
|
213
|
-
});
|
|
179
|
+
input: parsedInput
|
|
180
|
+
}) ?? {};
|
|
214
181
|
const isPublic = bucket._def.accessControl === undefined;
|
|
215
182
|
const autoSignedUrls = bucket._def.autoSignedUrls;
|
|
216
|
-
|
|
183
|
+
logger.debug('upload info', {
|
|
217
184
|
path,
|
|
218
185
|
metadata,
|
|
219
186
|
isPublic,
|
|
220
187
|
bucketType: bucket._def.type
|
|
221
188
|
});
|
|
222
|
-
const requestUploadRes = await provider.
|
|
189
|
+
const requestUploadRes = await provider.uploads.request({
|
|
223
190
|
bucketName,
|
|
224
191
|
bucketType: bucket._def.type,
|
|
225
192
|
fileInfo: {
|
|
@@ -231,7 +198,7 @@ async function requestUpload(params) {
|
|
|
231
198
|
autoSignedUrls
|
|
232
199
|
});
|
|
233
200
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
234
|
-
|
|
201
|
+
logger.debug('Finished [requestUpload]');
|
|
235
202
|
return {
|
|
236
203
|
...requestUploadRes,
|
|
237
204
|
size: fileInfo.size,
|
|
@@ -241,37 +208,58 @@ async function requestUpload(params) {
|
|
|
241
208
|
metadata
|
|
242
209
|
};
|
|
243
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
|
+
});
|
|
244
218
|
async function requestUploadParts(params) {
|
|
245
|
-
const { provider, ctxToken, body: { multipart, path } } = params;
|
|
246
|
-
|
|
247
|
-
log.debug('Running [requestUploadParts]', {
|
|
219
|
+
const { provider, ctxToken, logger, body: { multipart, path } } = params;
|
|
220
|
+
logger.debug('Running [requestUploadParts]', {
|
|
248
221
|
multipart,
|
|
249
222
|
path
|
|
250
223
|
});
|
|
251
224
|
if (!ctxToken) {
|
|
252
|
-
throw new
|
|
225
|
+
throw new EdgeStoreError({
|
|
253
226
|
message: 'Missing edgestore-ctx cookie',
|
|
254
227
|
code: 'UNAUTHORIZED'
|
|
255
228
|
});
|
|
256
229
|
}
|
|
257
230
|
await getContext(ctxToken); // just to check if the token is valid
|
|
258
|
-
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({
|
|
259
239
|
multipart,
|
|
260
240
|
path
|
|
261
241
|
});
|
|
262
|
-
|
|
242
|
+
logger.debug('Finished [requestUploadParts]');
|
|
263
243
|
return res;
|
|
264
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
|
+
});
|
|
265
254
|
async function completeMultipartUpload(params) {
|
|
266
|
-
const { provider, router, ctxToken, body: { bucketName, uploadId, key, parts } } = params;
|
|
267
|
-
|
|
268
|
-
log.debug('Running [completeMultipartUpload]', {
|
|
255
|
+
const { provider, router, ctxToken, logger, body: { bucketName, uploadId, key, parts } } = params;
|
|
256
|
+
logger.debug('Running [completeMultipartUpload]', {
|
|
269
257
|
bucketName,
|
|
270
258
|
uploadId,
|
|
271
259
|
key
|
|
272
260
|
});
|
|
273
261
|
if (!ctxToken) {
|
|
274
|
-
throw new
|
|
262
|
+
throw new EdgeStoreError({
|
|
275
263
|
message: 'Missing edgestore-ctx cookie',
|
|
276
264
|
code: 'UNAUTHORIZED'
|
|
277
265
|
});
|
|
@@ -279,28 +267,37 @@ async function completeMultipartUpload(params) {
|
|
|
279
267
|
await getContext(ctxToken); // just to check if the token is valid
|
|
280
268
|
const bucket = router.buckets[bucketName];
|
|
281
269
|
if (!bucket) {
|
|
282
|
-
throw new
|
|
270
|
+
throw new EdgeStoreError({
|
|
283
271
|
message: `Bucket ${bucketName} not found`,
|
|
284
272
|
code: 'BAD_REQUEST'
|
|
285
273
|
});
|
|
286
274
|
}
|
|
287
|
-
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({
|
|
288
283
|
uploadId,
|
|
289
284
|
key,
|
|
290
285
|
parts
|
|
291
286
|
});
|
|
292
|
-
|
|
293
|
-
return res;
|
|
287
|
+
logger.debug('Finished [completeMultipartUpload]');
|
|
294
288
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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]', {
|
|
299
296
|
bucketName,
|
|
300
|
-
|
|
297
|
+
urls
|
|
301
298
|
});
|
|
302
299
|
if (!ctxToken) {
|
|
303
|
-
throw new
|
|
300
|
+
throw new EdgeStoreError({
|
|
304
301
|
message: 'Missing edgestore-ctx cookie',
|
|
305
302
|
code: 'UNAUTHORIZED'
|
|
306
303
|
});
|
|
@@ -308,27 +305,37 @@ async function confirmUpload(params) {
|
|
|
308
305
|
await getContext(ctxToken); // just to check if the token is valid
|
|
309
306
|
const bucket = router.buckets[bucketName];
|
|
310
307
|
if (!bucket) {
|
|
311
|
-
throw new
|
|
308
|
+
throw new EdgeStoreError({
|
|
312
309
|
message: `Bucket ${bucketName} not found`,
|
|
313
310
|
code: 'BAD_REQUEST'
|
|
314
311
|
});
|
|
315
312
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
|
319
323
|
});
|
|
320
|
-
|
|
321
|
-
return
|
|
324
|
+
logger.debug('Finished [confirmUploads]');
|
|
325
|
+
return mapFrontendMutationResult(urls, result);
|
|
322
326
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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]', {
|
|
327
334
|
bucketName,
|
|
328
|
-
|
|
335
|
+
urls
|
|
329
336
|
});
|
|
330
337
|
if (!ctxToken) {
|
|
331
|
-
throw new
|
|
338
|
+
throw new EdgeStoreError({
|
|
332
339
|
message: 'Missing edgestore-ctx cookie',
|
|
333
340
|
code: 'UNAUTHORIZED'
|
|
334
341
|
});
|
|
@@ -336,107 +343,126 @@ async function deleteFile(params) {
|
|
|
336
343
|
const ctx = await getContext(ctxToken);
|
|
337
344
|
const bucket = router.buckets[bucketName];
|
|
338
345
|
if (!bucket) {
|
|
339
|
-
throw new
|
|
346
|
+
throw new EdgeStoreError({
|
|
340
347
|
message: `Bucket ${bucketName} not found`,
|
|
341
348
|
code: 'BAD_REQUEST'
|
|
342
349
|
});
|
|
343
350
|
}
|
|
344
351
|
if (!bucket._def.beforeDelete) {
|
|
345
|
-
throw new
|
|
352
|
+
throw new EdgeStoreError({
|
|
346
353
|
message: 'You need to define beforeDelete if you want to delete files directly from the frontend.',
|
|
347
354
|
code: 'SERVER_ERROR'
|
|
348
355
|
});
|
|
349
356
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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)) {
|
|
393
|
+
throw new EdgeStoreError({
|
|
359
394
|
message: 'Delete not allowed for the current context',
|
|
360
395
|
code: 'DELETE_NOT_ALLOWED'
|
|
361
396
|
});
|
|
362
397
|
}
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
398
|
+
const result = await provider.files.delete({
|
|
399
|
+
bucketName,
|
|
400
|
+
files
|
|
366
401
|
});
|
|
367
|
-
|
|
368
|
-
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
|
+
};
|
|
369
423
|
}
|
|
370
424
|
async function encryptJWT(ctx) {
|
|
371
425
|
const secret = getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
372
426
|
if (!secret) {
|
|
373
|
-
throw new
|
|
427
|
+
throw new EdgeStoreError({
|
|
374
428
|
message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
|
|
375
429
|
code: 'SERVER_ERROR'
|
|
376
430
|
});
|
|
377
431
|
}
|
|
378
432
|
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
379
|
-
return await new
|
|
433
|
+
return await new EncryptJWT({
|
|
434
|
+
ctx
|
|
435
|
+
}).setProtectedHeader({
|
|
380
436
|
alg: 'dir',
|
|
381
437
|
enc: 'A256GCM'
|
|
382
|
-
}).setIssuedAt().setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE).setJti(
|
|
438
|
+
}).setIssuedAt().setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE).setJti(crypto.randomUUID()).encrypt(encryptionSecret);
|
|
383
439
|
}
|
|
384
440
|
async function decryptJWT(token) {
|
|
385
441
|
const secret = getEnv('EDGE_STORE_JWT_SECRET') ?? getEnv('EDGE_STORE_SECRET_KEY');
|
|
386
442
|
if (!secret) {
|
|
387
|
-
throw new
|
|
443
|
+
throw new EdgeStoreError({
|
|
388
444
|
message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
|
|
389
445
|
code: 'SERVER_ERROR'
|
|
390
446
|
});
|
|
391
447
|
}
|
|
392
448
|
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
393
|
-
const { payload } = await
|
|
449
|
+
const { payload } = await jwtDecrypt(token, encryptionSecret, {
|
|
394
450
|
clockTolerance: 15
|
|
395
451
|
});
|
|
396
|
-
|
|
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;
|
|
397
463
|
}
|
|
398
464
|
async function getDerivedEncryptionKey(secret) {
|
|
399
|
-
return await hkdf
|
|
400
|
-
}
|
|
401
|
-
function buildPath(params) {
|
|
402
|
-
const { bucket } = params;
|
|
403
|
-
const pathParams = bucket._def.path;
|
|
404
|
-
const path = pathParams.map((param)=>{
|
|
405
|
-
const paramEntries = Object.entries(param);
|
|
406
|
-
if (paramEntries[0] === undefined) {
|
|
407
|
-
throw new shared.EdgeStoreError({
|
|
408
|
-
message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
|
|
409
|
-
code: 'SERVER_ERROR'
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
const [key, value] = paramEntries[0];
|
|
413
|
-
// this is a string like: "ctx.xxx" or "input.yyy.zzz"
|
|
414
|
-
const currParamVal = value().split('.').reduce((acc2, key)=>{
|
|
415
|
-
if (acc2[key] === undefined) {
|
|
416
|
-
throw new shared.EdgeStoreError({
|
|
417
|
-
message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
|
|
418
|
-
code: 'BAD_REQUEST'
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
return acc2[key];
|
|
422
|
-
}, params.pathAttrs);
|
|
423
|
-
return {
|
|
424
|
-
key,
|
|
425
|
-
value: currParamVal
|
|
426
|
-
};
|
|
427
|
-
});
|
|
428
|
-
return path;
|
|
429
|
-
}
|
|
430
|
-
function parsePath(path) {
|
|
431
|
-
const parsedPath = path.reduce((acc, curr)=>{
|
|
432
|
-
acc[curr.key] = curr.value;
|
|
433
|
-
return acc;
|
|
434
|
-
}, {});
|
|
435
|
-
const pathOrder = path.map((p)=>p.key);
|
|
436
|
-
return {
|
|
437
|
-
parsedPath,
|
|
438
|
-
pathOrder
|
|
439
|
-
};
|
|
465
|
+
return await hkdf('sha256', secret, '', 'EdgeStore Generated Encryption Key', 32);
|
|
440
466
|
}
|
|
441
467
|
async function getContext(token) {
|
|
442
468
|
return await decryptJWT(token);
|
|
@@ -457,28 +483,5 @@ async function getContext(token) {
|
|
|
457
483
|
}
|
|
458
484
|
return url;
|
|
459
485
|
}
|
|
460
|
-
function getEnv(key) {
|
|
461
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
462
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
463
|
-
return process.env[key] ?? undefined?.[key];
|
|
464
|
-
}
|
|
465
|
-
// @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
466
|
-
return undefined?.[key];
|
|
467
|
-
}
|
|
468
|
-
function isDev() {
|
|
469
|
-
return process?.env?.NODE_ENV === 'development' || // @ts-expect-error - In Vite/Astro, the env variables are available on `import.meta`.
|
|
470
|
-
undefined?.DEV;
|
|
471
|
-
}
|
|
472
486
|
|
|
473
|
-
|
|
474
|
-
exports.completeMultipartUpload = completeMultipartUpload;
|
|
475
|
-
exports.confirmUpload = confirmUpload;
|
|
476
|
-
exports.deleteFile = deleteFile;
|
|
477
|
-
exports.fetchProxyFile = fetchProxyFile;
|
|
478
|
-
exports.getCookieConfig = getCookieConfig;
|
|
479
|
-
exports.getEnv = getEnv;
|
|
480
|
-
exports.init = init;
|
|
481
|
-
exports.isDev = isDev;
|
|
482
|
-
exports.parsePath = parsePath;
|
|
483
|
-
exports.requestUpload = requestUpload;
|
|
484
|
-
exports.requestUploadParts = requestUploadParts;
|
|
487
|
+
export { completeMultipartUpload, completeMultipartUploadBodySchema, confirmUploads, confirmUploadsBodySchema, deleteFiles, deleteFilesBodySchema, fetchProxyFile, getCookieConfig, init, requestUpload, requestUploadBodySchema, requestUploadParts, requestUploadPartsBodySchema };
|
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type LogLevel } from '../../libs/logger';
|
|
3
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
|
+
import { type LogLevel } from '../../libs/logger.js';
|
|
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"}
|