trans-eventsource.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var EventSourceReceiver, 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. utils = require('./utils');
  7. transport = require('./transport');
  8. EventSourceReceiver = (function(superClass) {
  9. extend(EventSourceReceiver, superClass);
  10. function EventSourceReceiver() {
  11. return EventSourceReceiver.__super__.constructor.apply(this, arguments);
  12. }
  13. EventSourceReceiver.prototype.protocol = "eventsource";
  14. EventSourceReceiver.prototype.doSendFrame = function(payload) {
  15. var data;
  16. data = ['data: ', utils.escape_selected(payload, '\r\n\x00'), '\r\n\r\n'];
  17. return EventSourceReceiver.__super__.doSendFrame.call(this, data.join(''));
  18. };
  19. return EventSourceReceiver;
  20. })(transport.ResponseReceiver);
  21. exports.app = {
  22. eventsource: function(req, res) {
  23. var headers, origin;
  24. if (!req.headers['origin'] || req.headers['origin'] === 'null') {
  25. origin = '*';
  26. } else {
  27. origin = req.headers['origin'];
  28. res.setHeader('Access-Control-Allow-Credentials', 'true');
  29. }
  30. res.setHeader('Content-Type', 'text/event-stream');
  31. res.setHeader('Access-Control-Allow-Origin', origin);
  32. res.setHeader('Vary', 'Origin');
  33. headers = req.headers['access-control-request-headers'];
  34. if (headers) {
  35. res.setHeader('Access-Control-Allow-Headers', headers);
  36. }
  37. res.writeHead(200);
  38. res.write('\r\n');
  39. transport.register(req, this, new EventSourceReceiver(req, res, this.options));
  40. return true;
  41. }
  42. };
  43. }).call(this);