[6.x] [pluginDiscovery] dedupe packs by absolute path (#16829) (#16854)

* [pluginDiscovery] dedupe packs by absolute path

* [pluginDiscovery] extract distinct keySelector, add more docs
This commit is contained in:
Spencer 2018-02-21 16:31:02 -07:00 committed by GitHub
parent 34dbcd9f09
commit d2ba76c6b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View file

@ -70,5 +70,27 @@ describe('plugin discovery', () => {
expect(specs.map(s => s.getId()).sort())
.to.eql(['bar:two', 'foo']);
});
it('dedupes duplicate packs', async () => {
const { spec$ } = findPluginSpecs({
plugins: {
scanDirs: [PLUGIN_FIXTURES],
paths: [
resolve(PLUGIN_FIXTURES, 'foo'),
resolve(PLUGIN_FIXTURES, 'foo'),
resolve(PLUGIN_FIXTURES, 'bar'),
resolve(PLUGIN_FIXTURES, 'bar'),
],
}
});
const specs = await spec$.toArray().toPromise();
expect(specs).to.have.length(3);
specs.forEach(spec => {
expect(spec).to.be.a(PluginSpec);
});
expect(specs.map(s => s.getId()).sort())
.to.eql(['bar:one', 'bar:two', 'foo']);
});
});
});

View file

@ -1,4 +1,5 @@
import { Observable } from 'rxjs';
import { realpathSync } from 'fs';
import { transformDeprecations, Config } from '../server/config';
@ -31,6 +32,27 @@ function bufferAllResults(observable) {
.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.pack) {
return realpathSync(result.pack.getPath());
}
// non error/pack results shouldn't exist, but if they do they are all unique
return result;
}
/**
* Creates a collection of observables for discovering pluginSpecs
* using Kibana's defaults, settings, and config service
@ -46,6 +68,7 @@ export function findPluginSpecs(settings, config = defaultConfig(settings)) {
...config.get('plugins.paths').map(createPackAtPath$),
...config.get('plugins.scanDirs').map(createPacksInDirectory$)
)
.distinct(getDistinctKeyForFindResult)
.share();
const extendConfig$ = find$