Skip to content

Commit

Permalink
Expose realm. Closes #2235. Closes #2230
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 1, 2014
1 parent f18de3e commit cc0cd5b
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 129 deletions.
72 changes: 46 additions & 26 deletions docs/Reference.md
Expand Up @@ -4,14 +4,15 @@
- [`new Server([options])`](#new-serveroptions)
- [Server properties](#server-properties)
- [`server.app`](#serverapp)
- [`server.config`](#serverconfig)
- [`server.connections`](#serverconnections)
- [`server.info`](#serverinfo)
- [`server.load`](#serverload)
- [`server.listener`](#serverlistener)
- [`server.methods`](#servermethods)
- [`server.mime`](#servermime)
- [`server.plugins`](#serverplugins)
- [`server.realm`](#serverrealm)
- [`server.root`](#serverroot)
- [`server.settings`](#serversettings)
- [`server.version`](#serverversion)
- [`server.after(method, [dependencies])`](#serveraftermethod-dependencies)
Expand Down Expand Up @@ -218,28 +219,6 @@ var handler = function (request, reply) {
};
```

#### `server.config`

When the server object is provided as an argument to the plugin `register()` method, the `config`
property provides the registration preferences passed the
[`server.register()`](#serverregisterplugins-options-callback) method. `config` is an object with
the following properties:
- `routes` - routes preferences:
- `prefix` - the route path prefix used by any calls to [`server.route()`](#serverrouteoptions)
from the server.
- `vhost` - the route virtual host settings used by any calls to
[`server.route()`](#serverrouteoptions) from the server.

The `config` property should be considered read-only and should not be changed.

```js
exports.register = function (server, options, next) {

console.log(server.config.route.prefix);
next();
};
```

#### `server.connections`

An array containing the server's connections. When the server object is returned from
Expand Down Expand Up @@ -381,9 +360,10 @@ var server = new Hapi.Server(options);

#### `server.plugins`

An object containing the public API exposed by each plugin registered where each key is a plugin
name and the values are the exposed properties by each plugin using
[`server.expose()`](#serverexposekey-value).
An object containing the values exposed by each plugin registered where each key is a plugin name
and the values are the exposed properties by each plugin using
[`server.expose()`](#serverexposekey-value). Plugins may set the value of the
`server.plugins[name]` object directly or via the `server.expose()` method.

```js
exports.register = function (server, options, next) {
Expand All @@ -398,6 +378,46 @@ exports.register.attributes = {
};
```

#### `server.realm`

The realm object contains server-wide or plugin-specific state that can be shared across various
methods. For example, when calling [`server.bind()`](#serverbindcontext), the active realm
`settings.bind` property is set which is then used by routes and extensions added at the same level
(server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state
used by the framework when adding routes, extensions, and other properties.

- `modifiers` - when the server object is provided as an argument to the plugin `register()`
method, `modifiers` provides the registration preferences passed the
[`server.register()`](#serverregisterplugins-options-callback) method and includes:
- `route` - routes preferences:
- `prefix` - the route path prefix used by any calls to [`server.route()`](#serverrouteoptions)
from the server.
- `vhost` - the route virtual host settings used by any calls to
[`server.route()`](#serverrouteoptions) from the server.
- `plugin` - the active plugin name (empty string if at the server root).
- `plugins` - plugin-specific state to be shared only among activities sharing the same active
state. `plugins` is an object where each key is a plugin name and the value is the plugin state.
- `settings` - settings overrides:
- `files.relativeTo`
- `bind`

The `server.realm` object should be considered read-only and must not be changed directly except
for the `plugins` property can be directly manipulated by the plugins (each setting its own under
`plugins[name]`).

```js
exports.register = function (server, options, next) {

console.log(server.realm.modifiers.route.prefix);
return next();
};
```

#### `server.root`

The root server object containing all the connections and the root server methods (e.g. `start()`,
`stop()`, `connection()`).

#### `server.settings`

The server configuration object after defaults applied.
Expand Down
8 changes: 4 additions & 4 deletions lib/auth.js
Expand Up @@ -93,7 +93,7 @@ internals.Auth.prototype.test = function (name, request, next) {
return next(response, data && data.credentials);
};

var reply = request.server._replier.interface(request, transfer);
var reply = request.server._replier.interface(request, null, transfer);
strategy.authenticate(request, reply);
};

Expand Down Expand Up @@ -203,7 +203,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
exit(response, strategy, data);
};

var reply = request.server._replier.interface(request, transfer);
var reply = request.server._replier.interface(request, null, transfer);
self._strategies[strategy].authenticate(request, reply);
});
};
Expand Down Expand Up @@ -357,7 +357,7 @@ internals.Auth.payload = function (request, next) {

request._protect.run('auth:payload:' + request.auth.strategy, finalize, function (exit) {

var reply = request.server._replier.interface(request, exit);
var reply = request.server._replier.interface(request, null, exit);
strategy.payload(request, reply);
});
};
Expand All @@ -381,7 +381,7 @@ internals.Auth.response = function (request, next) {

request._protect.run('auth:response:' + request.auth.strategy, next, function (exit) {

var reply = request.server._replier.interface(request, exit);
var reply = request.server._replier.interface(request, null, exit);
strategy.response(request, reply);
});
};
22 changes: 11 additions & 11 deletions lib/connection.js
Expand Up @@ -288,7 +288,7 @@ internals.Connection.prototype.match = function (method, path, host) {
};


internals.Connection.prototype._ext = function (event, func, options, env) {
internals.Connection.prototype._ext = function (event, func, options, realm) {

options = options || {};

Expand All @@ -297,15 +297,15 @@ internals.Connection.prototype._ext = function (event, func, options, env) {
var settings = {
before: options.before,
after: options.after,
group: env.plugin
group: realm.plugin
};

var nodes = [];
([].concat(func)).forEach(function (fn, i) {

var node = {
func: fn, // function (request, next) { next(); }
env: env,
realm: realm,
bind: options.bind
};

Expand All @@ -317,7 +317,7 @@ internals.Connection.prototype._ext = function (event, func, options, env) {
};


internals.Connection.prototype._route = function (configs, env) {
internals.Connection.prototype._route = function (configs, realm) {

configs = [].concat(configs);
for (var i = 0, il = configs.length; i < il; ++i) {
Expand All @@ -329,19 +329,19 @@ internals.Connection.prototype._route = function (configs, env) {

var settings = Hoek.shallow(config);
settings.method = method;
this._addRoute(settings, env);
this._addRoute(settings, realm);
}
}
else {
this._addRoute(config, env);
this._addRoute(config, realm);
}
}
};


internals.Connection.prototype._addRoute = function (config, env) {
internals.Connection.prototype._addRoute = function (config, realm) {

var route = new Route(config, this, env); // Do no use config beyond this point, use route members
var route = new Route(config, this, realm); // Do no use config beyond this point, use route members
var vhosts = [].concat(route.settings.vhost || '*');

for (var i = 0, il = vhosts.length; i < il; ++i) {
Expand All @@ -365,7 +365,7 @@ internals.Connection.prototype._defaultRoutes = function () {
return reply(Boom.notFound());
}
}
}, this, this.server._env));
}, this, this.server.realm));

this._router.special('badRequest', new Route({
method: 'badRequest',
Expand All @@ -377,7 +377,7 @@ internals.Connection.prototype._defaultRoutes = function () {
return reply(Boom.badRequest());
}
}
}, this, this.server._env));
}, this, this.server.realm));

if (this.settings.routes.cors) {
this._router.special('options', new Route({
Expand All @@ -391,6 +391,6 @@ internals.Connection.prototype._defaultRoutes = function () {
return reply({});
}
}
}, this, this.server._env));
}, this, this.server.realm));
}
};
10 changes: 5 additions & 5 deletions lib/handler.js
Expand Up @@ -83,7 +83,7 @@ internals.handler = function (request, callback) {

// Decorate request

var reply = request.server._replier.interface(request, finalize);
var reply = request.server._replier.interface(request, request.route.realm, finalize);
var bind = request.route.bind;

// Execute handler
Expand Down Expand Up @@ -150,7 +150,7 @@ exports.register = function (server) {

server.decorate('reply', 'view', function (template, context, options) {

var viewsManager = this._env.views || this.request._route._viewsEnv.views || this.request.server._env.views;
var viewsManager = this.realm.plugins.vision.manager || this.request.server.realm.plugins.vision.manager;
Hoek.assert(viewsManager, 'Cannot render view without a views manager configured');
return this.response(viewsManager.response(template, context, options, this.request));
});
Expand Down Expand Up @@ -296,7 +296,7 @@ internals.pre = function (pre) {

// Setup environment

var reply = request.server._replier.interface(request, finalize);
var reply = request.server._replier.interface(request, request.route.realm, finalize);
var bind = request.route.bind;

// Execute handler
Expand All @@ -321,8 +321,8 @@ exports.invoke = function (request, event, callback) {

Items.serial(exts.nodes, function (ext, next) {

var reply = request.server._replier.interface(request, next, { env: ext.env }); // env passed for views
var bind = (ext.bind || ext.env.settings.bind);
var reply = request.server._replier.interface(request, ext.realm, next);
var bind = (ext.bind || ext.realm.settings.bind);

ext.func.call(bind, request, reply);
}, exit);
Expand Down
10 changes: 5 additions & 5 deletions lib/methods.js
Expand Up @@ -19,26 +19,26 @@ exports = module.exports = internals.Methods = function (server) {
};


internals.Methods.prototype.add = function (name, method, options, env) {
internals.Methods.prototype.add = function (name, method, options, realm) {

if (typeof name !== 'object') {
return this._add(name, method, options, env);
return this._add(name, method, options, realm);
}

// {} or [{}, {}]

var items = [].concat(name);
for (var i = 0, il = items.length; i < il; ++i) {
var item = items[i];
this._add(item.name, item.method, item.options, env);
this._add(item.name, item.method, item.options, realm);
}
};


exports.methodNameRx = /^[a-zA-Z]\w*(?:\.[a-zA-Z]\w*)*$/;


internals.Methods.prototype._add = function (name, method, options, env) {
internals.Methods.prototype._add = function (name, method, options, realm) {

var self = this;

Expand All @@ -52,7 +52,7 @@ internals.Methods.prototype._add = function (name, method, options, env) {

var settings = Hoek.cloneWithShallow(options, ['bind']);
settings.generateKey = settings.generateKey || internals.generateKey;
var bind = settings.bind || env.settings.bind || null;
var bind = settings.bind || realm.settings.bind || null;

// Normalize methods

Expand Down

0 comments on commit cc0cd5b

Please sign in to comment.