<commit_before>(function () {
    "use strict";

    var errors, mongo, bcrypt, async, url, request, fs, tv4, isSystemCollection, isFilesCollection,
        createObjectId, getUserId, sanitizeDocument, parseResourceUri, validatePermissions;

    errors = require("./errors");
    mongo = require("mongodb");
    bcrypt = require("bcrypt");
    async = require("async");
    url = require("url");
    request = require("request");
    fs = require("fs");
    tv4 = require("tv4");

    isSystemCollection = function (col) {
        if (col.indexOf("system.") === 0) {
            return true;
        } else if (col.indexOf("bounce.") === 0) {
            return true;
        } else if (col.lastIndexOf(".chunks") !== -1 && col.lastIndexOf(".chunks") === col.length - 7) {
            return true;
        } else {
            return false;
        }
    };

    isFilesCollection = function (col) {
        if (col.lastIndexOf(".files") !== -1 && col.lastIndexOf(".files") === col.length - 6) {
            return true;
        } else {
            return false;
        }
    };

    createObjectId = function (hex, callback) {
        var id;
        try {
            id = new mongo.ObjectID(hex);
            callback(null, id);
        } catch (err) {
            callback(new errors.BadRequest(err.message));
        }
    };

    getUserId = function (user) {
        if (typeof user === "object") {
            return user.username;
        } else {
            return user;
        }
    };

    sanitizeDocument = function (doc) {
        if (doc.hasOwnProperty("_links") === true) {
            if (doc._links.hasOwnProperty("self") === true) {
                delete doc._links.self;
            }
            if (doc._links.hasOwnProperty("governance") === true) {
                delete doc._links.governance;
            }
        }
        if (doc.hasOwnProperty("_embedded") === true) {
            delete doc._embedded;
        }
        if (doc.hasOwnProperty("_creator") === true) {
            delete doc._creator;
        }
        if (doc.hasOwnProperty("_permissions") === true) {
            delete doc._permissions;
        }
    };

    parseResourceUri = function (resourceUri, callback) {
        var resourcePath, resourceMatcher;
        resourceUri = url.parse(resourceUri);
        if (resourceUri.protocol !== null && resourceUri.host !== null) {
            callback(null, {
                type: "external",
                href: resourceUri.href
            });
        } else {
         // Extract collection and document name from internal URI.
            resourceMatcher = resourceUri.pathname.match(/^(\/)(?:([^\/]+)(?:\/([^\/]+))?)?(.*)*$/);
            if (resourceMatcher === null) {
                callback(new errors.BadRequest("\"resource\" URL parameter should be a valid pathname that starts with \"/\"."));
            } else {
                if (resourceMatcher[1] === "/" && resourceMatcher[2] === undefined && resourceMatcher[3] === undefined && resourceMatcher[4] === undefined) {
                    callback(null, {
                        type: "internal",
                        root: true
                    });
                } else if (resourceMatcher[1] === "/" && resourceMatcher[2] !== undefined && resourceMatcher[3] === undefined && resourceMatcher[4] === undefined) {
                    callback(null, {
                        type: "internal",
                        collectionName: resourceMatcher[2]
                    });
                } else if (resourceMatcher[1] === "/" && resourceMatcher[2] !== undefined && resourceMatcher[3] !== undefined && resourceMatcher[4] === undefined) {
                    callback(null, {
                        type: "internal",
                        collectionName: resourceMatcher[2],
                        documentName: resourceMatcher[3]
                    });
                } else {
                    callback(new errors.NotFound());
                }
            }
        }
    };

    validatePermissions = function (permissions) {
        var validationResult, operatorCount;
        validationResult = tv4.validateResult(permissions, {
            type: "object",
            properties: {
                _inherit: {
                    type: "string"
                },
                rules: {
                    type: "array",<commit_msg>Simplify resource pattern.<commit_after>(function () {
    "use strict";

    var errors, mongo, bcrypt, async, url, request, fs, tv4, isSystemCollection, isFilesCollection,
        createObjectId, getUserId, sanitizeDocument, parseResourceUri, validatePermissions;

    errors = require("./errors");
    mongo = require("mongodb");
    bcrypt = require("bcrypt");
    async = require("async");
    url = require("url");
    request = require("request");
    fs = require("fs");
    tv4 = require("tv4");

    isSystemCollection = function (col) {
        if (col.indexOf("system.") === 0) {
            return true;
        } else if (col.indexOf("bounce.") === 0) {
            return true;
        } else if (col.lastIndexOf(".chunks") !== -1 && col.lastIndexOf(".chunks") === col.length - 7) {
            return true;
        } else {
            return false;
        }
    };

    isFilesCollection = function (col) {
        if (col.lastIndexOf(".files") !== -1 && col.lastIndexOf(".files") === col.length - 6) {
            return true;
        } else {
            return false;
        }
    };

    createObjectId = function (hex, callback) {
        var id;
        try {
            id = new mongo.ObjectID(hex);
            callback(null, id);
        } catch (err) {
            callback(new errors.BadRequest(err.message));
        }
    };

    getUserId = function (user) {
        if (typeof user === "object") {
            return user.username;
        } else {
            return user;
        }
    };

    sanitizeDocument = function (doc) {
        if (doc.hasOwnProperty("_links") === true) {
            if (doc._links.hasOwnProperty("self") === true) {
                delete doc._links.self;
            }
            if (doc._links.hasOwnProperty("governance") === true) {
                delete doc._links.governance;
            }
        }
        if (doc.hasOwnProperty("_embedded") === true) {
            delete doc._embedded;
        }
        if (doc.hasOwnProperty("_creator") === true) {
            delete doc._creator;
        }
        if (doc.hasOwnProperty("_permissions") === true) {
            delete doc._permissions;
        }
    };

    parseResourceUri = function (resourceUri, callback) {
        var resourcePath, resourceMatcher;
        resourceUri = url.parse(resourceUri);
        if (resourceUri.protocol !== null && resourceUri.host !== null) {
            callback(null, {
                type: "external",
                href: resourceUri.href
            });
        } else {
         // Extract collection and document name from internal URI.
            resourceMatcher = resourceUri.pathname.match(/^\/(?:([^\/]+)(?:\/([^\/]+))?.*)?$/);
            if (resourceMatcher === null) {
                callback(new errors.BadRequest("\"resource\" URL parameter should be a valid pathname that starts with \"/\"."));
            } else {
                if (resourceMatcher[1] === undefined && resourceMatcher[2] === undefined) {
                    callback(null, {
                        type: "internal",
                        root: true
                    });
                } else if (resourceMatcher[1] !== undefined && resourceMatcher[2] === undefined) {
                    callback(null, {
                        type: "internal",
                        collectionName: resourceMatcher[1]
                    });
                } else if (resourceMatcher[1] !== undefined && resourceMatcher[2] !== undefined) {
                    callback(null, {
                        type: "internal",
                        collectionName: resourceMatcher[1],
                        documentName: resourceMatcher[2]
                    });
                } else {
                    callback(new errors.NotFound());
                }
            }
        }
    };

    validatePermissions = function (permissions) {
        var validationResult, operatorCount;
        validationResult = tv4.validateResult(permissions, {
            type: "object",
            properties: {
                _inherit: {
                    type: "string"
                },
                rules: {
                    type: "array",
{'fraction_non_alphanumeric': 0.13730751283247783, 'fraction_numerical': 0.005132991133924405, 'mean_word_length': 1.5675351901766996, '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': '709430', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2134, 'n_words': 478}