ifc_lite_server/routes/parse/
replay_header.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Marking a replayed `X-IFC-Metadata` header as served from cache (#5542).
6
7/// The stored `X-IFC-Metadata` header JSON, with `stats.from_cache` set to
8/// `true`.
9///
10/// The header is cached exactly as the live parse wrote it, and the live parse
11/// writes `from_cache: false`, because at that moment it was not. Replaying it
12/// verbatim therefore told every warm caller it had just been parsed:
13/// `parseParquet()` reported `stats.from_cache === false` on a cache hit
14/// (#5542), while `GET /api/v1/cache/{key}` and the JSON parse route both set
15/// the flag on theirs.
16///
17/// Works on the JSON value rather than a typed header so the flat and
18/// optimized headers (two different structs) share one rule, and so a field
19/// this build does not know about is carried through untouched. A header that
20/// is not a JSON object with a `stats` object is returned unchanged: that is
21/// what every replay path did before, and the flag is not this function's
22/// reason to reject an entry.
23pub(crate) fn mark_header_from_cache(header: String) -> String {
24    let Ok(mut value) = serde_json::from_str::<serde_json::Value>(&header) else {
25        return header;
26    };
27    let Some(stats) = value.get_mut("stats").and_then(serde_json::Value::as_object_mut) else {
28        return header;
29    };
30    stats.insert("from_cache".to_owned(), serde_json::Value::Bool(true));
31    serde_json::to_string(&value).unwrap_or(header)
32}