This commit improves the URL health checking mechanism to reduce false negatives. - Treat all 2XX status codes as successful, addressing issues with codes like `204`. - Exclude URLs within Markdown inline code blocks. - Send the Host header for improved handling of webpages behind proxies. - Improve formatting and context for output messages. - Fix the defaulting options for redirects and cookie handling. - Add URL exclusion support for non-responsive URLs. - Update the user agent pool to modern browsers and platforms. - Improve CI/CD workflow to respond to modifications in the `test/checks/external-urls` directory, offering immediate feedback on potential impacts to the external URL test. - Add support for randomizing TLS fingerprint to mimic various clients better, improving the effectiveness of checks. However, this is not fully supported by Node.js's HTTP client; see nodejs/undici#1983 for more details. - Use `AbortSignal` instead of `AbortController` as more modern and simpler way to handle timeouts.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { exec } from 'child_process';
|
|
import { indentText } from '@tests/shared/Text';
|
|
import type { ExecOptions, ExecException } from 'child_process';
|
|
|
|
const TIMEOUT_IN_SECONDS = 180;
|
|
const MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024; // 1 MB
|
|
|
|
export function runCommand(
|
|
command: string,
|
|
options?: ExecOptions,
|
|
): Promise<CommandResult> {
|
|
return new Promise((resolve) => {
|
|
options = {
|
|
cwd: process.cwd(),
|
|
timeout: TIMEOUT_IN_SECONDS * 1000,
|
|
maxBuffer: MAX_OUTPUT_BUFFER_SIZE * 2,
|
|
...(options ?? {}),
|
|
};
|
|
|
|
exec(command, options, (error, stdout, stderr) => {
|
|
let errorText: string | undefined;
|
|
if (error || stderr?.length > 0) {
|
|
errorText = formatError(command, error, stdout, stderr);
|
|
}
|
|
resolve({
|
|
stdout,
|
|
error: errorText,
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
export interface CommandResult {
|
|
readonly stdout: string;
|
|
readonly error?: string;
|
|
}
|
|
|
|
function formatError(
|
|
command: string,
|
|
error: ExecException | null,
|
|
stdout: string | undefined,
|
|
stderr: string | undefined,
|
|
) {
|
|
const errorParts = [
|
|
'Error while running command.',
|
|
`Command:\n${indentText(command, 1)}`,
|
|
];
|
|
if (error?.toString().trim()) {
|
|
errorParts.push(`Error:\n${indentText(error.toString(), 1)}`);
|
|
}
|
|
if (stderr?.trim()) {
|
|
errorParts.push(`stderr:\n${indentText(stderr, 1)}`);
|
|
}
|
|
if (stdout?.trim()) {
|
|
errorParts.push(`stdout:\n${indentText(stdout, 1)}`);
|
|
}
|
|
return errorParts.join('\n---\n');
|
|
}
|