-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
191 lines (164 loc) · 4.56 KB
/
utils.js
File metadata and controls
191 lines (164 loc) · 4.56 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
const _ = require('lodash');
const https = require('https');
/**
* Stop execution for a given number of milliseconds
*
* @param {number} ms - number of milliseconds to stop execution
* @returns {Promise<void>}
*/
module.exports.sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
/**
* Make `deepDiff` exportable
* return {object}
*/
module.exports.deepDiff = deepDiff;
/**
* Make `deepMerge` exportable
* return {object}
*/
module.exports.deepMerge = deepMerge;
/**
* Recursively scans two variables and returns another object with diffs
*
* @param {object|Array} source - source object
* @param {object|Array} target - target object
* @param {Array<string>} [requiredFields] - fields to leave in diff object
* @returns {object}
*/
function deepDiff(source, target, requiredFields= []) {
if (typeOf(target) === 'array') {
return arrayDiff(source, target);
} else if (typeOf(target) === 'object') {
return objectDiff(source, target, requiredFields);
} else if (source !== target) {
return target;
} else {
return source;
}
}
/**
* Returns two arrays difference as an new array
*
* @param {Array} source - source object
* @param {Array} target - target object
* @returns {Array}
*/
function arrayDiff(source, target) {
const diffArray = [];
for (let i = 0; i < target.length; i++) {
diffArray[i] = deepDiff(source[i], target[i]);
}
return diffArray;
}
/**
* Returns two objects difference as new object
*
* @param {object} objectA - first object for comparing
* @param {object} objectB - second object for comparing
* @param {Array<string>} requiredFields - fields to leave
*
* @returns {object}
*/
function objectDiff(objectA, objectB, requiredFields = []) {
const diffObject = {};
/**
* objectA is a subject,
* we compare objectB patches
*
* For that we enumerate objectB props and assume that
* target object has any changes
*
* But target object might have additional patches that might not be in subject
* This corner case says us that whole property is a patch
*/
if (!objectA) {
return objectB;
}
Object.keys(objectB).forEach((prop) => {
const objectAItem = objectA[prop];
const objectBItem = objectB[prop];
if (objectAItem === undefined) {
diffObject[prop] = objectBItem;
return;
}
if (objectAItem === objectBItem) {
if (requiredFields.includes(prop)) {
diffObject[prop] = objectAItem;
}
return;
}
diffObject[prop] = deepDiff(objectAItem, objectBItem);
});
return diffObject;
}
/**
* Merge to objects recursively
*
* @param {object} target - target object
* @param {object[]} sources - sources for mering
* @returns {object}
*/
function deepMerge(target, ...sources) {
const isObject = (item) => item && typeOf(item) === 'object';
return _.mergeWith({}, target, ...sources, function (_subject, _target) {
if (_.isArray(_subject) && _.isArray(_target)) {
const biggerArray = _subject.length > _target.length ? _subject : _target;
const lesser = _subject.length > _target.length ? _target : _subject;
return biggerArray.map((el, i) => {
if (isObject(el) && isObject(lesser[i])) {
return _.mergeWith({}, el, lesser[i]);
} else {
return el;
}
});
}
});
}
/**
* Returns real type of passed variable
*
* @param {*} obj - value to check
* @returns {string}
*/
function typeOf(obj) {
return Object.prototype.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
/**
* Sends alert to the Slack/Telegram
*
* @param {string} text - message to send
* @returns {Promise<void>}
*/
module.exports.sendReport = async function sendReport(text) {
const message = `🦩 Hawk workers | ${text}`;
const postData = 'parse_mode=Markdown&message=' + encodeURIComponent(message);
const endpoint = process.env.CODEX_BOT_WEBHOOK;
if (!endpoint) {
return;
}
return new Promise((resolve) => {
const request = https.request(endpoint, {
method: 'POST',
timeout: 3000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}, (response) => {
response.setEncoding('utf8');
response.on('data', (chunk) => {
console.log('📤 Reporting:', chunk);
resolve();
});
});
request.on('error', (e) => {
console.log('📤 Reporting failed:', e);
/**
* Does not throw error, so we don't need to catch it higher
* and the application will not exit
*/
resolve();
});
request.write(postData);
request.end();
});
};