source: trip-planner-front/node_modules/engine.io/lib/transport.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1const EventEmitter = require("events");
2const parser_v4 = require("engine.io-parser");
3const parser_v3 = require("./parser-v3/index");
4const debug = require("debug")("engine:transport");
5
6/**
7 * Noop function.
8 *
9 * @api private
10 */
11
12function noop() {}
13
14class 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
110module.exports = Transport;
Note: See TracBrowser for help on using the repository browser.