Skip to content

Commit

Permalink
Statehood API. Closes #1877
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Aug 22, 2014
1 parent 7d9636f commit e8bd550
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 58 deletions.
24 changes: 0 additions & 24 deletions lib/defaults.js
Expand Up @@ -148,30 +148,6 @@ exports.security = {
};


// State management

exports.state = {

// Validation settings

strictHeader: undefined, // Defaults to server.settings.state.cookies.strictHeader
failAction: undefined, // Defaults to server.settings.state.cookies.failAction
clearInvalid: undefined, // Defaults to server.settings.state.cookies.clearInvalid

// Cookie attributes

isSecure: false,
isHttpOnly: false,
path: null,
domain: null,
ttl: null, // MSecs, 0 means remove

// Value generation

encoding: 'none' // options: 'base64json', 'base64', 'form', 'iron', 'none'
};


// Views

exports.views = {
Expand Down
6 changes: 3 additions & 3 deletions lib/response/headers.js
Expand Up @@ -189,10 +189,10 @@ internals.state = function (response, request, next) {
states.push(request._states[name]);
}

keys = Object.keys(request.server._stateDefinitions);
keys = Object.keys(request.server._stateDefinitions.cookies);
Items.parallel(keys, function (name, nextKey) {

var autoValue = request.server._stateDefinitions[name].autoValue;
var autoValue = request.server._stateDefinitions.cookies[name].autoValue;
if (!autoValue || names[name]) {
return nextKey();
}
Expand Down Expand Up @@ -224,7 +224,7 @@ internals.state = function (response, request, next) {
return next();
}

Statehood.format(states, request.server, function (err, header) {
Statehood.format(states, request.server._stateDefinitions, function (err, header) {

if (err) {
return next(err);
Expand Down
4 changes: 2 additions & 2 deletions lib/route.js
Expand Up @@ -555,14 +555,14 @@ internals.state = function (request, next) {
var definitions = request.server._stateDefinitions;
var settings = request.server.settings.state;

Statehood.parse(cookies, definitions, settings, function (err, state, invalids) {
Statehood.parse(cookies, definitions, function (err, state, invalids) {

request.state = state;

var names = Object.keys(invalids);
for (var i = 0, il = names.length; i < il; ++i) {
var name = names[i];
var definition = definitions[name];
var definition = definitions.cookies[name];

if (definition &&
definition.clearInvalid !== undefined ? definition.clearInvalid : settings.cookies.clearInvalid) {
Expand Down
12 changes: 4 additions & 8 deletions lib/server.js
Expand Up @@ -9,6 +9,7 @@ var Shot = require('shot');
var Boom = require('boom');
var Hoek = require('hoek');
var LruCache = require('lru-cache');
var Statehood = require('statehood');
var Auth = require('./auth');
var Defaults = require('./defaults');
var Request = require('./request');
Expand Down Expand Up @@ -121,7 +122,7 @@ exports = module.exports = internals.Server = function (/* host, port, options *

this._ext = new Ext(['onRequest', 'onPreAuth', 'onPostAuth', 'onPreHandler', 'onPostHandler', 'onPreResponse']);

this._stateDefinitions = {};
this._stateDefinitions = new Statehood.Definitions(this.settings.state.cookies);
this._registrations = {};

if (args.pack) {
Expand Down Expand Up @@ -473,13 +474,8 @@ internals.Server.prototype._route = function (configs, env) {

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

Hoek.assert(name && typeof name === 'string', 'Invalid name');
Hoek.assert(!this._stateDefinitions[name], 'State already defined:', name);
if (options) {
Schema.assert('state', options, name);
}

this._stateDefinitions[name] = Hoek.applyToDefaults(Defaults.state, options || {});
Schema.assert('state', options, name);
this._stateDefinitions.add(name, options);
};


Expand Down
21 changes: 0 additions & 21 deletions test/server.js
Expand Up @@ -1206,27 +1206,6 @@ describe('Server', function () {
});
});

describe('#state', function () {

it('uses default options', function (done) {

var server = new Hapi.Server();
server.state('steve');
expect(server._stateDefinitions['steve']).deep.equal(Defaults.state);
done();
});

it('throws when missing name', function (done) {

var server = new Hapi.Server();
expect(function () {

server.state();
}).to.throw('Invalid name');
done();
});
});

describe('Timeouts', { parallel: false }, function () {

var slowHandler = function (request, reply) {
Expand Down

0 comments on commit e8bd550

Please sign in to comment.