legacy.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict'
  2. const { ArrayIsArray, ObjectSetPrototypeOf } = require('../../ours/primordials')
  3. const { EventEmitter: EE } = require('events')
  4. function Stream(opts) {
  5. EE.call(this, opts)
  6. }
  7. ObjectSetPrototypeOf(Stream.prototype, EE.prototype)
  8. ObjectSetPrototypeOf(Stream, EE)
  9. Stream.prototype.pipe = function (dest, options) {
  10. const source = this
  11. function ondata(chunk) {
  12. if (dest.writable && dest.write(chunk) === false && source.pause) {
  13. source.pause()
  14. }
  15. }
  16. source.on('data', ondata)
  17. function ondrain() {
  18. if (source.readable && source.resume) {
  19. source.resume()
  20. }
  21. }
  22. dest.on('drain', ondrain)
  23. // If the 'end' option is not supplied, dest.end() will be called when
  24. // source gets the 'end' or 'close' events. Only dest.end() once.
  25. if (!dest._isStdio && (!options || options.end !== false)) {
  26. source.on('end', onend)
  27. source.on('close', onclose)
  28. }
  29. let didOnEnd = false
  30. function onend() {
  31. if (didOnEnd) return
  32. didOnEnd = true
  33. dest.end()
  34. }
  35. function onclose() {
  36. if (didOnEnd) return
  37. didOnEnd = true
  38. if (typeof dest.destroy === 'function') dest.destroy()
  39. }
  40. // Don't leave dangling pipes when there are errors.
  41. function onerror(er) {
  42. cleanup()
  43. if (EE.listenerCount(this, 'error') === 0) {
  44. this.emit('error', er)
  45. }
  46. }
  47. prependListener(source, 'error', onerror)
  48. prependListener(dest, 'error', onerror)
  49. // Remove all the event listeners that were added.
  50. function cleanup() {
  51. source.removeListener('data', ondata)
  52. dest.removeListener('drain', ondrain)
  53. source.removeListener('end', onend)
  54. source.removeListener('close', onclose)
  55. source.removeListener('error', onerror)
  56. dest.removeListener('error', onerror)
  57. source.removeListener('end', cleanup)
  58. source.removeListener('close', cleanup)
  59. dest.removeListener('close', cleanup)
  60. }
  61. source.on('end', cleanup)
  62. source.on('close', cleanup)
  63. dest.on('close', cleanup)
  64. dest.emit('pipe', source)
  65. // Allow for unix-like usage: A.pipe(B).pipe(C)
  66. return dest
  67. }
  68. function prependListener(emitter, event, fn) {
  69. // Sadly this is not cacheable as some libraries bundle their own
  70. // event emitter implementation with them.
  71. if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn)
  72. // This is a hack to make sure that our error handler is attached before any
  73. // userland ones. NEVER DO THIS. This is here only because this code needs
  74. // to continue to work with older versions of Node.js that do not include
  75. // the prependListener() method. The goal is to eventually remove this hack.
  76. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn)
  77. else if (ArrayIsArray(emitter._events[event])) emitter._events[event].unshift(fn)
  78. else emitter._events[event] = [fn, emitter._events[event]]
  79. }
  80. module.exports = {
  81. Stream,
  82. prependListener
  83. }