-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
134 lines (113 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict'
const fp = require('fastify-plugin')
const Redis = require('ioredis')
function fastifyRedis (fastify, options, next) {
const { namespace, url, closeClient = false, ...redisOptions } = options
let client = options.client || null
if (namespace) {
if (!fastify.redis) {
fastify.decorate('redis', Object.create(null))
}
if (fastify.redis[namespace]) {
return next(new Error(`Redis '${namespace}' instance namespace has already been registered`))
}
const closeNamedInstance = (fastify) => fastify.redis[namespace].quit()
if (client) {
if (closeClient === true) {
fastify.addHook('onClose', closeNamedInstance)
}
} else {
try {
if (url) {
client = new Redis(url, redisOptions)
} else {
client = new Redis(redisOptions)
}
} catch (err) {
return next(err)
}
fastify.addHook('onClose', closeNamedInstance)
}
fastify.redis[namespace] = client
} else {
if (fastify.redis) {
return next(new Error('@fastify/redis has already been registered'))
} else {
if (client) {
if (closeClient === true) {
fastify.addHook('onClose', close)
}
} else {
try {
if (url) {
client = new Redis(url, redisOptions)
} else {
client = new Redis(redisOptions)
}
} catch (err) {
return next(err)
}
fastify.addHook('onClose', close)
}
fastify.decorate('redis', client)
}
}
// Testing this make the process crash on latest TAP :(
/* c8 ignore start */
const onEnd = function (err) {
client
.off('ready', onReady)
.off('error', onError)
.off('end', onEnd)
.quit()
next(err)
}
/* c8 ignore stop */
const onReady = function () {
client
.off('end', onEnd)
.off('error', onError)
.off('ready', onReady)
next()
}
// Testing this make the process crash on latest TAP :(
/* c8 ignore start */
const onError = function (err) {
if (err.code === 'ENOTFOUND') {
onEnd(err)
return
}
// 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)
}
}
/* c8 ignore stop */
// 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().catch(onError)
}
}
function close (fastify) {
return fastify.redis.quit()
}
module.exports = fp(fastifyRedis, {
fastify: '5.x',
name: '@fastify/redis'
})
module.exports.default = fastifyRedis
module.exports.fastifyRedis = fastifyRedis