trans-htmlfile.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var HtmlFileReceiver, iframe_template, 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. iframe_template = "<!doctype html>\n<html><head>\n <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n</head><body><h2>Don't panic!</h2>\n <script>\n document.domain = document.domain;\n var c = parent.{{ callback }};\n c.start();\n function p(d) {c.message(d);};\n window.onload = function() {c.stop();};\n </script>";
  9. iframe_template += Array(1024 - iframe_template.length + 14).join(' ');
  10. iframe_template += '\r\n\r\n';
  11. HtmlFileReceiver = (function(superClass) {
  12. extend(HtmlFileReceiver, superClass);
  13. function HtmlFileReceiver() {
  14. return HtmlFileReceiver.__super__.constructor.apply(this, arguments);
  15. }
  16. HtmlFileReceiver.prototype.protocol = "htmlfile";
  17. HtmlFileReceiver.prototype.doSendFrame = function(payload) {
  18. return HtmlFileReceiver.__super__.doSendFrame.call(this, '<script>\np(' + JSON.stringify(payload) + ');\n</script>\r\n');
  19. };
  20. return HtmlFileReceiver;
  21. })(transport.ResponseReceiver);
  22. exports.app = {
  23. htmlfile: function(req, res) {
  24. var callback;
  25. if (!('c' in req.query || 'callback' in req.query)) {
  26. throw {
  27. status: 500,
  28. message: '"callback" parameter required'
  29. };
  30. }
  31. callback = 'c' in req.query ? req.query['c'] : req.query['callback'];
  32. if (/[^a-zA-Z0-9-_.]/.test(callback)) {
  33. throw {
  34. status: 500,
  35. message: 'invalid "callback" parameter'
  36. };
  37. }
  38. res.setHeader('Content-Type', 'text/html; charset=UTF-8');
  39. res.writeHead(200);
  40. res.write(iframe_template.replace(/{{ callback }}/g, callback));
  41. transport.register(req, this, new HtmlFileReceiver(req, res, this.options));
  42. return true;
  43. }
  44. };
  45. }).call(this);