From: Justin Wind Date: Wed, 15 Mar 2023 00:41:31 +0000 (-0700) Subject: remove now-unused function, consolidate remaining common functions X-Git-Tag: v2.0.0~1^2 X-Git-Url: http://git.squeep.com/?p=squeep-mystery-box;a=commitdiff_plain;h=82e47c29e2c11196901826e68c5dc9b090d8c214 remove now-unused function, consolidate remaining common functions --- diff --git a/lib/common.js b/lib/common.js deleted file mode 100644 index 1d4223b..0000000 --- a/lib/common.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -const path = require('path'); -const { randomBytes } = require('crypto'); -const { promisify } = require('util'); -const randomBytesAsync = promisify(randomBytes); - - -/** - * 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}`; -} - - -/** - * Return an array containing x if x is something and not an array - * @param {*} x - */ -const ensureArray = (x) => { - if (x === undefined) { - return []; - } - if (!Array.isArray(x)) { - return Array(x); - } - return x; -}; - - -module.exports = { - ensureArray, - fileScope, - randomBytesAsync, -}; \ No newline at end of file diff --git a/lib/mystery-box.js b/lib/mystery-box.js index ddae815..a2ea8a8 100644 --- a/lib/mystery-box.js +++ b/lib/mystery-box.js @@ -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; diff --git a/test/lib/common.js b/test/lib/common.js deleted file mode 100644 index ad93ea1..0000000 --- a/test/lib/common.js +++ /dev/null @@ -1,39 +0,0 @@ -/* eslint-env mocha */ -'use strict'; - -const assert = require('assert'); -const common = require('../../lib/common'); - -describe('Common', function () { - - describe('fileScope', function () { - it('names a file path', function () { - const filename = 'lib/foo/bar.js'; - const result = common.fileScope(filename)('baz'); - assert.strictEqual(result, 'bar:baz'); - }); - it('names an index path', function () { - const filename = 'lib/foo/index.js'; - const result = common.fileScope(filename)('baz'); - assert.strictEqual(result, 'foo:baz'); - }); - }); // fileScope - - describe('ensureArray', function () { - it('returns empty array for no data', function () { - const result = common.ensureArray(); - assert.deepStrictEqual(result, []); - }); - it('returns same array passed in', function () { - const expected = [1, 2, 3, 'foo']; - const result = common.ensureArray(expected); - assert.deepStrictEqual(result, expected); - }); - it('returns array containing non-array data', function () { - const data = 'bar'; - const result = common.ensureArray(data); - assert.deepStrictEqual(result, [data]); - }); - }); // ensureArray - -}); // Common diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index e949139..510362b 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -63,6 +63,23 @@ describe('MysteryBox', function () { }); }); // constructor + describe('_ensureArray', function () { + it('returns empty array for no data', function () { + const result = MysteryBox._ensureArray(); + assert.deepStrictEqual(result, []); + }); + it('returns same array passed in', function () { + const expected = [1, 2, 3, 'foo']; + const result = MysteryBox._ensureArray(expected); + assert.deepStrictEqual(result, expected); + }); + it('returns array containing non-array data', function () { + const data = 'bar'; + const result = MysteryBox._ensureArray(data); + assert.deepStrictEqual(result, [data]); + }); + }); // _ensureArray + describe('_keyFromSecret', function () { it('covers invalid', async function () { assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), MysteryBoxError);