@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
@@ -4,22 +4,7 @@ var hkdf = require('@panva/hkdf');
4
4
  var cookie = require('cookie');
5
5
  var jose = require('jose');
6
6
  var uuid = require('uuid');
7
- var _define_property = require('@swc/helpers/_/_define_property');
8
-
9
- const EDGE_STORE_ERROR_CODES = {
10
- BAD_REQUEST: 400,
11
- UNAUTHORIZED: 401
12
- };
13
- class EdgeStoreError extends Error {
14
- constructor(opts){
15
- super(opts.message);
16
- _define_property._(this, "cause", void 0);
17
- _define_property._(this, "code", void 0);
18
- this.name = 'EdgeStoreError';
19
- this.code = opts.code;
20
- this.cause = opts.cause;
21
- }
22
- }
7
+ var index = require('./index-a2e7ca9e.js');
23
8
 
24
9
  const IMAGE_MIME_TYPES = [
25
10
  'image/jpeg',
@@ -35,7 +20,11 @@ const IMAGE_MIME_TYPES = [
35
20
  // TODO: change it to 1 hour when we have a way to refresh the token
36
21
  const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
37
22
  async function init(params) {
23
+ const log = globalThis._EDGE_STORE_LOGGER;
38
24
  const { ctx, provider, router } = params;
25
+ log.debug('Running [init]', {
26
+ ctx
27
+ });
39
28
  const ctxToken = await encryptJWT(ctx);
40
29
  const { token } = await provider.init({
41
30
  ctx,
@@ -54,6 +43,12 @@ async function init(params) {
54
43
  }));
55
44
  }
56
45
  const baseUrl = await provider.getBaseUrl();
46
+ log.debug('Finished [init]', {
47
+ ctx,
48
+ newCookies,
49
+ token,
50
+ baseUrl
51
+ });
57
52
  return {
58
53
  newCookies,
59
54
  token,
@@ -62,18 +57,31 @@ async function init(params) {
62
57
  }
63
58
  async function requestUpload(params) {
64
59
  const { provider, router, ctxToken, body: { bucketName, input, fileInfo } } = params;
60
+ const log = globalThis._EDGE_STORE_LOGGER;
61
+ log.debug('Running [requestUpload]', {
62
+ bucketName,
63
+ input,
64
+ fileInfo
65
+ });
65
66
  if (!ctxToken) {
66
- throw new EdgeStoreError({
67
+ throw new index.EdgeStoreError({
67
68
  message: 'Missing edgestore-ctx cookie',
68
69
  code: 'UNAUTHORIZED'
69
70
  });
70
71
  }
71
72
  const ctx = await getContext(ctxToken);
73
+ log.debug('Decrypted Context', {
74
+ ctx
75
+ });
72
76
  const bucket = router.buckets[bucketName];
73
77
  if (!bucket) {
74
- throw new Error(`Bucket ${bucketName} not found`);
78
+ throw new index.EdgeStoreError({
79
+ message: `Bucket ${bucketName} not found`,
80
+ code: 'BAD_REQUEST'
81
+ });
75
82
  }
76
83
  if (bucket._def.beforeUpload) {
84
+ log.debug('Running [beforeUpload]');
77
85
  const canUpload = await bucket._def.beforeUpload?.({
78
86
  ctx,
79
87
  input,
@@ -86,23 +94,37 @@ async function requestUpload(params) {
86
94
  temporary: fileInfo.temporary
87
95
  }
88
96
  });
97
+ log.debug('Finished [beforeUpload]', {
98
+ canUpload
99
+ });
89
100
  if (!canUpload) {
90
- throw new Error('Upload not allowed');
101
+ throw new index.EdgeStoreError({
102
+ message: 'Upload not allowed for the current context',
103
+ code: 'UPLOAD_NOT_ALLOWED'
104
+ });
91
105
  }
92
106
  }
93
107
  if (bucket._def.type === 'IMAGE') {
94
108
  if (!IMAGE_MIME_TYPES.includes(fileInfo.type)) {
95
- throw new EdgeStoreError({
96
- code: 'BAD_REQUEST',
97
- message: 'Only images are allowed in this bucket'
109
+ throw new index.EdgeStoreError({
110
+ code: 'MIME_TYPE_NOT_ALLOWED',
111
+ message: 'Only images are allowed in this bucket',
112
+ details: {
113
+ allowedMimeTypes: IMAGE_MIME_TYPES,
114
+ mimeType: fileInfo.type
115
+ }
98
116
  });
99
117
  }
100
118
  }
101
119
  if (bucket._def.bucketConfig?.maxSize) {
102
120
  if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
103
- throw new EdgeStoreError({
104
- code: 'BAD_REQUEST',
105
- message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`
121
+ throw new index.EdgeStoreError({
122
+ code: 'FILE_TOO_LARGE',
123
+ message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
124
+ details: {
125
+ maxFileSize: bucket._def.bucketConfig.maxSize,
126
+ fileSize: fileInfo.size
127
+ }
106
128
  });
107
129
  }
108
130
  }
@@ -122,9 +144,13 @@ async function requestUpload(params) {
122
144
  }
123
145
  }
124
146
  if (!accepted) {
125
- throw new EdgeStoreError({
126
- code: 'BAD_REQUEST',
127
- message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`
147
+ throw new index.EdgeStoreError({
148
+ code: 'MIME_TYPE_NOT_ALLOWED',
149
+ message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
150
+ details: {
151
+ allowedMimeTypes: accept,
152
+ mimeType: fileInfo.type
153
+ }
128
154
  });
129
155
  }
130
156
  }
@@ -141,6 +167,12 @@ async function requestUpload(params) {
141
167
  input
142
168
  });
143
169
  const isPublic = bucket._def.accessControl === undefined;
170
+ log.debug('upload info', {
171
+ path,
172
+ metadata,
173
+ isPublic,
174
+ bucketType: bucket._def.type
175
+ });
144
176
  const requestUploadRes = await provider.requestUpload({
145
177
  bucketName,
146
178
  bucketType: bucket._def.type,
@@ -151,11 +183,8 @@ async function requestUpload(params) {
151
183
  metadata
152
184
  }
153
185
  });
154
- const parsedPath = path.reduce((acc, curr)=>{
155
- acc[curr.key] = curr.value;
156
- return acc;
157
- }, {});
158
- const pathOrder = path.map((p)=>p.key);
186
+ const { parsedPath, pathOrder } = parsePath(path);
187
+ log.debug('Finished [requestUpload]');
159
188
  return {
160
189
  ...requestUploadRes,
161
190
  size: fileInfo.size,
@@ -166,27 +195,36 @@ async function requestUpload(params) {
166
195
  };
167
196
  }
168
197
  async function requestUploadParts(params) {
169
- const { provider, router, ctxToken, body: { multipart, path } } = params;
198
+ const { provider, ctxToken, body: { multipart, path } } = params;
199
+ const log = globalThis._EDGE_STORE_LOGGER;
200
+ log.debug('Running [requestUploadParts]', {
201
+ multipart,
202
+ path
203
+ });
170
204
  if (!ctxToken) {
171
- throw new EdgeStoreError({
205
+ throw new index.EdgeStoreError({
172
206
  message: 'Missing edgestore-ctx cookie',
173
207
  code: 'UNAUTHORIZED'
174
208
  });
175
209
  }
176
210
  await getContext(ctxToken); // just to check if the token is valid
177
- const bucket = router.buckets[multipart.uploadId];
178
- if (!bucket) {
179
- throw new Error(`Bucket ${multipart.uploadId} not found`);
180
- }
181
- return await provider.requestUploadParts({
211
+ const res = await provider.requestUploadParts({
182
212
  multipart,
183
213
  path
184
214
  });
215
+ log.debug('Finished [requestUploadParts]');
216
+ return res;
185
217
  }
186
218
  async function completeMultipartUpload(params) {
187
219
  const { provider, router, ctxToken, body: { bucketName, uploadId, key, parts } } = params;
220
+ const log = globalThis._EDGE_STORE_LOGGER;
221
+ log.debug('Running [completeMultipartUpload]', {
222
+ bucketName,
223
+ uploadId,
224
+ key
225
+ });
188
226
  if (!ctxToken) {
189
- throw new EdgeStoreError({
227
+ throw new index.EdgeStoreError({
190
228
  message: 'Missing edgestore-ctx cookie',
191
229
  code: 'UNAUTHORIZED'
192
230
  });
@@ -194,18 +232,28 @@ async function completeMultipartUpload(params) {
194
232
  await getContext(ctxToken); // just to check if the token is valid
195
233
  const bucket = router.buckets[bucketName];
196
234
  if (!bucket) {
197
- throw new Error(`Bucket ${bucketName} not found`);
235
+ throw new index.EdgeStoreError({
236
+ message: `Bucket ${bucketName} not found`,
237
+ code: 'BAD_REQUEST'
238
+ });
198
239
  }
199
- return await provider.completeMultipartUpload({
240
+ const res = await provider.completeMultipartUpload({
200
241
  uploadId,
201
242
  key,
202
243
  parts
203
244
  });
245
+ log.debug('Finished [completeMultipartUpload]');
246
+ return res;
204
247
  }
205
248
  async function confirmUpload(params) {
206
249
  const { provider, router, ctxToken, body: { bucketName, url } } = params;
250
+ const log = globalThis._EDGE_STORE_LOGGER;
251
+ log.debug('Running [confirmUpload]', {
252
+ bucketName,
253
+ url
254
+ });
207
255
  if (!ctxToken) {
208
- throw new EdgeStoreError({
256
+ throw new index.EdgeStoreError({
209
257
  message: 'Missing edgestore-ctx cookie',
210
258
  code: 'UNAUTHORIZED'
211
259
  });
@@ -213,17 +261,27 @@ async function confirmUpload(params) {
213
261
  await getContext(ctxToken); // just to check if the token is valid
214
262
  const bucket = router.buckets[bucketName];
215
263
  if (!bucket) {
216
- throw new Error(`Bucket ${bucketName} not found`);
264
+ throw new index.EdgeStoreError({
265
+ message: `Bucket ${bucketName} not found`,
266
+ code: 'BAD_REQUEST'
267
+ });
217
268
  }
218
- return await provider.confirmUpload({
269
+ const res = await provider.confirmUpload({
219
270
  bucket,
220
271
  url: unproxyUrl(url)
221
272
  });
273
+ log.debug('Finished [confirmUpload]');
274
+ return res;
222
275
  }
223
276
  async function deleteFile(params) {
224
277
  const { provider, router, ctxToken, body: { bucketName, url } } = params;
278
+ const log = globalThis._EDGE_STORE_LOGGER;
279
+ log.debug('Running [deleteFile]', {
280
+ bucketName,
281
+ url
282
+ });
225
283
  if (!ctxToken) {
226
- throw new EdgeStoreError({
284
+ throw new index.EdgeStoreError({
227
285
  message: 'Missing edgestore-ctx cookie',
228
286
  code: 'UNAUTHORIZED'
229
287
  });
@@ -231,30 +289,44 @@ async function deleteFile(params) {
231
289
  const ctx = await getContext(ctxToken);
232
290
  const bucket = router.buckets[bucketName];
233
291
  if (!bucket) {
234
- throw new Error(`Bucket ${bucketName} not found`);
292
+ throw new index.EdgeStoreError({
293
+ message: `Bucket ${bucketName} not found`,
294
+ code: 'BAD_REQUEST'
295
+ });
235
296
  }
236
297
  if (!bucket._def.beforeDelete) {
237
- throw new Error('You need to define beforeDelete if you want to delete files directly from the frontend.');
298
+ throw new index.EdgeStoreError({
299
+ message: 'You need to define beforeDelete if you want to delete files directly from the frontend.',
300
+ code: 'SERVER_ERROR'
301
+ });
238
302
  }
239
303
  const fileInfo = await provider.getFile({
240
- url
304
+ url: unproxyUrl(url)
241
305
  });
242
306
  const canDelete = await bucket._def.beforeDelete({
243
307
  ctx,
244
308
  fileInfo
245
309
  });
246
310
  if (!canDelete) {
247
- throw new Error('Delete not allowed');
311
+ throw new index.EdgeStoreError({
312
+ message: 'Delete not allowed for the current context',
313
+ code: 'DELETE_NOT_ALLOWED'
314
+ });
248
315
  }
249
- return await provider.deleteFile({
316
+ const res = await provider.deleteFile({
250
317
  bucket,
251
318
  url: unproxyUrl(url)
252
319
  });
320
+ log.debug('Finished [deleteFile]');
321
+ return res;
253
322
  }
254
323
  async function encryptJWT(ctx) {
255
324
  const secret = process.env.EDGE_STORE_JWT_SECRET ?? process.env.EDGE_STORE_SECRET_KEY;
256
325
  if (!secret) {
257
- throw new Error('EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined');
326
+ throw new index.EdgeStoreError({
327
+ message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
328
+ code: 'SERVER_ERROR'
329
+ });
258
330
  }
259
331
  const encryptionSecret = await getDerivedEncryptionKey(secret);
260
332
  return await new jose.EncryptJWT(ctx).setProtectedHeader({
@@ -265,7 +337,10 @@ async function encryptJWT(ctx) {
265
337
  async function decryptJWT(token) {
266
338
  const secret = process.env.EDGE_STORE_JWT_SECRET ?? process.env.EDGE_STORE_SECRET_KEY;
267
339
  if (!secret) {
268
- throw new Error('EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not set');
340
+ throw new index.EdgeStoreError({
341
+ message: 'EDGE_STORE_JWT_SECRET or EDGE_STORE_SECRET_KEY is not defined',
342
+ code: 'SERVER_ERROR'
343
+ });
269
344
  }
270
345
  const encryptionSecret = await getDerivedEncryptionKey(secret);
271
346
  const { payload } = await jose.jwtDecrypt(token, encryptionSecret, {
@@ -282,13 +357,19 @@ function buildPath(params) {
282
357
  const path = pathParams.map((param)=>{
283
358
  const paramEntries = Object.entries(param);
284
359
  if (paramEntries[0] === undefined) {
285
- throw new Error('Missing path param');
360
+ throw new index.EdgeStoreError({
361
+ message: `Empty path param found in: ${JSON.stringify(pathParams)}`,
362
+ code: 'SERVER_ERROR'
363
+ });
286
364
  }
287
365
  const [key, value] = paramEntries[0];
288
366
  // this is a string like: "ctx.xxx" or "input.yyy.zzz"
289
367
  const currParamVal = value().split('.').reduce((acc2, key)=>{
290
368
  if (acc2[key] === undefined) {
291
- throw new Error(`Missing key ${key} in ${JSON.stringify(acc2)}`);
369
+ throw new index.EdgeStoreError({
370
+ message: `Missing key ${key} in ${JSON.stringify(acc2)}`,
371
+ code: 'BAD_REQUEST'
372
+ });
292
373
  }
293
374
  return acc2[key];
294
375
  }, params.pathAttrs);
@@ -299,10 +380,18 @@ function buildPath(params) {
299
380
  });
300
381
  return path;
301
382
  }
383
+ function parsePath(path) {
384
+ const parsedPath = path.reduce((acc, curr)=>{
385
+ acc[curr.key] = curr.value;
386
+ return acc;
387
+ }, {});
388
+ const pathOrder = path.map((p)=>p.key);
389
+ return {
390
+ parsedPath,
391
+ pathOrder
392
+ };
393
+ }
302
394
  async function getContext(token) {
303
- if (!token) {
304
- throw new Error('No token');
305
- }
306
395
  return await decryptJWT(token);
307
396
  }
308
397
  /**
@@ -322,11 +411,11 @@ async function getContext(token) {
322
411
  return url;
323
412
  }
324
413
 
325
- exports.EDGE_STORE_ERROR_CODES = EDGE_STORE_ERROR_CODES;
326
- exports.EdgeStoreError = EdgeStoreError;
414
+ exports.buildPath = buildPath;
327
415
  exports.completeMultipartUpload = completeMultipartUpload;
328
416
  exports.confirmUpload = confirmUpload;
329
417
  exports.deleteFile = deleteFile;
330
418
  exports.init = init;
419
+ exports.parsePath = parsePath;
331
420
  exports.requestUpload = requestUpload;
332
421
  exports.requestUploadParts = requestUploadParts;