( levels: Array<ConsoleLevel>, viteServerUrl: string, )
| 21 | * Returns the inline code as a string - no imports needed since we use fetch. |
| 22 | */ |
| 23 | export function generateConsolePipeCode( |
| 24 | levels: Array<ConsoleLevel>, |
| 25 | viteServerUrl: string, |
| 26 | ): string { |
| 27 | const levelsArray = JSON.stringify(levels) |
| 28 | |
| 29 | return ` |
| 30 | ;(function __tsdConsolePipe() { |
| 31 | // Detect environment |
| 32 | var isServer = typeof window === 'undefined'; |
| 33 | var envKey = isServer ? '__TSD_SERVER_CONSOLE_PIPE_INITIALIZED__' : '__TSD_CONSOLE_PIPE_INITIALIZED__'; |
| 34 | var globalObj = isServer ? globalThis : window; |
| 35 | |
| 36 | // Only run once per environment |
| 37 | if (globalObj[envKey]) return; |
| 38 | globalObj[envKey] = true; |
| 39 | |
| 40 | var CONSOLE_LEVELS = ${levelsArray}; |
| 41 | var VITE_SERVER_URL = ${JSON.stringify(viteServerUrl)}; |
| 42 | |
| 43 | // Store original console methods before we override them |
| 44 | var originalConsole = {}; |
| 45 | for (var i = 0; i < CONSOLE_LEVELS.length; i++) { |
| 46 | var level = CONSOLE_LEVELS[i]; |
| 47 | originalConsole[level] = console[level].bind(console); |
| 48 | } |
| 49 | |
| 50 | // Simple inline batcher implementation |
| 51 | var batchedEntries = []; |
| 52 | var batchTimeout = null; |
| 53 | var BATCH_WAIT = isServer ? 50 : 100; |
| 54 | var BATCH_MAX_SIZE = isServer ? 20 : 50; |
| 55 | |
| 56 | function flushBatch() { |
| 57 | if (batchedEntries.length === 0) return; |
| 58 | |
| 59 | var entries = batchedEntries; |
| 60 | batchedEntries = []; |
| 61 | batchTimeout = null; |
| 62 | |
| 63 | // Determine endpoint based on environment |
| 64 | var endpoint = isServer |
| 65 | ? VITE_SERVER_URL + '/__tsd/console-pipe/server' |
| 66 | : '/__tsd/console-pipe'; |
| 67 | |
| 68 | // Send to Vite server via fetch |
| 69 | fetch(endpoint, { |
| 70 | method: 'POST', |
| 71 | headers: { 'Content-Type': 'application/json' }, |
| 72 | body: JSON.stringify({ entries: entries }), |
| 73 | }).catch(function( ) { |
| 74 | // Swallow errors |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | function addToBatch(entry) { |
| 79 | batchedEntries.push(entry); |
| 80 |
no outgoing calls