Skip to content

Commit

Permalink
add test, update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Oct 6, 2018
1 parent 1a52bc3 commit c997509
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
53 changes: 35 additions & 18 deletions docs/plugin.md
Expand Up @@ -12,10 +12,10 @@ Hemera's plugin system based on the powerful [Avvio](https://github.com/mcollina

Before we get into the plugin system of hemera you have to install a package called [`hemera-plugin`](https://github.com/hemerajs/hemera/tree/master/packages/hemera-plugin). This package can do some things for you:

* Check the bare-minimum version of Hemera
* Provide consistent interface to register plugins even when the api is changed
* Pass metadata to intialize your plugin with correct dependencies, default options and name
* Skip plugin encapsulation
- Check the bare-minimum version of Hemera
- Provide consistent interface to register plugins even when the api is changed
- Pass metadata to intialize your plugin with correct dependencies, default options and name
- Skip plugin encapsulation

## Create a plugin

Expand All @@ -42,24 +42,27 @@ const myPlugin = hp((hemera, opts, done) => {
module.exports = myPlugin
```

> Plugins are __encapsulated__ "_scoped_" by default. If you define an extension inside it will only effects the actions in your plugin. You can disable it if you set the hemera plugin option `scoped:false`.
> Plugins are **encapsulated** "_scoped_" by default. If you define an extension inside it will only effects the actions in your plugin. You can disable it if you set the hemera plugin option `scoped:false`.
## Break encapsulation

Sometimes it is still useful to write a plugin which effects child as well as sibling scopes. You can archive this with plugin option `scoped: false` property. This approach is used in payload validators `hemera-joi` or authentication `hemera-jwt`.

```js
const hp = require('hemera-plugin')
const myPlugin = hp((hemera, opts, done) => {
const topic = 'math'
const myPlugin = hp(
(hemera, opts, done) => {
const topic = 'math'

hemera.ext('onClientPostRequest', function(ctx, next) {
// some code
next()
})
hemera.ext('onClientPostRequest', function(ctx, next) {
// some code
next()
})

done()
}, { scoped: false })
done()
},
{ scoped: false }
)

hemera.use(myPlugin)
```
Expand Down Expand Up @@ -157,11 +160,11 @@ hemera.use(myPlugin)

### Scoped sensitive settings

* [Request/Response Extensions](extension.md#server-client-lifecycle)
* [Decorators](decorator.md)
* [Schema Compilers](payload-validation.md#use-your-custom-validator)
* [Codecs](codec.md)
* [Not found pattern](notfound-pattern.md)
- [Request/Response Extensions](extension.md#server-client-lifecycle)
- [Decorators](decorator.md)
- [Schema Compilers](payload-validation.md#use-your-custom-validator)
- [Codecs](codec.md)
- [Not found pattern](notfound-pattern.md)

## Add plugin metadata

Expand Down Expand Up @@ -260,3 +263,17 @@ hemera.use(plugin).after(cb)
hemera.use(plugin)
hemera.after(cb)
```

## Plugin timeout

If you need to handle a lot of plugins you could run into timeout issues and those errors are hard to track. In order to find out which plugin caused it you can specify a `pluginTimeout`.
The error contains a property `fn` which contains your plugin functions all associated informations about your plugin.

```js
new Hemera(nats, { pluginTimeout: 100 })
hemera.use(plugin)
hemera.use(err => {
err.fn.name // the name of the plugin function
err.fn[Symbol.for('plugin-meta')] // all plugin informations like name or options
})
```
32 changes: 32 additions & 0 deletions test/hemera/plugin-timeout.spec.js
Expand Up @@ -42,6 +42,38 @@ describe('Plugin timeout', function() {
})
})

it('Should be able to get the name of the plugin which produce the timeout', function(done) {
const nats = require('nats').connect(authUrl)

const hemera = new Hemera(nats, {
pluginTimeout: 100
})

// Plugin
let myPlugin = Hp(
function myPlugin(hemera, options, done) {
// done()
},
{
name: 'myPlugin',
options: { a: 1 }
}
)

hemera.use(myPlugin)

hemera.ready(err => {
expect(err).to.be.exists()
expect(err.code).to.be.equals('ERR_AVVIO_PLUGIN_TIMEOUT')
expect(err.fn.name).to.be.equals('myPlugin')
expect(err.fn[Symbol.for('plugin-meta')]).to.be.equals({
name: 'myPlugin',
options: { a: 1 }
})
hemera.close(done)
})
})

it('Should not produce an error when timeout is zero', function(done) {
const nats = require('nats').connect(authUrl)

Expand Down

0 comments on commit c997509

Please sign in to comment.