-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join-wait.js
465 lines (381 loc) · 15.6 KB
/
join-wait.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
module.exports = function (RED) {
'use strict';
const path = require('path');
const storage = require('node-persist');
const jsonata = require('jsonata');
function JoinWaitNode(config) {
RED.nodes.createNode(this, config);
try {
this.pathsToWait = JSON.parse(config.paths);
} catch (err) {
this.pathsToWait = false;
}
try {
this.pathsToExpire = JSON.parse(config.pathsToExpire);
if (hasDuplicatePath(this.pathsToExpire)) {
this.error(`join-wait pathsToExpire cannot have duplicate entries: ${this.pathsToExpire}`);
return;
}
} catch (err) {
this.pathsToExpire = false;
}
this.exactOrder = config.exactOrder === 'true';
this.topic = config.correlationTopic || false;
this.topicType = config.correlationTopicType;
if (this.topicType === 'jsonata') {
try {
this.topic = jsonata(this.topic);
} catch (err) {
this.error(`join-wait.invalid-expr topic ${err.message}`);
return;
}
}
this.pathTopic = config.pathTopic || 'topic';
this.pathTopicType = config.pathTopicType;
this.timeout = (Number(config.timeout) || 15000) * (Number(config.timeoutUnits) || 1);
this.firstMsg = config.firstMsg === 'true';
this.mapPayload = config.mapPayload === 'true';
this.useRegex = config.useRegex === true;
this.warnUnmatched = config.warnUnmatched === true;
this.disableComplete = config.disableComplete === true;
this.persistOnRestart = config.persistOnRestart === true;
storage.initSync({
dir: path.join(RED.settings.userDir, 'join-wait', config.id.toString()),
forgiveParseErrors: true,
});
const savedPaths = storage.getItemSync('paths');
storage.clear();
this.paths = savedPaths ? JSON.parse(savedPaths) : {};
let node = this;
for (const topic in node.paths) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(node.paths, topic)) {
if (node.persistOnRestart) {
makeNewQueueTimer(topic, 10);
} else {
clearQueueAllNoOutput(topic);
}
}
}
node.on('close', function (removed, done) {
for (const topic in node.paths) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(node.paths, topic)) {
clearTimeout(node.paths[topic].timeOut);
/* istanbul ignore else */
if (!node.persistOnRestart) {
clearQueueAllNoOutput(topic);
}
}
}
if (node.persistOnRestart) {
storage.setItemSync('paths', JSON.stringify(node.paths));
}
done();
});
node.on('input', function (msg) {
//
// error checking
//
// pathTopic & pathTopicType
let pathTopic = RED.util.evaluateNodeProperty(node.pathTopic, node.pathTopicType, node, msg);
const pathTopicName = `${node.pathTopicType}.${node.pathTopic}`;
if (!pathTopic) {
node.error(`join-wait "${pathTopicName}" is undefined or not set.`, [msg, null]);
return;
}
if (typeof pathTopic === 'string') {
pathTopic = {
[pathTopic]: true,
};
} else if (typeof pathTopic !== 'object' || Array.isArray(pathTopic)) {
node.error(
`join-wait "${pathTopicName}" must be a string or an object, e.g., ${pathTopicName} = 'value'.`,
[msg, null],
);
return;
}
// pathsToWait & pathsToExpire
node.pathsToWait = msg.pathsToWait || node.pathsToWait; // update global setting
if (!node.pathsToWait || !Array.isArray(node.pathsToWait) || !node.pathsToWait.length) {
node.error('join-wait pathsToWait must be a defined array.', [msg, null]);
return;
}
let pathsToWait = Object.assign([], node.pathsToWait);
let pathsToExpire = false;
node.pathsToExpire = msg.pathsToExpire || node.pathsToExpire; // update global setting
if (node.pathsToExpire) {
if (!Array.isArray(node.pathsToExpire) || !node.pathsToExpire.length) {
node.error('join-wait pathsToExpire must be undefined or an array.', [msg, null]);
return;
}
pathsToExpire = Object.assign([], node.pathsToExpire);
}
if (pathsToExpire && hasDuplicatePath(pathsToExpire)) {
node.error(`join-wait pathsToExpire cannot have duplicate entries: ${pathsToExpire}`);
return;
}
node.useRegex = Object.prototype.hasOwnProperty.call(msg, 'useRegex')
? msg.useRegex === true
: node.useRegex; // update global setting
if (node.useRegex) {
try {
pathsToWait = convertToRegex(pathsToWait);
pathsToExpire = convertToRegex(pathsToExpire);
} catch (err) {
node.error(`join-wait.regex-expr ${err.message}`, null);
return;
}
}
const pathKeys = Object.keys(pathTopic);
const foundKeys = pathKeys.filter(function (val) {
return pathsToWait.some(function (p) {
return node.useRegex ? p.test(val) : p === val;
});
});
const hasExpirePath = pathsToExpire && findAnyPath(pathKeys, pathsToExpire, node.useRegex);
if (!hasExpirePath) {
const notFoundKeys = pathKeys.filter(function (val) {
return foundKeys.indexOf(val) === -1;
});
if (node.warnUnmatched && notFoundKeys.length > 0) {
const unmatchedStr = notFoundKeys
.map(function (key) {
return `${pathTopicName}["${key}"]`;
})
.join(', ');
node.warn(`join-wait ${unmatchedStr} doesn't exist in pathsToWait or pathsToExpire!`, [msg, null]);
}
if (foundKeys.length === 0) {
return;
}
}
// correlation topic
let topic;
try {
if (node.topicType === 'jsonata') {
topic = node.topic.evaluate({
msg: msg,
});
} else {
topic = node.topic
? RED.util.evaluateNodeProperty(node.topic, node.topicType, node, msg)
: '_join-wait-node';
}
} catch (err) {
node.error(`join-wait.invalid-expr topic ${err.message}`);
return;
}
// map payload
if (node.mapPayload) {
pathKeys.forEach(function (item) {
pathTopic[item] = msg.payload;
});
}
//
// start processing
//
initQueue(topic);
const group = node.paths[topic];
group.queue.push([Date.now(), msg, pathTopic]);
if ((hasExpirePath && clearQueueAllWithOutput(topic)) || clearQueueExpiredByTime(topic)) {
return;
}
const pathData = group.queue.map(function (q) {
return q[2];
});
const allPathKeys = pathData.map(function (q) {
return Object.keys(q);
});
const numToKeep = node.exactOrder
? findAllPathsExactOrder(allPathKeys, pathsToWait, node.useRegex)
: findAllPathsAnyOrder(allPathKeys, pathsToWait, node.useRegex);
if (numToKeep !== null) {
clearQueueIfCompleteIsSet(topic, msg) || clearQueueExpiredByOrder(topic, numToKeep);
return;
}
// all paths found
const num = node.firstMsg ? 0 : group.queue.length - 1;
let output = group.queue[num][1];
output[node.pathTopic] = pathData.reduce(function (a, b) {
return Object.assign(a, b);
}, {});
node.send([output, null]);
clearQueueAllNoOutput(topic);
});
function convertToRegex(arr) {
if (!Array.isArray(arr)) {
return arr;
}
return arr.map(function (pattern) {
return new RegExp(pattern);
});
}
function flatten(arr) {
return [].concat.apply([], arr);
}
function condenseWithCount(arr) {
arr = arr.reduce((map, key) => map.set(key, (map.get(key) || 0) + 1), new Map());
return Array.from(arr, ([name, value]) => ({ name, value }));
}
function regexIndexOf(arr, needle) {
let result = -1;
arr.some(function (p, i) {
if (p.test(needle)) {
result = i;
return true;
}
});
return result;
}
function hasDuplicatePath(arr) {
return arr.some(function (p, index) {
return arr.indexOf(p) !== index;
});
}
function findAnyPath(msgPaths, arr, useRegex) {
return msgPaths.some(function (p) {
if (useRegex) {
return arr.some(function (pattern) {
return pattern.test(p);
});
} else {
return arr.includes(p);
}
});
}
function findAllPathsAnyOrder(arr, waitPaths, useRegex) {
const waitMap = condenseWithCount(waitPaths);
const keys = flatten(arr);
const result = countPathsAnyOrder(keys, waitMap, useRegex);
const allPathsFound = result.every(function (p) {
return p === true;
});
if (allPathsFound) {
return null;
}
const originalString = result.toString();
for (let i = 0; i < arr.length; i++) {
const newKeys = flatten(arr.slice(i + 1));
const expireByOne = countPathsAnyOrder(newKeys, waitMap, useRegex);
if (originalString !== expireByOne.toString()) {
return arr.length - i;
}
}
/* istanbul ignore next */
return 0;
}
function countPathsAnyOrder(keys, waitMap, useRegex) {
let used = [];
return waitMap.map(function (p) {
const count = keys.filter(function (val, i) {
if (used.indexOf(i) !== -1) {
return false;
}
const found = useRegex ? p.name.test(val) : p.name === val;
if (!found) {
return false;
}
used.push(i);
return true;
}).length;
return count < p.value ? count : true;
});
}
function findAllPathsExactOrder(arr, waitPaths, useRegex) {
let start = 0;
let marker = false;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
const path = arr[i][j];
let offBy = marker === false ? 0 : marker + 1;
const unusedWaitPaths = waitPaths.slice(offBy);
let index = useRegex ? regexIndexOf(unusedWaitPaths, path) : unusedWaitPaths.indexOf(path);
if (index === -1) {
/* istanbul ignore else */
if (offBy > 0) {
index = useRegex ? regexIndexOf(waitPaths, path) : waitPaths.indexOf(path);
if (index > 0) {
marker = false;
}
}
} else {
index += offBy;
}
if (index === 0) {
start = i;
} else if (index === -1 || marker === false) {
continue;
} else if (index < marker || index > marker + 1) {
marker = false;
continue;
}
if (index === waitPaths.length - 1) {
return null;
}
marker = index;
}
}
return marker === false ? 0 : arr.length - start;
}
// queue & timer handling
function initQueue(topic) {
if (!Object.prototype.hasOwnProperty.call(node.paths, topic)) {
node.paths[topic] = {
queue: [],
};
makeNewQueueTimer(topic, node.timeout);
}
}
function makeNewQueueTimer(topic, timeout) {
const group = node.paths[topic];
group.timeOut = setTimeout(function () {
if (clearQueueExpiredByTime(topic)) {
return;
} else {
const next = group.queue[0][0] + node.timeout - Date.now();
makeNewQueueTimer(topic, next);
}
}, timeout);
}
// returns boolean if queue is empty (= true)
function clearQueueAllNoOutput(topic) {
return _queueDeletionHandler(topic, false, false, 0);
}
function clearQueueAllWithOutput(topic) {
return _queueDeletionHandler(topic, true, false, 0);
}
function clearQueueIfCompleteIsSet(topic, msg) {
if (!node.disableComplete && Object.prototype.hasOwnProperty.call(msg, 'complete')) {
return clearQueueAllWithOutput(topic);
}
return false;
}
function clearQueueExpiredByOrder(topic, numToKeep) {
return _queueDeletionHandler(topic, true, false, numToKeep);
}
function clearQueueExpiredByTime(topic) {
return _queueDeletionHandler(topic, true, true, 0);
}
function _queueDeletionHandler(topic, sendExpired, checkExpireTime, numToKeep) {
const group = node.paths[topic];
const isExpired = function () {
return checkExpireTime ? group.queue[0][0] < Date.now() - node.timeout : true;
};
while (group.queue.length > numToKeep && isExpired()) {
const expired = group.queue.shift();
if (sendExpired) {
const msg = Object.assign(expired[1], { paths: expired[2] });
node.send([null, msg]);
}
}
if (group.queue.length !== 0) {
return false;
}
clearTimeout(group.timeOut);
delete node.paths[topic];
return true;
}
}
RED.nodes.registerType('join-wait', JoinWaitNode);
};