Skip to content

Commit

Permalink
[I18n] Inject Intl Polyfill to PhantomJS (#25465) (#25789)
Browse files Browse the repository at this point in the history
* [I18n] Inject Intl Polyfill to PhantomJS

* Refactor injection code

* Move intl to "dependencies"

* Move 'intl' to the root 'package.json'

* Fix polyfills paths

* Move intl to x-pack package.json
  • Loading branch information
LeanidShutau authored Nov 16, 2018
1 parent 8ac8b0f commit afbc1ce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 35 additions & 28 deletions x-pack/plugins/reporting/server/browsers/phantom/driver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}`);
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11226,6 +11226,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"
Expand Down

0 comments on commit afbc1ce

Please sign in to comment.