// Which public path prefixes are served by `createSidecarProxy` — routes whose body the platform only
// ever forwards and never reads.
//
// This exists so `bodyParser` can leave those request streams untouched. Parsing a body that is merely
// passing through is not just wasted work; for `multipart/form-data` it is corrupting. Hono caches the
// parsed `FormData` on the request, so when the proxy later asks for the bytes Hono re-serialises them
// from that cache — minting a NEW multipart boundary, while the proxy still forwards the ORIGINAL
// `content-type` header naming the old one. Header and body then disagree and the far side rejects every
// upload with "Multipart: Unexpected end of form".
//
// Registered by `createSidecarProxy` itself rather than hand-listed here, so a new sidecar cannot forget
// to add itself and rediscover the same bug.

const proxiedPrefixes = new Set<string>();

export const registerProxiedPrefix = (prefix: string): void => {
  proxiedPrefixes.add(prefix);
};

/** True when `path` is served by a sidecar proxy. Matches on a path boundary: `/api/memos` never matches
 * `/api/memoserver`. */
export function isProxiedPath(path: string): boolean {
  for (const prefix of proxiedPrefixes) {
    if (path === prefix || path.startsWith(`${prefix}/`)) return true;
  }
  return false;
}
