polling-jsonp.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const Polling = require("./polling");
  2. const qs = require("querystring");
  3. const rDoubleSlashes = /\\\\n/g;
  4. const rSlashes = /(\\)?\\n/g;
  5. class JSONP extends Polling {
  6. /**
  7. * JSON-P polling transport.
  8. *
  9. * @api public
  10. */
  11. constructor(req) {
  12. super(req);
  13. this.head = "___eio[" + (req._query.j || "").replace(/[^0-9]/g, "") + "](";
  14. this.foot = ");";
  15. }
  16. /**
  17. * Handles incoming data.
  18. * Due to a bug in \n handling by browsers, we expect a escaped string.
  19. *
  20. * @api private
  21. */
  22. onData(data) {
  23. // we leverage the qs module so that we get built-in DoS protection
  24. // and the fast alternative to decodeURIComponent
  25. data = qs.parse(data).d;
  26. if ("string" === typeof data) {
  27. // client will send already escaped newlines as \\\\n and newlines as \\n
  28. // \\n must be replaced with \n and \\\\n with \\n
  29. data = data.replace(rSlashes, function(match, slashes) {
  30. return slashes ? match : "\n";
  31. });
  32. super.onData(data.replace(rDoubleSlashes, "\\n"));
  33. }
  34. }
  35. /**
  36. * Performs the write.
  37. *
  38. * @api private
  39. */
  40. doWrite(data, options, callback) {
  41. // we must output valid javascript, not valid json
  42. // see: http://timelessrepo.com/json-isnt-a-javascript-subset
  43. const js = JSON.stringify(data)
  44. .replace(/\u2028/g, "\\u2028")
  45. .replace(/\u2029/g, "\\u2029");
  46. // prepare response
  47. data = this.head + js + this.foot;
  48. super.doWrite(data, options, callback);
  49. }
  50. }
  51. module.exports = JSONP;