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