-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathbundles_route.ts
131 lines (120 loc) · 3.69 KB
/
bundles_route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { extname, join } from 'path';
import Hapi from '@hapi/hapi';
import * as UiSharedDeps from '@kbn/ui-shared-deps';
import agent from 'elastic-apm-node';
import { createDynamicAssetResponse } from './dynamic_asset_response';
import { FileHashCache } from './file_hash_cache';
import { assertIsNpUiPluginPublicDirs, NpUiPluginPublicDirs } from '../np_ui_plugin_public_dirs';
import { fromRoot } from '../../core/server/utils';
/**
* Creates the routes that serves files from `bundlesPath`.
*
* @param {Object} options
* @property {Array<{id,path}>} options.npUiPluginPublicDirs array of ids and paths that should be served for new platform plugins
* @property {string} options.regularBundlesPath
* @property {string} options.basePublicPath
*
* @return Array.of({Hapi.Route})
*/
export function createBundlesRoute({
basePublicPath,
npUiPluginPublicDirs = [],
buildHash,
isDist = false,
}: {
basePublicPath: string;
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 FileHashCache();
assertIsNpUiPluginPublicDirs(npUiPluginPublicDirs);
if (typeof basePublicPath !== 'string') {
throw new TypeError('basePublicPath must be a string');
}
if (!basePublicPath.match(/(^$|^\/.*[^\/]$)/)) {
throw new TypeError('basePublicPath must be empty OR start and not end with a /');
}
return [
buildRouteForBundles({
publicPath: `${basePublicPath}/${buildHash}/bundles/kbn-ui-shared-deps/`,
routePath: `/${buildHash}/bundles/kbn-ui-shared-deps/`,
bundlesPath: UiSharedDeps.distDir,
fileHashCache,
isDist,
}),
...npUiPluginPublicDirs.map(({ id, path }) =>
buildRouteForBundles({
publicPath: `${basePublicPath}/${buildHash}/bundles/plugin/${id}/`,
routePath: `/${buildHash}/bundles/plugin/${id}/`,
bundlesPath: path,
fileHashCache,
isDist,
})
),
buildRouteForBundles({
publicPath: `${basePublicPath}/${buildHash}/bundles/core/`,
routePath: `/${buildHash}/bundles/core/`,
bundlesPath: fromRoot(join('src', 'core', 'target', 'public')),
fileHashCache,
isDist,
}),
];
}
function buildRouteForBundles({
publicPath,
routePath,
bundlesPath,
fileHashCache,
isDist,
}: {
publicPath: string;
routePath: string;
bundlesPath: string;
fileHashCache: FileHashCache;
isDist: boolean;
}) {
return {
method: 'GET',
path: `${routePath}{path*}`,
config: {
auth: false,
ext: {
onPreHandler: {
method(request: Hapi.Request, h: Hapi.ResponseToolkit) {
const ext = extname(request.params.path);
agent.setTransactionName('GET ?/bundles/?');
if (ext !== '.js' && ext !== '.css') {
return h.continue;
}
return createDynamicAssetResponse({
request,
h,
bundlesPath,
fileHashCache,
publicPath,
isDist,
});
},
},
},
},
handler: {
directory: {
path: bundlesPath,
listing: false,
lookupCompressed: true,
},
},
};
}