format.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // All logging goes through here, both to console and log files
  2. const { formatWithOptions: baseFormatWithOptions } = require('node:util')
  3. const { redactLog } = require('@npmcli/redact')
  4. // These are most assuredly not a mistake
  5. // https://eslint.org/docs/latest/rules/no-control-regex
  6. // \x00 through \x1f, \x7f through \x9f, not including \x09 \x0a \x0b \x0d
  7. /* eslint-disable-next-line no-control-regex */
  8. const HAS_C01 = /[\x00-\x08\x0c\x0e-\x1f\x7f-\x9f]/
  9. // Allows everything up to '[38;5;255m' in 8 bit notation
  10. const ALLOWED_SGR = /^\[[0-9;]{0,8}m/
  11. // '[38;5;255m'.length
  12. const SGR_MAX_LEN = 10
  13. // Strips all ANSI C0 and C1 control characters (except for SGR up to 8 bit)
  14. function STRIP_C01 (str) {
  15. if (!HAS_C01.test(str)) {
  16. return str
  17. }
  18. let result = ''
  19. for (let i = 0; i < str.length; i++) {
  20. const char = str[i]
  21. const code = char.charCodeAt(0)
  22. if (!HAS_C01.test(char)) {
  23. // Most characters are in this set so continue early if we can
  24. result = `${result}${char}`
  25. } else if (code === 27 && ALLOWED_SGR.test(str.slice(i + 1, i + SGR_MAX_LEN + 1))) {
  26. // \x1b with allowed SGR
  27. result = `${result}\x1b`
  28. } else if (code <= 31) {
  29. // escape all other C0 control characters besides \x7f
  30. result = `${result}^${String.fromCharCode(code + 64)}`
  31. } else {
  32. // hasC01 ensures this is now a C1 control character or \x7f
  33. result = `${result}^${String.fromCharCode(code - 64)}`
  34. }
  35. }
  36. return result
  37. }
  38. const formatWithOptions = ({ prefix: prefixes = [], eol = '\n', redact = true, ...options }, ...args) => {
  39. const prefix = prefixes.filter(p => p != null).join(' ')
  40. let formatted = STRIP_C01(baseFormatWithOptions(options, ...args))
  41. if (redact) {
  42. formatted = redactLog(formatted)
  43. }
  44. // Splitting could be changed to only `\n` once we are sure we only emit unix newlines.
  45. // The eol param to this function will put the correct newlines in place for the returned string.
  46. const lines = formatted.split(/\r?\n/)
  47. return lines.reduce((acc, l) => `${acc}${prefix}${prefix && l ? ' ' : ''}${l}${eol}`, '')
  48. }
  49. module.exports = { formatWithOptions }