<commit_before>/*!
 * Ext JS Connect
 * Copyright(c) 2010 Ext JS, Inc.
 * MIT Licensed
 */

// This provider is the core functionality of node-router put in a middleware

var Buffer = require('buffer').Buffer;

var routes = [];

function addRoute(method) {
    return function (pattern, handler, format) {
        if (typeof pattern === 'string') {
            pattern = new RegExp("^" + pattern + "$");
        }
        var route = {
            method: method,
            pattern: pattern,
            handler: handler
        };
        if (format !== undefined) {
            route.format = format;
        }
        routes.push(route);
    };
}

function handleRoute(req, res, route, match) {

    match = [req, res].concat(match.slice(1));

    function dispatch() {
        var result = route.handler.apply(null, match);
        if (result) {
            res.simpleBody(200, result, "text/html");
        }
    }
    if (route.format) {
        var body = "";
        req.setEncoding('utf8');
        req.addListener('data', function (chunk) {
            body += chunk;
        });
        req.addListener('end', function () {
            if (route.format === 'json') {
                body = JSON.parse(body);
            }
            match.push(body);
            dispatch();
        });
        return;
    }
    dispatch();

}

module.exports = {
    setup: function (env, setup) {
        setup({
            get: addRoute("GET"),
            put: addRoute("PUT"),
            del: addRoute("DELETE"),
            post: addRoute("POST")
        });
    },

    handle: function (req, res, next) {
        var origReq = req,
            origRes = res;
        req = Object.create(req);
        res = Object.create(res);

        res.notFound = function (message) {
            next(origReq, origRes);
        };

        for (var i = 0, l = routes.length; i < l; i++) {
            var route = routes[i];
            if (req.method === route.method) {
                var match = req.url.match(route.pattern);<commit_msg>Added deprecation warning to router middleware<commit_after>/*!
 * Ext JS Connect
 * Copyright(c) 2010 Ext JS, Inc.
 * MIT Licensed
 */

// This provider is the core functionality of node-router put in a middleware

var sys = require('sys'),
    Buffer = require('buffer').Buffer;

var routes = [];

function addRoute(method) {
    return function (pattern, handler, format) {
        if (typeof pattern === 'string') {
            pattern = new RegExp("^" + pattern + "$");
        }
        var route = {
            method: method,
            pattern: pattern,
            handler: handler
        };
        if (format !== undefined) {
            route.format = format;
        }
        routes.push(route);
    };
}

function handleRoute(req, res, route, match) {

    match = [req, res].concat(match.slice(1));

    function dispatch() {
        var result = route.handler.apply(null, match);
        if (result) {
            res.simpleBody(200, result, "text/html");
        }
    }
    if (route.format) {
        var body = "";
        req.setEncoding('utf8');
        req.addListener('data', function (chunk) {
            body += chunk;
        });
        req.addListener('end', function () {
            if (route.format === 'json') {
                body = JSON.parse(body);
            }
            match.push(body);
            dispatch();
        });
        return;
    }
    dispatch();

}

module.exports = {
    setup: function (env, setup) {
        sys.error('Warning: the "router" middleware is deprecated. Use "rest", `$ man connect-rest` for documentation');
        setup({
            get: addRoute("GET"),
            put: addRoute("PUT"),
            del: addRoute("DELETE"),
            post: addRoute("POST")
        });
    },

    handle: function (req, res, next) {
        var origReq = req,
            origRes = res;
        req = Object.create(req);
        res = Object.create(res);

        res.notFound = function (message) {
            next(origReq, origRes);
        };

        for (var i = 0, l = routes.length; i < l; i++) {
            var route = routes[i];
            if (req.method === route.method) {
                var match = req.url.match(route.pattern);
{'fraction_non_alphanumeric': 0.14275634995296332, 'fraction_numerical': 0.004703668861712135, 'mean_word_length': 1.7037507946598855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7126407', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1184, 'n_words': 314}