Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-accepts-serializer

Package Overview
Dependencies
Maintainers
7
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-accepts-serializer - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0

72

index.js
'use strict'
const fp = require('fastify-plugin')
const Boom = require('boom')
const SerializerManager = require('./SerializerManager')

@@ -15,53 +13,49 @@

fastify.register(require('fastify-accepts'), err => {
/* istanbul ignore next */
if (err) return next(err)
fastify.register(require('fastify-accepts'))
fastify.addHook('preHandler', (request, reply, done) => {
const types = request.types()
let serializer
let type
fastify.addHook('preHandler', (request, reply, done) => {
const types = request.types()
let serializer
let type
if (!reply.store.config.serializer) {
reply.store.config.serializer = {}
}
if (!reply.serializer) {
reply.serializer = {}
}
if (!reply.store.config.serializer.serializerManager) {
reply.store.config.serializer.serializerManager = SerializerManager.expand(reply.store.config.serializer, globalSerializerManager)
}
reply.serializer.serializerManager = SerializerManager.expand(reply.serializer, globalSerializerManager)
const serializerManager = reply.store.config.serializer.serializerManager
const serializerManager = reply.serializer.serializerManager
const s = serializerManager.findSerializer(types)
serializer = s.serializer
type = s.type
const s = serializerManager.findSerializer(types)
serializer = s.serializer
type = s.type
if (!serializer && defaultSerializer) {
serializer = defaultSerializer.serializer
type = defaultSerializer.type
}
if (!serializer && defaultSerializer) {
serializer = defaultSerializer.serializer
type = defaultSerializer.type
}
if (!serializer &&
if (!serializer &&
options.default !== FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE &&
!request.type(FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE)) {
const supportedTypes = serializerManager.getSupportedTypes()
.concat([FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE])
const supportedTypes = serializerManager.getSupportedTypes()
.concat([FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE])
return reply.code(406)
.type(FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE)
.send(Boom.notAcceptable('Allowed: ' + supportedTypes.join(',')))
}
const notAcceptable = new Error('Allowed: ' + supportedTypes.join(','))
return reply.type(FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE).code(406).send(notAcceptable)
}
if (serializer) {
reply.type(type)
reply._serializer = serializer.serializeFunction
}
if (serializer) {
reply.type(type)
reply.serializer(serializer.serializeFunction)
}
done()
})
next()
done()
})
next()
}
module.exports = fp(acceptsSerializerPlugin)
module.exports = fp(acceptsSerializerPlugin, {
fastify: '^2.0.0',
name: 'fastify-accepts-serializer'
})
{
"name": "fastify-accepts-serializer",
"version": "0.2.1",
"version": "1.0.0",
"description": "Serializer according to the accept header",
"main": "index.js",
"scripts": {
"lint": "standard | snazzy",
"unit": "mocha --check-leaks --full-trace --throw-deprecation --allow-uncaught test/*.js",
"test": "npm run lint && npm run unit",
"coverage": "nyc --reporter=lcov --reporter=html --check-coverage --lines 100 npm run unit",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
"lint": "standard --verbose | snazzy",
"unit": "tap test/test.js",
"test": "npm run lint && tap --cov test/test.js"
},

@@ -24,23 +22,15 @@ "repository": {

"devDependencies": {
"coveralls": "^2.13.1",
"fastify": "^0.26.0",
"msgpack5": "^3.5.0",
"fastify": "2.0.0-rc.0",
"msgpack5": "^4.2.1",
"pre-commit": "^1.2.2",
"protobufjs": "^6.8.0",
"shot": "^3.4.2",
"snazzy": "^7.0.0",
"standard": "^10.0.2",
"tap": "^10.7.1",
"yamljs": "^0.3.0",
"mocha": "^3.5.0",
"nyc": "^11.1.0"
"protobufjs": "^6.8.8",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
"tap": "^12.1.0",
"yamljs": "^0.3.0"
},
"peerDependencies": {
"fastify": ">=0.26.0"
},
"dependencies": {
"boom": "^5.2.0",
"fastify-accepts": ">=0.2.0",
"fastify-plugin": "^0.1.0"
"fastify-accepts": ">=0.5.0",
"fastify-plugin": "^1.2.1"
}
}
# fastify-accepts-serializer
[![Build Status](https://travis-ci.org/fastify/fastify-accepts-serializer.svg?branch=master)](https://travis-ci.org/fastify/fastify-accepts-serializer)
[![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-accepts-serializer/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify-accepts-serializer?branch=master)
[![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-accepts-serializer.svg)](https://greenkeeper.io/) [![Build Status](https://travis-ci.org/fastify/fastify-accepts-serializer.svg?branch=master)](https://travis-ci.org/fastify/fastify-accepts-serializer) [![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-accepts-serializer/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify-accepts-serializer?branch=master)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
Serializer according to the `Accept` header
Serializer according to the `Accept` header. Supports Fastify versions ^2.0.0

@@ -8,0 +7,0 @@ ## Install

@@ -30,4 +30,4 @@ 'use strict'

if (serializer.isAble(type)) {
this.cache[types] = {serializer, type}
return {serializer, type}
this.cache[types] = { serializer, type }
return { serializer, type }
}

@@ -47,3 +47,3 @@ }

options.serializers = options.serializers || []
return SerializerManager.expand(options, {serializers: []})
return SerializerManager.expand(options, { serializers: [] })
}

@@ -50,0 +50,0 @@

'use strict'
/* eslint-env node, mocha */
const assert = require('assert')
const t = require('tap')
const test = t.test
const plugin = require('../')
const Fastify = require('fastify')
const protobuf = require('protobufjs')

@@ -17,33 +15,33 @@ const YAML = require('yamljs')

describe('serializer', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
},
{
regex: /^application\/x-protobuf$/,
serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish()
},
{
regex: /^application\/x-msgpack$/,
serializer: body => msgpack.encode(body)
}
]
})
test('serializer', t => {
t.plan(4)
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
const fastify = Fastify()
fastify.get('/request2', function (req, reply) {
reply.send({pippo: 'pluto'})
})
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
},
{
regex: /^application\/x-protobuf$/,
serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish()
},
{
regex: /^application\/x-msgpack$/,
serializer: body => msgpack.encode(body)
}
]
})
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
it('application/yaml -> yaml', done => {
fastify.get('/request2', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
t.test('application/yaml -> yaml', t => {
t.plan(3)
fastify.inject({

@@ -56,11 +54,11 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/yaml')
assert.deepEqual(response.payload, YAML.stringify({pippo: 'pluto'}))
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/yaml')
t.strictDeepEqual(res.payload, YAML.stringify({ pippo: 'pluto' }))
})
})
it('application/x-protobuf -> protobuf', done => {
t.test('application/x-protobuf -> protobuf', t => {
t.plan(3)
fastify.inject({

@@ -73,11 +71,12 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/x-protobuf')
assert.deepEqual(response.payload, AwesomeMessage.encode(AwesomeMessage.create({pippo: 'pluto'})).finish().toString())
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/x-protobuf')
t.strictDeepEqual(res.payload, AwesomeMessage.encode(AwesomeMessage.create({ pippo: 'pluto' })).finish().toString())
})
})
it('application/x-protobuf -> protobuf', done => {
t.test('application/x-protobuf -> protobuf', t => {
t.plan(3)
fastify.inject({

@@ -90,11 +89,12 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/x-protobuf')
assert.deepEqual(response.payload, AwesomeMessage.encode(AwesomeMessage.create({pippo: 'pluto'})).finish().toString())
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/x-protobuf')
t.strictDeepEqual(res.payload, AwesomeMessage.encode(AwesomeMessage.create({ pippo: 'pluto' })).finish().toString())
})
})
it('application/x-msgpack -> msgpack', done => {
t.test('application/x-msgpack -> msgpack', t => {
t.plan(3)
fastify.inject({

@@ -107,7 +107,6 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/x-msgpack')
assert.deepEqual(response.payload, msgpack.encode({pippo: 'pluto'}).toString())
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/x-msgpack')
t.strictDeepEqual(res.payload, msgpack.encode({ pippo: 'pluto' }).toString())
})

@@ -117,21 +116,23 @@ })

describe('serializer - default = undefined', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
]
})
test('serializer - default = undefined', t => {
t.plan(1)
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
const fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
]
})
it('no match -> 406', done => {
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
t.test('no match -> 406', t => {
t.plan(4)
fastify.inject({

@@ -144,6 +145,7 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/json')
assert.deepEqual(response.statusCode, 406)
assert.deepEqual(response.payload, JSON.stringify({
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.strictDeepEqual(res.statusCode, 406)
t.strictDeepEqual(res.payload, JSON.stringify({
statusCode: 406,

@@ -153,4 +155,2 @@ error: 'Not Acceptable',

}))
done()
})

@@ -160,19 +160,18 @@ })

describe('serializer - default = application/json by fastify', () => {
let fastify
test('serializer - default = application/json by fastify', t => {
t.plan(1)
const fastify = Fastify()
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [ ],
default: 'application/json'
})
fastify.register(plugin, {
serializers: [ ],
default: 'application/json'
})
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
it('no match -> json', done => {
t.test('no match -> json', t => {
t.plan(3)
fastify.inject({

@@ -185,7 +184,6 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/json')
assert.deepEqual(response.payload, JSON.stringify({pippo: 'pluto'}))
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.strictDeepEqual(res.payload, JSON.stringify({ pippo: 'pluto' }))
})

@@ -195,22 +193,23 @@ })

describe('serializer - default = application/json by custom', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/json$/,
serializer: body => 'my-custom-string'
}
],
default: 'application/json'
})
test('serializer - default = application/json by custom', t => {
t.plan(1)
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
const fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/json$/,
serializer: () => 'my-custom-string'
}
],
default: 'application/json'
})
it('no match -> json', done => {
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
t.test('no match -> json', t => {
t.plan(3)
fastify.inject({

@@ -223,7 +222,6 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/json')
assert.deepEqual(response.payload, 'my-custom-string')
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/json')
t.strictDeepEqual(res.payload, 'my-custom-string')
})

@@ -233,22 +231,23 @@ })

describe('serializer - default = application/yaml', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
],
default: 'application/yaml'
})
test('serializer - default = application/yaml', t => {
t.plan(1)
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
const fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
],
default: 'application/yaml'
})
it('no match -> yaml', done => {
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
t.test('no match -> yaml', t => {
t.plan(3)
fastify.inject({

@@ -261,7 +260,6 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/yaml')
assert.deepEqual(response.payload, YAML.stringify({pippo: 'pluto'}))
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/yaml')
t.strictDeepEqual(res.payload, YAML.stringify({ pippo: 'pluto' }))
})

@@ -271,37 +269,33 @@ })

describe('serializer per route', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
],
default: 'application/yaml'
})
test('serializer per route', t => {
t.plan(2)
const config = {
serializer: {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => 'my-custom-string'
},
{
regex: /^application\/x-msgpack$/,
serializer: body => 'my-custom-string-msgpack'
}
]
const fastify = Fastify()
fastify.register(plugin, {
serializers: [
{
regex: /^application\/yaml$/,
serializer: body => YAML.stringify(body)
}
}
],
default: 'application/yaml'
})
fastify.get('/request', { config }, function (req, reply) {
reply.send({pippo: 'pluto'})
})
fastify.get('/request', function (req, reply) {
reply
.serializer(_ => 'my-custom-string')
.send({ pippo: 'pluto' })
})
it('overwrite', done => {
fastify.get('/request2', function (req, reply) {
reply
.type('application/x-msgpack')
.serializer(_ => 'my-custom-string-msgpack')
.send({ pippo: 'pluto' })
})
t.test('overwrite', t => {
t.plan(3)
fastify.inject({

@@ -314,14 +308,15 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/yaml')
assert.deepEqual(response.payload, 'my-custom-string')
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/yaml')
t.strictDeepEqual(res.payload, 'my-custom-string')
})
})
it('not defined globally', done => {
t.test('not defined globally', t => {
t.plan(3)
fastify.inject({
method: 'GET',
url: '/request',
url: '/request2',
payload: {},

@@ -331,7 +326,6 @@ headers: {

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/x-msgpack')
assert.deepEqual(response.payload, 'my-custom-string-msgpack')
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/x-msgpack')
t.strictDeepEqual(res.payload, 'my-custom-string-msgpack')
})

@@ -341,14 +335,16 @@ })

describe('serializer without conf', () => {
let fastify
before('load fastify', () => {
fastify = Fastify()
fastify.register(plugin)
test('serializer without conf', t => {
t.plan(2)
fastify.get('/request', function (req, reply) {
reply.send({pippo: 'pluto'})
})
const fastify = Fastify()
fastify.register(plugin)
fastify.get('/request', function (req, reply) {
reply.send({ pippo: 'pluto' })
})
it('application/json -> json', done => {
t.test('application/json -> json', t => {
t.plan(3)
fastify.inject({

@@ -361,11 +357,12 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/json')
assert.deepEqual(response.payload, JSON.stringify({pippo: 'pluto'}))
done()
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.strictDeepEqual(res.payload, JSON.stringify({ pippo: 'pluto' }))
})
})
it('application/yaml -> 406', done => {
t.test('application/yaml -> 406', t => {
t.plan(4)
fastify.inject({

@@ -378,6 +375,7 @@ method: 'GET',

}
}, response => {
assert.deepEqual(response.headers['content-type'], 'application/json')
assert.deepEqual(response.statusCode, 406)
assert.deepEqual(response.payload, JSON.stringify({
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.strictDeepEqual(res.statusCode, 406)
t.strictDeepEqual(res.payload, JSON.stringify({
statusCode: 406,

@@ -387,6 +385,4 @@ error: 'Not Acceptable',

}))
done()
})
})
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc