<commit_before>var bunyan = require('bunyan'),
    restify = require('restify'),
    cors = require('./cors.js'),
    placeholderManager = require('./placeholder-manager.js'),
    _ = require('lodash'),
    server, log;

exports.start = function (config) {
    var port = config.port,
        stubRegistry = require('./stub-registry.js').create(config.log);

    log = config.log.child({ component: "vanilli-server" });

    if (!port) {
        throw new Error("Port must be specified.");
    }

    if (!stubRegistry) {
        throw new Error("Stub Registry must be specified.");
    }

    server = restify.createServer();
    server.use(restify.bodyParser({ mapParams: false }));
    server.use(cors.configure({ allowedHeadersForCors: config.allowedHeadersForCors }));
    server.use(restify.queryParser());
    server.use(restify.jsonp());

    server.listen(port, function () {
        function respondTo(request, response, next) {
            function completeResponseWith(stub) {
                var contentFormatterExists = server.formatters[contentType],
                    body = placeholderManager.substitute(request, stub.respondWith.body);

                if (body && !contentFormatterExists) {
                    response.writeHead(stub.respondWith.status, {
                        'Content-Length': Buffer.byteLength(body),
                        'Content-Type': contentType
                    });
                    response.write(body);
                    response.end();
                } else {
                    response.send(stub.respondWith.status, body);
                }
            }

            var stub = stubRegistry.findMatchFor(request);

            if (stub) {
                var contentType = response.header('content-type') || stub.respondWith.contentType;

                response.header('Content-Type', contentType);

                if (stub.respondWith.headers) {
                    _.keys(stub.respondWith.headers).forEach(function (header) {
                        response.header(header, stub.respondWith.headers[header]);
                    });
                }

                if (stub.respondWith.wait) {
                    setTimeout(function () {
                        completeResponseWith(stub);
                    }, stub.respondWith.wait);
                } else {
                    completeResponseWith(stub);
                }

            } else {
                log.error("UNEXPECTED request: " + request.method + " " + request.url);

                response.send(404, { vanilli: "Stub not found." });
            }

            next();
        }

        function addToRegistry(request) {
            if (_.isArray(request.body)) {
                return request.body.map(function (stub) {
                    return stubRegistry.addStub(stub).id;<commit_msg>Log internal errors.<commit_after>var bunyan = require('bunyan'),
    restify = require('restify'),
    cors = require('./cors.js'),
    placeholderManager = require('./placeholder-manager.js'),
    _ = require('lodash'),
    server, log;

exports.start = function (config) {
    var port = config.port,
        stubRegistry = require('./stub-registry.js').create(config.log);

    log = config.log.child({ component: "vanilli-server" });

    if (!port) {
        throw new Error("Port must be specified.");
    }

    if (!stubRegistry) {
        throw new Error("Stub Registry must be specified.");
    }

    server = restify.createServer();
    server.use(restify.bodyParser({ mapParams: false }));
    server.use(cors.configure({ allowedHeadersForCors: config.allowedHeadersForCors }));
    server.use(restify.queryParser());
    server.use(restify.jsonp());

    server.listen(port, function () {
        function respondTo(request, response, next) {
            function completeResponseWith(stub) {
                var contentFormatterExists = server.formatters[contentType],
                    body = placeholderManager.substitute(request, stub.respondWith.body);

                if (body && !contentFormatterExists) {
                    response.writeHead(stub.respondWith.status, {
                        'Content-Length': Buffer.byteLength(body),
                        'Content-Type': contentType
                    });
                    response.write(body);
                    response.end();
                } else {
                    response.send(stub.respondWith.status, body);
                }
            }

            try {
                var stub = stubRegistry.findMatchFor(request);

                if (stub) {
                    var contentType = response.header('content-type') || stub.respondWith.contentType;

                    response.header('Content-Type', contentType);

                    if (stub.respondWith.headers) {
                        _.keys(stub.respondWith.headers).forEach(function (header) {
                            response.header(header, stub.respondWith.headers[header]);
                        });
                    }

                    if (stub.respondWith.wait) {
                        setTimeout(function () {
                            completeResponseWith(stub);
                        }, stub.respondWith.wait);
                    } else {
                        completeResponseWith(stub);
                    }

                } else {
                    log.error("UNEXPECTED request: " + request.method + " " + request.url);

                    response.send(404, { vanilli: "Stub not found." });
                }
            } catch (e) {
                log.error(e);
            }

            next();
        }

        function addToRegistry(request) {
            if (_.isArray(request.body)) {
                return request.body.map(function (stub) {
                    return stubRegistry.addStub(stub).id;
{'fraction_non_alphanumeric': 0.11930363543266768, 'fraction_numerical': 0.0010240655401945725, 'mean_word_length': 1.6125724476148016, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6305160', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1424, 'n_words': 249}