Skip to content

Commit

Permalink
Conditional connectionless mode. Closes #3350
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 27, 2016
1 parent 58485c0 commit 4f910d2
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 9 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .npmignore
100644 → 100755
Empty file.
7 changes: 5 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 15.0.x API Reference
# 15.1.x API Reference

- [Server](#server)
- [`new Server([options])`](#new-serveroptions)
Expand Down Expand Up @@ -2212,7 +2212,10 @@ The plugin function must include an `attributes` function property with the foll
setting dependencies via [`server.dependency()`](#serverdependencydependencies-after).
- `connections` - if `false`, does not allow the plugin to call server APIs that modify the
connections such as adding a route or configuring state. This flag allows the plugin to be
registered before connections are added and to pass dependency requirements. Defaults to `true`.
registered before connections are added and to pass dependency requirements. When set to
`'conditional'`, the mode is based on the presence of selected connections (if the server
has connections, it is the same as `true`, but if no connections are available, it is the
same as `false`). Defaults to `true`.
- `once` - if `true`, will only register the plugin once per connection (or once per server for a
connectionless plugin). If set, overrides the `once` option passed to `server.register()`.
Defaults to `undefined` (registration will be based on the `server.register()` option `once`).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse)
authentication, and other essential facilities for building web and services applications. **hapi** enables
developers to focus on writing reusable application logic in a highly modular and prescriptive approach.

Development version: **15.0.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
Development version: **15.1.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
[![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg?branch=master)](http://travis-ci.org/hapijs/hapi)

For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the
Expand Down
9 changes: 5 additions & 4 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback

// Protect against multiple registrations

if (!item.connections) {
const connectionless = (item.connections === 'conditional' ? selection.connections.length === 0 : !item.connections);
if (connectionless) {
if (this.root._registrations[item.name]) {
if (item.options.once) {
return next();
Expand Down Expand Up @@ -294,21 +295,21 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback
}

if (item.options.once &&
item.connections &&
!connectionless &&
!connections.length) {

return next(); // All the connections already registered
}
}

selection.connections = (item.connections ? connections : null);
selection.connections = (connectionless ? null : connections);
selection._single();

if (item.dependencies) {
selection.dependency(item.dependencies);
}

if (!item.connections) {
if (connectionless) {
selection.connection = this.connection;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ internals.plugin = internals.register.keys({
version: Joi.string(),
multiple: Joi.boolean().default(false),
dependencies: Joi.array().items(Joi.string()).single(),
connections: Joi.boolean().default(true),
connections: Joi.boolean().allow('conditional').default(true),
once: Joi.boolean().valid(true)
})
.required()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "15.0.4",
"version": "15.1.0",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/hapi"
Expand Down
Empty file modified test/file/note.txt
100644 → 100755
Empty file.
45 changes: 45 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,51 @@ describe('Plugin', () => {
});
});

it('register a conditional connectionless plugin (empty selection)', (done) => {

const test = function (srv, options, next) {

expect(srv.connections).to.be.null();
expect(srv.connection).to.be.a.function();
return next();
};

test.attributes = {
name: 'test',
connections: 'conditional'
};

const server = new Hapi.Server();
server.register(test, (err) => {

expect(err).to.not.exist();
done();
});
});

it('register a conditional connectionless plugin (with selection)', (done) => {

const test = function (srv, options, next) {

expect(srv.connections).to.exist();
expect(srv.connection).to.not.exist();
return next();
};

test.attributes = {
name: 'test',
connections: 'conditional'
};

const server = new Hapi.Server();
server.connection();
server.register(test, (err) => {

expect(err).to.not.exist();
done();
});
});

it('register a plugin once per connection (no selection left)', (done) => {

let count = 0;
Expand Down

0 comments on commit 4f910d2

Please sign in to comment.