bridge-index-kickoff-response.mjs
47 lines 2.3 KB
Raw
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor ⚠ breaking 11 days ago
1 /**
2 * Validate that an HTTP response from the `bridge-index-background` Netlify
3 * Function actually came back with the only status it can legitimately produce
4 * on a successful kickoff: 202.
5 *
6 * Why this exists (May 2026 hotfix): the catch-all redirect in
7 * `deploy/bridge/netlify.toml` (`from = "/*" force = true`) was intercepting
8 * `/.netlify/functions/bridge-index-background` requests because Netlify's
9 * normal exemption for `/.netlify/...` paths is BYPASSED when `force = true`.
10 * The redirect rewrote the URL to the regular `bridge` function, which had no
11 * matching Express route and returned 404. `await fetch(url)` resolves
12 * successfully on a 404 (since 404 is a valid HTTP response, not a network
13 * error), so the kickoff caller silently believed the background job started
14 * and returned `202 status:"background"` to the browser. The actual indexing
15 * never ran. The job lock then sat for its full 16-min TTL blocking any retry.
16 *
17 * This helper is deliberately a tiny pure function so it stays trivially
18 * unit-testable without spinning up Express, Netlify, or fetch mocks.
19 *
20 * @param {{ status?: number } | null | undefined} response - The fetch Response
21 * (or compatible shape with a numeric `status`).
22 * @param {string | null | undefined} body - The response body text (already
23 * read by the caller via `response.text()`). Used only for diagnostic logs.
24 * @throws {Error} when `response` is missing/malformed or `response.status !== 202`.
25 */
26 export function assertBackgroundKickoffOk(response, body) {
27 if (!response || typeof response.status !== 'number') {
28 throw new Error(
29 'background kickoff: invalid response from /.netlify/functions/bridge-index-background',
30 );
31 }
32 if (response.status !== 202) {
33 const snippet = typeof body === 'string' ? body.slice(0, KICKOFF_BODY_SNIPPET_MAX) : '';
34 const tail = snippet ? ` — body: ${snippet}` : '';
35 throw new Error(
36 `background kickoff: expected HTTP 202 from /.netlify/functions/bridge-index-background, got HTTP ${response.status}${tail}`,
37 );
38 }
39 }
40
41 /**
42 * Cap the diagnostic body snippet length to keep Lambda log lines bounded
43 * (Netlify charges by GB-seconds and CloudWatch enforces a 256 KB/event cap).
44 * 500 chars is enough to identify any reasonable error response without
45 * dumping multi-KB HTML error pages.
46 */
47 export const KICKOFF_BODY_SNIPPET_MAX = 500;
File History 4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor 11 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b fix(7C-L1b): route hosted delegation proposals through cani… Human minor 31 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91 fix: repair endpoint now sets has_active_subscription when … Human minor 51 days ago