Spaces:
Running

File size: 1,213 Bytes
e0d0231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Carries the unit conversion SearXNG's own `unit_converter` plugin computed
 * for the query, on `/search/text`. The value is the plugin's answer text (for
 * example `16.4041994751 ft`), percent-encoded so that any character survives
 * a header. The header is absent whenever the plugin answered nothing, so a
 * search without a conversion carries nothing new.
 *
 * The plugin needs no search engine, so it answers even where every engine is
 * blocked, which is why the header can arrive on a search with no results.
 */
export const SEARCH_UNIT_CONVERSION_HEADER = "X-Search-Unit-Conversion";

/** The answer as it travels in the header. */
export function encodeUnitConversion(answer: string): string {
  return encodeURIComponent(answer);
}

/**
 * The answer back from the header, or nothing when the value is not a valid
 * encoding or holds no text. The conversion is an extra on top of the results,
 * so a header mangled on the way drops the card and never fails the search.
 */
export function decodeUnitConversion(value: string): string | undefined {
  try {
    const answer = decodeURIComponent(value);
    return answer.trim() ? answer : undefined;
  } catch {
    return undefined;
  }
}