Skip to content

Commit

Permalink
[interpreter/loadPlugins] avoid deleting globals added by plugins (el…
Browse files Browse the repository at this point in the history
…astic#27171)

Plugins loaded by the `@kbn/interpreter` can sometimes setup global values while loading, like the regeneratorRuntime for instance, but the current plugin loading code is deleting every global that was added during plugin load. This changes the logic to only cleanup the `canvas` global after loading the canvas plugins.

resolves elastic#27162
  • Loading branch information
Spencer authored and w33ble committed Dec 28, 2018
1 parent 9513d1f commit 02803a2
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/kbn-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "6.20.0",
"css-loader": "1.0.0",
"copy-webpack-plugin": "^4.6.0",
"del": "^3.0.0",
"getopts": "^2.2.3",
"pegjs": "0.9.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/kbn-interpreter/src/plugin/functions/browser/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import '../common/register';
8 changes: 1 addition & 7 deletions packages/kbn-interpreter/src/server/server_registries.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export const populateServerRegistries = types => {
const remainingTypes = types;
const populatedTypes = {};

const globalKeys = Object.keys(global);

const loadType = () => {
const type = remainingTypes.pop();
getPluginPaths(type).then(paths => {
Expand All @@ -60,11 +58,7 @@ export const populateServerRegistries = types => {
require(path);
});

Object.keys(global).forEach(key => {
if (!globalKeys.includes(key)) {
delete global[key];
}
});
delete global.canvas;

populatedTypes[type] = registries[type];
if (remainingTypes.length) loadType();
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-interpreter/tasks/build/__fixtures__/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
import util from 'util';
console.log(util.format('hello world'));
43 changes: 43 additions & 0 deletions packages/kbn-interpreter/tasks/build/server_code_transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { extname } = require('path');

const { transform } = require('babel-core');

exports.createServerCodeTransformer = (sourceMaps) => {
return (content, path) => {
switch (extname(path)) {
case '.js':
const { code = '' } = transform(content.toString('utf8'), {
filename: path,
ast: false,
code: true,
sourceMaps: sourceMaps ? 'inline' : false,
babelrc: false,
presets: [require.resolve('@kbn/babel-preset/webpack_preset')],
});

return code;

default:
return content.toString('utf8');
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { readFileSync } from 'fs';
import { resolve } from 'path';
import { createServerCodeTransformer } from './server_code_transformer';

const JS_FIXTURE_PATH = resolve(__dirname, '__fixtures__/sample.js');
const JS_FIXTURE = readFileSync(JS_FIXTURE_PATH);

describe('js support', () => {
it('transpiles js file', () => {
const transformer = createServerCodeTransformer();
expect(transformer(JS_FIXTURE, JS_FIXTURE_PATH)).toMatchInlineSnapshot(`
"'use strict';
var _util = require('util');
var _util2 = _interopRequireDefault(_util);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_util2.default.format('hello world')); /* eslint-disable */"
`);
});

it('throws errors for js syntax errors', () => {
const transformer = createServerCodeTransformer();
expect(() => transformer(Buffer.from(`export default 'foo`), JS_FIXTURE_PATH)).toThrowError(
/Unterminated string constant/
);
});
});
15 changes: 14 additions & 1 deletion packages/kbn-interpreter/tasks/build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/

const { resolve } = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const { createServerCodeTransformer } = require('./server_code_transformer');

const {
PLUGIN_SOURCE_DIR,
PLUGIN_BUILD_DIR,
Expand All @@ -31,7 +35,7 @@ module.exports = function ({ sourceMaps }, { watch }) {
mode: 'none',
entry: {
'types/all': resolve(PLUGIN_SOURCE_DIR, 'types/register.js'),
'functions/common/all': resolve(PLUGIN_SOURCE_DIR, 'functions/common/register.js'),
'functions/browser/all': resolve(PLUGIN_SOURCE_DIR, 'functions/browser/register.js'),
},

// there were problems with the node and web targets since this code is actually
Expand Down Expand Up @@ -95,6 +99,15 @@ module.exports = function ({ sourceMaps }, { watch }) {
stats: 'errors-only',

plugins: [
new CopyWebpackPlugin([
{
from: resolve(PLUGIN_SOURCE_DIR, 'functions/common'),
to: resolve(PLUGIN_BUILD_DIR, 'functions/common'),
ignore: '**/__tests__/**',
transform: createServerCodeTransformer(sourceMaps)
},
]),

function LoaderFailHandlerPlugin() {
if (!watch) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/dev/mocha/run_mocha_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function runMochaCli() {

// check that we aren't leaking any globals
process.argv.push('--check-leaks');
// prevent globals injected from canvas plugins from triggering leak check
process.argv.push('--globals', 'core,regeneratorRuntime,_');

// ensure that mocha requires the setup_node_env script
process.argv.push('--require', require.resolve('../../setup_node_env'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { populateBrowserRegistries } from '@kbn/interpreter/public';
import { typesRegistry, functionsRegistry } from '@kbn/interpreter/common';

const types = {
commonFunctions: functionsRegistry,
browserFunctions: functionsRegistry,
types: typesRegistry
};
Expand Down
5 changes: 2 additions & 3 deletions src/setup_node_env/babel_register/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ var ignore = [
// is `x-pack` and `b` is not `node_modules`
/\/node_modules\/(?!x-pack\/(?!node_modules)([^\/]+))([^\/]+\/[^\/]+)/,

// ignore paths matching `/canvas/canvas_plugin/{a}/{b}` unless
// `a` is `functions` and `b` is `server`
/\/canvas\/canvas_plugin\/(?!functions\/server)([^\/]+\/[^\/]+)/,
// ignore paths matching `/canvas/canvas_plugin/`
/\/canvas\/canvas_plugin\//,
];

if (global.__BUILT_WITH_BABEL__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { functions as commonFunctions } from '../common';
import { browser } from './browser';
import { location } from './location';
import { urlparam } from './urlparam';
import { markdown } from './markdown';

export const functions = [browser, location, urlparam, markdown];
export const functions = [browser, location, urlparam, markdown, ...commonFunctions];
21 changes: 18 additions & 3 deletions x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const {
createServerCodeTransformer,
} = require('@kbn/interpreter/tasks/build/server_code_transformer');

const sourceDir = path.resolve(__dirname, '../../canvas_plugin_src');
const buildDir = path.resolve(__dirname, '../../canvas_plugin');
Expand All @@ -25,7 +28,6 @@ export function getWebpackConfig({ devtool, watch } = {}) {
'uis/datasources/all': path.join(sourceDir, 'uis/datasources/register.js'),
'uis/arguments/all': path.join(sourceDir, 'uis/arguments/register.js'),
'functions/browser/all': path.join(sourceDir, 'functions/browser/register.js'),
'functions/common/all': path.join(sourceDir, 'functions/common/register.js'),
'templates/all': path.join(sourceDir, 'templates/register.js'),
'uis/tags/all': path.join(sourceDir, 'uis/tags/register.js'),
},
Expand Down Expand Up @@ -74,8 +76,21 @@ export function getWebpackConfig({ devtool, watch } = {}) {
},
new CopyWebpackPlugin([
{
from: `${sourceDir}/functions/server/`,
to: `${buildDir}/functions/server/`,
from: path.resolve(sourceDir, 'functions/server'),
to: path.resolve(buildDir, 'functions/server'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
{
from: path.resolve(sourceDir, 'functions/common'),
to: path.resolve(buildDir, 'functions/common'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
{
from: path.resolve(sourceDir, 'lib'),
to: path.resolve(buildDir, 'lib'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
]),
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5822,6 +5822,20 @@ copy-webpack-plugin@^4.5.2:
p-limit "^1.0.0"
serialize-javascript "^1.4.0"

copy-webpack-plugin@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-4.6.0.tgz#e7f40dd8a68477d405dd1b7a854aae324b158bae"
integrity sha512-Y+SQCF+0NoWQryez2zXn5J5knmr9z/9qSQt7fbL78u83rxmigOy8X5+BFn8CFSuX+nKT8gpYwJX68ekqtQt6ZA==
dependencies:
cacache "^10.0.4"
find-cache-dir "^1.0.0"
globby "^7.1.1"
is-glob "^4.0.0"
loader-utils "^1.1.0"
minimatch "^3.0.4"
p-limit "^1.0.0"
serialize-javascript "^1.4.0"

core-js@^1.0.0:
version "1.2.7"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636"
Expand Down

0 comments on commit 02803a2

Please sign in to comment.