splat.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const util = require('util');
  3. const { SPLAT } = require('triple-beam');
  4. /**
  5. * Captures the number of format (i.e. %s strings) in a given string.
  6. * Based on `util.format`, see Node.js source:
  7. * https://github.com/nodejs/node/blob/b1c8f15c5f169e021f7c46eb7b219de95fe97603/lib/util.js#L201-L230
  8. * @type {RegExp}
  9. */
  10. const formatRegExp = /%[scdjifoO%]/g;
  11. /**
  12. * Captures the number of escaped % signs in a format string (i.e. %s strings).
  13. * @type {RegExp}
  14. */
  15. const escapedPercent = /%%/g;
  16. class Splatter {
  17. constructor(opts) {
  18. this.options = opts;
  19. }
  20. /**
  21. * Check to see if tokens <= splat.length, assign { splat, meta } into the
  22. * `info` accordingly, and write to this instance.
  23. *
  24. * @param {Info} info Logform info message.
  25. * @param {String[]} tokens Set of string interpolation tokens.
  26. * @returns {Info} Modified info message
  27. * @private
  28. */
  29. _splat(info, tokens) {
  30. const msg = info.message;
  31. const splat = info[SPLAT] || info.splat || [];
  32. const percents = msg.match(escapedPercent);
  33. const escapes = percents && percents.length || 0;
  34. // The expected splat is the number of tokens minus the number of escapes
  35. // e.g.
  36. // - { expectedSplat: 3 } '%d %s %j'
  37. // - { expectedSplat: 5 } '[%s] %d%% %d%% %s %j'
  38. //
  39. // Any "meta" will be arugments in addition to the expected splat size
  40. // regardless of type. e.g.
  41. //
  42. // logger.log('info', '%d%% %s %j', 100, 'wow', { such: 'js' }, { thisIsMeta: true });
  43. // would result in splat of four (4), but only three (3) are expected. Therefore:
  44. //
  45. // extraSplat = 3 - 4 = -1
  46. // metas = [100, 'wow', { such: 'js' }, { thisIsMeta: true }].splice(-1, -1 * -1);
  47. // splat = [100, 'wow', { such: 'js' }]
  48. const expectedSplat = tokens.length - escapes;
  49. const extraSplat = expectedSplat - splat.length;
  50. const metas = extraSplat < 0
  51. ? splat.splice(extraSplat, -1 * extraSplat)
  52. : [];
  53. // Now that { splat } has been separated from any potential { meta }. we
  54. // can assign this to the `info` object and write it to our format stream.
  55. if (metas.length === 1) {
  56. info.meta = metas[0];
  57. } else if (metas.length) {
  58. info.meta = metas;
  59. }
  60. info.message = util.format(msg, ...splat);
  61. return info;
  62. }
  63. /**
  64. * Transforms the `info` message by using `util.format` to complete
  65. * any `info.message` provided it has string interpolation tokens.
  66. * If no tokens exist then `info` is immutable.
  67. *
  68. * @param {Info} info Logform info message.
  69. * @param {Object} opts Options for this instance.
  70. * @returns {Info} Modified info message
  71. */
  72. transform(info) {
  73. const msg = info.message;
  74. const splat = info[SPLAT] || info.splat;
  75. // No need to process anything if splat is undefined
  76. if (!splat || !splat.length) {
  77. return info;
  78. }
  79. // Extract tokens, if none available default to empty array to
  80. // ensure consistancy in expected results
  81. const tokens = msg && msg.match && msg.match(formatRegExp);
  82. // This condition will take care of inputs with info[SPLAT]
  83. // but no tokens present
  84. if (!tokens && (splat || splat.length)) {
  85. const metas = splat.length > 1
  86. ? splat.splice(0)
  87. : splat;
  88. // Now that { splat } has been separated from any potential { meta }. we
  89. // can assign this to the `info` object and write it to our format stream.
  90. if (metas.length === 1) {
  91. info.meta = metas[0];
  92. } else if (metas.length) {
  93. info.meta = metas;
  94. }
  95. return info;
  96. }
  97. if (tokens) {
  98. return this._splat(info, tokens);
  99. }
  100. return info;
  101. }
  102. }
  103. /*
  104. * function splat (info)
  105. * Returns a new instance of the splat format TransformStream
  106. * which performs string interpolation from `info` objects. This was
  107. * previously exposed implicitly in `winston < 3.0.0`.
  108. */
  109. module.exports = opts => new Splatter(opts);