-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathindex.ts
299 lines (247 loc) · 8.23 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { URL } from 'url';
import mime from 'mime-types';
import type { Response } from 'node-fetch';
import { splitPkgKey as split } from '../../../../common';
import { KibanaAssetType } from '../../../types';
import type {
AssetsGroupedByServiceByType,
CategoryId,
CategorySummaryList,
InstallSource,
RegistryPackage,
RegistrySearchResults,
GetCategoriesRequest,
} from '../../../types';
import {
getArchiveFilelist,
getPathParts,
unpackBufferToCache,
getPackageInfo,
setPackageInfo,
} from '../archive';
import { streamToBuffer } from '../streams';
import { appContextService } from '../..';
import { PackageNotFoundError, PackageCacheError, RegistryResponseError } from '../../../errors';
import { getBundledPackageByName } from '../packages/bundled_packages';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { getRegistryUrl } from './registry_url';
export interface SearchParams {
category?: CategoryId;
experimental?: boolean;
}
export const splitPkgKey = split;
export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) =>
`${name}-${version}`;
export async function fetchList(params?: SearchParams): Promise<RegistrySearchResults> {
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search`);
if (params) {
if (params.category) {
url.searchParams.set('category', params.category);
}
if (params.experimental) {
url.searchParams.set('experimental', params.experimental.toString());
}
}
setKibanaVersion(url);
return fetchUrl(url.toString()).then(JSON.parse);
}
interface FetchFindLatestPackageOptions {
ignoreConstraints?: boolean;
}
async function _fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const { ignoreConstraints = false } = options ?? {};
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search?package=${packageName}&experimental=true`);
if (!ignoreConstraints) {
setKibanaVersion(url);
}
const res = await fetchUrl(url.toString(), 1);
const searchResults: RegistryPackage[] = JSON.parse(res);
return searchResults;
}
export async function fetchFindLatestPackageOrThrow(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
try {
const searchResults = await _fetchFindLatestPackage(packageName, options);
if (!searchResults.length) {
throw new PackageNotFoundError(`[${packageName}] package not found in registry`);
}
return searchResults[0];
} catch (error) {
const bundledPackage = await getBundledPackageByName(packageName);
if (!bundledPackage) {
throw error;
}
return bundledPackage;
}
}
export async function fetchFindLatestPackageOrUndefined(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
try {
const searchResults = await _fetchFindLatestPackage(packageName, options);
if (!searchResults.length) {
return undefined;
}
return searchResults[0];
} catch (error) {
const bundledPackage = await getBundledPackageByName(packageName);
if (!bundledPackage) {
return undefined;
}
return bundledPackage;
}
}
export async function fetchInfo(pkgName: string, pkgVersion: string): Promise<RegistryPackage> {
const registryUrl = getRegistryUrl();
try {
const res = await fetchUrl(`${registryUrl}/package/${pkgName}/${pkgVersion}`).then(JSON.parse);
return res;
} catch (err) {
if (err instanceof RegistryResponseError && err.status === 404) {
throw new PackageNotFoundError(`${pkgName}@${pkgVersion} not found`);
}
throw err;
}
}
export async function getFile(
pkgName: string,
pkgVersion: string,
relPath: string
): Promise<Response> {
const filePath = `/package/${pkgName}/${pkgVersion}/${relPath}`;
return fetchFile(filePath);
}
export async function fetchFile(filePath: string): Promise<Response> {
const registryUrl = getRegistryUrl();
return getResponse(`${registryUrl}${filePath}`);
}
function setKibanaVersion(url: URL) {
const disableVersionCheck =
appContextService.getConfig()?.developer?.disableRegistryVersionCheck ?? false;
if (disableVersionCheck) {
return;
}
const kibanaVersion = appContextService.getKibanaVersion().split('-')[0]; // may be x.y.z-SNAPSHOT
if (kibanaVersion) {
url.searchParams.set('kibana.version', kibanaVersion);
}
}
export async function fetchCategories(
params?: GetCategoriesRequest['query']
): Promise<CategorySummaryList> {
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/categories`);
if (params) {
if (params.experimental) {
url.searchParams.set('experimental', params.experimental.toString());
}
if (params.include_policy_templates) {
url.searchParams.set('include_policy_templates', params.include_policy_templates.toString());
}
}
setKibanaVersion(url);
return fetchUrl(url.toString()).then(JSON.parse);
}
export async function getInfo(name: string, version: string) {
let packageInfo = getPackageInfo({ name, version });
if (!packageInfo) {
packageInfo = await fetchInfo(name, version);
setPackageInfo({ name, version, packageInfo });
}
return packageInfo as RegistryPackage;
}
export async function getRegistryPackage(
name: string,
version: string
): Promise<{ paths: string[]; packageInfo: RegistryPackage }> {
const installSource = 'registry';
let paths = getArchiveFilelist({ name, version });
if (!paths || paths.length === 0) {
const { archiveBuffer, archivePath } = await fetchArchiveBuffer(name, version);
paths = await unpackBufferToCache({
name,
version,
installSource,
archiveBuffer,
contentType: ensureContentType(archivePath),
});
}
const packageInfo = await getInfo(name, version);
return { paths, packageInfo };
}
function ensureContentType(archivePath: string) {
const contentType = mime.lookup(archivePath);
if (!contentType) {
throw new Error(`Unknown compression format for '${archivePath}'. Please use .zip or .gz`);
}
return contentType;
}
export async function ensureCachedArchiveInfo(
name: string,
version: string,
installSource: InstallSource = 'registry'
) {
const paths = getArchiveFilelist({ name, version });
if (!paths || paths.length === 0) {
if (installSource === 'registry') {
await getRegistryPackage(name, version);
} else {
throw new PackageCacheError(
`Package ${name}-${version} not cached. If it was uploaded, try uninstalling and reinstalling manually.`
);
}
}
}
async function fetchArchiveBuffer(
pkgName: string,
pkgVersion: string
): Promise<{ archiveBuffer: Buffer; archivePath: string }> {
const { download: archivePath } = await getInfo(pkgName, pkgVersion);
const archiveUrl = `${getRegistryUrl()}${archivePath}`;
const archiveBuffer = await getResponseStream(archiveUrl).then(streamToBuffer);
return { archiveBuffer, archivePath };
}
export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByType {
const kibanaAssetTypes = Object.values<string>(KibanaAssetType);
// ASK: best way, if any, to avoid `any`?
const assets = paths.reduce((map: any, path) => {
const parts = getPathParts(path.replace(/^\/package\//, ''));
if (
(parts.service === 'kibana' && kibanaAssetTypes.includes(parts.type)) ||
parts.service === 'elasticsearch'
) {
if (!map[parts.service]) map[parts.service] = {};
if (!map[parts.service][parts.type]) map[parts.service][parts.type] = [];
map[parts.service][parts.type].push(parts);
}
return map;
}, {});
return {
kibana: assets.kibana,
elasticsearch: assets.elasticsearch,
};
}
export function getNoticePath(paths: string[]): string | undefined {
for (const path of paths) {
const parts = getPathParts(path.replace(/^\/package\//, ''));
if (parts.type === 'notice') {
const { pkgName, pkgVersion } = splitPkgKey(parts.pkgkey);
return `/package/${pkgName}/${pkgVersion}/${parts.file}`;
}
}
return undefined;
}