log successful ticket delivery, update devDependencies
[squeep-indieauth-helper] / lib / common.js
index 0a8723b0115a5c2ba2cbd21313e0ad367b6a59fe..789aa65ce2b55db08f177e9c255171c2f0ddaa27 100644 (file)
@@ -1,48 +1,30 @@
 '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}
+ * Pick out useful got response fields.
+ * @param {GotResponse} res
+ * @returns {Object}
  */
-const base64ToBase64URL = (input) => {
-  return input
-    .replace(/=/g, '')
-    .replace(/\+/g, '-')
-    .replace(/\//g, '_');
-};
-
-
-/**
- * Pick out useful axios response fields.
- * @param {*} res
- * @returns
- */
-const axiosResponseLogData = (res) => {
+const gotResponseLogData = (res) => {
   const data = pick(res, [
-    'status',
-    'statusText',
+    'statusCode',
+    'statusMessage',
     'headers',
-    'elapsedTimeMs',
-    'data',
+    'body',
+    'url',
+    'error',
   ]);
-  if (data.data) {
-    data.data = logTruncate(data.data, 100);
+  if (typeof res.body === 'string') {
+    data.body = logTruncate(data.body, 100);
+  } else if (res.body instanceof Buffer) {
+    data.body = `<Buffer ${res.body.byteLength} bytes>`;
+  }
+  data.elapsedTimeMs = res?.timings?.phases?.total;
+  if (res?.redirectUrls?.length) {
+    data.redirectUrls = res.redirectUrls;
+  }
+  if (res?.retryCount) {
+    data.retryCount = res.retryCount;
   }
   return data;
 };
@@ -77,10 +59,43 @@ const pick = (obj, props) => {
 };
 
 
+/**
+ * Return a set containing non-shared items between two sets.
+ * @param {Set} a
+ * @param {Set} b
+ * @returns {Set}
+ */
+const setSymmetricDifference = (a, b) => {
+  const d = new Set(a);
+  for (const x of b) {
+    if (d.has(x)) {
+      d.delete(x);
+    } else {
+      d.add(x);
+    }
+  }
+  return d;
+};
+
+
+/**
+ * URL objects have weird names.
+ * @param {String} component
+ * @returns {String}
+ */
+const properURLComponentName = (component) => {
+  // eslint-disable-next-line security/detect-object-injection
+  return {
+    hash: 'fragment',
+    protocol: 'scheme',
+  }[component] || component;
+}
+
+
 module.exports = {
-  fileScope,
-  base64ToBase64URL,
-  axiosResponseLogData,
+  gotResponseLogData,
   logTruncate,
   pick,
-};
\ No newline at end of file
+  setSymmetricDifference,
+  properURLComponentName,
+};