-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
248 lines (217 loc) · 7.61 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
/*
* 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 semverValid from 'semver/functions/valid';
import type { Response } from 'node-fetch';
import { KibanaAssetType } from '../../../types';
import type {
AssetsGroupedByServiceByType,
CategoryId,
CategorySummaryList,
InstallSource,
RegistryPackage,
RegistrySearchResults,
RegistrySearchResult,
} from '../../../types';
import {
getArchiveFilelist,
getPathParts,
unpackBufferToCache,
getPackageInfo,
setPackageInfo,
} from '../archive';
import { streamToBuffer } from '../streams';
import { appContextService } from '../..';
import {
PackageKeyInvalidError,
PackageNotFoundError,
PackageCacheError,
RegistryResponseError,
} from '../../../errors';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { getRegistryUrl } from './registry_url';
export interface SearchParams {
category?: CategoryId;
experimental?: boolean;
}
export interface CategoriesParams {
experimental?: boolean;
}
/**
* Extract the package name and package version from a string.
*
* @param pkgkey a string containing the package name delimited by the package version
*/
export function splitPkgKey(pkgkey: string): { pkgName: string; pkgVersion: string } {
// this will return an empty string if `indexOf` returns -1
const pkgName = pkgkey.substr(0, pkgkey.indexOf('-'));
if (pkgName === '') {
throw new PackageKeyInvalidError('Package key parsing failed: package name was empty');
}
// this will return the entire string if `indexOf` return -1
const pkgVersion = pkgkey.substr(pkgkey.indexOf('-') + 1);
if (!semverValid(pkgVersion)) {
throw new PackageKeyInvalidError(
'Package key parsing failed: package version was not a valid semver'
);
}
return { pkgName, pkgVersion };
}
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`);
const kibanaVersion = appContextService.getKibanaVersion().split('-')[0]; // may be x.y.z-SNAPSHOT
const kibanaBranch = appContextService.getKibanaBranch();
if (params) {
if (params.category) {
url.searchParams.set('category', params.category);
}
if (params.experimental) {
url.searchParams.set('experimental', params.experimental.toString());
}
}
// on master, request all packages regardless of version
if (kibanaVersion && kibanaBranch !== 'master') {
url.searchParams.set('kibana.version', kibanaVersion);
}
return fetchUrl(url.toString()).then(JSON.parse);
}
export async function fetchFindLatestPackage(packageName: string): Promise<RegistrySearchResult> {
const registryUrl = getRegistryUrl();
const kibanaVersion = appContextService.getKibanaVersion().split('-')[0]; // may be x.y.z-SNAPSHOT
const kibanaBranch = appContextService.getKibanaBranch();
const url = new URL(
`${registryUrl}/search?package=${packageName}&internal=true&experimental=true`
);
// on master, request all packages regardless of version
if (kibanaVersion && kibanaBranch !== 'master') {
url.searchParams.set('kibana.version', kibanaVersion);
}
const res = await fetchUrl(url.toString());
const searchResults = JSON.parse(res);
if (searchResults.length) {
return searchResults[0];
} else {
throw new PackageNotFoundError(`${packageName} not found`);
}
}
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}`);
}
export async function fetchCategories(params?: CategoriesParams): Promise<CategorySummaryList> {
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/categories`);
if (params) {
if (params.experimental) {
url.searchParams.set('experimental', params.experimental.toString());
}
}
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,
};
}