Files

30 lines
1.2 KiB
JavaScript

export function parseLines(value) {
return value.split(/\r?\n/).map(line => line.trim()).filter(Boolean);
}
export function parseArgs(value) {
if (!value.trim().startsWith('[')) return parseLines(value);
let args;
try { args = JSON.parse(value); }
catch { throw new Error('参数 JSON 格式不正确,请填写字符串数组。'); }
if (!Array.isArray(args) || args.some(arg => typeof arg !== 'string')) {
throw new Error('参数必须是 JSON 字符串数组,或每行一个参数。');
}
return args;
}
export function appendLog(previous, incoming, limit = 240000) {
const all = previous + incoming;
return {text: all.slice(-limit), trimmed: all.length > limit};
}
export function requestId() {
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
// LAN HTTP can lack randomUUID; getRandomValues remains available there.
const bytes = crypto.getRandomValues(new Uint8Array(16));
bytes[6] = (bytes[6] & 15) | 64;
bytes[8] = (bytes[8] & 63) | 128;
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}