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

transport.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Module dependencies.
  3. */
  4. var parser = require('engine.io-parser');
  5. var Emitter = require('component-emitter');
  6. /**
  7. * Module exports.
  8. */
  9. module.exports = Transport;
  10. /**
  11. * Transport abstract constructor.
  12. *
  13. * @param {Object} options.
  14. * @api private
  15. */
  16. function Transport (opts) {
  17. this.path = opts.path;
  18. this.hostname = opts.hostname;
  19. this.port = opts.port;
  20. this.secure = opts.secure;
  21. this.query = opts.query;
  22. this.timestampParam = opts.timestampParam;
  23. this.timestampRequests = opts.timestampRequests;
  24. this.readyState = '';
  25. this.agent = opts.agent || false;
  26. this.socket = opts.socket;
  27. this.enablesXDR = opts.enablesXDR;
  28. this.withCredentials = opts.withCredentials;
  29. // SSL options for Node.js client
  30. this.pfx = opts.pfx;
  31. this.key = opts.key;
  32. this.passphrase = opts.passphrase;
  33. this.cert = opts.cert;
  34. this.ca = opts.ca;
  35. this.ciphers = opts.ciphers;
  36. this.rejectUnauthorized = opts.rejectUnauthorized;
  37. this.forceNode = opts.forceNode;
  38. // results of ReactNative environment detection
  39. this.isReactNative = opts.isReactNative;
  40. // other options for Node.js client
  41. this.extraHeaders = opts.extraHeaders;
  42. this.localAddress = opts.localAddress;
  43. }
  44. /**
  45. * Mix in `Emitter`.
  46. */
  47. Emitter(Transport.prototype);
  48. /**
  49. * Emits an error.
  50. *
  51. * @param {String} str
  52. * @return {Transport} for chaining
  53. * @api public
  54. */
  55. Transport.prototype.onError = function (msg, desc) {
  56. var err = new Error(msg);
  57. err.type = 'TransportError';
  58. err.description = desc;
  59. this.emit('error', err);
  60. return this;
  61. };
  62. /**
  63. * Opens the transport.
  64. *
  65. * @api public
  66. */
  67. Transport.prototype.open = function () {
  68. if ('closed' === this.readyState || '' === this.readyState) {
  69. this.readyState = 'opening';
  70. this.doOpen();
  71. }
  72. return this;
  73. };
  74. /**
  75. * Closes the transport.
  76. *
  77. * @api private
  78. */
  79. Transport.prototype.close = function () {
  80. if ('opening' === this.readyState || 'open' === this.readyState) {
  81. this.doClose();
  82. this.onClose();
  83. }
  84. return this;
  85. };
  86. /**
  87. * Sends multiple packets.
  88. *
  89. * @param {Array} packets
  90. * @api private
  91. */
  92. Transport.prototype.send = function (packets) {
  93. if ('open' === this.readyState) {
  94. this.write(packets);
  95. } else {
  96. throw new Error('Transport not open');
  97. }
  98. };
  99. /**
  100. * Called upon open
  101. *
  102. * @api private
  103. */
  104. Transport.prototype.onOpen = function () {
  105. this.readyState = 'open';
  106. this.writable = true;
  107. this.emit('open');
  108. };
  109. /**
  110. * Called with data.
  111. *
  112. * @param {String} data
  113. * @api private
  114. */
  115. Transport.prototype.onData = function (data) {
  116. var packet = parser.decodePacket(data, this.socket.binaryType);
  117. this.onPacket(packet);
  118. };
  119. /**
  120. * Called with a decoded packet.
  121. */
  122. Transport.prototype.onPacket = function (packet) {
  123. this.emit('packet', packet);
  124. };
  125. /**
  126. * Called upon close.
  127. *
  128. * @api private
  129. */
  130. Transport.prototype.onClose = function () {
  131. this.readyState = 'closed';
  132. this.emit('close');
  133. };