File size: 4,373 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import type { CompressionConfig, CompressionPipelineStep } from "./types.ts";
import { resolveCompressionPlan } from "./resolveCompressionPlan.ts";
import {
  deriveDefaultPlan,
  type DerivedPlan,
  type CompressionSource,
} from "./deriveDefaultPlan.ts";

/** Named-combo map: combo id → its stacked pipeline (operator-defined profiles). */
type NamedCombos = Record<string, CompressionPipelineStep[]>;

/** Tags a plan with the precedence layer that produced it (Phase 3 observability). */
export function withSource(plan: DerivedPlan, source: CompressionSource): DerivedPlan {
  return { ...plan, source };
}

/**
 * Interprets the `x-omniroute-compression` request header into a plan, or null when the
 * value is unrecognized (caller falls through to normal resolution). Pure.
 *   off            -> no compression
 *   default        -> the panel-derived Default (ignores active profile / routing / auto-trigger)
 *   engine:<id>    -> that single engine, when enabled in config.engines
 *   <combo>        -> a named combo, matched name-first (lowercased) then exact id (Decision A)
 */
export function planFromHeader(
  config: CompressionConfig,
  header: string,
  combos: NamedCombos
): DerivedPlan | null {
  const h = header.trim();
  if (!h) return null;
  const lower = h.toLowerCase();

  if (lower === "off") return withSource({ mode: "off", stackedPipeline: [] }, "request-header");

  if (lower === "default") {
    // Empty combos + null comboId yields the pure panel default (no active-combo leak).
    return withSource(deriveDefaultPlanFromConfig(config, null, {}), "request-header");
  }

  if (lower.startsWith("engine:")) {
    const id = lower.slice("engine:".length).trim();
    const engine = config.engines?.[id];
    return engine?.enabled
      ? withSource(deriveDefaultPlan({ [id]: engine }, true), "request-header")
      : null;
  }

  const combo = combos[lower] ?? combos[h];
  return combo ? withSource({ mode: "stacked", stackedPipeline: combo }, "request-header") : null;
}

/** Renders the X-OmniRoute-Compression response header value. */
export function formatCompressionMeta(plan: DerivedPlan): string {
  return `${plan.mode}; source=${plan.source ?? "off"}`;
}

/**
 * Builds the named-combo lookup keyed by BOTH combo id and lowercased (trimmed) name, so the
 * `<combo>` header form can match by either. A combo with a blank/whitespace/missing name
 * contributes only its id key — a blank name would otherwise register a useless "" key, and a
 * single malformed row must not throw and disable the whole map (`name` is NOT NULL in the DB
 * but may be an empty string). Pure.
 */
export function buildNamedComboLookup(
  combos: Array<{ id: string; name?: string | null; pipeline: CompressionPipelineStep[] }>
): NamedCombos {
  const map: NamedCombos = {};
  for (const c of combos) {
    map[c.id] = c.pipeline;
    const name = c.name?.trim();
    if (name) map[name.toLowerCase()] = c.pipeline;
  }
  return map;
}

/**
 * Derived-default step. The per-engine toggle map drives the default ONLY when it was
 * EXPLICITLY configured via the panel (a stored `engines` row — `config.enginesExplicit`).
 * For legacy installs the map is backfilled for DISPLAY only (so the panel shows current
 * state); dispatch falls back to the historical `config.defaultMode` so behaviour is
 * byte-for-byte preserved until the operator opts into the panel by saving. This avoids a
 * silent behaviour change for installs whose backfilled engine flags don't exactly match
 * their old defaultMode.
 */
export function deriveDefaultPlanFromConfig(
  config: CompressionConfig,
  comboId: string | null,
  combos: NamedCombos = {}
): DerivedPlan {
  if (config.enginesExplicit) {
    // Panel-configured: the engines map (via the resolver, which stays header/active-combo
    // aware for Phases 2-3) is authoritative — including an explicit "everything off".
    return resolveCompressionPlan(config, { comboId, combos });
  }

  // Legacy path: defaultMode carries the effective mode (the engines map is display-only here).
  const legacyMode = config.defaultMode;
  if (legacyMode && legacyMode !== "off") {
    return legacyMode === "stacked"
      ? { mode: legacyMode, stackedPipeline: config.stackedPipeline ?? [] }
      : { mode: legacyMode, stackedPipeline: [] };
  }

  return { mode: "off", stackedPipeline: [] };
}