f7c54ef85a9ba1f96306e4dfb938897f14868348
1 /* eslint-disable security/detect-object-injection */
5 * Assorted utility functions.
8 const { common
} = require('@squeep/api-dingus');
10 const { randomBytes
, getHashes
} = require('crypto');
11 const { promisify
} = require('util');
15 * Wrap this in a promise, as crypto.randomBytes is capable of blocking.
17 const randomBytesAsync
= promisify(randomBytes
);
21 * The HMAC hashes we are willing to support.
22 * @param {String} algorithm
25 const validHash
= (algorithm
) => getHashes()
26 .filter((h
) => h
.match(/^sha[0-9]+$/))
31 * Return an array containing x if x is not an array.
34 const ensureArray
= (x
) => {
35 if (x
=== undefined) {
38 if (!Array
.isArray(x
)) {
46 * Recursively freeze an object.
50 const freezeDeep
= (o
) => {
52 Object
.getOwnPropertyNames(o
).forEach((prop
) => {
53 if (Object
.hasOwnProperty
.call(o
, prop
)
54 && ['object', 'function'].includes(typeof o
[prop
])
55 && !Object
.isFrozen(o
[prop
])) {
56 return freezeDeep(o
[prop
]);
64 * Pick out useful got response fields.
68 const gotResponseLogData
= (res
) => {
69 const data
= common
.pick(res
, [
76 if (typeof res
.body
=== 'string') {
77 data
.body
= logTruncate(data
.body
, 100);
78 } else if (res
.body
instanceof Buffer
) {
79 data
.body
= `<Buffer ${res.body.byteLength} bytes>`;
81 if (res
?.timings
?.phases
?.total
) {
82 data
.elapsedTimeMs
= res
.timings
.phases
.total
;
84 if (res
?.redirectUrls
?.length
) {
85 data
.redirectUrls
= res
.redirectUrls
;
87 if (res
?.retryCount
) {
88 data
.retryCount
= res
.retryCount
;
95 * Fallback values, if not configured.
98 const topicLeaseDefaults
= () => {
99 return Object
.freeze({
100 leaseSecondsPreferred: 86400 * 10,
101 leaseSecondsMin: 86400 * 1,
102 leaseSecondsMax: 86400 * 365,
108 * Pick from a range, constrained, with some fuzziness.
109 * @param {Number} attempt
110 * @param {Number[]} delays
111 * @param {Number} jitter
114 const attemptRetrySeconds
= (attempt
, retryBackoffSeconds
= [60, 120, 360, 1440, 7200, 43200, 86400], jitter
= 0.618) => {
115 const maxAttempt
= retryBackoffSeconds
.length
- 1;
116 if (typeof attempt
!== 'number' || attempt
< 0) {
118 } else if (attempt
> maxAttempt
) {
119 attempt
= maxAttempt
;
121 // eslint-disable-next-line security/detect-object-injection
122 let seconds
= retryBackoffSeconds
[attempt
];
123 seconds
+= Math
.floor(Math
.random() * seconds
* jitter
);
129 * Return array items split as an array of arrays of no more than per items each.
130 * @param {Array} array
131 * @param {Number} per
133 const arrayChunk
= (array
, per
= 1) => {
134 const nChunks
= Math
.ceil(array
.length
/ per
);
135 return Array
.from(Array(nChunks
), (_
, i
) => array
.slice(i
* per
, (i
+ 1) * per
));
140 * Be paranoid about blowing the stack when pushing to an array.
144 const stackSafePush
= (dst
, src
) => {
145 const jsEngineMaxArguments
= 2**16; // Current as of Node 12
146 arrayChunk(src
, jsEngineMaxArguments
).forEach((items
) => {
147 Array
.prototype.push
.apply(dst
, items
);
153 * Limit length of string to keep logs sane
154 * @param {String} str
155 * @param {Number} len
158 const logTruncate
= (str
, len
) => {
159 if (typeof str
!== 'string' || str
.toString().length
<= len
) {
162 return str
.toString().slice(0, len
) + `... (${str.toString().length} bytes)`;