You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*!
  2. * unpipe
  3. * Copyright(c) 2015 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module exports.
  9. * @public
  10. */
  11. module.exports = unpipe
  12. /**
  13. * Determine if there are Node.js pipe-like data listeners.
  14. * @private
  15. */
  16. function hasPipeDataListeners(stream) {
  17. var listeners = stream.listeners('data')
  18. for (var i = 0; i < listeners.length; i++) {
  19. if (listeners[i].name === 'ondata') {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. /**
  26. * Unpipe a stream from all destinations.
  27. *
  28. * @param {object} stream
  29. * @public
  30. */
  31. function unpipe(stream) {
  32. if (!stream) {
  33. throw new TypeError('argument stream is required')
  34. }
  35. if (typeof stream.unpipe === 'function') {
  36. // new-style
  37. stream.unpipe()
  38. return
  39. }
  40. // Node.js 0.8 hack
  41. if (!hasPipeDataListeners(stream)) {
  42. return
  43. }
  44. var listener
  45. var listeners = stream.listeners('close')
  46. for (var i = 0; i < listeners.length; i++) {
  47. listener = listeners[i]
  48. if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
  49. continue
  50. }
  51. // invoke the listener
  52. listener.call(stream)
  53. }
  54. }