@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,223 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EDGE_STORE_ERROR_CODES,
|
|
3
|
-
EdgeStoreError,
|
|
4
|
-
type EdgeStoreErrorCodeKey,
|
|
5
|
-
type EdgeStoreRouter,
|
|
6
|
-
type MaybePromise,
|
|
7
|
-
type Provider,
|
|
8
|
-
} from '@edgestore/shared';
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
9
2
|
import type { APIContext } from 'astro';
|
|
10
3
|
import Logger, { type LogLevel } from '../../libs/logger';
|
|
11
|
-
import { matchPath } from '../../libs/utils';
|
|
12
|
-
import { EdgeStoreProvider } from '../../providers/edgestore';
|
|
13
4
|
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
type CompleteMultipartUploadBody,
|
|
23
|
-
type ConfirmUploadBody,
|
|
24
|
-
type CookieConfig,
|
|
25
|
-
type DeleteFileBody,
|
|
26
|
-
type RequestUploadBody,
|
|
27
|
-
type RequestUploadPartsParams,
|
|
28
|
-
} from '../shared';
|
|
29
|
-
|
|
30
|
-
export type Config<TCtx> = {
|
|
31
|
-
provider?: Provider;
|
|
32
|
-
router: EdgeStoreRouter<TCtx>;
|
|
5
|
+
dispatchEdgeStoreRequest,
|
|
6
|
+
resolveContext,
|
|
7
|
+
type CreateContextConfig,
|
|
8
|
+
} from '../dispatcher';
|
|
9
|
+
import type { CookieConfig, HandlerEdgeStore } from '../shared';
|
|
10
|
+
|
|
11
|
+
export type Config<TCtx extends AnyContext> = {
|
|
12
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
33
13
|
logLevel?: LogLevel;
|
|
34
14
|
cookieConfig?: CookieConfig;
|
|
35
|
-
} &
|
|
36
|
-
? object
|
|
37
|
-
: {
|
|
38
|
-
provider?: Provider;
|
|
39
|
-
router: EdgeStoreRouter<TCtx>;
|
|
40
|
-
createContext: (opts: APIContext) => MaybePromise<TCtx>;
|
|
41
|
-
cookieConfig?: CookieConfig;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
declare const globalThis: {
|
|
45
|
-
_EDGE_STORE_LOGGER: Logger;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// Helper to safely get cookies from Astro request
|
|
49
|
-
function getCookie(request: Request, name: string): string | undefined {
|
|
50
|
-
const cookieHeader = request.headers.get('cookie');
|
|
51
|
-
if (!cookieHeader) return undefined;
|
|
52
|
-
|
|
53
|
-
const cookies = cookieHeader
|
|
54
|
-
.split(';')
|
|
55
|
-
.reduce<Record<string, string>>((acc, cookie) => {
|
|
56
|
-
const [key, value] = cookie.trim().split('=');
|
|
57
|
-
if (key && value) acc[key] = value;
|
|
58
|
-
return acc;
|
|
59
|
-
}, {});
|
|
15
|
+
} & CreateContextConfig<TCtx, APIContext>;
|
|
60
16
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export function createEdgeStoreAstroHandler<TCtx>(config: Config<TCtx>) {
|
|
65
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
17
|
+
export function createEdgeStoreAstroHandler<TCtx extends AnyContext>(
|
|
18
|
+
config: Config<TCtx>,
|
|
19
|
+
) {
|
|
66
20
|
const log = new Logger(config.logLevel);
|
|
67
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
68
21
|
log.debug('Creating EdgeStore Astro handler');
|
|
69
22
|
|
|
70
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
71
|
-
|
|
72
23
|
return async (context: APIContext) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
throw new EdgeStoreError({
|
|
88
|
-
message: 'Error creating context',
|
|
89
|
-
code: 'CREATE_CONTEXT_ERROR',
|
|
90
|
-
cause: err instanceof Error ? err : undefined,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
const { newCookies, ...body } = await init({
|
|
94
|
-
ctx,
|
|
95
|
-
provider,
|
|
96
|
-
router: config.router,
|
|
97
|
-
cookieConfig,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const headers = new Headers();
|
|
101
|
-
headers.set('Content-Type', 'application/json');
|
|
102
|
-
|
|
103
|
-
// Set cookies
|
|
104
|
-
if (Array.isArray(newCookies)) {
|
|
105
|
-
for (const cookie of newCookies) {
|
|
106
|
-
headers.append('Set-Cookie', cookie);
|
|
107
|
-
}
|
|
108
|
-
} else if (newCookies) {
|
|
109
|
-
headers.append('Set-Cookie', newCookies);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return new Response(JSON.stringify(body), { headers });
|
|
113
|
-
} else if (matchPath(url.pathname, 'request-upload')) {
|
|
114
|
-
const body = (await request.json()) as RequestUploadBody;
|
|
115
|
-
const result = await requestUpload({
|
|
116
|
-
provider,
|
|
117
|
-
router: config.router,
|
|
118
|
-
body,
|
|
119
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name),
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
return new Response(JSON.stringify(result), {
|
|
123
|
-
headers: { 'Content-Type': 'application/json' },
|
|
124
|
-
});
|
|
125
|
-
} else if (matchPath(url.pathname, 'request-upload-parts')) {
|
|
126
|
-
const body = (await request.json()) as RequestUploadPartsParams;
|
|
127
|
-
const result = await requestUploadParts({
|
|
128
|
-
provider,
|
|
129
|
-
router: config.router,
|
|
130
|
-
body,
|
|
131
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name),
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
return new Response(JSON.stringify(result), {
|
|
135
|
-
headers: { 'Content-Type': 'application/json' },
|
|
136
|
-
});
|
|
137
|
-
} else if (matchPath(url.pathname, 'complete-multipart-upload')) {
|
|
138
|
-
const body = (await request.json()) as CompleteMultipartUploadBody;
|
|
139
|
-
await completeMultipartUpload({
|
|
140
|
-
provider,
|
|
141
|
-
router: config.router,
|
|
142
|
-
body,
|
|
143
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name),
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
return new Response(null, { status: 200 });
|
|
147
|
-
} else if (matchPath(url.pathname, 'confirm-upload')) {
|
|
148
|
-
const body = (await request.json()) as ConfirmUploadBody;
|
|
149
|
-
const result = await confirmUpload({
|
|
150
|
-
provider,
|
|
151
|
-
router: config.router,
|
|
152
|
-
body,
|
|
153
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name),
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
return new Response(JSON.stringify(result), {
|
|
157
|
-
headers: { 'Content-Type': 'application/json' },
|
|
158
|
-
});
|
|
159
|
-
} else if (matchPath(url.pathname, 'delete-file')) {
|
|
160
|
-
const body = (await request.json()) as DeleteFileBody;
|
|
161
|
-
const result = await deleteFile({
|
|
162
|
-
provider,
|
|
163
|
-
router: config.router,
|
|
164
|
-
body,
|
|
165
|
-
ctxToken: getCookie(request, resolvedCookieConfig.ctx.name),
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
return new Response(JSON.stringify(result), {
|
|
169
|
-
headers: { 'Content-Type': 'application/json' },
|
|
170
|
-
});
|
|
171
|
-
} else if (matchPath(url.pathname, 'proxy-file')) {
|
|
172
|
-
const url = new URL(request.url).searchParams.get('url');
|
|
173
|
-
|
|
174
|
-
if (typeof url === 'string') {
|
|
175
|
-
const proxyRes = await fetchProxyFile({
|
|
176
|
-
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
177
|
-
url,
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
const headers = new Headers();
|
|
181
|
-
headers.set('Content-Type', proxyRes.contentType);
|
|
182
|
-
|
|
183
|
-
return new Response(proxyRes.body, {
|
|
184
|
-
headers,
|
|
185
|
-
status: proxyRes.status,
|
|
186
|
-
});
|
|
187
|
-
} else {
|
|
188
|
-
return new Response(null, { status: 400 });
|
|
189
|
-
}
|
|
190
|
-
} else {
|
|
191
|
-
return new Response(null, { status: 404 });
|
|
192
|
-
}
|
|
193
|
-
} catch (err) {
|
|
194
|
-
if (err instanceof EdgeStoreError) {
|
|
195
|
-
log[err.level](err.formattedMessage());
|
|
196
|
-
if (err.cause) log[err.level](err.cause);
|
|
197
|
-
|
|
198
|
-
return new Response(JSON.stringify(err.formattedJson()), {
|
|
199
|
-
status: EDGE_STORE_ERROR_CODES[err.code as EdgeStoreErrorCodeKey],
|
|
200
|
-
headers: {
|
|
201
|
-
'Content-Type': 'application/json',
|
|
202
|
-
},
|
|
203
|
-
});
|
|
204
|
-
} else {
|
|
205
|
-
log.error(err);
|
|
206
|
-
return new Response(
|
|
207
|
-
JSON.stringify(
|
|
208
|
-
new EdgeStoreError({
|
|
209
|
-
message: 'Internal Server Error',
|
|
210
|
-
code: 'SERVER_ERROR',
|
|
211
|
-
}).formattedJson(),
|
|
212
|
-
),
|
|
213
|
-
{
|
|
214
|
-
status: 500,
|
|
215
|
-
headers: {
|
|
216
|
-
'Content-Type': 'application/json',
|
|
217
|
-
},
|
|
218
|
-
},
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
24
|
+
const { request } = context;
|
|
25
|
+
const url = new URL(request.url);
|
|
26
|
+
return await dispatchEdgeStoreRequest<TCtx>({
|
|
27
|
+
edgestore: config.edgestore,
|
|
28
|
+
logger: log,
|
|
29
|
+
cookieConfig: config.cookieConfig,
|
|
30
|
+
request: {
|
|
31
|
+
pathname: url.pathname,
|
|
32
|
+
readJson: () => request.json(),
|
|
33
|
+
getQuery: (name) => url.searchParams.get(name) ?? undefined,
|
|
34
|
+
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
35
|
+
createContext: () => resolveContext<TCtx, APIContext>(config, context),
|
|
36
|
+
},
|
|
37
|
+
});
|
|
222
38
|
};
|
|
223
39
|
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EDGE_STORE_ERROR_CODES,
|
|
3
|
+
EdgeStoreError,
|
|
4
|
+
type AnyContext,
|
|
5
|
+
type EdgeStoreErrorCodeKey,
|
|
6
|
+
type MaybePromise,
|
|
7
|
+
} from '@edgestore/shared';
|
|
8
|
+
import { parseCookie } from 'cookie';
|
|
9
|
+
import { type z } from 'zod';
|
|
10
|
+
import type { LoggerLike } from '../libs/logger';
|
|
11
|
+
import { matchPath } from '../libs/utils';
|
|
12
|
+
import {
|
|
13
|
+
completeMultipartUpload,
|
|
14
|
+
completeMultipartUploadBodySchema,
|
|
15
|
+
confirmUploads,
|
|
16
|
+
confirmUploadsBodySchema,
|
|
17
|
+
deleteFiles,
|
|
18
|
+
deleteFilesBodySchema,
|
|
19
|
+
fetchProxyFile,
|
|
20
|
+
getCookieConfig,
|
|
21
|
+
init,
|
|
22
|
+
requestUpload,
|
|
23
|
+
requestUploadBodySchema,
|
|
24
|
+
requestUploadParts,
|
|
25
|
+
requestUploadPartsBodySchema,
|
|
26
|
+
type CookieConfig,
|
|
27
|
+
type HandlerEdgeStore,
|
|
28
|
+
} from './shared';
|
|
29
|
+
|
|
30
|
+
export type EdgeStoreDispatchRequest<TCtx extends AnyContext> = {
|
|
31
|
+
pathname: string;
|
|
32
|
+
readJson: () => Promise<unknown>;
|
|
33
|
+
getQuery: (name: string) => string | undefined;
|
|
34
|
+
cookieHeader?: string;
|
|
35
|
+
cookies?: Readonly<Record<string, string | undefined>>;
|
|
36
|
+
createContext: () => MaybePromise<TCtx>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type CreateContextConfig<TCtx extends AnyContext, TOptions> =
|
|
40
|
+
TCtx extends Record<string, never>
|
|
41
|
+
? {
|
|
42
|
+
createContext?: (options: TOptions) => MaybePromise<TCtx>;
|
|
43
|
+
}
|
|
44
|
+
: {
|
|
45
|
+
createContext: (options: TOptions) => MaybePromise<TCtx>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function resolveContext<TCtx extends AnyContext, TOptions>(
|
|
49
|
+
config: object,
|
|
50
|
+
options: TOptions,
|
|
51
|
+
): MaybePromise<TCtx> {
|
|
52
|
+
return hasCreateContext<TCtx, TOptions>(config)
|
|
53
|
+
? config.createContext(options)
|
|
54
|
+
: ({} as TCtx);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasCreateContext<TCtx extends AnyContext, TOptions>(
|
|
58
|
+
config: object,
|
|
59
|
+
): config is {
|
|
60
|
+
createContext: (options: TOptions) => MaybePromise<TCtx>;
|
|
61
|
+
} {
|
|
62
|
+
return (
|
|
63
|
+
'createContext' in config && typeof config.createContext === 'function'
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function dispatchEdgeStoreRequest<
|
|
68
|
+
TCtx extends AnyContext,
|
|
69
|
+
>(params: {
|
|
70
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
71
|
+
request: EdgeStoreDispatchRequest<TCtx>;
|
|
72
|
+
logger: LoggerLike;
|
|
73
|
+
cookieConfig?: CookieConfig;
|
|
74
|
+
}): Promise<Response> {
|
|
75
|
+
const { edgestore, request, logger, cookieConfig } = params;
|
|
76
|
+
const { provider, router } = edgestore;
|
|
77
|
+
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
78
|
+
const cookieHeader =
|
|
79
|
+
request.cookieHeader ??
|
|
80
|
+
Object.entries(request.cookies ?? {})
|
|
81
|
+
.filter((entry): entry is [string, string] => entry[1] !== undefined)
|
|
82
|
+
.map(([name, value]) => `${name}=${value}`)
|
|
83
|
+
.join('; ');
|
|
84
|
+
const ctxToken =
|
|
85
|
+
request.cookies?.[resolvedCookieConfig.ctx.name] ??
|
|
86
|
+
(cookieHeader
|
|
87
|
+
? parseCookie(cookieHeader)[resolvedCookieConfig.ctx.name]
|
|
88
|
+
: undefined);
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
if (matchPath(request.pathname, '/health')) {
|
|
92
|
+
return new Response('OK');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (matchPath(request.pathname, '/init')) {
|
|
96
|
+
let ctx: TCtx;
|
|
97
|
+
try {
|
|
98
|
+
ctx = await request.createContext();
|
|
99
|
+
} catch (error) {
|
|
100
|
+
throw new EdgeStoreError({
|
|
101
|
+
message: 'Error creating context',
|
|
102
|
+
code: 'CREATE_CONTEXT_ERROR',
|
|
103
|
+
cause: error instanceof Error ? error : undefined,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const { newCookies, ...body } = await init({
|
|
107
|
+
ctx,
|
|
108
|
+
provider,
|
|
109
|
+
router,
|
|
110
|
+
logger,
|
|
111
|
+
cookieConfig,
|
|
112
|
+
});
|
|
113
|
+
const headers = new Headers({ 'Content-Type': 'application/json' });
|
|
114
|
+
for (const cookie of newCookies) headers.append('Set-Cookie', cookie);
|
|
115
|
+
return new Response(JSON.stringify(body), { status: 200, headers });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (matchPath(request.pathname, '/request-upload')) {
|
|
119
|
+
return jsonResponse(
|
|
120
|
+
await requestUpload({
|
|
121
|
+
provider,
|
|
122
|
+
router,
|
|
123
|
+
body: await parseRequestBody(request, requestUploadBodySchema),
|
|
124
|
+
ctxToken,
|
|
125
|
+
logger,
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (matchPath(request.pathname, '/request-upload-parts')) {
|
|
131
|
+
return jsonResponse(
|
|
132
|
+
await requestUploadParts({
|
|
133
|
+
provider,
|
|
134
|
+
router,
|
|
135
|
+
body: await parseRequestBody(request, requestUploadPartsBodySchema),
|
|
136
|
+
ctxToken,
|
|
137
|
+
logger,
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (matchPath(request.pathname, '/complete-multipart-upload')) {
|
|
143
|
+
await completeMultipartUpload({
|
|
144
|
+
provider,
|
|
145
|
+
router,
|
|
146
|
+
body: await parseRequestBody(
|
|
147
|
+
request,
|
|
148
|
+
completeMultipartUploadBodySchema,
|
|
149
|
+
),
|
|
150
|
+
ctxToken,
|
|
151
|
+
logger,
|
|
152
|
+
});
|
|
153
|
+
return new Response(null, { status: 200 });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (matchPath(request.pathname, '/confirm-uploads')) {
|
|
157
|
+
return jsonResponse(
|
|
158
|
+
await confirmUploads({
|
|
159
|
+
provider,
|
|
160
|
+
router,
|
|
161
|
+
body: await parseRequestBody(request, confirmUploadsBodySchema),
|
|
162
|
+
ctxToken,
|
|
163
|
+
logger,
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (matchPath(request.pathname, '/delete-files')) {
|
|
169
|
+
return jsonResponse(
|
|
170
|
+
await deleteFiles({
|
|
171
|
+
provider,
|
|
172
|
+
router,
|
|
173
|
+
body: await parseRequestBody(request, deleteFilesBodySchema),
|
|
174
|
+
ctxToken,
|
|
175
|
+
logger,
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (matchPath(request.pathname, '/proxy-file')) {
|
|
181
|
+
const url = request.getQuery('url');
|
|
182
|
+
if (url === undefined) return new Response(null, { status: 400 });
|
|
183
|
+
const result = await fetchProxyFile({
|
|
184
|
+
cookieHeader,
|
|
185
|
+
url,
|
|
186
|
+
});
|
|
187
|
+
return new Response(result.body, {
|
|
188
|
+
status: result.status,
|
|
189
|
+
headers: { 'Content-Type': result.contentType },
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return new Response(null, { status: 404 });
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (error instanceof EdgeStoreError) {
|
|
196
|
+
logger[error.level](error.formattedMessage());
|
|
197
|
+
if (error.cause) logger[error.level](error.cause);
|
|
198
|
+
return jsonResponse(
|
|
199
|
+
error.formattedJson(),
|
|
200
|
+
EDGE_STORE_ERROR_CODES[error.code as EdgeStoreErrorCodeKey],
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
logger.error(error);
|
|
205
|
+
return jsonResponse(
|
|
206
|
+
new EdgeStoreError({
|
|
207
|
+
message: 'Internal server error',
|
|
208
|
+
code: 'SERVER_ERROR',
|
|
209
|
+
}).formattedJson(),
|
|
210
|
+
500,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function toNodeDispatchResponse(response: Response) {
|
|
216
|
+
const contentType = response.headers.get('Content-Type') ?? '';
|
|
217
|
+
const headers: [string, string | string[]][] = [];
|
|
218
|
+
response.headers.forEach((value, name) => {
|
|
219
|
+
if (name !== 'set-cookie') {
|
|
220
|
+
headers.push([name === 'content-type' ? 'Content-Type' : name, value]);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
const setCookies = response.headers.getSetCookie();
|
|
224
|
+
if (setCookies.length > 0) headers.push(['Set-Cookie', setCookies]);
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
status: response.status,
|
|
228
|
+
headers,
|
|
229
|
+
isJson: contentType.includes('application/json'),
|
|
230
|
+
body:
|
|
231
|
+
response.body === null
|
|
232
|
+
? undefined
|
|
233
|
+
: contentType.includes('application/json')
|
|
234
|
+
? await response.json()
|
|
235
|
+
: contentType.startsWith('text/')
|
|
236
|
+
? await response.text()
|
|
237
|
+
: Buffer.from(await response.arrayBuffer()),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function jsonResponse(body: unknown, status = 200) {
|
|
242
|
+
return new Response(JSON.stringify(body), {
|
|
243
|
+
status,
|
|
244
|
+
headers: { 'Content-Type': 'application/json' },
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function parseRequestBody<TSchema extends z.ZodTypeAny>(
|
|
249
|
+
request: EdgeStoreDispatchRequest<AnyContext>,
|
|
250
|
+
schema: TSchema,
|
|
251
|
+
): Promise<z.output<TSchema>> {
|
|
252
|
+
try {
|
|
253
|
+
return await schema.parseAsync(await request.readJson());
|
|
254
|
+
} catch (error) {
|
|
255
|
+
throw new EdgeStoreError({
|
|
256
|
+
message: 'Invalid request body',
|
|
257
|
+
code: 'BAD_REQUEST',
|
|
258
|
+
cause: error instanceof Error ? error : undefined,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|