buffer-stream.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const stream = require('stream')
  11. // ------------------------------------------------------------------------------
  12. // Public Interface
  13. // ------------------------------------------------------------------------------
  14. /**
  15. * The stream to accumulate written data as a single string.
  16. */
  17. module.exports = class BufferStream extends stream.Writable {
  18. /**
  19. * Initialize the current data as a empty string.
  20. */
  21. constructor () {
  22. super()
  23. /**
  24. * Accumulated data.
  25. * @type {string}
  26. */
  27. this.value = ''
  28. }
  29. /**
  30. * Accumulates written data.
  31. *
  32. * @param {string|Buffer} chunk - A written data.
  33. * @param {string} _encoding - The encoding of chunk.
  34. * @param {function} callback - The callback to notify done.
  35. * @returns {void}
  36. */
  37. _write (chunk, _encoding, callback) {
  38. this.value += chunk.toString()
  39. callback()
  40. }
  41. }