Kibana install metadata available as module

The metadata for the current kibana project install that is seeded to
the client-side app via window.__KBN__ is now accessible anywhere via
the immutable ui/metadata singleton. window.__KBN__ now becomes a tested
implementation detail of only a single module in the whole app, whereas
any consumer of the metadata now has an explicit module dependency and
has no way to modify the metadata contents.
This commit is contained in:
Court Ewing 2015-09-14 13:17:51 -04:00
parent 3ec5587bc0
commit a4a7206ef4
4 changed files with 45 additions and 1 deletions

View file

@ -21,6 +21,8 @@ ${pluginSlug}
*/
window.__KBN__ = {
version: '1.2.3',
buildNum: 1234,
vars: {
kbnIndex: '.kibana',
esShardTimeout: 1500,

View file

@ -0,0 +1,17 @@
describe('ui/metadata', () => {
const expect = require('expect.js');
const metadata = require('ui/metadata');
it('is same data as window.__KBN__', () => {
expect(metadata.version).to.equal(window.__KBN__.version);
expect(metadata.vars.kbnIndex).to.equal(window.__KBN__.vars.kbnIndex);
});
it('is immutable', () => {
expect(() => metadata.foo = 'something').to.throw;
expect(() => metadata.version = 'something').to.throw;
expect(() => metadata.vars = {}).to.throw;
expect(() => metadata.vars.kbnIndex = 'something').to.throw;
});
});

View file

@ -8,6 +8,7 @@ require('ui/timefilter');
require('ui/private');
require('ui/promises');
var metadata = require('ui/metadata');
var TabCollection = require('ui/chrome/TabCollection');
var chrome = {
@ -17,7 +18,7 @@ var chrome = {
};
var internals = _.assign(
_.cloneDeep(window.__KBN__ || {}),
_.cloneDeep(metadata),
{
tabs: new TabCollection(),
rootController: null,

24
src/ui/public/metadata.js Normal file
View file

@ -0,0 +1,24 @@
// singleton for immutable copy of window.__KBN__
define(function (require) {
const _ = require('lodash');
if (!_.has(window, '__KBN__')) {
throw new Error('window.__KBN__ must be set for metadata');
}
const kbn = _.cloneDeep(window.__KBN__ || {});
return deepFreeze(kbn);
function deepFreeze(object) {
// for any properties that reference an object, makes sure that object is
// recursively frozen as well
Object.keys(object).forEach(key => {
const value = object[key];
if (_.isObject(value)) {
deepFreeze(value);
}
});
return Object.freeze(object);
}
});