Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into pr/64414
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/optimize/bundles_route/bundles_route.ts
#	src/optimize/bundles_route/dynamic_asset_response.ts
#	src/optimize/bundles_route/proxy_bundles_route.ts
#	src/optimize/index.ts
#	src/optimize/np_ui_plugin_public_dirs.ts
  • Loading branch information
spalger committed Apr 29, 2020
2 parents 6770a4a + 408ad6f commit f06d044
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// these are necessary to bootstrap the local angular.
// They can stay even after NP cutover
import angular from 'angular';
// required for `ngSanitize` angular module
import 'angular-sanitize';
import { EuiIcon } from '@elastic/eui';
import { i18nDirective, i18nFilter, I18nProvider } from '@kbn/i18n/angular';
import { CoreStart, LegacyCoreStart } from 'kibana/public';
Expand Down
2 changes: 2 additions & 0 deletions src/legacy/core_plugins/timelion/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

import _ from 'lodash';
// required for `ngSanitize` angular module
import 'angular-sanitize';

import { i18n } from '@kbn/i18n';

Expand Down
1 change: 1 addition & 0 deletions src/legacy/ui/public/angular_ui_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import 'jquery';
import 'angular';
// required for `ngSanitize` angular module
import 'angular-sanitize';
import 'ui-select/dist/select';

Expand Down
1 change: 1 addition & 0 deletions src/legacy/ui/public/i18n/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { render } from 'enzyme';
import PropTypes from 'prop-types';
import React from 'react';

jest.mock('angular-sanitize', () => {});
jest.mock('ui/new_platform', () => ({
npStart: {
core: {
Expand Down
2 changes: 2 additions & 0 deletions src/legacy/ui/public/i18n/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

import React from 'react';
// required for `ngSanitize` angular module
import 'angular-sanitize';

import { i18nDirective, i18nFilter, I18nProvider } from '@kbn/i18n/angular';
// @ts-ignore
Expand Down
12 changes: 7 additions & 5 deletions src/optimize/bundles_route/bundles_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/

import { isAbsolute, extname, join } from 'path';

import Hapi from 'hapi';
import LruCache from 'lru-cache';
import * as UiSharedDeps from '@kbn/ui-shared-deps';

import { createDynamicAssetResponse } from './dynamic_asset_response';
import { assertIsNpUiPluginPublicDirs } from '../np_ui_plugin_public_dirs';
import { FileHashCache } from './file_hash_cache';
import { assertIsNpUiPluginPublicDirs, NpUiPluginPublicDirs } from '../np_ui_plugin_public_dirs';
import { fromRoot } from '../../core/server/utils';

/**
Expand Down Expand Up @@ -52,14 +54,14 @@ export function createBundlesRoute({
dllBundlesPath: string;
basePublicPath: string;
builtCssPath: string;
npUiPluginPublicDirs?: any[];
npUiPluginPublicDirs?: NpUiPluginPublicDirs;
buildHash: string;
isDist?: boolean;
}) {
// rather than calculate the fileHash on every request, we
// provide a cache object to `resolveDynamicAssetResponse()` that
// will store the 100 most recently used hashes.
const fileHashCache = new LruCache(100);
const fileHashCache = new FileHashCache();
assertIsNpUiPluginPublicDirs(npUiPluginPublicDirs);

if (typeof regularBundlesPath !== 'string' || !isAbsolute(regularBundlesPath)) {
Expand Down Expand Up @@ -144,7 +146,7 @@ function buildRouteForBundles({
publicPath: string;
routePath: string;
bundlesPath: string;
fileHashCache: LruCache<unknown, unknown>;
fileHashCache: FileHashCache;
replacePublicPath?: boolean;
isDist: boolean;
}) {
Expand Down
23 changes: 8 additions & 15 deletions src/optimize/bundles_route/dynamic_asset_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
* under the License.
*/

import Util from 'util';
import Fs from 'fs';
import { resolve } from 'path';

import Hapi from 'hapi';
import LruCache from 'lru-cache';
import { promisify } from 'util';

import Boom from 'boom';
import Hapi from 'hapi';

// @ts-ignore
import { FileHashCache } from './file_hash_cache';
import { getFileHash } from './file_hash';
// @ts-ignore
import { replacePlaceholder } from '../public_path_placeholder';
Expand All @@ -35,9 +33,9 @@ const MINUTE = 60;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;

const asyncOpen = Util.promisify(Fs.open);
const asyncClose = Util.promisify(Fs.close);
const asyncFstat = Util.promisify(Fs.fstat);
const asyncOpen = promisify(Fs.open);
const asyncClose = promisify(Fs.close);
const asyncFstat = promisify(Fs.fstat);

/**
* Create a Hapi response for the requested path. This is designed
Expand All @@ -57,12 +55,6 @@ const asyncFstat = Util.promisify(Fs.fstat);
* - cached hash/etag is based on the file on disk, but modified
* by the public path so that individual public paths have
* different etags, but can share a cache
*
* @param {Object} options
* @property {Hapi.Request} options.request
* @property {string} options.bundlesPath
* @property {string} options.publicPath
* @property {LruCache} options.fileHashCache
*/
export async function createDynamicAssetResponse({
request,
Expand All @@ -77,11 +69,12 @@ export async function createDynamicAssetResponse({
h: Hapi.ResponseToolkit;
bundlesPath: string;
publicPath: string;
fileHashCache: LruCache<unknown, unknown>;
fileHashCache: FileHashCache;
replacePublicPath: boolean;
isDist: boolean;
}) {
let fd: number | undefined;

try {
const path = resolve(bundlesPath, request.params.path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@
*/

import { createHash } from 'crypto';
import { createReadStream } from 'fs';
import Fs from 'fs';

import * as Rx from 'rxjs';
import { merge, mergeMap, takeUntil } from 'rxjs/operators';
import { takeUntil, map } from 'rxjs/operators';

import { FileHashCache } from './file_hash_cache';

/**
* Get the hash of a file via a file descriptor
* @param {LruCache} cache
* @param {string} path
* @param {Fs.Stat} stat
* @param {Fs.FileDescriptor} fd
* @return {Promise<string>}
*/
export async function getFileHash(cache, path, stat, fd) {
export async function getFileHash(cache: FileHashCache, path: string, stat: Fs.Stats, fd: number) {
const key = `${path}:${stat.ino}:${stat.size}:${stat.mtime.getTime()}`;

const cached = cache.get(key);
Expand All @@ -40,17 +37,21 @@ export async function getFileHash(cache, path, stat, fd) {
}

const hash = createHash('sha1');
const read = createReadStream(null, {
const read = Fs.createReadStream(null as any, {
fd,
start: 0,
autoClose: false,
});

const promise = Rx.fromEvent(read, 'data')
.pipe(
merge(Rx.fromEvent(read, 'error').pipe(mergeMap(Rx.throwError))),
takeUntil(Rx.fromEvent(read, 'end'))
const promise = Rx.merge(
Rx.fromEvent<Buffer>(read, 'data'),
Rx.fromEvent<Error>(read, 'error').pipe(
map(error => {
throw error;
})
)
)
.pipe(takeUntil(Rx.fromEvent(read, 'end')))
.forEach(chunk => hash.update(chunk))
.then(() => hash.digest('hex'))
.catch(error => {
Expand Down
36 changes: 36 additions & 0 deletions src/optimize/bundles_route/file_hash_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 LruCache from 'lru-cache';

export class FileHashCache {
private lru = new LruCache<string, Promise<string>>(100);

get(key: string) {
return this.lru.get(key);
}

set(key: string, value: Promise<string>) {
this.lru.set(key, value);
}

del(key: string) {
this.lru.del(key);
}
}
4 changes: 2 additions & 2 deletions src/optimize/bundles_route/proxy_bundles_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createProxyBundlesRoute({
buildHash,
}: {
host: string;
port: string;
port: number;
buildHash: string;
}) {
return [
Expand All @@ -33,7 +33,7 @@ export function createProxyBundlesRoute({
];
}

function buildProxyRouteForBundles(routePath: string, host: string, port: string) {
function buildProxyRouteForBundles(routePath: string, host: string, port: number) {
return {
path: `${routePath}{path*}`,
method: 'GET',
Expand Down
21 changes: 21 additions & 0 deletions src/optimize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { optimizeMixin } from './optimize_mixin';
export default optimizeMixin;
17 changes: 11 additions & 6 deletions src/optimize/np_ui_plugin_public_dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/
import KbnServer from '../legacy/server/kbn_server';

export function getNpUiPluginPublicDirs(kbnServer: KbnServer) {
export type NpUiPluginPublicDirs = Array<{
id: string;
path: string;
}>;

export function getNpUiPluginPublicDirs(kbnServer: KbnServer): NpUiPluginPublicDirs {
return Array.from(kbnServer.newPlatform.__internals.uiPlugins.internal.entries()).map(
([id, { publicTargetDir }]) => ({
id,
Expand All @@ -27,17 +32,17 @@ export function getNpUiPluginPublicDirs(kbnServer: KbnServer) {
);
}

export function isNpUiPluginPublicDirs(something: any) {
export function isNpUiPluginPublicDirs(x: any): x is NpUiPluginPublicDirs {
return (
Array.isArray(something) &&
something.every(
Array.isArray(x) &&
x.every(
s => typeof s === 'object' && s && typeof s.id === 'string' && typeof s.path === 'string'
)
);
}

export function assertIsNpUiPluginPublicDirs(something: any) {
if (!isNpUiPluginPublicDirs(something)) {
export function assertIsNpUiPluginPublicDirs(x: any): asserts x is NpUiPluginPublicDirs {
if (!isNpUiPluginPublicDirs(x)) {
throw new TypeError(
'npUiPluginPublicDirs must be an array of objects with string `id` and `path` properties'
);
Expand Down
12 changes: 8 additions & 4 deletions src/optimize/index.ts → src/optimize/optimize_mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
*/

import Hapi from 'hapi';
// @ts-ignore

// @ts-ignore not TS yet
import FsOptimizer from './fs_optimizer';
import { createBundlesRoute } from './bundles_route';
// @ts-ignore
// @ts-ignore not TS yet
import { DllCompiler } from './dynamic_dll_plugin';
import { fromRoot } from '../core/server/utils';
import { getNpUiPluginPublicDirs } from './np_ui_plugin_public_dirs';
import KbnServer, { KibanaConfig } from '../legacy/server/kbn_server';

// eslint-disable-next-line import/no-default-export
export default async (kbnServer: KbnServer, server: Hapi.Server, config: KibanaConfig) => {
export const optimizeMixin = async (
kbnServer: KbnServer,
server: Hapi.Server,
config: KibanaConfig
) => {
if (!config.get('optimize.enabled')) return;

// the watch optimizer sets up two threads, one is the server listening
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
* under the License.
*/

import { createReplaceStream } from '../legacy/utils';
import Stream from 'stream';
import Fs from 'fs';

import * as Rx from 'rxjs';
import { take, takeUntil } from 'rxjs/operators';
import { createReplaceStream } from '../legacy/utils';

export const PUBLIC_PATH_PLACEHOLDER = '__REPLACE_WITH_PUBLIC_PATH__';

export function replacePlaceholder(read, replacement) {
interface ClosableTransform extends Stream.Transform {
close(): void;
}

export function replacePlaceholder(read: Stream.Readable, replacement: string) {
const replace = createReplaceStream(PUBLIC_PATH_PLACEHOLDER, replacement);

// handle errors on the read stream by proxying them
Expand All @@ -37,13 +43,15 @@ export function replacePlaceholder(read, replacement) {
replace.end();
});

replace.close = () => {
read.unpipe();
const closableReplace: ClosableTransform = Object.assign(replace, {
close: () => {
read.unpipe();

if (read.close) {
read.close();
}
};
if ('close' in read) {
(read as Fs.ReadStream).close();
}
},
});

return read.pipe(replace);
return read.pipe(closableReplace);
}
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import './index.scss';

import { EuiIcon } from '@elastic/eui';
import angular, { IModule } from 'angular';
// required for `ngSanitize` angular module
import 'angular-sanitize';
import { i18nDirective, i18nFilter, I18nProvider } from '@kbn/i18n/angular';
import {
AppMountContext,
Expand Down
Loading

0 comments on commit f06d044

Please sign in to comment.