From 6b4757acbc25dc7e0ca02de7afe26e723a485dba Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 19:58:28 +0800 Subject: [PATCH] refactor: unify relay recovery re-sends through one egress chokepoint (#6003) The five recovery re-send rungs in forwardCaptured (ws-byte-shed, role-ladder, pck-strip, image-count-shed, context-overflow refold) each re-inlined the same two-step egress tail before their fetchWithTimeout: re-sign (#1884) then v1internal re-wrap (#3129). Extract that tail into a single finalizeEgress() so the final re-sign -> wrap decision lives in one place instead of five copies, and so a future user egress-transform pipeline has exactly one hook to attach to (before the re-sign, so the signature always covers the shipped bytes). Zero behavior change: byte-identical bodies, headers, and fetch calls; only the duplication is removed. Groundwork for the user-registerable egress pipeline (#6003); the pipeline itself is not added here. The main send keeps its inline re-sign/wrap because the pre-send gate stack consumes the signed inner body between the two steps; fake-completion keeps its own option-based path (its wrap->sign order differs from every other site). --- src/server/relay.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/server/relay.ts b/src/server/relay.ts index 127f699523..db55aec853 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -436,6 +436,14 @@ async function forwardCaptured( log("warn", `[${prepared?.session.id ?? "passthrough"}] [resign] re-sign failed; sending the previous signature: ${String(err)}`); } }; + // Single egress chokepoint for every recovery re-send (#6003 groundwork): the + // re-sign (#1884) then v1internal re-wrap (#3129) each retry rung used to + // re-inline now lives here once. A future user egress-transform pipeline hooks + // in before the re-sign so the signature always covers the shipped bytes. + const finalizeEgress = (variant: string | Buffer): string | Buffer => { + applyResign(headers, variant); + return v1Wrap(variant); + }; // #1093 output-side compression: resolve through the standard three-level // compress cascade (global → provider); default off = byte-for-byte passthrough. // The kernel decides (turn kind / verbosity / lower-effort); bili only lands it. @@ -1052,8 +1060,7 @@ async function forwardCaptured( log("info", `[${prepared.session.id}] [ws-byte-shed] upstream closed 1009 with zero events at ${formatMiB(sentBytes)} MiB — shed to ${formatMiB(stats.finalBytes)} MiB (archived=${stats.archived} downscaled=${stats.downscaled}), resending once within this request`); try { const shrunkWire = scrubLoneSurrogatesOnWire(shrunk); - applyResign(headers, shrunkWire); - const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(shrunkWire) }, undefined, clientAbort.signal); + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: finalizeEgress(shrunkWire) }, undefined, clientAbort.signal); if (retried.response.ok) { firstAttemptClear?.(); wireBody = shrunkWire; @@ -1167,8 +1174,7 @@ async function forwardCaptured( if (fixed.rewritten === 0) return "other"; let r: Awaited>; try { - applyResign(headers, fixed.body); - r = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(fixed.body) }, undefined, clientAbort.signal); + r = await fetchWithTimeout(upstreamUrl, { ...init, body: finalizeEgress(fixed.body) }, undefined, clientAbort.signal); } catch { return "other"; // transport failure — keep the original 400 } @@ -1266,8 +1272,7 @@ async function forwardCaptured( if (strippedPckBody !== null && !contentWall) { let retried: Awaited> | null = null; try { - applyResign(headers, strippedPckBody); - retried = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(strippedPckBody) }, undefined, clientAbort.signal); + retried = await fetchWithTimeout(upstreamUrl, { ...init, body: finalizeEgress(strippedPckBody) }, undefined, clientAbort.signal); } catch { retried = null; // transport failure — inconclusive, original 400 stands } @@ -1518,8 +1523,7 @@ async function forwardCaptured( } else { try { s.metadata.imageCountShed = { at: Date.now(), phase: "same-request-retry", cap: overflowInfo.imageCountCap, archived: countShed.stats.archived }; - applyResign(headers, countShed.body); - const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(countShed.body) }, undefined, clientAbort.signal); + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: finalizeEgress(countShed.body) }, undefined, clientAbort.signal); if (retried.response.ok) { upstreamResult.clearTimer(); wireBody = countShed.body; // track the accepted re-send as the wire base @@ -1574,8 +1578,7 @@ async function forwardCaptured( // summaries — scrub string bodies again so a lone // surrogate can't poison the retry (see the #1884 seam). const wireRefolded = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body; - applyResign(headers, wireRefolded); - const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(wireRefolded) }, undefined, clientAbort.signal); + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: finalizeEgress(wireRefolded) }, undefined, clientAbort.signal); if (retried.response.ok) { upstreamResult.clearTimer(); wireBody = wireRefolded; // #1900: track the accepted re-send as the wire base