| 373 | } |
| 374 | |
| 375 | function printer( |
| 376 | val: unknown, |
| 377 | config: Config, |
| 378 | indentation: string, |
| 379 | depth: number, |
| 380 | refs: Refs, |
| 381 | hasCalledToJSON?: boolean, |
| 382 | ): string { |
| 383 | let result: string |
| 384 | |
| 385 | const plugin = findPlugin(config.plugins, val) |
| 386 | if (plugin !== null) { |
| 387 | result = printPlugin(plugin, val, config, indentation, depth, refs) |
| 388 | } |
| 389 | else { |
| 390 | const basicResult = printBasicValue( |
| 391 | val, |
| 392 | config.printFunctionName, |
| 393 | config.escapeRegex, |
| 394 | config.escapeString, |
| 395 | ) |
| 396 | if (basicResult !== null) { |
| 397 | result = basicResult |
| 398 | } |
| 399 | else { |
| 400 | result = printComplexValue( |
| 401 | val, |
| 402 | config, |
| 403 | indentation, |
| 404 | depth, |
| 405 | refs, |
| 406 | hasCalledToJSON, |
| 407 | ) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Per-depth output budget (inspired by Node's util.inspect). |
| 412 | // Each depth level tracks output independently, so nested results |
| 413 | // don't inflate a single counter (which would undercount by ~Nx for |
| 414 | // N levels of nesting). Nodes at the same depth produce disjoint spans |
| 415 | // in the output string, so each bucket accurately reflects output at |
| 416 | // that level. Total output is bounded by maxDepth × maxOutputLength. |
| 417 | config._outputLengthPerDepth[depth] ??= 0 |
| 418 | config._outputLengthPerDepth[depth] += result.length |
| 419 | if (config._outputLengthPerDepth[depth] > config.maxOutputLength) { |
| 420 | config.maxDepth = 0 |
| 421 | } |
| 422 | |
| 423 | return result |
| 424 | } |
| 425 | |
| 426 | const DEFAULT_THEME: Theme = { |
| 427 | comment: 'gray', |