<commit_before>/*jshint node:true */

// JSONP middleware


var url = require('url');
var JSON3 = require('json3');


var getCallbackName = function(params) {
  return params.callback || params.jsonp;
};

var isJsonP = function(params) {
  return !!getCallbackName(params);
};

var escapeClosingTags = function(str) {
  // http://stackoverflow.com/a/9249932/358804
  return str.replace(/<\//g, '<\\/');
};

var wrapInCallback = function(callbackName, body) {
  return callbackName + '(' + body + ');';
};

var transformJsonPBody = function(params, body) {
  var raw = !!params.raw; // undocumented, for now
  if (raw){
    // escape and pass via JSON
    body = JSON3.stringify({data: body});
  }

  body = escapeClosingTags(body);

  var callbackName = getCallbackName(params);
  body = wrapInCallback(callbackName, body);

  return body;
};


module.exports = function(req, res, next) {
  /*
    Monkey-patch the response. It would be great to make this less of a hack, though it seems to be how other modules are doing it (as of 12/6/14):
    * https://github.com/nemtsov/express-partial-response/blob/cf9a426/index.js#L30-L31
    * https://github.com/expressjs/compression/blob/c45fae3/index.js#L85-L133
    * https://github.com/jshttp/on-headers/blob/cc3688b/index.js#L30
    * http://stackoverflow.com/a/15103881/358804
  */
  var originalSend = res.send;
  res.send = function(body) {
    var params = url.parse(req.url, true).query;

    if (isJsonP(params)){
      res.set('content-type', 'text/javascript'); // use instead of 'application/javascript' for IE < 8 compatibility
      body = transformJsonPBody(params, body);
    }

    originalSend.call(this, body);
  };

  next();
};<commit_msg>have jsonp middleware check for raw responses based on response MIME type<commit_after>/*jshint node:true */

// JSONP middleware


var url = require('url');
var JSON3 = require('json3');


var isTextResponse = function(res) {
  // not the most accurate way to check, but good enough
  return /text\//.test(res.get('content-type'));
};

var getCallbackName = function(params) {
  return params.callback || params.jsonp;
};

var isJsonP = function(params) {
  return !!getCallbackName(params);
};

var escapeClosingTags = function(str) {
  // http://stackoverflow.com/a/9249932/358804
  return str.replace(/<\//g, '<\\/');
};

var wrapInCallback = function(callbackName, body) {
  return callbackName + '(' + body + ');';
};

var transformJsonPBody = function(params, body, res) {
  if (isTextResponse(res)) {
    // escape and pass via JSON
    body = JSON3.stringify({data: body});
  }

  body = escapeClosingTags(body);

  var callbackName = getCallbackName(params);
  body = wrapInCallback(callbackName, body);

  return body;
};


module.exports = function(req, res, next) {
  /*
    Monkey-patch the response. It would be great to make this less of a hack, though it seems to be how other modules are doing it (as of 12/6/14):
    * https://github.com/nemtsov/express-partial-response/blob/cf9a426/index.js#L30-L31
    * https://github.com/expressjs/compression/blob/c45fae3/index.js#L85-L133
    * https://github.com/jshttp/on-headers/blob/cc3688b/index.js#L30
    * http://stackoverflow.com/a/15103881/358804
  */
  var originalSend = res.send;
  res.send = function(body) {
    var params = url.parse(req.url, true).query;

    if (isJsonP(params)){
      body = transformJsonPBody(params, body, res);
      res.set('content-type', 'text/javascript'); // use instead of 'application/javascript' for IE < 8 compatibility
    }

    originalSend.call(this, body);
  };

  next();
};
{'fraction_non_alphanumeric': 0.15001391594767605, 'fraction_numerical': 0.03228499860840523, 'mean_word_length': 3.6017925736235594, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7166790', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1180, 'n_words': 276}