@edgestore/server 0.1.5-alpha.0 → 0.1.5-alpha.10

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.
Files changed (71) hide show
  1. package/adapters/express/index.d.ts +1 -0
  2. package/adapters/express/index.js +1 -0
  3. package/dist/adapters/express/index.d.ts +20 -0
  4. package/dist/adapters/express/index.d.ts.map +1 -0
  5. package/dist/adapters/express/index.js +117 -0
  6. package/dist/adapters/express/index.mjs +113 -0
  7. package/dist/adapters/next/app/index.d.ts +5 -3
  8. package/dist/adapters/next/app/index.d.ts.map +1 -1
  9. package/dist/adapters/next/app/index.js +45 -16
  10. package/dist/adapters/next/app/index.mjs +44 -15
  11. package/dist/adapters/next/pages/index.d.ts +5 -3
  12. package/dist/adapters/next/pages/index.d.ts.map +1 -1
  13. package/dist/adapters/next/pages/index.js +37 -13
  14. package/dist/adapters/next/pages/index.mjs +36 -12
  15. package/dist/adapters/shared.d.ts +19 -1
  16. package/dist/adapters/shared.d.ts.map +1 -1
  17. package/dist/core/client/index.d.ts +116 -4
  18. package/dist/core/client/index.d.ts.map +1 -1
  19. package/dist/core/index.d.ts +2 -0
  20. package/dist/core/index.d.ts.map +1 -1
  21. package/dist/core/index.js +109 -34
  22. package/dist/core/index.mjs +110 -36
  23. package/dist/core/sdk/index.d.ts.map +1 -1
  24. package/dist/{index-f33a00fb.js → index-0c401ce1.js} +38 -4
  25. package/dist/{index-50ab9e08.js → index-23d1ede9.mjs} +42 -7
  26. package/dist/{index-30a3741e.mjs → index-a2e7ca9e.js} +49 -4
  27. package/dist/libs/errors/EdgeStoreApiClientError.d.ts +8 -0
  28. package/dist/libs/errors/EdgeStoreApiClientError.d.ts.map +1 -0
  29. package/dist/libs/errors/EdgeStoreCredentialsError.d.ts.map +1 -1
  30. package/dist/libs/errors/EdgeStoreError.d.ts +36 -4
  31. package/dist/libs/errors/EdgeStoreError.d.ts.map +1 -1
  32. package/dist/libs/logger.d.ts +13 -0
  33. package/dist/libs/logger.d.ts.map +1 -0
  34. package/dist/logger-7ea2248c.mjs +40 -0
  35. package/dist/logger-b530a3e1.js +42 -0
  36. package/dist/logger-e0066db9.js +33 -0
  37. package/dist/providers/aws/index.js +1 -1
  38. package/dist/providers/aws/index.mjs +1 -1
  39. package/dist/providers/azure/index.d.ts +20 -0
  40. package/dist/providers/azure/index.d.ts.map +1 -0
  41. package/dist/providers/azure/index.js +61 -0
  42. package/dist/providers/azure/index.mjs +57 -0
  43. package/dist/providers/edgestore/index.d.ts.map +1 -1
  44. package/dist/providers/edgestore/index.js +10 -3
  45. package/dist/providers/edgestore/index.mjs +10 -3
  46. package/dist/providers/types.d.ts +1 -0
  47. package/dist/providers/types.d.ts.map +1 -1
  48. package/dist/{shared-306c1af2.js → shared-06cb0d86.js} +150 -61
  49. package/dist/{shared-9fad0d51.js → shared-d474acc6.js} +114 -46
  50. package/dist/{shared-6dea9e91.mjs → shared-d7ea66fb.mjs} +141 -52
  51. package/dist/types.d.ts +6 -0
  52. package/dist/types.d.ts.map +1 -1
  53. package/package.json +17 -2
  54. package/providers/azure/index.d.ts +1 -0
  55. package/providers/azure/index.js +1 -0
  56. package/src/adapters/express/index.ts +164 -0
  57. package/src/adapters/next/app/index.ts +63 -20
  58. package/src/adapters/next/pages/index.ts +52 -15
  59. package/src/adapters/shared.ts +142 -40
  60. package/src/core/client/index.ts +233 -51
  61. package/src/core/index.ts +6 -0
  62. package/src/core/sdk/index.ts +7 -1
  63. package/src/libs/errors/EdgeStoreApiClientError.ts +14 -0
  64. package/src/libs/errors/EdgeStoreCredentialsError.ts +1 -2
  65. package/src/libs/errors/EdgeStoreError.ts +74 -7
  66. package/src/libs/logger.ts +44 -0
  67. package/src/providers/aws/index.ts +1 -1
  68. package/src/providers/azure/index.ts +89 -0
  69. package/src/providers/edgestore/index.ts +9 -2
  70. package/src/providers/types.ts +1 -0
  71. package/src/types.ts +8 -0
@@ -2,22 +2,7 @@ import { hkdf } from '@panva/hkdf';
2
2
  import { serialize } from 'cookie';
3
3
  import { EncryptJWT, jwtDecrypt } from 'jose';
4
4
  import { v4 } from 'uuid';
5
- import { _ } from '@swc/helpers/_/_define_property';
6
-
7
- const EDGE_STORE_ERROR_CODES = {
8
- BAD_REQUEST: 400,
9
- UNAUTHORIZED: 401
10
- };
11
- class EdgeStoreError extends Error {
12
- constructor(opts){
13
- super(opts.message);
14
- _(this, "cause", void 0);
15
- _(this, "code", void 0);
16
- this.name = 'EdgeStoreError';
17
- this.code = opts.code;
18
- this.cause = opts.cause;
19
- }
20
- }
5
+ import { E as EdgeStoreError } from './index-23d1ede9.mjs';
21
6
 
22
7
  const IMAGE_MIME_TYPES = [
23
8
  'image/jpeg',
@@ -33,7 +18,11 @@ const IMAGE_MIME_TYPES = [
33
18
  // TODO: change it to 1 hour when we have a way to refresh the token
34
19
  const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
35
20
  async function init(params) {
21
+ const log = globalThis._EDGE_STORE_LOGGER;
36
22
  const { ctx, provider, router } = params;
23
+ log.debug('Running [init]', {
24
+ ctx
25
+ });
37
26
  const ctxToken = await encryptJWT(ctx);
38
27
  const { token } = await provider.init({
39
28
  ctx,
@@ -52,6 +41,12 @@ async function init(params) {
52
41
  }));
53
42
  }
54
43
  const baseUrl = await provider.getBaseUrl();
44
+ log.debug('Finished [init]', {
45
+ ctx,
46
+ newCookies,
47
+ token,
48
+ baseUrl
49
+ });
55
50
  return {
56
51
  newCookies,
57
52
  token,
@@ -60,6 +55,12 @@ async function init(params) {
60
55
  }
61
56
  async function requestUpload(params) {
62
57
  const { provider, router, ctxToken, body: { bucketName, input, fileInfo } } = params;
58
+ const log = globalThis._EDGE_STORE_LOGGER;
59
+ log.debug('Running [requestUpload]', {
60
+ bucketName,
61
+ input,
62
+ fileInfo
63
+ });
63
64
  if (!ctxToken) {
64
65
  throw new EdgeStoreError({
65
66
  message: 'Missing edgestore-ctx cookie',
@@ -67,11 +68,18 @@ async function requestUpload(params) {
67
68
  });
68
69
  }
69
70
  const ctx = await getContext(ctxToken);
71
+ log.debug('Decrypted Context', {
72
+ ctx
73
+ });
70
74
  const bucket = router.buckets[bucketName];
71
75
  if (!bucket) {
72
- throw new Error(`Bucket ${bucketName} not found`);
76
+ throw new EdgeStoreError({
77
+ message: `Bucket ${bucketName} not found`,
78
+ code: 'BAD_REQUEST'
79
+ });
73
80
  }
74
81
  if (bucket._def.beforeUpload) {
82
+ log.debug('Running [beforeUpload]');
75
83
  const canUpload = await bucket._def.beforeUpload?.({
76
84
  ctx,
77
85
  input,
@@ -84,23 +92,37 @@ async function requestUpload(params) {
84
92
  temporary: fileInfo.temporary
85
93
  }
86
94
  });
95
+ log.debug('Finished [beforeUpload]', {
96
+ canUpload
97
+ });
87
98
  if (!canUpload) {
88
- throw new Error('Upload not allowed');
99
+ throw new EdgeStoreError({
100
+ message: 'Upload not allowed for the current context',
101
+ code: 'UPLOAD_NOT_ALLOWED'
102
+ });
89
103
  }
90
104
  }
91
105
  if (bucket._def.type === 'IMAGE') {
92
106
  if (!IMAGE_MIME_TYPES.includes(fileInfo.type)) {
93
107
  throw new EdgeStoreError({
94
- code: 'BAD_REQUEST',
95
- message: 'Only images are allowed in this bucket'
108
+ code: 'MIME_TYPE_NOT_ALLOWED',
109
+ message: 'Only images are allowed in this bucket',
110
+ details: {
111
+ allowedMimeTypes: IMAGE_MIME_TYPES,
112
+ mimeType: fileInfo.type
113
+ }
96
114
  });
97
115
  }
98
116
  }
99
117
  if (bucket._def.bucketConfig?.maxSize) {
100
118
  if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
101
119
  throw new EdgeStoreError({
102
- code: 'BAD_REQUEST',
103
- message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`
120
+ code: 'FILE_TOO_LARGE',
121
+ message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
122
+ details: {
123
+ maxFileSize: bucket._def.bucketConfig.maxSize,
124
+ fileSize: fileInfo.size
125
+ }
104
126
  });
105
127
  }
106
128
  }
@@ -121,8 +143,12 @@ async function requestUpload(params) {
121
143
  }
122
144
  if (!accepted) {
123
145
  throw new EdgeStoreError({
124
- code: 'BAD_REQUEST',
125
- message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`
146
+ code: 'MIME_TYPE_NOT_ALLOWED',
147
+ message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
148
+ details: {
149
+ allowedMimeTypes: accept,
150
+ mimeType: fileInfo.type
151
+ }
126
152
  });
127
153
  }
128
154
  }
@@ -139,6 +165,12 @@ async function requestUpload(params) {
139
165
  input
140
166
  });
141
167
  const isPublic = bucket._def.accessControl === undefined;
168
+ log.debug('upload info', {
169
+ path,
170
+ metadata,
171
+ isPublic,
172
+ bucketType: bucket._def.type
173
+ });
142
174
  const requestUploadRes = await provider.requestUpload({
143
175
  bucketName,
144
176
  bucketType: bucket._def.type,
@@ -149,11 +181,8 @@ async function requestUpload(params) {
149
181
  metadata
150
182
  }
151
183
  });
152
- const parsedPath = path.reduce((acc, curr)=>{
153
- acc[curr.key] = curr.value;
154
- return acc;
155
- }, {});
156
- const pathOrder = path.map((p)=>p.key);
184
+ const { parsedPath, pathOrder } = parsePath(path);
185
+ log.debug('Finished [requestUpload]');
157
186
  return {
158
187
  ...requestUploadRes,
159
188
  size: fileInfo.size,
@@ -164,7 +193,12 @@ async function requestUpload(params) {
164
193
  };
165
194
  }
166
195
  async function requestUploadParts(params) {
167
- const { provider, router, ctxToken, body: { multipart, path } } = params;
196
+ const { provider, ctxToken, body: { multipart, path } } = params;
197
+ const log = globalThis._EDGE_STORE_LOGGER;
198
+ log.debug('Running [requestUploadParts]', {
199
+ multipart,
200
+ path
201
+ });
168
202
  if (!ctxToken) {
169
203
  throw new EdgeStoreError({
170
204
  message: 'Missing edgestore-ctx cookie',
@@ -172,17 +206,21 @@ async function requestUploadParts(params) {
172
206
  });
173
207
  }
174
208
  await getContext(ctxToken); // just to check if the token is valid
175
- const bucket = router.buckets[multipart.uploadId];
176
- if (!bucket) {
177
- throw new Error(`Bucket ${multipart.uploadId} not found`);
178
- }
179
- return await provider.requestUploadParts({
209
+ const res = await provider.requestUploadParts({
180
210
  multipart,
181
211
  path
182
212
  });
213
+ log.debug('Finished [requestUploadParts]');
214
+ return res;
183
215
  }
184
216
  async function completeMultipartUpload(params) {
185
217
  const { provider, router, ctxToken, body: { bucketName, uploadId, key, parts } } = params;
218
+ const log = globalThis._EDGE_STORE_LOGGER;
219
+ log.debug('Running [completeMultipartUpload]', {
220
+ bucketName,
221
+ uploadId,
222
+ key
223
+ });
186
224
  if (!ctxToken) {
187
225
  throw new EdgeStoreError({
188
226
  message: 'Missing edgestore-ctx cookie',
@@ -192,16 +230,26 @@ async function completeMultipartUpload(params) {
192
230
  await getContext(ctxToken); // just to check if the token is valid
193
231
  const bucket = router.buckets[bucketName];
194
232
  if (!bucket) {
195
- throw new Error(`Bucket ${bucketName} not found`);
233
+ throw new EdgeStoreError({
234
+ message: `Bucket ${bucketName} not found`,
235
+ code: 'BAD_REQUEST'
236
+ });
196
237
  }
197
- return await provider.completeMultipartUpload({
238
+ const res = await provider.completeMultipartUpload({
198
239
  uploadId,
199
240
  key,
200
241
  parts
201
242
  });
243
+ log.debug('Finished [completeMultipartUpload]');
244
+ return res;
202
245
  }
203
246
  async function confirmUpload(params) {
204
247
  const { provider, router, ctxToken, body: { bucketName, url } } = params;
248
+ const log = globalThis._EDGE_STORE_LOGGER;
249
+ log.debug('Running [confirmUpload]', {
250
+ bucketName,
251
+ url
252
+ });
205
253
  if (!ctxToken) {
206
254
  throw new EdgeStoreError({
207
255
  message: 'Missing edgestore-ctx cookie',
@@ -211,15 +259,25 @@ async function confirmUpload(params) {
211
259
  await getContext(ctxToken); // just to check if the token is valid
212
260
  const bucket = router.buckets[bucketName];
213
261
  if (!bucket) {
214
- throw new Error(`Bucket ${bucketName} not found`);
262
+ throw new EdgeStoreError({
263
+ message: `Bucket ${bucketName} not found`,
264
+ code: 'BAD_REQUEST'
265
+ });
215
266
  }
216
- return await provider.confirmUpload({
267
+ const res = await provider.confirmUpload({
217
268
  bucket,
218
269
  url: unproxyUrl(url)
219
270
  });
271
+ log.debug('Finished [confirmUpload]');
272
+ return res;
220
273
  }
221
274
  async function deleteFile(params) {
222
275
  const { provider, router, ctxToken, body: { bucketName, url } } = params;
276
+ const log = globalThis._EDGE_STORE_LOGGER;
277
+ log.debug('Running [deleteFile]', {
278
+ bucketName,
279
+ url
280
+ });
223
281
  if (!ctxToken) {
224
282
  throw new EdgeStoreError({
225
283
  message: 'Missing edgestore-ctx cookie',
@@ -229,30 +287,44 @@ async function deleteFile(params) {
229
287
  const ctx = await getContext(ctxToken);
230
288
  const bucket = router.buckets[bucketName];
231
289
  if (!bucket) {
232
- throw new Error(`Bucket ${bucketName} not found`);
290
+ throw new EdgeStoreError({
291
+ message: `Bucket ${bucketName} not found`,
292
+ code: 'BAD_REQUEST'
293
+ });
233
294
  }
234
295
  if (!bucket._def.beforeDelete) {
235
- throw new Error('You need to define beforeDelete if you want to delete files directly from the frontend.');
296
+ throw new EdgeStoreError({
297
+ message: 'You need to define beforeDelete if you want to delete files directly from the frontend.',
298
+ code: 'SERVER_ERROR'
299
+ });
236
300
  }
237
301
  const fileInfo = await provider.getFile({
238
- url
302
+ url: unproxyUrl(url)
239
303
  });
240
304
  const canDelete = await bucket._def.beforeDelete({
241
305
  ctx,
242
306
  fileInfo
243
307
  });
244
308
  if (!canDelete) {
245
- throw new Error('Delete not allowed');
309
+ throw new EdgeStoreError({
310
+ message: 'Delete not allowed for the current context',
311
+ code: 'DELETE_NOT_ALLOWED'
312
+ });
246
313
  }
247
- return await provider.deleteFile({
314
+ const res = await provider.deleteFile({
248
315
  bucket,
249
316
  url: unproxyUrl(url)
250
317
  });
318
+ log.debug('Finished [deleteFile]');
319
+ return res;
251
320
  }
252
321
  async function encryptJWT(ctx) {
253
322
  const secret = process.env.EDGE_STORE_JWT_SECRET ?? process.env.EDGE_STORE_SECRET_KEY;
254
323
  if (!secret) {
255
- throw new Error('EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined');
324
+ throw new EdgeStoreError({
325
+ message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
326
+ code: 'SERVER_ERROR'
327
+ });
256
328
  }
257
329
  const encryptionSecret = await getDerivedEncryptionKey(secret);
258
330
  return await new EncryptJWT(ctx).setProtectedHeader({
@@ -263,7 +335,10 @@ async function encryptJWT(ctx) {
263
335
  async function decryptJWT(token) {
264
336
  const secret = process.env.EDGE_STORE_JWT_SECRET ?? process.env.EDGE_STORE_SECRET_KEY;
265
337
  if (!secret) {
266
- throw new Error('EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not set');
338
+ throw new EdgeStoreError({
339
+ message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
340
+ code: 'SERVER_ERROR'
341
+ });
267
342
  }
268
343
  const encryptionSecret = await getDerivedEncryptionKey(secret);
269
344
  const { payload } = await jwtDecrypt(token, encryptionSecret, {
@@ -280,13 +355,19 @@ function buildPath(params) {
280
355
  const path = pathParams.map((param)=>{
281
356
  const paramEntries = Object.entries(param);
282
357
  if (paramEntries[0] === undefined) {
283
- throw new Error('Missing path param');
358
+ throw new EdgeStoreError({
359
+ message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
360
+ code: 'SERVER_ERROR'
361
+ });
284
362
  }
285
363
  const [key, value] = paramEntries[0];
286
364
  // this is a string like: "ctx.xxx" or "input.yyy.zzz"
287
365
  const currParamVal = value().split('.').reduce((acc2, key)=>{
288
366
  if (acc2[key] === undefined) {
289
- throw new Error(`Missing key ${key} in ${JSON.stringify(acc2)}`);
367
+ throw new EdgeStoreError({
368
+ message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
369
+ code: 'BAD_REQUEST'
370
+ });
290
371
  }
291
372
  return acc2[key];
292
373
  }, params.pathAttrs);
@@ -297,10 +378,18 @@ function buildPath(params) {
297
378
  });
298
379
  return path;
299
380
  }
381
+ function parsePath(path) {
382
+ const parsedPath = path.reduce((acc, curr)=>{
383
+ acc[curr.key] = curr.value;
384
+ return acc;
385
+ }, {});
386
+ const pathOrder = path.map((p)=>p.key);
387
+ return {
388
+ parsedPath,
389
+ pathOrder
390
+ };
391
+ }
300
392
  async function getContext(token) {
301
- if (!token) {
302
- throw new Error('No token');
303
- }
304
393
  return await decryptJWT(token);
305
394
  }
306
395
  /**
@@ -320,4 +409,4 @@ async function getContext(token) {
320
409
  return url;
321
410
  }
322
411
 
323
- export { EdgeStoreError as E, requestUploadParts as a, confirmUpload as b, completeMultipartUpload as c, deleteFile as d, EDGE_STORE_ERROR_CODES as e, init as i, requestUpload as r };
412
+ export { requestUploadParts as a, buildPath as b, completeMultipartUpload as c, confirmUpload as d, deleteFile as e, init as i, parsePath as p, requestUpload as r };
package/dist/types.d.ts CHANGED
@@ -35,6 +35,12 @@ export type ThenArg<TType> = TType extends PromiseLike<infer U> ? ThenArg<U> : T
35
35
  export type Simplify<TType> = TType extends any[] | Date ? TType : {
36
36
  [K in keyof TType]: TType[K];
37
37
  };
38
+ /**
39
+ * @internal
40
+ */
41
+ export type Prettify<TType> = {
42
+ [K in keyof TType]: TType[K];
43
+ } & {};
38
44
  /**
39
45
  * @public
40
46
  */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,KAAK,IAAI,KAAK,CAAC;AAEpC,MAAM,MAAM,aAAa,CAAC,KAAK,EAAE,KAAK,SAAS,MAAM,KAAK,IAAI,IAAI,CAChE,KAAK,EACL,KAAK,CACN,GACC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAE9B,MAAM,MAAM,aAAa,CAAC,KAAK,IAAI;KAChC,CAAC,IAAI,MAAM,KAAK,GAAG,SAAS,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load files with ease from React/Next.js",
5
5
  "homepage": "https://edgestore.dev",
6
6
  "repository": "https://github.com/edgestorejs/edgestore.git",
@@ -42,6 +42,11 @@
42
42
  "require": "./dist/adapters/index.js",
43
43
  "default": "./dist/adapters/index.js"
44
44
  },
45
+ "./adapters/express": {
46
+ "import": "./dist/adapters/express/index.mjs",
47
+ "require": "./dist/adapters/express/index.js",
48
+ "default": "./dist/adapters/express/index.js"
49
+ },
45
50
  "./adapters/next/pages": {
46
51
  "import": "./dist/adapters/next/pages/index.mjs",
47
52
  "require": "./dist/adapters/next/pages/index.js",
@@ -57,6 +62,11 @@
57
62
  "require": "./dist/providers/aws/index.js",
58
63
  "default": "./dist/providers/aws/index.js"
59
64
  },
65
+ "./providers/azure": {
66
+ "import": "./dist/providers/azure/index.mjs",
67
+ "require": "./dist/providers/azure/index.js",
68
+ "default": "./dist/providers/azure/index.js"
69
+ },
60
70
  "./providers/edgestore": {
61
71
  "import": "./dist/providers/edgestore/index.mjs",
62
72
  "require": "./dist/providers/edgestore/index.js",
@@ -92,6 +102,7 @@
92
102
  "peerDependencies": {
93
103
  "@aws-sdk/client-s3": ">=3.0.0",
94
104
  "@aws-sdk/s3-request-presigner": ">=3.0.0",
105
+ "@azure/storage-blob": "^12.17.0",
95
106
  "zod": ">=3.0.0"
96
107
  },
97
108
  "peerDependenciesMeta": {
@@ -103,11 +114,15 @@
103
114
  },
104
115
  "@aws-sdk/s3-request-presigner": {
105
116
  "optional": true
117
+ },
118
+ "@azure/storage-blob": {
119
+ "optional": true
106
120
  }
107
121
  },
108
122
  "devDependencies": {
109
123
  "@aws-sdk/client-s3": "^3.420.0",
110
124
  "@aws-sdk/s3-request-presigner": "^3.420.0",
125
+ "@azure/storage-blob": "^12.17.0",
111
126
  "@types/cookie": "^0.5.1",
112
127
  "@types/node": "^18.11.18",
113
128
  "@types/uuid": "^9.0.1",
@@ -117,5 +132,5 @@
117
132
  "typescript": "^5.1.6",
118
133
  "zod": "^3.21.4"
119
134
  },
120
- "gitHead": "bea31f57147499993eaf25c86f24c211dc86dd91"
135
+ "gitHead": "6cf9f5bbea5c1cf990b8b8caa70bc4341e046e59"
121
136
  }
@@ -0,0 +1 @@
1
+ export * from '../../dist/providers/azure';
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/providers/azure');
@@ -0,0 +1,164 @@
1
+ import { type Request, type Response } from 'express';
2
+ import { type EdgeStoreRouter } from '../../core/internals/bucketBuilder';
3
+ import EdgeStoreError, {
4
+ EDGE_STORE_ERROR_CODES,
5
+ type EdgeStoreErrorCodeKey,
6
+ } from '../../libs/errors/EdgeStoreError';
7
+ import Logger, { type LogLevel } from '../../libs/logger';
8
+ import { EdgeStoreProvider } from '../../providers/edgestore';
9
+ import { type Provider } from '../../providers/types';
10
+ import { type MaybePromise } from '../../types';
11
+ import {
12
+ completeMultipartUpload,
13
+ confirmUpload,
14
+ deleteFile,
15
+ init,
16
+ requestUpload,
17
+ requestUploadParts,
18
+ type CompleteMultipartUploadBody,
19
+ type ConfirmUploadBody,
20
+ type DeleteFileBody,
21
+ type RequestUploadBody,
22
+ type RequestUploadPartsParams,
23
+ } from '../shared';
24
+
25
+ export type CreateContextOptions = {
26
+ req: Request;
27
+ res: Response;
28
+ };
29
+
30
+ export type Config<TCtx> = {
31
+ provider?: Provider;
32
+ router: EdgeStoreRouter<TCtx>;
33
+ logLevel?: LogLevel;
34
+ } & (TCtx extends Record<string, never>
35
+ ? object
36
+ : {
37
+ provider?: Provider;
38
+ router: EdgeStoreRouter<TCtx>;
39
+ createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
40
+ });
41
+
42
+ declare const globalThis: {
43
+ _EDGE_STORE_LOGGER: Logger;
44
+ };
45
+
46
+ export function createEdgeStoreExpressHandler<TCtx>(config: Config<TCtx>) {
47
+ const { provider = EdgeStoreProvider() } = config;
48
+ const log = new Logger(config.logLevel);
49
+ globalThis._EDGE_STORE_LOGGER = log;
50
+ log.debug('Creating Edge Store Express handler');
51
+
52
+ return async (req: Request, res: Response) => {
53
+ try {
54
+ if (req.url?.includes?.('/health')) {
55
+ res.send('OK');
56
+ } else if (req.url?.includes?.('/init')) {
57
+ let ctx = {} as TCtx;
58
+ try {
59
+ ctx =
60
+ 'createContext' in config
61
+ ? await config.createContext({ req, res })
62
+ : ({} as TCtx);
63
+ } catch (err) {
64
+ throw new EdgeStoreError({
65
+ message: 'Error creating context',
66
+ code: 'CREATE_CONTEXT_ERROR',
67
+ cause: err instanceof Error ? err : undefined,
68
+ });
69
+ }
70
+ const { newCookies, token, baseUrl } = await init({
71
+ ctx,
72
+ provider,
73
+ router: config.router,
74
+ });
75
+ res.setHeader('Set-Cookie', newCookies);
76
+ res.json({
77
+ token,
78
+ baseUrl,
79
+ });
80
+ } else if (req.url?.includes?.('/request-upload')) {
81
+ res.json(
82
+ await requestUpload({
83
+ provider,
84
+ router: config.router,
85
+ body: req.body as RequestUploadBody,
86
+ ctxToken: req.cookies['edgestore-ctx'],
87
+ }),
88
+ );
89
+ } else if (req.url?.includes?.('/request-upload-parts')) {
90
+ res.json(
91
+ await requestUploadParts({
92
+ provider,
93
+ router: config.router,
94
+ body: req.body as RequestUploadPartsParams,
95
+ ctxToken: req.cookies['edgestore-ctx'],
96
+ }),
97
+ );
98
+ } else if (req.url?.includes?.('/complete-multipart-upload')) {
99
+ await completeMultipartUpload({
100
+ provider,
101
+ router: config.router,
102
+ body: req.body as CompleteMultipartUploadBody,
103
+ ctxToken: req.cookies['edgestore-ctx'],
104
+ });
105
+ res.status(200).end();
106
+ } else if (req.url?.includes?.('/confirm-upload')) {
107
+ res.json(
108
+ await confirmUpload({
109
+ provider,
110
+ router: config.router,
111
+ body: req.body as ConfirmUploadBody,
112
+ ctxToken: req.cookies['edgestore-ctx'],
113
+ }),
114
+ );
115
+ } else if (req.url?.includes?.('/delete-file')) {
116
+ res.json(
117
+ await deleteFile({
118
+ provider,
119
+ router: config.router,
120
+ body: req.body as DeleteFileBody,
121
+ ctxToken: req.cookies['edgestore-ctx'],
122
+ }),
123
+ );
124
+ } else if (req.url?.includes?.('/proxy-file')) {
125
+ const { url } = req.query;
126
+ if (typeof url === 'string') {
127
+ const proxyRes = await fetch(url, {
128
+ headers: {
129
+ cookie: req.headers.cookie ?? '',
130
+ },
131
+ });
132
+
133
+ const data = await proxyRes.arrayBuffer();
134
+ res.setHeader(
135
+ 'Content-Type',
136
+ proxyRes.headers.get('Content-Type') ?? 'application/octet-stream',
137
+ );
138
+
139
+ res.end(Buffer.from(data));
140
+ } else {
141
+ res.status(400).end();
142
+ }
143
+ } else {
144
+ res.status(404).end();
145
+ }
146
+ } catch (err) {
147
+ if (err instanceof EdgeStoreError) {
148
+ log[err.level](err.formattedMessage());
149
+ if (err.cause) log[err.level](err.cause);
150
+ res
151
+ .status(EDGE_STORE_ERROR_CODES[err.code as EdgeStoreErrorCodeKey])
152
+ .json(err.formattedJson());
153
+ } else {
154
+ log.error(err);
155
+ res.status(500).send(
156
+ new EdgeStoreError({
157
+ message: 'Internal Server Error',
158
+ code: 'SERVER_ERROR',
159
+ }).formattedJson(),
160
+ );
161
+ }
162
+ }
163
+ };
164
+ }