New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/redis

Package Overview
Dependencies
Maintainers
18
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/redis - npm Package Compare versions

Comparing version 5.0.0 to 6.0.0

106

index.js

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

const closeNamedInstance = (fastify, done) => {
fastify.redis[namespace].quit(done)
const closeNamedInstance = (fastify) => {
return fastify.redis[namespace].quit()
}

@@ -40,8 +40,5 @@

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'))
return next(new Error('@fastify/redis has already been registered'))
} else {

@@ -63,65 +60,68 @@ if (!client) {

fastify.decorate('redis', client)
if (options.closeClient === true) {
fastify.addHook('onClose', close)
}
}
}
if (!redisOptions.lazyConnect) {
const onEnd = function (err) {
client
.off('ready', onReady)
.off('error', onError)
.off('end', onEnd)
.quit(() => next(err))
}
// Testing this make the process crash on latest TAP :(
/* istanbul ignore next */
const onEnd = function (err) {
client
.off('ready', onReady)
.off('error', onError)
.off('end', onEnd)
.quit()
const onReady = function () {
client
.off('end', onEnd)
.off('error', onError)
.off('ready', onReady)
next(err)
}
next()
}
const onReady = function () {
client
.off('end', onEnd)
.off('error', onError)
.off('ready', onReady)
const onError = function (err) {
// Swallow network errors to allow ioredis
// to perform reconnection and emit 'end'
// event if reconnection eventually
// fails.
// Any other errors during startup will
// trigger the 'end' event.
if (err instanceof Redis.ReplyError) {
onEnd(err)
}
next()
}
// Testing this make the process crash on latest TAP :(
/* istanbul ignore next */
const onError = function (err) {
if (err.code === 'ENOTFOUND') {
onEnd(err)
return
}
// node-redis provides the connection-ready state in a .ready property,
// whereas ioredis provides it in a .status property
if (client.ready === true || client.status === 'ready') {
// client is already connected, do not register event handlers
// call next() directly to avoid ERR_AVVIO_PLUGIN_TIMEOUT
next()
} else {
// ready event can still be emitted
client
.on('end', onEnd)
.on('error', onError)
.on('ready', onReady)
// Swallow network errors to allow ioredis
// to perform reconnection and emit 'end'
// event if reconnection eventually
// fails.
// Any other errors during startup will
// trigger the 'end' event.
if (err instanceof Redis.ReplyError) {
onEnd(err)
}
}
return
// ioredis provides it in a .status property
if (client.status === 'ready') {
// client is already connected, do not register event handlers
// call next() directly to avoid ERR_AVVIO_PLUGIN_TIMEOUT
next()
} else {
// ready event can still be emitted
client
.on('end', onEnd)
.on('error', onError)
.on('ready', onReady)
client.ping()
}
next()
}
function close (fastify, done) {
fastify.redis.quit(done)
function close (fastify) {
return fastify.redis.quit()
}
module.exports = fp(fastifyRedis, {
fastify: '>=1.x',
name: 'fastify-redis'
fastify: '4.x',
name: '@fastify/redis'
})
{
"name": "@fastify/redis",
"version": "5.0.0",
"version": "6.0.0",
"description": "Plugin to share a common Redis connection across Fastify.",

@@ -10,3 +10,3 @@ "main": "index.js",

"lint:fix": "standard --fix",
"redis": "docker run -p 6379:6379 --rm redis:5",
"redis": "docker run -p 6379:6379 --rm redis",
"test": "npm run lint && npm run unit && npm run typescript",

@@ -39,12 +39,12 @@ "typescript": "tsd",

"@types/node": "^17.0.0",
"fastify": "^3.21.6",
"fastify": "^4.0.0-rc.2",
"proxyquire": "^2.1.3",
"redis": "^3.1.2",
"standard": "^17.0.0",
"tap": "^16.0.0",
"tsd": "^0.20.0"
"tsd": "^0.20.0",
"why-is-node-running": "^2.2.2"
},
"dependencies": {
"fastify-plugin": "^3.0.0",
"ioredis": "^4.27.9"
"ioredis": "^5.0.0"
},

@@ -51,0 +51,0 @@ "tsd": {

@@ -1,5 +0,5 @@

# fastify-redis
# @fastify/redis
![CI](https://github.com/fastify/fastify-redis/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/fastify-redis.svg?style=flat)](https://www.npmjs.com/package/fastify-redis)
[![NPM version](https://img.shields.io/npm/v/@fastify/redis.svg?style=flat)](https://www.npmjs.com/package/@fastify/redis)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-redis/badge.svg)](https://snyk.io/test/github/fastify/fastify-redis)

@@ -13,3 +13,3 @@ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

```
npm i fastify-redis --save
npm i @fastify/redis --save
```

@@ -29,9 +29,9 @@

// create by specifying host
fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
fastify.register(require('@fastify/redis'), { host: '127.0.0.1' })
// OR by specifying Redis URL
fastify.register(require('fastify-redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
fastify.register(require('@fastify/redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
// OR with more options
fastify.register(require('fastify-redis'), {
fastify.register(require('@fastify/redis'), {
host: '127.0.0.1',

@@ -54,3 +54,3 @@ password: '***',

const Fastify = require('fastify')
const fastifyRedis = require('fastify-redis')
const fastifyRedis = require('@fastify/redis')

@@ -97,18 +97,12 @@ const fastify = Fastify({ logger: true })

const fastify = require('fastify')()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const Redis = require('ioredis')
fastify.register(require('fastify-redis'), { client: redis })
const client = new Redis({ host: 'localhost', port: 6379 })
fastify.register(require('@fastify/redis'), { client })
```
Note: by default, *fastify-redis* will **not** automatically close the client
connection when the Fastify server shuts down. To opt-in to this behavior,
register the client like so:
Note: by default, *@fastify/redis* will **not** automatically close the client
connection when the Fastify server shuts down.
```js
fastify.register(require('fastify-redis'), {
client: redis,
closeClient: true
})
```
## Registering multiple Redis client instances

@@ -122,6 +116,5 @@

const fastify = require('fastify')()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
fastify
.register(require('fastify-redis'), {
.register(require('@fastify/redis'), {
host: '127.0.0.1',

@@ -131,3 +124,3 @@ port: 6380,

})
.register(require('fastify-redis'), {
.register(require('@fastify/redis'), {
client: redis,

@@ -182,3 +175,3 @@ namespace: 'world'

`fastify-redis` supports Redis streams out of the box.
`@fastify/redis` supports Redis streams out of the box.

@@ -190,3 +183,3 @@ ```js

fastify.register(require('fastify-redis'), {
fastify.register(require('@fastify/redis'), {
host: '127.0.0.1',

@@ -193,0 +186,0 @@ port: 6380

'use strict'
const whyIsNodeRunning = require('why-is-node-running')
const t = require('tap')

@@ -9,31 +10,2 @@ const proxyquire = require('proxyquire')

const TEST_PASSWORD = 'my_secret_password'
const setRedisPassword = async (password) => {
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
await fastify.ready()
await fastify.redis.flushall()
await fastify.redis.config(['set', 'requirepass', password])
await fastify.close()
}
const unsetRedisPassword = async (currentPassword) => {
const fastify = Fastify()
fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: currentPassword
})
await fastify.ready()
await fastify.redis.flushall()
await fastify.redis.config(['set', 'requirepass', ''])
await fastify.close()
}
t.beforeEach(async () => {

@@ -85,2 +57,3 @@ const fastify = Fastify()

}
this.status = 'ready'
this.off = function () { return this }

@@ -192,30 +165,2 @@

test('custom client', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ 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 ioredis client that is already connected', (t) => {

@@ -262,6 +207,7 @@ t.plan(10)

test('custom redis client that is already connected', (t) => {
test('custom ioredis client that is already connected', (t) => {
t.plan(10)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const Redis = require('ioredis')
const redis = new Redis({ host: 'localhost', port: 6379 })

@@ -277,3 +223,3 @@ // use the client now, so that it is connected and ready

client: redis,
lazyConnect: false
namespace: 'foo'
})

@@ -283,7 +229,7 @@

t.error(err)
t.equal(fastify.redis, redis)
t.equal(fastify.redis.foo, redis)
fastify.redis.set('key2', 'value2', (err) => {
fastify.redis.foo.set('key2', 'value2', (err) => {
t.error(err)
fastify.redis.get('key2', (err, val) => {
fastify.redis.foo.get('key2', (err, val) => {
t.error(err)

@@ -294,3 +240,3 @@ t.equal(val, 'value2')

t.error(err)
fastify.redis.quit(function (err) {
fastify.redis.foo.quit(function (err) {
t.error(err)

@@ -306,99 +252,2 @@ })

test('custom client gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ 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 redis = require('redis').createClient({ 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 redis = require('redis').createClient({ 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) => {

@@ -442,3 +291,3 @@ t.plan(1)

fastify.ready((err) => {
t.equal(err.message, 'fastify-redis has already been registered')
t.equal(err.message, '@fastify/redis has already been registered')
})

@@ -478,3 +327,4 @@ })

test('Should throw when trying to connect on an invalid host', (t) => {
// Skipped because it makes TAP crash
test('Should throw when trying to connect on an invalid host', { skip: true }, (t) => {
t.plan(1)

@@ -495,62 +345,2 @@

test('Should not throw when trying to connect on an invalid host but the lazyConnect option has been provided', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(fastifyRedis, {
host: 'invalid_host',
lazyConnect: true
})
fastify.ready((err) => {
t.error(err)
})
})
test('Should throw authentication error when trying to connect on a valid host with a wrong password', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(async () => {
fastify.close()
await unsetRedisPassword(TEST_PASSWORD)
})
setRedisPassword(TEST_PASSWORD)
.then(_ => {
fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: 'my_wrong_secret_password'
})
fastify.ready(err => {
t.ok(err)
})
})
})
test('Should throw authentication error when trying to connect on a valid host without a password', (t) => {
t.plan(1)
const fastify = Fastify()
t.teardown(async () => {
fastify.close()
await unsetRedisPassword(TEST_PASSWORD)
})
setRedisPassword(TEST_PASSWORD)
.then(_ => {
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
fastify.ready(err => {
t.ok(err)
})
})
})
test('Should successfully create a Redis client when registered with a `url` option and without a `client` option in a namespaced instance', async t => {

@@ -572,3 +362,3 @@ t.plan(2)

test('Should be able to register multiple namespaced fastify-redis instances', async t => {
test('Should be able to register multiple namespaced @fastify/redis instances', async t => {
t.plan(3)

@@ -595,3 +385,3 @@

test('Should throw when fastify-redis is initialized with an option that makes Redis throw', (t) => {
test('Should throw when @fastify/redis is initialized with an option that makes Redis throw', (t) => {
t.plan(1)

@@ -612,3 +402,3 @@

test('Should throw when fastify-redis is initialized with a namespace and an option that makes Redis throw', (t) => {
test('Should throw when @fastify/redis is initialized with a namespace and an option that makes Redis throw', (t) => {
t.plan(1)

@@ -629,1 +419,5 @@

})
setInterval(() => {
whyIsNodeRunning()
}, 5000).unref()

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