source: trip-planner-front/node_modules/sockjs/lib/transport.js@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 11.9 KB
Line 
1// Generated by CoffeeScript 1.12.7
2(function() {
3 var GenericReceiver, MAP, ResponseReceiver, Session, SockJSConnection, Transport, closeFrame, register, stream, utils, uuidv4,
4 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5 hasProp = {}.hasOwnProperty;
6
7 stream = require('stream');
8
9 uuidv4 = require('uuid/v4');
10
11 utils = require('./utils');
12
13 Transport = (function() {
14 function Transport() {}
15
16 return Transport;
17
18 })();
19
20 Transport.CONNECTING = 0;
21
22 Transport.OPEN = 1;
23
24 Transport.CLOSING = 2;
25
26 Transport.CLOSED = 3;
27
28 closeFrame = function(status, reason) {
29 return 'c' + JSON.stringify([status, reason]);
30 };
31
32 SockJSConnection = (function(superClass) {
33 extend(SockJSConnection, superClass);
34
35 function SockJSConnection(_session) {
36 this._session = _session;
37 this.id = uuidv4();
38 this.headers = {};
39 this.prefix = this._session.prefix;
40 }
41
42 SockJSConnection.prototype.toString = function() {
43 return '<SockJSConnection ' + this.id + '>';
44 };
45
46 SockJSConnection.prototype.write = function(string) {
47 return this._session.send('' + string);
48 };
49
50 SockJSConnection.prototype.end = function(string) {
51 if (string) {
52 this.write(string);
53 }
54 this.close();
55 return null;
56 };
57
58 SockJSConnection.prototype.close = function(code, reason) {
59 return this._session.close(code, reason);
60 };
61
62 SockJSConnection.prototype.destroy = function() {
63 this.end();
64 return this.removeAllListeners();
65 };
66
67 SockJSConnection.prototype.destroySoon = function() {
68 return this.destroy();
69 };
70
71 return SockJSConnection;
72
73 })(stream.Stream);
74
75 SockJSConnection.prototype.__defineGetter__('readable', function() {
76 return this._session.readyState === Transport.OPEN;
77 });
78
79 SockJSConnection.prototype.__defineGetter__('writable', function() {
80 return this._session.readyState === Transport.OPEN;
81 });
82
83 SockJSConnection.prototype.__defineGetter__('readyState', function() {
84 return this._session.readyState;
85 });
86
87 MAP = {};
88
89 Session = (function() {
90 function Session(session_id1, server) {
91 this.session_id = session_id1;
92 this.heartbeat_delay = server.options.heartbeat_delay;
93 this.disconnect_delay = server.options.disconnect_delay;
94 this.prefix = server.options.prefix;
95 this.send_buffer = [];
96 this.is_closing = false;
97 this.readyState = Transport.CONNECTING;
98 if (this.session_id) {
99 MAP[this.session_id] = this;
100 }
101 this.timeout_cb = (function(_this) {
102 return function() {
103 return _this.didTimeout();
104 };
105 })(this);
106 this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
107 this.connection = new SockJSConnection(this);
108 this.emit_open = (function(_this) {
109 return function() {
110 _this.emit_open = null;
111 return server.emit('connection', _this.connection);
112 };
113 })(this);
114 }
115
116 Session.prototype.register = function(req, recv) {
117 if (this.recv) {
118 recv.doSendFrame(closeFrame(2010, "Another connection still open"));
119 recv.didClose();
120 return;
121 }
122 if (this.to_tref) {
123 clearTimeout(this.to_tref);
124 this.to_tref = null;
125 }
126 if (this.readyState === Transport.CLOSING) {
127 this.flushToRecv(recv);
128 recv.doSendFrame(this.close_frame);
129 recv.didClose();
130 this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
131 return;
132 }
133 this.recv = recv;
134 this.recv.session = this;
135 this.decorateConnection(req);
136 if (this.readyState === Transport.CONNECTING) {
137 this.recv.doSendFrame('o');
138 this.readyState = Transport.OPEN;
139 process.nextTick(this.emit_open);
140 }
141 if (!this.recv) {
142 return;
143 }
144 this.tryFlush();
145 };
146
147 Session.prototype.decorateConnection = function(req) {
148 var address, headers, i, key, len, ref, remoteAddress, remotePort, socket, x;
149 if (!(socket = this.recv.connection)) {
150 socket = this.recv.response.connection;
151 }
152 try {
153 remoteAddress = socket.remoteAddress;
154 remotePort = socket.remotePort;
155 address = socket.address();
156 } catch (error) {
157 x = error;
158 }
159 if (remoteAddress) {
160 this.connection.remoteAddress = remoteAddress;
161 this.connection.remotePort = remotePort;
162 this.connection.address = address;
163 }
164 this.connection.url = req.url;
165 this.connection.pathname = req.pathname;
166 this.connection.protocol = this.recv.protocol;
167 headers = {};
168 ref = ['referer', 'x-client-ip', 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-port', 'x-cluster-client-ip', 'via', 'x-real-ip', 'x-forwarded-proto', 'x-ssl', 'dnt', 'host', 'user-agent', 'accept-language'];
169 for (i = 0, len = ref.length; i < len; i++) {
170 key = ref[i];
171 if (req.headers[key]) {
172 headers[key] = req.headers[key];
173 }
174 }
175 if (headers) {
176 return this.connection.headers = headers;
177 }
178 };
179
180 Session.prototype.unregister = function() {
181 var delay;
182 delay = this.recv.delay_disconnect;
183 this.recv.session = null;
184 this.recv = null;
185 if (this.to_tref) {
186 clearTimeout(this.to_tref);
187 }
188 if (delay) {
189 return this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
190 } else {
191 return this.timeout_cb();
192 }
193 };
194
195 Session.prototype.flushToRecv = function(recv) {
196 var ref, sb;
197 if (this.send_buffer.length > 0) {
198 ref = [this.send_buffer, []], sb = ref[0], this.send_buffer = ref[1];
199 recv.doSendBulk(sb);
200 return true;
201 }
202 return false;
203 };
204
205 Session.prototype.tryFlush = function() {
206 var x;
207 if (!this.flushToRecv(this.recv) || !this.to_tref) {
208 if (this.to_tref) {
209 clearTimeout(this.to_tref);
210 }
211 x = (function(_this) {
212 return function() {
213 if (_this.recv) {
214 _this.to_tref = setTimeout(x, _this.heartbeat_delay);
215 return _this.recv.heartbeat();
216 }
217 };
218 })(this);
219 this.to_tref = setTimeout(x, this.heartbeat_delay);
220 }
221 };
222
223 Session.prototype.didTimeout = function() {
224 if (this.to_tref) {
225 clearTimeout(this.to_tref);
226 this.to_tref = null;
227 }
228 if (this.readyState !== Transport.CONNECTING && this.readyState !== Transport.OPEN && this.readyState !== Transport.CLOSING) {
229 throw Error('INVALID_STATE_ERR');
230 }
231 if (this.recv) {
232 throw Error('RECV_STILL_THERE');
233 }
234 this.readyState = Transport.CLOSED;
235 this.connection.emit('end');
236 this.connection.emit('close');
237 this.connection = null;
238 if (this.session_id) {
239 delete MAP[this.session_id];
240 return this.session_id = null;
241 }
242 };
243
244 Session.prototype.didMessage = function(payload) {
245 if (this.readyState === Transport.OPEN) {
246 this.connection.emit('data', payload);
247 }
248 };
249
250 Session.prototype.send = function(payload) {
251 if (this.readyState !== Transport.OPEN) {
252 return false;
253 }
254 this.send_buffer.push('' + payload);
255 if (this.recv) {
256 this.tryFlush();
257 }
258 return true;
259 };
260
261 Session.prototype.close = function(status, reason) {
262 if (status == null) {
263 status = 1000;
264 }
265 if (reason == null) {
266 reason = "Normal closure";
267 }
268 if (this.readyState !== Transport.OPEN) {
269 return false;
270 }
271 this.readyState = Transport.CLOSING;
272 this.close_frame = closeFrame(status, reason);
273 if (this.recv) {
274 this.recv.doSendFrame(this.close_frame);
275 if (this.recv) {
276 this.recv.didClose();
277 }
278 if (this.recv) {
279 this.unregister();
280 }
281 }
282 return true;
283 };
284
285 return Session;
286
287 })();
288
289 Session.bySessionId = function(session_id) {
290 if (!session_id) {
291 return null;
292 }
293 return MAP[session_id] || null;
294 };
295
296 register = function(req, server, session_id, receiver) {
297 var session;
298 session = Session.bySessionId(session_id);
299 if (!session) {
300 session = new Session(session_id, server);
301 }
302 session.register(req, receiver);
303 return session;
304 };
305
306 exports.register = function(req, server, receiver) {
307 return register(req, server, req.session, receiver);
308 };
309
310 exports.registerNoSession = function(req, server, receiver) {
311 return register(req, server, void 0, receiver);
312 };
313
314 GenericReceiver = (function() {
315 function GenericReceiver(thingy) {
316 this.thingy = thingy;
317 this.setUp(this.thingy);
318 }
319
320 GenericReceiver.prototype.setUp = function() {
321 this.thingy_end_cb = (function(_this) {
322 return function() {
323 return _this.didAbort();
324 };
325 })(this);
326 this.thingy.addListener('close', this.thingy_end_cb);
327 return this.thingy.addListener('end', this.thingy_end_cb);
328 };
329
330 GenericReceiver.prototype.tearDown = function() {
331 this.thingy.removeListener('close', this.thingy_end_cb);
332 this.thingy.removeListener('end', this.thingy_end_cb);
333 return this.thingy_end_cb = null;
334 };
335
336 GenericReceiver.prototype.didAbort = function() {
337 this.delay_disconnect = false;
338 return this.didClose();
339 };
340
341 GenericReceiver.prototype.didClose = function() {
342 if (this.thingy) {
343 this.tearDown(this.thingy);
344 this.thingy = null;
345 }
346 if (this.session) {
347 return this.session.unregister();
348 }
349 };
350
351 GenericReceiver.prototype.doSendBulk = function(messages) {
352 var m, q_msgs;
353 q_msgs = (function() {
354 var i, len, results;
355 results = [];
356 for (i = 0, len = messages.length; i < len; i++) {
357 m = messages[i];
358 results.push(utils.quote(m));
359 }
360 return results;
361 })();
362 return this.doSendFrame('a' + '[' + q_msgs.join(',') + ']');
363 };
364
365 GenericReceiver.prototype.heartbeat = function() {
366 return this.doSendFrame('h');
367 };
368
369 return GenericReceiver;
370
371 })();
372
373 ResponseReceiver = (function(superClass) {
374 extend(ResponseReceiver, superClass);
375
376 ResponseReceiver.prototype.max_response_size = void 0;
377
378 ResponseReceiver.prototype.delay_disconnect = true;
379
380 function ResponseReceiver(request, response, options) {
381 var x;
382 this.request = request;
383 this.response = response;
384 this.options = options;
385 this.curr_response_size = 0;
386 try {
387 this.request.connection.setKeepAlive(true, 5000);
388 } catch (error) {
389 x = error;
390 }
391 ResponseReceiver.__super__.constructor.call(this, this.request.connection);
392 if (this.max_response_size === void 0) {
393 this.max_response_size = this.options.response_limit;
394 }
395 }
396
397 ResponseReceiver.prototype.doSendFrame = function(payload) {
398 var r, x;
399 this.curr_response_size += payload.length;
400 r = false;
401 try {
402 this.response.write(payload);
403 r = true;
404 } catch (error) {
405 x = error;
406 }
407 if (this.max_response_size && this.curr_response_size >= this.max_response_size) {
408 this.didClose();
409 }
410 return r;
411 };
412
413 ResponseReceiver.prototype.didClose = function() {
414 var x;
415 ResponseReceiver.__super__.didClose.apply(this, arguments);
416 try {
417 this.response.end();
418 } catch (error) {
419 x = error;
420 }
421 return this.response = null;
422 };
423
424 return ResponseReceiver;
425
426 })(GenericReceiver);
427
428 exports.GenericReceiver = GenericReceiver;
429
430 exports.Transport = Transport;
431
432 exports.Session = Session;
433
434 exports.ResponseReceiver = ResponseReceiver;
435
436 exports.SockJSConnection = SockJSConnection;
437
438}).call(this);
Note: See TracBrowser for help on using the repository browser.