<commit_before>var path    = require('path')
var fs      = require('fs')
var http    = require('http')
var __slice = [].slice

var cadence = require('cadence')
var connect = require('connect')

function httpStatusMessage(statusCode) {
    return statusCode + ' ' + http.STATUS_CODES[statusCode] + '\n'
}

exports.createServer = function (port, directory, probe, callback) {
    var server = http.createServer()

    var routes = exports.routes(directory)
    server.on('request', function (request, response) {
        routes(request, response, function (error, matched) {
            if (error) server.emit('error', error)
            else if (!matched) {
                response.statusCode = 404
                response.setHeader('content-type', 'text/plain; charset=utf8')
                response.end(httpStatusMessage(404), 'utf8')
            }
        })
    })

    server.on('error', nextPort)

    function nextPort (e) {
        if (!probe || e.code != 'EADDRINUSE') callback(e)
        else server.listen(++port, '127.0.0.1')
    }

    server.on('listening', function () {
        server.removeListener('error', nextPort)
        callback(null, server)
    })

    server.listen(port, '127.0.0.1')
}

exports.argvParser = function (cwd, file, args) {
    args = args.slice()
    var url = require('url')
    var object = { method: 'get' }
    var parsed, $

    if ($ = /^(.*?)\/\/(.*)$/.exec(file)) {
        object.directory = path.resolve(cwd, $[1])
        file = $[2]
    } else {
        object.directory = path.resolve(cwd, path.dirname(file))
        file = path.basename(file)
    }

    if (args[0] && /^[a-z]{3,}$/i.test(args[0])) {
        object.method = args.shift().toLowerCase()
    }

    // todo: no. only invoke as intended. do not specify alternate path.
    if (/^\s*\//.test(args[0]) || /^[^=:]+:/.test(args[0])) {
        parsed = url.parse(args.shift(), true)
    } else {
        parsed = url.parse(file, true)
    }

    args.forEach(function (pair) {
        var $ = /^([^=]*)(?:=(.*))$/.exec(pair)
        parsed.query[$[1]] = $[2]
    })

    delete parsed.search

    object.url = url.parse(url.format(parsed), true)

    return object
}

function Register (file) {
    this._file = file
    this._handlers = {}
}

'any get post put delete patch'.split(/\s/).forEach(function (verb) {
    Register.prototype[verb] = function (handler) {
        this._handlers[verb] = handler
        return this
    }
})

function parameterize (program, context) {
    var $ = /^function\s*[^(]*\(([^)]*)\)/.exec(program.toString())
    return $[1].trim().split(/\s*,\s*/).map(function (parameter) {
        return context[parameter]
    })
}

var compiled = []

function raise (statusCode, headers) {
    var error = new Error
    error.statusCode = statusCode
    if (headers) {
        error.headers = headers
    }
    throw error
}

exports.routes = function routes (base) {
    var find = require('avenue')
    var path = require('path')

    var url = require('url')
    var routes = find(base, 'cgi.js')

    routes.forEach(function (route) {
        var file = path.join(base, route.script)
        if (!compiled[file]) {
            try {
                global.on = compiled[file] = new Register(file)
                require(file)
            } finally {
                delete global.on<commit_msg>Compiled scripts are now stored in an object.<commit_after>var path    = require('path')
var fs      = require('fs')
var http    = require('http')
var __slice = [].slice

var cadence = require('cadence')
var connect = require('connect')

function httpStatusMessage(statusCode) {
    return statusCode + ' ' + http.STATUS_CODES[statusCode] + '\n'
}

exports.createServer = function (port, directory, probe, callback) {
    var server = http.createServer()

    var routes = exports.routes(directory)
    server.on('request', function (request, response) {
        routes(request, response, function (error, matched) {
            if (error) server.emit('error', error)
            else if (!matched) {
                response.statusCode = 404
                response.setHeader('content-type', 'text/plain; charset=utf8')
                response.end(httpStatusMessage(404), 'utf8')
            }
        })
    })

    server.on('error', nextPort)

    function nextPort (e) {
        if (!probe || e.code != 'EADDRINUSE') callback(e)
        else server.listen(++port, '127.0.0.1')
    }

    server.on('listening', function () {
        server.removeListener('error', nextPort)
        callback(null, server)
    })

    server.listen(port, '127.0.0.1')
}

exports.argvParser = function (cwd, file, args) {
    args = args.slice()
    var url = require('url')
    var object = { method: 'get' }
    var parsed, $

    if ($ = /^(.*?)\/\/(.*)$/.exec(file)) {
        object.directory = path.resolve(cwd, $[1])
        file = $[2]
    } else {
        object.directory = path.resolve(cwd, path.dirname(file))
        file = path.basename(file)
    }

    if (args[0] && /^[a-z]{3,}$/i.test(args[0])) {
        object.method = args.shift().toLowerCase()
    }

    // todo: no. only invoke as intended. do not specify alternate path.
    if (/^\s*\//.test(args[0]) || /^[^=:]+:/.test(args[0])) {
        parsed = url.parse(args.shift(), true)
    } else {
        parsed = url.parse(file, true)
    }

    args.forEach(function (pair) {
        var $ = /^([^=]*)(?:=(.*))$/.exec(pair)
        parsed.query[$[1]] = $[2]
    })

    delete parsed.search

    object.url = url.parse(url.format(parsed), true)

    return object
}

function Register (file) {
    this._file = file
    this._handlers = {}
}

'any get post put delete patch'.split(/\s/).forEach(function (verb) {
    Register.prototype[verb] = function (handler) {
        this._handlers[verb] = handler
        return this
    }
})

function parameterize (program, context) {
    var $ = /^function\s*[^(]*\(([^)]*)\)/.exec(program.toString())
    return $[1].trim().split(/\s*,\s*/).map(function (parameter) {
        return context[parameter]
    })
}

var compiled = {}

function raise (statusCode, headers) {
    var error = new Error
    error.statusCode = statusCode
    if (headers) {
        error.headers = headers
    }
    throw error
}

exports.routes = function routes (base) {
    var find = require('avenue')
    var path = require('path')

    var url = require('url')
    var routes = find(base, 'cgi.js')

    routes.forEach(function (route) {
        var file = path.join(base, route.script)
        if (!compiled[file]) {
            try {
                global.on = compiled[file] = new Register(file)
                require(file)
            } finally {
                delete global.on
{'fraction_non_alphanumeric': 0.1688021454112038, 'fraction_numerical': 0.008939213349225268, 'mean_word_length': 2.375062845651081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '1814581', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2098, 'n_words': 468}