22 * @prettier
33 */
44import express from 'express' ;
5- import * as path from 'path' ;
5+ import path from 'path' ;
66import debug from 'debug' ;
7- import * as https from 'https' ;
8- import * as http from 'http' ;
7+ import https from 'https' ;
8+ import http from 'http' ;
99import morgan from 'morgan' ;
10- import * as fs from 'fs' ;
10+ import fs from 'fs' ;
1111import timeout from 'connect-timeout' ;
12- import * as bodyParser from 'body-parser' ;
13- import * as _ from 'lodash' ;
12+ import bodyParser from 'body-parser' ;
13+ import _ from 'lodash' ;
1414import { SSL_OP_NO_TLSv1 } from 'constants' ;
15+ import pjson from '../package.json' ;
1516
1617import { Config , config , TlsMode } from './config' ;
1718import * as routes from './routes' ;
1819
1920const debugLogger = debug ( 'enclaved:express' ) ;
20- const pjson = require ( '../package.json' ) ;
2121
2222/**
2323 * Set up the logging middleware provided by morgan
@@ -80,16 +80,17 @@ function isTLS(config: Config): boolean {
8080}
8181
8282async function createHttpsServer ( app : express . Application , config : Config ) : Promise < https . Server > {
83- const { keyPath, crtPath, tlsKey, tlsCert, tlsMode, mtlsRequestCert, mtlsRejectUnauthorized } = config ;
83+ const { keyPath, crtPath, tlsKey, tlsCert, tlsMode, mtlsRequestCert, mtlsRejectUnauthorized } =
84+ config ;
8485 let key : string ;
8586 let cert : string ;
8687 if ( tlsKey && tlsCert ) {
8788 key = tlsKey ;
8889 cert = tlsCert ;
8990 console . log ( 'Using TLS key and cert from environment variables' ) ;
9091 } else if ( keyPath && crtPath ) {
91- const privateKeyPromise = require ( 'fs' ) . promises . readFile ( keyPath , 'utf8' ) ;
92- const certificatePromise = require ( 'fs' ) . promises . readFile ( crtPath , 'utf8' ) ;
92+ const privateKeyPromise = fs . promises . readFile ( keyPath , 'utf8' ) ;
93+ const certificatePromise = fs . promises . readFile ( crtPath , 'utf8' ) ;
9394 [ key , cert ] = await Promise . all ( [ privateKeyPromise , certificatePromise ] ) ;
9495 console . log ( `Using TLS key and cert from files: ${ keyPath } , ${ crtPath } ` ) ;
9596 } else {
@@ -133,7 +134,10 @@ function createHttpServer(app: express.Application): http.Server {
133134 return http . createServer ( app ) ;
134135}
135136
136- export async function createServer ( config : Config , app : express . Application ) : Promise < https . Server | http . Server > {
137+ export async function createServer (
138+ config : Config ,
139+ app : express . Application ,
140+ ) : Promise < https . Server | http . Server > {
137141 const server = isTLS ( config ) ? await createHttpsServer ( app , config ) : createHttpServer ( app ) ;
138142 if ( config . keepAliveTimeout !== undefined ) {
139143 server . keepAliveTimeout = config . keepAliveTimeout ;
@@ -155,7 +159,12 @@ export function createBaseUri(config: Config): string {
155159 * Create error handling middleware
156160 */
157161function errorHandler ( ) {
158- return function ( err : any , req : express . Request , res : express . Response , _next : express . NextFunction ) {
162+ return function (
163+ err : any ,
164+ req : express . Request ,
165+ res : express . Response ,
166+ _next : express . NextFunction ,
167+ ) {
159168 debugLogger ( 'Error: ' + ( err && err . message ? err . message : String ( err ) ) ) ;
160169 const statusCode = err && err . status ? err . status : 500 ;
161170 const result = {
@@ -191,7 +200,11 @@ export function app(cfg: Config): express.Application {
191200 }
192201
193202 // Be more robust about accepting URLs with double slashes
194- app . use ( function replaceUrlSlashes ( req : express . Request , res : express . Response , next : express . NextFunction ) {
203+ app . use ( function replaceUrlSlashes (
204+ req : express . Request ,
205+ res : express . Response ,
206+ next : express . NextFunction ,
207+ ) {
195208 req . url = req . url . replace ( / \/ { 2 , } / g, '/' ) ;
196209 next ( ) ;
197210 } ) ;
0 commit comments