+++ /dev/null
-'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
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');
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...
const payloadFlagsMask = (availableFlags.BufferPayload);
const payloadFlagsShift = 7;
-
class MysteryBox extends EventEmitter {
/**
* @param {Object} options
*/
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');
}
}
+ /**
+ * 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.
*/
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;
+++ /dev/null
-/* 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
});
}); // 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);