()
| 58 | } |
| 59 | |
| 60 | export function initializeWarningHandler(): void { |
| 61 | // Only set up handler once - check if our handler is already installed |
| 62 | const currentListeners = process.listeners('warning') |
| 63 | if (warningHandler && currentListeners.includes(warningHandler)) { |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | // For external users, remove default Node.js handler to suppress stderr output |
| 68 | // For internal users, only keep default warnings for development builds |
| 69 | // Check development mode directly to avoid async call in init |
| 70 | // This preserves the same logic as getCurrentInstallationType() without async |
| 71 | const isDevelopment = |
| 72 | process.env.NODE_ENV === 'development' || isRunningFromBuildDirectory() |
| 73 | if (!isDevelopment) { |
| 74 | process.removeAllListeners('warning') |
| 75 | } |
| 76 | |
| 77 | // Create and store our warning handler |
| 78 | warningHandler = (warning: Error) => { |
| 79 | try { |
| 80 | const warningKey = `${warning.name}: ${warning.message.slice(0, 50)}` |
| 81 | const count = warningCounts.get(warningKey) || 0 |
| 82 | |
| 83 | // Bound the map to prevent unbounded memory growth from unique warning keys. |
| 84 | // Once the cap is reached, new unique keys are not tracked — their |
| 85 | // occurrence_count will always be reported as 1 in analytics. |
| 86 | if ( |
| 87 | warningCounts.has(warningKey) || |
| 88 | warningCounts.size < MAX_WARNING_KEYS |
| 89 | ) { |
| 90 | warningCounts.set(warningKey, count + 1) |
| 91 | } |
| 92 | |
| 93 | const isInternal = isInternalWarning(warning) |
| 94 | |
| 95 | // Always log to Statsig for monitoring |
| 96 | // Include full details for ant users only, since they may contain code or filepaths |
| 97 | logEvent('tengu_node_warning', { |
| 98 | is_internal: isInternal ? 1 : 0, |
| 99 | occurrence_count: count + 1, |
| 100 | classname: |
| 101 | warning.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 102 | ...(process.env.USER_TYPE === 'ant' && { |
| 103 | message: |
| 104 | warning.message as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 105 | }), |
| 106 | }) |
| 107 | |
| 108 | // In debug mode, show all warnings with context |
| 109 | if (isEnvTruthy(process.env.CLAUDE_DEBUG)) { |
| 110 | const prefix = isInternal ? '[Internal Warning]' : '[Warning]' |
| 111 | logForDebugging(`${prefix} ${warning.toString()}`, { level: 'warn' }) |
| 112 | } |
| 113 | // Hide all warnings from users - they are only logged to Statsig for monitoring |
| 114 | } catch { |
| 115 | // Fail silently - we don't want the warning handler to cause issues |
| 116 | } |
| 117 | } |
no test coverage detected