Skip to content

Commit

Permalink
feat(common): close http/https server connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 2, 2022
1 parent fc87d80 commit 0668c3d
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 127 deletions.
8 changes: 4 additions & 4 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"lcov"
],
"check-coverage": true,
"lines": 99.55,
"statements": 99.54,
"functions": 99.08,
"branches": 86.45
"lines": 99.52,
"statements": 98.48,
"functions": 98.83,
"branches": 86.66
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
]
},
"resolutions": {
"mongoose": "5.12.2"
"mongoose": "6.1.7"
},
"collective": {
"type": "opencollective",
Expand Down
4 changes: 2 additions & 2 deletions packages/orm/mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@tsed/json-mapper": "6.107.5",
"@tsed/logger": ">=6.1.1",
"@tsed/schema": "6.107.5",
"mongoose": "^5.12.2"
"mongoose": "^6.1.7"
},
"peerDependencies": {
"@tsed/ajv": "6.107.5",
Expand All @@ -47,6 +47,6 @@
"@tsed/json-mapper": "6.107.5",
"@tsed/logger": ">=6.1.1",
"@tsed/schema": "6.107.5",
"mongoose": ">=5.12.2"
"mongoose": ">=6.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/orm/testing-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"@tsed/common": "6.107.5",
"@tsed/core": "6.107.5",
"@tsed/mongoose": "6.107.5",
"mongoose": "^5.12.2"
"mongoose": "^6.1.7"
}
}
14 changes: 10 additions & 4 deletions packages/platform/common/src/builder/PlatformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {PlatformStaticsSettings} from "../config/interfaces/PlatformStaticsSetti
import {getStaticsOptions} from "../utils/getStaticsOptions";
import {Route} from "../interfaces/Route";
import {getConfiguration} from "../utils/getConfiguration";
import {IncomingMessage, ServerResponse} from "http";
import type {IncomingMessage, ServerResponse, Server} from "http";
import {PlatformAdapter, PlatformBuilderSettings} from "../interfaces/PlatformAdapter";
import {importProviders} from "../utils/importProviders";
import {createInjector} from "../utils/createInjector";
import {GlobalAcceptMimesMiddleware} from "../middlewares/GlobalAcceptMimesMiddleware";
import {printRoutes} from "../utils/printRoutes";
import {createHttpServer} from "../utils/createHttpServer";
import {createHttpsServer} from "../utils/createHttpsServer";
import type Https from "https";

/**
* @platform
Expand All @@ -30,7 +31,8 @@ export class PlatformBuilder<App = TsED.Application, Router = TsED.Router> {
#rootModule: Type<any>;
#promise: Promise<this>;
#adapter: PlatformAdapter<App, Router>;
#servers: (() => Promise<void>)[];
#servers: (() => Promise<Server | Https.Server>)[];
#listeners: (Server | Https.Server)[] = [];

protected constructor(adapter: Type<PlatformAdapter<App, Router>> | undefined, module: Type, settings: Partial<TsED.Configuration>) {
this.#rootModule = module;
Expand Down Expand Up @@ -227,7 +229,11 @@ export class PlatformBuilder<App = TsED.Application, Router = TsED.Router> {

async stop() {
await this.callHook("$onDestroy");
return this.injector.destroy();
await this.injector.destroy();

this.#listeners.map((server) => {
return new Promise((resolve) => server.close(() => resolve(undefined)));
});
}

public async ready() {
Expand Down Expand Up @@ -345,7 +351,7 @@ export class PlatformBuilder<App = TsED.Application, Router = TsED.Router> {
}

protected async listenServers(): Promise<void> {
await Promise.all(this.#servers.map((cb) => cb && cb()));
this.#listeners = await Promise.all(this.#servers.map((cb) => cb && cb()));
}

protected async logRoutes() {
Expand Down
3 changes: 2 additions & 1 deletion packages/platform/common/src/utils/createHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ export function createHttpServer(injector: InjectorService, requestListener: Htt
injector.invoke(HttpServer);
injector.invoke(Http.Server);

if (httpPort !== false) {
if (server) {
const hostInfo = getHostInfoFromPort("http", httpPort);

return async () => {
const resolvedHostInfo = await listenServer(injector, server as Http.Server, hostInfo);
settings.setRaw("httpPort", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);
return server;
};
}
}
3 changes: 2 additions & 1 deletion packages/platform/common/src/utils/createHttpsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ export function createHttpsServer(injector: InjectorService, requestListener?: H
injector.invoke(HttpsServer);
injector.invoke(Https.Server);

if (httpsPort !== false) {
if (server) {
const hostInfo = getHostInfoFromPort("https", httpsPort);

return async () => {
const resolvedHostInfo = await listenServer(injector, server as Https.Server, hostInfo);
settings.setRaw("httpsPort", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);
return server;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Configuration, createContext, Inject, PlatformApplication, PlatformHandler} from "@tsed/common";
import {Configuration, Inject, PlatformApplication, PlatformHandler} from "@tsed/common";
import Express from "express";
import {PlatformExpressStaticsOptions} from "../interfaces/PlatformExpressStaticsOptions";
import {PlatformExpressRouter} from "./PlatformExpressRouter";
Expand Down
2 changes: 1 addition & 1 deletion packages/security/passport/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 99.36,
branches: 80.7,
branches: 80,
functions: 100,
lines: 99.32
}
Expand Down
4 changes: 2 additions & 2 deletions packages/third-parties/formio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
"express": "^4.17.1",
"formio": "^2.0.0",
"mongodb": "*",
"mongoose": "^5.12.2"
"mongoose": "^6.1.7"
},
"peerDependencies": {
"@tsed/common": "6.107.5",
"express": "^4.17.1",
"formio": "^2.0.0",
"lodash": "^4.17.21",
"mongodb": "*",
"mongoose": "^5.12.2"
"mongoose": "^6.1.7"
},
"peerDependenciesMeta": {
"@tsed/common": {
Expand Down
4 changes: 2 additions & 2 deletions packages/third-parties/socketio/src/SocketIOModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {$log, AfterListen, Constant, InjectorService, Module, OnDestroy, Provider} from "@tsed/common";
import {catchError, nameOf} from "@tsed/core";
import {nameOf} from "@tsed/core";
import {Server, ServerOptions} from "socket.io";
import {SocketProviderMetadata} from "./class/SocketProviderMetadata"; // tslint:disable-line: no-unused-variable
import {PROVIDER_TYPE_SOCKET_SERVICE} from "./constants/constants";
Expand Down Expand Up @@ -58,7 +58,7 @@ export class SocketIOModule implements AfterListen, OnDestroy {
}

$onDestroy() {
catchError(() => this.io.close());
return this.io?.close && this.io?.engine && new Promise((resolve) => this.io.close(() => resolve(undefined)));
}

/**
Expand Down
Loading

0 comments on commit 0668c3d

Please sign in to comment.