Front end of the Slack clone application.

index.js 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*!
  2. * destroy
  3. * Copyright(c) 2014 Jonathan Ong
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module dependencies.
  9. * @private
  10. */
  11. var ReadStream = require('fs').ReadStream
  12. var Stream = require('stream')
  13. /**
  14. * Module exports.
  15. * @public
  16. */
  17. module.exports = destroy
  18. /**
  19. * Destroy a stream.
  20. *
  21. * @param {object} stream
  22. * @public
  23. */
  24. function destroy(stream) {
  25. if (stream instanceof ReadStream) {
  26. return destroyReadStream(stream)
  27. }
  28. if (!(stream instanceof Stream)) {
  29. return stream
  30. }
  31. if (typeof stream.destroy === 'function') {
  32. stream.destroy()
  33. }
  34. return stream
  35. }
  36. /**
  37. * Destroy a ReadStream.
  38. *
  39. * @param {object} stream
  40. * @private
  41. */
  42. function destroyReadStream(stream) {
  43. stream.destroy()
  44. if (typeof stream.close === 'function') {
  45. // node.js core bug work-around
  46. stream.on('open', onOpenClose)
  47. }
  48. return stream
  49. }
  50. /**
  51. * On open handler to close stream.
  52. * @private
  53. */
  54. function onOpenClose() {
  55. if (typeof this.fd === 'number') {
  56. // actually close down the fd
  57. this.close()
  58. }
  59. }