UNPKG

@deepkit/app

Version:

Deepkit App, CLI framework and service container

285 lines 13.8 kB
/*@ts-ignore*/ import { __ΩClassType } from '@deepkit/core'; /*@ts-ignore*/ import { __ΩModuleDefinition } from './module.js'; /*@ts-ignore*/ import { __ΩEventListenerRegistered } from '@deepkit/event'; /*@ts-ignore*/ import { __ΩMiddlewareConfig } from './module.js'; function __assignType(fn, args) { fn.__type = args; return fn; } /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ import { getClassName, isClass, isFunction } from '@deepkit/core'; import { EventDispatcher, isEventListenerContainerEntryCallback } from '@deepkit/event'; import { AppModule, ConfigurationInvalidError } from './module.js'; import { injectedFunction, Injector, InjectorContext, isProvided, resolveToken, } from '@deepkit/injector'; import { cli } from './command.js'; import { WorkflowDefinition } from '@deepkit/workflow'; import { deserialize, ReflectionClass, ReflectionFunction, validate } from '@deepkit/type'; import { ConsoleTransport, Logger, ScopedLogger } from '@deepkit/logger'; import { Stopwatch } from '@deepkit/stopwatch'; export class CliControllerRegistry { constructor() { this.controllers = []; } } CliControllerRegistry.__type = ['ControllerConfig', 'controllers', function () { return []; }, 'CliControllerRegistry', '"w!F3"9>#5w$']; const __ΩMiddlewareRegistryEntry = [() => __ΩMiddlewareConfig, 'config', () => AppModule, 'module', 'MiddlewareRegistryEntry', 'Pn!4"P"7#4$Mw%y']; export { __ΩMiddlewareRegistryEntry as __ΩMiddlewareRegistryEntry }; export class MiddlewareRegistry { constructor() { this.configs = []; } } MiddlewareRegistry.__type = [() => __ΩMiddlewareRegistryEntry, 'configs', function () { return []; }, 'MiddlewareRegistry', 'n!F3"9>#5w$']; export class WorkflowRegistry { constructor(workflows) { this.workflows = workflows; } get(name) { for (const w of this.workflows) { if (w.name === name) return w; } throw new Error(`Workflow with name ${name} does not exist`); } add(workflow) { this.workflows.push(workflow); } } WorkflowRegistry.__type = [() => WorkflowDefinition, 'workflows', 'constructor', 'name', () => WorkflowDefinition, 'get', () => WorkflowDefinition, 'workflow', 'add', 'WorkflowRegistry', 'PP"7!F2":9"0#P&2$P"7%0&PP"7\'2("0)5w*']; const __ΩConfigLoader = [() => AppModule, 'module', 'config', () => ReflectionClass, 'schema', 'load', 'ConfigLoader', 'PPP"7!2"P&"LM2#P"7$2%$1&Mw\'y']; export { __ΩConfigLoader as __ΩConfigLoader }; export class ServiceContainer { constructor(appModule) { this.appModule = appModule; this.cliControllerRegistry = new CliControllerRegistry; this.middlewareRegistry = new MiddlewareRegistry; this.workflowRegistry = new WorkflowRegistry([]); this.configLoaders = []; /** * All modules in the whole module tree. * This is stored to call service container hooks like processController/processProvider. */ this.modules = (Set.Ω = [[() => AppModule, 'P"7!']], new Set()); this.eventDispatcher = new EventDispatcher(this.injectorContext); } addConfigLoader(loader) { this.configLoaders.push(loader); } /** * Builds the whole module tree, processes all providers, controllers, and listeners. * Makes InjectorContext available. Is usually automatically called when the injector is requested. */ process() { if (this.injectorContext) return; this.appModule.addProvider({ provide: ServiceContainer, useValue: this }); this.appModule.addProvider({ provide: EventDispatcher, useValue: this.eventDispatcher }); this.appModule.addProvider({ provide: CliControllerRegistry, useValue: this.cliControllerRegistry }); this.appModule.addProvider({ provide: MiddlewareRegistry, useValue: this.middlewareRegistry }); this.appModule.addProvider({ provide: InjectorContext, useFactory: () => this.injectorContext }); this.appModule.addProvider({ provide: Stopwatch }); this.appModule.addProvider(ConsoleTransport); if (!this.appModule.isProvided(Logger)) { this.appModule.addProvider({ provide: Logger, useFactory: __assignType((t) => new Logger([t]), [() => ConsoleTransport, 't', '', 'PP7!2""/#']) }); } this.appModule.addProvider(ScopedLogger); this.setupHook(this.appModule); this.findModules(this.appModule); this.processModule(this.appModule); this.postProcess(); this.injectorContext = new InjectorContext(this.appModule); this.injectorContext.getRootInjector(); //trigger all injector builds this.bootstrapModules(); } postProcess() { for (const m of this.modules) { m.postProcess(); } } findModules(module) { if (this.modules.has(module)) return; this.modules.add(module); for (const m of module.getImports()) { this.findModules(m); } } getInjectorContext() { this.process(); return this.injectorContext; } setupHook(module) { let config = module.getConfig(); if (module.configDefinition) { const schema = ReflectionClass.from(module.configDefinition); for (const loader of this.configLoaders) { loader.load(module, config, schema); } //config loads can set arbitrary values (like string for numbers), so we try deserialize them automatically Object.assign(config, deserialize(config, undefined, undefined, undefined, schema.type)); for (const setupConfig of module.setupConfigs) setupConfig(module, config); //at this point, no deserialization needs to happen anymore, so validation happens on the config object itself. const errors = validate(config, schema.type); if (errors.length) { const errorsMessage = errors.map(__assignType(v => v.toString(module.getName()), ['v', '', 'P"2!"/"'])).join(', '); throw new ConfigurationInvalidError(`Configuration for module ${module.getName() || 'root'} is invalid. Make sure the module is correctly configured. Error: ` + errorsMessage); } } module.process(); for (const setup of module.setups) setup(module, config); for (const importModule of module.getImports()) { this.setupHook(importModule); } return module; } bootstrapModules() { for (const module of this.modules) { if (module.options.bootstrap) { this.getInjector(module).get(module.options.bootstrap); } for (const use of module.uses) { const resolvedFunction = injectedFunction(use, this.getInjector(module)); resolvedFunction(); } } } getInjector(moduleOrClass) { this.process(); if (!isClass(moduleOrClass)) return this.getInjectorContext().getInjector(moduleOrClass); for (const m of this.modules) { if (m instanceof moduleOrClass) { return this.getInjectorContext().getInjector(m); } } throw new Error(`No module loaded from type ${getClassName(moduleOrClass)}`); } getModule(moduleClass) { this.process(); for (const m of this.modules) { if (m instanceof moduleClass) { return m; } } throw new Error(`No module loaded from type ${getClassName(moduleClass)}`); } /** * Returns all known instantiated modules. */ getModules() { this.process(); return [...this.modules]; } getRootInjector() { this.process(); return this.getInjectorContext().getInjector(this.appModule); } processModule(module) { if (module.injector) { throw new Error(`Module ${getClassName(module)} (id=${module.name}) was already imported. Can not re-use module instances.`); } const providers = module.getProviders(); const controllers = module.getControllers(); const commands = module.getCommands(); const listeners = module.getListeners(); const middlewares = module.getMiddlewares(); if (module.options.bootstrap && !isFunction(module.options.bootstrap) && !module.isProvided(module.options.bootstrap)) { providers.push(module.options.bootstrap); } for (const w of module.getWorkflows()) this.workflowRegistry.add(w); for (const middleware of middlewares) { const config = middleware(); for (const fnOrClassTye of config.getClassTypes()) { if (!isClass(fnOrClassTye)) continue; if (!isProvided(providers, fnOrClassTye)) { providers.unshift(fnOrClassTye); } } this.middlewareRegistry.configs.push({ config, module }); } for (const controller of controllers) { this.processController(module, { module, controller }); } for (const command of commands) { this.processController(module, { module, for: 'cli', ...command }); } for (const provider of providers) { this.processProvider(module, resolveToken(provider), provider); } for (const listener of listeners) { if (isClass(listener)) { providers.unshift({ provide: listener }); for (const listenerEntry of this.eventDispatcher.registerListener(listener, module)) { this.processListener(module, listenerEntry); } } else { const listenerObject = { fn: listener.callback, order: listener.order, module: listener.module || module }; this.eventDispatcher.add(listener.eventToken, listenerObject); this.processListener(module, { eventToken: listener.eventToken, listener: listenerObject }); } } for (const imp of module.getImports()) { if (!imp) continue; this.processModule(imp); } } processListener(module, listener) { const addedListener = { eventToken: listener.eventToken, reflection: isEventListenerContainerEntryCallback(listener.listener) ? ReflectionFunction.from(listener.listener.fn) : ReflectionClass.from(listener.listener.classType).getMethod(listener.listener.methodName), module: listener.listener.module, order: listener.listener.order, }; for (const m of this.modules) { m.processListener(module, addedListener); } } processController(module, controller) { let name = controller.name || ''; if (controller.controller) { if (!name) { const cliConfig = cli._fetch(controller.controller); if (cliConfig) { controller.name = name || cliConfig.name || ''; //make sure CLI controllers are provided in cli scope if (!module.isProvided(controller.controller)) { module.addProvider({ provide: controller.controller, scope: 'cli' }); } this.cliControllerRegistry.controllers.push(controller); } } } else if (controller.for === 'cli') { this.cliControllerRegistry.controllers.push(controller); } for (const m of this.modules) { m.processController(module, controller); } } processProvider(module, token, provider) { for (const m of this.modules) { m.processProvider(module, token, provider); } } } ServiceContainer.__type = ['cliControllerRegistry', function () { return new CliControllerRegistry; }, 'middlewareRegistry', function () { return new MiddlewareRegistry; }, 'workflowRegistry', function () { return new WorkflowRegistry([]); }, () => InjectorContext, 'injectorContext', () => EventDispatcher, 'eventDispatcher', () => __ΩConfigLoader, 'configLoaders', function () { return []; }, 'modules', function () { return (Set.Ω = [[() => AppModule, 'P"7!']], new Set()); }, () => AppModule, 'appModule', 'constructor', () => __ΩConfigLoader, 'loader', 'addConfigLoader', 'process', 'postProcess', () => AppModule, 'module', 'findModules', () => InjectorContext, 'getInjectorContext', () => AppModule, 'setupHook', 'bootstrapModules', () => __ΩClassType, 'moduleOrClass', () => Injector, 'getInjector', () => __ΩClassType, () => AppModule, 'moduleClass', () => AppModule, 'getModule', () => AppModule, 'getModules', () => Injector, 'getRootInjector', () => __ΩModuleDefinition, () => AppModule, 'processModule', () => AppModule, () => __ΩEventListenerRegistered, 'listener', 'processListener', () => AppModule, 'ControllerConfig', 'controller', 'processController', () => AppModule, 'Token', 'token', 'ProviderWithScope', 'provider', 'processProvider', 'ServiceContainer', '!3!9>"!3#9>$!3%9>&P7\'3(8<P7)3*<n+F3,<>-!3.<>/PP"7021:"02Pn324"05P"06P"07<PP"7829"0:<PP7;0<PP"7=29"0>;P$0?<PP"o@""J2AP7B0CPP"7EoD"2FP"7G0HPP"7IF0JPP7K0LPPnM7N29$0O<PP"7P29nQ2R"0S<PP"7T29"wU2V"0W<PP"7X29"wY2Z"w[2\\"0]<5w^']; //# sourceMappingURL=service-container.js.map