context.js 930 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const jsonpBody = require('jsonp-body');
  3. const { JSONP_CONFIG } = require('../../lib/private_key');
  4. module.exports = {
  5. /**
  6. * detect if response should be jsonp
  7. */
  8. get acceptJSONP() {
  9. return !!(this[JSONP_CONFIG] && this[JSONP_CONFIG].jsonpFunction);
  10. },
  11. /**
  12. * JSONP wrap body function
  13. * Set jsonp response wrap function, other plugin can use it.
  14. * If not necessary, please don't use this method in your application code.
  15. * @param {Object} body respones body
  16. * @private
  17. */
  18. createJsonpBody(body) {
  19. const jsonpConfig = this[JSONP_CONFIG];
  20. if (!jsonpConfig || !jsonpConfig.jsonpFunction) {
  21. this.body = body;
  22. return;
  23. }
  24. this.set('x-content-type-options', 'nosniff');
  25. this.type = 'js';
  26. body = body === undefined ? null : body;
  27. // protect from jsonp xss
  28. this.body = jsonpBody(body, jsonpConfig.jsonpFunction, jsonpConfig.options);
  29. },
  30. };