remove now-unused function, consolidate remaining common functions
[squeep-mystery-box] / lib / mystery-box.js
index ddae8155e1cc29cf385e0390a7ca1c178f392d2d..a2ea8a82b05919ccac0c166f681cfe2a64b56fce 100644 (file)
@@ -4,7 +4,6 @@ const { EventEmitter } = require('events');
 const crypto = require('crypto');
 const zlib = require('zlib');
 const { promisify } = require('util');
-const common = require('./common');
 const { MysteryBoxError } = require('./errors');
 const allVersions = require('./version-parameters');
 const { performance } = require('perf_hooks');
@@ -20,6 +19,7 @@ const brotliDecompressAsync = promisify(zlib.brotliDecompress);
 const deflateRawAsync = promisify(zlib.deflateRaw);
 const inflateRawAsync = promisify(zlib.inflateRaw);
 const scryptAsync = promisify(crypto.scrypt);
+const randomBytesAsync = promisify(crypto.randomBytes);
 
 /**
  * Only you will know what's inside your...
@@ -63,7 +63,6 @@ const compressionFlagsShift = 0;
 const payloadFlagsMask = (availableFlags.BufferPayload);
 const payloadFlagsShift = 7;
 
-
 class MysteryBox extends EventEmitter {
   /**
    * @param {Object} options
@@ -72,7 +71,7 @@ class MysteryBox extends EventEmitter {
    */
   constructor(options = {}, ...args) {
     super(...args);
-    this.secrets = common.ensureArray(options.encryptionSecret);
+    this.secrets = MysteryBox._ensureArray(options.encryptionSecret);
     if (!this.secrets.length) {
       throw new MysteryBoxError('missing encryption secret');
     }
@@ -105,6 +104,21 @@ class MysteryBox extends EventEmitter {
   }
 
 
+  /**
+   * Return an array containing x if x is something and not an array
+   * @param {*} x
+   */
+  static _ensureArray(x) {
+    if (x === undefined) {
+      return [];
+    }
+    if (!Array.isArray(x)) {
+      return Array(x);
+    }
+    return x;
+  }
+
+
   /**
    * Parse the bits out of the flags.
    */
@@ -327,7 +341,7 @@ class MysteryBox extends EventEmitter {
     const [iv, salt] = await Promise.all([
       v.ivBytes,
       v.saltBytes,
-    ].map((b) => common.randomBytesAsync(b)));
+    ].map((b) => randomBytesAsync(b)));
 
     timingsMs.preCompress = performance.now();
     let compressedContents;