bump package version to 1.5.0
[squeep-authentication-module] / lib / common.js
1 'use strict';
2
3 const { common } = require('@squeep/api-dingus');
4 const { randomBytes } = require('node:crypto');
5 const { promisify } = require('node:util');
6
7 const randomBytesAsync = promisify(randomBytes);
8
9 /**
10 * Recursively freeze an object.
11 * @param {object} o object to freeze
12 * @returns {object} frozen object
13 */
14 const freezeDeep = (o) => {
15 Object.freeze(o);
16 Object.getOwnPropertyNames(o).forEach((prop) => {
17 if (Object.hasOwn(o, prop)
18 && ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection
19 && !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection
20 return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection
21 }
22 });
23 return o;
24 };
25
26 /**
27 * Return a new object duplicating `o`, without the properties specified.
28 * @param {object} o source object
29 * @param {string[]} props list of property names to omit
30 * @returns {object} pruned object
31 */
32 const omit = (o, props) => {
33 return Object.fromEntries(Object.entries(o).filter(([k]) => !props.includes(k)));
34 };
35
36 /**
37 * Helper to log mystery-box statistics.
38 * @param {object} logger logger
39 * @param {Function} logger.debug log debug
40 * @param {string} scope scope
41 * @returns {Function} stats log decorator
42 */
43 const mysteryBoxLogger = (logger, scope) => {
44 return (s) => {
45 logger.debug(scope, `${s.packageName}@${s.packageVersion}:${s.method}`, omit(s, [
46 'packageName',
47 'packageVersion',
48 'method',
49 ]));
50 };
51 };
52
53 /**
54 * Hide sensitive part of an Authorization header.
55 * @param {string} authHeader header value
56 * @returns {string} scrubbed header value
57 */
58 const obscureAuthorizationHeader = (authHeader) => {
59 if (!authHeader) {
60 return authHeader;
61 }
62 const space = authHeader.indexOf(' ');
63 // This blurs entire string if no space found, because -1.
64 return authHeader.slice(0, space + 1) + '*'.repeat(authHeader.length - (space + 1));
65 };
66
67 module.exports = Object.assign(Object.create(common), {
68 freezeDeep,
69 mysteryBoxLogger,
70 obscureAuthorizationHeader,
71 omit,
72 randomBytesAsync,
73 });