Comparing version 4.2.1 to 5.0.0
209
API.md
## Interface | ||
Glue exports a single function `compose` accepting a JSON `manifest` file specifying the hapi server options, connections, and registrations. | ||
Glue exports a single function `compose` accepting a JSON `manifest` file specifying the hapi server options and plugin registrations and returns a hapi `server` object. | ||
To start the server use the returned object to call `await server.start()`. | ||
### `compose(manifest, [options], [callback])` | ||
### `await compose(manifest, [options])` | ||
@@ -12,19 +13,52 @@ Composes a hapi server object where: | ||
+ If `server.cache` is specified, Glue will parse the entry and replace any prototype function field (eg. `engine`) specified as string by calling `require()` with that string. | ||
* `connections` - an array of connection options objects that are mapped to calls of [server.connection([options])](http://hapijs.com/api#serverconnectionoptions) | ||
* `registrations` - an array of objects holding entries to register with [server.register(plugin, [options], callback)](http://hapijs.com/api#serverregisterplugins-options-callback). Each object has two fields that map directly to the `server.register` named parameters: | ||
+ `plugin` - Glue will parse the entry and replace any plugin function field specified as a string by calling `require()` with that string or just put a plugin function to register it. The array form of this parameter accepted by `server.register()` is not allowed; use multiple registration objects instead. | ||
+ `options` - optional option object passed to `server.register()`. | ||
+ `options` - an object having | ||
* `relativeTo` - a file-system path string that is used to resolve loading modules with `require`. Used in `server.cache` and `registrations[].plugin` | ||
* `preConnections` - a callback function that is called prior to adding connections to the server. The function signature is `function (server, next)` where: | ||
+ `server` - is the server object returned from `new Server(options)`. | ||
+ `next`- the callback function the method must call to return control over to glue | ||
* `preRegister` - a callback function that is called prior to registering plugins with the server. The function signature is `function (server, next)` where: | ||
+ `server` - is the server object with all connections selected. | ||
+ `next`- the callback function the method must call to return control over to glue | ||
+ `callback` - the callback function with signature `function (err, server)` where: | ||
* `err` - the error response if a failure occurred, otherwise `null`. | ||
* `server` - the server object. Call `server.start()` to actually start the server. | ||
* `register` - an object containing two properties: the `plugins` to be registered and `options` to pass to `server.register` | ||
+ `plugins` - an array of entries to register with [await server.register(plugins, [options])](http://hapijs.com/api#-await-serverregisterplugins-options). | ||
* each entry may be one of three alternatives: | ||
1. A string to be `require()`d during composition. | ||
```js | ||
{ | ||
register: { | ||
plugins: [ 'myplugin' ] | ||
} | ||
} | ||
``` | ||
2. An object containing the `plugin` property which is a string to be `require`d during composition | ||
```js | ||
{ | ||
register: { | ||
plugins: [ { plugin: 'myplugin' } ] | ||
} | ||
} | ||
``` | ||
3. An object containing the `plugin` property which is the plugin object to be passed directly to `await server.register`*[]: | ||
```js | ||
{ | ||
register: { | ||
plugins: [ { plugin: require('myplugin') } ] | ||
} | ||
} | ||
``` | ||
* object entries may also contain the `options` property, which contains the plugin-level options passed to the plugin at registration time. | ||
```js | ||
{ | ||
register: { | ||
plugins: [ { plugin: 'myplugin', options: { host: 'my-host.com' } } ] | ||
} | ||
} | ||
``` | ||
* object entries may also contain override registration-options such as `routes`. | ||
```js | ||
{ | ||
register: { | ||
plugins: [ { plugin: 'myplugin', routes: { prefix: '/test/' } } ] | ||
} | ||
} | ||
``` | ||
+ `options` - optional registration-options object passed to `server.register()`. | ||
+ `options` - an object containing the following `compose` options: | ||
* `relativeTo` - a file-system path string that is used to resolve loading modules with `require`. Used in `server.cache` and `register.plugins[]` | ||
* `preRegister` - an async function that is called prior to registering plugins with the server. The function signature is `async function (server)` where: | ||
+ `server` - is the hapi server object. | ||
If no `callback` is provided, a `Promise` object is returned where the value passed to the Promise resolve handler is the `server` object and the value passed to the Promise reject handler is the error response if a failure occurred. | ||
`compose` returns the hapi server object. Call `await server.start()` to actually start the server. | ||
@@ -44,38 +78,22 @@ ### Notes | ||
server: { | ||
cache: 'redis' | ||
cache: 'redis', | ||
port: 8000 | ||
}, | ||
connections: [ | ||
{ | ||
port: 8000, | ||
labels: ['web'] | ||
}, | ||
{ | ||
port: 8001, | ||
labels: ['admin'] | ||
} | ||
], | ||
registrations: [ | ||
{ | ||
plugin: { | ||
register: './assets', | ||
register: { | ||
plugins: [ | ||
'./awesome-plugin.js', | ||
{ | ||
plugin: require('myplugin'), | ||
options: { | ||
uglify: true | ||
} | ||
} | ||
}, | ||
{ | ||
plugin: './ui-user', | ||
options: { | ||
select: ['web'] | ||
} | ||
}, | ||
{ | ||
plugin: { | ||
register: './ui-admin', | ||
}, | ||
{ | ||
plugin: './ui-user' | ||
}, | ||
{ | ||
plugin: './ui-admin', | ||
options: { | ||
sessiontime: 500 | ||
} | ||
}, | ||
options: { | ||
select: ['admin'], | ||
}, | ||
routes: { | ||
@@ -85,12 +103,4 @@ prefix: '/admin' | ||
} | ||
}, | ||
{ | ||
plugin: { | ||
register: require('./awesome-plugin.js'), | ||
options: { | ||
whyNot: true | ||
} | ||
} | ||
}, | ||
] | ||
] | ||
} | ||
}; | ||
@@ -102,12 +112,11 @@ | ||
Glue.compose(manifest, options, (err, server) => { | ||
if (err) { | ||
throw err; | ||
} | ||
server.start(() => { | ||
console.log('hapi days!'); | ||
}); | ||
}); | ||
try { | ||
const server = await Glue.compose(manifest, options); | ||
await server.start(); | ||
console.log('hapi days!'); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
``` | ||
@@ -118,52 +127,28 @@ | ||
```javascript | ||
'use strict'; | ||
try { | ||
const server = Hapi.server({ cache: [{ engine: require('redis') }], port: 8000 }); | ||
const plugins = []; | ||
const registerOptions = {}; | ||
let pluginPath; | ||
const server = Hapi.Server({ cache: [{ engine: require('redis') }] }); | ||
server.connection({ port: 8000, labels: ['web'] }); | ||
server.connection({ port: 8001, labels: ['admin'] }); | ||
let plugin; | ||
let pluginPath; | ||
let pluginOptions; | ||
let registerOptions; | ||
pluginPath = Path.join(__dirname, './assets'); | ||
pluginOptions = { uglify: true }; | ||
plugin = { register: require(pluginPath), options: pluginOptions }; | ||
registerOptions = { }; | ||
server.register(plugin, registerOptions, (err) => { | ||
pluginPath = Path.join(__dirname, './awesome-plugin.js'); | ||
plugins.push({ plugin: require(pluginPath) }); | ||
if (err) { | ||
throw err; | ||
} | ||
plugins.push({ plugin: require('myplugin'), options:{ uglify: true } }); | ||
pluginPath = Path.join(__dirname, './ui-user'); | ||
pluginOptions = { }; | ||
plugin = { register: require(pluginPath), options: pluginOptions }; | ||
registerOptions = { select: ['web'] }; | ||
server.register(plugin, registerOptions, (err) => { | ||
plugins.push({ plugin: require(pluginPath) }); | ||
if (err) { | ||
throw err; | ||
} | ||
pluginPath = Path.join(__dirname, './ui-admin'); | ||
pluginOptions = { sessiontime: 500 }; | ||
plugin = { register: require(pluginPath), options: pluginOptions }; | ||
registerOptions = { select: ['admin'], routes: { prefix: '/admin' } }; | ||
server.register(plugin, registerOptions, (err) => { | ||
pluginPath = Path.join(__dirname, './ui-admin'); | ||
plugins.push({ plugin: require(pluginPath), options: { sessiontime: 500 }, routes: { prefix: '/admin' } }); | ||
if (err) { | ||
throw err; | ||
} | ||
plugin = require('./awesome-plugin.js'); | ||
server.register(plugin, {whyNot: true}, (err) => { | ||
await server.register(plugins, registerOptions); | ||
if (err) { | ||
throw err; | ||
} | ||
server.start(() => { | ||
console.log('hapi days!'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
await server.start(); | ||
console.log('hapi days!'); | ||
} | ||
catch (err) | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
``` |
152
lib/index.js
@@ -7,3 +7,2 @@ 'use strict'; | ||
const Hoek = require('hoek'); | ||
const Items = require('items'); | ||
const Joi = require('joi'); | ||
@@ -25,10 +24,6 @@ | ||
server: Joi.object(), | ||
connections: Joi.array().items(Joi.object()), | ||
registrations: Joi.array().items(Joi.object({ | ||
plugin: [ | ||
Joi.string(), | ||
Joi.object({ register: [Joi.string(), Joi.func()] }).unknown() | ||
], | ||
options: Joi.object() | ||
})) | ||
register: Joi.object({ | ||
plugins: Joi.array(), | ||
options: Joi.any() | ||
}) | ||
}) | ||
@@ -38,114 +33,34 @@ }; | ||
exports.compose = function (manifest /*, [options], [callback] */) { | ||
exports.compose = async function (manifest, options = {}) { | ||
Hoek.assert(arguments.length <= 3, 'Invalid number of arguments'); | ||
const options = arguments.length === 1 || typeof arguments[1] === 'function' ? {} : arguments[1]; | ||
const callback = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length - 1] : null; | ||
// Return Promise if no callback provided | ||
if (!callback) { | ||
return new Promise((resolve, reject) => { | ||
exports.compose(manifest, options, (err, server) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(server); | ||
}); | ||
}); | ||
} | ||
Joi.assert(options, internals.schema.options, 'Invalid options'); | ||
Joi.assert(manifest, internals.schema.manifest, 'Invalid manifest'); | ||
// Create server | ||
const serverOpts = internals.parseServer(manifest.server || {}, options.relativeTo); | ||
const server = new Hapi.Server(serverOpts); | ||
const server = Hapi.server(serverOpts); | ||
const steps = []; | ||
steps.push((next) => { | ||
if (options.preRegister) { | ||
await options.preRegister(server); | ||
} | ||
if (options.preConnections) { | ||
options.preConnections(server, next); | ||
} | ||
else { | ||
next(); | ||
} | ||
}); | ||
if (manifest.register && manifest.register.plugins) { | ||
const plugins = manifest.register.plugins.map((plugin) => { | ||
steps.push((next) => { | ||
return internals.parsePlugin(plugin, options.relativeTo); | ||
}); | ||
// Load connections | ||
await server.register(plugins, manifest.register.options || {}); | ||
} | ||
if (manifest.connections && manifest.connections.length > 0) { | ||
manifest.connections.forEach((connection) => { | ||
server.connection(connection); | ||
}); | ||
} | ||
else { | ||
server.connection(); | ||
} | ||
next(); | ||
}); | ||
steps.push((next) => { | ||
if (options.preRegister) { | ||
options.preRegister(server, next); | ||
} | ||
else { | ||
next(); | ||
} | ||
}); | ||
steps.push((next) => { | ||
// Load registrations | ||
if (manifest.registrations) { | ||
const registrations = manifest.registrations.map((reg) => { | ||
return { | ||
plugin: internals.parsePlugin(reg.plugin, options.relativeTo), | ||
options: reg.options || {} | ||
}; | ||
}); | ||
Items.serial(registrations, (reg, nextRegister) => { | ||
server.register(reg.plugin, reg.options, nextRegister); | ||
}, next); | ||
} | ||
else { | ||
next(); | ||
} | ||
}); | ||
Items.serial(steps, (step, nextStep) => { | ||
step(nextStep); | ||
}, (err) => { | ||
if (err) { | ||
return Hoek.nextTick(callback)(err); | ||
} | ||
Hoek.nextTick(callback)(null, server); | ||
}); | ||
return server; | ||
}; | ||
internals.parseServer = function (server, relativeTo) { | ||
internals.parseServer = function (serverOpts, relativeTo) { | ||
if (server.cache) { | ||
server = Hoek.clone(server); | ||
if (serverOpts.cache) { | ||
serverOpts = Hoek.clone(serverOpts); | ||
const caches = []; | ||
const config = [].concat(server.cache); | ||
const config = [].concat(serverOpts.cache); | ||
for (let i = 0; i < config.length; ++i) { | ||
@@ -169,23 +84,19 @@ let item = config[i]; | ||
server.cache = caches; | ||
serverOpts.cache = caches; | ||
} | ||
return server; | ||
return serverOpts; | ||
}; | ||
internals.parsePlugin = function (plugin, relativeTo) { | ||
plugin = Hoek.cloneWithShallow(plugin, ['options']); | ||
if (typeof plugin === 'string') { | ||
plugin = { register: plugin }; | ||
return internals.requireRelativeTo(plugin, relativeTo); | ||
} | ||
if (typeof plugin.register === 'string') { | ||
let path = plugin.register; | ||
if (relativeTo && path[0] === '.') { | ||
path = Path.join(relativeTo, path); | ||
} | ||
if (typeof plugin.plugin === 'string') { | ||
const pluginObject = Hoek.cloneWithShallow(plugin, ['options']); | ||
pluginObject.plugin = internals.requireRelativeTo(plugin.plugin, relativeTo); | ||
plugin.register = require(path); | ||
return pluginObject; | ||
} | ||
@@ -195,1 +106,10 @@ | ||
}; | ||
internals.requireRelativeTo = function (path, relativeTo) { | ||
if (relativeTo && path[0] === '.') { | ||
path = Path.join(relativeTo, path); | ||
} | ||
return require(path); | ||
}; |
{ | ||
"name": "glue", | ||
"description": "Server composer for hapi.js", | ||
"version": "4.2.1", | ||
"version": "5.0.0", | ||
"repository": { | ||
@@ -18,14 +18,13 @@ "type": "git", | ||
"engines": { | ||
"node": ">=4.0" | ||
"node": ">=8.0" | ||
}, | ||
"dependencies": { | ||
"hapi": "11.x.x || 12.x.x || 13.x.x || 14.x.x || 15.x.x || 16.x.x", | ||
"hoek": "4.x.x", | ||
"items": "2.x.x", | ||
"joi": "10.x.x" | ||
"hapi": "17.x.x", | ||
"hoek": "5.x.x", | ||
"joi": "13.x.x" | ||
}, | ||
"devDependencies": { | ||
"catbox-memory": "2.x.x", | ||
"code": "4.x.x", | ||
"lab": "14.x.x" | ||
"catbox-memory": "3.x.x", | ||
"code": "5.x.x", | ||
"lab": "15.x.x" | ||
}, | ||
@@ -32,0 +31,0 @@ "scripts": { |
@@ -11,5 +11,4 @@ ## glue | ||
* `server = new Hapi.Server(Options)` | ||
* one or more `server.connection(Options)` | ||
* zero or more `server.register(Plugin, Options)` | ||
* `server = Hapi.server(Options)` | ||
* `server.register(Plugins, Options)` | ||
@@ -20,3 +19,3 @@ calling each based on the configuration generated from the Glue manifest. | ||
Glue's [API](API.md) is a single function `compose` accepting a JSON `manifest` file specifying the hapi server options, connections, and registrations. | ||
Glue's [API](API.md) is a single function `compose` accepting a JSON `manifest` specifying the hapi server options and registrations. | ||
@@ -29,2 +28,3 @@ ### hapi version dependency | ||
Glue currently supports hapi **11**, **12**, **13**, **14**, **15**, and **16**. | ||
Glue version 5 currently only supports hapi **17**. | ||
For support of hapi **11**, **12**, **13**, **14**, **15**, or **16** please use Glue@4.2.x . |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
11250
78
+ Addedaccept@3.1.3(transitive)
+ Addedammo@3.0.3(transitive)
+ Addedb64@4.1.2(transitive)
+ Addedbig-time@2.0.1(transitive)
+ Addedboom@7.3.0(transitive)
+ Addedbounce@1.2.3(transitive)
+ Addedcall@5.0.3(transitive)
+ Addedcatbox@10.0.6(transitive)
+ Addedcatbox-memory@3.1.4(transitive)
+ Addedcontent@4.0.6(transitive)
+ Addedcryptiles@4.1.3(transitive)
+ Addedhapi@17.8.5(transitive)
+ Addedheavy@6.1.2(transitive)
+ Addedhoek@5.0.46.1.3(transitive)
+ Addediron@5.0.6(transitive)
+ Addedisemail@3.2.0(transitive)
+ Addedjoi@13.7.014.3.1(transitive)
+ Addedmimos@4.0.2(transitive)
+ Addednigel@3.0.4(transitive)
+ Addedpez@4.0.5(transitive)
+ Addedpodium@3.2.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedshot@4.0.7(transitive)
+ Addedsomever@2.0.0(transitive)
+ Addedstatehood@6.0.9(transitive)
+ Addedsubtext@6.0.12(transitive)
+ Addedteamwork@3.2.0(transitive)
+ Addedtopo@3.0.3(transitive)
+ Addedvise@3.0.2(transitive)
+ Addedwreck@14.2.0(transitive)
- Removeditems@2.x.x
- Removed@hapi/address@1.0.1(transitive)
- Removedaccept@2.2.3(transitive)
- Removedammo@2.1.2(transitive)
- Removedb64@3.1.1(transitive)
- Removedboom@5.3.3(transitive)
- Removedcall@4.1.2(transitive)
- Removedcatbox@7.2.1(transitive)
- Removedcatbox-memory@2.1.1(transitive)
- Removedcontent@3.1.2(transitive)
- Removedcryptiles@3.2.1(transitive)
- Removedhapi@16.8.4(transitive)
- Removedheavy@4.1.1(transitive)
- Removedhoek@4.3.1(transitive)
- Removediron@4.1.1(transitive)
- Removedisemail@2.2.1(transitive)
- Removeditems@2.2.1(transitive)
- Removedjoi@10.6.012.1.1(transitive)
- Removedmimos@3.1.1(transitive)
- Removednigel@2.1.1(transitive)
- Removedpez@2.2.2(transitive)
- Removedpodium@1.4.1(transitive)
- Removedshot@3.5.2(transitive)
- Removedsomever@1.1.1(transitive)
- Removedstatehood@5.1.1(transitive)
- Removedsubtext@5.1.3(transitive)
- Removedtopo@2.1.1(transitive)
- Removedvise@2.1.1(transitive)
- Removedwreck@12.6.2(transitive)
Updatedhapi@17.x.x
Updatedhoek@5.x.x
Updatedjoi@13.x.x