support providing multiple secrets, always encrypt with first, attempt decryption...
[squeep-mystery-box] / lib / common.js
index a47226f6e76fe355416380f3a77db24450481783..1d4223b1749d1cce37b729233f988098abc36b5a 100644 (file)
@@ -20,55 +20,22 @@ const fileScope = (filename) => {
 
 
 /**
- * Convert Base64 to Base64URL.
- * @param {String} input
- * @returns {String}
+ * Return an array containing x if x is something and not an array
+ * @param {*} x
  */
-const base64ToBase64URL = (input) => {
-  if (!input) {
-    return input;
+const ensureArray = (x) => {
+  if (x === undefined) {
+    return [];
   }
-  return input
-    .replace(/=/g, '')
-    .replace(/\+/g, '-')
-    .replace(/\//g, '_');
-};
-
-
-/**
- * Convert Base64URL to normal Base64.
- * @param {String} input
- * @returns {String}
- */
-const base64URLToBase64 = (input) => {
-  if (!input) {
-    return input;
-  }
-  return base64RePad(input)
-    .replace(/-/g, '+')
-    .replace(/_/, '/');
-};
-
-
-/**
- * Add any missing trailing padding which may have been removed from Base64URL encoding.
- * @param {String} input
- */
-const base64RePad = (input) => {
-  const blockSize = 4;
-  const lastBlockSize = input.length % blockSize;
-  if (lastBlockSize) {
-    const missing = blockSize - lastBlockSize;
-    return input + '='.repeat(missing);
+  if (!Array.isArray(x)) {
+    return Array(x);
   }
-  return input;
+  return x;
 };
 
 
 module.exports = {
-  base64ToBase64URL,
-  base64URLToBase64,
-  base64RePad,
+  ensureArray,
   fileScope,
   randomBytesAsync,
 };
\ No newline at end of file