trans-jsonp.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var JsonpReceiver, transport,
  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. JsonpReceiver = (function(superClass) {
  8. extend(JsonpReceiver, superClass);
  9. JsonpReceiver.prototype.protocol = "jsonp-polling";
  10. JsonpReceiver.prototype.max_response_size = 1;
  11. function JsonpReceiver(req, res, options, callback1) {
  12. this.callback = callback1;
  13. JsonpReceiver.__super__.constructor.call(this, req, res, options);
  14. }
  15. JsonpReceiver.prototype.doSendFrame = function(payload) {
  16. return JsonpReceiver.__super__.doSendFrame.call(this, "/**/" + this.callback + "(" + JSON.stringify(payload) + ");\r\n");
  17. };
  18. return JsonpReceiver;
  19. })(transport.ResponseReceiver);
  20. exports.app = {
  21. jsonp: function(req, res, _, next_filter) {
  22. var callback;
  23. if (!('c' in req.query || 'callback' in req.query)) {
  24. throw {
  25. status: 500,
  26. message: '"callback" parameter required'
  27. };
  28. }
  29. callback = 'c' in req.query ? req.query['c'] : req.query['callback'];
  30. if (/[^a-zA-Z0-9-_.]/.test(callback) || callback.length > 32) {
  31. throw {
  32. status: 500,
  33. message: 'invalid "callback" parameter'
  34. };
  35. }
  36. res.setHeader('X-Content-Type-Options', 'nosniff');
  37. res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
  38. res.writeHead(200);
  39. transport.register(req, this, new JsonpReceiver(req, res, this.options, callback));
  40. return true;
  41. },
  42. jsonp_send: function(req, res, query) {
  43. var d, i, jsonp, len, message, x;
  44. if (!query) {
  45. throw {
  46. status: 500,
  47. message: 'Payload expected.'
  48. };
  49. }
  50. if (typeof query === 'string') {
  51. try {
  52. d = JSON.parse(query);
  53. } catch (error) {
  54. x = error;
  55. throw {
  56. status: 500,
  57. message: 'Broken JSON encoding.'
  58. };
  59. }
  60. } else {
  61. d = query.d;
  62. }
  63. if (typeof d === 'string' && d) {
  64. try {
  65. d = JSON.parse(d);
  66. } catch (error) {
  67. x = error;
  68. throw {
  69. status: 500,
  70. message: 'Broken JSON encoding.'
  71. };
  72. }
  73. }
  74. if (!d || d.__proto__.constructor !== Array) {
  75. throw {
  76. status: 500,
  77. message: 'Payload expected.'
  78. };
  79. }
  80. jsonp = transport.Session.bySessionId(req.session);
  81. if (jsonp === null) {
  82. throw {
  83. status: 404
  84. };
  85. }
  86. for (i = 0, len = d.length; i < len; i++) {
  87. message = d[i];
  88. jsonp.didMessage(message);
  89. }
  90. res.setHeader('Content-Length', '2');
  91. res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
  92. res.writeHead(200);
  93. res.end('ok');
  94. return true;
  95. }
  96. };
  97. }).call(this);