-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(boot): add helpers to create a booter for component applications #5304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright IBM Corp. 2019,2020. All Rights Reserved. | ||
| // Node module: @loopback/boot | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Application, Component} from '@loopback/core'; | ||
| import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab'; | ||
| import {resolve} from 'path'; | ||
| import { | ||
| BootBindings, | ||
| BootMixin, | ||
| createComponentApplicationBooterBinding, | ||
| } from '../..'; | ||
| import {BooterApp} from '../fixtures/application'; | ||
|
|
||
| describe('component application booter acceptance tests', () => { | ||
| let app: BooterApp; | ||
| const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), { | ||
| // We intentionally use this flag so that `dist/application.js` can keep | ||
| // its relative path to satisfy import statements | ||
| subdir: false, | ||
| }); | ||
|
|
||
| beforeEach('reset sandbox', () => sandbox.reset()); | ||
| beforeEach(getApp); | ||
|
|
||
| it('binds artifacts booted from the component application', async () => { | ||
| class BooterAppComponent implements Component { | ||
| bindings = [createComponentApplicationBooterBinding(app)]; | ||
| } | ||
|
|
||
| const mainApp = new MainApp(); | ||
| mainApp.component(BooterAppComponent); | ||
| const appBindingsBeforeBoot = mainApp.find( | ||
| // Exclude boot related bindings | ||
| binding => | ||
| ![ | ||
| BootBindings.BOOT_OPTIONS.key, | ||
| BootBindings.PROJECT_ROOT.key, | ||
| BootBindings.BOOTSTRAPPER_KEY.key, | ||
| ].includes(binding.key), | ||
| ); | ||
| await mainApp.boot(); | ||
| const controllers = mainApp.find('controllers.*').map(b => b.key); | ||
| expect(controllers).to.eql([ | ||
| 'controllers.ArtifactOne', | ||
| 'controllers.ArtifactTwo', | ||
| ]); | ||
|
|
||
| // Assert main app bindings before boot are not overridden | ||
| const appBindingsAfterBoot = mainApp.find(binding => | ||
| appBindingsBeforeBoot.includes(binding), | ||
| ); | ||
| expect(appBindingsAfterBoot.map(b => b.key)).to.eql( | ||
| appBindingsBeforeBoot.map(b => b.key), | ||
| ); | ||
| }); | ||
|
|
||
| it('binds artifacts booted from the component application by filter', async () => { | ||
| class BooterAppComponent implements Component { | ||
| bindings = [ | ||
| createComponentApplicationBooterBinding(app, binding => { | ||
| return binding.key === 'controllers.ArtifactOne'; | ||
| }), | ||
| ]; | ||
| } | ||
|
|
||
| const mainApp = new MainApp(); | ||
| mainApp.component(BooterAppComponent); | ||
| await mainApp.boot(); | ||
| const controllers = mainApp.find('controllers.*').map(b => b.key); | ||
| expect(controllers).to.eql(['controllers.ArtifactOne']); | ||
| }); | ||
|
|
||
| it('does not override locked bindings', async () => { | ||
| class BooterAppComponent implements Component { | ||
| bindings = [createComponentApplicationBooterBinding(app)]; | ||
| } | ||
|
|
||
| const mainApp = new MainApp(); | ||
| const lockedBinding = mainApp | ||
| .bind('controllers.ArtifactTwo') | ||
| .to('-locked-') | ||
| .lock(); | ||
| mainApp.component(BooterAppComponent); | ||
| await mainApp.boot(); | ||
| const current = mainApp.getBinding('controllers.ArtifactTwo', { | ||
| optional: true, | ||
| }); | ||
| expect(current).to.be.exactly(lockedBinding); | ||
| }); | ||
|
|
||
| class MainApp extends BootMixin(Application) { | ||
| constructor() { | ||
| super(); | ||
| this.projectRoot = __dirname; | ||
| } | ||
| } | ||
|
|
||
| async function getApp() { | ||
| await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json')); | ||
| await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
| await sandbox.copyFile( | ||
| resolve(__dirname, '../fixtures/multiple.artifact.js'), | ||
| 'controllers/multiple.controller.js', | ||
| ); | ||
|
|
||
| const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp; | ||
| app = new MyApp({ | ||
| rest: givenHttpServerConfig(), | ||
| }); | ||
| } | ||
| }); |
113 changes: 113 additions & 0 deletions
113
packages/boot/src/booters/component-application.booter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/boot | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import { | ||
| Application, | ||
| Binding, | ||
| BindingFilter, | ||
| Constructor, | ||
| CoreBindings, | ||
| createBindingFromClass, | ||
| inject, | ||
| } from '@loopback/core'; | ||
| import debugFactory from 'debug'; | ||
| import {BootBindings} from '../keys'; | ||
| import {Bootable, Booter, booter} from '../types'; | ||
|
|
||
| const debug = debugFactory('loopback:boot:booter:component-application'); | ||
|
|
||
| /** | ||
| * Create a booter that boots the component application. Bindings that exist | ||
| * in the component application before `boot` are skipped. Locked bindings in | ||
| * the main application will not be overridden. | ||
| * | ||
| * @param componentApp - The application exposing a component | ||
| * @param filter Binding filter to selected bindings to be added | ||
| */ | ||
| export function createBooterForComponentApplication( | ||
| componentApp: Application & Bootable, | ||
| filter: BindingFilter = () => true, | ||
| ): Constructor<Booter> { | ||
| /** | ||
| * A booter to boot artifacts for the component application | ||
| */ | ||
| @booter('componentApplications') | ||
| class ComponentApplicationBooter implements Booter { | ||
| constructor( | ||
| @inject(CoreBindings.APPLICATION_INSTANCE) private mainApp: Application, | ||
| ) {} | ||
|
|
||
| async load() { | ||
| const bootBindingKeys = [ | ||
| BootBindings.BOOT_OPTIONS.key, | ||
| BootBindings.PROJECT_ROOT.key, | ||
| BootBindings.BOOTSTRAPPER_KEY.key, | ||
| ]; | ||
| /** | ||
| * List all bindings before boot | ||
| */ | ||
| let bindings = componentApp.find(() => true); | ||
| const bindingsBeforeBoot = new Set(bindings); | ||
| // Boot the component application | ||
| await componentApp.boot(); | ||
| /** | ||
| * Add bindings from the component application to the main application | ||
| */ | ||
| bindings = componentApp.find(filter); | ||
| for (const binding of bindings) { | ||
| // Exclude boot related bindings | ||
| if (bootBindingKeys.includes(binding.key)) continue; | ||
|
|
||
| // Exclude bindings from the app before boot | ||
| if (bindingsBeforeBoot.has(binding)) { | ||
| debug( | ||
| 'Skipping binding %s that exists before booting %s', | ||
| binding.key, | ||
| componentApp.name, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| // Do not override locked bindings | ||
| const locked = this.mainApp.find(binding.key).some(b => b.isLocked); | ||
| if (locked) { | ||
| debug( | ||
| 'Skipping binding %s from %s - locked in %s', | ||
| binding.key, | ||
| componentApp.name, | ||
| this.mainApp.name, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| debug( | ||
| 'Adding binding from %s to %s', | ||
| componentApp.name, | ||
| this.mainApp.name, | ||
| binding, | ||
| ); | ||
| this.mainApp.add(binding as Binding); | ||
| } | ||
| } | ||
| } | ||
| return ComponentApplicationBooter; | ||
| } | ||
|
|
||
| /** | ||
| * Create a binding to register a booter that boots the component application. | ||
| * Bindings that exist in the component application before `boot` are skipped. | ||
| * Locked bindings in the main application will not be overridden. | ||
| * | ||
| * @param componentApp - The application exposing a component | ||
| * @param filter Binding filter to selected bindings to be added | ||
| */ | ||
| export function createComponentApplicationBooterBinding( | ||
| componentApp: Application & Bootable, | ||
| filter?: BindingFilter, | ||
| ) { | ||
| return createBindingFromClass( | ||
| createBooterForComponentApplication(componentApp, filter), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raymondfeng I find these instructions quite involved. Would the following approach work & make sense too?
I think it would be great to add a helper method to
BootMixinto wrap those two calls in a single API, but I find it difficult to find a good method name. How aboutaddApplicationBooter?Intended usage:
WDYT?