Skip to content

Commit

Permalink
Cleanup for #1521
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Mar 26, 2014
1 parent 458d17f commit 277f07c
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 124 deletions.
18 changes: 9 additions & 9 deletions docs/Reference.md
Expand Up @@ -1245,10 +1245,13 @@ server.inject('/', function (res) {

#### `server.handler(name, method)`

Registers a new handler type. This allows you to define a new handler type which can then be used in routes. Overriding the built in handler types (`directory`, `file`, `proxy`, and `view`), or any previously registered type is not allowed.
Registers a new handler type which can then be used in routes. Overriding the built in handler types (`directory`, `file`, `proxy`, and `view`),
or any previously registered types is not allowed.

- `name` - String name for the handler that you want to register.
- `method` - The method that will be used to handle the requests routed to it.
- `name` - string name for the handler being registered.
- `method` - the function used to generate the route handler using the signature `function(route, options)` where:
- `route` - the internal route object.
- `options` - the configuration object provided in the handler config.

```javascript
var Hapi = require('hapi');
Expand All @@ -1259,7 +1262,7 @@ server.handler('test', function (route, options) {

return function (request, reply) {

reply ('new handler: ' + options.msg);
reply('new handler: ' + options.msg);
}
});

Expand Down Expand Up @@ -2744,17 +2747,14 @@ exports.register = function (plugin, options, next) {

#### `plugin.handler(name, method)`

Registers a new handler type. This allows you to define a new handler type which can then be used in routes. Overriding the built in handler types (`directory`, `file`, `proxy`, and `view`), or any previously registered type is not allowed.

- `name` - String name for the handler that you want to register.
- `method` - The method that will be used to handle the requests routed to it.
Registers a new handler type as describe in [`server.handler(name, method)`](#serverhandlername-method).

```javascript
exports.register = function (plugin, options, next) {

var handlerFunc = function (route, options) {

return function(request, reply) {
return function (request, reply) {

reply('Message from plugin handler: ' + options.msg);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/directory.js
Expand Up @@ -3,7 +3,6 @@
var Fs = require('fs');
var Path = require('path');
var Boom = require('boom');
var Joi = require('joi');
var Async = require('async');
var Response = require('./response');
var File = require('./file');
Expand All @@ -18,8 +17,7 @@ var internals = {};

exports.handler = function (route, options) {

var schemaError = Joi.validate(options, Schema.directoryHandlerSchema);
Utils.assert(!schemaError, 'Invalid handler options for directory:', schemaError);
Schema.assert('directory handler', options, route.path);
Utils.assert(route.path[route.path.length - 1] === '}', 'The route path must end with a parameter:', route.path);
Utils.assert(route.params.length >= 1, 'The route path must include at least one parameter:', route.path);

Expand Down
4 changes: 1 addition & 3 deletions lib/file.js
Expand Up @@ -5,7 +5,6 @@ var Path = require('path');
var Crypto = require('crypto');
var Mime = require('mime');
var Boom = require('boom');
var Joi = require('joi');
var Utils = require('./utils');
var Response = require('./response');
var Schema = require('./schema');
Expand All @@ -18,8 +17,7 @@ var internals = {};

exports.handler = function (route, options) {

var schemaError = Joi.validate(options, Schema.fileHandlerSchema);
Utils.assert(!schemaError, 'Invalid handler options for file:', schemaError);
Schema.assert('file handler', options, route.path);
var settings = (typeof options !== 'object' ? { path: options } : Utils.clone(options)); // options can be reused
Utils.assert(typeof settings.path !== 'string' || settings.path[settings.path.length - 1] !== '/', 'File path cannot end with a \'/\':', route.path);

Expand Down
22 changes: 9 additions & 13 deletions lib/handler.js
Expand Up @@ -214,6 +214,15 @@ exports.configure = function (handler, route) {
};


exports.register = function (pack) {

pack._handler('proxy', Proxy.handler);
pack._handler('file', File.handler);
pack._handler('directory', Directory.handler);
pack._handler('view', Views.handler);
};


exports.prerequisites = function (config, server) {

if (!config) {
Expand Down Expand Up @@ -390,16 +399,3 @@ exports.invoke = function (request, event, callback) {
});
});
};


exports.register = function (server) {
// If handlers are already registered, just return
if (Object.keys(server.pack._handlers).length > 0) {
return;
}

server.handler('proxy', Proxy.handler);
server.handler('file', File.handler);
server.handler('directory', Directory.handler);
server.handler('view', Views.handler);
};
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -3,7 +3,7 @@
exports.error = exports.Error = exports.boom = exports.Boom = require('boom');
exports.Server = require('./server');
exports.utils = require('./utils');
exports.types = require('joi').types;
exports.types = require('joi');
exports.Pack = require('./pack');
exports.Composer = require('./composer');

Expand Down
3 changes: 1 addition & 2 deletions lib/methods.js
Expand Up @@ -45,8 +45,7 @@ internals.Methods.prototype._add = function (name, fn, options, env) {
Utils.assert(!Utils.reach(this.methods, name, { functions: false }), 'Server method function name already exists');

var options = options || {};
var schemaError = Schema.method(options);
Utils.assert(!schemaError, 'Invalid method options for', name, ':', schemaError);
Schema.assert('method', options, name);

var settings = Utils.clone(options);
settings.generateKey = settings.generateKey || internals.generateKey;
Expand Down
4 changes: 3 additions & 1 deletion lib/pack.js
Expand Up @@ -11,6 +11,7 @@ var Defaults = require('./defaults');
var Ext = require('./ext');
var Methods = require('./methods');
var Protect = require('./protect');
var Handler = require('./handler');


// Declare internals
Expand Down Expand Up @@ -58,6 +59,8 @@ exports = module.exports = internals.Pack = function (options) {
}

this._ext = new Ext(['onPreStart']);

Handler.register(this);
};


Expand Down Expand Up @@ -334,7 +337,6 @@ internals.Pack.prototype._register = function (plugin, options, callback, _depen
root.handler = function (/* name, method */) {

var args = Array.prototype.slice.call(arguments);

return self._handler.apply(self, args);
};

Expand Down
4 changes: 1 addition & 3 deletions lib/proxy.js
Expand Up @@ -2,7 +2,6 @@

var Boom = require('boom');
var Nipple = require('nipple');
var Joi = require('joi');
var Defaults = require('./defaults');
var Utils = require('./utils');
var Response = require('./response');
Expand All @@ -16,8 +15,7 @@ var internals = {};

exports.handler = function (route, options) {

var schemaError = Joi.validate(options, Schema.proxyHandlerSchema);
Utils.assert(!schemaError, 'Invalid handler options for proxy:', schemaError);
Schema.assert('proxy handler', options, route.path);
Utils.assert(!route.settings.payload || ((route.settings.payload.output === 'data' || route.settings.payload.output === 'stream') && !route.settings.payload.parse), 'Cannot proxy if payload is parsed or if output is not stream or data');
var settings = Utils.applyToDefaults(Defaults.proxy, options);
settings.mapUri = options.mapUri || internals.mapUri(options.protocol, options.host, options.port, options.uri);
Expand Down
7 changes: 2 additions & 5 deletions lib/route.js
Expand Up @@ -30,11 +30,8 @@ exports = module.exports = internals.Route = function (options, server, env) {
this.settings = Utils.clone(options.config) || {};
this.settings.handler = this.settings.handler || options.handler;

var schemaError = Schema.routeOptions(options);
Utils.assert(!schemaError, 'Invalid route options for', options.path, ':', schemaError);

schemaError = Schema.routeConfig(this.settings);
Utils.assert(!schemaError, 'Invalid route config for', options.path, ':', schemaError);
Schema.assert('route', options, options.path);
Schema.assert('routeConfig', this.settings, options.path);

this.server = server;
this.env = env || {}; // Plugin-specific environment
Expand Down
64 changes: 14 additions & 50 deletions lib/schema.js
Expand Up @@ -9,13 +9,11 @@ var Utils = require('./utils');
var internals = {};


// Validate server options
exports.assert = function (type, options, message) {

exports.server = function (options) {

var error = Joi.validate(options, internals.serverSchema);
return (error ? error.annotated() : null);
};
var error = Joi.validate(options, internals[type]);
Utils.assert(!error, 'Invalid', type, 'options', message ? '(' + message + ')' : '', error && error.annotated());
}


internals.cache = Joi.object({
Expand Down Expand Up @@ -49,7 +47,7 @@ internals.viewSchema = function (base) {
};


internals.serverSchema = {
internals.server = {
app: Joi.object().allow(null),
cache: Joi.alternatives(Joi.string(), internals.cache, Joi.array().includes(internals.cache)).allow(null),
cors: Joi.object({
Expand Down Expand Up @@ -115,16 +113,7 @@ internals.serverSchema = {
};


// Validate route options

exports.routeOptions = function (options) {

var error = Joi.validate(options, internals.routeOptionsSchema);
return (error ? error.annotated() : null);
};


internals.routeOptionsSchema = {
internals.route = {
method: Joi.alternatives(Joi.string(), Joi.array().includes(Joi.string()).min(1)).required(),
path: Joi.string().required(),
vhost: [Joi.string(), Joi.array()],
Expand All @@ -133,15 +122,6 @@ internals.routeOptionsSchema = {
};


// Validate route config

exports.routeConfig = function (config) {

var error = Joi.validate(config, internals.routeConfigSchema);
return (error ? error.annotated() : null);
};


internals.pre = [
Joi.string(),
Joi.func(),
Expand All @@ -154,7 +134,7 @@ internals.pre = [
];


internals.routeConfigSchema = {
internals.routeConfig = {
pre: Joi.array().includes(internals.pre.concat(Joi.array().includes(internals.pre).min(1))),
handler: [
Joi.func(),
Expand Down Expand Up @@ -211,9 +191,7 @@ internals.routeConfigSchema = {
};


// Built in route handler schemas

exports.directoryHandlerSchema = Joi.object({
internals['directory handler'] = Joi.object({
path: Joi.alternatives(Joi.string(), Joi.array().includes(Joi.string()), Joi.func()).required(),
index: Joi.boolean(),
listing: Joi.boolean(),
Expand All @@ -224,7 +202,7 @@ exports.directoryHandlerSchema = Joi.object({
});


exports.fileHandlerSchema = [
internals['file handler'] = [
Joi.string(),
Joi.func(),
Joi.object({
Expand All @@ -236,7 +214,7 @@ exports.fileHandlerSchema = [
];


exports.proxyHandlerSchema = Joi.object({
internals['proxy handler'] = Joi.object({
host: Joi.string().xor('mapUri', 'uri'),
port: Joi.number().integer().without('mapUri', 'uri'),
protocol: Joi.string().valid('http', 'https', 'http:', 'https:').without('mapUri', 'uri'),
Expand All @@ -253,7 +231,7 @@ exports.proxyHandlerSchema = Joi.object({
});


exports.viewHandlerSchema = [
internals['view handler'] = [
Joi.string(),
Joi.object({
template: Joi.string(),
Expand All @@ -278,14 +256,7 @@ exports.view = function (options) {
};


exports.cache = function (options) {

var error = Joi.validate(options, internals.cacheSchema);
return (error ? error.annotated() : null);
};


internals.cacheSchema = {
internals.cachePolicy = {
cache: Joi.string().allow(null).allow(''),
segment: Joi.string(),
shared: Joi.boolean(),
Expand All @@ -296,15 +267,8 @@ internals.cacheSchema = {
};


exports.method = function (options) {

var error = Joi.validate(options, internals.methodSchema);
return (error ? error.annotated() : null);
};


internals.methodSchema = {
internals.method = {
bind: Joi.object().allow(null),
generateKey: Joi.func(),
cache: internals.cacheSchema
cache: internals.cachePolicy
};
10 changes: 2 additions & 8 deletions lib/server.js
Expand Up @@ -15,7 +15,6 @@ var Router = require('./router');
var Schema = require('./schema');
var Views = require('./views');
var Ext = require('./ext');
var Handler = require('./handler');
var Utils = require('./utils');
// Pack delayed required inline

Expand Down Expand Up @@ -71,8 +70,7 @@ exports = module.exports = internals.Server = function (/* host, port, options *
}

this.settings = Utils.applyToDefaults(Defaults.server, args.options || {});
var schemaError = Schema.server(this.settings);
Utils.assert(!schemaError, 'Invalid server options:', schemaError);
Schema.assert('server', this.settings);

this.settings.labels = Utils.unique([].concat(this.settings.labels)); // Convert string to array and removes duplicates

Expand Down Expand Up @@ -221,8 +219,6 @@ exports = module.exports = internals.Server = function (/* host, port, options *
this.info.uri = this.info.protocol + '://' + (this._host || Os.hostname() || 'localhost') + ':' + this.info.port;
}
}

Handler.register(this);
};

Utils.inherits(internals.Server, Events.EventEmitter);
Expand Down Expand Up @@ -440,9 +436,7 @@ internals.Server.prototype.views = function (options) {

internals.Server.prototype.cache = function (name, options) {

var schemaError = Schema.cache(options);
Utils.assert(!schemaError, 'Invalid cache options for', name, ':', schemaError);

Schema.assert('cachePolicy', options, name);
Utils.assert(!options.segment, 'Cannot override segment name in server cache');
return this.pack._provisionCache(options, 'server', name);
};
Expand Down
4 changes: 1 addition & 3 deletions lib/views.js
Expand Up @@ -3,7 +3,6 @@
var Fs = require('fs');
var Path = require('path');
var Boom = require('boom');
var Joi = require('joi');
var Defaults = require('./defaults');
var Schema = require('./schema');
var Utils = require('./utils');
Expand Down Expand Up @@ -360,8 +359,7 @@ internals.Manager.prototype._compile = function (template, engine, settings, cal

exports.handler = function (route, options) {

var schemaError = Joi.validate(options, Schema.viewHandlerSchema);
Utils.assert(!schemaError, 'Invalid handler options for view:', schemaError);
Schema.assert('view handler', options, route.path);

if (typeof options === 'string') {
options = { template: options };
Expand Down

0 comments on commit 277f07c

Please sign in to comment.