UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
3// Node module: @loopback/boot
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.createComponentApplicationBooterBinding = exports.createBooterForComponentApplication = exports.bindingKeysExcludedFromSubApp = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const debug_1 = tslib_1.__importDefault(require("debug"));
11const keys_1 = require("../keys");
12const types_1 = require("../types");
13const debug = (0, debug_1.default)('loopback:boot:booter:component-application');
14/**
15 * Binding keys excluded from a sub application. These bindings booted from the
16 * sub application won't be added to the main application.
17 */
18exports.bindingKeysExcludedFromSubApp = [
19 keys_1.BootBindings.BOOT_OPTIONS.key,
20 keys_1.BootBindings.PROJECT_ROOT.key,
21 keys_1.BootBindings.BOOTSTRAPPER_KEY.key,
22 core_1.CoreBindings.APPLICATION_CONFIG.key,
23 core_1.CoreBindings.APPLICATION_INSTANCE.key,
24 core_1.CoreBindings.APPLICATION_METADATA.key,
25 core_1.CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY.key,
26 core_1.CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS.key,
27];
28/**
29 * Create a booter that boots the component application. Bindings that exist
30 * in the component application before `boot` are skipped. Locked bindings in
31 * the main application will not be overridden.
32 *
33 * @param componentApp - The application exposing a component
34 * @param filter Binding filter to selected bindings to be added
35 */
36function createBooterForComponentApplication(componentApp, filter = () => true) {
37 /**
38 * A booter to boot artifacts for the component application
39 */
40 let ComponentApplicationBooter = class ComponentApplicationBooter {
41 constructor(mainApp) {
42 this.mainApp = mainApp;
43 }
44 async load() {
45 /**
46 * List all bindings before boot
47 */
48 let bindings = componentApp.find(() => true);
49 const bindingsBeforeBoot = new Set(bindings);
50 // Boot the component application
51 await componentApp.boot();
52 /**
53 * Add bindings from the component application to the main application
54 */
55 bindings = componentApp.find(filter);
56 for (const binding of bindings) {
57 // Exclude boot related bindings
58 if (exports.bindingKeysExcludedFromSubApp.includes(binding.key))
59 continue;
60 // Exclude bindings from the app before boot
61 if (bindingsBeforeBoot.has(binding)) {
62 debug('Skipping binding %s that exists before booting %s', binding.key, componentApp.name);
63 continue;
64 }
65 // Do not override locked bindings
66 const locked = this.mainApp.find(binding.key).some(b => b.isLocked);
67 if (locked) {
68 debug('Skipping binding %s from %s - locked in %s', binding.key, componentApp.name, this.mainApp.name);
69 continue;
70 }
71 debug('Adding binding from %s to %s', componentApp.name, this.mainApp.name, binding);
72 this.mainApp.add(binding);
73 }
74 }
75 };
76 ComponentApplicationBooter = tslib_1.__decorate([
77 (0, types_1.booter)('componentApplications'),
78 tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
79 tslib_1.__metadata("design:paramtypes", [core_1.Application])
80 ], ComponentApplicationBooter);
81 return ComponentApplicationBooter;
82}
83exports.createBooterForComponentApplication = createBooterForComponentApplication;
84/**
85 * Create a binding to register a booter that boots the component application.
86 * Bindings that exist in the component application before `boot` are skipped.
87 * Locked bindings in the main application will not be overridden.
88 *
89 * @param componentApp - The application exposing a component
90 * @param filter Binding filter to selected bindings to be added
91 */
92function createComponentApplicationBooterBinding(componentApp, filter) {
93 return (0, core_1.createBindingFromClass)(createBooterForComponentApplication(componentApp, filter), { key: `booters.${componentApp.name}` });
94}
95exports.createComponentApplicationBooterBinding = createComponentApplicationBooterBinding;
96//# sourceMappingURL=component-application.booter.js.map
\No newline at end of file