UNPKG

browser-debugger-cli

Version:

DevTools telemetry in your terminal. For humans and agents. Direct WebSocket to Chrome's debugging port.

78 lines 2.84 kB
/** * Type guards for runtime type validation. * * Provides type-safe validation of IPC response data, replacing unsafe * `as unknown as` type assertions with proper runtime checks. * * WHY: Type assertions bypass TypeScript's type system and can lead to * runtime errors. Type guards provide actual runtime validation while * maintaining type safety. */ import { CommandError } from '../ui/errors/index.js'; import { EXIT_CODES } from '../utils/exitCodes.js'; /** * Type guard to check if a value is a valid NetworkRequest. * * Validates the presence of required NetworkRequest properties. * * @param value - Unknown value to validate * @returns True if value is a valid NetworkRequest * * @example * ```typescript * if (isNetworkRequest(item)) { * console.log(item.url); // TypeScript knows item is NetworkRequest * } * ``` */ export function isNetworkRequest(value) { if (!value || typeof value !== 'object') { return false; } const record = value; return (typeof record['requestId'] === 'string' && typeof record['url'] === 'string' && typeof record['method'] === 'string' && typeof record['timestamp'] === 'number'); } /** * Type guard to check if a value is a valid ConsoleMessage. * * Validates the presence of required ConsoleMessage properties. * * @param value - Unknown value to validate * @returns True if value is a valid ConsoleMessage * * @example * ```typescript * if (isConsoleMessage(item)) { * console.log(item.text); // TypeScript knows item is ConsoleMessage * } * ``` */ export function isConsoleMessage(value) { if (!value || typeof value !== 'object') { return false; } const record = value; return (typeof record['type'] === 'string' && typeof record['text'] === 'string' && typeof record['timestamp'] === 'number'); } // eslint-disable-next-line no-redeclare export function validateDetailsItem(item, type) { if (!item || typeof item !== 'object') { throw new CommandError('Invalid item data: expected an object', { suggestion: 'This may indicate corrupted data from the daemon' }, EXIT_CODES.SESSION_FILE_ERROR); } if (type === 'network') { if (!isNetworkRequest(item)) { throw new CommandError('Invalid network request data: missing required fields (requestId, url, method, timestamp)', { suggestion: 'The request data may be incomplete or corrupted' }, EXIT_CODES.SESSION_FILE_ERROR); } return item; } if (!isConsoleMessage(item)) { throw new CommandError('Invalid console message data: missing required fields (type, text, timestamp)', { suggestion: 'The console message data may be incomplete or corrupted' }, EXIT_CODES.SESSION_FILE_ERROR); } return item; } //# sourceMappingURL=typeGuards.js.map