@edgestore/server 0.8.0 → 1.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +2 -2
- package/dist/adapters/astro/index.js +28 -30
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.js +28 -30
- package/dist/adapters/fastify/index.d.ts +2 -2
- package/dist/adapters/fastify/index.js +28 -30
- package/dist/adapters/hono/index.d.ts +2 -2
- package/dist/adapters/hono/index.js +28 -30
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +2 -2
- package/dist/adapters/next/app/index.js +29 -31
- package/dist/adapters/next/pages/index.d.ts +2 -2
- package/dist/adapters/next/pages/index.js +29 -31
- package/dist/adapters/remix/index.d.ts +2 -2
- package/dist/adapters/remix/index.js +28 -30
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +44 -50
- package/dist/adapters/start/index.d.ts +2 -2
- package/dist/adapters/start/index.js +28 -30
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +7 -9
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -10
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/core/sdk/index.js +10 -13
- package/dist/index.js +1 -10
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- package/dist/providers/aws/index.d.ts.map +1 -1
- package/dist/providers/aws/index.js +12 -15
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +6 -9
- package/dist/providers/edgestore/index.js +12 -14
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +2 -5
- package/package.json +51 -50
- package/providers/aws/index.d.ts +1 -1
- package/providers/aws/index.js +1 -1
- package/providers/azure/index.d.ts +1 -1
- package/providers/azure/index.js +1 -1
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- package/src/adapters/shared.ts +12 -13
- package/src/core/client/index.ts +0 -1
- package/src/core/sdk/index.ts +1 -8
- package/src/providers/aws/index.ts +1 -2
- package/src/providers/azure/index.ts +1 -2
- 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.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.mjs +0 -103
- package/dist/providers/azure/index.mjs +0 -85
- package/dist/providers/edgestore/index.mjs +0 -164
- package/dist/version.mjs +0 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/astro';
|
|
1
|
+
export * from '../../dist/adapters/astro/index.js';
|
package/adapters/astro/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/astro/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/express';
|
|
1
|
+
export * from '../../dist/adapters/express/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/express/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/fastify';
|
|
1
|
+
export * from '../../dist/adapters/fastify/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/fastify/index.js';
|
package/adapters/hono/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/hono';
|
|
1
|
+
export * from '../../dist/adapters/hono/index.js';
|
package/adapters/hono/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/hono/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../../dist/adapters/next/app';
|
|
1
|
+
export * from '../../../dist/adapters/next/app/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../../dist/adapters/next/app/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../../dist/adapters/next/pages';
|
|
1
|
+
export * from '../../../dist/adapters/next/pages/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../../dist/adapters/next/pages/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/remix';
|
|
1
|
+
export * from '../../dist/adapters/remix/index.js';
|
package/adapters/remix/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/remix/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../../dist/adapters/start';
|
|
1
|
+
export * from '../../dist/adapters/start/index.js';
|
package/adapters/start/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../../dist/adapters/start/index.js';
|
package/core/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '../dist/core';
|
|
1
|
+
export * from '../dist/core/index.js';
|
package/core/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export * from '../dist/core/index.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type EdgeStoreRouter, type MaybePromise, type Provider } from '@edgestore/shared';
|
|
2
2
|
import type { APIContext } from 'astro';
|
|
3
|
-
import { type LogLevel } from '../../libs/logger';
|
|
4
|
-
import { type CookieConfig } from '../shared';
|
|
3
|
+
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
+
import { type CookieConfig } from '../shared.js';
|
|
5
5
|
export type Config<TCtx> = {
|
|
6
6
|
provider?: Provider;
|
|
7
7
|
router: EdgeStoreRouter<TCtx>;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var index = require('../../providers/edgestore/index.js');
|
|
7
|
-
var shared = require('../shared.js');
|
|
1
|
+
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
+
import Logger from '../../libs/logger.js';
|
|
3
|
+
import { matchPath } from '../../libs/utils.js';
|
|
4
|
+
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
+
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
8
6
|
|
|
9
7
|
// Helper to safely get cookies from Astro request
|
|
10
8
|
function getCookie(request, name) {
|
|
@@ -18,29 +16,29 @@ function getCookie(request, name) {
|
|
|
18
16
|
return cookies[name];
|
|
19
17
|
}
|
|
20
18
|
function createEdgeStoreAstroHandler(config) {
|
|
21
|
-
const { provider =
|
|
22
|
-
const log = new
|
|
19
|
+
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
20
|
+
const log = new Logger(config.logLevel);
|
|
23
21
|
globalThis._EDGE_STORE_LOGGER = log;
|
|
24
22
|
log.debug('Creating EdgeStore Astro handler');
|
|
25
|
-
const resolvedCookieConfig =
|
|
23
|
+
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
26
24
|
return async (context)=>{
|
|
27
25
|
try {
|
|
28
26
|
const { request } = context;
|
|
29
27
|
const url = new URL(request.url);
|
|
30
|
-
if (
|
|
28
|
+
if (matchPath(url.pathname, 'health')) {
|
|
31
29
|
return new Response('OK');
|
|
32
|
-
} else if (
|
|
30
|
+
} else if (matchPath(url.pathname, 'init')) {
|
|
33
31
|
let ctx = {};
|
|
34
32
|
try {
|
|
35
33
|
ctx = 'createContext' in config ? await config.createContext(context) : {};
|
|
36
34
|
} catch (err) {
|
|
37
|
-
throw new
|
|
35
|
+
throw new EdgeStoreError({
|
|
38
36
|
message: 'Error creating context',
|
|
39
37
|
code: 'CREATE_CONTEXT_ERROR',
|
|
40
38
|
cause: err instanceof Error ? err : undefined
|
|
41
39
|
});
|
|
42
40
|
}
|
|
43
|
-
const { newCookies, ...body } = await
|
|
41
|
+
const { newCookies, ...body } = await init({
|
|
44
42
|
ctx,
|
|
45
43
|
provider,
|
|
46
44
|
router: config.router,
|
|
@@ -59,9 +57,9 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
59
57
|
return new Response(JSON.stringify(body), {
|
|
60
58
|
headers
|
|
61
59
|
});
|
|
62
|
-
} else if (
|
|
60
|
+
} else if (matchPath(url.pathname, 'request-upload')) {
|
|
63
61
|
const body = await request.json();
|
|
64
|
-
const result = await
|
|
62
|
+
const result = await requestUpload({
|
|
65
63
|
provider,
|
|
66
64
|
router: config.router,
|
|
67
65
|
body,
|
|
@@ -72,9 +70,9 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
72
70
|
'Content-Type': 'application/json'
|
|
73
71
|
}
|
|
74
72
|
});
|
|
75
|
-
} else if (
|
|
73
|
+
} else if (matchPath(url.pathname, 'request-upload-parts')) {
|
|
76
74
|
const body = await request.json();
|
|
77
|
-
const result = await
|
|
75
|
+
const result = await requestUploadParts({
|
|
78
76
|
provider,
|
|
79
77
|
router: config.router,
|
|
80
78
|
body,
|
|
@@ -85,9 +83,9 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
85
83
|
'Content-Type': 'application/json'
|
|
86
84
|
}
|
|
87
85
|
});
|
|
88
|
-
} else if (
|
|
86
|
+
} else if (matchPath(url.pathname, 'complete-multipart-upload')) {
|
|
89
87
|
const body = await request.json();
|
|
90
|
-
await
|
|
88
|
+
await completeMultipartUpload({
|
|
91
89
|
provider,
|
|
92
90
|
router: config.router,
|
|
93
91
|
body,
|
|
@@ -96,9 +94,9 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
96
94
|
return new Response(null, {
|
|
97
95
|
status: 200
|
|
98
96
|
});
|
|
99
|
-
} else if (
|
|
97
|
+
} else if (matchPath(url.pathname, 'confirm-upload')) {
|
|
100
98
|
const body = await request.json();
|
|
101
|
-
const result = await
|
|
99
|
+
const result = await confirmUpload({
|
|
102
100
|
provider,
|
|
103
101
|
router: config.router,
|
|
104
102
|
body,
|
|
@@ -109,9 +107,9 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
109
107
|
'Content-Type': 'application/json'
|
|
110
108
|
}
|
|
111
109
|
});
|
|
112
|
-
} else if (
|
|
110
|
+
} else if (matchPath(url.pathname, 'delete-file')) {
|
|
113
111
|
const body = await request.json();
|
|
114
|
-
const result = await
|
|
112
|
+
const result = await deleteFile({
|
|
115
113
|
provider,
|
|
116
114
|
router: config.router,
|
|
117
115
|
body,
|
|
@@ -122,10 +120,10 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
122
120
|
'Content-Type': 'application/json'
|
|
123
121
|
}
|
|
124
122
|
});
|
|
125
|
-
} else if (
|
|
123
|
+
} else if (matchPath(url.pathname, 'proxy-file')) {
|
|
126
124
|
const url = new URL(request.url).searchParams.get('url');
|
|
127
125
|
if (typeof url === 'string') {
|
|
128
|
-
const proxyRes = await
|
|
126
|
+
const proxyRes = await fetchProxyFile({
|
|
129
127
|
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
130
128
|
url
|
|
131
129
|
});
|
|
@@ -146,18 +144,18 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
146
144
|
});
|
|
147
145
|
}
|
|
148
146
|
} catch (err) {
|
|
149
|
-
if (err instanceof
|
|
147
|
+
if (err instanceof EdgeStoreError) {
|
|
150
148
|
log[err.level](err.formattedMessage());
|
|
151
149
|
if (err.cause) log[err.level](err.cause);
|
|
152
150
|
return new Response(JSON.stringify(err.formattedJson()), {
|
|
153
|
-
status:
|
|
151
|
+
status: EDGE_STORE_ERROR_CODES[err.code],
|
|
154
152
|
headers: {
|
|
155
153
|
'Content-Type': 'application/json'
|
|
156
154
|
}
|
|
157
155
|
});
|
|
158
156
|
} else {
|
|
159
157
|
log.error(err);
|
|
160
|
-
return new Response(JSON.stringify(new
|
|
158
|
+
return new Response(JSON.stringify(new EdgeStoreError({
|
|
161
159
|
message: 'Internal Server Error',
|
|
162
160
|
code: 'SERVER_ERROR'
|
|
163
161
|
}).formattedJson()), {
|
|
@@ -171,4 +169,4 @@ function createEdgeStoreAstroHandler(config) {
|
|
|
171
169
|
};
|
|
172
170
|
}
|
|
173
171
|
|
|
174
|
-
|
|
172
|
+
export { createEdgeStoreAstroHandler };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type EdgeStoreRouter, type MaybePromise, type Provider } from '@edgestore/shared';
|
|
2
2
|
import { type Request, type Response } from 'express';
|
|
3
|
-
import { type LogLevel } from '../../libs/logger';
|
|
4
|
-
import { type CookieConfig } from '../shared';
|
|
3
|
+
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
+
import { type CookieConfig } from '../shared.js';
|
|
5
5
|
export type CreateContextOptions = {
|
|
6
6
|
req: Request;
|
|
7
7
|
res: Response;
|
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var index = require('../../providers/edgestore/index.js');
|
|
7
|
-
var shared = require('../shared.js');
|
|
1
|
+
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
+
import Logger from '../../libs/logger.js';
|
|
3
|
+
import { matchPath } from '../../libs/utils.js';
|
|
4
|
+
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
+
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
8
6
|
|
|
9
7
|
function createEdgeStoreExpressHandler(config) {
|
|
10
|
-
const { provider =
|
|
11
|
-
const log = new
|
|
8
|
+
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
9
|
+
const log = new Logger(config.logLevel);
|
|
12
10
|
globalThis._EDGE_STORE_LOGGER = log;
|
|
13
11
|
log.debug('Creating EdgeStore Express handler');
|
|
14
|
-
const resolvedCookieConfig =
|
|
12
|
+
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
15
13
|
return async (req, res)=>{
|
|
16
14
|
try {
|
|
17
15
|
const pathname = req.url ?? '';
|
|
18
|
-
if (
|
|
16
|
+
if (matchPath(pathname, '/health')) {
|
|
19
17
|
res.send('OK');
|
|
20
|
-
} else if (
|
|
18
|
+
} else if (matchPath(pathname, '/init')) {
|
|
21
19
|
let ctx = {};
|
|
22
20
|
try {
|
|
23
21
|
ctx = 'createContext' in config ? await config.createContext({
|
|
@@ -25,13 +23,13 @@ function createEdgeStoreExpressHandler(config) {
|
|
|
25
23
|
res
|
|
26
24
|
}) : {};
|
|
27
25
|
} catch (err) {
|
|
28
|
-
throw new
|
|
26
|
+
throw new EdgeStoreError({
|
|
29
27
|
message: 'Error creating context',
|
|
30
28
|
code: 'CREATE_CONTEXT_ERROR',
|
|
31
29
|
cause: err instanceof Error ? err : undefined
|
|
32
30
|
});
|
|
33
31
|
}
|
|
34
|
-
const { newCookies, ...body } = await
|
|
32
|
+
const { newCookies, ...body } = await init({
|
|
35
33
|
ctx,
|
|
36
34
|
provider,
|
|
37
35
|
router: config.router,
|
|
@@ -39,46 +37,46 @@ function createEdgeStoreExpressHandler(config) {
|
|
|
39
37
|
});
|
|
40
38
|
res.setHeader('Set-Cookie', newCookies);
|
|
41
39
|
res.json(body);
|
|
42
|
-
} else if (
|
|
43
|
-
res.json(await
|
|
40
|
+
} else if (matchPath(pathname, '/request-upload')) {
|
|
41
|
+
res.json(await requestUpload({
|
|
44
42
|
provider,
|
|
45
43
|
router: config.router,
|
|
46
44
|
body: req.body,
|
|
47
45
|
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
48
46
|
}));
|
|
49
|
-
} else if (
|
|
50
|
-
res.json(await
|
|
47
|
+
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
48
|
+
res.json(await requestUploadParts({
|
|
51
49
|
provider,
|
|
52
50
|
router: config.router,
|
|
53
51
|
body: req.body,
|
|
54
52
|
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
55
53
|
}));
|
|
56
|
-
} else if (
|
|
57
|
-
await
|
|
54
|
+
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
55
|
+
await completeMultipartUpload({
|
|
58
56
|
provider,
|
|
59
57
|
router: config.router,
|
|
60
58
|
body: req.body,
|
|
61
59
|
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
62
60
|
});
|
|
63
61
|
res.status(200).end();
|
|
64
|
-
} else if (
|
|
65
|
-
res.json(await
|
|
62
|
+
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
63
|
+
res.json(await confirmUpload({
|
|
66
64
|
provider,
|
|
67
65
|
router: config.router,
|
|
68
66
|
body: req.body,
|
|
69
67
|
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
70
68
|
}));
|
|
71
|
-
} else if (
|
|
72
|
-
res.json(await
|
|
69
|
+
} else if (matchPath(pathname, '/delete-file')) {
|
|
70
|
+
res.json(await deleteFile({
|
|
73
71
|
provider,
|
|
74
72
|
router: config.router,
|
|
75
73
|
body: req.body,
|
|
76
74
|
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
77
75
|
}));
|
|
78
|
-
} else if (
|
|
76
|
+
} else if (matchPath(pathname, '/proxy-file')) {
|
|
79
77
|
const { url } = req.query;
|
|
80
78
|
if (typeof url === 'string') {
|
|
81
|
-
const proxyRes = await
|
|
79
|
+
const proxyRes = await fetchProxyFile({
|
|
82
80
|
cookieHeader: req.headers.cookie,
|
|
83
81
|
url
|
|
84
82
|
});
|
|
@@ -92,13 +90,13 @@ function createEdgeStoreExpressHandler(config) {
|
|
|
92
90
|
res.status(404).end();
|
|
93
91
|
}
|
|
94
92
|
} catch (err) {
|
|
95
|
-
if (err instanceof
|
|
93
|
+
if (err instanceof EdgeStoreError) {
|
|
96
94
|
log[err.level](err.formattedMessage());
|
|
97
95
|
if (err.cause) log[err.level](err.cause);
|
|
98
|
-
res.status(
|
|
96
|
+
res.status(EDGE_STORE_ERROR_CODES[err.code]).json(err.formattedJson());
|
|
99
97
|
} else {
|
|
100
98
|
log.error(err);
|
|
101
|
-
res.status(500).send(new
|
|
99
|
+
res.status(500).send(new EdgeStoreError({
|
|
102
100
|
message: 'Internal Server Error',
|
|
103
101
|
code: 'SERVER_ERROR'
|
|
104
102
|
}).formattedJson());
|
|
@@ -107,4 +105,4 @@ function createEdgeStoreExpressHandler(config) {
|
|
|
107
105
|
};
|
|
108
106
|
}
|
|
109
107
|
|
|
110
|
-
|
|
108
|
+
export { createEdgeStoreExpressHandler };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type EdgeStoreRouter, type MaybePromise, type Provider } from '@edgestore/shared';
|
|
2
2
|
import { type FastifyReply, type FastifyRequest } from 'fastify';
|
|
3
|
-
import { type LogLevel } from '../../libs/logger';
|
|
4
|
-
import { type CookieConfig } from '../shared';
|
|
3
|
+
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
+
import { type CookieConfig } from '../shared.js';
|
|
5
5
|
export type CreateContextOptions = {
|
|
6
6
|
req: FastifyRequest;
|
|
7
7
|
reply: FastifyReply;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var index = require('../../providers/edgestore/index.js');
|
|
7
|
-
var shared = require('../shared.js');
|
|
1
|
+
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
+
import Logger from '../../libs/logger.js';
|
|
3
|
+
import { matchPath } from '../../libs/utils.js';
|
|
4
|
+
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
+
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
8
6
|
|
|
9
7
|
// Helper to safely get cookies from Fastify request
|
|
10
8
|
function getCookie(req, name) {
|
|
@@ -24,18 +22,18 @@ function getCookie(req, name) {
|
|
|
24
22
|
return cookies[name];
|
|
25
23
|
}
|
|
26
24
|
function createEdgeStoreFastifyHandler(config) {
|
|
27
|
-
const { provider =
|
|
28
|
-
const log = new
|
|
25
|
+
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
26
|
+
const log = new Logger(config.logLevel);
|
|
29
27
|
globalThis._EDGE_STORE_LOGGER = log;
|
|
30
28
|
log.debug('Creating EdgeStore Fastify handler');
|
|
31
|
-
const resolvedCookieConfig =
|
|
29
|
+
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
32
30
|
return async (req, reply)=>{
|
|
33
31
|
try {
|
|
34
32
|
// Get the URL from the request - simplified approach
|
|
35
33
|
const pathname = req.url;
|
|
36
|
-
if (
|
|
34
|
+
if (matchPath(pathname, '/health')) {
|
|
37
35
|
return reply.send('OK');
|
|
38
|
-
} else if (
|
|
36
|
+
} else if (matchPath(pathname, '/init')) {
|
|
39
37
|
let ctx = {};
|
|
40
38
|
try {
|
|
41
39
|
ctx = 'createContext' in config ? await config.createContext({
|
|
@@ -43,13 +41,13 @@ function createEdgeStoreFastifyHandler(config) {
|
|
|
43
41
|
reply
|
|
44
42
|
}) : {};
|
|
45
43
|
} catch (err) {
|
|
46
|
-
throw new
|
|
44
|
+
throw new EdgeStoreError({
|
|
47
45
|
message: 'Error creating context',
|
|
48
46
|
code: 'CREATE_CONTEXT_ERROR',
|
|
49
47
|
cause: err instanceof Error ? err : undefined
|
|
50
48
|
});
|
|
51
49
|
}
|
|
52
|
-
const { newCookies, ...body } = await
|
|
50
|
+
const { newCookies, ...body } = await init({
|
|
53
51
|
ctx,
|
|
54
52
|
provider,
|
|
55
53
|
router: config.router,
|
|
@@ -67,46 +65,46 @@ function createEdgeStoreFastifyHandler(config) {
|
|
|
67
65
|
void reply.header('Set-Cookie', newCookies);
|
|
68
66
|
}
|
|
69
67
|
return reply.send(body);
|
|
70
|
-
} else if (
|
|
71
|
-
return reply.send(await
|
|
68
|
+
} else if (matchPath(pathname, '/request-upload')) {
|
|
69
|
+
return reply.send(await requestUpload({
|
|
72
70
|
provider,
|
|
73
71
|
router: config.router,
|
|
74
72
|
body: req.body,
|
|
75
73
|
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
76
74
|
}));
|
|
77
|
-
} else if (
|
|
78
|
-
return reply.send(await
|
|
75
|
+
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
76
|
+
return reply.send(await requestUploadParts({
|
|
79
77
|
provider,
|
|
80
78
|
router: config.router,
|
|
81
79
|
body: req.body,
|
|
82
80
|
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
83
81
|
}));
|
|
84
|
-
} else if (
|
|
85
|
-
await
|
|
82
|
+
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
83
|
+
await completeMultipartUpload({
|
|
86
84
|
provider,
|
|
87
85
|
router: config.router,
|
|
88
86
|
body: req.body,
|
|
89
87
|
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
90
88
|
});
|
|
91
89
|
return reply.status(200).send();
|
|
92
|
-
} else if (
|
|
93
|
-
return reply.send(await
|
|
90
|
+
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
91
|
+
return reply.send(await confirmUpload({
|
|
94
92
|
provider,
|
|
95
93
|
router: config.router,
|
|
96
94
|
body: req.body,
|
|
97
95
|
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
98
96
|
}));
|
|
99
|
-
} else if (
|
|
100
|
-
return reply.send(await
|
|
97
|
+
} else if (matchPath(pathname, '/delete-file')) {
|
|
98
|
+
return reply.send(await deleteFile({
|
|
101
99
|
provider,
|
|
102
100
|
router: config.router,
|
|
103
101
|
body: req.body,
|
|
104
102
|
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
105
103
|
}));
|
|
106
|
-
} else if (
|
|
104
|
+
} else if (matchPath(pathname, '/proxy-file')) {
|
|
107
105
|
const url = req.query ? req.query.url : undefined;
|
|
108
106
|
if (typeof url === 'string') {
|
|
109
|
-
const proxyRes = await
|
|
107
|
+
const proxyRes = await fetchProxyFile({
|
|
110
108
|
cookieHeader: req.headers.cookie,
|
|
111
109
|
url
|
|
112
110
|
});
|
|
@@ -119,13 +117,13 @@ function createEdgeStoreFastifyHandler(config) {
|
|
|
119
117
|
return reply.status(404).send();
|
|
120
118
|
}
|
|
121
119
|
} catch (err) {
|
|
122
|
-
if (err instanceof
|
|
120
|
+
if (err instanceof EdgeStoreError) {
|
|
123
121
|
log[err.level](err.formattedMessage());
|
|
124
122
|
if (err.cause) log[err.level](err.cause);
|
|
125
|
-
return reply.status(
|
|
123
|
+
return reply.status(EDGE_STORE_ERROR_CODES[err.code]).send(err.formattedJson());
|
|
126
124
|
} else {
|
|
127
125
|
log.error(err);
|
|
128
|
-
return reply.status(500).send(new
|
|
126
|
+
return reply.status(500).send(new EdgeStoreError({
|
|
129
127
|
message: 'Internal Server Error',
|
|
130
128
|
code: 'SERVER_ERROR'
|
|
131
129
|
}).formattedJson());
|
|
@@ -134,4 +132,4 @@ function createEdgeStoreFastifyHandler(config) {
|
|
|
134
132
|
};
|
|
135
133
|
}
|
|
136
134
|
|
|
137
|
-
|
|
135
|
+
export { createEdgeStoreFastifyHandler };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type EdgeStoreRouter, type MaybePromise, type Provider } from '@edgestore/shared';
|
|
2
2
|
import { type Context as HonoContext } from 'hono';
|
|
3
|
-
import { type LogLevel } from '../../libs/logger';
|
|
4
|
-
import { type CookieConfig } from '../shared';
|
|
3
|
+
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
+
import { type CookieConfig } from '../shared.js';
|
|
5
5
|
export type CreateContextOptions = {
|
|
6
6
|
c: HonoContext;
|
|
7
7
|
};
|