<commit_before>"use strict";
var Boom = require("boom");
var Joi = require("joi");
var FalcorRouter = require("falcor-router");

var requestToContext = require("./requestToContext");
var FalcorEndpoint = module.exports = {};

var internals = {};

FalcorEndpoint.dataSourceRoute = function(getDataSource) {
    return function(req, reply) {
        var obs;
        var dataSource = getDataSource(req, reply);
        var context = requestToContext(req);
        // probably this should be sanity check function?
        if (Object.keys(context).length === 0) {
            return reply(Boom.badRequest("Request not supported"));
        }
        if (typeof context.method === "undefined" || context.method.length === 0) {
            return reply(Boom.badRequest("No query method provided"));
        }
        if (typeof dataSource[context.method] === "undefined") {
            return reply(Boom.badRequest("Data source does not implement the requested method"));
        }
        if (context.method === "set") {
            obs = dataSource[context.method](context.jsonGraph);
        } else if (context.method === "call") {
            obs = dataSource[context.method](context.callPath, context.arguments, context.pathSuffixes, context.paths);
        } else {
            obs = dataSource[context.method]([].concat(context.paths));
        }
        obs.subscribe(function(jsonGraphEnvelope) {
            reply(jsonGraphEnvelope);
        }, function(err) {
            reply(err);
        });
    };
};

FalcorEndpoint.register = function (server, options, next) {
    server.handler("falcor", internals.falcorHandler);
};

FalcorEndpoint.register.attributes = {
    pkg: require("../package.json")
};

internals.schema = Joi.object({
    routes: Joi.array().items(
        Joi.object().keys({
          route: Joi.string().required(),
          get: Joi.func().optional(),
          set: Joi.func().optional(),
          call: Joi.func().optional()
        }).or("get", "set", "call")
    ).required(),
    cacheRoutes: Joi.boolean().default(true),
    options: Joi.object().optional(),
    initialize: Joi.func().optional(),
    routerClass: Joi.func().optional()
}).nand("initialize", "routerClass");


internals.falcorHandler = function(route, options) {
    Joi.assert(options, internals.schema, "Invalid falcor handler options (" + route.path + ")");
    options = Joi.validate(options, internals.schema).value;
<commit_msg>Signal to hapi that plugin registration is done.<commit_after>"use strict";
var Boom = require("boom");
var Joi = require("joi");
var FalcorRouter = require("falcor-router");

var requestToContext = require("./requestToContext");
var FalcorEndpoint = module.exports = {};

var internals = {};

FalcorEndpoint.dataSourceRoute = function(getDataSource) {
    return function(req, reply) {
        var obs;
        var dataSource = getDataSource(req, reply);
        var context = requestToContext(req);
        // probably this should be sanity check function?
        if (Object.keys(context).length === 0) {
            return reply(Boom.badRequest("Request not supported"));
        }
        if (typeof context.method === "undefined" || context.method.length === 0) {
            return reply(Boom.badRequest("No query method provided"));
        }
        if (typeof dataSource[context.method] === "undefined") {
            return reply(Boom.badRequest("Data source does not implement the requested method"));
        }
        if (context.method === "set") {
            obs = dataSource[context.method](context.jsonGraph);
        } else if (context.method === "call") {
            obs = dataSource[context.method](context.callPath, context.arguments, context.pathSuffixes, context.paths);
        } else {
            obs = dataSource[context.method]([].concat(context.paths));
        }
        obs.subscribe(function(jsonGraphEnvelope) {
            reply(jsonGraphEnvelope);
        }, function(err) {
            reply(err);
        });
    };
};

FalcorEndpoint.register = function (server, options, next) {
    server.handler("falcor", internals.falcorHandler);
    
    next();
};

FalcorEndpoint.register.attributes = {
    pkg: require("../package.json")
};

internals.schema = Joi.object({
    routes: Joi.array().items(
        Joi.object().keys({
          route: Joi.string().required(),
          get: Joi.func().optional(),
          set: Joi.func().optional(),
          call: Joi.func().optional()
        }).or("get", "set", "call")
    ).required(),
    cacheRoutes: Joi.boolean().default(true),
    options: Joi.object().optional(),
    initialize: Joi.func().optional(),
    routerClass: Joi.func().optional()
}).nand("initialize", "routerClass");


internals.falcorHandler = function(route, options) {
    Joi.assert(options, internals.schema, "Invalid falcor handler options (" + route.path + ")");
    options = Joi.validate(options, internals.schema).value;

{'fraction_non_alphanumeric': 0.15456389452332658, 'fraction_numerical': 0.0008113590263691683, 'mean_word_length': 2.9606425702811245, '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': '4349373', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1376, 'n_words': 292}