const { common } = require('@squeep/api-dingus');
-const { randomBytes } = require('crypto');
-const { promisify } = require('util');
+const { randomBytes } = require('node:crypto');
+const { promisify } = require('node:util');
const randomBytesAsync = promisify(randomBytes);
-/**
- * Pick out useful axios response fields.
- * @param {*} res
- * @returns
- */
-const axiosResponseLogData = (res) => {
- const data = common.pick(res, [
- 'status',
- 'statusText',
- 'headers',
- 'elapsedTimeMs',
- 'data',
- ]);
- if (data.data) {
- data.data = logTruncate(data.data, 100);
- }
- return data;
-};
-
/**
* Limit length of string to keep logs sane
* @param {String} str
const freezeDeep = (o) => {
Object.freeze(o);
Object.getOwnPropertyNames(o).forEach((prop) => {
- if (Object.hasOwnProperty.call(o, prop)
+ if (Object.hasOwn(o, prop)
&& ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection
&& !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection
return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection
return Math.ceil(dateMs / 1000);
};
+
+const omit = (o, props) => {
+ return Object.fromEntries(Object.entries(o).filter(([k]) => !props.includes(k)));
+};
+
+
+/**
+ * Log Mystery Box statistics events.
+ * @param {Console} logger
+ * @param {String} scope
+ */
+const mysteryBoxLogger = (logger, scope) => {
+ return (s) => {
+ logger.debug(scope, `${s.packageName}@${s.packageVersion}:${s.method}`, omit(s, [
+ 'packageName',
+ 'packageVersion',
+ 'method',
+ ]));
+ };
+};
+
+
+const nop = () => { /**/ };
+
module.exports = {
...common,
- axiosResponseLogData,
camelfy,
dateToEpoch,
ensureArray,
freezeDeep,
logTruncate,
+ mysteryBoxLogger,
newSecret,
+ omit,
randomBytesAsync,
validScope,
validError,
+ nop,
};