@@ -17,8 +17,8 @@ function fastifyRedis (fastify, options, next) {
17
17
return next ( new Error ( `Redis '${ namespace } ' instance namespace has already been registered` ) )
18
18
}
19
19
20
- const closeNamedInstance = ( fastify , done ) => {
21
- fastify . redis [ namespace ] . quit ( done )
20
+ const closeNamedInstance = ( fastify ) => {
21
+ return fastify . redis [ namespace ] . quit ( )
22
22
}
23
23
24
24
if ( ! client ) {
@@ -36,12 +36,9 @@ function fastifyRedis (fastify, options, next) {
36
36
}
37
37
38
38
fastify . redis [ namespace ] = client
39
- if ( options . closeClient === true ) {
40
- fastify . addHook ( 'onClose' , closeNamedInstance )
41
- }
42
39
} else {
43
40
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' ) )
45
42
} else {
46
43
if ( ! client ) {
47
44
try {
@@ -58,67 +55,70 @@ function fastifyRedis (fastify, options, next) {
58
55
}
59
56
60
57
fastify . decorate ( 'redis' , client )
61
- if ( options . closeClient === true ) {
62
- fastify . addHook ( 'onClose' , close )
63
- }
64
58
}
65
59
}
66
60
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 ( )
75
69
76
- const onReady = function ( ) {
77
- client
78
- . off ( 'end' , onEnd )
79
- . off ( 'error' , onError )
80
- . off ( 'ready' , onReady )
70
+ next ( err )
71
+ }
81
72
82
- next ( )
83
- }
73
+ const onReady = function ( ) {
74
+ client
75
+ . off ( 'end' , onEnd )
76
+ . off ( 'error' , onError )
77
+ . off ( 'ready' , onReady )
84
78
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
+ }
96
81
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
109
88
}
110
89
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
+ }
112
99
}
113
100
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
+ }
115
115
}
116
116
117
- function close ( fastify , done ) {
118
- fastify . redis . quit ( done )
117
+ function close ( fastify ) {
118
+ return fastify . redis . quit ( )
119
119
}
120
120
121
121
module . exports = fp ( fastifyRedis , {
122
- fastify : '>=1 .x' ,
123
- name : 'fastify- redis'
122
+ fastify : '4 .x' ,
123
+ name : '@ fastify/ redis'
124
124
} )
0 commit comments