@edgestore/server 1.0.0-next.0 → 1.0.0-next.2
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 +11 -5
- package/dist/adapters/astro/index.d.ts +7 -12
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +14 -161
- 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 +7 -12
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +24 -94
- package/dist/adapters/fastify/index.d.ts +7 -12
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +23 -121
- package/dist/adapters/hono/index.d.ts +7 -12
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +14 -121
- package/dist/adapters/next/app/index.d.ts +7 -12
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +21 -148
- package/dist/adapters/next/pages/index.d.ts +7 -12
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +34 -95
- package/dist/adapters/remix/index.d.ts +7 -12
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +14 -136
- package/dist/adapters/shared.d.ts +140 -55
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +188 -179
- package/dist/adapters/start/index.d.ts +7 -12
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +14 -169
- 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 +120 -142
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +13 -218
- package/dist/core/index.d.ts +20 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -2
- 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 +20 -0
- package/dist/core/routerRules.d.ts.map +1 -0
- package/dist/core/routerRules.js +42 -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 +3 -0
- 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/logger.d.ts +2 -1
- package/dist/libs/logger.d.ts.map +1 -1
- package/dist/libs/logger.js +1 -1
- 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 +304 -136
- 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/package.json +17 -20
- package/providers/azure-blob/index.d.ts +1 -0
- package/providers/azure-blob/index.js +1 -0
- 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 +237 -261
- package/src/adapters/start/index.ts +28 -200
- package/src/core/client/bucketClient.ts +476 -0
- package/src/core/client/index.ts +301 -428
- package/src/core/index.ts +42 -1
- package/src/core/provider.ts +80 -0
- package/src/core/routerRules.ts +63 -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/core/sdk/index.d.ts +0 -282
- package/dist/core/sdk/index.d.ts.map +0 -1
- package/dist/core/sdk/index.js +0 -253
- package/dist/providers/aws/index.d.ts.map +0 -1
- package/dist/providers/aws/index.js +0 -102
- 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 -84
- package/dist/version.js +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 -542
- package/src/providers/aws/index.ts +0 -239
- package/src/providers/azure/index.ts +0 -141
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
2
|
import { type FastifyReply, type FastifyRequest } from 'fastify';
|
|
3
3
|
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
-
import { type
|
|
4
|
+
import { type CreateContextConfig } from '../dispatcher.js';
|
|
5
|
+
import type { CookieConfig, HandlerEdgeStore } from '../shared.js';
|
|
5
6
|
export type CreateContextOptions = {
|
|
6
7
|
req: FastifyRequest;
|
|
7
8
|
reply: FastifyReply;
|
|
8
9
|
};
|
|
9
|
-
export type Config<TCtx> = {
|
|
10
|
-
|
|
11
|
-
router: EdgeStoreRouter<TCtx>;
|
|
10
|
+
export type Config<TCtx extends AnyContext> = {
|
|
11
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
12
12
|
logLevel?: LogLevel;
|
|
13
13
|
cookieConfig?: CookieConfig;
|
|
14
|
-
} &
|
|
15
|
-
|
|
16
|
-
router: EdgeStoreRouter<TCtx>;
|
|
17
|
-
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
18
|
-
cookieConfig?: CookieConfig;
|
|
19
|
-
});
|
|
20
|
-
export declare function createEdgeStoreFastifyHandler<TCtx>(config: Config<TCtx>): (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
|
|
14
|
+
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
15
|
+
export declare function createEdgeStoreFastifyHandler<TCtx extends AnyContext>(config: Config<TCtx>): (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
|
|
21
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAC;AACjE,OAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAIL,KAAK,mBAAmB,EACzB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEhE,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,cAAc,CAAC;IACpB,KAAK,EAAE,YAAY,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,UAAU,IAAI;IAC5C,SAAS,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,GAAG,mBAAmB,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;AAEpD,wBAAgB,6BAA6B,CAAC,IAAI,SAAS,UAAU,EACnE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,IAKN,KAAK,cAAc,EAAE,OAAO,YAAY,KAAG,OAAO,CAAC,IAAI,CAAC,CAkCvE"}
|
|
@@ -1,134 +1,36 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
1
|
import Logger from '../../libs/logger.js';
|
|
3
|
-
import {
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
2
|
+
import { dispatchEdgeStoreRequest, toNodeDispatchResponse, resolveContext } from '../dispatcher.js';
|
|
6
3
|
|
|
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
4
|
function createEdgeStoreFastifyHandler(config) {
|
|
25
|
-
const { provider = EdgeStoreProvider(), cookieConfig } = config;
|
|
26
5
|
const log = new Logger(config.logLevel);
|
|
27
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
28
6
|
log.debug('Creating EdgeStore Fastify handler');
|
|
29
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
30
7
|
return async (req, reply)=>{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
8
|
+
const url = new URL(req.url, 'http://edgestore.local');
|
|
9
|
+
const response = await dispatchEdgeStoreRequest({
|
|
10
|
+
edgestore: config.edgestore,
|
|
11
|
+
logger: log,
|
|
12
|
+
cookieConfig: config.cookieConfig,
|
|
13
|
+
request: {
|
|
14
|
+
pathname: url.pathname,
|
|
15
|
+
readJson: async ()=>req.body,
|
|
16
|
+
getQuery: (name)=>{
|
|
17
|
+
const value = req.query[name];
|
|
18
|
+
return typeof value === 'string' ? value : url.searchParams.get(name) ?? undefined;
|
|
19
|
+
},
|
|
20
|
+
cookieHeader: req.headers.cookie,
|
|
21
|
+
cookies: 'cookies' in req ? req.cookies : undefined,
|
|
22
|
+
createContext: ()=>resolveContext(config, {
|
|
40
23
|
req,
|
|
41
24
|
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());
|
|
25
|
+
})
|
|
130
26
|
}
|
|
27
|
+
});
|
|
28
|
+
const normalized = await toNodeDispatchResponse(response);
|
|
29
|
+
for (const [name, value] of normalized.headers){
|
|
30
|
+
void reply.header(name, value);
|
|
131
31
|
}
|
|
32
|
+
void reply.status(normalized.status);
|
|
33
|
+
return reply.send(normalized.body);
|
|
132
34
|
};
|
|
133
35
|
}
|
|
134
36
|
|
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
2
|
import { type Context as HonoContext } from 'hono';
|
|
3
3
|
import { type LogLevel } from '../../libs/logger.js';
|
|
4
|
-
import { type
|
|
4
|
+
import { type CreateContextConfig } from '../dispatcher.js';
|
|
5
|
+
import { type CookieConfig, type HandlerEdgeStore } from '../shared.js';
|
|
5
6
|
export type CreateContextOptions = {
|
|
6
7
|
c: HonoContext;
|
|
7
8
|
};
|
|
8
|
-
export type Config<TCtx> = {
|
|
9
|
-
|
|
10
|
-
router: EdgeStoreRouter<TCtx>;
|
|
9
|
+
export type Config<TCtx extends AnyContext> = {
|
|
10
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
11
11
|
logLevel?: LogLevel;
|
|
12
12
|
cookieConfig?: CookieConfig;
|
|
13
|
-
} &
|
|
14
|
-
|
|
15
|
-
router: EdgeStoreRouter<TCtx>;
|
|
16
|
-
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
17
|
-
cookieConfig?: CookieConfig;
|
|
18
|
-
});
|
|
19
|
-
export declare function createEdgeStoreHonoHandler<TCtx>(config: Config<TCtx>): (c: HonoContext) => Promise<Response>;
|
|
13
|
+
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
14
|
+
export declare function createEdgeStoreHonoHandler<TCtx extends AnyContext>(config: Config<TCtx>): (c: HonoContext) => Promise<Response>;
|
|
20
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/hono/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/hono/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,OAAO,IAAI,WAAW,EAAE,MAAM,MAAM,CAAC;AACnD,OAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAErE,MAAM,MAAM,oBAAoB,GAAG;IACjC,CAAC,EAAE,WAAW,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,UAAU,IAAI;IAC5C,SAAS,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,GAAG,mBAAmB,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;AAEpD,wBAAgB,0BAA0B,CAAC,IAAI,SAAS,UAAU,EAChE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,IAMN,GAAG,WAAW,KAAG,OAAO,CAAC,QAAQ,CAAC,CAgBjD"}
|
|
@@ -1,131 +1,24 @@
|
|
|
1
|
-
import { EdgeStoreError, EDGE_STORE_ERROR_CODES } from '@edgestore/shared';
|
|
2
1
|
import Logger from '../../libs/logger.js';
|
|
3
|
-
import {
|
|
4
|
-
import { EdgeStoreProvider } from '../../providers/edgestore/index.js';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../shared.js';
|
|
2
|
+
import { dispatchEdgeStoreRequest, resolveContext } from '../dispatcher.js';
|
|
6
3
|
|
|
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
4
|
function createEdgeStoreHonoHandler(config) {
|
|
15
|
-
const {
|
|
5
|
+
const { cookieConfig } = config;
|
|
16
6
|
const log = new Logger(config.logLevel);
|
|
17
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
18
7
|
log.debug('Creating EdgeStore Hono handler');
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
return async (c)=>await dispatchEdgeStoreRequest({
|
|
9
|
+
edgestore: config.edgestore,
|
|
10
|
+
logger: log,
|
|
11
|
+
cookieConfig,
|
|
12
|
+
request: {
|
|
13
|
+
pathname: new URL(c.req.url).pathname,
|
|
14
|
+
readJson: ()=>c.req.json(),
|
|
15
|
+
getQuery: (name)=>c.req.query(name),
|
|
16
|
+
cookieHeader: c.req.header('cookie'),
|
|
17
|
+
createContext: ()=>resolveContext(config, {
|
|
29
18
|
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);
|
|
19
|
+
})
|
|
126
20
|
}
|
|
127
|
-
}
|
|
128
|
-
};
|
|
21
|
+
});
|
|
129
22
|
}
|
|
130
23
|
|
|
131
24
|
export { createEdgeStoreHonoHandler };
|
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
2
|
import { type NextRequest } from 'next/server';
|
|
3
3
|
import { type LogLevel } from '../../../libs/logger.js';
|
|
4
|
-
import { type
|
|
4
|
+
import { type CreateContextConfig } from '../../dispatcher.js';
|
|
5
|
+
import { type CookieConfig, type HandlerEdgeStore } from '../../shared.js';
|
|
5
6
|
export type CreateContextOptions = {
|
|
6
7
|
req: NextRequest;
|
|
7
8
|
};
|
|
8
|
-
export type Config<TCtx> = {
|
|
9
|
-
|
|
10
|
-
router: EdgeStoreRouter<TCtx>;
|
|
9
|
+
export type Config<TCtx extends AnyContext> = {
|
|
10
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
11
11
|
logLevel?: LogLevel;
|
|
12
12
|
cookieConfig?: CookieConfig;
|
|
13
|
-
} &
|
|
14
|
-
|
|
15
|
-
router: EdgeStoreRouter<TCtx>;
|
|
16
|
-
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
17
|
-
cookieConfig?: CookieConfig;
|
|
18
|
-
});
|
|
19
|
-
export declare function createEdgeStoreNextHandler<TCtx>(config: Config<TCtx>): (req: NextRequest) => Promise<Response>;
|
|
13
|
+
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
14
|
+
export declare function createEdgeStoreNextHandler<TCtx extends AnyContext>(config: Config<TCtx>): (req: NextRequest) => Promise<Response>;
|
|
20
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/adapters/next/app/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/adapters/next/app/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAExE,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,WAAW,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,UAAU,IAAI;IAC5C,SAAS,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,GAAG,mBAAmB,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;AAEpD,wBAAgB,0BAA0B,CAAC,IAAI,SAAS,UAAU,EAChE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,IAMN,KAAK,WAAW,uBA2B/B"}
|
|
@@ -1,162 +1,35 @@
|
|
|
1
|
-
import { EdgeStoreError
|
|
1
|
+
import { EdgeStoreError } from '@edgestore/shared';
|
|
2
2
|
import Logger from '../../../libs/logger.js';
|
|
3
|
-
import {
|
|
4
|
-
import { EdgeStoreProvider } from '../../../providers/edgestore/index.js';
|
|
5
|
-
import { getCookieConfig, init, requestUpload, requestUploadParts, completeMultipartUpload, confirmUpload, deleteFile, fetchProxyFile } from '../../shared.js';
|
|
3
|
+
import { dispatchEdgeStoreRequest, resolveContext } from '../../dispatcher.js';
|
|
6
4
|
|
|
7
5
|
function createEdgeStoreNextHandler(config) {
|
|
8
|
-
const {
|
|
6
|
+
const { cookieConfig } = config;
|
|
9
7
|
const log = new Logger(config.logLevel);
|
|
10
|
-
globalThis._EDGE_STORE_LOGGER = log;
|
|
11
8
|
log.debug('Creating EdgeStore Next handler (app adapter)');
|
|
12
|
-
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
13
9
|
return async (req)=>{
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
if (!('nextUrl' in req)) {
|
|
11
|
+
const error = new EdgeStoreError({
|
|
16
12
|
message: 'Error running the app adapter. Make sure you are importing the correct adapter in your router configuration',
|
|
17
13
|
code: 'SERVER_ERROR'
|
|
18
14
|
});
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
}
|
|
15
|
+
return Response.json(error.formattedJson(), {
|
|
16
|
+
status: 500
|
|
158
17
|
});
|
|
159
18
|
}
|
|
19
|
+
return await dispatchEdgeStoreRequest({
|
|
20
|
+
edgestore: config.edgestore,
|
|
21
|
+
logger: log,
|
|
22
|
+
cookieConfig,
|
|
23
|
+
request: {
|
|
24
|
+
pathname: req.nextUrl.pathname,
|
|
25
|
+
readJson: ()=>req.json(),
|
|
26
|
+
getQuery: (name)=>req.nextUrl.searchParams.get(name) ?? undefined,
|
|
27
|
+
cookieHeader: req.headers?.get('cookie') ?? (req.cookies.toString() || undefined),
|
|
28
|
+
createContext: ()=>resolveContext(config, {
|
|
29
|
+
req
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
});
|
|
160
33
|
};
|
|
161
34
|
}
|
|
162
35
|
|
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type AnyContext } from '@edgestore/shared';
|
|
2
2
|
import { type NextApiRequest, type NextApiResponse } from 'next/types';
|
|
3
3
|
import { type LogLevel } from '../../../libs/logger.js';
|
|
4
|
-
import { type
|
|
4
|
+
import { type CreateContextConfig } from '../../dispatcher.js';
|
|
5
|
+
import type { CookieConfig, HandlerEdgeStore } from '../../shared.js';
|
|
5
6
|
export type CreateContextOptions = {
|
|
6
7
|
req: NextApiRequest;
|
|
7
8
|
res: NextApiResponse;
|
|
8
9
|
};
|
|
9
|
-
export type Config<TCtx> = {
|
|
10
|
-
|
|
11
|
-
router: EdgeStoreRouter<TCtx>;
|
|
10
|
+
export type Config<TCtx extends AnyContext> = {
|
|
11
|
+
edgestore: HandlerEdgeStore<TCtx>;
|
|
12
12
|
logLevel?: LogLevel;
|
|
13
13
|
cookieConfig?: CookieConfig;
|
|
14
|
-
} &
|
|
15
|
-
|
|
16
|
-
router: EdgeStoreRouter<TCtx>;
|
|
17
|
-
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
18
|
-
cookieConfig?: CookieConfig;
|
|
19
|
-
});
|
|
20
|
-
export declare function createEdgeStoreNextHandler<TCtx>(config: Config<TCtx>): (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
14
|
+
} & CreateContextConfig<TCtx, CreateContextOptions>;
|
|
15
|
+
export declare function createEdgeStoreNextHandler<TCtx extends AnyContext>(config: Config<TCtx>): (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
21
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/adapters/next/pages/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/adapters/next/pages/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AACvE,OAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAIL,KAAK,mBAAmB,EACzB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEnE,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,UAAU,IAAI;IAC5C,SAAS,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,GAAG,mBAAmB,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC;AAEpD,wBAAgB,0BAA0B,CAAC,IAAI,SAAS,UAAU,EAChE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,IAKN,KAAK,cAAc,EAAE,KAAK,eAAe,mBA8CxD"}
|