File size: 1,661 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * Parses textarea input for bulk API key creation.
 *
 * Supported line formats (one per line):
 *   - `name|apiKey`
 *   - `apiKey` (auto-named as `Key N`)
 *   - `# comment` (skipped)
 *   - blank lines (skipped)
 *
 * `apiKey` may contain `|` — only the first `|` is treated as the separator.
 */

export interface BulkApiKeyEntry {
  name: string;
  apiKey: string;
  lineNumber: number;
}

export interface BulkApiKeyParseResult {
  entries: BulkApiKeyEntry[];
  warnings: string[];
}

const MAX_BULK_LINES = 200;

export function parseBulkApiKeys(text: string): BulkApiKeyParseResult {
  const lines = text.split(/\r?\n/);
  const entries: BulkApiKeyEntry[] = [];
  const warnings: string[] = [];
  let autoIdx = 1;

  if (lines.length > MAX_BULK_LINES) {
    warnings.push(
      `Input has ${lines.length} lines; only the first ${MAX_BULK_LINES} will be processed.`
    );
  }

  const bound = Math.min(lines.length, MAX_BULK_LINES);
  for (let i = 0; i < bound; i++) {
    const raw = lines[i].trim();
    if (!raw) continue;
    if (raw.startsWith("#")) continue;

    const pipeIdx = raw.indexOf("|");
    let name: string;
    let apiKey: string;
    if (pipeIdx === -1) {
      name = `Key ${autoIdx++}`;
      apiKey = raw;
    } else {
      const namePart = raw.slice(0, pipeIdx).trim();
      apiKey = raw.slice(pipeIdx + 1).trim();
      name = namePart || `Key ${autoIdx++}`;
    }

    if (!apiKey) {
      warnings.push(`Line ${i + 1}: empty apiKey, skipped`);
      continue;
    }

    entries.push({ name, apiKey, lineNumber: i + 1 });
  }

  return { entries, warnings };
}

export const BULK_API_KEY_MAX_LINES = MAX_BULK_LINES;