char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
1.1 KiB45 linesraw
1/**
2 * Header utilities for parsing and serializing HTTP headers.
3 * Generic utilities not specific to MCP.
4 */
5
6/**
7 * Parses a JSON string of headers into an array of key-value pairs.
8 * Returns empty array if the JSON is invalid or empty.
9 */
10export function parseHeadersToArray(headersJson: string): { key: string; value: string }[] {
11	if (!headersJson?.trim()) return [];
12
13	try {
14		const parsed = JSON.parse(headersJson);
15
16		if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
17			return Object.entries(parsed).map(([key, value]) => ({
18				key,
19				value: String(value)
20			}));
21		}
22	} catch {
23		return [];
24	}
25
26	return [];
27}
28
29/**
30 * Serializes an array of header key-value pairs to a JSON string.
31 * Filters out pairs with empty keys and returns empty string if no valid pairs.
32 */
33export function serializeHeaders(pairs: { key: string; value: string }[]): string {
34	const validPairs = pairs.filter((p) => p.key.trim());
35
36	if (validPairs.length === 0) return '';
37
38	const obj: Record<string, string> = {};
39
40	for (const pair of validPairs) {
41		obj[pair.key.trim()] = pair.value;
42	}
43
44	return JSON.stringify(obj);
45}