From: Justin Wind Date: Tue, 14 Mar 2023 22:43:59 +0000 (-0700) Subject: constructor no longer accepts logger, emit statistics events instead X-Git-Tag: v2.0.0~1^2~2 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=2ef5f9c74d92c8eb8cc0bd03fecc9d1a1d306489;p=squeep-mystery-box constructor no longer accepts logger, emit statistics events instead --- diff --git a/lib/mystery-box.js b/lib/mystery-box.js index abef55c..5dcf1b1 100644 --- a/lib/mystery-box.js +++ b/lib/mystery-box.js @@ -1,13 +1,18 @@ 'use strict'; +const { EventEmitter } = require('events'); const crypto = require('crypto'); const zlib = require('zlib'); const { promisify } = require('util'); const common = require('./common'); const allVersions = require('./version-parameters'); const { performance } = require('perf_hooks'); +const { name: packageName, version: packageVersion } = require('../package.json'); -const _fileScope = common.fileScope(__filename); +const packageInfo = { + packageName, + packageVersion, +}; const brotliCompressAsync = promisify(zlib.brotliCompress); const brotliDecompressAsync = promisify(zlib.brotliDecompress); @@ -58,17 +63,14 @@ const payloadFlagsMask = (availableFlags.BufferPayload); const payloadFlagsShift = 7; -class MysteryBox { +class MysteryBox extends EventEmitter { /** - * @param {Console} logger * @param {Object} options * @param {String|String[]} options.encryptionSecret - if an array, will always encrypt with first secret, will attempt to decrypt with all; useful for rolling secrets * @param {Number=} options.defaultFlags */ - constructor(logger, options = {}) { - this.logger = logger; - this.options = options; - + constructor(options = {}, ...args) { + super(...args); this.secrets = common.ensureArray(options.encryptionSecret); if (!this.secrets.length) { throw new Error('missing encryption secret'); @@ -257,6 +259,32 @@ class MysteryBox { } + /** + * Stats tracked when packing/unpacking boxes. + * @returns {Object} + */ + static _newStats(method) { + return { + stats: { + method, + version: undefined, + flags: undefined, + flagsRaw: undefined, + serializedBytes: undefined, + compressedBytes: undefined, + }, + timingsMs: { + start: performance.now(), + preCompress: 0, + postCompress: 0, + preCrypt: 0, + postCrypt: 0, + end: 0, + }, + }; + } + + /** * Put contents into a mysterious box. * @param {Object|Buffer} contents @@ -265,27 +293,14 @@ class MysteryBox { * @returns {String} */ async pack(contents, version = this.bestVersion, flags = this.defaultFlags) { - const _scope = _fileScope('pack'); - const timingsMs = { - start: performance.now(), - preCompress: 0, - postCompress: 0, - preCrypt: 0, - postCrypt: 0, - end: 0, - }; - const stats = { - version, - flags: undefined, - serializedBytes: undefined, // original contents size in bytes - compressedBytes: undefined, // compressed contents size in bytes - }; + const { stats, timingsMs } = MysteryBox._newStats('pack'); if (!(version in this.versionParameters)) { throw new RangeError(`MysteryBox format version ${version} not supported`); } // eslint-disable-next-line security/detect-object-injection const v = this.versionParameters[version]; + stats.version = version; const { compression, payloadIsBuffer } = MysteryBox._decodeFlags(flags); @@ -342,6 +357,7 @@ class MysteryBox { const flagsBuffer = Buffer.alloc(v.flagsBytes); flagsBuffer.writeUInt8(flags, 0); + stats.flagsRaw = flags; stats.flags = this._prettyFlags(flags); // Authenticate all this data @@ -359,7 +375,7 @@ class MysteryBox { const result = Buffer.concat([versionBuffer, flagsBuffer, iv, salt, tag, encrypted, final]).toString('base64url'); timingsMs.end = timingsMs.postCrypt = performance.now(); - this.logger.debug(_scope, 'statistics', { ...stats, ...MysteryBox._timingsLog(timingsMs) }); + this.emit('statistics', { ...stats, ...MysteryBox._timingsLog(timingsMs), ...packageInfo }); return result; } @@ -371,15 +387,7 @@ class MysteryBox { * @returns {Object} */ async unpack(box) { - const _scope = _fileScope('unpack'); - const timingsMs = { - start: performance.now(), - preCompress: 0, - postCompress: 0, - preCrypt: 0, - postCrypt: 0, - end: 0, - }; + const { stats, timingsMs } = MysteryBox._newStats('unpack'); if (!box) { throw new RangeError('nothing to unpack'); @@ -399,6 +407,7 @@ class MysteryBox { throw new Error('internal inconsistency, mismatched version byte length'); } offset += v.versionBytes; + stats.version = version; const minBytes = v.versionBytes + v.flagsBytes + v.ivBytes + v.saltBytes + v.tagBytes; if (raw.length < minBytes) { @@ -407,6 +416,7 @@ class MysteryBox { const flags = raw.subarray(offset, offset + v.flagsBytes).readUInt8(0); offset += v.flagsBytes; + stats.flagsRaw = flags; const { compression, payloadIsBuffer } = MysteryBox._decodeFlags(flags); @@ -461,12 +471,17 @@ class MysteryBox { break; } timingsMs.end = timingsMs.postCompress = performance.now(); + stats.serializedBytes = payload.byteLength; + if (compression) { + stats.compressedBytes = decrypted.byteLength; + } if (!payloadIsBuffer) { payload = JSON.parse(payload.toString('utf8')); } - this.logger.debug(_scope, 'statistics', { version, flags: this._prettyFlags(flags), ...MysteryBox._timingsLog(timingsMs) }); + stats.flags = this._prettyFlags(flags); + this.emit('statistics', { ...stats, ...MysteryBox._timingsLog(timingsMs), ...packageInfo }); return payload; } @@ -504,7 +519,4 @@ class MysteryBox { } -// Expose for stubbing in tests -MysteryBox._test = { crypto }; - module.exports = MysteryBox; \ No newline at end of file diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index 9f48ff6..dd54a88 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -5,7 +5,13 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const MysteryBox = require('../../lib/mystery-box'); -const stubLogger = require('../stub-logger'); +const crypto = require('crypto'); + +function _verbose(mb) { + if (process.env.VERBOSE_TESTS) { + mb.on('statistics', (...args) => console.log(...args)); + } +} describe('MysteryBox', function () { let mb, options, object; @@ -13,7 +19,6 @@ describe('MysteryBox', function () { options = { encryptionSecret: 'this is not a very good secret', }; - mb = new MysteryBox(stubLogger, options); }); afterEach(function () { sinon.restore(); @@ -22,7 +27,7 @@ describe('MysteryBox', function () { describe('constructor', function () { it('needs a secret', async function () { options = {}; - assert.rejects(() => new MysteryBox(stubLogger, options)); + assert.rejects(() => new MysteryBox(options)); }); it('accepts multiple secrets', async function () { @@ -30,7 +35,8 @@ describe('MysteryBox', function () { options = { encryptionSecret: ['first poor secret', 'second poor secret'], }; - mb = new MysteryBox(stubLogger, options); + mb = new MysteryBox(options); + _verbose(mb); object = { foo: 'bar', baz: 'quux', @@ -42,17 +48,17 @@ describe('MysteryBox', function () { }); it('covers options', function () { - assert.rejects(() => new MysteryBox(stubLogger)); + assert.rejects(() => new MysteryBox()); }); it('covers bad flags', function () { options.defaultFlags = 300; - assert.rejects(() => new MysteryBox(stubLogger, options), RangeError); + assert.rejects(() => new MysteryBox(options), RangeError); }); it('covers missing ciphers', function () { - sinon.stub(MysteryBox._test.crypto, 'getCiphers').returns(['rot13']); - assert.rejects(() => new MysteryBox(stubLogger, options)); + sinon.stub(crypto, 'getCiphers').returns(['rot13']); + assert.rejects(() => new MysteryBox(options)); }); }); // constructor @@ -236,7 +242,8 @@ describe('MysteryBox', function () { describe('pack, unpack', function () { beforeEach(function () { - mb = new MysteryBox(stubLogger, options); + mb = new MysteryBox(options); + _verbose(mb); }); it('covers packing unsupported version', async function () { @@ -280,8 +287,10 @@ describe('MysteryBox', function () { it('decrypts secondary (older) secret', async function () { this.slow(500); - const oldmb = new MysteryBox(stubLogger, { encryptionSecret: 'old secret' }); - const newmb = new MysteryBox(stubLogger, { encryptionSecret: ['new secret', 'old secret'] }); + const oldmb = new MysteryBox({ encryptionSecret: 'old secret' }); + _verbose(oldmb); + const newmb = new MysteryBox({ encryptionSecret: ['new secret', 'old secret'] }); + _verbose(newmb); object = { foo: 'bar', baz: 'quux', @@ -294,8 +303,10 @@ describe('MysteryBox', function () { it('fails to decrypt invalid secret', async function () { this.slow(500); - const oldmb = new MysteryBox(stubLogger, { encryptionSecret: 'very old secret' }); - const newmb = new MysteryBox(stubLogger, { encryptionSecret: ['new secret', 'old secret'] }); + const oldmb = new MysteryBox({ encryptionSecret: 'very old secret' }); + _verbose(oldmb); + const newmb = new MysteryBox({ encryptionSecret: ['new secret', 'old secret'] }); + _verbose(newmb); object = { foo: 'bar', baz: 'quux', @@ -305,7 +316,7 @@ describe('MysteryBox', function () { assert.rejects(() => newmb.unpack(oldEncrypted)); }); - it('encrypts and decrypts all available versions +brotli', async function () { + it('encrypts and decrypts all available versions no compression', async function () { Object.keys(mb.versionParameters).map((v) => Number(v)).forEach(async (version) => { object = { foo: 'bar', @@ -318,6 +329,19 @@ describe('MysteryBox', function () { }); }); + it('encrypts and decrypts all available versions +brotli', async function () { + Object.keys(mb.versionParameters).map((v) => Number(v)).forEach(async (version) => { + object = { + foo: 'bar', + baz: 'quux', + flarp: 13, + }; + const encryptedResult = await mb.pack(object, version, mb.Flags.Brotli); + const decryptedResult = await mb.unpack(encryptedResult); + assert.deepStrictEqual(decryptedResult, object, `${version} results not symmetric`); + }); + }); + it('encrypts and decrypts all available versions +flate', async function () { Object.keys(mb.versionParameters).map((v) => Number(v)).forEach(async (version) => { object = { @@ -325,7 +349,7 @@ describe('MysteryBox', function () { baz: 'quux', flarp: 13, }; - const encryptedResult = await mb.pack(object, version, 0x01); + const encryptedResult = await mb.pack(object, version, mb.Flags.Flate); const decryptedResult = await mb.unpack(encryptedResult); assert.deepStrictEqual(decryptedResult, object, `${version} results not symmetric`); }); @@ -341,7 +365,7 @@ describe('MysteryBox', function () { object = { longProperty: 'x'.repeat(384 * 1024).split('').map(() => String.fromCharCode(rnd())).join(''), }; - const encryptedResult = await mb.pack(object, mb.bestVersion, 0x00); + const encryptedResult = await mb.pack(object, mb.bestVersion, mb.Flags.Brotli); const decryptedResult = await mb.unpack(encryptedResult); assert.deepStrictEqual(decryptedResult, object); }); @@ -356,7 +380,7 @@ describe('MysteryBox', function () { object = { longProperty: 'x'.repeat(384 * 1024).split('').map(() => String.fromCharCode(rnd())).join(''), }; - const encryptedResult = await mb.pack(object, mb.bestVersion, 0x01); + const encryptedResult = await mb.pack(object, mb.bestVersion, mb.Flags.Flate); const decryptedResult = await mb.unpack(encryptedResult); assert.deepStrictEqual(decryptedResult, object); }); @@ -372,12 +396,14 @@ describe('MysteryBox', function () { }); it('covers internal error, incorrect version byte size, pack', async function () { + this.slow(500); const version = 1; sinon.stub(mb.versionParameters[version], 'versionBytes').value(10); assert.rejects(() => mb.pack({}, version), Error); }); it('covers internal error, incorrect version byte size, unpack', async function () { + this.slow(500); const version = 1; const encryptedResult = await mb.pack({}, version); sinon.stub(mb.versionParameters[version], 'versionBytes').value(10); diff --git a/test/stub-logger.js b/test/stub-logger.js deleted file mode 100644 index 5ac0b98..0000000 --- a/test/stub-logger.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require - -const nop = () => { /* */ }; -const stubLogger = process.env.VERBOSE_TESTS ? console : { - debug: nop, - error: nop, - info: nop, -}; -stubLogger['_reset'] = () => { - sinon.spy(stubLogger, 'debug'); - sinon.spy(stubLogger, 'error'); - sinon.spy(stubLogger, 'info'); -}, - - -module.exports = stubLogger; \ No newline at end of file