Skip to content

Commit

Permalink
Merge feat-optimize-boostrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Nov 19, 2021
2 parents 669a161 + 4a96382 commit f4d761c
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 67 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: benchmarks
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
Expand Down
1 change: 1 addition & 0 deletions packages/di/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./utils/createContainer";
export * from "./utils/getConfiguration";
export * from "./utils/getConfiguration";
export * from "./utils/setLoggerLevel";
export * from "./utils/colors";
46 changes: 23 additions & 23 deletions packages/platform/common/src/builder/PlatformBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {nameOf, toMap, Type} from "@tsed/core";
import {Container, createContainer, getConfiguration, InjectorService, IProvider, ProviderScope, setLoggerLevel} from "@tsed/di";
import {deepMerge, Env, nameOf, Store, toMap, Type} from "@tsed/core";
import {colors, Container, createContainer, InjectorService, IProvider, ProviderScope, ProviderType, setLoggerLevel} from "@tsed/di";
import {importProviders} from "@tsed/components-scan";
import {PerfLogger} from "@tsed/perf";
import {getMiddlewaresForHook} from "@tsed/platform-middlewares";
import {GlobalAcceptMimesMiddleware} from "../middlewares";
import {Platform} from "../services/Platform";
Expand All @@ -22,6 +21,7 @@ import {
import {PlatformStaticsSettings} from "../config/interfaces/PlatformStaticsSettings";
import {getStaticsOptions} from "../utils/getStaticsOptions";
import {Route} from "../interfaces/Route";
import {getConfiguration} from "../utils/getConfiguration";

/**
* @ignore
Expand All @@ -30,8 +30,6 @@ export interface PlatformType<T = any> extends Type<T> {
providers: IProvider[];
}

const {bind, start, end, log} = PerfLogger.get("bootstrap");

/**
* @ignore
*/
Expand All @@ -46,6 +44,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
static currentPlatform: Type<PlatformBuilder<any, any>> & PlatformBootstrap;
readonly name: string = "";
protected startedAt = new Date();
protected current = new Date();
protected locals: Container;

#injector: InjectorService;
Expand Down Expand Up @@ -123,7 +122,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
}

log(...data: any[]) {
return this.disableBootstrapLog && this.logger.info(...data);
return !this.disableBootstrapLog && this.logger.info(...data, this.diff());
}

/**
Expand Down Expand Up @@ -171,6 +170,8 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout

await this.loadRoutes();
await this.logRoutes();

return this;
}

async loadInjector() {
Expand All @@ -186,7 +187,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout

await injector.load(container);

this.log("Settings and injector loaded");
this.log("Settings and injector loaded...");

await this.callHook("$afterInit");
}
Expand All @@ -199,7 +200,6 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
await this.callHook("$afterListen");

await this.ready();
end();
}

async stop() {
Expand All @@ -218,17 +218,16 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout

async callHook(hook: string, ...args: any[]) {
const {injector} = this;
log(hook);

if (!this.disableBootstrapLog) {
injector.logger.info(`\x1B[1mCall hook ${hook}\x1B[22m`);
injector.logger.debug(`\x1B[1mCall hook ${hook}\x1B[22m`);
}

// Load middlewares for the given hook
this.loadMiddlewaresFor(hook);

// call hooks added by providers
await injector.emit(hook);
await injector.emit(hook, ...args);
}

async loadStatics(hook: string): Promise<void> {
Expand All @@ -247,13 +246,20 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
return this;
}

protected diff() {
const ms = colors.yellow(`+${new Date().getTime() - this.current.getTime()}ms`);
this.current = new Date();
return ms;
}

protected async importProviders() {
this.injector.logger.debug("Scan components");

const providers = await importProviders(this.injector.settings, ["imports", "mount", "componentsScan"]);
const routes = providers.filter((provider) => !!provider.route).map(({route, token}) => ({route, token}));

this.settings.set("routes", routes);
this.log("Providers loaded...");
}

/**
Expand All @@ -276,16 +282,6 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
}

protected async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
// istanbul ignore next
if (settings.logger?.perf) {
start();
bind(this);
settings.logger = {
...settings.logger,
level: "off"
};
}

this.createInjector(module, {
...settings,
PLATFORM_NAME: this.name
Expand All @@ -303,8 +299,9 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
protected async logRoutes() {
const {logger, platform} = this;

this.log("Routes mounted...");

if (!this.settings.logger?.disableRoutesSummary && !this.disableBootstrapLog) {
logger.info("Routes mounted :");
logger.info(printRoutes(await this.injector.alterAsync("$logRoutes", platform.getRoutes())));
}
}
Expand All @@ -321,6 +318,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
}

this.log("Load routes");

await this.loadStatics("$beforeRoutesInit");
await this.callHook("$beforeRoutesInit");

Expand All @@ -338,7 +336,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
protected createInjector(module: Type<any>, settings: any) {
this.#rootModule = module;

const configuration = getConfiguration(module, settings);
const configuration = getConfiguration(settings, module);

this.#injector = createInjector(configuration);

Expand All @@ -350,5 +348,7 @@ export abstract class PlatformBuilder<App = TsED.Application, Router = TsED.Rout
createPlatformApplication(this.injector);
createHttpsServer(this.injector);
createHttpServer(this.injector);

this.log("Injector created...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,6 @@ describe("PlatformConfiguration", () => {
});
});

describe("Test PRODUCTION", () => {
before(() => {
process.env.NODE_ENV = "production";
settings = new PlatformConfiguration();
});

it("should return env PROD", () => {
expect(settings.env).to.equal(Env.PROD);
});

it("should have logging jsonIndentaion set to 0", () => {
expect(settings.logger.jsonIndentation).to.equal(0);
});
});

describe("set logger format", () => {
before(() => {
settings = new PlatformConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,7 @@ const rootDir = process.cwd();
})
export class PlatformConfiguration extends DIConfiguration {
constructor() {
super({
rootDir,
env: (process.env.NODE_ENV as Env) || Env.DEV,
httpPort: 8080,
httpsPort: false,
scopes: {
[ProviderType.CONTROLLER]: ProviderScope.SINGLETON
},
logger: {
debug: false,
level: "info",
logRequest: true,
jsonIndentation: process.env.NODE_ENV === Env.PROD ? 0 : 2
},
errors: {
headerName: "errors"
},
mount: {
"/rest": "${rootDir}/controllers/**/*.ts"
},
exclude: ["**/*.spec.ts", "**/*.spec.js"],
componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"]
});
super({rootDir});
}

get version() {
Expand Down
3 changes: 2 additions & 1 deletion packages/platform/common/src/services/PlatformTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {PlatformContext, PlatformContextOptions} from "../domain/PlatformContext
import {createInjector} from "../utils/createInjector";
import {PlatformApplication} from "./PlatformApplication";
import {PlatformViews} from "@tsed/platform-views";
import {getConfiguration} from "../utils/getConfiguration";

/**
* @platform
Expand All @@ -13,7 +14,7 @@ export class PlatformTest extends DITest {
public static platformBuilder: Type<PlatformBuilder<any, any>>;

static async create(options: Partial<TsED.Configuration> = {}) {
DITest.injector = PlatformTest.createInjector(options);
DITest.injector = PlatformTest.createInjector(getConfiguration(options, null));
const container = createContainer();

await DITest.injector.load(container);
Expand Down
131 changes: 131 additions & 0 deletions packages/platform/common/src/utils/getConfiguration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {Configuration} from "@tsed/di";
import {expect} from "chai";
import {getConfiguration} from "./getConfiguration";
import {Env} from "@tsed/core";

describe("getConfiguration()", () => {
it("should load configuration for test", () => {
@Configuration({})
class App {}

const config = getConfiguration({}, App);

expect(config).to.deep.eq({
componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"],
env: "test",
exclude: ["**/*.spec.ts", "**/*.spec.js"],
httpPort: 8080,
httpsPort: false,
logger: {
debug: false,
jsonIndentation: 2,
level: "off",
logRequest: true
},
mount: {
"/rest": "${rootDir}/controllers/**/*.ts"
},
scopes: {
controller: "singleton"
}
});
});
it("should load configuration for production", () => {
@Configuration({
env: Env.PROD
})
class App {}

const config = getConfiguration({}, App);

expect(config).to.deep.eq({
componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"],
env: Env.PROD,
exclude: ["**/*.spec.ts", "**/*.spec.js"],
httpPort: 8080,
httpsPort: false,
logger: {
debug: false,
jsonIndentation: 2,
level: "off",
logRequest: true
},
mount: {
"/rest": "${rootDir}/controllers/**/*.ts"
},
scopes: {
controller: "singleton"
}
});
});
it("should load configuration with mount from config", () => {
@Configuration({
env: Env.PROD
})
class App {}

class Ctrl {}

const config = getConfiguration(
{
mount: {
"/": [Ctrl]
}
},
App
);

expect(config).to.deep.eq({
componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"],
env: Env.PROD,
exclude: ["**/*.spec.ts", "**/*.spec.js"],
httpPort: 8080,
httpsPort: false,
logger: {
debug: false,
jsonIndentation: 2,
level: "off",
logRequest: true
},
mount: {
"/": [Ctrl]
},
scopes: {
controller: "singleton"
}
});
});
it("should load configuration with mount from decorator", () => {
class Ctrl {}

@Configuration({
env: Env.PROD,
mount: {
"/": [Ctrl]
}
})
class App {}

const config = getConfiguration({}, App);

expect(config).to.deep.eq({
componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"],
env: Env.PROD,
exclude: ["**/*.spec.ts", "**/*.spec.js"],
httpPort: 8080,
httpsPort: false,
logger: {
debug: false,
jsonIndentation: 2,
level: "info",
logRequest: true
},
mount: {
"/": [Ctrl]
},
scopes: {
controller: "singleton"
}
});
});
});

0 comments on commit f4d761c

Please sign in to comment.