minor doc update
[squeep-mystery-box] / lib / mystery-box.js
index ddae8155e1cc29cf385e0390a7ca1c178f392d2d..55d1c3827746ccbc35b2e2e64ce30e9df692888f 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.
    */
@@ -291,7 +305,7 @@ class MysteryBox extends EventEmitter {
    * @param {Object|Buffer} contents
    * @param {Number=} version
    * @param {Number=} flags
-   * @returns {String}
+   * @returns {Promise<String>}
    */
   async pack(contents, version = this.bestVersion, flags = this.defaultFlags) {
     const { stats, timingsMs } = MysteryBox._newStats('pack');
@@ -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;
@@ -385,7 +399,7 @@ class MysteryBox extends EventEmitter {
   /**
    * Take contents out of a mysterious box.
    * @param {String} box - Base64URL encoded payload
-   * @returns {Object}
+   * @returns {Promise<Object>}
    */
   async unpack(box) {
     const { stats, timingsMs } = MysteryBox._newStats('unpack');
@@ -508,7 +522,7 @@ class MysteryBox extends EventEmitter {
   /**
    * Everyone loves numbers.
    * @param {Object} timingsMs
-   * @returns 
+   * @returns {Object}
    */
   static _timingsLog({ start, preCompress, postCompress, preCrypt, postCrypt, end }) {
     return {
@@ -520,4 +534,4 @@ class MysteryBox extends EventEmitter {
 
 }
 
-module.exports = MysteryBox;
\ No newline at end of file
+module.exports = MysteryBox;