From 5cc945f9e8a9e979771ef5bebc3c03aa2e3e0cb4 Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Sun, 4 Oct 2020 22:10:19 +0300 Subject: [PATCH] load js-yaml lazily (#79092) --- .../public/components/navigation/connected_link.tsx | 4 ++-- .../public/lib/__tests__/config_blocks.test.ts | 12 ++++++------ .../public/lib/configuration_blocks.ts | 11 ++++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/x-pack/plugins/beats_management/public/components/navigation/connected_link.tsx b/x-pack/plugins/beats_management/public/components/navigation/connected_link.tsx index ebac34afa016b9..57fb6d4fc33bba 100644 --- a/x-pack/plugins/beats_management/public/components/navigation/connected_link.tsx +++ b/x-pack/plugins/beats_management/public/components/navigation/connected_link.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import _ from 'lodash'; +import { get } from 'lodash'; import React from 'react'; import { EuiLink } from '@elastic/eui'; @@ -31,7 +31,7 @@ export const ConnectedLinkComponent = ({ } // Shorthand for pathname - const pathname = path || _.get(props.to, 'pathname') || location.pathname; + const pathname = path || get(props.to, 'pathname') || location.pathname; return ( { }); it('should use helper function to convert users yaml in tag to config object', async () => { - const convertedBlocks = lib.userConfigsToJson([ + const convertedBlocks = await lib.userConfigsToJson([ { id: 'foo', tag: 'basic', @@ -42,7 +42,7 @@ describe('Tags Client Domain Lib', () => { }); it('should use helper function to convert user config to json with undefined `other`', async () => { - const convertedTag = lib.userConfigsToJson([ + const convertedTag = await lib.userConfigsToJson([ { id: 'foo', tag: 'basic', @@ -61,7 +61,7 @@ describe('Tags Client Domain Lib', () => { }); it('should use helper function to convert users yaml in tag to config object, where empty other leads to no other fields saved', async () => { - const convertedTag = lib.userConfigsToJson([ + const convertedTag = await lib.userConfigsToJson([ { id: 'foo', tag: 'basic', @@ -83,7 +83,7 @@ describe('Tags Client Domain Lib', () => { }); it('should convert tokenized fields to JSON', async () => { - const convertedTag = lib.userConfigsToJson([ + const convertedTag = await lib.userConfigsToJson([ { id: 'foo', tag: 'basic', @@ -106,7 +106,7 @@ describe('Tags Client Domain Lib', () => { }); it('should use helper function to convert config object to users yaml', async () => { - const convertedTag = lib.jsonConfigToUserYaml([ + const convertedTag = await lib.jsonConfigToUserYaml([ { id: 'foo', tag: 'basic', @@ -127,7 +127,7 @@ describe('Tags Client Domain Lib', () => { }); it('should use helper function to convert config object to users yaml with empty `other`', async () => { - const convertedTag = lib.jsonConfigToUserYaml([ + const convertedTag = await lib.jsonConfigToUserYaml([ { id: 'foo', tag: 'basic', diff --git a/x-pack/plugins/beats_management/public/lib/configuration_blocks.ts b/x-pack/plugins/beats_management/public/lib/configuration_blocks.ts index 09c079ea129e67..d84bd21381c3ee 100644 --- a/x-pack/plugins/beats_management/public/lib/configuration_blocks.ts +++ b/x-pack/plugins/beats_management/public/lib/configuration_blocks.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import yaml from 'js-yaml'; import { set } from '@elastic/safer-lodash-set'; import { get, has, omit } from 'lodash'; import { ConfigBlockSchema, ConfigurationBlock } from '../../common/domain_types'; @@ -19,16 +18,17 @@ export class ConfigBlocksLib { ) {} public upsert = async (blocks: ConfigurationBlock[]) => { - return await this.adapter.upsert(this.userConfigsToJson(blocks)); + return await this.adapter.upsert(await this.userConfigsToJson(blocks)); }; public getForTags = async (tagIds: string[], page: number) => { const result = await this.adapter.getForTags(tagIds, page); - result.list = this.jsonConfigToUserYaml(result.list); + result.list = await this.jsonConfigToUserYaml(result.list); return result; }; - public jsonConfigToUserYaml(blocks: ConfigurationBlock[]): ConfigurationBlock[] { + public async jsonConfigToUserYaml(blocks: ConfigurationBlock[]): Promise { + const yaml = await import('js-yaml'); // configuration_blocks yaml, JS cant read YAML so we parse it into JS, // because beats flattens all fields, and we need more structure. // we take tagConfigs, grab the config that applies here, render what we can into @@ -73,7 +73,8 @@ export class ConfigBlocksLib { }); } - public userConfigsToJson(blocks: ConfigurationBlock[]): ConfigurationBlock[] { + public async userConfigsToJson(blocks: ConfigurationBlock[]): Promise { + const yaml = await import('js-yaml'); // configurations is the JS representation of the config yaml, // so here we take that JS and convert it into a YAML string. // we do so while also flattening "other" into the flat yaml beats expect