You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fastify-redis-mock

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-redis-mock - npm Package Compare versions

Comparing version

to
1.1.0

.history/CHANGELOG_20210712174015.md

59

index.js

@@ -7,16 +7,55 @@ 'use strict'

function fastifyRedisMock (fastify, options, next) {
var client = options.client || null
const { namespace, url, ...redisOptions } = options
let client = options.client || null
if (!client) {
try {
client = new RedisMock(options)
} catch (err) {
return next(err)
if (namespace) {
if (!fastify.redis) {
fastify.decorate('redis', {})
}
if (fastify.redis[namespace]) {
return next(new Error(`Redis '${namespace}' instance namespace has already been registered`))
}
const closeNamedInstance = (fastify, done) => {
fastify.redis[namespace].quit(done)
}
if (!client) {
try {
client = new RedisMock(redisOptions)
} catch (err) {
return next(err)
}
fastify.addHook('onClose', closeNamedInstance)
}
fastify.redis[namespace] = client
if (options.closeClient === true) {
fastify.addHook('onClose', closeNamedInstance)
}
} else {
if (fastify.redis) {
return next(new Error('fastify-redis has already been registered'))
} else {
if (!client) {
try {
client = new RedisMock(redisOptions)
} catch (err) {
return next(err)
}
fastify.addHook('onClose', close)
}
fastify.decorate('redis', client)
if (options.closeClient === true) {
fastify.addHook('onClose', close)
}
}
}
fastify
.decorate('redis', client)
.addHook('onClose', close)
next()

@@ -23,0 +62,0 @@ }

14

package.json
{
"name": "fastify-redis-mock",
"version": "1.0.0",
"version": "1.1.0",
"description": "Plugin to share a common Redis mock connection across Fastify.",

@@ -28,11 +28,11 @@ "main": "index.js",

"devDependencies": {
"fastify": "^1.9.0",
"standard": "12.0.1",
"tap": "^12.0.1"
"fastify": "^3.18.1",
"standard": "^16.0.3",
"tap": "^15.0.9"
},
"dependencies": {
"fastify-plugin": "^1.2.0",
"ioredis": "^4.0.0",
"ioredis-mock": "^3.14.3"
"fastify-plugin": "^3.0.0",
"ioredis": "^4.27.6",
"ioredis-mock": "^5.6.0"
}
}

@@ -7,3 +7,3 @@ # fastify-redis-mock

Full credit goes to the [Fastify](https://github.com/fastify) team for building [fastify-redis](https://github.com/fastify/fastify-redis), and [stripsan](https://github.com/stipsan) for building [ioredis-mock](https://github.com/stipsan/ioredis-mock).
Full credit goes to the [Fastify](https://github.com/fastify) team for building [fastify-redis](https://github.com/fastify/fastify-redis), and [stipsan](https://github.com/stipsan) for building [ioredis-mock](https://github.com/stipsan/ioredis-mock).

@@ -10,0 +10,0 @@ For documentation, please refer to either the [Fastify-redis docs](https://github.com/fastify/fastify-redis#readme), or [ioredis-mock docs](https://github.com/stipsan/ioredis-mock#readme).

@@ -8,13 +8,48 @@ 'use strict'

t.beforeEach(done => {
t.beforeEach(async () => {
const fastify = Fastify()
fastify.register(fastifyRedis)
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready(err => {
await fastify.ready()
await fastify.redis.flushall()
await fastify.close()
})
test('fastify.redis should exist', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.error(err)
t.ok(fastify.redis)
fastify.redis.flushall(() => {
fastify.close()
done()
fastify.close()
})
})
test('fastify.redis should be the redis client', (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.error(err)
fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
fastify.close()
})
})

@@ -24,10 +59,15 @@ })

test('fastify.redis should exist', t => {
t.plan(2)
test('fastify.redis.test namespace should exist', (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifyRedis)
fastify.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test'
})
fastify.ready(err => {
fastify.ready((err) => {
t.error(err)
t.ok(fastify.redis)
t.ok(fastify.redis.test)

@@ -38,16 +78,19 @@ fastify.close()

test('fastify.redis should be the redis client', t => {
test('fastify.redis.test should be the redis client', (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifyRedis)
fastify.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test'
})
fastify.ready(err => {
fastify.ready((err) => {
t.error(err)
fastify.redis.set('key', 'value', err => {
fastify.redis.test.set('key_namespace', 'value_namespace', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
fastify.redis.test.get('key_namespace', (err, val) => {
t.error(err)
t.equal(val, 'value')
t.equal(val, 'value_namespace')

@@ -60,21 +103,226 @@ fastify.close()

test('promises support', t => {
test('promises support', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifyRedis)
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready(err => {
fastify.ready((err) => {
t.error(err)
fastify.redis.set('key', 'value')
fastify.redis
.set('key', 'value')
.then(() => {
return fastify.redis.get('key')
})
.then(val => {
.then((val) => {
t.equal(val, 'value')
fastify.close()
})
.catch(err => t.fail(err))
.catch((err) => t.fail(err))
})
})
test('custom client', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, { client: redis })
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis, redis)
fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
fastify.close(function (err) {
t.error(err)
fastify.redis.quit(function (err) {
t.error(err)
})
})
})
})
})
})
test('custom client gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, { client: redis, closeClient: true })
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis, redis)
fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
const origQuit = fastify.redis.quit
fastify.redis.quit = (cb) => {
t.pass('redis client closed')
origQuit.call(fastify.redis, cb)
}
fastify.close(function (err) {
t.error(err)
})
})
})
})
})
test('custom client inside a namespace', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, {
namespace: 'test',
client: redis
})
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis.test, redis)
fastify.redis.test.set('key', 'value', (err) => {
t.error(err)
fastify.redis.test.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
fastify.close(function (err) {
t.error(err)
fastify.redis.test.quit(function (err) {
t.error(err)
})
})
})
})
})
})
test('custom client inside a namespace gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const RedisMock = require('ioredis-mock')
const redis = new RedisMock({ host: 'localhost', port: 6379 })
fastify.register(fastifyRedis, {
namespace: 'test',
client: redis,
closeClient: true
})
fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis.test, redis)
fastify.redis.test.set('key', 'value', (err) => {
t.error(err)
fastify.redis.test.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')
const origQuit = fastify.redis.test.quit
fastify.redis.test.quit = (cb) => {
t.pass('redis client closed')
origQuit.call(fastify.redis.test, cb)
}
fastify.close(function (err) {
t.error(err)
})
})
})
})
})
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
t.plan(1)
const namespace = 'test'
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(fastifyRedis, {
host: '127.0.0.1',
namespace
})
.register(fastifyRedis, {
host: '127.0.0.1',
namespace
})
fastify.ready((err) => {
t.equal(err.message, `Redis '${namespace}' instance namespace has already been registered`)
})
})
test('Should throw when trying to register multiple instances without giving a namespace', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(fastifyRedis, {
host: '127.0.0.1'
})
.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready((err) => {
t.equal(err.message, 'fastify-redis has already been registered')
})
})
test('Should not throw within different contexts', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify.register(function (instance, options, next) {
instance.register(fastifyRedis, {
host: '127.0.0.1'
})
next()
})
fastify.register(function (instance, options, next) {
instance
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test1'
})
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'test2'
})
next()
})
fastify.ready((error) => {
t.error(error)
})
})
SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.