mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Revert keyToLabels label parsing
This commit is contained in:
1 parent
3f8ef85987
commit
81fcea8398
1 file changed
+11
-55
@@ -271,64 +271,20 @@ function labelsToKey(labels: LabelValues, metricLabels: string[]) {
|
||||
|
||||
function keyToLabels(key: string) {
|
||||
let labels = new Map<string, string>();
|
||||
let index = 0;
|
||||
|
||||
while (index < key.length) {
|
||||
let equal = key.indexOf("=", index);
|
||||
if (equal === -1) {
|
||||
throw new Error(`Malformed label key '${key}'`);
|
||||
}
|
||||
|
||||
let name = key.slice(index, equal);
|
||||
let cursor = equal + 1;
|
||||
if (key[cursor] !== "\"") {
|
||||
throw new Error(`Malformed label key '${key}'`);
|
||||
}
|
||||
|
||||
cursor += 1;
|
||||
let valueParts: string[] = [];
|
||||
while (cursor < key.length) {
|
||||
let char = key[cursor];
|
||||
if (char === "\"") {
|
||||
break;
|
||||
if (key !== "") {
|
||||
for (let pair of key.split(",")) {
|
||||
let [name, value] = pair.split("=", 2);
|
||||
if (!name || value === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "\\") {
|
||||
cursor += 1;
|
||||
if (cursor >= key.length) {
|
||||
throw new Error(`Malformed escape in label value for '${name}'`);
|
||||
}
|
||||
char = key[cursor];
|
||||
if (char === "n") {
|
||||
valueParts.push("\n");
|
||||
} else if (char === "\\") {
|
||||
valueParts.push("\\");
|
||||
} else if (char === "\"") {
|
||||
valueParts.push("\"");
|
||||
} else {
|
||||
valueParts.push(char);
|
||||
}
|
||||
} else {
|
||||
valueParts.push(char);
|
||||
}
|
||||
cursor += 1;
|
||||
labels.set(name, value
|
||||
.slice(1, -1)
|
||||
.replace(/\\"/g, "\"")
|
||||
.replace(/\\n/g, "\n")
|
||||
.replace(/\\\\/g, "\\")
|
||||
);
|
||||
}
|
||||
|
||||
if (cursor >= key.length || key[cursor] !== "\"") {
|
||||
throw new Error(`Missing closing quote for label '${name}'`);
|
||||
}
|
||||
|
||||
labels.set(name, valueParts.join(""));
|
||||
cursor += 1;
|
||||
|
||||
if (cursor >= key.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (key[cursor] !== ",") {
|
||||
throw new Error(`Malformed label key '${key}'`);
|
||||
}
|
||||
index = cursor + 1;
|
||||
}
|
||||
|
||||
return labels;
|
||||
|
||||
Reference in new issue
Block a user