Skip to content

Commit

Permalink
feat: rewrite to TypeScript
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Flow to TypeScript migration.
  • Loading branch information
gajus committed Feb 9, 2021
1 parent 433a587 commit a1cba71
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 176 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"extends": [
"canonical",
"canonical/flowtype"
"canonical/typescript"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"root": true
}
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
language: node_js
node_js:
- node
- 10
env:
- NODE_TLS_REJECT_UNAUTHORIZED=0
script:
- npm run lint
- npm run test
- nyc --silent npm run test
- nyc report --reporter=text-lcov | coveralls
- nyc check-coverage --lines 60
- nyc check-coverage --lines 30
after_success:
- NODE_ENV=production npm run build
- semantic-release
Expand Down
78 changes: 28 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,40 @@
"url": "http://gajus.com"
},
"ava": {
"babel": {
"compileAsTests": [
"test/helpers/**/*"
]
},
"extensions": [
"ts"
],
"files": [
"test/http-terminator/**/*"
],
"require": [
"@babel/register"
"ts-node/register/transpile-only"
]
},
"dependencies": {
"delay": "^4.3.0",
"roarr": "^2.14.6"
"delay": "^5.0.0",
"roarr": "^4.0.8",
"type-fest": "^0.20.2"
},
"description": "Gracefully terminates HTTP(S) server.",
"devDependencies": {
"@ava/babel": "^1.0.0",
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/node": "^7.8.3",
"@babel/plugin-transform-flow-strip-types": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@babel/register": "^7.8.3",
"agentkeepalive": "^4.1.0",
"ava": "^3.0.0",
"agentkeepalive": "^4.1.4",
"ava": "^3.15.0",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-transform-export-default-name": "^2.0.4",
"coveralls": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-canonical": "^18.1.0",
"flow-bin": "^0.116.1",
"flow-copy-source": "^2.0.9",
"gitdown": "^3.1.2",
"got": "^10.2.2",
"husky": "^4.0.10",
"nyc": "^15.0.0",
"pem": "^1.14.3",
"semantic-release": "^16.0.2",
"sinon": "^8.1.0"
"babel-plugin-transform-export-default-name": "^2.1.0",
"coveralls": "^3.1.0",
"del-cli": "^3.0.1",
"eslint": "^7.19.0",
"eslint-config-canonical": "^25.8.15",
"gitdown": "^3.1.3",
"got": "^11.8.1",
"husky": "^4.0.0",
"nyc": "^15.1.0",
"pem": "^1.14.4",
"semantic-release": "^17.3.8",
"sinon": "^9.2.4",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
"engines": {
"node": ">=10"
Expand All @@ -60,33 +53,18 @@
"prometheus"
],
"license": "BSD-3-Clause",
"main": "./dist/index.js",
"main": "./dist/src/index.js",
"name": "http-terminator",
"nyc": {
"all": true,
"include": [
"src/**/*.js"
],
"instrument": false,
"reporter": [
"html",
"text-summary"
],
"require": [
"@babel/register"
],
"silent": true,
"sourceMap": false
},
"repository": {
"type": "git",
"url": "https://github.com/gajus/http-terminator"
},
"scripts": {
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps && flow-copy-source src dist",
"build": "del-cli ./dist && tsc",
"generate-readme": "gitdown ./.README/README.md --output-file ./README.md",
"lint": "eslint ./src ./test && flow",
"test": "NODE_ENV=test ava --verbose --serial"
"lint": "eslint ./src ./test && tsc --noEmit",
"test": "NODE_ENV=test ava --serial --verbose"
},
"typings": "./dist/src/index.d.ts",
"version": "1.0.0"
}
2 changes: 0 additions & 2 deletions src/Logger.js → src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import Roarr from 'roarr';

export default Roarr.child({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow

import type {
HttpTerminatorType,
HttpTerminatorConfigurationInputType,
HttpTerminator,
HttpTerminatorConfigurationInput,
} from '../types';
import createInternalHttpTerminator from './createInternalHttpTerminator';

export default (configurationInput: HttpTerminatorConfigurationInputType): HttpTerminatorType => {
export default (
configurationInput: HttpTerminatorConfigurationInput,
): HttpTerminator => {
const httpTerminator = createInternalHttpTerminator(configurationInput);

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
// @flow

import http from 'http';
import type {
Socket,
} from 'net';
import type {
TLSSocket,
} from 'tls';
import delay from 'delay';
import Logger from '../Logger';
import type {
HttpTerminatorConfigurationInputType,
InternalHttpTerminatorType,
HttpTerminatorConfigurationInput,
InternalHttpTerminator,
} from '../types';
import Logger from '../Logger';

const log = Logger.child({
namespace: 'createHttpTerminator',
});

const configurationDefaults = {
gracefulTerminationTimeout: 1000,
gracefulTerminationTimeout: 1_000,
};

export default (configurationInput: HttpTerminatorConfigurationInputType): InternalHttpTerminatorType => {
export default (
configurationInput: HttpTerminatorConfigurationInput,
): InternalHttpTerminator => {
const configuration = {
...configurationDefaults,
...configurationInput,
};

const server = configuration.server;

const sockets = new Set();
const secureSockets = new Set();
const sockets = new Set<Socket>();
const secureSockets = new Set<TLSSocket>();

let terminating;

Expand Down Expand Up @@ -87,11 +93,12 @@ export default (configurationInput: HttpTerminatorConfigurationInputType): Inter

for (const socket of sockets) {
// This is the HTTP CONNECT request socket.
// @ts-expect-error Unclear if I am using wrong type or how else this should be handled.
if (!(socket.server instanceof http.Server)) {
continue;
}

// $FlowFixMe
// @ts-expect-error Unclear if I am using wrong type or how else this should be handled.
const serverResponse = socket._httpMessage;

if (serverResponse) {
Expand All @@ -106,7 +113,7 @@ export default (configurationInput: HttpTerminatorConfigurationInputType): Inter
}

for (const socket of secureSockets) {
// $FlowFixMe
// @ts-expect-error Unclear if I am using wrong type or how else this should be handled.
const serverResponse = socket._httpMessage;

if (serverResponse) {
Expand Down
6 changes: 0 additions & 6 deletions src/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
default as createHttpTerminator,
} from './factories/createHttpTerminator';
export type {
HttpTerminator,
} from './types';
33 changes: 0 additions & 33 deletions src/types.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type {
Server as HttpServer,
} from 'http';
import type {
Server as HttpsServer,
} from 'https';
import type {
Socket,
} from 'net';
import type {
Merge,
} from 'type-fest';

/**
* @property gracefulTerminationTimeout Number of milliseconds to allow for the active sockets to complete serving the response (default: 5000).
* @property server Instance of http.Server.
*/
export type HttpTerminatorConfigurationInput = {
readonly gracefulTerminationTimeout?: number,
readonly server: HttpServer | HttpsServer,
};

/**
* @property terminate Terminates HTTP server.
*/
export type HttpTerminator = {
readonly terminate: () => Promise<void>,
};

export type InternalHttpTerminator = Merge<HttpTerminator, {
readonly secureSockets: Set<Socket>,
readonly sockets: Set<Socket>,
}>;
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
// @flow

import {
createServer,
import type {
Server,
IncomingMessage as HttpIncomingMessage,
ServerResponse as HttpServerResponse,
} from 'http';
import {
createServer,
} from 'http';
import {
promisify,
} from 'util';

type RequestHandlerType = (incomingMessage: HttpIncomingMessage, outgoingMessage: HttpServerResponse) => void;
type RequestHandler = (incomingMessage: HttpIncomingMessage, outgoingMessage: HttpServerResponse) => void;

type HttpServerType = {|
+getConnections: () => Promise<number>,
+port: number,
+server: Server,
+stop: () => Promise<void>,
+url: string,
|};
type HttpServerType = {
readonly getConnections: () => Promise<number>,
readonly port: number,
readonly server: Server,
readonly stop: () => Promise<void>,
readonly url: string,
};

export type HttpServerFactoryType = (requestHandler: RequestHandlerType) => Promise<HttpServerType>;
export type HttpServerFactory = (requestHandler: RequestHandler) => Promise<HttpServerType>;

export default (requestHandler: RequestHandlerType): Promise<HttpServerType> => {
export default (
requestHandler: RequestHandler,
): Promise<HttpServerType> => {
const server = createServer(requestHandler);

let serverShutingDown;
Expand All @@ -45,6 +47,7 @@ export default (requestHandler: RequestHandlerType): Promise<HttpServerType> =>
server.once('error', reject);

server.listen(() => {
// @ts-expect-error-error address should be always available inside the `.listen()` block.
const port = server.address().port;
const url = 'http://localhost:' + port;

Expand Down
Loading

0 comments on commit a1cba71

Please sign in to comment.