diff --git a/x-pack/package.json b/x-pack/package.json index 278f350386751..249cc08c8b900 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -178,6 +178,7 @@ "humps": "2.0.1", "icalendar": "0.7.1", "inline-style": "^2.0.0", + "intl": "^1.2.5", "isomorphic-fetch": "2.2.1", "joi": "^13.5.2", "jquery": "^3.3.1", diff --git a/x-pack/plugins/reporting/server/browsers/phantom/driver/index.js b/x-pack/plugins/reporting/server/browsers/phantom/driver/index.js index af2036c5382e1..f8e7f0f254bea 100644 --- a/x-pack/plugins/reporting/server/browsers/phantom/driver/index.js +++ b/x-pack/plugins/reporting/server/browsers/phantom/driver/index.js @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import path from 'path'; import { randomBytes } from 'crypto'; import { fromCallback } from 'bluebird'; import { transformFn } from './transform_fn'; @@ -125,7 +124,24 @@ export function PhantomDriver({ page, browser, zoom, logger }) { randomBytes(6).toString('base64'), ].join('-'); - return _injectPromise(page) + const intlPath = require.resolve('intl/dist/Intl.min.js'); + const promisePath = require.resolve('bluebird/js/browser/bluebird.js'); + + return injectPolyfill( + page, + intlPath, + function hasIntl() { + return (window.Intl !== undefined); + } + ) + .then(() => + injectPolyfill( + page, + promisePath, + function hasPromise() { + return (window.Promise !== undefined); + } + )) .then(() => { return fromCallback(cb => { page.evaluate(transformFn(evaluateWrapper), transformFn(fn).toString(), uniqId, args, cb); @@ -315,35 +331,26 @@ export function PhantomDriver({ page, browser, zoom, logger }) { }; } +async function injectPolyfill(page, pathToPolyfillFile, checkFunction) { + const hasPolyfill = await fromCallback(cb => { + page.evaluate(checkFunction, cb); + }); -function _injectPromise(page) { - function checkForPromise() { - return fromCallback(cb => { - page.evaluate(function hasPromise() { - return (typeof window.Promise !== 'undefined'); - }, cb); - }); + if (hasPolyfill) { + return; } - return checkForPromise() - .then(hasPromise => { - if (hasPromise) return; + const status = await fromCallback(cb => page.injectJs(pathToPolyfillFile, cb)); - const nodeModules = path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'node_modules'); - const promisePath = path.join(nodeModules, 'bluebird', 'js', 'browser', 'bluebird.js'); - return fromCallback(cb => page.injectJs(promisePath, cb)) - .then(status => { - if (status !== true) { - return Promise.reject('Failed to load Promise library'); - } - }) - .then(checkForPromise) - .then(hasPromiseLoaded => { - if (hasPromiseLoaded !== true) { - return Promise.reject('Failed to inject Promise'); - } - }); - }); -} + if (!status) { + return Promise.reject(`Failed to load ${pathToPolyfillFile} library`); + } + const hasPolyfillLoaded = await fromCallback(cb => { + page.evaluate(checkFunction, cb); + }); + if (!hasPolyfillLoaded) { + return Promise.reject(`Failed to inject ${pathToPolyfillFile}`); + } +} diff --git a/yarn.lock b/yarn.lock index 1c888a3a2be28..7aa4417df69e4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11239,6 +11239,11 @@ intl-relativeformat@^2.1.0: dependencies: intl-messageformat "^2.0.0" +intl@^1.2.5: + version "1.2.5" + resolved "https://registry.yarnpkg.com/intl/-/intl-1.2.5.tgz#82244a2190c4e419f8371f5aa34daa3420e2abde" + integrity sha1-giRKIZDE5Bn4Nx9ao02qNCDiq94= + into-stream@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/into-stream/-/into-stream-3.1.0.tgz#96fb0a936c12babd6ff1752a17d05616abd094c6"