Merge pull request #5273 from spalger/fix/buffersInConfig
[server/config] support buffers in config
This commit is contained in:
commit
df0fe171b3
|
@ -4,6 +4,7 @@ let _ = require('lodash');
|
|||
let { zipObject } = require('lodash');
|
||||
let override = require('./override');
|
||||
let pkg = require('requirefrom')('src/utils')('packageJson');
|
||||
const clone = require('./deepCloneWithBuffers');
|
||||
|
||||
const schema = Symbol('Joi Schema');
|
||||
const schemaKeys = Symbol('Schema Extensions');
|
||||
|
@ -15,7 +16,7 @@ module.exports = class Config {
|
|||
this[schemaKeys] = new Map();
|
||||
|
||||
this[vals] = Object.create(null);
|
||||
this[pendingSets] = new Map(_.pairs(_.cloneDeep(initialSettings || {})));
|
||||
this[pendingSets] = new Map(_.pairs(clone(initialSettings || {})));
|
||||
|
||||
if (initialSchema) this.extendSchema(initialSchema);
|
||||
}
|
||||
|
@ -64,7 +65,7 @@ module.exports = class Config {
|
|||
|
||||
set(key, value) {
|
||||
// clone and modify the config
|
||||
let config = _.cloneDeep(this[vals]);
|
||||
let config = clone(this[vals]);
|
||||
if (_.isPlainObject(key)) {
|
||||
config = override(config, key);
|
||||
} else {
|
||||
|
@ -114,7 +115,7 @@ module.exports = class Config {
|
|||
|
||||
get(key) {
|
||||
if (!key) {
|
||||
return _.cloneDeep(this[vals]);
|
||||
return clone(this[vals]);
|
||||
}
|
||||
|
||||
let value = _.get(this[vals], key);
|
||||
|
@ -123,7 +124,7 @@ module.exports = class Config {
|
|||
throw new Error('Unknown config key: ' + key);
|
||||
}
|
||||
}
|
||||
return _.cloneDeep(value);
|
||||
return clone(value);
|
||||
}
|
||||
|
||||
has(key) {
|
||||
|
|
61
src/server/config/__tests__/deepCloneWithBuffers.js
Normal file
61
src/server/config/__tests__/deepCloneWithBuffers.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
import deepCloneWithBuffers from '../deepCloneWithBuffers';
|
||||
import expect from 'expect.js';
|
||||
|
||||
describe('deepCloneWithBuffers()', function () {
|
||||
it('deep clones objects', function () {
|
||||
const source = {
|
||||
a: {
|
||||
b: {},
|
||||
c: {},
|
||||
d: [
|
||||
{
|
||||
e: 'f'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
const output = deepCloneWithBuffers(source);
|
||||
|
||||
expect(source.a).to.eql(output.a);
|
||||
expect(source.a).to.not.be(output.a);
|
||||
|
||||
expect(source.a.b).to.eql(output.a.b);
|
||||
expect(source.a.b).to.not.be(output.a.b);
|
||||
|
||||
expect(source.a.c).to.eql(output.a.c);
|
||||
expect(source.a.c).to.not.be(output.a.c);
|
||||
|
||||
expect(source.a.d).to.eql(output.a.d);
|
||||
expect(source.a.d).to.not.be(output.a.d);
|
||||
|
||||
expect(source.a.d[0]).to.eql(output.a.d[0]);
|
||||
expect(source.a.d[0]).to.not.be(output.a.d[0]);
|
||||
});
|
||||
|
||||
it('copies buffers but keeps them buffers', function () {
|
||||
const input = new Buffer('i am a teapot', 'utf8');
|
||||
const output = deepCloneWithBuffers(input);
|
||||
|
||||
expect(Buffer.isBuffer(input)).to.be.ok();
|
||||
expect(Buffer.isBuffer(output)).to.be.ok();
|
||||
expect(Buffer.compare(output, input));
|
||||
expect(output).to.not.be(input);
|
||||
});
|
||||
|
||||
it('copies buffers that are deep', function () {
|
||||
const input = {
|
||||
a: {
|
||||
b: {
|
||||
c: new Buffer('i am a teapot', 'utf8')
|
||||
}
|
||||
}
|
||||
};
|
||||
const output = deepCloneWithBuffers(input);
|
||||
|
||||
expect(Buffer.isBuffer(input.a.b.c)).to.be.ok();
|
||||
expect(Buffer.isBuffer(output.a.b.c)).to.be.ok();
|
||||
expect(Buffer.compare(output.a.b.c, input.a.b.c));
|
||||
expect(output.a.b.c).to.not.be(input.a.b.c);
|
||||
});
|
||||
});
|
11
src/server/config/deepCloneWithBuffers.js
Normal file
11
src/server/config/deepCloneWithBuffers.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { cloneDeep } from 'lodash';
|
||||
|
||||
function cloneBuffersCustomizer(val) {
|
||||
if (Buffer.isBuffer(val)) {
|
||||
return new Buffer(val);
|
||||
}
|
||||
}
|
||||
|
||||
export default function (vals) {
|
||||
return cloneDeep(vals, cloneBuffersCustomizer);
|
||||
};
|
Loading…
Reference in a new issue