File size: 991 Bytes
7a1ad33 | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Parses custom headers and returns a map of key and vallues
*/
export function parseCustomHeaders(
envValue: string | undefined,
): Record<string, string> {
const headers: Record<string, string> = {};
if (!envValue) {
return headers;
}
// Split the string on commas that are followed by a header key (key:),
// but ignore commas that are part of a header value (including values with colons or commas)
for (const entry of envValue.split(/,(?=\s*[^,:]+:)/)) {
const trimmedEntry = entry.trim();
if (!trimmedEntry) {
continue;
}
const separatorIndex = trimmedEntry.indexOf(':');
if (separatorIndex === -1) {
continue;
}
const name = trimmedEntry.slice(0, separatorIndex).trim();
const value = trimmedEntry.slice(separatorIndex + 1).trim();
if (!name) {
continue;
}
headers[name] = value;
}
return headers;
}
|