Skip to content

Commit

Permalink
restore replacePublicPath option from elastic#64226
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Apr 28, 2020
1 parent 240553f commit 7c35408
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/optimize/bundles_route/bundles_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) =>
Expand All @@ -96,6 +97,7 @@ export function createBundlesRoute({
routePath: `/${buildHash}/bundles/plugin/${id}/`,
bundlesPath: path,
fileHashCache,
replacePublicPath: false,
isDist,
})
),
Expand Down Expand Up @@ -135,12 +137,14 @@ function buildRouteForBundles({
routePath,
bundlesPath,
fileHashCache,
replacePublicPath = true,
isDist,
}: {
publicPath: string;
routePath: string;
bundlesPath: string;
fileHashCache: LruCache<unknown, unknown>;
replacePublicPath?: boolean;
isDist: boolean;
}) {
return {
Expand All @@ -163,6 +167,7 @@ function buildRouteForBundles({
bundlesPath,
fileHashCache,
publicPath,
replacePublicPath,
isDist,
});
},
Expand Down
17 changes: 10 additions & 7 deletions src/optimize/bundles_route/dynamic_asset_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,13 +66,15 @@ export async function createDynamicAssetResponse({
bundlesPath,
publicPath,
fileHashCache,
replacePublicPath,
isDist,
}: {
request: Hapi.Request;
h: Hapi.ResponseToolkit;
bundlesPath: string;
publicPath: string;
fileHashCache: LruCache<unknown, unknown>;
replacePublicPath: boolean;
isDist: boolean;
}) {
let fd: number | undefined;
Expand All @@ -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);
Expand All @@ -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
}
Expand Down

0 comments on commit 7c35408

Please sign in to comment.