<commit_before>'use strict';

var Q = require('q'),
    util = require('util'),
    qhttp = require("q-io/http"),
    jwt = require('jsonwebtoken'),
    uuidGen = require('node-uuid')

Q.longStackSupport = true

function HttpError(statusCode, msgCode, details) {
    Error.captureStackTrace(this, this.constructor)

    this.status = statusCode
    this.msgCode = msgCode
    this.details = details || {}

    this.name = this.constructor.name
    this.message = [
        "Returning HTTP status ",
        statusCode,
        " because ",
        msgCode,
        ": ",
        JSON.stringify(details)
    ].join('')
}    
util.inherits(HttpError, Error)

var jwtVerifyQ = Q.nbind(jwt.verify, jwt);

var self = {
    uuidRe: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
     assertVector: function(v) {
        if (v === undefined || v === null || isNaN(v.x) || isNaN(v.y) || isNaN(v.z) || Object.keys(v).length !== 3)
            throw new Error("invalid vector: " + JSON.stringify(v))

        return v
    },
    assertUUID: function(value) {
        if (typeof value !== 'string' || !self.uuidRe.test(value))
            throw new Error("invalid uuid "+value)
    },
    logging: require('./logging'),
    http: {
        Error: HttpError,
        errHandler: function(req, res) {
            return function(err) {
                if (err.stack === undefined) {
                    var fakeErr = new Error();
                    Error.captureStackTrace(fakeErr, self.http.errHandler);
                    err.stack = fakeErr.stack
                }

                req.ctx.error({ err: err }, 'http request server error')

                if (self.isA(err, "HttpError")) {
                    res.status(err.status).send({
                        errorCode: err.msgCode,
                        errorDetails: err.details
                    })
                } else {
                    res.status(500).send(err.toString())
                }
            }
        },
        cors_policy: function(app) {
            var cors = require('cors')({
                credentials: true,
                origin: function(origin, cb) {
                    cb(null, true)
                }
            })

            app.use(cors)
            app.options("*", cors)
        },
        authorize_req: function (req, restricted) {
            var auth_header = req.headers.authorization

            if (auth_header === undefined) {
                // We do this so that the Q-promise error handling
                // will catch it
                return Q.fcall(function() {
                    throw new Error("not authorized missing header")
                })
            }

            var parts = auth_header.split(' ')
            if (parts[0] != "Bearer") {
                return Q.fcall(function() {
                    throw new Error("not authorized missing bearer")
                })
            }<commit_msg>lots of stuff, plus swagger<commit_after>'use strict';

var Q = require('q'),
    util = require('util'),
    qhttp = require("q-io/http"),
    jwt = require('jsonwebtoken'),
    uuidGen = require('node-uuid')

Q.longStackSupport = true

function HttpError(statusCode, msgCode, details) {
    Error.captureStackTrace(this, this.constructor)

    this.status = statusCode
    this.msgCode = msgCode
    this.details = details || {}

    this.name = this.constructor.name
    this.message = [
        "Returning HTTP status ",
        statusCode,
        " because ",
        msgCode,
        ": ",
        JSON.stringify(details)
    ].join('')
}    
util.inherits(HttpError, Error)

var jwtVerifyQ = Q.nbind(jwt.verify, jwt);

var self = {
    uuidRe: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
     assertVector: function(v) {
        if (v === undefined || v === null || isNaN(v.x) || isNaN(v.y) || isNaN(v.z) || Object.keys(v).length !== 3)
            throw new Error("invalid vector: " + JSON.stringify(v))

        return v
    },
    assertUUID: function(value) {
        if (typeof value !== 'string' || !self.uuidRe.test(value))
            throw new Error("invalid uuid "+value)
    },
    logging: require('./logging'),
    http: {
        Error: HttpError,
        errHandler: function(req, res) {
            return function(err) {
                if (err.stack === undefined) {
                    var fakeErr = new Error();
                    Error.captureStackTrace(fakeErr, self.http.errHandler);
                    err.stack = fakeErr.stack
                }

                req.ctx.error({ err: err }, 'http request server error')

                if (self.isA(err, "HttpError")) {
                    res.status(err.status).json({
                        errorCode: err.msgCode,
                        errorDetails: err.details
                    })
                } else {
                    res.status(500).json({
                        errorDetails: err.toString()
                    })
                }
            }
        },
        cors_policy: function(app) {
            var cors = require('cors')({
                credentials: true,
                origin: function(origin, cb) {
                    cb(null, true)
                }
            })

            app.use(cors)
            app.options("*", cors)
        },
        authorize_req: function (req, restricted) {
            var auth_header = req.headers.authorization

            if (auth_header === undefined) {
                // We do this so that the Q-promise error handling
                // will catch it
                return Q.fcall(function() {
                    throw new Error("not authorized missing header")
                })
            }

            var parts = auth_header.split(' ')
            if (parts[0] != "Bearer") {
                return Q.fcall(function() {
                    throw new Error("not authorized missing bearer")
                })
            }
{'fraction_non_alphanumeric': 0.13598396257935183, 'fraction_numerical': 0.00835282325425994, 'mean_word_length': 1.4966638865721436, 'pattern_counts': {'":': 2, '<': 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': '7604852', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1670, 'n_words': 358}