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

transport.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Module dependencies.
  3. */
  4. var EventEmitter = require('events').EventEmitter;
  5. var parser = require('engine.io-parser');
  6. var util = require('util');
  7. var debug = require('debug')('engine:transport');
  8. /**
  9. * Expose the constructor.
  10. */
  11. module.exports = Transport;
  12. /**
  13. * Noop function.
  14. *
  15. * @api private
  16. */
  17. function noop () {}
  18. /**
  19. * Transport constructor.
  20. *
  21. * @param {http.IncomingMessage} request
  22. * @api public
  23. */
  24. function Transport (req) {
  25. this.readyState = 'open';
  26. this.discarded = false;
  27. }
  28. /**
  29. * Inherits from EventEmitter.
  30. */
  31. util.inherits(Transport, EventEmitter);
  32. /**
  33. * Flags the transport as discarded.
  34. *
  35. * @api private
  36. */
  37. Transport.prototype.discard = function () {
  38. this.discarded = true;
  39. };
  40. /**
  41. * Called with an incoming HTTP request.
  42. *
  43. * @param {http.IncomingMessage} request
  44. * @api private
  45. */
  46. Transport.prototype.onRequest = function (req) {
  47. debug('setting request');
  48. this.req = req;
  49. };
  50. /**
  51. * Closes the transport.
  52. *
  53. * @api private
  54. */
  55. Transport.prototype.close = function (fn) {
  56. if ('closed' === this.readyState || 'closing' === this.readyState) return;
  57. this.readyState = 'closing';
  58. this.doClose(fn || noop);
  59. };
  60. /**
  61. * Called with a transport error.
  62. *
  63. * @param {String} message error
  64. * @param {Object} error description
  65. * @api private
  66. */
  67. Transport.prototype.onError = function (msg, desc) {
  68. if (this.listeners('error').length) {
  69. var err = new Error(msg);
  70. err.type = 'TransportError';
  71. err.description = desc;
  72. this.emit('error', err);
  73. } else {
  74. debug('ignored transport error %s (%s)', msg, desc);
  75. }
  76. };
  77. /**
  78. * Called with parsed out a packets from the data stream.
  79. *
  80. * @param {Object} packet
  81. * @api private
  82. */
  83. Transport.prototype.onPacket = function (packet) {
  84. this.emit('packet', packet);
  85. };
  86. /**
  87. * Called with the encoded packet data.
  88. *
  89. * @param {String} data
  90. * @api private
  91. */
  92. Transport.prototype.onData = function (data) {
  93. this.onPacket(parser.decodePacket(data));
  94. };
  95. /**
  96. * Called upon transport close.
  97. *
  98. * @api private
  99. */
  100. Transport.prototype.onClose = function () {
  101. this.readyState = 'closed';
  102. this.emit('close');
  103. };