refactor: tweak router in entry.js

This commit is contained in:
qaz741wsd856 2025-12-29 12:11:14 +00:00
parent 249fd5e800
commit 99a511f9f0

View file

@ -146,6 +146,7 @@ function parseDownloadPath(path) {
export default { export default {
async fetch(request, env, ctx) { async fetch(request, env, ctx) {
const url = new URL(request.url); const url = new URL(request.url);
const method = (request.method || "GET").toUpperCase();
// Optional: route selected CPU-heavy endpoints to Durable Objects. // Optional: route selected CPU-heavy endpoints to Durable Objects.
// This keeps the main Worker on a low-CPU path while allowing heavy work to complete. // This keeps the main Worker on a low-CPU path while allowing heavy work to complete.
@ -153,10 +154,7 @@ export default {
// Token endpoint: // Token endpoint:
// - password grant is CPU-heavy (password verification) => offload // - password grant is CPU-heavy (password verification) => offload
// - refresh_token grant is lightweight (JWT HS256 verify) => keep in Worker/WASM // - refresh_token grant is lightweight (JWT HS256 verify) => keep in Worker/WASM
if ( if (url.pathname === "/identity/connect/token" && method === "POST") {
url.pathname === "/identity/connect/token" &&
(request.method || "GET").toUpperCase() === "POST"
) {
const body = await request.clone().text(); const body = await request.clone().text();
const params = new URLSearchParams(body); const params = new URLSearchParams(body);
const grantType = params.get("grant_type"); const grantType = params.get("grant_type");
@ -167,9 +165,7 @@ export default {
const stub = env.HEAVY_DO.get(id); const stub = env.HEAVY_DO.get(id);
return stub.fetch(request, { body }); return stub.fetch(request, { body });
} }
} } else if (shouldOffloadToHeavyDo(request, url)) {
if (shouldOffloadToHeavyDo(request, url)) {
const shardKey = await getHeavyDoShardKey(request, url); const shardKey = await getHeavyDoShardKey(request, url);
const name = shardKey ? `user:${shardKey}` : "user:default"; const name = shardKey ? `user:${shardKey}` : "user:default";
const id = env.HEAVY_DO.idFromName(name); const id = env.HEAVY_DO.idFromName(name);
@ -179,7 +175,7 @@ export default {
} }
// Attachment upload/download fast-path (R2 zero-copy streaming + JWT validation) // Attachment upload/download fast-path (R2 zero-copy streaming + JWT validation)
if (request.method === "PUT") { if (method === "PUT") {
const parsed = parseAzureUploadPath(url.pathname); const parsed = parseAzureUploadPath(url.pathname);
if (parsed) { if (parsed) {
const token = url.searchParams.get("token"); const token = url.searchParams.get("token");
@ -190,16 +186,14 @@ export default {
); );
} }
return handleAzureUpload( return handleAzureUpload(
request, request,
env, env,
parsed.cipherId, parsed.cipherId,
parsed.attachmentId, parsed.attachmentId,
token token
); );
} }
} } else if (method === "GET") {
if (request.method === "GET") {
const parsed = parseDownloadPath(url.pathname); const parsed = parseDownloadPath(url.pathname);
if (parsed) { if (parsed) {
const token = url.searchParams.get("token"); const token = url.searchParams.get("token");