handleRelayRequest function

Future<void> handleRelayRequest(
  1. HttpRequest request, {
  2. required String? requireCredential(),
  3. required String? allowedOrigin,
})

One POST /relay call (issue #633): the desktop add-in taskpane has no extension to carry its provider HTTP, so the pane proxies it through the local hub, which fetches CORS-free by construction. The body is the SW-bridge request envelope {url, method?, headers?, bodyB64?}; the upstream answer is streamed back raw (status + content-type + body), so provider SSE flows through incrementally.

Loopback-bound hubs relay for the taskpane origins (same trust domain as /healthz); a lan-bound hub demands the master bearer — an open relay on the LAN would be a fetch oracle.

Implementation

Future<void> handleRelayRequest(
  HttpRequest request, {
  required String? Function() requireCredential,
  required String? allowedOrigin,
}) async {
  if (request.method == 'OPTIONS') {
    await _relayPreflight(request, allowedOrigin);
    return;
  }
  final credential = requireCredential();
  if (credential != null &&
      request.headers.value('authorization') != 'Bearer $credential') {
    request.response.statusCode = 401;
    await request.response.close();
    return;
  }
  final parsed = await _relayEnvelope(request, allowedOrigin);
  if (parsed == null) return;
  await _relayForward(request, parsed.$1, parsed.$2, allowedOrigin);
}