-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (38 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict'
const fp = require('fastify-plugin')
const Nats = require('nats')
const Hemera = require('nats-hemera')
function fastifyHemera(fastify, opts, next) {
const hemera = new Hemera(opts.natsInstance || Nats.connect(opts.nats), opts.hemera)
hemera.ext('onError', (error) => fastify.log.error(error))
if (opts.plugins && opts.plugins.length) {
opts.plugins.forEach((p) => {
if (p.plugin) {
hemera.use(p.plugin, p.options)
} else {
hemera.use(p)
}
})
}
fastify.addHook('onClose', (instance, done) => {
fastify.log.debug('closing hemera...')
hemera.close(() => {
fastify.log.debug('hemera closed!')
done()
})
})
fastify.decorate('hemera', hemera)
fastify.decorateRequest('hemera', hemera)
fastify.decorateReply('add', hemera.add.bind(hemera))
fastify.decorateReply('act', function act(pattern) {
return hemera
.act(pattern)
.then((resp) => this.send(resp.data))
.catch((err) => this.send(err))
})
hemera.ready(next)
}
module.exports = fp(fastifyHemera, {
fastify: '>=3.0.0',
name: 'fastify-hemera'
})