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);