splat.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 the additional metas are **NOT** objects or **LACK** enumerable properties
  56. // you are going to have a bad time.
  57. const metalen = metas.length;
  58. if (metalen) {
  59. for (let i = 0; i < metalen; i++) {
  60. Object.assign(info, metas[i]);
  61. }
  62. }
  63. info.message = util.format(msg, ...splat);
  64. return info;
  65. }
  66. /**
  67. * Transforms the `info` message by using `util.format` to complete
  68. * any `info.message` provided it has string interpolation tokens.
  69. * If no tokens exist then `info` is immutable.
  70. *
  71. * @param {Info} info Logform info message.
  72. * @param {Object} opts Options for this instance.
  73. * @returns {Info} Modified info message
  74. */
  75. transform(info) {
  76. const msg = info.message;
  77. const splat = info[SPLAT] || info.splat;
  78. // No need to process anything if splat is undefined
  79. if (!splat || !splat.length) {
  80. return info;
  81. }
  82. // Extract tokens, if none available default to empty array to
  83. // ensure consistancy in expected results
  84. const tokens = msg && msg.match && msg.match(formatRegExp);
  85. // This condition will take care of inputs with info[SPLAT]
  86. // but no tokens present
  87. if (!tokens && (splat || splat.length)) {
  88. const metas = splat.length > 1
  89. ? splat.splice(0)
  90. : splat;
  91. // Now that { splat } has been separated from any potential { meta }. we
  92. // can assign this to the `info` object and write it to our format stream.
  93. // If the additional metas are **NOT** objects or **LACK** enumerable properties
  94. // you are going to have a bad time.
  95. const metalen = metas.length;
  96. if (metalen) {
  97. for (let i = 0; i < metalen; i++) {
  98. Object.assign(info, metas[i]);
  99. }
  100. }
  101. return info;
  102. }
  103. if (tokens) {
  104. return this._splat(info, tokens);
  105. }
  106. return info;
  107. }
  108. }
  109. /*
  110. * function splat (info)
  111. * Returns a new instance of the splat format TransformStream
  112. * which performs string interpolation from `info` objects. This was
  113. * previously exposed implicitly in `winston < 3.0.0`.
  114. */
  115. module.exports = opts => new Splatter(opts);