Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache #31

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion SerializerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ class Serializer {
class SerializerManager {
constructor (configuration) {
this.serializers = configuration.serializers
this.cache = configuration.cache
}

findSerializer (types) {
const cacheValue = this.cache[types]

if (cacheValue) return cacheValue
Eomm marked this conversation as resolved.
Show resolved Hide resolved

for (let i = 0; i < types.length; i++) {
const type = types[i]

for (let j = 0; j < this.serializers.length; j++) {
const serializer = this.serializers[j]
if (serializer.isAble(type)) {
this.cache[types] = { serializer, type }
return { serializer, type }
}
}
Expand All @@ -45,7 +51,8 @@ SerializerManager.expand = function (options, fallbackSerializer) {

const serializers = options.serializers.map(c => new Serializer(c))
return new SerializerManager({
serializers: serializers.concat(fallbackSerializer.serializers)
serializers: serializers.concat(fallbackSerializer.serializers),
cache: options.cache
})
}

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const SerializerManager = require('./SerializerManager')
const FASTIFY_DEFAULT_SERIALIZE_MIME_TYPE = 'application/json'

function acceptsSerializerPlugin (fastify, options, next) {
const serializerCache = {}
options.cache = serializerCache

const globalSerializerManager = SerializerManager.build(options)

const defaultSerializer = globalSerializerManager.findSerializer([options.default])
Expand All @@ -17,6 +20,7 @@ function acceptsSerializerPlugin (fastify, options, next) {
let serializer
let type

reply.serializer.cache = serializerCache
reply.serializer.serializerManager = SerializerManager.expand(reply.serializer, globalSerializerManager)

const serializerManager = reply.serializer.serializerManager
Expand Down
51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,54 @@ test('serializer without conf', t => {
})
})
})

test('serializer cache', t => {
t.plan(1)

const fastify = Fastify()

fastify.register(plugin, {
serializers: [
{
regex: /^application\/cache$/,
serializer: (body) => body
}
]
})

t.test('it shoud populate cache', t => {
t.plan(8)

fastify.get('/request', function (req, reply) {
t.strictDeepEqual(Object.keys(reply.serializer.cache), ['application/cache'])

reply.send('cache')
})

fastify.inject({
method: 'GET',
url: '/request',
payload: {},
headers: {
accept: 'application/cache'
}
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/cache')
t.strictDeepEqual(res.payload, 'cache')
})

fastify.inject({
method: 'GET',
url: '/request',
payload: {},
headers: {
accept: 'application/cache'
}
}, (err, res) => {
t.error(err)
t.strictDeepEqual(res.headers['content-type'], 'application/cache')
t.strictDeepEqual(res.payload, 'cache')
})
})
})