Merge remote-tracking branch 'origin/master' into marley/19264-circular-dependency-fix
This commit is contained in:
@@ -1,5 +1,56 @@
|
||||
# @backstage/backend-app-api
|
||||
|
||||
## 0.5.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- b9c57a4f857e: **BREAKING**: Renamed `configServiceFactory` to `rootConfigServiceFactory`.
|
||||
- a6d7983f349c: **BREAKING**: Removed the `services` option from `createBackend`. Service factories are now `BackendFeature`s and should be installed with `backend.add(...)` instead. The following should be migrated:
|
||||
|
||||
```ts
|
||||
const backend = createBackend({ services: [myCustomServiceFactory] });
|
||||
```
|
||||
|
||||
To instead pass the service factory via `backend.add(...)`:
|
||||
|
||||
```ts
|
||||
const backend = createBackend();
|
||||
backend.add(customRootLoggerServiceFactory);
|
||||
```
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- e65c4896f755: Do not throw in backend.stop, if start failed
|
||||
- c7aa4ff1793c: Allow modules to register extension points.
|
||||
- 57a10c6c69cc: Add validation to make sure that extension points do not cross plugin boundaries.
|
||||
- cc9256a33bcc: Added new experimental `featureDiscoveryServiceFactory`, available as an `/alpha` export.
|
||||
- Updated dependencies
|
||||
- @backstage/backend-common@0.19.2
|
||||
- @backstage/config-loader@1.4.0
|
||||
- @backstage/backend-plugin-api@0.6.0
|
||||
- @backstage/cli-node@0.1.3
|
||||
- @backstage/plugin-auth-node@0.2.17
|
||||
- @backstage/backend-tasks@0.5.5
|
||||
- @backstage/plugin-permission-node@0.7.11
|
||||
- @backstage/cli-common@0.1.12
|
||||
- @backstage/config@1.0.8
|
||||
- @backstage/errors@1.2.1
|
||||
- @backstage/types@1.1.0
|
||||
|
||||
## 0.5.0-next.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- e65c4896f755: Do not throw in backend.stop, if start failed
|
||||
- cc9256a33bcc: Added new experimental `featureDiscoveryServiceFactory`, available as an `/alpha` export.
|
||||
- Updated dependencies
|
||||
- @backstage/backend-plugin-api@0.6.0-next.2
|
||||
- @backstage/backend-tasks@0.5.5-next.2
|
||||
- @backstage/backend-common@0.19.2-next.2
|
||||
- @backstage/plugin-permission-node@0.7.11-next.2
|
||||
- @backstage/plugin-auth-node@0.2.17-next.2
|
||||
- @backstage/config-loader@1.4.0-next.1
|
||||
|
||||
## 0.5.0-next.1
|
||||
|
||||
### Minor Changes
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## API Report File for "@backstage/backend-app-api"
|
||||
|
||||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
||||
|
||||
```ts
|
||||
import { FeatureDiscoveryService } from '@backstage/backend-plugin-api/alpha';
|
||||
import { ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
|
||||
// @alpha (undocumented)
|
||||
export const featureDiscoveryServiceFactory: () => ServiceFactory<
|
||||
FeatureDiscoveryService,
|
||||
'root'
|
||||
>;
|
||||
|
||||
// (No @packageDocumentation comment for this package)
|
||||
```
|
||||
@@ -43,7 +43,7 @@ import { UrlReader } from '@backstage/backend-common';
|
||||
// @public (undocumented)
|
||||
export interface Backend {
|
||||
// (undocumented)
|
||||
add(feature: BackendFeature): void;
|
||||
add(feature: BackendFeature | (() => BackendFeature)): void;
|
||||
// (undocumented)
|
||||
start(): Promise<void>;
|
||||
// (undocumented)
|
||||
@@ -81,7 +81,7 @@ export function createSpecializedBackend(
|
||||
// @public (undocumented)
|
||||
export interface CreateSpecializedBackendOptions {
|
||||
// (undocumented)
|
||||
services: ServiceFactoryOrFunction[];
|
||||
defaultServiceFactories: ServiceFactoryOrFunction[];
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
{
|
||||
"name": "@backstage/backend-app-api",
|
||||
"description": "Core API used by Backstage backend apps",
|
||||
"version": "0.5.0-next.1",
|
||||
"version": "0.5.0",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"main": "dist/index.cjs.js",
|
||||
"types": "dist/index.d.ts"
|
||||
"access": "public"
|
||||
},
|
||||
"backstage": {
|
||||
"role": "node-library"
|
||||
},
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./alpha": "./src/alpha.ts",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"alpha": [
|
||||
"src/alpha.ts"
|
||||
],
|
||||
"package.json": [
|
||||
"package.json"
|
||||
]
|
||||
}
|
||||
},
|
||||
"homepage": "https://backstage.io",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -36,6 +49,7 @@
|
||||
"@backstage/backend-plugin-api": "workspace:^",
|
||||
"@backstage/backend-tasks": "workspace:^",
|
||||
"@backstage/cli-common": "workspace:^",
|
||||
"@backstage/cli-node": "workspace:^",
|
||||
"@backstage/config": "workspace:^",
|
||||
"@backstage/config-loader": "workspace:^",
|
||||
"@backstage/errors": "workspace:^",
|
||||
@@ -73,6 +87,7 @@
|
||||
"@types/node-forge": "^1.3.0",
|
||||
"@types/stoppable": "^1.1.0",
|
||||
"http-errors": "^2.0.0",
|
||||
"mock-fs": "^5.2.0",
|
||||
"supertest": "^6.1.3"
|
||||
},
|
||||
"files": [
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
export { featureDiscoveryServiceFactory } from './alpha/featureDiscoveryServiceFactory';
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed 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 mockFs from 'mock-fs';
|
||||
import { resolve as resolvePath, dirname } from 'path';
|
||||
import { startTestBackend, mockServices } from '@backstage/backend-test-utils';
|
||||
import { featureDiscoveryServiceFactory } from './featureDiscoveryServiceFactory';
|
||||
import {
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
const rootDir = dirname(process.argv[1]);
|
||||
|
||||
describe('featureDiscoveryServiceFactory', () => {
|
||||
beforeEach(() => {
|
||||
mockFs({
|
||||
[rootDir]: {
|
||||
'package.json': JSON.stringify({
|
||||
name: 'example-app',
|
||||
dependencies: {
|
||||
'detected-plugin': '0.0.0',
|
||||
'detected-module': '0.0.0',
|
||||
},
|
||||
}),
|
||||
},
|
||||
[resolvePath(rootDir, 'node_modules/detected-plugin')]: {
|
||||
'package.json': JSON.stringify({
|
||||
name: 'detected-plugin',
|
||||
main: 'index.js',
|
||||
backstage: {
|
||||
role: 'backend-plugin',
|
||||
},
|
||||
}),
|
||||
'index.js': `
|
||||
const { createBackendPlugin, coreServices } = require('@backstage/backend-plugin-api');
|
||||
exports.detectedPlugin = createBackendPlugin({
|
||||
pluginId: 'detected',
|
||||
register(env) {
|
||||
env.registerInit({
|
||||
deps: { identity: coreServices.identity },
|
||||
async init({ identity }) {
|
||||
identity.getIdentity('detected-plugin');
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
`,
|
||||
},
|
||||
[resolvePath(rootDir, 'node_modules/detected-module')]: {
|
||||
'package.json': JSON.stringify({
|
||||
name: 'detected-module',
|
||||
main: 'index.js',
|
||||
backstage: {
|
||||
role: 'backend-module',
|
||||
},
|
||||
}),
|
||||
'index.js': `
|
||||
const { createBackendModule, coreServices } = require('@backstage/backend-plugin-api');
|
||||
exports.detectedModuleDerp = createBackendModule({
|
||||
pluginId: 'detected',
|
||||
moduleId: 'derp',
|
||||
register(env) {
|
||||
env.registerInit({
|
||||
deps: { identity: coreServices.identity },
|
||||
async init({ identity }) {
|
||||
identity.getIdentity('detected-module');
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
`,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
it('should detect plugin and module packages', async () => {
|
||||
const fn = jest.fn().mockResolvedValue({});
|
||||
|
||||
await startTestBackend({
|
||||
features: [
|
||||
createServiceFactory({
|
||||
service: coreServices.identity,
|
||||
deps: {},
|
||||
factory: () => ({ getIdentity: fn }),
|
||||
}),
|
||||
featureDiscoveryServiceFactory(),
|
||||
mockServices.rootConfig.factory({
|
||||
data: { backend: { packages: 'all' } },
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(fn).toHaveBeenCalledWith('detected-plugin');
|
||||
expect(fn).toHaveBeenCalledWith('detected-module');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed 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 {
|
||||
BackendFeature,
|
||||
RootConfigService,
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
featureDiscoveryServiceRef,
|
||||
FeatureDiscoveryService,
|
||||
} from '@backstage/backend-plugin-api/alpha';
|
||||
import { resolve as resolvePath, dirname } from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { BackstagePackageJson } from '@backstage/cli-node';
|
||||
|
||||
const LOADED_PACKAGE_ROLES = ['backend-plugin', 'backend-module'];
|
||||
|
||||
/** @internal */
|
||||
async function findClosestPackageDir(
|
||||
searchDir: string,
|
||||
): Promise<string | undefined> {
|
||||
let path = searchDir;
|
||||
|
||||
// Some confidence check to avoid infinite loop
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
const packagePath = resolvePath(path, 'package.json');
|
||||
const exists = await fs.pathExists(packagePath);
|
||||
if (exists) {
|
||||
return path;
|
||||
}
|
||||
|
||||
const newPath = dirname(path);
|
||||
if (newPath === path) {
|
||||
return undefined;
|
||||
}
|
||||
path = newPath;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Iteration limit reached when searching for root package.json at ${searchDir}`,
|
||||
);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
class PackageDiscoveryService implements FeatureDiscoveryService {
|
||||
constructor(private readonly config: RootConfigService) {}
|
||||
|
||||
async getBackendFeatures(): Promise<{ features: Array<BackendFeature> }> {
|
||||
if (this.config.getOptionalString('backend.packages') !== 'all') {
|
||||
return { features: [] };
|
||||
}
|
||||
|
||||
const packageDir = await findClosestPackageDir(process.argv[1]);
|
||||
if (!packageDir) {
|
||||
throw new Error('Package discovery failed to find package.json');
|
||||
}
|
||||
const { dependencies } = require(resolvePath(
|
||||
packageDir,
|
||||
'package.json',
|
||||
)) as BackstagePackageJson;
|
||||
const dependencyNames = Object.keys(dependencies || {});
|
||||
|
||||
const features: BackendFeature[] = [];
|
||||
|
||||
for (const name of dependencyNames) {
|
||||
const depPkg = require(require.resolve(`${name}/package.json`, {
|
||||
paths: [packageDir],
|
||||
})) as BackstagePackageJson;
|
||||
if (!LOADED_PACKAGE_ROLES.includes(depPkg?.backstage?.role ?? '')) {
|
||||
continue;
|
||||
}
|
||||
const depModule = require(require.resolve(name, { paths: [packageDir] }));
|
||||
for (const exportValue of Object.values(depModule)) {
|
||||
if (isBackendFeature(exportValue)) {
|
||||
features.push(exportValue);
|
||||
}
|
||||
if (isBackendFeatureFactory(exportValue)) {
|
||||
features.push(exportValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { features };
|
||||
}
|
||||
}
|
||||
|
||||
/** @alpha */
|
||||
export const featureDiscoveryServiceFactory = createServiceFactory({
|
||||
service: featureDiscoveryServiceRef,
|
||||
deps: {
|
||||
config: coreServices.rootConfig,
|
||||
},
|
||||
factory({ config }) {
|
||||
return new PackageDiscoveryService(config);
|
||||
},
|
||||
});
|
||||
|
||||
function isBackendFeature(value: unknown): value is BackendFeature {
|
||||
return (
|
||||
!!value &&
|
||||
typeof value === 'object' &&
|
||||
(value as BackendFeature).$$type === '@backstage/BackendFeature'
|
||||
);
|
||||
}
|
||||
|
||||
function isBackendFeatureFactory(
|
||||
value: unknown,
|
||||
): value is () => BackendFeature {
|
||||
return (
|
||||
!!value &&
|
||||
typeof value === 'function' &&
|
||||
(value as any).$$type === '@backstage/BackendFeatureFactory'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed 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 { DependencyGraph } from './DependencyGraph';
|
||||
|
||||
describe('DependencyGraph', () => {
|
||||
it('should be empty', async () => {
|
||||
const empty = DependencyGraph.fromMap({});
|
||||
expect(empty.findUnsatisfiedDeps()).toEqual([]);
|
||||
expect(empty.detectCircularDependency()).toBeUndefined();
|
||||
await expect(
|
||||
empty.parallelTopologicalTraversal(async id => id),
|
||||
).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
describe('detectCircularDependency', () => {
|
||||
it('should return undefined with not deps', () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: {},
|
||||
2: {},
|
||||
3: {},
|
||||
4: {},
|
||||
}).detectCircularDependency(),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined with no circular deps', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { consumes: ['a'], provides: ['b', 'c'] },
|
||||
3: { consumes: ['b'] },
|
||||
4: { consumes: ['c'] },
|
||||
}).detectCircularDependency(),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should detect an immediate circular dep', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'], consumes: ['a'] },
|
||||
}).detectCircularDependency(),
|
||||
).toEqual(['1', '1']);
|
||||
});
|
||||
|
||||
it('should detect a small circular dep', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'], consumes: ['b'] },
|
||||
2: { provides: ['b'], consumes: ['a'] },
|
||||
}).detectCircularDependency(),
|
||||
).toEqual(['1', '2', '1']);
|
||||
});
|
||||
|
||||
it('should detect a larger distant circular dep', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { provides: ['b'], consumes: ['a', 'e'] },
|
||||
3: { provides: ['c'], consumes: ['b'] },
|
||||
4: { provides: ['d', 'e'], consumes: ['c', 'a'] },
|
||||
}).detectCircularDependency(),
|
||||
).toEqual(['2', '3', '4', '2']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findUnsatisfiedDeps', () => {
|
||||
it('should return nothing with no deps', () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: {},
|
||||
2: {},
|
||||
3: {},
|
||||
4: {},
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return nothing when all deps are satisfied', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { consumes: ['a'], provides: ['b', 'c'] },
|
||||
3: { consumes: ['b'] },
|
||||
4: { consumes: ['c'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('should find a single unsatisfied dep', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { consumes: ['a'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([{ value: '1', unsatisfied: ['a'] }]);
|
||||
});
|
||||
|
||||
it('should handle circular dependencies', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { consumes: ['a'], provides: ['a'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([]);
|
||||
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { consumes: ['a'], provides: ['b'] },
|
||||
2: { consumes: ['b'], provides: ['a'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([]);
|
||||
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { consumes: ['a'] },
|
||||
2: { consumes: ['b'], provides: ['c'] },
|
||||
3: { consumes: ['c'], provides: ['a', 'b'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('should find multiple unsatisfied deps for one node', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'], consumes: ['b'] },
|
||||
2: { provides: ['b'], consumes: ['a', 'd', 'e'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([{ value: '2', unsatisfied: ['d', 'e'] }]);
|
||||
});
|
||||
|
||||
it('should find multiple unsatisfied deps for multiple nodes', async () => {
|
||||
expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { provides: ['b'], consumes: ['a', 'd', 'e'] },
|
||||
3: { provides: [], consumes: ['b'] },
|
||||
4: { provides: [], consumes: ['c', 'a'] },
|
||||
}).findUnsatisfiedDeps(),
|
||||
).toEqual([
|
||||
{ value: '2', unsatisfied: ['d', 'e'] },
|
||||
{ value: '4', unsatisfied: ['c'] },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parallelTopologicalTraversal', () => {
|
||||
it('should traverse with no deps', async () => {
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: {},
|
||||
2: {},
|
||||
3: {},
|
||||
4: {},
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).resolves.toEqual(['1', '2', '3', '4']);
|
||||
});
|
||||
|
||||
it('should traverse with a few deps', async () => {
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { consumes: ['a'], provides: ['b', 'c'] },
|
||||
3: { consumes: ['b'] },
|
||||
4: { consumes: ['c'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).resolves.toEqual(['1', '2', '3', '4']);
|
||||
});
|
||||
|
||||
it('should traverse in reverse', async () => {
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { consumes: ['c'] },
|
||||
2: { provides: ['c'], consumes: ['b'] },
|
||||
3: { provides: ['b'], consumes: ['a'] },
|
||||
4: { provides: ['a'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).resolves.toEqual(['4', '3', '2', '1']);
|
||||
});
|
||||
|
||||
it('should execute in parallel', async () => {
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { provides: ['b'], consumes: ['a'] },
|
||||
3: { provides: ['c'], consumes: ['a'] },
|
||||
4: { consumes: ['b'] },
|
||||
5: { consumes: ['c'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).resolves.toEqual(['1', '2', '3', '4', '5']);
|
||||
|
||||
// Same as above, but with 2 being delayed
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { provides: ['b'], consumes: ['a'] },
|
||||
3: { provides: ['c'], consumes: ['a'] },
|
||||
4: { consumes: ['b'] },
|
||||
5: { consumes: ['c'] },
|
||||
}).parallelTopologicalTraversal(async id => {
|
||||
// When delaying 2 we expect 3 and 5 to complete before 2 and 4
|
||||
if (id === '2') {
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
return id;
|
||||
}),
|
||||
).resolves.toEqual(['1', '3', '5', '2', '4']);
|
||||
});
|
||||
|
||||
it('should detect circular dependencies', async () => {
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'], consumes: ['a'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).rejects.toThrow('Circular dependency detected');
|
||||
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'], consumes: ['b'] },
|
||||
2: { provides: ['b'], consumes: ['a'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).rejects.toThrow('Circular dependency detected');
|
||||
|
||||
await expect(
|
||||
DependencyGraph.fromMap({
|
||||
1: { provides: ['a'] },
|
||||
2: { provides: ['c'], consumes: ['a', 'b'] },
|
||||
3: { provides: ['b'], consumes: ['a', 'c'] },
|
||||
}).parallelTopologicalTraversal(async id => id),
|
||||
).rejects.toThrow('Circular dependency detected');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
interface NodeInput<T> {
|
||||
value: T;
|
||||
consumes?: Iterable<string>;
|
||||
provides?: Iterable<string>;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
class Node<T> {
|
||||
static from<T>(input: NodeInput<T>) {
|
||||
return new Node<T>(
|
||||
input.value,
|
||||
input.consumes ? new Set(input.consumes) : new Set(),
|
||||
input.provides ? new Set(input.provides) : new Set(),
|
||||
);
|
||||
}
|
||||
|
||||
private constructor(
|
||||
readonly value: T,
|
||||
readonly consumes: Set<string>,
|
||||
readonly provides: Set<string>,
|
||||
) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal helper to help validate and traverse a dependency graph.
|
||||
* @internal
|
||||
*/
|
||||
export class DependencyGraph<T> {
|
||||
static fromMap(
|
||||
nodes: Record<string, Omit<NodeInput<unknown>, 'value'>>,
|
||||
): DependencyGraph<string> {
|
||||
return this.fromIterable(
|
||||
Object.entries(nodes).map(([key, node]) => ({
|
||||
value: String(key),
|
||||
...node,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
static fromIterable<T>(
|
||||
nodeInputs: Iterable<NodeInput<T>>,
|
||||
): DependencyGraph<T> {
|
||||
const nodes = new Array<Node<T>>();
|
||||
for (const nodeInput of nodeInputs) {
|
||||
nodes.push(Node.from(nodeInput));
|
||||
}
|
||||
|
||||
return new DependencyGraph(nodes);
|
||||
}
|
||||
|
||||
#nodes: Array<Node<T>>;
|
||||
#allProvided: Set<string>;
|
||||
|
||||
private constructor(nodes: Array<Node<T>>) {
|
||||
this.#nodes = nodes;
|
||||
this.#allProvided = new Set();
|
||||
|
||||
for (const node of this.#nodes.values()) {
|
||||
for (const produced of node.provides) {
|
||||
this.#allProvided.add(produced);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find all nodes that consume dependencies that are not provided by any other node
|
||||
findUnsatisfiedDeps(): Array<{ value: T; unsatisfied: string[] }> {
|
||||
const unsatisfiedDependencies = [];
|
||||
for (const node of this.#nodes.values()) {
|
||||
const unsatisfied = Array.from(node.consumes).filter(
|
||||
id => !this.#allProvided.has(id),
|
||||
);
|
||||
if (unsatisfied.length > 0) {
|
||||
unsatisfiedDependencies.push({ value: node.value, unsatisfied });
|
||||
}
|
||||
}
|
||||
return unsatisfiedDependencies;
|
||||
}
|
||||
|
||||
// Detect circular dependencies within the graph, returning the path of nodes that
|
||||
// form a cycle, with the same node as the first and last element of the array.
|
||||
detectCircularDependency(): T[] | undefined {
|
||||
for (const startNode of this.#nodes) {
|
||||
const visited = new Set<Node<T>>();
|
||||
const stack = new Array<[node: Node<T>, path: T[]]>([
|
||||
startNode,
|
||||
[startNode.value],
|
||||
]);
|
||||
|
||||
while (stack.length > 0) {
|
||||
const [node, path] = stack.pop()!;
|
||||
if (visited.has(node)) {
|
||||
continue;
|
||||
}
|
||||
visited.add(node);
|
||||
for (const produced of node.provides) {
|
||||
const consumerNodes = this.#nodes.filter(other =>
|
||||
other.consumes.has(produced),
|
||||
);
|
||||
for (const consumer of consumerNodes) {
|
||||
if (consumer === startNode) {
|
||||
return [...path, startNode.value];
|
||||
}
|
||||
if (!visited.has(consumer)) {
|
||||
stack.push([consumer, [...path, consumer.value]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses the dependency graph in topological order, calling the provided
|
||||
* function for each node and waiting for it to resolve.
|
||||
*
|
||||
* The nodes are traversed in parallel, but in such a way that no node is
|
||||
* visited before all of its dependencies.
|
||||
*
|
||||
* Dependencies of nodes that are not produced by any other nodes will be ignored.
|
||||
*/
|
||||
async parallelTopologicalTraversal<TResult>(
|
||||
fn: (value: T) => Promise<TResult>,
|
||||
): Promise<TResult[]> {
|
||||
const allProvided = this.#allProvided;
|
||||
const producedSoFar = new Set<string>();
|
||||
const waiting = new Set(this.#nodes.values());
|
||||
const visited = new Set<Node<T>>();
|
||||
const results = new Array<TResult>();
|
||||
let inFlight = 0; // Keep track of how many callbacks are in flight, so that we know if we got stuck
|
||||
|
||||
// Find all nodes that have no dependencies that have not already been produced by visited nodes
|
||||
async function processMoreNodes() {
|
||||
if (waiting.size === 0) {
|
||||
return;
|
||||
}
|
||||
const nodesToProcess = [];
|
||||
for (const node of waiting) {
|
||||
let ready = true;
|
||||
for (const consumed of node.consumes) {
|
||||
if (allProvided.has(consumed) && !producedSoFar.has(consumed)) {
|
||||
ready = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (ready) {
|
||||
nodesToProcess.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
for (const node of nodesToProcess) {
|
||||
waiting.delete(node);
|
||||
}
|
||||
|
||||
if (nodesToProcess.length === 0 && inFlight === 0) {
|
||||
// We expect the caller to check for circular dependencies before
|
||||
// traversal, so this error should never happen
|
||||
throw new Error('Circular dependency detected');
|
||||
}
|
||||
|
||||
await Promise.all(nodesToProcess.map(processNode));
|
||||
}
|
||||
|
||||
// Process an individual node, and then add its produced dependencies to the set of available products
|
||||
async function processNode(node: Node<T>) {
|
||||
visited.add(node);
|
||||
inFlight += 1;
|
||||
|
||||
const result = await fn(node.value);
|
||||
results.push(result);
|
||||
|
||||
node.provides.forEach(produced => producedSoFar.add(produced));
|
||||
inFlight -= 1;
|
||||
await processMoreNodes();
|
||||
}
|
||||
|
||||
await processMoreNodes();
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -61,8 +61,7 @@ describe('schedulerFactory', () => {
|
||||
});
|
||||
|
||||
await startTestBackend({
|
||||
features: [plugin()],
|
||||
services: [subject],
|
||||
features: [plugin(), subject],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,10 +20,15 @@ import {
|
||||
coreServices,
|
||||
createBackendPlugin,
|
||||
createBackendModule,
|
||||
createExtensionPoint,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { BackendInitializer } from './BackendInitializer';
|
||||
import { ServiceRegistry } from './ServiceRegistry';
|
||||
import { rootLifecycleServiceFactory } from '../services/implementations';
|
||||
|
||||
import {
|
||||
lifecycleServiceFactory,
|
||||
loggerServiceFactory,
|
||||
rootLifecycleServiceFactory,
|
||||
} from '../services/implementations';
|
||||
|
||||
const rootRef = createServiceRef<{ x: number }>({
|
||||
id: '1',
|
||||
@@ -44,12 +49,23 @@ class MockLogger {
|
||||
}
|
||||
}
|
||||
|
||||
const baseFactories = [
|
||||
lifecycleServiceFactory(),
|
||||
rootLifecycleServiceFactory(),
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLogger,
|
||||
deps: {},
|
||||
factory: () => new MockLogger(),
|
||||
})(),
|
||||
loggerServiceFactory(),
|
||||
];
|
||||
|
||||
describe('BackendInitializer', () => {
|
||||
it('should initialize root scoped services', async () => {
|
||||
const rootFactory = jest.fn();
|
||||
const pluginFactory = jest.fn();
|
||||
|
||||
const registry = new ServiceRegistry([
|
||||
const services = [
|
||||
createServiceFactory({
|
||||
service: rootRef,
|
||||
deps: {},
|
||||
@@ -66,17 +82,76 @@ describe('BackendInitializer', () => {
|
||||
deps: {},
|
||||
factory: () => new MockLogger(),
|
||||
})(),
|
||||
]);
|
||||
];
|
||||
|
||||
const init = new BackendInitializer(registry);
|
||||
const init = new BackendInitializer(services);
|
||||
await init.start();
|
||||
|
||||
expect(rootFactory).toHaveBeenCalled();
|
||||
expect(pluginFactory).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should initialize modules with extension points', async () => {
|
||||
expect.assertions(3);
|
||||
|
||||
const extensionPoint = createExtensionPoint<{ values: string[] }>({
|
||||
id: 'a',
|
||||
});
|
||||
const init = new BackendInitializer(baseFactories);
|
||||
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
moduleId: 'modA',
|
||||
register(reg) {
|
||||
reg.registerInit({
|
||||
deps: { extension: extensionPoint },
|
||||
async init({ extension }) {
|
||||
expect(extension.values).toEqual(['b']);
|
||||
extension.values.push('a');
|
||||
},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
moduleId: 'modB',
|
||||
register(reg) {
|
||||
const values = ['b'];
|
||||
reg.registerExtensionPoint(extensionPoint, { values });
|
||||
reg.registerInit({
|
||||
deps: {},
|
||||
async init() {
|
||||
expect(values).toEqual(['b', 'a', 'c']);
|
||||
},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
moduleId: 'modC',
|
||||
register(reg) {
|
||||
reg.registerInit({
|
||||
deps: { extension: extensionPoint },
|
||||
async init({ extension }) {
|
||||
expect(extension.values).toEqual(['b', 'a']);
|
||||
extension.values.push('c');
|
||||
},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
await init.start();
|
||||
});
|
||||
|
||||
it('should forward errors when plugins fail to start', async () => {
|
||||
const init = new BackendInitializer(new ServiceRegistry([]));
|
||||
const init = new BackendInitializer([]);
|
||||
init.add(
|
||||
createBackendPlugin({
|
||||
pluginId: 'test',
|
||||
@@ -96,7 +171,7 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
it('should forward errors when modules fail to start', async () => {
|
||||
const init = new BackendInitializer(new ServiceRegistry([]));
|
||||
const init = new BackendInitializer([]);
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
@@ -117,7 +192,7 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
it('should reject duplicate plugins', async () => {
|
||||
const init = new BackendInitializer(new ServiceRegistry([]));
|
||||
const init = new BackendInitializer([]);
|
||||
init.add(
|
||||
createBackendPlugin({
|
||||
pluginId: 'test',
|
||||
@@ -146,7 +221,7 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
it('should reject duplicate modules', async () => {
|
||||
const init = new BackendInitializer(new ServiceRegistry([]));
|
||||
const init = new BackendInitializer([]);
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
@@ -175,4 +250,78 @@ describe('BackendInitializer', () => {
|
||||
"Module 'mod' for plugin 'test' is already registered",
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject modules with circular dependencies', async () => {
|
||||
const extA = createExtensionPoint<string>({ id: 'a' });
|
||||
const extB = createExtensionPoint<string>({ id: 'b' });
|
||||
const init = new BackendInitializer([
|
||||
rootLifecycleServiceFactory(),
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLogger,
|
||||
deps: {},
|
||||
factory: () => new MockLogger(),
|
||||
})(),
|
||||
]);
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
moduleId: 'modA',
|
||||
register(reg) {
|
||||
reg.registerExtensionPoint(extA, 'a');
|
||||
reg.registerInit({
|
||||
deps: { ext: extB },
|
||||
async init() {},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'test',
|
||||
moduleId: 'modB',
|
||||
register(reg) {
|
||||
reg.registerExtensionPoint(extB, 'b');
|
||||
reg.registerInit({
|
||||
deps: { ext: extA },
|
||||
async init() {},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
await expect(init.start()).rejects.toThrow(
|
||||
"Circular dependency detected for modules of plugin 'test', 'modA' -> 'modB' -> 'modA'",
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject modules that depend on extension points other plugins', async () => {
|
||||
const init = new BackendInitializer(baseFactories);
|
||||
const extA = createExtensionPoint<string>({ id: 'a' });
|
||||
init.add(
|
||||
createBackendPlugin({
|
||||
pluginId: 'testA',
|
||||
register(reg) {
|
||||
reg.registerExtensionPoint(extA, 'a');
|
||||
reg.registerInit({
|
||||
deps: {},
|
||||
async init() {},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
init.add(
|
||||
createBackendModule({
|
||||
pluginId: 'testB',
|
||||
moduleId: 'mod',
|
||||
register(reg) {
|
||||
reg.registerInit({
|
||||
deps: { ext: extA },
|
||||
async init() {},
|
||||
});
|
||||
},
|
||||
})(),
|
||||
);
|
||||
await expect(init.start()).rejects.toThrow(
|
||||
"Extension point registered for plugin 'testA' may not be used by module for plugin 'testB'",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
ExtensionPoint,
|
||||
coreServices,
|
||||
ServiceRef,
|
||||
ServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { BackendLifecycleImpl } from '../services/implementations/rootLifecycle/rootLifecycleServiceFactory';
|
||||
import { BackendPluginLifecycleImpl } from '../services/implementations/lifecycle/lifecycleServiceFactory';
|
||||
@@ -26,7 +27,10 @@ import { EnumerableServiceHolder, ServiceOrExtensionPoint } from './types';
|
||||
// Direct internal import to avoid duplication
|
||||
// eslint-disable-next-line @backstage/no-forbidden-package-imports
|
||||
import { InternalBackendFeature } from '@backstage/backend-plugin-api/src/wiring/types';
|
||||
import { ForwardedError } from '@backstage/errors';
|
||||
import { ForwardedError, ConflictError } from '@backstage/errors';
|
||||
import { featureDiscoveryServiceRef } from '@backstage/backend-plugin-api/alpha';
|
||||
import { DependencyGraph } from '../lib/DependencyGraph';
|
||||
import { ServiceRegistry } from './ServiceRegistry';
|
||||
|
||||
export interface BackendRegisterInit {
|
||||
consumes: Set<ServiceOrExtensionPoint>;
|
||||
@@ -40,11 +44,16 @@ export interface BackendRegisterInit {
|
||||
export class BackendInitializer {
|
||||
#startPromise?: Promise<void>;
|
||||
#features = new Array<InternalBackendFeature>();
|
||||
#extensionPoints = new Map<ExtensionPoint<unknown>, unknown>();
|
||||
#serviceHolder: EnumerableServiceHolder;
|
||||
#extensionPoints = new Map<
|
||||
ExtensionPoint<unknown>,
|
||||
{ impl: unknown; pluginId: string }
|
||||
>();
|
||||
#serviceHolder: EnumerableServiceHolder | undefined;
|
||||
#providedServiceFactories = new Array<ServiceFactory>();
|
||||
#defaultApiFactories: ServiceFactory[];
|
||||
|
||||
constructor(serviceHolder: EnumerableServiceHolder) {
|
||||
this.#serviceHolder = serviceHolder;
|
||||
constructor(defaultApiFactories: ServiceFactory[]) {
|
||||
this.#defaultApiFactories = defaultApiFactories;
|
||||
}
|
||||
|
||||
async #getInitDeps(
|
||||
@@ -55,13 +64,16 @@ export class BackendInitializer {
|
||||
const missingRefs = new Set<ServiceOrExtensionPoint>();
|
||||
|
||||
for (const [name, ref] of Object.entries(deps)) {
|
||||
const extensionPoint = this.#extensionPoints.get(
|
||||
ref as ExtensionPoint<unknown>,
|
||||
);
|
||||
if (extensionPoint) {
|
||||
result.set(name, extensionPoint);
|
||||
const ep = this.#extensionPoints.get(ref as ExtensionPoint<unknown>);
|
||||
if (ep) {
|
||||
if (ep.pluginId !== pluginId) {
|
||||
throw new Error(
|
||||
`Extension point registered for plugin '${ep.pluginId}' may not be used by module for plugin '${pluginId}'`,
|
||||
);
|
||||
}
|
||||
result.set(name, ep.impl);
|
||||
} else {
|
||||
const impl = await this.#serviceHolder.get(
|
||||
const impl = await this.#serviceHolder!.get(
|
||||
ref as ServiceRef<unknown>,
|
||||
pluginId,
|
||||
);
|
||||
@@ -87,18 +99,44 @@ export class BackendInitializer {
|
||||
if (this.#startPromise) {
|
||||
throw new Error('feature can not be added after the backend has started');
|
||||
}
|
||||
this.#addFeature(feature);
|
||||
}
|
||||
|
||||
#addFeature(feature: BackendFeature) {
|
||||
if (feature.$$type !== '@backstage/BackendFeature') {
|
||||
throw new Error(
|
||||
`Failed to add feature, invalid type '${feature.$$type}'`,
|
||||
);
|
||||
}
|
||||
const internalFeature = feature as InternalBackendFeature;
|
||||
if (internalFeature.version !== 'v1') {
|
||||
|
||||
if (isServiceFactory(feature)) {
|
||||
if (feature.service.id === coreServices.pluginMetadata.id) {
|
||||
throw new Error(
|
||||
`The ${coreServices.pluginMetadata.id} service cannot be overridden`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
this.#providedServiceFactories.find(
|
||||
sf => sf.service.id === feature.service.id,
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
`Duplicate service implementations provided for ${feature.service.id}`,
|
||||
);
|
||||
}
|
||||
this.#providedServiceFactories.push(feature);
|
||||
} else if (isInternalBackendFeature(feature)) {
|
||||
if (feature.version !== 'v1') {
|
||||
throw new Error(
|
||||
`Failed to add feature, invalid version '${feature.version}'`,
|
||||
);
|
||||
}
|
||||
this.#features.push(feature);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Failed to add feature, invalid version '${internalFeature.version}'`,
|
||||
`Failed to add feature, invalid feature ${JSON.stringify(feature)}`,
|
||||
);
|
||||
}
|
||||
this.#features.push(internalFeature);
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
@@ -129,6 +167,23 @@ export class BackendInitializer {
|
||||
}
|
||||
|
||||
async #doStart(): Promise<void> {
|
||||
this.#serviceHolder = new ServiceRegistry([
|
||||
...this.#defaultApiFactories,
|
||||
...this.#providedServiceFactories,
|
||||
]);
|
||||
|
||||
const featureDiscovery = await this.#serviceHolder.get(
|
||||
featureDiscoveryServiceRef,
|
||||
'root',
|
||||
);
|
||||
|
||||
if (featureDiscovery) {
|
||||
const { features } = await featureDiscovery.getBackendFeatures();
|
||||
for (const feature of features) {
|
||||
this.#addFeature(feature);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize all root scoped services
|
||||
for (const ref of this.#serviceHolder.getServiceRefs()) {
|
||||
if (ref.scope === 'root') {
|
||||
@@ -144,14 +199,17 @@ export class BackendInitializer {
|
||||
for (const r of feature.getRegistrations()) {
|
||||
const provides = new Set<ExtensionPoint<unknown>>();
|
||||
|
||||
if (r.type === 'plugin') {
|
||||
if (r.type === 'plugin' || r.type === 'module') {
|
||||
for (const [extRef, extImpl] of r.extensionPoints) {
|
||||
if (this.#extensionPoints.has(extRef)) {
|
||||
throw new Error(
|
||||
`ExtensionPoint with ID '${extRef.id}' is already registered`,
|
||||
);
|
||||
}
|
||||
this.#extensionPoints.set(extRef, extImpl);
|
||||
this.#extensionPoints.set(extRef, {
|
||||
impl: extImpl,
|
||||
pluginId: r.pluginId,
|
||||
});
|
||||
provides.add(extRef);
|
||||
}
|
||||
}
|
||||
@@ -193,21 +251,41 @@ export class BackendInitializer {
|
||||
await Promise.all(
|
||||
allPluginIds.map(async pluginId => {
|
||||
// Modules are initialized before plugins, so that they can provide extension to the plugin
|
||||
const modules = moduleInits.get(pluginId) ?? [];
|
||||
await Promise.all(
|
||||
Array.from(modules).map(async ([moduleId, moduleInit]) => {
|
||||
const moduleDeps = await this.#getInitDeps(
|
||||
moduleInit.init.deps,
|
||||
pluginId,
|
||||
const modules = moduleInits.get(pluginId);
|
||||
if (modules) {
|
||||
const tree = DependencyGraph.fromIterable(
|
||||
Array.from(modules).map(([moduleId, moduleInit]) => ({
|
||||
value: { moduleId, moduleInit },
|
||||
// Relationships are reversed at this point since we're only interested in the extension points.
|
||||
// If a modules provides extension point A we want it to be initialized AFTER all modules
|
||||
// that depend on extension point A, so that they can provide their extensions.
|
||||
consumes: Array.from(moduleInit.provides).map(p => p.id),
|
||||
provides: Array.from(moduleInit.consumes).map(c => c.id),
|
||||
})),
|
||||
);
|
||||
const circular = tree.detectCircularDependency();
|
||||
if (circular) {
|
||||
throw new ConflictError(
|
||||
`Circular dependency detected for modules of plugin '${pluginId}', ${circular
|
||||
.map(({ moduleId }) => `'${moduleId}'`)
|
||||
.join(' -> ')}`,
|
||||
);
|
||||
await moduleInit.init.func(moduleDeps).catch(error => {
|
||||
throw new ForwardedError(
|
||||
`Module '${moduleId}' for plugin '${pluginId}' startup failed`,
|
||||
error,
|
||||
}
|
||||
await tree.parallelTopologicalTraversal(
|
||||
async ({ moduleId, moduleInit }) => {
|
||||
const moduleDeps = await this.#getInitDeps(
|
||||
moduleInit.init.deps,
|
||||
pluginId,
|
||||
);
|
||||
});
|
||||
}),
|
||||
);
|
||||
await moduleInit.init.func(moduleDeps).catch(error => {
|
||||
throw new ForwardedError(
|
||||
`Module '${moduleId}' for plugin '${pluginId}' startup failed`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Once all modules have been initialized, we can initialize the plugin itself
|
||||
const pluginInit = pluginInits.get(pluginId);
|
||||
@@ -272,7 +350,7 @@ export class BackendInitializer {
|
||||
|
||||
// Bit of a hacky way to grab the lifecycle services, potentially find a nicer way to do this
|
||||
async #getRootLifecycleImpl(): Promise<BackendLifecycleImpl> {
|
||||
const lifecycleService = await this.#serviceHolder.get(
|
||||
const lifecycleService = await this.#serviceHolder!.get(
|
||||
coreServices.rootLifecycle,
|
||||
'root',
|
||||
);
|
||||
@@ -285,7 +363,7 @@ export class BackendInitializer {
|
||||
async #getPluginLifecycleImpl(
|
||||
pluginId: string,
|
||||
): Promise<BackendPluginLifecycleImpl> {
|
||||
const lifecycleService = await this.#serviceHolder.get(
|
||||
const lifecycleService = await this.#serviceHolder!.get(
|
||||
coreServices.lifecycle,
|
||||
pluginId,
|
||||
);
|
||||
@@ -295,3 +373,15 @@ export class BackendInitializer {
|
||||
throw new Error('Unexpected plugin lifecycle service implementation');
|
||||
}
|
||||
}
|
||||
|
||||
function isServiceFactory(feature: BackendFeature): feature is ServiceFactory {
|
||||
return !!(feature as ServiceFactory).service;
|
||||
}
|
||||
|
||||
function isInternalBackendFeature(
|
||||
feature: BackendFeature,
|
||||
): feature is InternalBackendFeature {
|
||||
return (
|
||||
typeof (feature as InternalBackendFeature).getRegistrations === 'function'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,22 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ServiceFactory, BackendFeature } from '@backstage/backend-plugin-api';
|
||||
import { BackendFeature, ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
import { BackendInitializer } from './BackendInitializer';
|
||||
import { ServiceRegistry } from './ServiceRegistry';
|
||||
import { Backend } from './types';
|
||||
|
||||
export class BackstageBackend implements Backend {
|
||||
#services: ServiceRegistry;
|
||||
#initializer: BackendInitializer;
|
||||
|
||||
constructor(apiFactories: ServiceFactory[]) {
|
||||
this.#services = new ServiceRegistry(apiFactories);
|
||||
this.#initializer = new BackendInitializer(this.#services);
|
||||
constructor(defaultServiceFactories: ServiceFactory[]) {
|
||||
this.#initializer = new BackendInitializer(defaultServiceFactories);
|
||||
}
|
||||
|
||||
add(feature: BackendFeature): void {
|
||||
this.#initializer.add(feature);
|
||||
add(feature: BackendFeature | (() => BackendFeature)): void {
|
||||
this.#initializer.add(typeof feature === 'function' ? feature() : feature);
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
|
||||
@@ -31,7 +31,7 @@ const sf1 = createServiceFactory({
|
||||
async factory() {
|
||||
return { x: 1 };
|
||||
},
|
||||
})();
|
||||
});
|
||||
|
||||
const ref2 = createServiceRef<{ x: number }>({
|
||||
scope: 'root',
|
||||
@@ -43,14 +43,14 @@ const sf2 = createServiceFactory({
|
||||
async factory() {
|
||||
return { x: 2 };
|
||||
},
|
||||
})();
|
||||
});
|
||||
const sf2b = createServiceFactory({
|
||||
service: ref2,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return { x: 22 };
|
||||
},
|
||||
})();
|
||||
});
|
||||
|
||||
const refDefault1 = createServiceRef<{ x: number }>({
|
||||
id: '1',
|
||||
@@ -61,7 +61,7 @@ const refDefault1 = createServiceRef<{ x: number }>({
|
||||
async factory() {
|
||||
return { x: 10 };
|
||||
},
|
||||
})(),
|
||||
}),
|
||||
});
|
||||
|
||||
const refDefault2a = createServiceRef<{ x: number }>({
|
||||
@@ -95,7 +95,7 @@ describe('ServiceRegistry', () => {
|
||||
});
|
||||
|
||||
it('should return an implementation for a registered ref', async () => {
|
||||
const registry = new ServiceRegistry([sf1]);
|
||||
const registry = new ServiceRegistry([sf1()]);
|
||||
await expect(registry.get(ref1, 'catalog')).resolves.toEqual({ x: 1 });
|
||||
await expect(registry.get(ref1, 'scaffolder')).resolves.toEqual({ x: 1 });
|
||||
expect(await registry.get(ref1, 'catalog')).toBe(
|
||||
@@ -110,7 +110,7 @@ describe('ServiceRegistry', () => {
|
||||
});
|
||||
|
||||
it('should handle multiple factories with different serviceRefs', async () => {
|
||||
const registry = new ServiceRegistry([sf1, sf2]);
|
||||
const registry = new ServiceRegistry([sf1(), sf2()]);
|
||||
|
||||
await expect(registry.get(ref1, 'catalog')).resolves.toEqual({
|
||||
x: 1,
|
||||
@@ -131,7 +131,7 @@ describe('ServiceRegistry', () => {
|
||||
return { x: 2 };
|
||||
},
|
||||
});
|
||||
const registry = new ServiceRegistry([factory(), sf1]);
|
||||
const registry = new ServiceRegistry([factory(), sf1()]);
|
||||
await expect(registry.get(ref2, 'catalog')).rejects.toThrow(
|
||||
"Failed to instantiate 'root' scoped service '2' because it depends on 'plugin' scoped service '1'.",
|
||||
);
|
||||
@@ -145,7 +145,7 @@ describe('ServiceRegistry', () => {
|
||||
return { x: rootDep.x };
|
||||
},
|
||||
});
|
||||
const registry = new ServiceRegistry([factory(), sf2]);
|
||||
const registry = new ServiceRegistry([factory(), sf2()]);
|
||||
await expect(registry.get(ref1, 'catalog')).resolves.toEqual({
|
||||
x: 2,
|
||||
});
|
||||
@@ -160,7 +160,7 @@ describe('ServiceRegistry', () => {
|
||||
return { x: rootDep.x };
|
||||
},
|
||||
});
|
||||
const registry = new ServiceRegistry([factory(), sf2]);
|
||||
const registry = new ServiceRegistry([factory(), sf2()]);
|
||||
await expect(registry.get(ref, 'catalog')).resolves.toEqual({
|
||||
x: 2,
|
||||
});
|
||||
@@ -182,7 +182,7 @@ describe('ServiceRegistry', () => {
|
||||
});
|
||||
|
||||
it('should use the last factory for each ref', async () => {
|
||||
const registry = new ServiceRegistry([sf2, sf2b]);
|
||||
const registry = new ServiceRegistry([sf2(), sf2b()]);
|
||||
await expect(registry.get(ref2, 'catalog')).resolves.toEqual({
|
||||
x: 22,
|
||||
});
|
||||
@@ -196,7 +196,7 @@ describe('ServiceRegistry', () => {
|
||||
});
|
||||
|
||||
it('should not use the defaultFactory from the ref if provided to the registry', async () => {
|
||||
const registry = new ServiceRegistry([sf1]);
|
||||
const registry = new ServiceRegistry([sf1()]);
|
||||
await expect(registry.get(refDefault1, 'catalog')).resolves.toEqual({
|
||||
x: 1,
|
||||
});
|
||||
|
||||
@@ -39,7 +39,7 @@ function toInternalServiceFactory<TService, TScope extends 'plugin' | 'root'>(
|
||||
factory: ServiceFactory<TService, TScope>,
|
||||
): InternalServiceFactory<TService, TScope> {
|
||||
const f = factory as InternalServiceFactory<TService, TScope>;
|
||||
if (f.$$type !== '@backstage/ServiceFactory') {
|
||||
if (f.$$type !== '@backstage/BackendFeature') {
|
||||
throw new Error(`Invalid service factory, bad type '${f.$$type}'`);
|
||||
}
|
||||
if (f.version !== 'v1') {
|
||||
@@ -49,10 +49,10 @@ function toInternalServiceFactory<TService, TScope extends 'plugin' | 'root'>(
|
||||
}
|
||||
|
||||
const pluginMetadataServiceFactory = createServiceFactory(
|
||||
(options: { pluginId: string }) => ({
|
||||
(options?: { pluginId: string }) => ({
|
||||
service: coreServices.pluginMetadata,
|
||||
deps: {},
|
||||
factory: async () => ({ getId: () => options.pluginId }),
|
||||
factory: async () => ({ getId: () => options?.pluginId! }),
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -22,13 +22,15 @@ import { createSpecializedBackend } from './createSpecializedBackend';
|
||||
|
||||
describe('createSpecializedBackend', () => {
|
||||
it('should create a backend without services', () => {
|
||||
expect(() => createSpecializedBackend({ services: [] })).not.toThrow();
|
||||
expect(() =>
|
||||
createSpecializedBackend({ defaultServiceFactories: [] }),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw on duplicate service implementations', () => {
|
||||
expect(() =>
|
||||
createSpecializedBackend({
|
||||
services: [
|
||||
defaultServiceFactories: [
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
@@ -55,7 +57,7 @@ describe('createSpecializedBackend', () => {
|
||||
it('should throw when providing a plugin metadata service implementation', () => {
|
||||
expect(() =>
|
||||
createSpecializedBackend({
|
||||
services: [
|
||||
defaultServiceFactories: [
|
||||
createServiceFactory({
|
||||
service: coreServices.pluginMetadata,
|
||||
deps: {},
|
||||
|
||||
@@ -24,7 +24,7 @@ import { Backend, CreateSpecializedBackendOptions } from './types';
|
||||
export function createSpecializedBackend(
|
||||
options: CreateSpecializedBackendOptions,
|
||||
): Backend {
|
||||
const services = options.services.map(sf =>
|
||||
const services = options.defaultServiceFactories.map(sf =>
|
||||
typeof sf === 'function' ? sf() : sf,
|
||||
);
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
* @public
|
||||
*/
|
||||
export interface Backend {
|
||||
add(feature: BackendFeature): void;
|
||||
add(feature: BackendFeature | (() => BackendFeature)): void;
|
||||
start(): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ export interface Backend {
|
||||
* @public
|
||||
*/
|
||||
export interface CreateSpecializedBackendOptions {
|
||||
services: ServiceFactoryOrFunction[];
|
||||
defaultServiceFactories: ServiceFactoryOrFunction[];
|
||||
}
|
||||
|
||||
export interface ServiceHolder {
|
||||
|
||||
Reference in New Issue
Block a user