-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (35 loc) · 799 Bytes
/
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
'use strict'
const fp = require('fastify-plugin')
const msgpack = require('@msgpack/msgpack')
/**
*
* @param {FastifyInstance} fastify
* @param {*} options
* @param {*} next
*/
function fastifyMsgpack (fastify, options, next) {
fastify.register(require('fastify-accepts-serializer'), {
serializers: [
{
regex: /^application\/x-msgpack$/,
serializer: body => Buffer.from(msgpack.encode(body))
}
],
default: 'application/json'
})
fastify.addContentTypeParser('application/x-msgpack', {
parseAs: 'buffer'
}, (req, body, done) => {
try {
const res = msgpack.decode(body)
return res
} catch (err) {
done(err)
}
})
next()
}
module.exports = fp(fastifyMsgpack, {
fastify: '3.x',
name: 'fastify-msgpack'
})