-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitpool_uploader.js
More file actions
788 lines (703 loc) · 35.6 KB
/
Copy pathbitpool_uploader.js
File metadata and controls
788 lines (703 loc) · 35.6 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
MIT License Copyright 2021-2024 - Bitpool Pty Ltd
*/
module.exports = function (RED) {
const axios = require('axios');
const { Pool, FileStorageCache, DataType } = require("./uploader")
const os = require("os");
const { Mutex } = require("async-mutex");
const camelCase = require('camelcase');
const { Agent } = require('https');
const HTTP_TIMEOUT_SECS = 10; // Default HTTP session timeout in seconds
const DATA_UPLOAD_INTERVAL_SECS = 60; // Interval in seconds to check for data upload (default, configurable by user)
const DATA_UPLOAD_CHUNK_SIZE = 5; // Number of records to upload in a single batch
const DATA_STORAGE_PATH = './bitpool-data';
const APP_POLL_UI_SECS = 1;
const APP_POLL_ENQUEUE_SECS = 1;
const APP_POLL_DEQUEUE_SECS = 2;
function BITPOOL(config) {
RED.nodes.createNode(this, config);
// Config options for Bitpool API
let apiKeyConfig = RED.nodes.getNode(config.apiKey);
let poolSettings = RED.nodes.getNode(config.pool_settings);
let public = poolSettings.public || false;
let virtual = poolSettings.virtual || false;
let api_key = cleanUserInput(apiKeyConfig.api_key) || "";
let pool_name = cleanUserInput(poolSettings.pool_name) || "";
let api_endpoint = cleanUserInput(apiKeyConfig.api_endpoint) || "api.bitpool.com";
// Config options for status monitoring
let timeoutSecs = Number(config.timeout || HTTP_TIMEOUT_SECS);
let uploadIntervalSecs = Number(config.uploadInterval || DATA_UPLOAD_INTERVAL_SECS);
let bpChkShowDebugWarnings = config.bpChkShowDebugWarnings;
// Timers
let timerCheckUI = null;
let timerEnqueue = null;
let timerDequeue = null;
let enqueuedStreamBlocks = [];
// Runtime variables
const timers = {};
this.mutex = new Mutex();
this.pool = new Pool();
this.fileCache = new FileStorageCache(DATA_STORAGE_PATH);
this.rootUrlv2 = `https://${api_endpoint}/public/v2/`;
this.rootUrlv3 = `https://${api_endpoint}/public/v3/`;
this.initComplete = false;
this.PoolTags = config.PoolTags;
this.lastUploadCheckTimeUI = new Date();
this.lastUploadCheckTimeEnqueue = new Date();
let node = this;
const axiosInstance = axios.create({
timeout: timeoutSecs * 1000,
headers: {
"Authorization": api_key,
"Content-Type": "application/json"
},
httpsAgent: new Agent({ keepAlive: false })
});
async function initializeNode() {
try {
// Ensure all previous timers are stopped
try {
stopAllTimers();
} catch (error) {
logError("Error stopping timers:", error.message || error);
}
// Check if the API key is set
if (!isValidApiKey(api_key)) {
logError("API key is invalid or empty, please check to continue.");
return;
}
// Check if the pool name is set
if (!isValidPoolName(pool_name)) {
logError("Pool Name is invalid or empty, please check to continue.");
return;
}
if (bpChkShowDebugWarnings) {
try {
// Send the pool configuration to the debug window on startup
node.send({
"payload": {
"config": {
"api_key": apiKeyConfig.api_key,
"pool_name": pool_name,
"api_endpoint_v2": node.rootUrlv2,
"api_endpoint_v3": node.rootUrlv3,
"upload_on_expired_secs": uploadIntervalSecs,
"http_timeout_secs": timeoutSecs,
"option_show_debug_warnings": bpChkShowDebugWarnings,
}
}
});
} catch (error) {
logError('Failed to load configuration state.', error || error.message);
}
}
try {
// Wait for the system to load, then set up the pool and station with tags
setTimeout(async () => {
try {
node.pool = new Pool();
// Create pool object
node.pool.setPoolName(pool_name);
// Add default tags and clean
let defaultTags = [
`operatingSystem=${os.version()}`,
`nodejsVersion=${process.version}`,
`nodeRedVersion=${RED.version()}`
];
let poolTagsOnNode = node.pool.cleanTags(node.PoolTags + "," + defaultTags.join(','))
// Get pool details from API
let serverPool = await postAddUpdatePool(pool_name);
if (serverPool.PoolKey && isValidGUID(serverPool.PoolKey)) {
node.pool.setPoolKey(serverPool.PoolKey);
await postAddPoolTags(serverPool.PoolKey, poolTagsOnNode);
node.pool.setPoolTags(poolTagsOnNode);
// Get station details from API
let stationID = await postAddUpdateStation(serverPool.PoolKey, pool_name);
node.pool.setStationId(stationID)
updateStatusMsg({ fill: "green", shape: "dot", text: "Pool initialised OK" });
} else {
logWarn("Error initializing:", error.message || error);
node.status({ fill: "red", shape: "ring", text: "Error initializing" });
}
} catch (error) {
logError("Error initializing:", error.message || error);
logWarn("Please make sure the API is valid and the Pool Name is correct.");
node.status({ fill: "red", shape: "ring", text: "Error, check valid API key and Pool Name" });
}
}, 1);
} catch (error) {
logError("Error initializing:", error.message || error);
node.status({ fill: "red", shape: "ring", text: "Error initializing" });
}
backPopStreams();
// Set up timer to update the UI
clearInterval(timerCheckUI);
if (timerCheckUI === null) {
timers.uiTimer = setInterval(async () => {
try {
const now = new Date();
const elapsedSeconds = Math.floor((now - node.lastUploadCheckTimeUI) / 1000);
const remainingSeconds = uploadIntervalSecs - elapsedSeconds;
const cacheSize = node.fileCache.getSize()
const totalPoolRecordCount = node.pool.getCountOfAllRecords();
const streamCount = node.pool.getStreamCount();
if (remainingSeconds < 0) {
node.lastUploadCheckTimeUI = now;
} else {
const formattedTime = formatDateToDdHhMmSs(remainingSeconds);
let message = `In:(${totalPoolRecordCount}) Out:(${enqueuedStreamBlocks.length}) Time:(${formattedTime}) Streams:(${streamCount})`;
if (cacheSize > 0) {
const storedKeys = node.fileCache.listStoredKeys();
const countPoolCachedStreams = Array.from(storedKeys).reduce((count, [key]) => {
return count + (node.pool.hasStreamKey(key) ? 1 : 0);
}, 0);
if (countPoolCachedStreams > 0) {
message = `In:(${totalPoolRecordCount}) Out:(${enqueuedStreamBlocks.length}) Time:(${formattedTime}) Streams:(${streamCount}) Cached:(${countPoolCachedStreams})`;
}
}
updateStatusMsg({ fill: "blue", shape: "dot", text: message });
}
} catch (error) {
logError("Error in Timer:", error.message || error);
}
}, APP_POLL_UI_SECS * 1000);
} else {
logWarn("At initializeNode(), the upload timer is already running!");
}
// Set up timer to enqueue data
clearInterval(timerEnqueue);
if (timerEnqueue === null) {
timers.enqueueTimer = setInterval(async () => {
const now = new Date();
const elapsedSeconds = Math.floor((now - node.lastUploadCheckTimeEnqueue) / 1000);
const remainingSeconds = uploadIntervalSecs - elapsedSeconds;
if (remainingSeconds <= 0) {
await pushDataToUploadQueue();
node.lastUploadCheckTimeEnqueue = now;
}
}, APP_POLL_ENQUEUE_SECS * 1000);
} else {
logWarn("At initializeNode(), the upload timer is already running!");
}
// Set up timer to dequeue data
clearInterval(timerDequeue);
if (timerDequeue === null) {
timers.dequeueTimer = setInterval(async () => {
await uploadQueuedDataNow();
}, APP_POLL_DEQUEUE_SECS * 1000);
} else {
logWarn("At initializeNode(), the upload timer is already running!");
}
} catch (error) {
logError("Error on initialise:", error.message || error);
} finally {
node.initComplete = true;
}
}
initializeNode();
async function backPopStreams() {
try {
const poolKey = node.pool.getPoolKey();
if (!poolKey) {
setTimeout(backPopStreams, getRandomNumber(500, 3000));
return;
}
let serverStreams = await getPoolStreams(poolKey);
if (serverStreams) {
for (const serverStream of serverStreams) {
node.pool.addStreamName(serverStream.Name);
node.pool.updateStreamKey(serverStream.Name, serverStream.StreamKey);
node.pool.setDataType(serverStream.Name, serverStream.DataType);
}
} else {
logWarn(`No streams found for pool (${node.pool.getPoolName()}).`);
}
if (bpChkShowDebugWarnings) {
// Send the pool object to the debug window on startup
node.send({
"payload": {
"pool": node.pool
}
});
}
} catch (error) {
logError("Error in Streams Timer:", error.message || error);
}
}
async function pushDataToUploadQueue() {
try {
let streams = node.pool.getStreamKeyList();
for (const stream of streams) {
if (stream) {
await enqueueStreamDataForUpload(stream);
}
}
} catch (error) {
logError("Error in Upload Queue Timer:", error.message || error);
}
}
async function uploadQueuedDataNow() {
try {
if (enqueuedStreamBlocks.length > 0) {
const maxNumberOfStreamsPerBulkPost = DATA_UPLOAD_CHUNK_SIZE;
while (enqueuedStreamBlocks.length > 0) {
const chunk = enqueuedStreamBlocks.splice(0, maxNumberOfStreamsPerBulkPost);
const result = await postBulkStreamData(chunk);
if (!result) {
logWarn(`Failed to upload stream data`);
}
}
}
} catch (error) {
logError("Error in Upload Post Timer:", error.message || error);
} finally {
enqueuedStreamBlocks = [];
}
}
node.on('input', async function (msg, send, done) {
if (node.initComplete) {
try {
if (msg.BPCommand == "REBUILD") {
logInfo("The REBUILD command is no longer supported (use CREATE_CONFIG). Note, CREATE_CONFIG will delete all exsiting configuration data.")
} else if (msg.BPCommand == "SHOW_CONFIG") { // ----------------------------------------------
send({
"payload": {
"config": {
"api_key": apiKeyConfig.api_key,
"pool_name": pool_name,
"api_endpoint_v2": node.rootUrlv2,
"api_endpoint_v3": node.rootUrlv3,
"upload_on_expired_secs": uploadIntervalSecs,
"http_timeout_secs": timeoutSecs,
"option_show_debug_warnings": bpChkShowDebugWarnings,
}
}
});
} else if (msg.BPCommand == "SHOW_POOL") { // ----------------------------------------------
send({
"payload": {
"pool": node.pool
}
});
} else if (msg.BPCommand == "SHOW_CACHE") { // ----------------------------------------------
const storedKeys = node.fileCache.listStoredKeys();
if (storedKeys && storedKeys.size > 0) {
let foundAnEntry = false
for (const [key, value] of storedKeys) {
try {
if (node.fileCache.streamKeyExists(key)) {
let dataType = node.pool.getStreamByNameOrKey(key).getDataType();
if (dataType) {
const dataEntries = await node.fileCache.retrieveData(key, dataType);
node.send({
"payload": {
"key": key,
"data": dataEntries
}
});
foundAnEntry = true
}
}
} catch (error) { }
}
if (!foundAnEntry) {
logInfo(`Did not find cached data for this pool.`);
}
} else {
logInfo(`There are no stored cached records.`);
}
} else if (msg.BPCommand == "CREATE_POOL") { // ----------------------------------------------
node.pool = new Pool();
node.initComplete = false;
initializeNode();
const poolName = node.pool.getPoolName();
if (poolName) {
logInfo(`Rebuilt the pool (${poolName}).`);
} else {
logInfo(`Built a new pool.`);
}
} else if (msg.BPCommand == "SAVE_POOL") { // ----------------------------------------------
logInfo("The command SAVE_POOL is no longer supported in this version.")
} else if (msg.BPCommand == "PURGE_DATA") { // ----------------------------------------------
node.pool.purgeRecordsOnly();
node.fileCache.purge();
logInfo(`Purged all record data stored on this pool (${node.pool.getPoolName()}) node.`);
} else if (msg.BPCommand == "UPLOAD_DATA") { // ----------------------------------------------
try {
await pushDataToUploadQueue();
await uploadQueuedDataNow();
logInfo(`Uploaded all record data stored on this pool (${node.pool.getPoolName()}) node.`);
} catch (error) {
logError('Did not find stored pool configuration, will configure using defaults.', error || error.message);
}
} else {
if (msg.topic && msg.topic.trim() !== "") {
if (node.initComplete) {
let streamName = msg.topic;
let areStreamTagsPosted = false;
node.pool.addStreamName(streamName);
stream = node.pool.getStreamByNameOrKey(streamName);
let streamKey = null;
let utcSeconds = msg.utcSeconds !== undefined ? msg.utcSeconds : null;
if (utcSeconds !== null) {
let recordDate = new Date(utcSeconds * 1000);
stream.addData(msg.payload, recordDate);
} else {
stream.addData(msg.payload);
}
streamKey = stream.getStreamKey();
areStreamTagsPosted = stream.getTagsPosted();
if (!streamKey) {
// Ensure that only one stream is created at a time, else you will ddos the api
// Other events will continue to append data until the stream is configured.
// A Stream Key only exists if it has been configured using the Stream Name.
let release;
try {
release = await node.mutex.acquire();
} catch (error) {
return;
}
try {
let poolKey = node.pool.getPoolKey();
let stationId = node.pool.getStationId();
let dataType = node.pool.getStreamByNameOrKey(streamName).getDataType();
if (poolKey && stationId && streamName && dataType) {
let stream = await postAddUpdateStream(poolKey, stationId, streamName, dataType);
node.pool.updateStreamKey(streamName, stream.StreamKey);
}
} catch (error) { }
finally {
release();
}
}
// Check if tags have been posted. Do one off post if not
if (!areStreamTagsPosted && streamKey) {
node.pool.getStreamByNameOrKey(streamName).setTagsPosted(true);
let streamTags = msg.meta?.split(/\s*,\s*/) || null;
if (streamTags) {
streamTags = _removeNameSpacePrefix(streamTags);
streamTags = [...new Set(streamTags)];
}
node.pool.getStreamByNameOrKey(streamName).updateTags(streamTags);
await postAddStreamTags(streamKey, streamTags)
}
} else {
updateStatusMsg({ fill: "red", shape: "dot", text: "Pool not initialised" });
}
} else {
logWarn("Input topic is empty, skipping...");
}
}
} catch (error) {
logError("Error on input:", error.message || error);
}
finally {
if (done) {
done();
}
}
} else {
// ignore incoming events until init complete
}
});
async function enqueueStreamDataForUpload(key) {
try {
const stream = node.pool.getStreamByNameOrKey(key);
const streamRecordCount = stream.getRecordCount()
if (streamRecordCount > 0) {
let streamKey = key;
let data = stream.getAllValues();
let length = data.length;
if (length > 0) {
enqueuedStreamBlocks.push({ streamKey, data });
}
}
} catch (error) {
logError("Error in Enqueue Data:", error.message || error);
}
}
node.on('close', async function () {
try {
stopAllTimers();
enqueuedStreamBlocks = [];
} catch (error) {
logError("Error in On Close:", error.message || error);
}
});
// HTTP Functions------------------------------------------------------------------------------------------------------------------------------
async function getPoolStreams(poolKey) {
const uri = `${node.rootUrlv2}pools/${poolKey}/streams`;
try {
const res = await axiosInstance.get(uri);
if (res.status == 200) {
const payload = res.data;
return payload;
} else {
const text = res.data ? JSON.stringify(res.data) : "No response";
logWarn("Error loading streams: ", `${res.status}: ${text} `);
throw res;
}
} catch (error) {
logError(`Error loading streams: ${uri}`, error.message || error);
throw error;
}
}
async function postAddUpdatePool(poolName) {
const uri = `${node.rootUrlv2}pools`;
try {
const res = await axiosInstance.post(uri, {
Poolname: poolName,
Public: public,
Virtual: virtual
});
if (res.status == 200) {
const payload = res.data;
return payload;
} else {
const text = res.data ? JSON.stringify(res.data) : "No response";
logWarn("Error loading pool: ", `${res.status}: ${text} `);
throw res;
}
} catch (error) {
logError(`Error loading pool: ${uri}`, error.message || error);
throw error;
}
}
async function postAddPoolTags(poolKey, poolTags) {
const uri = `${node.rootUrlv3}pool/${poolKey}/tags`;
try {
if (poolTags.length > 0) {
tags = fixTags(poolTags);
const res = await axiosInstance.post(uri, tags);
if (res.status == 204 || res.status == 200) {
return true;
} else {
logWarn("Error pushing tags to pool: ", `${res.status}: ${text}`);
return false;
}
}
} catch (error) {
logError(`Error loading pool: ${uri}`, error.message || error);
return false
}
}
async function postAddUpdateStation(poolKey, poolName) {
const uri = `${node.rootUrlv2}pools/${poolKey}/stations`;
const stationName = cleanString(poolName || "1");
try {
const res = await axiosInstance.post(uri, JSON.stringify(stationName));
if (res.status == 200) {
const payload = res.data;
return payload.StationID;
} else {
const text = res.data ? JSON.stringify(res.data) : "No response";
logWarn("Error creating station: ", `${res.status}: ${text}`);
throw res;
}
} catch (error) {
logError(`Error loading station: ${uri}`, error.message || error);
}
}
async function postAddUpdateStream(poolKey, stationId, streamName, dataType) {
const uri = `${node.rootUrlv2}pools/${poolKey}/stations/${stationId}/streams`;
try {
const res = await axiosInstance.post(uri, {
"LocalIndex": streamName,
"StreamName": streamName,
"Description": streamName,
"Public": public,
"DataType": dataType
});
if (res.status == 200) {
return res.data;
} else {
const text = res.data ? JSON.stringify(res.data) : "No response";
logWarn("Error creating stream: ", `${res.status}: ${text}`);
throw res;
}
} catch (error) {
logError(`Error loading stream: ${uri} streamName: ${streamName} dataType: ${dataType}`, error.message || error);
throw error;
}
}
async function postAddStreamTags(streamKey, tags) {
const uri = `${node.rootUrlv3}stream/${streamKey}/tags`;
try {
if (tags && tags.length > 0) {
tags = fixTags(tags);
const res = await axiosInstance.post(uri, tags);
if (res.status == 204 || res.status == 200) {
return true;
} else {
logWarn("Error pushing tags to stream: ", `${res.status}: ${text}`);
return false;
}
}
} catch (error) {
logError(`Error loading tags: ${uri}`, error.message || error);
return false;
}
}
async function postBulkStreamData(enqueuedStreamBlocks) {
const maxConcurrentRequests = DATA_UPLOAD_CHUNK_SIZE;
async function uploadBlock(pair) {
const uri = `${node.rootUrlv2}streams/${pair.streamKey}/logs`;
const streamKey = pair.streamKey;
const newData = pair.data;
let allDataToSend = [];
try {
// Get cached data if available
const dataType = node.pool.getStreamByNameOrKey(streamKey)?.getDataType();
if (node.fileCache.streamKeyExists(streamKey) && dataType) {
try {
const cachedData = await node.fileCache.retrieveData(streamKey, dataType);
allDataToSend.push(...cachedData);
} catch (cacheErr) {
logError(`Failed to read cache for stream ${streamKey}`, cacheErr.message);
}
}
allDataToSend.push(...newData);
if (allDataToSend.length === 0) {
logInfo(`No data to upload for stream ${streamKey}`);
return;
}
const body = JSON.stringify(allDataToSend);
const res = await axiosInstance.post(uri, body);
// Print the entire axios response object with streamKey etc to assist with debugging
if (bpChkShowDebugWarnings) {
streamName = node.pool.getStreamNameByKey(streamKey) || "Unknown";
streamData = { allDataToSend, body };
node.warn({ streamName, streamKey, streamData, ...res });
}
if (res.status === 200) {
if (node.fileCache.streamKeyExists(streamKey)) {
await node.fileCache.deleteFile(streamKey);
}
} else {
throw new Error(`Upload failed with status ${res.status}`);
}
} catch (error) {
try {
await node.fileCache.appendData(streamKey, pair.data);
} catch (cacheWriteErr) {
logError(`Failed to cache unsent data for stream ${streamKey}`, cacheWriteErr.message);
}
}
}
try {
for (let i = 0; i < enqueuedStreamBlocks.length; i += maxConcurrentRequests) {
const chunk = enqueuedStreamBlocks.slice(i, i + maxConcurrentRequests);
await Promise.all(chunk.map(pair => uploadBlock(pair)));
}
return true;
} catch (error) {
logError("Error posting bulk data: ", error.message || error);
throw error;
} finally {
enqueuedStreamBlocks.length = 0;
}
}
// Helper Functions------------------------------------------------------------------------------------------------------------------------------
function stopAllTimers() {
try {
Object.keys(timers).forEach(key => {
clearInterval(timers[key]);
delete timers[key];
});
} catch (error) {
logError("Error in Stop All Timers:", error.message || error);
}
}
function cleanUserInput(input) {
if (typeof input !== 'string') {
return '';
}
// Remove non-printable characters, including control characters
return input.replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim();
}
function isValidApiKey(apiKey) {
if (apiKey == null) { // Check for null or undefined
return false;
}
const apiKeyRegex = /^Bitpool2 [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[4][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
return apiKeyRegex.test(apiKey);
}
function isValidPoolName(poolName) {
// Check if poolName is not null, not undefined, not empty, not just numbers, and its length is <= 255
return poolName != null && typeof poolName === 'string' && poolName.length > 0 && poolName.length <= 255 && !/^\d+$/.test(poolName);
}
function fixTags(tags) {
// Fix tags format
for (let i = 0; i < tags.length; i++) {
// If tag start with not alpha, add 'a' to it
if (!/^[a-zA-Z]/.test(tags[i])) {
tags[i] = 'a' + tags[i];
}
// convert tags to camelCase
if (tags[i].includes("=")) {
const parts = tags[i].split("=").map(x => x.trim());
tags[i] = `${camelCase(parts[0])}=${parts[1]}`;
} else {
tags[i] = camelCase(tags[i].trim());
}
}
return tags;
}
function updateStatusMsg(jsonProps) {
node.status({ fill: jsonProps.fill, shape: jsonProps.shape, text: jsonProps.text });
}
function logError(param1, param2) {
if (bpChkShowDebugWarnings) {
if (param1) node.warn(`ERROR: ${param1}`);
if (param2) node.warn(`${param2.response?.data ? JSON.stringify(param2.response?.data) : param2.response ? JSON.stringify(param2.response) : JSON.stringify(param2)}`);
}
}
function logWarn(param1, param2) {
if (bpChkShowDebugWarnings) {
if (param1) node.warn(`WARNING: ${param1}`);
if (param2) node.warn(`${param2.response?.data ? JSON.stringify(param2.response?.data) : param2.response ? JSON.stringify(param2.response) : JSON.stringify(param2)}`);
}
}
function logInfo(param1) {
if (bpChkShowDebugWarnings) {
node.warn(`INFO: ${param1}`)
}
}
function _removeNameSpacePrefix(tags) {
for (let index = 0; index < tags.length; index++) {
if (tags[index].includes(":")) {
const splitTag = tags[index].split(":")
tags[index] = splitTag[1];
}
};
return tags;
}
function isValidGUID(str) {
const guidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[4][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
return guidRegex.test(str);
}
function cleanString(inputString) {
// Remove unwanted backslashes and extra double quotes
return inputString.replace(/["\\]/g, '');
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function formatDateToDdHhMmSs(seconds) {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
const formattedHours = String(hours).padStart(2, '0');
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(secs).padStart(2, '0');
if (days > 0) {
return `${days} ${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
} else {
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
}
}
};
RED.nodes.registerType('uploader', BITPOOL);
}