saveFieldDetectionCacheForUrl function infrastructure exported ✓ 100.0%

Last updated: 2026-02-24T21:07:57.500Z

Metrics

LOC: 45 Complexity: 9 Params: 3 Coverage: 100.0% (12/12 lines, 5x executed)

Signature

saveFieldDetectionCacheForUrl( url: string, fields: DetectedFieldSummary[], ): : Promise<FieldDetectionCacheEntry>

Summary

Saves detected fields for a URL in the cache. Deduplicates by URL and origin+path. Evicts oldest entries beyond the limit.

Tags

#@param url - Full page URL#@param fields - Array of detected field summaries#@returns The saved cache entry

Source Code

export async function saveFieldDetectionCacheForUrl(
  url: string,
  fields: DetectedFieldSummary[],
): Promise<FieldDetectionCacheEntry> {
  const now = Date.now();
  let parsed: URL | null = null;
  try {
    parsed = new URL(url);
  } catch {
    // keep null and fallback to plain values
  }

  const entry: FieldDetectionCacheEntry = {
    url,
    origin: parsed?.origin ?? "",
    hostname: parsed?.hostname ?? "",
    path: parsed?.pathname ?? "",
    count: fields.length,
    fields,
    updatedAt: now,
  };

  await updateStorageAtomically(
    STORAGE_KEYS.FIELD_CACHE,
    [] as FieldDetectionCacheEntry[],
    (existing) => {
      const filtered = existing.filter(
        (item) =>
          item.url !== url &&
          !(
            entry.origin &&
            entry.path &&
            item.origin === entry.origin &&
            item.path === entry.path
          ),
      );
      filtered.push(entry);

      return filtered
        .sort((a, b) => b.updatedAt - a.updatedAt)
        .slice(0, MAX_FIELD_CACHE_ENTRIES);
    },
  );
  return entry;
}

Dependencies (Outgoing)

graph LR saveFieldDetectionCacheForUrl["saveFieldDetectionCacheForUrl"] updateStorageAtomically["updateStorageAtomically"] saveFieldDetectionCacheForUrl -->|calls| updateStorageAtomically style saveFieldDetectionCacheForUrl fill:#dbeafe,stroke:#2563eb,stroke-width:2px click saveFieldDetectionCacheForUrl "5f18074b51936498.html" click updateStorageAtomically "bf2f57323401fa98.html"
TargetType
updateStorageAtomically calls

Impact (Incoming)

graph LR saveFieldDetectionCacheForUrl["saveFieldDetectionCacheForUrl"] handle["handle"] handle -->|uses| saveFieldDetectionCacheForUrl style saveFieldDetectionCacheForUrl fill:#dbeafe,stroke:#2563eb,stroke-width:2px click saveFieldDetectionCacheForUrl "5f18074b51936498.html" click handle "8b929c06f047f82c.html"
SourceType
handle uses