'use strict';
const MysteryBox = require('./lib/mystery-box');
+const { MysteryBoxError } = require('./lib/errors');
module.exports = {
MysteryBox,
+ MysteryBoxError,
};
--- /dev/null
+'use strict';
+
+class MysteryBoxError extends Error {
+ constructor(...args) {
+ super(...args);
+ Error.captureStackTrace(MysteryBoxError);
+ }
+
+ get name() {
+ return this.constructor.name;
+ }
+}
+
+module.exports = {
+ MysteryBoxError,
+};
\ No newline at end of file
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');
const { name: packageName, version: packageVersion } = require('../package.json');
super(...args);
this.secrets = common.ensureArray(options.encryptionSecret);
if (!this.secrets.length) {
- throw new Error('missing encryption secret');
+ throw new MysteryBoxError('missing encryption secret');
}
// Filter any unavailable algorithms
// Default to highest
this.bestVersion = Number(Object.keys(this.versionParameters).sort().pop());
if (Number.isNaN(this.bestVersion)) {
- throw new Error('no supported versions available');
+ throw new MysteryBoxError('no supported versions available');
}
this.Flags = availableFlags;
this.defaultFlags = 'defaultFlags' in options ? options.defaultFlags : availableFlags.Flate;
if (this.defaultFlags < 0 || this.defaultFlags > 255) {
- throw new RangeError('Invalid default flag value');
+ throw new MysteryBoxError('Invalid default flag value');
}
}
return scryptAsync(secret, salt, keyBytes);
default:
- throw new RangeError('unsupported key deriver');
+ throw new MysteryBoxError('unsupported key deriver');
}
}
}
}
// Nine bytes would be an extravagence.
- throw new RangeError(`unsupported version header (0x${firstByte.toString(16)})`);
+ throw new MysteryBoxError(`unsupported version header (0x${firstByte.toString(16)})`);
}
}
if (versionBytes > 6) {
- throw new RangeError(`unsupported version (${versionBytes} bytes)`);
+ throw new MysteryBoxError(`unsupported version (${versionBytes} bytes)`);
}
// Otherwise, update the masked first byte and parse the rest of the buffer.
} else if (version <= 0x03ffffffffff) { // 34359738368-4398046511103
versionBytes = 6;
} else {
- throw new RangeError('version too large to encode');
+ throw new MysteryBoxError(`version too large to encode (${version})`);
}
const buffer = Buffer.alloc(versionBytes);
const { stats, timingsMs } = MysteryBox._newStats('pack');
if (!(version in this.versionParameters)) {
- throw new RangeError(`MysteryBox format version ${version} not supported`);
+ throw new MysteryBoxError(`MysteryBox format version ${version} not supported`);
}
// eslint-disable-next-line security/detect-object-injection
const v = this.versionParameters[version];
const { buffer: versionBuffer, versionBytes } = MysteryBox._versionEncode(v.version);
if (versionBytes !== v.versionBytes) {
- throw new Error('internal inconsistency, mismatched version byte length');
+ throw new MysteryBoxError('internal inconsistency, mismatched version byte length');
}
const [iv, salt] = await Promise.all([
const { stats, timingsMs } = MysteryBox._newStats('unpack');
if (!box) {
- throw new RangeError('nothing to unpack');
+ throw new MysteryBoxError('nothing to unpack');
}
const raw = Buffer.from(box, 'base64url');
const { version, versionBytes } = MysteryBox._versionDecode(raw);
if (!(version in this.versionParameters)) {
- throw new RangeError('unsupported version');
+ throw new MysteryBoxError(`unsupported version (${version})`);
}
// eslint-disable-next-line security/detect-object-injection
const v = this.versionParameters[version];
if (v.versionBytes !== versionBytes) {
- throw new Error('internal inconsistency, mismatched version byte length');
+ throw new MysteryBoxError('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) {
- throw new RangeError('not enough to unpack');
+ throw new MysteryBoxError('not enough to unpack');
}
const flags = raw.subarray(offset, offset + v.flagsBytes).readUInt8(0);
--- /dev/null
+'use strict';
+
+const assert = require('assert');
+const { MysteryBoxError } = require('../../lib/errors');
+
+describe('Errors', function () {
+ it('MysteryBoxError', function () {
+ const e = new MysteryBoxError();
+ assert.strictEqual(e.name, 'MysteryBoxError');
+ });
+}); // Errors
\ No newline at end of file
const assert = require('assert');
const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
const MysteryBox = require('../../lib/mystery-box');
+const { MysteryBoxError } = require('../../lib/errors');
const crypto = require('crypto');
function _verbose(mb) {
it('covers bad flags', function () {
options.defaultFlags = 300;
- assert.rejects(() => new MysteryBox(options), RangeError);
+ assert.rejects(() => new MysteryBox(options), MysteryBoxError);
});
it('covers missing ciphers', function () {
describe('_keyFromSecret', function () {
it('covers invalid', async function () {
- assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), RangeError);
+ assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), MysteryBoxError);
});
}); // _keyFromSecret
_check(0xfe, 8, 0x00);
});
it('covers unsupported', function () {
- assert.throws(() => MysteryBox._versionHeaderDecode(0xff), RangeError);
+ assert.throws(() => MysteryBox._versionHeaderDecode(0xff), MysteryBoxError);
});
}); // _versionHeaderDecode
});
it('covers too big', function () {
const buffer = Buffer.from([0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
- assert.throws(() => MysteryBox._versionDecode(buffer), RangeError);
+ assert.throws(() => MysteryBox._versionDecode(buffer), MysteryBoxError);
});
}); // _versionDecode
});
it('covers too large', function () {
const version = 277076930199552;
- assert.rejects(() => MysteryBox._versionEncode(version), RangeError);
+ assert.rejects(() => MysteryBox._versionEncode(version), MysteryBoxError);
});
it('recipricates', function () {
[
});
it('covers packing unsupported version', async function () {
- assert.rejects(() => mb.pack({}, 0), RangeError);
+ assert.rejects(() => mb.pack({}, 0), MysteryBoxError);
});
it('covers unpacking unsupported version', async function () {
const badBuffer = Buffer.alloc(128);
badBuffer.writeUInt8(0, 0); // No such thing as version 0
const badPayload = badBuffer.toString('base64url');
- assert.rejects(() => mb.unpack(badPayload), RangeError);
+ assert.rejects(() => mb.unpack(badPayload), MysteryBoxError);
});
it('encrypts and decrypts default version', async function () {
});
it('handles undefined', async function () {
- assert.rejects(() => mb.unpack(), RangeError);
+ assert.rejects(() => mb.unpack(), MysteryBoxError);
});
it('handles incomplete', async function () {
this.slow(500);
const encryptedResult = await mb.pack({ foo: 'bar' });
- assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError);
+ assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), MysteryBoxError);
});
it('covers internal error, incorrect version byte size, pack', async function () {