source: trip-planner-front/node_modules/sockjs/lib/trans-xhr.js@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1// Generated by CoffeeScript 1.12.7
2(function() {
3 var XhrPollingReceiver, XhrStreamingReceiver, transport, utils,
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 transport = require('./transport');
8
9 utils = require('./utils');
10
11 XhrStreamingReceiver = (function(superClass) {
12 extend(XhrStreamingReceiver, superClass);
13
14 function XhrStreamingReceiver() {
15 return XhrStreamingReceiver.__super__.constructor.apply(this, arguments);
16 }
17
18 XhrStreamingReceiver.prototype.protocol = "xhr-streaming";
19
20 XhrStreamingReceiver.prototype.doSendFrame = function(payload) {
21 return XhrStreamingReceiver.__super__.doSendFrame.call(this, payload + '\n');
22 };
23
24 return XhrStreamingReceiver;
25
26 })(transport.ResponseReceiver);
27
28 XhrPollingReceiver = (function(superClass) {
29 extend(XhrPollingReceiver, superClass);
30
31 function XhrPollingReceiver() {
32 return XhrPollingReceiver.__super__.constructor.apply(this, arguments);
33 }
34
35 XhrPollingReceiver.prototype.protocol = "xhr-polling";
36
37 XhrPollingReceiver.prototype.max_response_size = 1;
38
39 return XhrPollingReceiver;
40
41 })(XhrStreamingReceiver);
42
43 exports.app = {
44 xhr_options: function(req, res) {
45 res.statusCode = 204;
46 res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, POST');
47 res.setHeader('Access-Control-Max-Age', res.cache_for);
48 return '';
49 },
50 xhr_send: function(req, res, data) {
51 var d, i, jsonp, len, message, x;
52 if (!data) {
53 throw {
54 status: 500,
55 message: 'Payload expected.'
56 };
57 }
58 try {
59 d = JSON.parse(data);
60 } catch (error) {
61 x = error;
62 throw {
63 status: 500,
64 message: 'Broken JSON encoding.'
65 };
66 }
67 if (!d || d.__proto__.constructor !== Array) {
68 throw {
69 status: 500,
70 message: 'Payload expected.'
71 };
72 }
73 jsonp = transport.Session.bySessionId(req.session);
74 if (!jsonp) {
75 throw {
76 status: 404
77 };
78 }
79 for (i = 0, len = d.length; i < len; i++) {
80 message = d[i];
81 jsonp.didMessage(message);
82 }
83 res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
84 res.writeHead(204);
85 res.end();
86 return true;
87 },
88 xhr_cors: function(req, res, content) {
89 var headers, origin;
90 if (this.options.disable_cors) {
91 return content;
92 }
93 if (!req.headers['origin']) {
94 origin = '*';
95 } else {
96 origin = req.headers['origin'];
97 res.setHeader('Access-Control-Allow-Credentials', 'true');
98 }
99 res.setHeader('Access-Control-Allow-Origin', origin);
100 res.setHeader('Vary', 'Origin');
101 headers = req.headers['access-control-request-headers'];
102 if (headers) {
103 res.setHeader('Access-Control-Allow-Headers', headers);
104 }
105 return content;
106 },
107 xhr_poll: function(req, res, _, next_filter) {
108 res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
109 res.writeHead(200);
110 transport.register(req, this, new XhrPollingReceiver(req, res, this.options));
111 return true;
112 },
113 xhr_streaming: function(req, res, _, next_filter) {
114 res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
115 res.writeHead(200);
116 res.write(Array(2049).join('h') + '\n');
117 transport.register(req, this, new XhrStreamingReceiver(req, res, this.options));
118 return true;
119 }
120 };
121
122}).call(this);
Note: See TracBrowser for help on using the repository browser.