Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ export default {
}
};
const { pathname } = new URL(request.url);
switch (true) {
case pathname.endsWith("/chat/completions"):
assert(request.method === "POST");
return handleCompletions(await request.json(), apiKey)
.catch(errHandler);
case pathname.endsWith("/embeddings"):
assert(request.method === "POST");
return handleEmbeddings(await request.json(), apiKey)
.catch(errHandler);
case pathname.endsWith("/models"):
assert(request.method === "GET");
return handleModels(apiKey)
.catch(errHandler);
default:
throw new HttpError("404 Not Found", 404);
const routeRegex = /^\/([\w.-]+)\/v1\/(chat\/completions|embeddings)$/;
const match = pathname.match(routeRegex);

if (!match) {
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/chat/completions", 404);
Copy link
Preview

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message only mentions chat/completions format but the code also supports embeddings. Consider updating to: '404 Not Found. Use format: //v1/{chat/completions|embeddings}'

Suggested change
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/chat/completions", 404);
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/{chat/completions|embeddings}", 404);

Copilot uses AI. Check for mistakes.

}

const modelFromUrl = match[1];
const endpoint = match[2];
let body;
Copy link
Preview

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing method validation. The original code verified POST method for both endpoints. Consider adding 'assert(request.method === "POST");' before parsing JSON.

Suggested change
let body;
let body;
assert(request.method === "POST");

Copilot uses AI. Check for mistakes.

try {
body = await request.json();
} catch (e) {
body = {};
Copy link
Preview

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent fallback to empty object when JSON parsing fails could mask client errors. Consider throwing a more specific error for malformed JSON requests.

Copilot uses AI. Check for mistakes.

}
body.model = modelFromUrl;

if (endpoint === "chat/completions") {
return handleCompletions(body, apiKey).catch(errHandler);
} else if (endpoint === "embeddings") {
return handleEmbeddings(body, apiKey).catch(errHandler);
Copy link
Preview

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return statement for the embeddings case and no fallback for unexpected endpoint values. This could cause the function to return undefined.

Suggested change
return handleEmbeddings(body, apiKey).catch(errHandler);
return handleEmbeddings(body, apiKey).catch(errHandler);
} else {
throw new HttpError("Invalid endpoint. Supported endpoints are 'chat/completions' and 'embeddings'.", 400);

Copilot uses AI. Check for mistakes.

}
} catch (err) {
return errHandler(err);
Expand Down