streams.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Promises are promises and streams are streams, and ne'er the twain shall
  3. * meet.
  4. * @module
  5. */
  6. 'use strict'
  7. const BPromise = require('bluebird')
  8. const zlib = require('zlib')
  9. const stringToStream = require('string-to-stream')
  10. const { pipeline, Writable } = require('stream')
  11. function promisePipe(readStream, writeStream) {
  12. return new BPromise(function (resolve, reject) {
  13. pipeline(readStream, writeStream, function (err) {
  14. if (err) {
  15. reject(err)
  16. } else {
  17. resolve()
  18. }
  19. })
  20. })
  21. }
  22. /**
  23. * Pipe a read stream to a write stream. The promise resolves when the write
  24. * stream finishes.
  25. *
  26. * @function
  27. * @param {stream.Readable} readStream
  28. * @param {stream.Writable} writeStream
  29. * @return {Promise}
  30. */
  31. exports.promisePipe = promisePipe
  32. class WritableBuffer extends Writable {
  33. constructor(options) {
  34. super(options)
  35. this.buffers = []
  36. }
  37. _write(chunk, encoding, callback) {
  38. this.buffers.push(chunk)
  39. callback()
  40. }
  41. _final(callback) {
  42. callback()
  43. }
  44. contents() {
  45. return Buffer.concat(this.buffers)
  46. }
  47. }
  48. function readStreamToBuffer(readStream) {
  49. return new BPromise(function (resolve, reject) {
  50. const bufferStream = new WritableBuffer()
  51. pipeline(readStream, bufferStream, function (err) {
  52. if (err) {
  53. reject(err)
  54. } else {
  55. resolve(bufferStream.contents())
  56. }
  57. })
  58. })
  59. }
  60. /**
  61. * Create a promise for the result of reading a stream to a buffer.
  62. *
  63. * @function
  64. * @param {stream.Readable} readStream
  65. * @return {Promise.<Buffer>}
  66. */
  67. exports.readStreamToBuffer = readStreamToBuffer
  68. function gunzipStreamToBuffer(readStream) {
  69. const gunzip = zlib.createGunzip()
  70. const bufferStream = new WritableBuffer()
  71. return new BPromise(function (resolve, reject) {
  72. pipeline(readStream, gunzip, bufferStream, function (err) {
  73. if (err) {
  74. reject(err)
  75. } else {
  76. resolve(bufferStream.contents())
  77. }
  78. })
  79. })
  80. }
  81. /**
  82. * Create a promise for the result of un-gzipping a stream to a buffer.
  83. *
  84. * @function
  85. * @param {stream.Readable} readStream
  86. * @return {Promise.<Buffer>}
  87. */
  88. exports.gunzipStreamToBuffer = gunzipStreamToBuffer
  89. function gzipStringToStream(string) {
  90. const gzip = zlib.createGzip()
  91. return stringToStream(string).pipe(gzip)
  92. }
  93. /**
  94. * Create a write stream that gzips the given string.
  95. *
  96. * @function
  97. * @param {string} string
  98. * @return {stream.Writable}
  99. */
  100. exports.gzipStringToStream = gzipStringToStream