forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_plugin_specs.js
260 lines (231 loc) · 7.39 KB
/
find_plugin_specs.js
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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import * as Rx from 'rxjs';
import { distinct, toArray, mergeMap, share, shareReplay, filter, last, map, tap } from 'rxjs/operators';
import { realpathSync } from 'fs';
import { transformDeprecations, Config } from '../server/config';
import {
extendConfigService,
disableConfigExtension,
} from './plugin_config';
import {
createPack$,
createPackageJsonAtPath$,
createPackageJsonsInDirectory$,
} from './plugin_pack';
import {
isInvalidDirectoryError,
isInvalidPackError,
} from './errors';
function defaultConfig(settings) {
return Config.withDefaultSchema(
transformDeprecations(settings)
);
}
function bufferAllResults(observable) {
return observable.pipe(
// buffer all results into a single array
toArray(),
// merge the array back into the stream when complete
mergeMap(array => array)
);
}
/**
* Determine a distinct value for each result from find$
* so they can be deduplicated
* @param {{error?,pack?}} result
* @return {Any}
*/
function getDistinctKeyForFindResult(result) {
// errors are distinct by their message
if (result.error) {
return result.error.message;
}
// packs are distinct by their absolute and real path
if (result.packageJson) {
return realpathSync(result.packageJson.directoryPath);
}
// non error/pack results shouldn't exist, but if they do they are all unique
return result;
}
function groupSpecsById(specs) {
const specsById = new Map();
for (const spec of specs) {
const id = spec.getId();
if (specsById.has(id)) {
specsById.get(id).push(spec);
} else {
specsById.set(id, [spec]);
}
}
return specsById;
}
/**
* Creates a collection of observables for discovering pluginSpecs
* using Kibana's defaults, settings, and config service
*
* @param {Object} settings
* @param {ConfigService} [configToMutate] when supplied **it is mutated** to
* include the config from discovered plugin specs
* @return {Object<name,Rx>}
*/
export function findPluginSpecs(settings, configToMutate) {
const config$ = Rx
.defer(async () => {
if (configToMutate) {
return configToMutate;
}
return defaultConfig(settings);
})
.pipe(shareReplay());
// find plugin packs in configured paths/dirs
const packageJson$ = config$.pipe(
mergeMap(config => Rx.merge(
...config.get('plugins.paths').map(createPackageJsonAtPath$),
...config.get('plugins.scanDirs').map(createPackageJsonsInDirectory$)
)),
distinct(getDistinctKeyForFindResult),
share()
);
const pack$ = createPack$(packageJson$).pipe(
share()
);
const extendConfig$ = config$.pipe(
mergeMap(config => (
pack$.pipe(
// get the specs for each found plugin pack
mergeMap(({ pack }) => (
pack ? pack.getPluginSpecs() : []
)),
// make sure that none of the plugin specs have conflicting ids, fail
// early if conflicts detected or merge the specs back into the stream
toArray(),
mergeMap(allSpecs => {
for (const [id, specs] of groupSpecsById(allSpecs)) {
if (specs.length > 1) {
throw new Error(
`Multiple plugins found with the id "${id}":\n${
specs.map(spec => ` - ${id} at ${spec.getPath()}`).join('\n')
}`
);
}
}
return allSpecs;
}),
mergeMap(async (spec) => {
// extend the config service with this plugin spec and
// collect its deprecations messages if some of its
// settings are outdated
const deprecations = [];
await extendConfigService(spec, config, settings, (message) => {
deprecations.push({ spec, message });
});
return {
spec,
deprecations,
};
}),
// extend the config with all plugins before determining enabled status
bufferAllResults,
map(({ spec, deprecations }) => {
const isRightVersion = spec.isVersionCompatible(config.get('pkg.version'));
const enabled = isRightVersion && spec.isEnabled(config);
return {
config,
spec,
deprecations,
enabledSpecs: enabled ? [spec] : [],
disabledSpecs: enabled ? [] : [spec],
invalidVersionSpecs: isRightVersion ? [] : [spec],
};
}),
// determine which plugins are disabled before actually removing things from the config
bufferAllResults,
tap(result => {
for (const spec of result.disabledSpecs) {
disableConfigExtension(spec, config);
}
})
)
)),
share()
);
return {
// package JSONs found when searching configure paths
packageJson$: packageJson$.pipe(
mergeMap(result => (
result.packageJson ? [result.packageJson] : []
))
),
// plugin packs found when searching configured paths
pack$: pack$.pipe(
mergeMap(result => (
result.pack ? [result.pack] : []
))
),
// errors caused by invalid directories of plugin directories
invalidDirectoryError$: pack$.pipe(
mergeMap(result => (
isInvalidDirectoryError(result.error) ? [result.error] : []
))
),
// errors caused by directories that we expected to be plugin but were invalid
invalidPackError$: pack$.pipe(
mergeMap(result => (
isInvalidPackError(result.error) ? [result.error] : []
))
),
otherError$: pack$.pipe(
mergeMap(result => (
isUnhandledError(result.error) ? [result.error] : []
))
),
// { spec, message } objects produced when transforming deprecated
// settings for a plugin spec
deprecation$: extendConfig$.pipe(
mergeMap(result => result.deprecations)
),
// the config service we extended with all of the plugin specs,
// only emitted once it is fully extended by all
extendedConfig$: extendConfig$.pipe(
mergeMap(result => result.config),
filter(Boolean),
last()
),
// all enabled PluginSpec objects
spec$: extendConfig$.pipe(
mergeMap(result => result.enabledSpecs)
),
// all disabled PluginSpec objects
disabledSpec$: extendConfig$.pipe(
mergeMap(result => result.disabledSpecs)
),
// all PluginSpec objects that were disabled because their version was incompatible
invalidVersionSpec$: extendConfig$.pipe(
mergeMap(result => result.invalidVersionSpecs)
),
};
}
function isUnhandledError(error) {
return (
error != null &&
!isInvalidDirectoryError(error) &&
!isInvalidPackError(error)
);
}