@projectlibertylabs/p2p-peer-test
Version:
CLI tool to test libp2p connections and discover peer protocols
167 lines (150 loc) • 5.35 kB
JavaScript
import chalk from 'chalk';
import { formatMultiSummary } from './multi-tester.js';
import { OUTPUT_CONFIG } from './config.js';
/**
* Displays connection test results in either JSON or human-readable format
* @param {Object} result - The connection test result
* @param {string} multiaddr - The multiaddr that was tested
* @param {Object} [options={}] - Display options
* @param {boolean} [options.json=false] - Whether to output in JSON format
*/
export function displayResult(result, multiaddr, options = {}) {
const { json = false } = options;
if (json) {
console.log(
JSON.stringify({
success: result.success,
multiaddr: multiaddr,
duration: result.duration,
peerId: result.peerId || null,
protocols: result.protocols || [],
error: result.error || null,
details: result.details || null
})
);
return;
}
// Human-readable output
if (result.success) {
console.log(
chalk[OUTPUT_CONFIG.COLORS.SUCCESS](OUTPUT_CONFIG.SYMBOLS.SUCCESS + ' Connected'),
chalk[OUTPUT_CONFIG.COLORS.MUTED](`(${result.duration}ms)`)
);
console.log('Peer ID:', chalk[OUTPUT_CONFIG.COLORS.WARNING](result.peerId));
if (result.protocols && result.protocols.length > 0) {
console.log(`Protocols (${result.protocols.length}):`);
result.protocols.forEach((protocol) => {
console.log(
` ${OUTPUT_CONFIG.SYMBOLS.BULLET} ${chalk[OUTPUT_CONFIG.COLORS.INFO](protocol)}`
);
});
}
} else {
console.log(
chalk[OUTPUT_CONFIG.COLORS.ERROR](OUTPUT_CONFIG.SYMBOLS.FAILURE + ' Failed'),
chalk[OUTPUT_CONFIG.COLORS.MUTED](`(${result.duration}ms)`)
);
console.log('Error:', chalk[OUTPUT_CONFIG.COLORS.ERROR](result.error));
if (result.details) {
console.log('Hint:', chalk[OUTPUT_CONFIG.COLORS.WARNING](result.details));
}
}
}
/**
* Displays error messages in either JSON or human-readable format
* @param {Error} error - The error object to display
* @param {Object} [options={}] - Display options
* @param {boolean} [options.json=false] - Whether to output in JSON format
* @param {boolean} [options.verbose=false] - Whether to show verbose error details
*/
export function displayError(error, options = {}) {
const { json = false, verbose = false } = options;
if (json) {
console.log(JSON.stringify({ success: false, error: error.message }));
return;
}
console.error(chalk[OUTPUT_CONFIG.COLORS.ERROR]('Error:'), error.message);
if (verbose) {
console.error(error);
}
}
/**
* Displays validation error messages in either JSON or human-readable format
* @param {string} validationError - The validation error message
* @param {Object} [options={}] - Display options
* @param {boolean} [options.json=false] - Whether to output in JSON format
*/
export function displayValidationError(validationError, options = {}) {
const { json = false } = options;
if (json) {
console.log(JSON.stringify({ success: false, error: 'Invalid multiaddr: ' + validationError }));
return;
}
console.error(chalk[OUTPUT_CONFIG.COLORS.ERROR]('Invalid multiaddr:'), validationError);
}
/**
* Displays results from testing multiple multiaddrs
* @param {Object} multiResults - Results from testing multiple addresses
* @param {Object} [options={}] - Display options
* @param {boolean} [options.json=false] - Whether to output in JSON format
*/
export function displayMultiResults(multiResults, options = {}) {
const { json = false } = options;
if (json) {
console.log(
JSON.stringify({
success: multiResults.success,
totalResults: multiResults.totalResults,
successCount: multiResults.successCount,
failureCount: multiResults.failureCount,
duration: multiResults.duration,
results: multiResults.results.map((result) => ({
multiaddr: result.multiaddr,
success: result.success,
duration: result.duration,
peerId: result.peerId || null,
protocols: result.protocols || [],
error: result.error || null,
details: result.details || null
}))
})
);
return;
}
// Human-readable output
multiResults.results.forEach((result) => {
console.log(
chalk[OUTPUT_CONFIG.COLORS.INFO](`\n[${result.index}/${result.total}]`),
chalk[OUTPUT_CONFIG.COLORS.MUTED](result.multiaddr)
);
if (result.success) {
console.log(
chalk[OUTPUT_CONFIG.COLORS.SUCCESS](OUTPUT_CONFIG.SYMBOLS.SUCCESS + ' Connected'),
chalk[OUTPUT_CONFIG.COLORS.MUTED](`(${result.duration}ms)`)
);
if (result.peerId) {
console.log('Peer ID:', chalk[OUTPUT_CONFIG.COLORS.WARNING](result.peerId));
}
if (result.protocols && result.protocols.length > 0) {
console.log(`Protocols (${result.protocols.length}):`);
result.protocols.forEach((protocol) => {
console.log(
` ${OUTPUT_CONFIG.SYMBOLS.BULLET} ${chalk[OUTPUT_CONFIG.COLORS.INFO](protocol)}`
);
});
}
} else {
console.log(
chalk[OUTPUT_CONFIG.COLORS.ERROR](OUTPUT_CONFIG.SYMBOLS.FAILURE + ' Failed'),
chalk[OUTPUT_CONFIG.COLORS.MUTED](`(${result.duration}ms)`)
);
console.log('Error:', chalk[OUTPUT_CONFIG.COLORS.ERROR](result.error));
if (result.details) {
console.log('Hint:', chalk[OUTPUT_CONFIG.COLORS.WARNING](result.details));
}
}
});
// Display summary
console.log(chalk[OUTPUT_CONFIG.COLORS.INFO]('\n' + '='.repeat(50)));
console.log(formatMultiSummary(multiResults));
}