Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

stream.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. const { Duplex } = require('stream');
  3. /**
  4. * Emits the `'close'` event on a stream.
  5. *
  6. * @param {stream.Duplex} The stream.
  7. * @private
  8. */
  9. function emitClose(stream) {
  10. stream.emit('close');
  11. }
  12. /**
  13. * The listener of the `'end'` event.
  14. *
  15. * @private
  16. */
  17. function duplexOnEnd() {
  18. if (!this.destroyed && this._writableState.finished) {
  19. this.destroy();
  20. }
  21. }
  22. /**
  23. * The listener of the `'error'` event.
  24. *
  25. * @private
  26. */
  27. function duplexOnError(err) {
  28. this.removeListener('error', duplexOnError);
  29. this.destroy();
  30. if (this.listenerCount('error') === 0) {
  31. // Do not suppress the throwing behavior.
  32. this.emit('error', err);
  33. }
  34. }
  35. /**
  36. * Wraps a `WebSocket` in a duplex stream.
  37. *
  38. * @param {WebSocket} ws The `WebSocket` to wrap
  39. * @param {Object} options The options for the `Duplex` constructor
  40. * @return {stream.Duplex} The duplex stream
  41. * @public
  42. */
  43. function createWebSocketStream(ws, options) {
  44. let resumeOnReceiverDrain = true;
  45. function receiverOnDrain() {
  46. if (resumeOnReceiverDrain) ws._socket.resume();
  47. }
  48. if (ws.readyState === ws.CONNECTING) {
  49. ws.once('open', function open() {
  50. ws._receiver.removeAllListeners('drain');
  51. ws._receiver.on('drain', receiverOnDrain);
  52. });
  53. } else {
  54. ws._receiver.removeAllListeners('drain');
  55. ws._receiver.on('drain', receiverOnDrain);
  56. }
  57. const duplex = new Duplex({
  58. ...options,
  59. autoDestroy: false,
  60. emitClose: false,
  61. objectMode: false,
  62. writableObjectMode: false
  63. });
  64. ws.on('message', function message(msg) {
  65. if (!duplex.push(msg)) {
  66. resumeOnReceiverDrain = false;
  67. ws._socket.pause();
  68. }
  69. });
  70. ws.once('error', function error(err) {
  71. duplex.destroy(err);
  72. });
  73. ws.once('close', function close() {
  74. if (duplex.destroyed) return;
  75. duplex.push(null);
  76. });
  77. duplex._destroy = function(err, callback) {
  78. if (ws.readyState === ws.CLOSED) {
  79. callback(err);
  80. process.nextTick(emitClose, duplex);
  81. return;
  82. }
  83. ws.once('close', function close() {
  84. callback(err);
  85. process.nextTick(emitClose, duplex);
  86. });
  87. ws.terminate();
  88. };
  89. duplex._final = function(callback) {
  90. if (ws.readyState === ws.CONNECTING) {
  91. ws.once('open', function open() {
  92. duplex._final(callback);
  93. });
  94. return;
  95. }
  96. if (ws._socket._writableState.finished) {
  97. if (duplex._readableState.endEmitted) duplex.destroy();
  98. callback();
  99. } else {
  100. ws._socket.once('finish', function finish() {
  101. // `duplex` is not destroyed here because the `'end'` event will be
  102. // emitted on `duplex` after this `'finish'` event. The EOF signaling
  103. // `null` chunk is, in fact, pushed when the WebSocket emits `'close'`.
  104. callback();
  105. });
  106. ws.close();
  107. }
  108. };
  109. duplex._read = function() {
  110. if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) {
  111. resumeOnReceiverDrain = true;
  112. if (!ws._receiver._writableState.needDrain) ws._socket.resume();
  113. }
  114. };
  115. duplex._write = function(chunk, encoding, callback) {
  116. if (ws.readyState === ws.CONNECTING) {
  117. ws.once('open', function open() {
  118. duplex._write(chunk, encoding, callback);
  119. });
  120. return;
  121. }
  122. ws.send(chunk, callback);
  123. };
  124. duplex.on('end', duplexOnEnd);
  125. duplex.on('error', duplexOnError);
  126. return duplex;
  127. }
  128. module.exports = createWebSocketStream;