@redkern/node-red-redis 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +188 -0
- package/README.md +283 -2
- package/SECURITY.md +11 -0
- package/examples/commands-and-pubsub.json +15 -0
- package/examples/streams-at-least-once.json +23 -0
- package/lib/completion.js +79 -0
- package/lib/control.js +59 -0
- package/lib/denylist.js +118 -0
- package/lib/dlq.js +49 -0
- package/lib/format.js +122 -0
- package/lib/inflight.js +131 -0
- package/lib/list-consumer.js +125 -0
- package/lib/metrics.js +85 -0
- package/lib/node-helpers.js +77 -0
- package/lib/palette.js +168 -0
- package/lib/slot.js +33 -0
- package/lib/stream-consumer.js +669 -0
- package/nodes/icons/redkern-redis-command.svg +4 -0
- package/nodes/icons/redkern-redis-list-in.svg +4 -0
- package/nodes/icons/redkern-redis-multi.svg +4 -0
- package/nodes/icons/redkern-redis-scan.svg +4 -0
- package/nodes/icons/redkern-redis-stream-ack.svg +4 -0
- package/nodes/icons/redkern-redis-stream-claim.svg +4 -0
- package/nodes/icons/redkern-redis-stream-control.svg +4 -0
- package/nodes/icons/redkern-redis-stream-gc.svg +4 -0
- package/nodes/icons/redkern-redis-stream-in.svg +4 -0
- package/nodes/icons/redkern-redis-stream-metrics.svg +4 -0
- package/nodes/icons/redkern-redis-stream-out.svg +4 -0
- package/nodes/icons/redkern-redis-subscribe.svg +4 -0
- package/nodes/icons/redkern-redis.svg +4 -0
- package/nodes/locales/en-US/redkern-redis.html +299 -0
- package/nodes/locales/en-US/redkern-redis.json +308 -0
- package/nodes/redkern-redis-command.js +49 -0
- package/nodes/redkern-redis-config.js +273 -0
- package/nodes/redkern-redis-list-in.js +88 -0
- package/nodes/redkern-redis-multi.js +75 -0
- package/nodes/redkern-redis-scan.js +105 -0
- package/nodes/redkern-redis-stream-ack.js +66 -0
- package/nodes/redkern-redis-stream-claim.js +60 -0
- package/nodes/redkern-redis-stream-control.js +35 -0
- package/nodes/redkern-redis-stream-gc.js +77 -0
- package/nodes/redkern-redis-stream-in.js +155 -0
- package/nodes/redkern-redis-stream-metrics.js +85 -0
- package/nodes/redkern-redis-stream-out.js +55 -0
- package/nodes/redkern-redis-subscribe.js +51 -0
- package/nodes/redkern-redis.html +1126 -0
- package/nodes/redkern-redis.js +31 -0
- package/package.json +76 -4
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
{
|
|
2
|
+
"common": {
|
|
3
|
+
"tabs": {
|
|
4
|
+
"general": "General",
|
|
5
|
+
"reliability": "Reliability",
|
|
6
|
+
"advanced": "Advanced"
|
|
7
|
+
},
|
|
8
|
+
"label": {
|
|
9
|
+
"name": "Name",
|
|
10
|
+
"server": "Redis",
|
|
11
|
+
"concurrency": "Concurrency",
|
|
12
|
+
"maxQueue": "Max queue",
|
|
13
|
+
"errorOutput": "Send errors to a separate output",
|
|
14
|
+
"legacyMsgFields": "Legacy msg fields",
|
|
15
|
+
"stream": "Stream",
|
|
16
|
+
"group": "Group",
|
|
17
|
+
"consumer": "Consumer"
|
|
18
|
+
},
|
|
19
|
+
"tip": {
|
|
20
|
+
"concurrency": "Messages handled at the same time. With more than 1 the order of results is not guaranteed; set 1 when order matters.",
|
|
21
|
+
"errorOutput": "Errors leave through the last output labelled error instead of Catch. Use it for high-rate flows, where a Redis outage would otherwise flood the logs.",
|
|
22
|
+
"legacyMsgFields": "Also writes the fields of @yroshcha/node-red-contrib-redis-full (msg.streamId and others). Turn it on for a migrated node when the flow still reads those fields; keep it off for new nodes.",
|
|
23
|
+
"env": "Can be ${VAR}: the value is taken from the environment and the flow in Git shows where it comes from."
|
|
24
|
+
},
|
|
25
|
+
"ports": {
|
|
26
|
+
"input": "message",
|
|
27
|
+
"error": "error"
|
|
28
|
+
},
|
|
29
|
+
"placeholder": {
|
|
30
|
+
"optional": "optional",
|
|
31
|
+
"fromMsg": "from msg",
|
|
32
|
+
"consumer": "pod name and node id"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"config": {
|
|
36
|
+
"paletteLabel": "redis",
|
|
37
|
+
"default": "redis",
|
|
38
|
+
"label": {
|
|
39
|
+
"mode": "Mode",
|
|
40
|
+
"host": "Host",
|
|
41
|
+
"port": "Port",
|
|
42
|
+
"clusterNodes": "Cluster nodes",
|
|
43
|
+
"db": "Database",
|
|
44
|
+
"username": "Username",
|
|
45
|
+
"password": "Password",
|
|
46
|
+
"tls": "Use TLS",
|
|
47
|
+
"tlsServername": "TLS server name",
|
|
48
|
+
"connectTimeout": "Connect timeout (ms)",
|
|
49
|
+
"commandTimeout": "Command timeout (ms)",
|
|
50
|
+
"connections": "Connections"
|
|
51
|
+
},
|
|
52
|
+
"mode": {
|
|
53
|
+
"standalone": "standalone",
|
|
54
|
+
"cluster": "cluster"
|
|
55
|
+
},
|
|
56
|
+
"placeholder": {
|
|
57
|
+
"clusterNodes": "host:port, host:port (empty: host and port above)",
|
|
58
|
+
"tlsServername": "defaults to the host"
|
|
59
|
+
},
|
|
60
|
+
"tip": {
|
|
61
|
+
"cluster": "Cluster uses database 0. Streams with a DLQ need a hash tag, for example orders:{eu} and orders:{eu}:dlq.",
|
|
62
|
+
"connections": "Per pod: 1 shared connection, 1 subscriber with Pub/Sub nodes and 1 blocking connection per stream-in and list-in node."
|
|
63
|
+
},
|
|
64
|
+
"status": {
|
|
65
|
+
"unavailable": "Live state needs adminAuth and a deployed config node.",
|
|
66
|
+
"state": "State",
|
|
67
|
+
"none": "not connected yet"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"command": {
|
|
71
|
+
"paletteLabel": "command",
|
|
72
|
+
"default": "command",
|
|
73
|
+
"label": {
|
|
74
|
+
"command": "Command",
|
|
75
|
+
"allowMsgCommand": "Allow msg.command",
|
|
76
|
+
"disableDenylist": "Disable denylist"
|
|
77
|
+
},
|
|
78
|
+
"placeholder": {
|
|
79
|
+
"command": "HGET"
|
|
80
|
+
},
|
|
81
|
+
"tip": {
|
|
82
|
+
"command": "Arguments come from msg.args, for example [\"user:1\", \"name\"].",
|
|
83
|
+
"allowMsgCommand": "msg.command (string or array of tokens) replaces the configured command. The denylist still applies and scripts (EVAL, FCALL) are rejected.",
|
|
84
|
+
"denylist": "Without the denylist, FLUSHALL, CONFIG, SHUTDOWN and similar commands can run from this node. Blocking commands are always rejected."
|
|
85
|
+
},
|
|
86
|
+
"ports": {
|
|
87
|
+
"result": "result"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"multi": {
|
|
91
|
+
"paletteLabel": "multi",
|
|
92
|
+
"default": "MULTI",
|
|
93
|
+
"label": {
|
|
94
|
+
"commands": "Commands",
|
|
95
|
+
"disableDenylist": "Disable denylist"
|
|
96
|
+
},
|
|
97
|
+
"placeholder": {
|
|
98
|
+
"commands": "[[\"SET\",\"k\",\"v\"],[\"INCR\",\"c\"]] (empty: msg.commands)"
|
|
99
|
+
},
|
|
100
|
+
"tip": {
|
|
101
|
+
"commands": "msg.commands overrides this list. Every command passes the denylist."
|
|
102
|
+
},
|
|
103
|
+
"ports": {
|
|
104
|
+
"results": "results"
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"scan": {
|
|
108
|
+
"paletteLabel": "scan",
|
|
109
|
+
"default": "scan",
|
|
110
|
+
"label": {
|
|
111
|
+
"scanType": "Type",
|
|
112
|
+
"match": "Match",
|
|
113
|
+
"count": "Count per call",
|
|
114
|
+
"maxResults": "Max results",
|
|
115
|
+
"timeoutMs": "Timeout (ms)"
|
|
116
|
+
},
|
|
117
|
+
"placeholder": {
|
|
118
|
+
"match": "user:*"
|
|
119
|
+
},
|
|
120
|
+
"tip": {
|
|
121
|
+
"scan": "Walks the cursor until the end, the result limit or the timeout. In cluster mode SCAN visits every master. HSCAN, SSCAN and ZSCAN take msg.key."
|
|
122
|
+
},
|
|
123
|
+
"ports": {
|
|
124
|
+
"items": "items"
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"subscribe": {
|
|
128
|
+
"paletteLabel": "subscribe",
|
|
129
|
+
"default": "subscribe",
|
|
130
|
+
"label": {
|
|
131
|
+
"mode": "Mode",
|
|
132
|
+
"channels": "Channels"
|
|
133
|
+
},
|
|
134
|
+
"mode": {
|
|
135
|
+
"subscribe": "channels (SUBSCRIBE)",
|
|
136
|
+
"psubscribe": "patterns (PSUBSCRIBE)"
|
|
137
|
+
},
|
|
138
|
+
"placeholder": {
|
|
139
|
+
"channels": "news, alerts"
|
|
140
|
+
},
|
|
141
|
+
"tip": {
|
|
142
|
+
"subscribe": "Pub/Sub only. Subscriptions are restored after a reconnect. For list queues use list in."
|
|
143
|
+
},
|
|
144
|
+
"ports": {
|
|
145
|
+
"message": "message"
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
"listIn": {
|
|
149
|
+
"paletteLabel": "list in",
|
|
150
|
+
"default": "list in",
|
|
151
|
+
"label": {
|
|
152
|
+
"key": "List key",
|
|
153
|
+
"direction": "Pop from",
|
|
154
|
+
"timeout": "Block timeout (s)",
|
|
155
|
+
"prefetch": "Prefetch",
|
|
156
|
+
"releaseTimeoutMs": "Release timeout (ms)"
|
|
157
|
+
},
|
|
158
|
+
"direction": {
|
|
159
|
+
"BLPOP": "head (BLPOP)",
|
|
160
|
+
"BRPOP": "tail (BRPOP)"
|
|
161
|
+
},
|
|
162
|
+
"tip": {
|
|
163
|
+
"listIn": "Transitional node for list queues: popping removes the element. Delivery guarantees come from Redis Streams; this node will be deprecated after the move to Streams.",
|
|
164
|
+
"prefetch": "1 pops the next element only after the flow completed the previous message. 0 means unlimited (legacy behaviour)."
|
|
165
|
+
},
|
|
166
|
+
"ports": {
|
|
167
|
+
"element": "element"
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
"streamIn": {
|
|
171
|
+
"paletteLabel": "stream in",
|
|
172
|
+
"default": "stream in",
|
|
173
|
+
"label": {
|
|
174
|
+
"ackMode": "ACK",
|
|
175
|
+
"startId": "Start ID",
|
|
176
|
+
"startPaused": "Start paused",
|
|
177
|
+
"maxPending": "Max pending",
|
|
178
|
+
"backpressureScope": "Backpressure by",
|
|
179
|
+
"maxDeliveries": "Max deliveries",
|
|
180
|
+
"deadLetterStream": "Dead-letter stream",
|
|
181
|
+
"autoClaim": "Reclaim abandoned entries",
|
|
182
|
+
"reclaimIdleMs": "Reclaim idle (ms)",
|
|
183
|
+
"reclaimIntervalMs": "Reclaim interval (ms)",
|
|
184
|
+
"reclaimCount": "Reclaim count",
|
|
185
|
+
"drainTimeoutMs": "Drain timeout (ms)",
|
|
186
|
+
"count": "Read count",
|
|
187
|
+
"blockMs": "Block (ms)",
|
|
188
|
+
"readIntervalMs": "Read interval (ms)",
|
|
189
|
+
"rateLimitPerSecond": "Rate limit (msg/s)",
|
|
190
|
+
"batchWindowMs": "Batch window (ms)",
|
|
191
|
+
"batchIntervalMs": "Batch interval (ms)",
|
|
192
|
+
"capacityCheckIntervalMs": "Capacity check (ms)",
|
|
193
|
+
"capacityPollMs": "Capacity poll (ms)",
|
|
194
|
+
"initialBackoffMs": "Initial backoff (ms)",
|
|
195
|
+
"maxBackoffMs": "Max backoff (ms)",
|
|
196
|
+
"backoffMultiplier": "Backoff multiplier"
|
|
197
|
+
},
|
|
198
|
+
"ackMode": {
|
|
199
|
+
"manual": "manual: at-least-once",
|
|
200
|
+
"auto": "auto: at-most-once"
|
|
201
|
+
},
|
|
202
|
+
"backpressureScope": {
|
|
203
|
+
"consumer": "own PEL of this consumer",
|
|
204
|
+
"group": "PEL of the whole group"
|
|
205
|
+
},
|
|
206
|
+
"placeholder": {
|
|
207
|
+
"deadLetterStream": "stream:dlq"
|
|
208
|
+
},
|
|
209
|
+
"tip": {
|
|
210
|
+
"ackMode": "Manual keeps every entry in the PEL until stream ack; after a pod crash or slow processing an entry can arrive again, so downstream must be idempotent.",
|
|
211
|
+
"reclaimIdleMs": "Must be longer than the slowest processing of one message, otherwise another consumer takes the entry and it is processed twice.",
|
|
212
|
+
"startPaused": "For Kubernetes rollout: the group is created but no work is reserved until resume.",
|
|
213
|
+
"batch": "A batch window above 0 sends arrays: msg.payload and msg.redkern.redis.id become arrays of the same length."
|
|
214
|
+
},
|
|
215
|
+
"ports": {
|
|
216
|
+
"entries": "entries"
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
"streamOut": {
|
|
220
|
+
"paletteLabel": "stream out",
|
|
221
|
+
"default": "stream out",
|
|
222
|
+
"label": {
|
|
223
|
+
"allowMsgStream": "Allow msg.stream",
|
|
224
|
+
"maxlen": "MAXLEN",
|
|
225
|
+
"approxTrim": "Approximate trim (~)",
|
|
226
|
+
"unsafeTrim": "Allow trimming"
|
|
227
|
+
},
|
|
228
|
+
"tip": {
|
|
229
|
+
"payload": "msg.payload must be a flat object; nested objects and arrays are stored as JSON.",
|
|
230
|
+
"trim": "MAXLEN can remove entries still pending in a consumer group. Use stream gc for safe trimming."
|
|
231
|
+
},
|
|
232
|
+
"ports": {
|
|
233
|
+
"added": "added"
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
"streamAck": {
|
|
237
|
+
"paletteLabel": "stream ack",
|
|
238
|
+
"default": "stream ack",
|
|
239
|
+
"label": {
|
|
240
|
+
"deleteAfterAck": "XDEL after ACK",
|
|
241
|
+
"passThrough": "Pass the message on"
|
|
242
|
+
},
|
|
243
|
+
"tip": {
|
|
244
|
+
"ack": "Stream and group come from msg.redkern.redis; the fields here are fallbacks for messages without them."
|
|
245
|
+
},
|
|
246
|
+
"ports": {
|
|
247
|
+
"acked": "acknowledged"
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
"streamClaim": {
|
|
251
|
+
"paletteLabel": "stream claim",
|
|
252
|
+
"default": "stream claim",
|
|
253
|
+
"label": {
|
|
254
|
+
"minIdleMs": "Min idle (ms)",
|
|
255
|
+
"count": "Count per call",
|
|
256
|
+
"maxMessages": "Max entries"
|
|
257
|
+
},
|
|
258
|
+
"ports": {
|
|
259
|
+
"claimed": "claimed"
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
"streamGc": {
|
|
263
|
+
"paletteLabel": "stream gc",
|
|
264
|
+
"default": "stream gc",
|
|
265
|
+
"label": {
|
|
266
|
+
"minIdleMs": "Min idle (ms)"
|
|
267
|
+
},
|
|
268
|
+
"tip": {
|
|
269
|
+
"gc": "Deletes consumers idle longer than the threshold whose PEL is empty; the check and the delete run in one script."
|
|
270
|
+
},
|
|
271
|
+
"ports": {
|
|
272
|
+
"report": "report"
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
"streamMetrics": {
|
|
276
|
+
"paletteLabel": "stream metrics",
|
|
277
|
+
"default": "stream metrics",
|
|
278
|
+
"label": {
|
|
279
|
+
"deadLetterStream": "Dead-letter stream"
|
|
280
|
+
},
|
|
281
|
+
"tip": {
|
|
282
|
+
"metrics": "Each sample also updates the palette metrics. With the prometheus palette they appear in its /metrics; without it on port 9551 when REDKERN_REDIS_METRICS_TOKEN is set."
|
|
283
|
+
},
|
|
284
|
+
"ports": {
|
|
285
|
+
"sample": "sample"
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
"streamControl": {
|
|
289
|
+
"paletteLabel": "stream control",
|
|
290
|
+
"default": "stream control",
|
|
291
|
+
"label": {
|
|
292
|
+
"action": "Action"
|
|
293
|
+
},
|
|
294
|
+
"action": {
|
|
295
|
+
"msg": "from msg.payload.action",
|
|
296
|
+
"pause": "pause",
|
|
297
|
+
"resume": "resume",
|
|
298
|
+
"drain": "drain",
|
|
299
|
+
"status": "status"
|
|
300
|
+
},
|
|
301
|
+
"tip": {
|
|
302
|
+
"control": "Acts on every stream in node of this runtime, or the ones matching stream, group or nodeId in the message."
|
|
303
|
+
},
|
|
304
|
+
"ports": {
|
|
305
|
+
"result": "result"
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toTokens, checkCommand } = require('../lib/denylist.js');
|
|
4
|
+
const { codeError, setMeta } = require('../lib/format.js');
|
|
5
|
+
const { INPUT_SCHEMA, startShared, inputOptions, primary } = require('../lib/node-helpers.js');
|
|
6
|
+
|
|
7
|
+
module.exports = function register(RED, palette) {
|
|
8
|
+
const { rk } = palette;
|
|
9
|
+
|
|
10
|
+
function RedisCommandNode(config) {
|
|
11
|
+
RED.nodes.createNode(this, config);
|
|
12
|
+
const node = this;
|
|
13
|
+
const k = rk.bind(node);
|
|
14
|
+
const parsed = k.configure(config, {
|
|
15
|
+
server: { type: 'str', required: true },
|
|
16
|
+
command: { type: 'str', default: '' },
|
|
17
|
+
allowMsgCommand: { type: 'bool', default: false },
|
|
18
|
+
disableDenylist: { type: 'bool', default: false },
|
|
19
|
+
legacyMsgFields: { type: 'bool', default: false },
|
|
20
|
+
...INPUT_SCHEMA
|
|
21
|
+
});
|
|
22
|
+
const options = parsed.ok ? parsed.value : {};
|
|
23
|
+
if (parsed.ok && options.disableDenylist) k.log.warn('Command denylist is disabled for this node', { key: 'DENYLIST_DISABLED' });
|
|
24
|
+
const shared = startShared(RED, k, node, config.server);
|
|
25
|
+
let count = 0;
|
|
26
|
+
|
|
27
|
+
k.onInput(async (msg, send) => {
|
|
28
|
+
const useMsg = options.allowMsgCommand && msg.command !== undefined && msg.command !== null && msg.command !== '';
|
|
29
|
+
const tokens = toTokens(useMsg ? msg.command : options.command, msg.args);
|
|
30
|
+
checkCommand(tokens, { denylist: !options.disableDenylist, fromMsg: useMsg });
|
|
31
|
+
const name = String(tokens[0]).toUpperCase();
|
|
32
|
+
let result;
|
|
33
|
+
try {
|
|
34
|
+
result = await shared.client().call(...tokens);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
// Redis reply errors carry the server message only, never the arguments.
|
|
37
|
+
throw codeError(error?.code || 'REDIS_COMMAND_FAILED', error?.message || 'Redis command failed');
|
|
38
|
+
}
|
|
39
|
+
msg.payload = result;
|
|
40
|
+
setMeta(msg, { command: name });
|
|
41
|
+
if (options.legacyMsgFields) msg.command = useMsg ? msg.command : options.command;
|
|
42
|
+
count += 1;
|
|
43
|
+
k.status('ok', `${name.toLowerCase()} ${count}`);
|
|
44
|
+
primary(send, options.errorOutput, msg);
|
|
45
|
+
}, inputOptions(parsed, 1));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
RED.nodes.registerType('redkern-redis-command', RedisCommandNode);
|
|
49
|
+
};
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Redis = require('ioredis');
|
|
4
|
+
const { createRedisClient, classifyRedisError } = require('@redkern/node-red-kit/redis');
|
|
5
|
+
const { splitList } = require('../lib/format.js');
|
|
6
|
+
|
|
7
|
+
const TLS_REJECTED = /^(CERT_|ERR_TLS_|DEPTH_ZERO_SELF_SIGNED_CERT|SELF_SIGNED_CERT_IN_CHAIN|UNABLE_TO_)/;
|
|
8
|
+
|
|
9
|
+
const SCHEMA = Object.freeze({
|
|
10
|
+
mode: { type: 'enum', values: ['standalone', 'cluster'], default: 'standalone' },
|
|
11
|
+
host: { type: 'str', default: '127.0.0.1' },
|
|
12
|
+
port: { type: 'int', min: 1, max: 65535, default: 6379 },
|
|
13
|
+
clusterNodes: { type: 'str', default: '' },
|
|
14
|
+
db: { type: 'int', min: 0, max: 1024, default: 0 },
|
|
15
|
+
username: { type: 'str', default: '' },
|
|
16
|
+
tls: { type: 'bool', default: false },
|
|
17
|
+
tlsServername: { type: 'str', default: '' },
|
|
18
|
+
connectTimeout: { type: 'int', min: 100, max: 600000, default: 10000 },
|
|
19
|
+
commandTimeout: { type: 'int', min: 100, max: 600000, default: 5000 }
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Parses "host:port, host:port" into cluster seed nodes.
|
|
24
|
+
* @param {string} value
|
|
25
|
+
* @param {string} host fallback seed host
|
|
26
|
+
* @param {number} port fallback seed port
|
|
27
|
+
*/
|
|
28
|
+
function parseClusterNodes(value, host, port) {
|
|
29
|
+
const items = splitList(value);
|
|
30
|
+
if (items.length === 0) return [{ host, port }];
|
|
31
|
+
return items.map((item) => {
|
|
32
|
+
const match = item.match(/^\[?([^\]]+?)\]?:(\d{1,5})$/);
|
|
33
|
+
if (!match) throw Object.assign(new Error('Cluster nodes must be host:port, comma separated'), { code: 'CONFIG_INVALID' });
|
|
34
|
+
const nodePort = Number(match[2]);
|
|
35
|
+
if (nodePort < 1 || nodePort > 65535) throw Object.assign(new Error('Cluster node port is invalid'), { code: 'CONFIG_INVALID' });
|
|
36
|
+
return { host: match[1], port: nodePort };
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = function register(RED, palette) {
|
|
41
|
+
const { rk, kit } = palette;
|
|
42
|
+
|
|
43
|
+
function RedisConfigNode(config) {
|
|
44
|
+
RED.nodes.createNode(this, config);
|
|
45
|
+
const node = this;
|
|
46
|
+
const k = rk.bind(node);
|
|
47
|
+
const parsed = k.configure(config, SCHEMA);
|
|
48
|
+
const password = k.readSecret('password');
|
|
49
|
+
const options = parsed.ok ? parsed.value : { mode: 'standalone' };
|
|
50
|
+
const issues = parsed.ok ? [] : parsed.errors;
|
|
51
|
+
let seeds = [];
|
|
52
|
+
if (parsed.ok && options.mode === 'cluster') {
|
|
53
|
+
try {
|
|
54
|
+
seeds = parseClusterNodes(options.clusterNodes, options.host, options.port);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
issues.push({ field: 'clusterNodes', code: 'CONFIG_INVALID', message: error.message });
|
|
57
|
+
k.status('error', 'CONFIG_INVALID');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const handles = { shared: undefined, subscriber: undefined, blocking: new Set() };
|
|
62
|
+
const dependents = new Set();
|
|
63
|
+
let dependentsIdle;
|
|
64
|
+
let closing = false;
|
|
65
|
+
|
|
66
|
+
function clientOptions(role, extra = {}) {
|
|
67
|
+
if (issues.length) throw new kit.ConfigError(issues);
|
|
68
|
+
return {
|
|
69
|
+
ioredis: Redis,
|
|
70
|
+
domain: 'redis',
|
|
71
|
+
configId: node.id.replace(/[^A-Za-z0-9_-]/g, '_').padEnd(8, '_'),
|
|
72
|
+
role,
|
|
73
|
+
mode: options.mode,
|
|
74
|
+
host: options.host,
|
|
75
|
+
port: options.port,
|
|
76
|
+
nodes: options.mode === 'cluster' ? seeds : undefined,
|
|
77
|
+
db: options.mode === 'cluster' ? 0 : options.db,
|
|
78
|
+
tls: options.tls ? (options.tlsServername ? { servername: options.tlsServername } : {}) : undefined,
|
|
79
|
+
username: options.username || undefined,
|
|
80
|
+
password: password || undefined,
|
|
81
|
+
connectTimeout: options.connectTimeout,
|
|
82
|
+
commandTimeout: role === 'shared' ? options.commandTimeout : extra.commandTimeout,
|
|
83
|
+
blockMs: extra.blockMs,
|
|
84
|
+
logger: k.log
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function connections() {
|
|
89
|
+
const ready = (handle) => (handle && handle.client.status === 'ready' ? 1 : 0);
|
|
90
|
+
let blocking = 0;
|
|
91
|
+
for (const handle of handles.blocking) blocking += ready(handle);
|
|
92
|
+
return { shared: ready(handles.shared), blocking, subscriber: ready(handles.subscriber) };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function describe() {
|
|
96
|
+
return {
|
|
97
|
+
name: node.name || undefined,
|
|
98
|
+
mode: options.mode,
|
|
99
|
+
state: issues.length ? 'invalid' : handles.shared ? handles.shared.client.status : 'idle',
|
|
100
|
+
connections: connections(),
|
|
101
|
+
dependents: dependents.size
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function updateStatus() {
|
|
106
|
+
if (issues.length) return;
|
|
107
|
+
const counts = connections();
|
|
108
|
+
const total = counts.shared + counts.blocking + counts.subscriber;
|
|
109
|
+
const state = handles.shared?.client.status;
|
|
110
|
+
if (state && state !== 'ready' && state !== 'wait') k.status('warn', `${state} ${total}`);
|
|
111
|
+
else k.status('ok', `connections ${total}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function watch(handle) {
|
|
115
|
+
for (const event of ['ready', 'end', 'reconnecting', 'close']) handle.client.on(event, updateStatus);
|
|
116
|
+
handle.client.on('error', (error) => { handle.lastError = error; });
|
|
117
|
+
handle.client.on('ready', () => { handle.lastError = undefined; });
|
|
118
|
+
return handle;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* ioredis rejects connect() with a generic "Connection is closed" after an
|
|
123
|
+
* AUTH failure; surface the reply error so kit classifies it as auth
|
|
124
|
+
* (red status right away, KIT-LIFE-3).
|
|
125
|
+
*/
|
|
126
|
+
async function connect(handle) {
|
|
127
|
+
try {
|
|
128
|
+
await handle.connect();
|
|
129
|
+
} catch (error) {
|
|
130
|
+
const last = handle.lastError;
|
|
131
|
+
if (last && classifyRedisError(last)) {
|
|
132
|
+
const code = String(last.message || '').match(/^[A-Z]+/)?.[0] || 'REDIS_AUTH';
|
|
133
|
+
throw Object.assign(new Error(last.message), { code, cause: error });
|
|
134
|
+
}
|
|
135
|
+
// A rejected server certificate does not heal on retry: keep its code
|
|
136
|
+
// so kit classifies it as auth and shows a red status.
|
|
137
|
+
if (last && typeof last.code === 'string' && TLS_REJECTED.test(last.code)) {
|
|
138
|
+
throw Object.assign(new Error(last.message), { code: last.code, cause: error });
|
|
139
|
+
}
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Lazily created shared client for non-blocking commands (RDS-CON-2). */
|
|
145
|
+
async function shared() {
|
|
146
|
+
if (closing) throw Object.assign(new Error('Redis config is closing'), { code: 'NODE_CLOSING' });
|
|
147
|
+
if (!handles.shared) handles.shared = watch(createRedisClient(clientOptions('shared')));
|
|
148
|
+
await connect(handles.shared);
|
|
149
|
+
return handles.shared.client;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Dedicated blocking connection, one per stream-in / list-in node. */
|
|
153
|
+
function createBlocking({ blockMs = 0, commandTimeout } = {}) {
|
|
154
|
+
if (closing) throw Object.assign(new Error('Redis config is closing'), { code: 'NODE_CLOSING' });
|
|
155
|
+
const handle = watch(createRedisClient(clientOptions('blocking', { blockMs, commandTimeout })));
|
|
156
|
+
handles.blocking.add(handle);
|
|
157
|
+
const release = async () => {
|
|
158
|
+
if (!handles.blocking.delete(handle)) return;
|
|
159
|
+
handle.force();
|
|
160
|
+
updateStatus();
|
|
161
|
+
};
|
|
162
|
+
return { client: handle.client, connect: () => connect(handle), release };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Pub/Sub multiplexer on one subscriber connection per config node. */
|
|
166
|
+
const channels = new Map();
|
|
167
|
+
const patterns = new Map();
|
|
168
|
+
async function subscriberClient() {
|
|
169
|
+
if (!handles.subscriber) {
|
|
170
|
+
const handle = watch(createRedisClient(clientOptions('subscriber')));
|
|
171
|
+
handle.client.on('message', (channel, message) => {
|
|
172
|
+
for (const listener of channels.get(channel) || []) listener({ channel, message });
|
|
173
|
+
});
|
|
174
|
+
handle.client.on('pmessage', (pattern, channel, message) => {
|
|
175
|
+
for (const listener of patterns.get(pattern) || []) listener({ pattern, channel, message });
|
|
176
|
+
});
|
|
177
|
+
handles.subscriber = handle;
|
|
178
|
+
}
|
|
179
|
+
await connect(handles.subscriber);
|
|
180
|
+
return handles.subscriber.client;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function subscribe(mode, names, listener) {
|
|
184
|
+
const map = mode === 'psubscribe' ? patterns : channels;
|
|
185
|
+
const fresh = [];
|
|
186
|
+
for (const name of names) {
|
|
187
|
+
let set = map.get(name);
|
|
188
|
+
if (!set) {
|
|
189
|
+
set = new Set();
|
|
190
|
+
map.set(name, set);
|
|
191
|
+
fresh.push(name);
|
|
192
|
+
}
|
|
193
|
+
set.add(listener);
|
|
194
|
+
}
|
|
195
|
+
const release = async () => {
|
|
196
|
+
const gone = [];
|
|
197
|
+
for (const name of names) {
|
|
198
|
+
const set = map.get(name);
|
|
199
|
+
if (!set) continue;
|
|
200
|
+
set.delete(listener);
|
|
201
|
+
if (set.size === 0) {
|
|
202
|
+
map.delete(name);
|
|
203
|
+
gone.push(name);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const client = handles.subscriber?.client;
|
|
207
|
+
if (gone.length && client && client.status === 'ready') {
|
|
208
|
+
try {
|
|
209
|
+
if (mode === 'psubscribe') await client.punsubscribe(...gone);
|
|
210
|
+
else await client.unsubscribe(...gone);
|
|
211
|
+
} catch { /* the connection closes anyway when the last dependent leaves */ }
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
const client = await subscriberClient();
|
|
216
|
+
if (fresh.length) {
|
|
217
|
+
if (mode === 'psubscribe') await client.psubscribe(...fresh);
|
|
218
|
+
else await client.subscribe(...fresh);
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
await release();
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
return release;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Dependent nodes register so the config closes its clients after them. */
|
|
228
|
+
function register(id) {
|
|
229
|
+
dependents.add(id);
|
|
230
|
+
return () => {
|
|
231
|
+
dependents.delete(id);
|
|
232
|
+
if (dependents.size === 0 && dependentsIdle) dependentsIdle();
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
k.onClose(async ({ deadline }) => {
|
|
237
|
+
closing = true;
|
|
238
|
+
if (dependents.size) {
|
|
239
|
+
const idle = new Promise((resolve) => { dependentsIdle = resolve; });
|
|
240
|
+
const wait = Math.max(0, deadline - Date.now() - 1000);
|
|
241
|
+
await Promise.race([idle, new Promise((resolve) => setTimeout(resolve, wait).unref?.())]);
|
|
242
|
+
}
|
|
243
|
+
for (const handle of handles.blocking) handle.force();
|
|
244
|
+
handles.blocking.clear();
|
|
245
|
+
await Promise.allSettled([handles.shared?.close(), handles.subscriber?.close()]);
|
|
246
|
+
}, {
|
|
247
|
+
force() {
|
|
248
|
+
handles.shared?.force();
|
|
249
|
+
handles.subscriber?.force();
|
|
250
|
+
for (const handle of handles.blocking) handle.force();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
k.onClose(palette.addConfig(node.id, describe));
|
|
254
|
+
|
|
255
|
+
node.redkern = {
|
|
256
|
+
mode: options.mode,
|
|
257
|
+
issues,
|
|
258
|
+
shared,
|
|
259
|
+
createBlocking,
|
|
260
|
+
subscribe,
|
|
261
|
+
register,
|
|
262
|
+
describe,
|
|
263
|
+
classify: (error) => classifyRedisError(error)
|
|
264
|
+
};
|
|
265
|
+
if (!issues.length) k.status('idle', 'idle');
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
RED.nodes.registerType('redkern-redis-config', RedisConfigNode, {
|
|
269
|
+
credentials: { password: { type: 'password' } }
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
module.exports.parseClusterNodes = parseClusterNodes;
|