From 7c35408996fcbb2d4c38c938fc46da598246b47b Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 28 Apr 2020 01:46:40 -0700 Subject: [PATCH] restore replacePublicPath option from #64226 --- src/optimize/bundles_route/bundles_route.ts | 5 +++++ .../bundles_route/dynamic_asset_response.ts | 17 ++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/optimize/bundles_route/bundles_route.ts b/src/optimize/bundles_route/bundles_route.ts index 089f0ca2cbf44..e831b15e8f8e3 100644 --- a/src/optimize/bundles_route/bundles_route.ts +++ b/src/optimize/bundles_route/bundles_route.ts @@ -88,6 +88,7 @@ export function createBundlesRoute({ routePath: `/${buildHash}/bundles/kbn-ui-shared-deps/`, bundlesPath: UiSharedDeps.distDir, fileHashCache, + replacePublicPath: false, isDist, }), ...npUiPluginPublicDirs.map(({ id, path }) => @@ -96,6 +97,7 @@ export function createBundlesRoute({ routePath: `/${buildHash}/bundles/plugin/${id}/`, bundlesPath: path, fileHashCache, + replacePublicPath: false, isDist, }) ), @@ -135,12 +137,14 @@ function buildRouteForBundles({ routePath, bundlesPath, fileHashCache, + replacePublicPath = true, isDist, }: { publicPath: string; routePath: string; bundlesPath: string; fileHashCache: LruCache; + replacePublicPath?: boolean; isDist: boolean; }) { return { @@ -163,6 +167,7 @@ function buildRouteForBundles({ bundlesPath, fileHashCache, publicPath, + replacePublicPath, isDist, }); }, diff --git a/src/optimize/bundles_route/dynamic_asset_response.ts b/src/optimize/bundles_route/dynamic_asset_response.ts index 800e75cf59cfc..06ca906a5df21 100644 --- a/src/optimize/bundles_route/dynamic_asset_response.ts +++ b/src/optimize/bundles_route/dynamic_asset_response.ts @@ -31,8 +31,7 @@ import { getFileHash } from './file_hash'; // @ts-ignore import { replacePlaceholder } from '../public_path_placeholder'; -const SECOND = 1000; -const MINUTE = 60 * SECOND; +const MINUTE = 60; const HOUR = 60 * MINUTE; const DAY = 24 * HOUR; @@ -67,6 +66,7 @@ export async function createDynamicAssetResponse({ bundlesPath, publicPath, fileHashCache, + replacePublicPath, isDist, }: { request: Hapi.Request; @@ -74,6 +74,7 @@ export async function createDynamicAssetResponse({ bundlesPath: string; publicPath: string; fileHashCache: LruCache; + replacePublicPath: boolean; isDist: boolean; }) { let fd: number | undefined; @@ -90,18 +91,20 @@ export async function createDynamicAssetResponse({ // the file 2 or 3 times per request it seems logical fd = await fcb(cb => open(path, 'r', cb)); + const stat = await fcb(cb => fstat(fd!, cb)); + const hash = isDist ? undefined : await getFileHash(fileHashCache, path, stat, fd); + const read = createReadStream(null as any, { fd, start: 0, autoClose: true, }); - - const stat = await fcb(cb => fstat(fd!, cb)); - const hash = isDist ? undefined : await getFileHash(fileHashCache, path, stat, fd); fd = undefined; // read stream is now responsible for fd + const content = replacePublicPath ? replacePlaceholder(read, publicPath) : read; + const response = h - .response(replacePlaceholder(read, publicPath)) + .response(content) .takeover() .code(200) .type(request.server.mime.path(path).type); @@ -118,7 +121,7 @@ export async function createDynamicAssetResponse({ if (fd) { try { await fcb(cb => close(fd!, cb)); - } catch (err) { + } catch (_) { // ignore errors from close, we already have one to report // and it's very likely they are the same }