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