source: trip-planner-front/node_modules/socket.io/dist/client.js@ 6a80231

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

initial commit

  • Property mode set to 100644
File size: 7.5 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Client = void 0;
4const socket_io_parser_1 = require("socket.io-parser");
5const debugModule = require("debug");
6const url = require("url");
7const debug = debugModule("socket.io:client");
8class Client {
9 /**
10 * Client constructor.
11 *
12 * @param server instance
13 * @param conn
14 * @package
15 */
16 constructor(server, conn) {
17 this.sockets = new Map();
18 this.nsps = new Map();
19 this.server = server;
20 this.conn = conn;
21 this.encoder = server.encoder;
22 this.decoder = new server._parser.Decoder();
23 this.id = conn.id;
24 this.setup();
25 }
26 /**
27 * @return the reference to the request that originated the Engine.IO connection
28 *
29 * @public
30 */
31 get request() {
32 return this.conn.request;
33 }
34 /**
35 * Sets up event listeners.
36 *
37 * @private
38 */
39 setup() {
40 this.onclose = this.onclose.bind(this);
41 this.ondata = this.ondata.bind(this);
42 this.onerror = this.onerror.bind(this);
43 this.ondecoded = this.ondecoded.bind(this);
44 // @ts-ignore
45 this.decoder.on("decoded", this.ondecoded);
46 this.conn.on("data", this.ondata);
47 this.conn.on("error", this.onerror);
48 this.conn.on("close", this.onclose);
49 this.connectTimeout = setTimeout(() => {
50 if (this.nsps.size === 0) {
51 debug("no namespace joined yet, close the client");
52 this.close();
53 }
54 else {
55 debug("the client has already joined a namespace, nothing to do");
56 }
57 }, this.server._connectTimeout);
58 }
59 /**
60 * Connects a client to a namespace.
61 *
62 * @param {String} name - the namespace
63 * @param {Object} auth - the auth parameters
64 * @private
65 */
66 connect(name, auth = {}) {
67 if (this.server._nsps.has(name)) {
68 debug("connecting to namespace %s", name);
69 return this.doConnect(name, auth);
70 }
71 this.server._checkNamespace(name, auth, (dynamicNspName) => {
72 if (dynamicNspName) {
73 debug("dynamic namespace %s was created", dynamicNspName);
74 this.doConnect(name, auth);
75 }
76 else {
77 debug("creation of namespace %s was denied", name);
78 this._packet({
79 type: socket_io_parser_1.PacketType.CONNECT_ERROR,
80 nsp: name,
81 data: {
82 message: "Invalid namespace",
83 },
84 });
85 }
86 });
87 }
88 /**
89 * Connects a client to a namespace.
90 *
91 * @param name - the namespace
92 * @param {Object} auth - the auth parameters
93 *
94 * @private
95 */
96 doConnect(name, auth) {
97 const nsp = this.server.of(name);
98 const socket = nsp._add(this, auth, () => {
99 this.sockets.set(socket.id, socket);
100 this.nsps.set(nsp.name, socket);
101 if (this.connectTimeout) {
102 clearTimeout(this.connectTimeout);
103 this.connectTimeout = undefined;
104 }
105 });
106 }
107 /**
108 * Disconnects from all namespaces and closes transport.
109 *
110 * @private
111 */
112 _disconnect() {
113 for (const socket of this.sockets.values()) {
114 socket.disconnect();
115 }
116 this.sockets.clear();
117 this.close();
118 }
119 /**
120 * Removes a socket. Called by each `Socket`.
121 *
122 * @private
123 */
124 _remove(socket) {
125 if (this.sockets.has(socket.id)) {
126 const nsp = this.sockets.get(socket.id).nsp.name;
127 this.sockets.delete(socket.id);
128 this.nsps.delete(nsp);
129 }
130 else {
131 debug("ignoring remove for %s", socket.id);
132 }
133 }
134 /**
135 * Closes the underlying connection.
136 *
137 * @private
138 */
139 close() {
140 if ("open" === this.conn.readyState) {
141 debug("forcing transport close");
142 this.conn.close();
143 this.onclose("forced server close");
144 }
145 }
146 /**
147 * Writes a packet to the transport.
148 *
149 * @param {Object} packet object
150 * @param {Object} opts
151 * @private
152 */
153 _packet(packet, opts) {
154 opts = opts || {};
155 const self = this;
156 // this writes to the actual connection
157 function writeToEngine(encodedPackets) {
158 // TODO clarify this.
159 if (opts.volatile && !self.conn.transport.writable)
160 return;
161 for (let i = 0; i < encodedPackets.length; i++) {
162 self.conn.write(encodedPackets[i], { compress: opts.compress });
163 }
164 }
165 if ("open" === this.conn.readyState) {
166 debug("writing packet %j", packet);
167 if (!opts.preEncoded) {
168 // not broadcasting, need to encode
169 writeToEngine(this.encoder.encode(packet)); // encode, then write results to engine
170 }
171 else {
172 // a broadcast pre-encodes a packet
173 writeToEngine(packet);
174 }
175 }
176 else {
177 debug("ignoring packet write %j", packet);
178 }
179 }
180 /**
181 * Called with incoming transport data.
182 *
183 * @private
184 */
185 ondata(data) {
186 // try/catch is needed for protocol violations (GH-1880)
187 try {
188 this.decoder.add(data);
189 }
190 catch (e) {
191 this.onerror(e);
192 }
193 }
194 /**
195 * Called when parser fully decodes a packet.
196 *
197 * @private
198 */
199 ondecoded(packet) {
200 if (socket_io_parser_1.PacketType.CONNECT === packet.type) {
201 if (this.conn.protocol === 3) {
202 const parsed = url.parse(packet.nsp, true);
203 this.connect(parsed.pathname, parsed.query);
204 }
205 else {
206 this.connect(packet.nsp, packet.data);
207 }
208 }
209 else {
210 const socket = this.nsps.get(packet.nsp);
211 if (socket) {
212 process.nextTick(function () {
213 socket._onpacket(packet);
214 });
215 }
216 else {
217 debug("no socket for namespace %s", packet.nsp);
218 }
219 }
220 }
221 /**
222 * Handles an error.
223 *
224 * @param {Object} err object
225 * @private
226 */
227 onerror(err) {
228 for (const socket of this.sockets.values()) {
229 socket._onerror(err);
230 }
231 this.conn.close();
232 }
233 /**
234 * Called upon transport close.
235 *
236 * @param reason
237 * @private
238 */
239 onclose(reason) {
240 debug("client close with reason %s", reason);
241 // ignore a potential subsequent `close` event
242 this.destroy();
243 // `nsps` and `sockets` are cleaned up seamlessly
244 for (const socket of this.sockets.values()) {
245 socket._onclose(reason);
246 }
247 this.sockets.clear();
248 this.decoder.destroy(); // clean up decoder
249 }
250 /**
251 * Cleans up event listeners.
252 * @private
253 */
254 destroy() {
255 this.conn.removeListener("data", this.ondata);
256 this.conn.removeListener("error", this.onerror);
257 this.conn.removeListener("close", this.onclose);
258 // @ts-ignore
259 this.decoder.removeListener("decoded", this.ondecoded);
260 if (this.connectTimeout) {
261 clearTimeout(this.connectTimeout);
262 this.connectTimeout = undefined;
263 }
264 }
265}
266exports.Client = Client;
Note: See TracBrowser for help on using the repository browser.