<commit_before>var httpProxy      = require('http-proxy');
var handlers       = require('./lib/handlers');
var contextMatcher = require('./lib/context-matcher');
var PathRewriter   = require('./lib/path-rewriter');

var httpProxyMiddleware = function (context, opts) {
    var isWsUpgradeListened = false;
    var proxyOptions = opts || {};
    var pathRewriter;

    // Legacy option.proxyHost
    // set options.headers.host when option.proxyHost is provided
    if (proxyOptions.proxyHost) {
        console.log('*************************************');
        console.log('[HPM] Deprecated "option.proxyHost"');
        console.log('      Use "option.changeOrigin" or "option.headers.host" instead');
        console.log('      "option.proxyHost" will be removed in future release.');
        console.log('*************************************');

        proxyOptions.headers = proxyOptions.headers || {};
        proxyOptions.headers.host = proxyOptions.proxyHost;
    }

    // create proxy
    var proxy = httpProxy.createProxyServer(proxyOptions);
    console.log('[HPM] Proxy created:', context, ' -> ', proxyOptions.target);

    pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided

    // handle option.pathRewrite
    if (pathRewriter) {
        proxy.on('proxyReq', function (proxyReq, req, res, options) {
            proxyReq.path = pathRewriter(proxyReq.path);
        });
    }

    // handle error and close connection properly<commit_msg>added handler for proxyRes option, it needs to be a function<commit_after>var httpProxy      = require('http-proxy');
var handlers       = require('./lib/handlers');
var contextMatcher = require('./lib/context-matcher');
var PathRewriter   = require('./lib/path-rewriter');

var httpProxyMiddleware = function (context, opts) {
    var isWsUpgradeListened = false;
    var proxyOptions = opts || {};
    var pathRewriter;

    // Legacy option.proxyHost
    // set options.headers.host when option.proxyHost is provided
    if (proxyOptions.proxyHost) {
        console.log('*************************************');
        console.log('[HPM] Deprecated "option.proxyHost"');
        console.log('      Use "option.changeOrigin" or "option.headers.host" instead');
        console.log('      "option.proxyHost" will be removed in future release.');
        console.log('*************************************');

        proxyOptions.headers = proxyOptions.headers || {};
        proxyOptions.headers.host = proxyOptions.proxyHost;
    }

    // create proxy
    var proxy = httpProxy.createProxyServer(proxyOptions);
    console.log('[HPM] Proxy created:', context, ' -> ', proxyOptions.target);

    pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided

    // handle option.pathRewrite
    if (pathRewriter) {
        proxy.on('proxyReq', function (proxyReq, req, res, options) {
            proxyReq.path = pathRewriter(proxyReq.path);
        });
    }

    // handle option.proxyRes for success callback
    if (proxyOptions.proxyRes && typeof proxyOptions.proxyRes === 'function') {
        proxy.on('proxyRes', proxyOptions.proxyRes);
    }

    // handle error and close connection properly
{'fraction_non_alphanumeric': 0.16697080291970803, 'fraction_numerical': 0.0, 'mean_word_length': 3.293733681462141, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '543697', 'n_tokens_mistral': 900, 'n_tokens_neox': 852, 'n_words': 211}