This commit is contained in:
1 parent
9d96f67261
commit
1e289ee49b
9 files changed
+129
-78
No files matched your search
+45
-26
@@ -69,37 +69,56 @@ function valueScrub(value, secrets) {
|
||||
return JSON.parse(scrubbed);
|
||||
}
|
||||
|
||||
function collectFile(filePath, label, maxBytes, options) {
|
||||
async function collectFileSnapshot(filePath, label, maxBytes, options) {
|
||||
const includePath = !options || options.includePath !== false;
|
||||
if (!filePath) return `=== ${label} ===\n<no path configured>\n\n`;
|
||||
const signal = options?.signal;
|
||||
if (!filePath) return { text: `=== ${label} ===\n<no path configured>\n\n`, sizeBytes: null };
|
||||
let stat;
|
||||
try { stat = fs.statSync(filePath); }
|
||||
catch (err) {
|
||||
const context = includePath ? ` (${filePath})` : '';
|
||||
if (err && err.code === 'ENOENT') return `=== ${label}${context} ===\n<file does not exist yet>\n\n`;
|
||||
return `=== ${label}${context} ===\n<stat error: ${err.message}>\n\n`;
|
||||
}
|
||||
const cap = Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : 5 * 1024 * 1024;
|
||||
let content;
|
||||
let file;
|
||||
try {
|
||||
if (stat.size > cap) {
|
||||
const fd = fs.openSync(filePath, 'r');
|
||||
const buf = Buffer.alloc(cap);
|
||||
fs.readSync(fd, buf, 0, cap, stat.size - cap);
|
||||
fs.closeSync(fd);
|
||||
const skipped = stat.size - cap;
|
||||
content = `<truncated: skipped first ${skipped} bytes; showing last ${cap} bytes of ${stat.size}>\n` + buf.toString('utf-8');
|
||||
} else {
|
||||
content = fs.readFileSync(filePath, 'utf-8');
|
||||
signal?.throwIfAborted();
|
||||
try {
|
||||
file = await fs.promises.open(filePath, 'r');
|
||||
stat = await file.stat();
|
||||
} catch (err) {
|
||||
signal?.throwIfAborted();
|
||||
const context = includePath ? ` (${filePath})` : '';
|
||||
const message = err?.code === 'ENOENT' ? '<file does not exist yet>' : `<stat error: ${err.message}>`;
|
||||
return { text: `=== ${label}${context} ===\n${message}\n\n`, sizeBytes: null };
|
||||
}
|
||||
} catch (err) {
|
||||
content = `<read error: ${err.message}>`;
|
||||
const cap = Number.isFinite(maxBytes) && maxBytes > 0 ? Math.max(1, Math.floor(maxBytes)) : 5 * 1024 * 1024;
|
||||
let content;
|
||||
try {
|
||||
signal?.throwIfAborted();
|
||||
if (!stat.isFile()) throw new Error('Not a regular file');
|
||||
const size = Math.min(stat.size, cap);
|
||||
const start = Math.max(0, stat.size - size);
|
||||
const buffer = Buffer.alloc(size);
|
||||
let offset = 0;
|
||||
while (offset < size) {
|
||||
signal?.throwIfAborted();
|
||||
const { bytesRead } = await file.read(buffer, offset, Math.min(size - offset, 256 * 1024), start + offset);
|
||||
if (!bytesRead) break;
|
||||
offset += bytesRead;
|
||||
}
|
||||
signal?.throwIfAborted();
|
||||
content = (start > 0 ? `<truncated: skipped first ${start} bytes; showing last ${size} bytes of ${stat.size}>\n` : '') + buffer.subarray(0, offset).toString('utf-8');
|
||||
} catch (err) {
|
||||
signal?.throwIfAborted();
|
||||
content = `<read error: ${err.message}>`;
|
||||
}
|
||||
const metadata = includePath ? `${filePath}, size=${stat.size} bytes` : `size=${stat.size} bytes`;
|
||||
return { text: `=== ${label} (${metadata}) ===\n${content}\n\n`, sizeBytes: stat.size };
|
||||
} finally {
|
||||
await file?.close().catch(() => {});
|
||||
}
|
||||
const metadata = includePath ? `${filePath}, size=${stat.size} bytes` : `size=${stat.size} bytes`;
|
||||
return `=== ${label} (${metadata}) ===\n${content}\n\n`;
|
||||
}
|
||||
|
||||
function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
|
||||
async function collectFile(filePath, label, maxBytes, options) {
|
||||
return (await collectFileSnapshot(filePath, label, maxBytes, options)).text;
|
||||
}
|
||||
|
||||
async function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
|
||||
const parts = [];
|
||||
parts.push('=== Multi-Hoster-Upload Support Bundle ===\n');
|
||||
if (header && typeof header === 'object') {
|
||||
@@ -110,9 +129,9 @@ function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
|
||||
parts.push(redactLogText(JSON.stringify(sanitizedConfig, null, 2), secrets));
|
||||
parts.push('\n\n');
|
||||
for (const f of (files || [])) {
|
||||
parts.push(redactLogText(collectFile(f.path, f.label || f.path, f.maxBytes, { includePath: false }), secrets));
|
||||
parts.push(redactLogText(await collectFile(f.path, f.label || f.path, f.maxBytes, { includePath: false }), secrets));
|
||||
}
|
||||
return parts.join('');
|
||||
}
|
||||
|
||||
module.exports = { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, buildSupportBundleText, CRED_KEYS, REDACTED };
|
||||
module.exports = { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, collectFileSnapshot, buildSupportBundleText, CRED_KEYS, REDACTED };
|
||||
Reference in new issue
Block a user