From a4a7206ef47084d557f64fdb2e76834cb1fb0d1d Mon Sep 17 00:00:00 2001 From: Court Ewing Date: Mon, 14 Sep 2015 13:17:51 -0400 Subject: [PATCH] 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. --- src/plugins/testsBundle/testsEntryTemplate.js | 2 ++ src/ui/public/__tests__/metadata.js | 17 +++++++++++++ src/ui/public/chrome/chrome.js | 3 ++- src/ui/public/metadata.js | 24 +++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/ui/public/__tests__/metadata.js create mode 100644 src/ui/public/metadata.js diff --git a/src/plugins/testsBundle/testsEntryTemplate.js b/src/plugins/testsBundle/testsEntryTemplate.js index 3725f6a01c19..9ea755ae3db3 100644 --- a/src/plugins/testsBundle/testsEntryTemplate.js +++ b/src/plugins/testsBundle/testsEntryTemplate.js @@ -21,6 +21,8 @@ ${pluginSlug} */ window.__KBN__ = { + version: '1.2.3', + buildNum: 1234, vars: { kbnIndex: '.kibana', esShardTimeout: 1500, diff --git a/src/ui/public/__tests__/metadata.js b/src/ui/public/__tests__/metadata.js new file mode 100644 index 000000000000..58e213c1761d --- /dev/null +++ b/src/ui/public/__tests__/metadata.js @@ -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; + }); +}); diff --git a/src/ui/public/chrome/chrome.js b/src/ui/public/chrome/chrome.js index 97585950ff47..872dd4ab5a3d 100644 --- a/src/ui/public/chrome/chrome.js +++ b/src/ui/public/chrome/chrome.js @@ -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, diff --git a/src/ui/public/metadata.js b/src/ui/public/metadata.js new file mode 100644 index 000000000000..215adb51eaa2 --- /dev/null +++ b/src/ui/public/metadata.js @@ -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); + } +});