diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.npmignore b/.npmignore old mode 100644 new mode 100755 diff --git a/API.md b/API.md index 2ade5f42c..c6cfcaefb 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 15.0.x API Reference +# 15.1.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) @@ -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`). diff --git a/README.md b/README.md index 77e476e45..0e89cb572 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/plugin.js b/lib/plugin.js index 8dcecf45f..43cd11143 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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(); @@ -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; } diff --git a/lib/schema.js b/lib/schema.js index 9db42ff6e..3274017b2 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -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() diff --git a/package.json b/package.json index 9ffebdf94..9f44dd8a8 100755 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/file/note.txt b/test/file/note.txt old mode 100644 new mode 100755 diff --git a/test/plugin.js b/test/plugin.js index c13169f5a..b64e7df79 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -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;