Skip to content

Commit dbded11

Browse files
authored
Updated Fastify to v4. Dropped redis module support (#147)
* Updated Fastify to v4. Dropped redis module support Signed-off-by: Matteo Collina <[email protected]> * drop old node Signed-off-by: Matteo Collina <[email protected]> * fix * fixup * simplify module and test 100% Signed-off-by: Matteo Collina <[email protected]>
1 parent f56f77d commit dbded11

File tree

5 files changed

+85
-298
lines changed

5 files changed

+85
-298
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ jobs:
1414
strategy:
1515
matrix:
1616
node-version:
17-
- 10
18-
- 12
1917
- 14
2018
- 16
19+
- 18
2120
redis-tag:
2221
- 5
2322
- 6
23+
- 7
2424
services:
2525
redis:
2626
image: redis:${{ matrix.redis-tag }}

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,15 @@ closed.
9191
'use strict'
9292

9393
const fastify = require('fastify')()
94-
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
94+
const Redis = require('ioredis')
9595

96-
fastify.register(require('@fastify/redis'), { client: redis })
96+
const client = new Redis({ host: 'localhost', port: 6379 })
97+
98+
fastify.register(require('@fastify/redis'), { client })
9799
```
98100

99101
Note: by default, *@fastify/redis* will **not** automatically close the client
100-
connection when the Fastify server shuts down. To opt-in to this behavior,
101-
register the client like so:
102-
103-
```js
104-
fastify.register(require('@fastify/redis'), {
105-
client: redis,
106-
closeClient: true
107-
})
108-
```
102+
connection when the Fastify server shuts down.
109103

110104
## Registering multiple Redis client instances
111105

@@ -115,7 +109,6 @@ By using the `namespace` option you can register multiple Redis client instances
115109
'use strict'
116110

117111
const fastify = require('fastify')()
118-
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
119112

120113
fastify
121114
.register(require('@fastify/redis'), {

index.js

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function fastifyRedis (fastify, options, next) {
1717
return next(new Error(`Redis '${namespace}' instance namespace has already been registered`))
1818
}
1919

20-
const closeNamedInstance = (fastify, done) => {
21-
fastify.redis[namespace].quit(done)
20+
const closeNamedInstance = (fastify) => {
21+
return fastify.redis[namespace].quit()
2222
}
2323

2424
if (!client) {
@@ -36,12 +36,9 @@ function fastifyRedis (fastify, options, next) {
3636
}
3737

3838
fastify.redis[namespace] = client
39-
if (options.closeClient === true) {
40-
fastify.addHook('onClose', closeNamedInstance)
41-
}
4239
} else {
4340
if (fastify.redis) {
44-
return next(new Error('fastify-redis has already been registered'))
41+
return next(new Error('@fastify/redis has already been registered'))
4542
} else {
4643
if (!client) {
4744
try {
@@ -58,67 +55,70 @@ function fastifyRedis (fastify, options, next) {
5855
}
5956

6057
fastify.decorate('redis', client)
61-
if (options.closeClient === true) {
62-
fastify.addHook('onClose', close)
63-
}
6458
}
6559
}
6660

67-
if (!redisOptions.lazyConnect) {
68-
const onEnd = function (err) {
69-
client
70-
.off('ready', onReady)
71-
.off('error', onError)
72-
.off('end', onEnd)
73-
.quit(() => next(err))
74-
}
61+
// Testing this make the process crash on latest TAP :(
62+
/* istanbul ignore next */
63+
const onEnd = function (err) {
64+
client
65+
.off('ready', onReady)
66+
.off('error', onError)
67+
.off('end', onEnd)
68+
.quit()
7569

76-
const onReady = function () {
77-
client
78-
.off('end', onEnd)
79-
.off('error', onError)
80-
.off('ready', onReady)
70+
next(err)
71+
}
8172

82-
next()
83-
}
73+
const onReady = function () {
74+
client
75+
.off('end', onEnd)
76+
.off('error', onError)
77+
.off('ready', onReady)
8478

85-
const onError = function (err) {
86-
// Swallow network errors to allow ioredis
87-
// to perform reconnection and emit 'end'
88-
// event if reconnection eventually
89-
// fails.
90-
// Any other errors during startup will
91-
// trigger the 'end' event.
92-
if (err instanceof Redis.ReplyError) {
93-
onEnd(err)
94-
}
95-
}
79+
next()
80+
}
9681

97-
// node-redis provides the connection-ready state in a .ready property,
98-
// whereas ioredis provides it in a .status property
99-
if (client.ready === true || client.status === 'ready') {
100-
// client is already connected, do not register event handlers
101-
// call next() directly to avoid ERR_AVVIO_PLUGIN_TIMEOUT
102-
next()
103-
} else {
104-
// ready event can still be emitted
105-
client
106-
.on('end', onEnd)
107-
.on('error', onError)
108-
.on('ready', onReady)
82+
// Testing this make the process crash on latest TAP :(
83+
/* istanbul ignore next */
84+
const onError = function (err) {
85+
if (err.code === 'ENOTFOUND') {
86+
onEnd(err)
87+
return
10988
}
11089

111-
return
90+
// Swallow network errors to allow ioredis
91+
// to perform reconnection and emit 'end'
92+
// event if reconnection eventually
93+
// fails.
94+
// Any other errors during startup will
95+
// trigger the 'end' event.
96+
if (err instanceof Redis.ReplyError) {
97+
onEnd(err)
98+
}
11299
}
113100

114-
next()
101+
// ioredis provides it in a .status property
102+
if (client.status === 'ready') {
103+
// client is already connected, do not register event handlers
104+
// call next() directly to avoid ERR_AVVIO_PLUGIN_TIMEOUT
105+
next()
106+
} else {
107+
// ready event can still be emitted
108+
client
109+
.on('end', onEnd)
110+
.on('error', onError)
111+
.on('ready', onReady)
112+
113+
client.ping()
114+
}
115115
}
116116

117-
function close (fastify, done) {
118-
fastify.redis.quit(done)
117+
function close (fastify) {
118+
return fastify.redis.quit()
119119
}
120120

121121
module.exports = fp(fastifyRedis, {
122-
fastify: '>=1.x',
123-
name: 'fastify-redis'
122+
fastify: '4.x',
123+
name: '@fastify/redis'
124124
})

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"lint": "standard",
99
"lint:fix": "standard --fix",
10-
"redis": "docker run -p 6379:6379 --rm redis:5",
10+
"redis": "docker run -p 6379:6379 --rm redis",
1111
"test": "npm run lint && npm run unit && npm run typescript",
1212
"typescript": "tsd",
1313
"unit": "tap test/test.js",
@@ -35,16 +35,16 @@
3535
"devDependencies": {
3636
"@types/ioredis": "^4.27.4",
3737
"@types/node": "^17.0.0",
38-
"fastify": "^3.21.6",
38+
"fastify": "^4.0.0-rc.2",
3939
"proxyquire": "^2.1.3",
40-
"redis": "^3.1.2",
4140
"standard": "^17.0.0",
4241
"tap": "^16.0.0",
43-
"tsd": "^0.20.0"
42+
"tsd": "^0.20.0",
43+
"why-is-node-running": "^2.2.2"
4444
},
4545
"dependencies": {
4646
"fastify-plugin": "^3.0.0",
47-
"ioredis": "^4.27.9"
47+
"ioredis": "^5.0.0"
4848
},
4949
"tsd": {
5050
"directory": "test/types"

0 commit comments

Comments
 (0)