create-prefix-transform-stream.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @module create-prefix-transform-stream
  3. * @author Toru Nagashima
  4. * @copyright 2016 Toru Nagashima. All rights reserved.
  5. * See LICENSE file in root directory for full license.
  6. */
  7. "use strict"
  8. //------------------------------------------------------------------------------
  9. // Requirements
  10. //------------------------------------------------------------------------------
  11. const stream = require("stream")
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. const ALL_BR = /\n/g
  16. /**
  17. * The transform stream to insert a specific prefix.
  18. *
  19. * Several streams can exist for the same output stream.
  20. * This stream will insert the prefix if the last output came from other instance.
  21. * To do that, this stream is using a shared state object.
  22. *
  23. * @private
  24. */
  25. class PrefixTransform extends stream.Transform {
  26. /**
  27. * @param {string} prefix - A prefix text to be inserted.
  28. * @param {object} state - A state object.
  29. * @param {string} state.lastPrefix - The last prefix which is printed.
  30. * @param {boolean} state.lastIsLinebreak -The flag to check whether the last output is a line break or not.
  31. */
  32. constructor(prefix, state) {
  33. super()
  34. this.prefix = prefix
  35. this.state = state
  36. }
  37. /**
  38. * Transforms the output chunk.
  39. *
  40. * @param {string|Buffer} chunk - A chunk to be transformed.
  41. * @param {string} _encoding - The encoding of the chunk.
  42. * @param {function} callback - A callback function that is called when done.
  43. * @returns {void}
  44. */
  45. _transform(chunk, _encoding, callback) {
  46. const prefix = this.prefix
  47. const nPrefix = `\n${prefix}`
  48. const state = this.state
  49. const firstPrefix =
  50. state.lastIsLinebreak ? prefix :
  51. (state.lastPrefix !== prefix) ? "\n" :
  52. /* otherwise */ ""
  53. const prefixed = `${firstPrefix}${chunk}`.replace(ALL_BR, nPrefix)
  54. const index = prefixed.indexOf(prefix, Math.max(0, prefixed.length - prefix.length))
  55. state.lastPrefix = prefix
  56. state.lastIsLinebreak = (index !== -1)
  57. callback(null, (index !== -1) ? prefixed.slice(0, index) : prefixed)
  58. }
  59. }
  60. //------------------------------------------------------------------------------
  61. // Public API
  62. //------------------------------------------------------------------------------
  63. /**
  64. * Create a transform stream to insert the specific prefix.
  65. *
  66. * Several streams can exist for the same output stream.
  67. * This stream will insert the prefix if the last output came from other instance.
  68. * To do that, this stream is using a shared state object.
  69. *
  70. * @param {string} prefix - A prefix text to be inserted.
  71. * @param {object} state - A state object.
  72. * @param {string} state.lastPrefix - The last prefix which is printed.
  73. * @param {boolean} state.lastIsLinebreak -The flag to check whether the last output is a line break or not.
  74. * @returns {stream.Transform} The created transform stream.
  75. */
  76. module.exports = function createPrefixTransform(prefix, state) {
  77. return new PrefixTransform(prefix, state)
  78. }