Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions lib/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,42 @@ internals.Ext.prototype.add = function (event, func, options) {

internals.Ext.prototype._add = function (event, func, options, plugin) {

var self = this;

options = options || {};

Utils.assert(['onRequest', 'onPreHandler', 'onPostHandler'].indexOf(event) !== -1, 'Unknown event type: ' + event);

this._events[event] = this._events[event] || []
// Validate rules

var ext = {
priority: this._events[event].length,
before: [].concat(options.before || []),
after: [].concat(options.after || []),
group: plugin || '?',
func: func
};
var before = [].concat(options.before || []);
var after = [].concat(options.after || []);
var group = plugin || '?';

// Validate rules
Utils.assert(before.indexOf(group) === -1, 'Plugin ext cannot come before itself (' + group + ')');
Utils.assert(before.indexOf('?') === -1, 'Plugin ext cannot come before unassociated exts');
Utils.assert(after.indexOf(group) === -1, 'Plugin ext cannot come after itself (' + group + ')');
Utils.assert(after.indexOf('?') === -1, 'Plugin ext cannot come after unassociated exts');

// Add functions

Utils.assert(ext.before.indexOf(ext.group) === -1, 'Plugin ext cannot come before itself (' + ext.plugin + ')');
Utils.assert(ext.before.indexOf('?') === -1, 'Plugin ext cannot come before unassociated exts');
Utils.assert(ext.after.indexOf(ext.group) === -1, 'Plugin ext cannot come after itself (' + ext.plugin + ')');
Utils.assert(ext.after.indexOf('?') === -1, 'Plugin ext cannot come after unassociated exts');
this._events[event] = this._events[event] || [];

([].concat(func)).forEach(function (fn, i) {

var ext = {
priority: self._events[event].length,
before: before,
after: after,
group: group,
func: fn
};

self._events[event].push(ext);
});

// Insert event

this._events[event].push(ext);
var error = this.sort(event);
Utils.assert(!error, event + ' extension' + (plugin ? ' add by ' + plugin : '') + ' created a dependencies error');
};
Expand Down
30 changes: 29 additions & 1 deletion test/integration/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var it = Lab.test;

describe('Ext', function () {

describe('onRequest', function (done) {
describe('#onRequest', function (done) {

it('replies with custom response', function (done) {

Expand All @@ -39,4 +39,32 @@ describe('Ext', function () {
});
});
});

describe('#ext', function () {

it('supports adding an array of ext methods', function (done) {

var server = new Hapi.Server();
server.ext('onPreHandler', [
function (request, next) {

request.x = '1';
next();
},
function (request, next) {

request.x += '2';
next();
}
]);

server.route({ method: 'GET', path: '/', handler: function () { this.reply(this.x); } });

server.inject({ method: 'GET', url: '/' }, function (res) {

expect(res.result).to.equal('12');
done();
});
});
});
});
20 changes: 10 additions & 10 deletions test/integration/pack/--test1/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ var internals = {};

// Plugin registration

exports.register = function (pack, options, next) {
exports.register = function (pack, options, next) {

pack.select({ label: 'test' }).route({ path: '/test1', method: 'GET', handler: function () { this.reply('testing123'); } });
pack.api(internals.math);
pack.api(internals.text.glue, 'glue');

return next();
return next();
};


internals.math = {
add: function (a, b) {
internals.math = {
add: function (a, b) {

return a + b;
}
return a + b;
}
};


internals.text = {
glue: function (a, b) {
internals.text = {
glue: function (a, b) {

return a + b;
}
return a + b;
}
};


4 changes: 2 additions & 2 deletions test/integration/pack/--test2/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var internals = {};

// Plugin registration

exports.register = function (pack, options, next) {
exports.register = function (pack, options, next) {

pack.route({ path: '/test2', method: 'GET', handler: function () { this.reply('testing123'); } });
return next();
return next();
};