@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
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.mjs';
|
|
6
|
-
|
|
7
|
-
// Helper to safely get cookies from Fastify request
|
|
8
|
-
function getCookie(req, name) {
|
|
9
|
-
// Check if cookies plugin is available
|
|
10
|
-
if ('cookies' in req) {
|
|
11
|
-
// Type assertion for TypeScript
|
|
12
|
-
return req.cookies[name];
|
|
13
|
-
}
|
|
14
|
-
// Fallback to parsing cookie header
|
|
15
|
-
const cookieHeader = req.headers.cookie;
|
|
16
|
-
if (!cookieHeader) return undefined;
|
|
17
|
-
const cookies = cookieHeader.split(';').reduce((acc, cookie)=>{
|
|
18
|
-
const [key, value] = cookie.trim().split('=');
|
|
19
|
-
if (key && value) acc[key] = value;
|
|
20
|
-
return acc;
|
|
21
|
-
}, {});
|
|
22
|
-
return cookies[name];
|
|
23
|
-
}
|
|
24
|
-
function createEdgeStoreFastifyHandler(config) {
|
|
25
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
26
|
-
const log = new Logger(config.logLevel);
|
|
27
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
28
|
-
log.debug('Creating EdgeStore Fastify handler');
|
|
29
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
30
|
-
return async (req, reply)=>{
|
|
31
|
-
try {
|
|
32
|
-
// Get the URL from the request - simplified approach
|
|
33
|
-
const pathname = req.url;
|
|
34
|
-
if (matchPath(pathname, '/health')) {
|
|
35
|
-
return reply.send('OK');
|
|
36
|
-
} else if (matchPath(pathname, '/init')) {
|
|
37
|
-
let ctx = {};
|
|
38
|
-
try {
|
|
39
|
-
ctx = 'createContext' in config ? await config.createContext({
|
|
40
|
-
req,
|
|
41
|
-
reply
|
|
42
|
-
}) : {};
|
|
43
|
-
} catch (err) {
|
|
44
|
-
throw new EdgeStoreError({
|
|
45
|
-
message: 'Error creating context',
|
|
46
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
47
|
-
cause: err instanceof Error ? err : undefined
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
const { newCookies, ...body } = await init({
|
|
51
|
-
ctx,
|
|
52
|
-
provider,
|
|
53
|
-
router: config.router,
|
|
54
|
-
cookieConfig
|
|
55
|
-
});
|
|
56
|
-
// Set cookies more efficiently - handling them using void operator
|
|
57
|
-
// to explicitly mark these synchronous calls as intentionally not awaited
|
|
58
|
-
if (Array.isArray(newCookies)) {
|
|
59
|
-
// If it's an array of cookies, set them all
|
|
60
|
-
for (const cookie of newCookies){
|
|
61
|
-
void reply.header('Set-Cookie', cookie);
|
|
62
|
-
}
|
|
63
|
-
} else if (newCookies) {
|
|
64
|
-
// If it's a single cookie string
|
|
65
|
-
void reply.header('Set-Cookie', newCookies);
|
|
66
|
-
}
|
|
67
|
-
return reply.send(body);
|
|
68
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
69
|
-
return reply.send(await requestUpload({
|
|
70
|
-
provider,
|
|
71
|
-
router: config.router,
|
|
72
|
-
body: req.body,
|
|
73
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
74
|
-
}));
|
|
75
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
76
|
-
return reply.send(await requestUploadParts({
|
|
77
|
-
provider,
|
|
78
|
-
router: config.router,
|
|
79
|
-
body: req.body,
|
|
80
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
81
|
-
}));
|
|
82
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
83
|
-
await completeMultipartUpload({
|
|
84
|
-
provider,
|
|
85
|
-
router: config.router,
|
|
86
|
-
body: req.body,
|
|
87
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
88
|
-
});
|
|
89
|
-
return reply.status(200).send();
|
|
90
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
91
|
-
return reply.send(await confirmUpload({
|
|
92
|
-
provider,
|
|
93
|
-
router: config.router,
|
|
94
|
-
body: req.body,
|
|
95
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
96
|
-
}));
|
|
97
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
98
|
-
return reply.send(await deleteFile({
|
|
99
|
-
provider,
|
|
100
|
-
router: config.router,
|
|
101
|
-
body: req.body,
|
|
102
|
-
ctxToken: getCookie(req, resolvedCookieConfig.ctx.name)
|
|
103
|
-
}));
|
|
104
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
105
|
-
const url = req.query ? req.query.url : undefined;
|
|
106
|
-
if (typeof url === 'string') {
|
|
107
|
-
const proxyRes = await fetchProxyFile({
|
|
108
|
-
cookieHeader: req.headers.cookie,
|
|
109
|
-
url
|
|
110
|
-
});
|
|
111
|
-
void reply.header('Content-Type', proxyRes.contentType);
|
|
112
|
-
return reply.status(proxyRes.status).send(proxyRes.body === null ? undefined : Buffer.from(proxyRes.body));
|
|
113
|
-
} else {
|
|
114
|
-
return reply.status(400).send();
|
|
115
|
-
}
|
|
116
|
-
} else {
|
|
117
|
-
return reply.status(404).send();
|
|
118
|
-
}
|
|
119
|
-
} catch (err) {
|
|
120
|
-
if (err instanceof EdgeStoreError) {
|
|
121
|
-
log[err.level](err.formattedMessage());
|
|
122
|
-
if (err.cause) log[err.level](err.cause);
|
|
123
|
-
return reply.status(EDGE_STORE_ERROR_CODES[err.code]).send(err.formattedJson());
|
|
124
|
-
} else {
|
|
125
|
-
log.error(err);
|
|
126
|
-
return reply.status(500).send(new EdgeStoreError({
|
|
127
|
-
message: 'Internal Server Error',
|
|
128
|
-
code: 'SERVER_ERROR'
|
|
129
|
-
}).formattedJson());
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export { createEdgeStoreFastifyHandler };
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.mjs';
|
|
6
|
-
|
|
7
|
-
// Helper to get a cookie value from Hono Context
|
|
8
|
-
function getCookie(c, name) {
|
|
9
|
-
const cookies = c.req.header('cookie');
|
|
10
|
-
if (!cookies) return undefined;
|
|
11
|
-
const match = new RegExp(`${name}=([^;]+)`).exec(cookies);
|
|
12
|
-
return match ? match[1] : undefined;
|
|
13
|
-
}
|
|
14
|
-
function createEdgeStoreHonoHandler(config) {
|
|
15
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
16
|
-
const log = new Logger(config.logLevel);
|
|
17
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
18
|
-
log.debug('Creating EdgeStore Hono handler');
|
|
19
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
20
|
-
return async (c)=>{
|
|
21
|
-
try {
|
|
22
|
-
const pathname = new URL(c.req.url).pathname;
|
|
23
|
-
if (matchPath(pathname, '/health')) {
|
|
24
|
-
return c.text('OK');
|
|
25
|
-
} else if (matchPath(pathname, '/init')) {
|
|
26
|
-
let ctx = {};
|
|
27
|
-
try {
|
|
28
|
-
ctx = 'createContext' in config ? await config.createContext({
|
|
29
|
-
c
|
|
30
|
-
}) : {};
|
|
31
|
-
} catch (err) {
|
|
32
|
-
throw new EdgeStoreError({
|
|
33
|
-
message: 'Error creating context',
|
|
34
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
35
|
-
cause: err instanceof Error ? err : undefined
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
const { newCookies, ...body } = await init({
|
|
39
|
-
ctx,
|
|
40
|
-
provider,
|
|
41
|
-
router: config.router,
|
|
42
|
-
cookieConfig
|
|
43
|
-
});
|
|
44
|
-
// Set cookies
|
|
45
|
-
if (Array.isArray(newCookies)) {
|
|
46
|
-
for (const cookie of newCookies){
|
|
47
|
-
c.header('Set-Cookie', cookie, {
|
|
48
|
-
append: true
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
} else if (newCookies) {
|
|
52
|
-
c.header('Set-Cookie', newCookies);
|
|
53
|
-
}
|
|
54
|
-
return c.json(body);
|
|
55
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
56
|
-
const body = await c.req.json();
|
|
57
|
-
return c.json(await requestUpload({
|
|
58
|
-
provider,
|
|
59
|
-
router: config.router,
|
|
60
|
-
body,
|
|
61
|
-
ctxToken: getCookie(c, resolvedCookieConfig.ctx.name)
|
|
62
|
-
}));
|
|
63
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
64
|
-
const body = await c.req.json();
|
|
65
|
-
return c.json(await requestUploadParts({
|
|
66
|
-
provider,
|
|
67
|
-
router: config.router,
|
|
68
|
-
body,
|
|
69
|
-
ctxToken: getCookie(c, resolvedCookieConfig.ctx.name)
|
|
70
|
-
}));
|
|
71
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
72
|
-
const body = await c.req.json();
|
|
73
|
-
await completeMultipartUpload({
|
|
74
|
-
provider,
|
|
75
|
-
router: config.router,
|
|
76
|
-
body,
|
|
77
|
-
ctxToken: getCookie(c, resolvedCookieConfig.ctx.name)
|
|
78
|
-
});
|
|
79
|
-
return c.body(null, 200);
|
|
80
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
81
|
-
const body = await c.req.json();
|
|
82
|
-
return c.json(await confirmUpload({
|
|
83
|
-
provider,
|
|
84
|
-
router: config.router,
|
|
85
|
-
body,
|
|
86
|
-
ctxToken: getCookie(c, resolvedCookieConfig.ctx.name)
|
|
87
|
-
}));
|
|
88
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
89
|
-
const body = await c.req.json();
|
|
90
|
-
return c.json(await deleteFile({
|
|
91
|
-
provider,
|
|
92
|
-
router: config.router,
|
|
93
|
-
body,
|
|
94
|
-
ctxToken: getCookie(c, resolvedCookieConfig.ctx.name)
|
|
95
|
-
}));
|
|
96
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
97
|
-
const url = c.req.query('url');
|
|
98
|
-
if (typeof url === 'string') {
|
|
99
|
-
const proxyRes = await fetchProxyFile({
|
|
100
|
-
cookieHeader: c.req.header('cookie'),
|
|
101
|
-
url
|
|
102
|
-
});
|
|
103
|
-
return new Response(proxyRes.body === null ? null : Buffer.from(proxyRes.body), {
|
|
104
|
-
status: proxyRes.status,
|
|
105
|
-
headers: {
|
|
106
|
-
'Content-Type': proxyRes.contentType
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
} else {
|
|
110
|
-
return c.body(null, 400);
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
return c.body(null, 404);
|
|
114
|
-
}
|
|
115
|
-
} catch (err) {
|
|
116
|
-
if (err instanceof EdgeStoreError) {
|
|
117
|
-
log[err.level](err.formattedMessage());
|
|
118
|
-
if (err.cause) log[err.level](err.cause);
|
|
119
|
-
return c.json(err.formattedJson(), EDGE_STORE_ERROR_CODES[err.code]);
|
|
120
|
-
} else {
|
|
121
|
-
log.error(err);
|
|
122
|
-
return c.json(new EdgeStoreError({
|
|
123
|
-
message: 'Internal Server Error',
|
|
124
|
-
code: 'SERVER_ERROR'
|
|
125
|
-
}).formattedJson(), 500);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export { createEdgeStoreHonoHandler };
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../../shared.mjs';
|
|
6
|
-
|
|
7
|
-
function createEdgeStoreNextHandler(config) {
|
|
8
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
9
|
-
const log = new Logger(config.logLevel);
|
|
10
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
11
|
-
log.debug('Creating EdgeStore Next handler (app adapter)');
|
|
12
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
13
|
-
return async (req)=>{
|
|
14
|
-
try {
|
|
15
|
-
if (!('nextUrl' in req)) throw new EdgeStoreError({
|
|
16
|
-
message: 'Error running the app adapter. Make sure you are importing the correct adapter in your router configuration',
|
|
17
|
-
code: 'SERVER_ERROR'
|
|
18
|
-
});
|
|
19
|
-
const pathname = req.nextUrl.pathname;
|
|
20
|
-
if (matchPath(pathname, '/health')) {
|
|
21
|
-
return new Response('OK', {
|
|
22
|
-
status: 200
|
|
23
|
-
});
|
|
24
|
-
} else if (matchPath(pathname, '/init')) {
|
|
25
|
-
let ctx = {};
|
|
26
|
-
try {
|
|
27
|
-
ctx = 'createContext' in config ? await config.createContext({
|
|
28
|
-
req
|
|
29
|
-
}) : {};
|
|
30
|
-
} catch (err) {
|
|
31
|
-
throw new EdgeStoreError({
|
|
32
|
-
message: 'Error creating context',
|
|
33
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
34
|
-
cause: err instanceof Error ? err : undefined
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const { newCookies, ...body } = await init({
|
|
38
|
-
ctx,
|
|
39
|
-
provider,
|
|
40
|
-
router: config.router,
|
|
41
|
-
cookieConfig
|
|
42
|
-
});
|
|
43
|
-
const res = new Response(JSON.stringify(body), {
|
|
44
|
-
status: 200,
|
|
45
|
-
headers: {
|
|
46
|
-
'Content-Type': 'application/json'
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
for (const cookie of newCookies){
|
|
50
|
-
res.headers.append('Set-Cookie', cookie);
|
|
51
|
-
}
|
|
52
|
-
return res;
|
|
53
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
54
|
-
const res = await requestUpload({
|
|
55
|
-
provider,
|
|
56
|
-
router: config.router,
|
|
57
|
-
body: await req.json(),
|
|
58
|
-
ctxToken: req.cookies.get(resolvedCookieConfig.ctx.name)?.value
|
|
59
|
-
});
|
|
60
|
-
return new Response(JSON.stringify(res), {
|
|
61
|
-
status: 200,
|
|
62
|
-
headers: {
|
|
63
|
-
'Content-Type': 'application/json'
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
67
|
-
const res = await requestUploadParts({
|
|
68
|
-
provider,
|
|
69
|
-
router: config.router,
|
|
70
|
-
body: await req.json(),
|
|
71
|
-
ctxToken: req.cookies.get(resolvedCookieConfig.ctx.name)?.value
|
|
72
|
-
});
|
|
73
|
-
return new Response(JSON.stringify(res), {
|
|
74
|
-
status: 200,
|
|
75
|
-
headers: {
|
|
76
|
-
'Content-Type': 'application/json'
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
80
|
-
await completeMultipartUpload({
|
|
81
|
-
provider,
|
|
82
|
-
router: config.router,
|
|
83
|
-
body: await req.json(),
|
|
84
|
-
ctxToken: req.cookies.get(resolvedCookieConfig.ctx.name)?.value
|
|
85
|
-
});
|
|
86
|
-
return new Response(null, {
|
|
87
|
-
status: 200
|
|
88
|
-
});
|
|
89
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
90
|
-
const res = await confirmUpload({
|
|
91
|
-
provider,
|
|
92
|
-
router: config.router,
|
|
93
|
-
body: await req.json(),
|
|
94
|
-
ctxToken: req.cookies.get(resolvedCookieConfig.ctx.name)?.value
|
|
95
|
-
});
|
|
96
|
-
return new Response(JSON.stringify(res), {
|
|
97
|
-
status: 200,
|
|
98
|
-
headers: {
|
|
99
|
-
'Content-Type': 'application/json'
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
103
|
-
const res = await deleteFile({
|
|
104
|
-
provider,
|
|
105
|
-
router: config.router,
|
|
106
|
-
body: await req.json(),
|
|
107
|
-
ctxToken: req.cookies.get(resolvedCookieConfig.ctx.name)?.value
|
|
108
|
-
});
|
|
109
|
-
return new Response(JSON.stringify(res), {
|
|
110
|
-
status: 200,
|
|
111
|
-
headers: {
|
|
112
|
-
'Content-Type': 'application/json'
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
116
|
-
const url = req.nextUrl.searchParams.get('url');
|
|
117
|
-
if (typeof url === 'string') {
|
|
118
|
-
const proxyRes = await fetchProxyFile({
|
|
119
|
-
cookieHeader: req.cookies.toString(),
|
|
120
|
-
url
|
|
121
|
-
});
|
|
122
|
-
return new Response(proxyRes.body, {
|
|
123
|
-
status: proxyRes.status,
|
|
124
|
-
headers: {
|
|
125
|
-
'Content-Type': proxyRes.contentType
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
} else {
|
|
129
|
-
return new Response(null, {
|
|
130
|
-
status: 400
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
} else {
|
|
134
|
-
return new Response(null, {
|
|
135
|
-
status: 404
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
} catch (err) {
|
|
139
|
-
if (err instanceof EdgeStoreError) {
|
|
140
|
-
log[err.level](err.formattedMessage());
|
|
141
|
-
if (err.cause) log[err.level](err.cause);
|
|
142
|
-
return new Response(JSON.stringify(err.formattedJson()), {
|
|
143
|
-
status: EDGE_STORE_ERROR_CODES[err.code],
|
|
144
|
-
headers: {
|
|
145
|
-
'Content-Type': 'application/json'
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
log.error(err);
|
|
150
|
-
return new Response(JSON.stringify(new EdgeStoreError({
|
|
151
|
-
message: 'Internal server error',
|
|
152
|
-
code: 'SERVER_ERROR'
|
|
153
|
-
}).formattedJson()), {
|
|
154
|
-
status: 500,
|
|
155
|
-
headers: {
|
|
156
|
-
'Content-Type': 'application/json'
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export { createEdgeStoreNextHandler };
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
|
-
import Logger from '../../../libs/logger.mjs';
|
|
3
|
-
import { matchPath } from '../../../libs/utils.mjs';
|
|
4
|
-
import { EdgeStoreProvider } from '../../../providers/edgestore/index.mjs';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../../shared.mjs';
|
|
6
|
-
|
|
7
|
-
function createEdgeStoreNextHandler(config) {
|
|
8
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
9
|
-
const log = new Logger(config.logLevel);
|
|
10
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
11
|
-
log.debug('Creating EdgeStore Next handler (pages adapter)');
|
|
12
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
13
|
-
return async (req, res)=>{
|
|
14
|
-
try {
|
|
15
|
-
if (!('json' in res)) throw new EdgeStoreError({
|
|
16
|
-
message: 'Error running the pages adapter. Make sure you are importing the correct adapter in your router configuration',
|
|
17
|
-
code: 'SERVER_ERROR'
|
|
18
|
-
});
|
|
19
|
-
const pathname = req.url ?? '';
|
|
20
|
-
if (matchPath(pathname, '/health')) {
|
|
21
|
-
res.send('OK');
|
|
22
|
-
} else if (matchPath(pathname, '/init')) {
|
|
23
|
-
let ctx = {};
|
|
24
|
-
try {
|
|
25
|
-
ctx = 'createContext' in config ? await config.createContext({
|
|
26
|
-
req,
|
|
27
|
-
res
|
|
28
|
-
}) : {};
|
|
29
|
-
} catch (err) {
|
|
30
|
-
throw new EdgeStoreError({
|
|
31
|
-
message: 'Error creating context',
|
|
32
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
33
|
-
cause: err instanceof Error ? err : undefined
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
const { newCookies, ...body } = await init({
|
|
37
|
-
ctx,
|
|
38
|
-
provider,
|
|
39
|
-
router: config.router,
|
|
40
|
-
cookieConfig
|
|
41
|
-
});
|
|
42
|
-
res.setHeader('Set-Cookie', newCookies);
|
|
43
|
-
res.json(body);
|
|
44
|
-
} else if (matchPath(pathname, '/request-upload')) {
|
|
45
|
-
res.json(await requestUpload({
|
|
46
|
-
provider,
|
|
47
|
-
router: config.router,
|
|
48
|
-
body: req.body,
|
|
49
|
-
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
50
|
-
}));
|
|
51
|
-
} else if (matchPath(pathname, '/request-upload-parts')) {
|
|
52
|
-
res.json(await requestUploadParts({
|
|
53
|
-
provider,
|
|
54
|
-
router: config.router,
|
|
55
|
-
body: req.body,
|
|
56
|
-
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
57
|
-
}));
|
|
58
|
-
} else if (matchPath(pathname, '/complete-multipart-upload')) {
|
|
59
|
-
await completeMultipartUpload({
|
|
60
|
-
provider,
|
|
61
|
-
router: config.router,
|
|
62
|
-
body: req.body,
|
|
63
|
-
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
64
|
-
});
|
|
65
|
-
res.status(200).end();
|
|
66
|
-
} else if (matchPath(pathname, '/confirm-upload')) {
|
|
67
|
-
res.json(await confirmUpload({
|
|
68
|
-
provider,
|
|
69
|
-
router: config.router,
|
|
70
|
-
body: req.body,
|
|
71
|
-
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
72
|
-
}));
|
|
73
|
-
} else if (matchPath(pathname, '/delete-file')) {
|
|
74
|
-
res.json(await deleteFile({
|
|
75
|
-
provider,
|
|
76
|
-
router: config.router,
|
|
77
|
-
body: req.body,
|
|
78
|
-
ctxToken: req.cookies[resolvedCookieConfig.ctx.name]
|
|
79
|
-
}));
|
|
80
|
-
} else if (matchPath(pathname, '/proxy-file')) {
|
|
81
|
-
const { url } = req.query;
|
|
82
|
-
if (typeof url === 'string') {
|
|
83
|
-
const proxyRes = await fetchProxyFile({
|
|
84
|
-
cookieHeader: req.headers.cookie,
|
|
85
|
-
url
|
|
86
|
-
});
|
|
87
|
-
res.setHeader('Content-Type', proxyRes.contentType);
|
|
88
|
-
res.status(proxyRes.status);
|
|
89
|
-
res.end(proxyRes.body === null ? undefined : Buffer.from(proxyRes.body));
|
|
90
|
-
} else {
|
|
91
|
-
res.status(400).end();
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
res.status(404).end();
|
|
95
|
-
}
|
|
96
|
-
} catch (err) {
|
|
97
|
-
if (err instanceof EdgeStoreError) {
|
|
98
|
-
log[err.level](err.formattedMessage());
|
|
99
|
-
if (err.cause) log[err.level](err.cause);
|
|
100
|
-
res.status(EDGE_STORE_ERROR_CODES[err.code]).json(err.formattedJson());
|
|
101
|
-
} else {
|
|
102
|
-
log.error(err);
|
|
103
|
-
res.status(500).send(new EdgeStoreError({
|
|
104
|
-
message: 'Internal Server Error',
|
|
105
|
-
code: 'SERVER_ERROR'
|
|
106
|
-
}).formattedJson());
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export { createEdgeStoreNextHandler };
|