trans-xhr.js 3.7 KB

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