cdp-diagnostic-helpers.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import type {
  2. CDPConsoleDiagnosticEntry,
  3. CDPNetworkDiagnosticEntry,
  4. CDPNetworkDiagnosticResourceType,
  5. CDPNetworkDiagnosticStatusFilter,
  6. } from './cdp-helper-contracts.js';
  7. import {
  8. sanitizeConsoleDiagnosticUrl,
  9. sanitizeConsoleMessage,
  10. truncateConsoleValue,
  11. } from './console-diagnostic-sanitizer.js';
  12. export const MAX_CONSOLE_DIAGNOSTIC_ENTRIES = 200;
  13. export const MAX_CONSOLE_DIAGNOSTIC_PAGE_BYTES = 48 * 1024;
  14. export const MAX_NETWORK_DIAGNOSTIC_ENTRIES = 200;
  15. export const MAX_NETWORK_DIAGNOSTIC_PAGE_BYTES = 48 * 1024;
  16. const MAX_CONSOLE_STACK_FRAMES = 8;
  17. const MAX_NETWORK_TEXT_CHARS = 1_024;
  18. export const browserInputDiagnosticLogger = {
  19. info(fields: Record<string, unknown>): void {
  20. // eslint-disable-next-line no-console -- opt-in local browser diagnostics
  21. if (process.env.MAVIS_BROWSER_DEBUG === '1') console.info(fields);
  22. },
  23. warn(fields: Record<string, unknown>): void {
  24. // eslint-disable-next-line no-console -- opt-in local browser diagnostics
  25. if (process.env.MAVIS_BROWSER_DEBUG === '1') console.warn(fields);
  26. },
  27. };
  28. export interface CDPRemoteObjectSummary {
  29. type?: string;
  30. subtype?: string;
  31. className?: string;
  32. value?: unknown;
  33. unserializableValue?: string;
  34. description?: string;
  35. objectId?: string;
  36. }
  37. export interface CDPConsoleCallFrame {
  38. functionName?: string;
  39. url?: string;
  40. lineNumber?: number;
  41. columnNumber?: number;
  42. }
  43. export interface CDPConsoleStackTrace {
  44. callFrames?: CDPConsoleCallFrame[];
  45. }
  46. export interface BufferedConsoleDiagnosticEntry extends CDPConsoleDiagnosticEntry {
  47. exceptionId?: number;
  48. }
  49. export interface BufferedNetworkDiagnosticEntry extends CDPNetworkDiagnosticEntry {
  50. requestId: string;
  51. startedAtMonotonic: number;
  52. loaderId?: string;
  53. }
  54. export function sanitizeNetworkDiagnosticUrl(value: string): string {
  55. try {
  56. const url = new URL(value);
  57. if (url.protocol === 'data:') return 'data:[REDACTED]';
  58. if (url.protocol === 'blob:') {
  59. return `blob:${sanitizeConsoleDiagnosticUrl(value.slice('blob:'.length))}`;
  60. }
  61. } catch {
  62. // The common sanitizer still applies bounded best-effort credential/query removal.
  63. }
  64. return sanitizeConsoleDiagnosticUrl(value);
  65. }
  66. export function sanitizeNetworkText(value: string): string {
  67. return truncateConsoleValue(
  68. sanitizeConsoleMessage(value).replace(/\bdata:[^\s<>"']+/giu, 'data:[REDACTED]'),
  69. MAX_NETWORK_TEXT_CHARS,
  70. );
  71. }
  72. const NETWORK_RESOURCE_TYPES = new Set<CDPNetworkDiagnosticResourceType>([
  73. 'document',
  74. 'stylesheet',
  75. 'image',
  76. 'media',
  77. 'font',
  78. 'script',
  79. 'xhr',
  80. 'fetch',
  81. 'eventsource',
  82. 'manifest',
  83. 'preflight',
  84. 'other',
  85. ]);
  86. export function normalizeNetworkResourceType(value: unknown): CDPNetworkDiagnosticResourceType {
  87. const normalized = typeof value === 'string' ? value.toLowerCase() : 'other';
  88. return NETWORK_RESOURCE_TYPES.has(normalized as CDPNetworkDiagnosticResourceType)
  89. ? (normalized as CDPNetworkDiagnosticResourceType)
  90. : 'other';
  91. }
  92. export function networkStatusMatches(
  93. entry: CDPNetworkDiagnosticEntry,
  94. filters: ReadonlySet<CDPNetworkDiagnosticStatusFilter>,
  95. ): boolean {
  96. if (filters.size === 0) return true;
  97. if (filters.has('pending') && entry.outcome === 'pending') return true;
  98. if (filters.has('failed') && entry.outcome === 'failed') return true;
  99. if (filters.has('success') && entry.outcome === 'success') return true;
  100. if (typeof entry.status !== 'number') return false;
  101. const statusClass = `${Math.floor(entry.status / 100)}xx` as CDPNetworkDiagnosticStatusFilter;
  102. return filters.has(statusClass);
  103. }
  104. export function summarizeRemoteObject(remote: CDPRemoteObjectSummary): string {
  105. if (remote.value !== undefined) {
  106. if (typeof remote.value === 'string') return remote.value;
  107. if (remote.value === null) return 'null';
  108. return String(remote.value);
  109. }
  110. if (remote.unserializableValue) return remote.unserializableValue;
  111. if (remote.description) return remote.description;
  112. if (remote.subtype === 'null') return 'null';
  113. return remote.className || remote.subtype || remote.type || '';
  114. }
  115. export function formatConsoleStack(stackTrace?: CDPConsoleStackTrace): string[] | undefined {
  116. const frames = stackTrace?.callFrames?.slice(0, MAX_CONSOLE_STACK_FRAMES) ?? [];
  117. if (frames.length === 0) return undefined;
  118. return frames.map((frame) => {
  119. const fn = truncateConsoleValue(
  120. sanitizeConsoleMessage(frame.functionName || '<anonymous>'),
  121. 256,
  122. );
  123. const url = sanitizeConsoleDiagnosticUrl(frame.url || '<unknown>');
  124. const line = Math.max(0, Number(frame.lineNumber ?? 0)) + 1;
  125. const column = Math.max(0, Number(frame.columnNumber ?? 0)) + 1;
  126. return `${fn} (${url}:${line}:${column})`;
  127. });
  128. }