Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[I18n] Inject Intl Polyfill to PhantomJS #25465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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"
Expand Down