refactor: konsolidasi handler ke router.ts + complexity + test coverage

=== DRY Konsolidasi ===
- handleRelay, requireAuth, handleHealth, handleIndex, getClientIP → 1x di src/lib/router.ts
- api/relay.ts, src/worker.ts, src/index.ts jadi thin wrapper

=== Dead exports ===
- 35 dead exports dibersihkan (dari router.ts, worker.ts, relay.ts)
- Duplikasi simbol dari 12 ke 3 (wajar)

=== Keamanan ===
- Default API key sk-dummy-key dihapus — requireAuth return null saat key kosong
- Semua CORS konsisten via getCorsHeaders()

=== Kompleksitas ===
- handleRequest → pecah ke validateRelayTarget, createJsonErrorResponse, dll
- transformAnthropicStream (170 baris) → 4 sub-fungsi (emitInitEvents, emitDoneEvents, etc)
- handleAnthropicMessages → handleUpstreamError, buildStreamHeaders, buildJsonHeaders
- Fix duplicate stream? field di AnthropicRequest interface

=== Test Coverage ===
- Test naik dari 171 ke 248 (+77 test)
- File baru: src/router.test.ts (requireAuth, getClientIP, CORS, health, index)
- File baru: src/relay-integration.test.ts (filterResponseHeaders, shouldSendBody, buildRelayRequest, classifyFetchError, createRelayResponse, normalizeTargetUrl, SSRF, isPrivateIp)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-24 23:48:16 +07:00
co-authored by Claude Opus 4.8
parent db281c5175
commit fc69a0e93c
8 changed files with 1343 additions and 1667 deletions
+3 -3
View File
@@ -5,15 +5,15 @@
* bypassing the HTTP server layer.
*/
import { test, expect, describe, beforeAll, afterAll } from "bun:test";
import { test, expect, describe } from "bun:test";
// Import handler functions directly from index.ts
// Note: this will also start the Bun.serve() instance, which we allow.
import {
handleHealth,
handleIndex,
getClientIP,
} from "./index";
getClientIPFromServer as getClientIP,
} from "./lib/router";
describe("handleHealth", () => {
test("should return 200 with JSON body", async () => {