better-convex 0.6.4 → 0.7.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/dist/aggregate/index.d.ts +390 -0
- package/dist/aggregate/index.js +37 -0
- package/dist/{auth-client → auth/client}/index.js +32 -15
- package/dist/auth/http/index.d.ts +63 -0
- package/dist/auth/http/index.js +429 -0
- package/dist/auth/index.d.ts +19009 -192
- package/dist/auth/index.js +402 -688
- package/dist/{auth-nextjs → auth/nextjs}/index.d.ts +3 -5
- package/dist/{auth-nextjs → auth/nextjs}/index.js +16 -13
- package/dist/{caller-factory-B1FvYSKr.js → caller-factory-D3OuR1eI.js} +20 -14
- package/dist/cli.mjs +2601 -13
- package/dist/codegen-Cz1idI3-.mjs +969 -0
- package/dist/{create-schema-orm-DplxTtYj.js → create-schema-orm-69VF4CFV.js} +4 -3
- package/dist/crpc/index.d.ts +2 -2
- package/dist/crpc/index.js +3 -3
- package/dist/{http-types-BRLY10NX.d.ts → http-types-BCf2wCgp.d.ts} +25 -25
- package/dist/meta-utils-DDVYp9Xf.js +117 -0
- package/dist/orm/index.d.ts +4 -3012
- package/dist/orm/index.js +9631 -2
- package/dist/{index-BQkhP2ny.d.ts → procedure-caller-CcjtUFvL.d.ts} +211 -74
- package/dist/query-context-BDSis9rT.js +1518 -0
- package/dist/query-context-DGExXZIV.d.ts +42 -0
- package/dist/react/index.d.ts +31 -35
- package/dist/react/index.js +152 -59
- package/dist/rsc/index.d.ts +4 -7
- package/dist/rsc/index.js +14 -10
- package/dist/runtime-B9xQFY8W.js +2280 -0
- package/dist/server/index.d.ts +3 -4
- package/dist/server/index.js +384 -10
- package/dist/{types-o-5rYcTr.d.ts → types-CIBGEYXq.d.ts} +4 -3
- package/dist/types-DgwvxKbT.d.ts +4 -0
- package/dist/watcher.mjs +8 -8
- package/dist/where-clause-compiler-CRP-i1Qa.d.ts +3463 -0
- package/package.json +15 -11
- package/dist/codegen-DkpPBVPn.mjs +0 -189
- package/dist/context-utils-DSuX99Da.d.ts +0 -17
- package/dist/meta-utils-DCpLSBWB.js +0 -41
- package/dist/orm-BKc-pwj_.js +0 -8821
- /package/dist/{auth-client → auth/client}/index.d.ts +0 -0
- /package/dist/{auth-config → auth/config}/index.d.ts +0 -0
- /package/dist/{auth-config → auth/config}/index.js +0 -0
- /package/dist/{create-schema-DhWXOhnU.js → create-schema-BdZOL6ns.js} +0 -0
- /package/dist/{customFunctions-C1okqCzL.js → customFunctions-CZnCwoR3.js} +0 -0
- /package/dist/{error-BZUhlhYz.js → error-Be4OcwwD.js} +0 -0
- /package/dist/{query-options-BL1Q0X7q.js → query-options-B0c1b6pZ.js} +0 -0
- /package/dist/{transformer-CTNSPjwp.js → transformer-Dh0w2py0.js} +0 -0
- /package/dist/{types-jftzhhuc.d.ts → types-DwGkkq2s.d.ts} +0 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { ROUTABLE_HTTP_METHODS, httpActionGeneric, httpRouter } from "convex/server";
|
|
2
|
+
|
|
3
|
+
//#region src/auth/error-response.ts
|
|
4
|
+
const isApiErrorLike = (error) => !!error && typeof error === "object" && (error.name === "APIError" && "statusCode" in error || typeof error.statusCode === "number");
|
|
5
|
+
const toResponseInit = (error) => {
|
|
6
|
+
const init = {
|
|
7
|
+
headers: new Headers(error.headers ?? {}),
|
|
8
|
+
status: typeof error.statusCode === "number" ? error.statusCode : 500
|
|
9
|
+
};
|
|
10
|
+
if (typeof error.status === "string") init.statusText = error.status;
|
|
11
|
+
return init;
|
|
12
|
+
};
|
|
13
|
+
const toAuthErrorResponse = (error) => {
|
|
14
|
+
if (!isApiErrorLike(error)) return null;
|
|
15
|
+
const init = toResponseInit(error);
|
|
16
|
+
const { body } = error;
|
|
17
|
+
if (body === void 0) return new Response(null, init);
|
|
18
|
+
if (typeof body === "string") {
|
|
19
|
+
if (init.headers instanceof Headers && !init.headers.has("content-type")) init.headers.set("content-type", "text/plain");
|
|
20
|
+
return new Response(body, init);
|
|
21
|
+
}
|
|
22
|
+
return Response.json(body, init);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/auth/middleware.ts
|
|
27
|
+
/**
|
|
28
|
+
* Create auth middleware that handles auth routes and OpenID well-known redirect.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { Hono } from 'hono';
|
|
33
|
+
* import { cors } from 'hono/cors';
|
|
34
|
+
* import { authMiddleware } from 'better-convex/auth/http';
|
|
35
|
+
* import { createHttpRouter } from 'better-convex/server';
|
|
36
|
+
*
|
|
37
|
+
* const app = new Hono();
|
|
38
|
+
* app.use('/api/*', cors({ origin: process.env.SITE_URL, credentials: true }));
|
|
39
|
+
* app.use(authMiddleware(getAuth));
|
|
40
|
+
*
|
|
41
|
+
* export default createHttpRouter(app, httpRouter);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function authMiddleware(getAuth, opts = {}) {
|
|
45
|
+
const basePath = opts.basePath ?? "/api/auth";
|
|
46
|
+
return async (c, next) => {
|
|
47
|
+
if (c.req.path === "/.well-known/openid-configuration") return c.redirect(`${process.env.CONVEX_SITE_URL}${basePath}/convex/.well-known/openid-configuration`);
|
|
48
|
+
if (c.req.path.startsWith(basePath)) {
|
|
49
|
+
if (opts.verbose) console.log("request headers", c.req.raw.headers);
|
|
50
|
+
const auth = getAuth(c.env);
|
|
51
|
+
let response;
|
|
52
|
+
try {
|
|
53
|
+
response = await auth.handler(c.req.raw);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
const errorResponse = toAuthErrorResponse(error);
|
|
56
|
+
if (errorResponse) return errorResponse;
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
if (opts.verbose) console.log("response headers", response.headers);
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
return next();
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/internal/upstream/server/cors.ts
|
|
68
|
+
/** biome-ignore-all lint: vendored upstream helper source */
|
|
69
|
+
/**
|
|
70
|
+
* Vendored from upstream helper repository at commit c5e52c8.
|
|
71
|
+
* Source path: packages/convex_helpers/server/cors.ts
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* This file defines a CorsHttpRouter class that extends Convex's HttpRouter.
|
|
75
|
+
* It provides CORS (Cross-Origin Resource Sharing) support for HTTP routes.
|
|
76
|
+
*
|
|
77
|
+
* The CorsHttpRouter:
|
|
78
|
+
* 1. Allows specifying allowed origins for CORS.
|
|
79
|
+
* 2. Overrides the route method to add CORS headers to all non-OPTIONS requests.
|
|
80
|
+
* 3. Automatically adds an OPTIONS route to handle CORS preflight requests.
|
|
81
|
+
* 4. Uses the handleCors helper function to apply CORS headers consistently.
|
|
82
|
+
*
|
|
83
|
+
* This router simplifies the process of making Convex HTTP endpoints
|
|
84
|
+
* accessible to web applications hosted on different domains while
|
|
85
|
+
* maintaining proper CORS configuration.
|
|
86
|
+
*/
|
|
87
|
+
const DEFAULT_EXPOSED_HEADERS = ["Content-Range", "Accept-Ranges"];
|
|
88
|
+
/**
|
|
89
|
+
* Factory function to create a router that adds CORS support to routes.
|
|
90
|
+
* @param allowedOrigins An array of allowed origins for CORS.
|
|
91
|
+
* @returns A function to use instead of http.route when you want CORS.
|
|
92
|
+
*/
|
|
93
|
+
const corsRouter = (http, corsConfig) => {
|
|
94
|
+
const allowedExactMethodsByPath = /* @__PURE__ */ new Map();
|
|
95
|
+
const allowedPrefixMethodsByPath = /* @__PURE__ */ new Map();
|
|
96
|
+
return {
|
|
97
|
+
http,
|
|
98
|
+
route: (routeSpec) => {
|
|
99
|
+
const tempRouter = httpRouter();
|
|
100
|
+
tempRouter.exactRoutes = http.exactRoutes;
|
|
101
|
+
tempRouter.prefixRoutes = http.prefixRoutes;
|
|
102
|
+
const config = {
|
|
103
|
+
...corsConfig,
|
|
104
|
+
...routeSpec
|
|
105
|
+
};
|
|
106
|
+
const httpCorsHandler = handleCors({
|
|
107
|
+
originalHandler: routeSpec.handler,
|
|
108
|
+
allowedMethods: [routeSpec.method],
|
|
109
|
+
...config
|
|
110
|
+
});
|
|
111
|
+
/**
|
|
112
|
+
* Figure out what kind of route we're adding: exact or prefix and handle
|
|
113
|
+
* accordingly.
|
|
114
|
+
*/
|
|
115
|
+
if ("path" in routeSpec) {
|
|
116
|
+
let methods = allowedExactMethodsByPath.get(routeSpec.path);
|
|
117
|
+
if (!methods) {
|
|
118
|
+
methods = /* @__PURE__ */ new Set();
|
|
119
|
+
allowedExactMethodsByPath.set(routeSpec.path, methods);
|
|
120
|
+
}
|
|
121
|
+
methods.add(routeSpec.method);
|
|
122
|
+
tempRouter.route({
|
|
123
|
+
path: routeSpec.path,
|
|
124
|
+
method: routeSpec.method,
|
|
125
|
+
handler: httpCorsHandler
|
|
126
|
+
});
|
|
127
|
+
handleExactRoute(tempRouter, routeSpec, config, Array.from(methods));
|
|
128
|
+
} else {
|
|
129
|
+
let methods = allowedPrefixMethodsByPath.get(routeSpec.pathPrefix);
|
|
130
|
+
if (!methods) {
|
|
131
|
+
methods = /* @__PURE__ */ new Set();
|
|
132
|
+
allowedPrefixMethodsByPath.set(routeSpec.pathPrefix, methods);
|
|
133
|
+
}
|
|
134
|
+
methods.add(routeSpec.method);
|
|
135
|
+
tempRouter.route({
|
|
136
|
+
pathPrefix: routeSpec.pathPrefix,
|
|
137
|
+
method: routeSpec.method,
|
|
138
|
+
handler: httpCorsHandler
|
|
139
|
+
});
|
|
140
|
+
handlePrefixRoute(tempRouter, routeSpec, config, Array.from(methods));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Copy the routes from the temporary router to the main router.
|
|
144
|
+
*/
|
|
145
|
+
http.exactRoutes = new Map(tempRouter.exactRoutes);
|
|
146
|
+
http.prefixRoutes = new Map(tempRouter.prefixRoutes);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Handles exact route matching and adds OPTIONS handler.
|
|
152
|
+
* @param tempRouter Temporary router instance.
|
|
153
|
+
* @param routeSpec Route specification for exact matching.
|
|
154
|
+
*/
|
|
155
|
+
function handleExactRoute(tempRouter, routeSpec, config, allowedMethods) {
|
|
156
|
+
const currentMethodsForPath = tempRouter.exactRoutes.get(routeSpec.path);
|
|
157
|
+
/**
|
|
158
|
+
* Add the OPTIONS handler for the given path
|
|
159
|
+
*/
|
|
160
|
+
const optionsHandler = createOptionsHandlerForMethods(allowedMethods, config);
|
|
161
|
+
currentMethodsForPath?.set("OPTIONS", optionsHandler);
|
|
162
|
+
tempRouter.exactRoutes.set(routeSpec.path, new Map(currentMethodsForPath));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Handles prefix route matching and adds OPTIONS handler.
|
|
166
|
+
* @param tempRouter Temporary router instance.
|
|
167
|
+
* @param routeSpec Route specification for prefix matching.
|
|
168
|
+
*/
|
|
169
|
+
function handlePrefixRoute(tempRouter, routeSpec, config, allowedMethods) {
|
|
170
|
+
/**
|
|
171
|
+
* prefixRoutes is structured differently than exactRoutes. It's defined as
|
|
172
|
+
* a Map<string, Map<string, PublicHttpAction>> where the KEY is the
|
|
173
|
+
* METHOD and the VALUE is a map of paths and handlers.
|
|
174
|
+
*/
|
|
175
|
+
const optionsHandler = createOptionsHandlerForMethods(allowedMethods, config);
|
|
176
|
+
const optionsPrefixes = tempRouter.prefixRoutes.get("OPTIONS") || /* @__PURE__ */ new Map();
|
|
177
|
+
optionsPrefixes.set(routeSpec.pathPrefix, optionsHandler);
|
|
178
|
+
tempRouter.prefixRoutes.set("OPTIONS", optionsPrefixes);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates an OPTIONS handler for the given HTTP methods.
|
|
182
|
+
* @param methods Array of HTTP methods to be allowed.
|
|
183
|
+
* @returns A CORS-enabled OPTIONS handler.
|
|
184
|
+
*/
|
|
185
|
+
function createOptionsHandlerForMethods(methods, config) {
|
|
186
|
+
return handleCors({
|
|
187
|
+
...config,
|
|
188
|
+
allowedMethods: methods
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* handleCors() is a higher-order function that wraps a Convex HTTP action handler to add CORS support.
|
|
193
|
+
* It allows for customization of allowed HTTP methods and origins for cross-origin requests.
|
|
194
|
+
*
|
|
195
|
+
* The function:
|
|
196
|
+
* 1. Validates and normalizes the allowed HTTP methods.
|
|
197
|
+
* 2. Generates appropriate CORS headers based on the provided configuration.
|
|
198
|
+
* 3. Handles preflight OPTIONS requests automatically.
|
|
199
|
+
* 4. Wraps the original handler to add CORS headers to its response.
|
|
200
|
+
*
|
|
201
|
+
* This helper simplifies the process of making Convex HTTP actions accessible
|
|
202
|
+
* to web applications hosted on different domains.
|
|
203
|
+
*/
|
|
204
|
+
const SECONDS_IN_A_DAY = 3600 * 24;
|
|
205
|
+
/**
|
|
206
|
+
* Example CORS origins:
|
|
207
|
+
* - "*" (allow all origins)
|
|
208
|
+
* - "https://example.com" (allow a specific domain)
|
|
209
|
+
* - "https://*.example.com" (allow all subdomains of example.com)
|
|
210
|
+
* - "https://example1.com, https://example2.com" (allow multiple specific domains)
|
|
211
|
+
* - "null" (allow requests from data URLs or local files)
|
|
212
|
+
*/
|
|
213
|
+
const handleCors = ({ originalHandler, allowedMethods = ["OPTIONS"], allowedOrigins = ["*"], allowedHeaders = ["Content-Type"], exposedHeaders = DEFAULT_EXPOSED_HEADERS, allowCredentials = false, browserCacheMaxAge = SECONDS_IN_A_DAY, enforceAllowOrigins = false, debug = false }) => {
|
|
214
|
+
const filteredMethods = Array.from(new Set(allowedMethods.map((method) => method.toUpperCase()))).filter((method) => ROUTABLE_HTTP_METHODS.includes(method));
|
|
215
|
+
if (filteredMethods.length === 0) throw new Error("No valid HTTP methods provided");
|
|
216
|
+
/**
|
|
217
|
+
* Ensure OPTIONS is not duplicated if it was passed in
|
|
218
|
+
* E.g. if allowedMethods = ["GET", "OPTIONS"]
|
|
219
|
+
*/
|
|
220
|
+
const allowMethods = filteredMethods.includes("OPTIONS") ? filteredMethods.join(", ") : [...filteredMethods].join(", ");
|
|
221
|
+
/**
|
|
222
|
+
* Build up the set of CORS headers
|
|
223
|
+
*/
|
|
224
|
+
const commonHeaders = { Vary: "Origin" };
|
|
225
|
+
if (allowCredentials) commonHeaders["Access-Control-Allow-Credentials"] = "true";
|
|
226
|
+
if (exposedHeaders.length > 0) commonHeaders["Access-Control-Expose-Headers"] = exposedHeaders.join(", ");
|
|
227
|
+
async function parseAllowedOrigins(request) {
|
|
228
|
+
return Array.isArray(allowedOrigins) ? allowedOrigins : await allowedOrigins(request);
|
|
229
|
+
}
|
|
230
|
+
async function isAllowedOrigin(request) {
|
|
231
|
+
const requestOrigin = request.headers.get("origin");
|
|
232
|
+
if (!requestOrigin) return false;
|
|
233
|
+
return (await parseAllowedOrigins(request)).some((allowed) => {
|
|
234
|
+
if (allowed === "*") return true;
|
|
235
|
+
if (allowed === requestOrigin) return true;
|
|
236
|
+
if (allowed.startsWith("*.")) {
|
|
237
|
+
const wildcardDomain = allowed.slice(1);
|
|
238
|
+
const rootDomain = allowed.slice(2);
|
|
239
|
+
try {
|
|
240
|
+
const url = new URL(requestOrigin);
|
|
241
|
+
return url.protocol === "https:" && (url.hostname.endsWith(wildcardDomain) || url.hostname === rootDomain);
|
|
242
|
+
} catch {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Return our modified HTTP action
|
|
251
|
+
*/
|
|
252
|
+
return httpActionGeneric(async (ctx, request) => {
|
|
253
|
+
if (debug) console.log("CORS request", {
|
|
254
|
+
path: request.url,
|
|
255
|
+
origin: request.headers.get("origin"),
|
|
256
|
+
headers: request.headers,
|
|
257
|
+
method: request.method,
|
|
258
|
+
body: request.body
|
|
259
|
+
});
|
|
260
|
+
const requestOrigin = request.headers.get("origin");
|
|
261
|
+
const parsedAllowedOrigins = await parseAllowedOrigins(request);
|
|
262
|
+
if (debug) console.log("allowed origins", parsedAllowedOrigins);
|
|
263
|
+
let allowOrigins = null;
|
|
264
|
+
if (parsedAllowedOrigins.includes("*") && requestOrigin && !allowCredentials) allowOrigins = requestOrigin;
|
|
265
|
+
else if (requestOrigin) {
|
|
266
|
+
if (await isAllowedOrigin(request)) allowOrigins = requestOrigin;
|
|
267
|
+
}
|
|
268
|
+
if (enforceAllowOrigins && !allowOrigins) {
|
|
269
|
+
console.error(`Request from origin ${requestOrigin} blocked, missing from allowed origins: ${parsedAllowedOrigins.join()}`);
|
|
270
|
+
return new Response(null, { status: 403 });
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* OPTIONS has no handler and just returns headers
|
|
274
|
+
*/
|
|
275
|
+
if (request.method === "OPTIONS") {
|
|
276
|
+
const responseHeaders = new Headers({
|
|
277
|
+
...commonHeaders,
|
|
278
|
+
...allowOrigins ? { "Access-Control-Allow-Origin": allowOrigins } : {},
|
|
279
|
+
"Access-Control-Allow-Methods": allowMethods,
|
|
280
|
+
"Access-Control-Allow-Headers": allowedHeaders.join(", "),
|
|
281
|
+
"Access-Control-Max-Age": browserCacheMaxAge.toString()
|
|
282
|
+
});
|
|
283
|
+
if (debug) console.log("CORS OPTIONS response headers", responseHeaders);
|
|
284
|
+
return new Response(null, {
|
|
285
|
+
status: 204,
|
|
286
|
+
headers: responseHeaders
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* If the method is not OPTIONS, it must pass a handler
|
|
291
|
+
*/
|
|
292
|
+
if (!originalHandler) throw new Error("No PublicHttpAction provider to CORS handler");
|
|
293
|
+
const originalResponse = await ("_handler" in originalHandler ? originalHandler["_handler"] : originalHandler)(ctx, request);
|
|
294
|
+
/**
|
|
295
|
+
* Second, get a copy of the original response's headers and add the
|
|
296
|
+
* allow origin header if it's allowed
|
|
297
|
+
*/
|
|
298
|
+
const newHeaders = new Headers(originalResponse.headers);
|
|
299
|
+
if (allowOrigins) newHeaders.set("Access-Control-Allow-Origin", allowOrigins);
|
|
300
|
+
/**
|
|
301
|
+
* Third, add or update our other CORS headers
|
|
302
|
+
*/
|
|
303
|
+
Object.entries(commonHeaders).forEach(([key, value]) => {
|
|
304
|
+
newHeaders.set(key, value);
|
|
305
|
+
});
|
|
306
|
+
if (debug) console.log("CORS response headers", newHeaders);
|
|
307
|
+
/**
|
|
308
|
+
* Fourth, return the modified Response.
|
|
309
|
+
* A Response object is immutable, so we create a new one to return here.
|
|
310
|
+
*/
|
|
311
|
+
return new Response(originalResponse.body, {
|
|
312
|
+
status: originalResponse.status,
|
|
313
|
+
statusText: originalResponse.statusText,
|
|
314
|
+
headers: newHeaders
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/auth/registerRoutes.ts
|
|
321
|
+
/** biome-ignore-all lint/suspicious/noConsole: lib */
|
|
322
|
+
const registerRoutes = (http, getAuth, opts = {}) => {
|
|
323
|
+
const staticAuth = getAuth({});
|
|
324
|
+
const path = staticAuth.options.basePath ?? "/api/auth";
|
|
325
|
+
const authRequestHandler = httpActionGeneric(async (ctx, request) => {
|
|
326
|
+
if (opts?.verbose) {
|
|
327
|
+
console.log("options.baseURL", staticAuth.options.baseURL);
|
|
328
|
+
console.log("request headers", request.headers);
|
|
329
|
+
}
|
|
330
|
+
const auth = getAuth(ctx);
|
|
331
|
+
let response;
|
|
332
|
+
try {
|
|
333
|
+
response = await auth.handler(request);
|
|
334
|
+
} catch (error) {
|
|
335
|
+
const errorResponse = toAuthErrorResponse(error);
|
|
336
|
+
if (errorResponse) return errorResponse;
|
|
337
|
+
throw error;
|
|
338
|
+
}
|
|
339
|
+
if (opts?.verbose) console.log("response headers", response.headers);
|
|
340
|
+
return response;
|
|
341
|
+
});
|
|
342
|
+
if (!http.lookup("/.well-known/openid-configuration", "GET")) http.route({
|
|
343
|
+
handler: httpActionGeneric(async () => {
|
|
344
|
+
const url = `${process.env.CONVEX_SITE_URL}${path}/convex/.well-known/openid-configuration`;
|
|
345
|
+
return Response.redirect(url);
|
|
346
|
+
}),
|
|
347
|
+
method: "GET",
|
|
348
|
+
path: "/.well-known/openid-configuration"
|
|
349
|
+
});
|
|
350
|
+
if (!opts.cors) {
|
|
351
|
+
http.route({
|
|
352
|
+
handler: authRequestHandler,
|
|
353
|
+
method: "GET",
|
|
354
|
+
pathPrefix: `${path}/`
|
|
355
|
+
});
|
|
356
|
+
http.route({
|
|
357
|
+
handler: authRequestHandler,
|
|
358
|
+
method: "POST",
|
|
359
|
+
pathPrefix: `${path}/`
|
|
360
|
+
});
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const corsOpts = typeof opts.cors === "boolean" ? {
|
|
364
|
+
allowedHeaders: [],
|
|
365
|
+
allowedOrigins: [],
|
|
366
|
+
exposedHeaders: []
|
|
367
|
+
} : opts.cors;
|
|
368
|
+
let trustedOriginsOption;
|
|
369
|
+
const cors = corsRouter(http, {
|
|
370
|
+
allowCredentials: true,
|
|
371
|
+
allowedHeaders: [
|
|
372
|
+
"Content-Type",
|
|
373
|
+
"Better-Auth-Cookie",
|
|
374
|
+
"Authorization"
|
|
375
|
+
].concat(corsOpts.allowedHeaders ?? []),
|
|
376
|
+
debug: opts?.verbose,
|
|
377
|
+
enforceAllowOrigins: false,
|
|
378
|
+
exposedHeaders: ["Set-Better-Auth-Cookie"].concat(corsOpts.exposedHeaders ?? []),
|
|
379
|
+
allowedOrigins: async (request) => {
|
|
380
|
+
trustedOriginsOption = trustedOriginsOption ?? (await staticAuth.$context).options.trustedOrigins ?? [];
|
|
381
|
+
return (Array.isArray(trustedOriginsOption) ? trustedOriginsOption : await trustedOriginsOption?.(request) ?? []).map((origin) => origin.endsWith("*") && origin.length > 1 ? origin.slice(0, -1) : origin).concat(corsOpts.allowedOrigins ?? []);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
cors.route({
|
|
385
|
+
handler: authRequestHandler,
|
|
386
|
+
method: "GET",
|
|
387
|
+
pathPrefix: `${path}/`
|
|
388
|
+
});
|
|
389
|
+
cors.route({
|
|
390
|
+
handler: authRequestHandler,
|
|
391
|
+
method: "POST",
|
|
392
|
+
pathPrefix: `${path}/`
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/auth-http/index.ts
|
|
398
|
+
/**
|
|
399
|
+
* Install Convex-safe polyfills required by Better Auth's HTTP handling.
|
|
400
|
+
* This runs automatically when importing `better-convex/auth/http`.
|
|
401
|
+
*/
|
|
402
|
+
function installAuthHttpPolyfills() {
|
|
403
|
+
if (typeof MessageChannel !== "undefined") return;
|
|
404
|
+
class MockMessagePort {
|
|
405
|
+
onmessage;
|
|
406
|
+
onmessageerror;
|
|
407
|
+
addEventListener() {}
|
|
408
|
+
close() {}
|
|
409
|
+
dispatchEvent(_event) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
postMessage(_message, _transfer = []) {}
|
|
413
|
+
removeEventListener() {}
|
|
414
|
+
start() {}
|
|
415
|
+
}
|
|
416
|
+
class MockMessageChannel {
|
|
417
|
+
port1;
|
|
418
|
+
port2;
|
|
419
|
+
constructor() {
|
|
420
|
+
this.port1 = new MockMessagePort();
|
|
421
|
+
this.port2 = new MockMessagePort();
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
globalThis.MessageChannel = MockMessageChannel;
|
|
425
|
+
}
|
|
426
|
+
installAuthHttpPolyfills();
|
|
427
|
+
|
|
428
|
+
//#endregion
|
|
429
|
+
export { authMiddleware, installAuthHttpPolyfills, registerRoutes };
|