initial commit
[squeep-indieauth-helper] / lib / common.js
diff --git a/lib/common.js b/lib/common.js
new file mode 100644 (file)
index 0000000..0a8723b
--- /dev/null
@@ -0,0 +1,86 @@
+'use strict';
+
+const path = require('path');
+
+/**
+ * Return a function which combines a part of the filename with a scope, for use in logging.
+ * @param {string} filename
+ */
+const fileScope = (filename) => {
+  let fScope = path.basename(filename, '.js');
+  if (fScope === 'index') {
+    fScope = path.basename(path.dirname(filename));
+  }
+  return (scope) => `${fScope}:${scope}`;
+}
+
+
+/**
+ * Convert Base64 to Base64URL.
+ * @param {String} input
+ * @returns {String}
+ */
+const base64ToBase64URL = (input) => {
+  return input
+    .replace(/=/g, '')
+    .replace(/\+/g, '-')
+    .replace(/\//g, '_');
+};
+
+
+/**
+ * Pick out useful axios response fields.
+ * @param {*} res
+ * @returns
+ */
+const axiosResponseLogData = (res) => {
+  const data = 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
+ * @param {Number} len
+ * @returns {String}
+ */
+const logTruncate = (str, len) => {
+  if (typeof str !== 'string' || str.toString().length <= len) {
+    return str;
+  }
+  return str.toString().slice(0, len) + `... (${str.toString().length} bytes)`;
+};
+
+
+/**
+ * Return a new object with selected props.
+ * @param {Object} obj
+ * @param {String[]} props
+ */
+const pick = (obj, props) => {
+  return props.reduce((acc, prop) => {
+    if (prop in obj) {
+      acc[prop] = obj[prop]; // eslint-disable-line security/detect-object-injection
+    }
+    return acc;
+  }, {});
+};
+
+
+module.exports = {
+  fileScope,
+  base64ToBase64URL,
+  axiosResponseLogData,
+  logTruncate,
+  pick,
+};
\ No newline at end of file