1- import type {
2- RequestHandler ,
3- Request ,
4- Response ,
5- ErrorRequestHandler ,
6- } from "express" ;
7- import { getClientIp } from "request-ip" ;
8- import type { Histogram } from "@opentelemetry/api" ;
9- import cleanStack from "clean-stack" ;
1+ import type { RequestHandler , Request , Response , ErrorRequestHandler } from 'express' ;
2+ import { getClientIp } from 'request-ip' ;
3+ import type { Histogram } from '@opentelemetry/api' ;
4+ import cleanStack from 'clean-stack' ;
105
11- import { ServiceError } from "../error.js" ;
12- import type {
13- AnyServiceLocals ,
14- RequestWithApp ,
15- ServiceExpress ,
16- ServiceLocals ,
17- } from "../types.js" ;
18- import type { ServiceHandler } from "../express-app/types.js" ;
19- import type { ConfigurationSchema } from "../config/schema.js" ;
20- import { getNodeEnv } from "../env.js" ;
6+ import { ServiceError } from '../error.js' ;
7+ import type { AnyServiceLocals , RequestWithApp , ServiceExpress , ServiceLocals } from '../types.js' ;
8+ import type { ServiceHandler } from '../express-app/types.js' ;
9+ import type { ConfigurationSchema } from '../config/schema.js' ;
10+ import { getNodeEnv } from '../env.js' ;
2111
22- const LOG_PREFS = Symbol ( " Logging information" ) ;
23- const LOGGED_SEMAPHORE = Symbol ( " Logged semaphore" ) ;
24- const SUCCESS_SEMAPHORE = Symbol ( " Response finished semaphore" ) ;
12+ const LOG_PREFS = Symbol ( ' Logging information' ) ;
13+ const LOGGED_SEMAPHORE = Symbol ( ' Logged semaphore' ) ;
14+ const SUCCESS_SEMAPHORE = Symbol ( ' Response finished semaphore' ) ;
2515
2616interface LogPrefs {
2717 start : [ number , number ] ;
@@ -49,12 +39,12 @@ function getBasicInfo(req: Request): [string, Record<string, string | number>] {
4939 const url = req . originalUrl || req . url ;
5040
5141 const preInfo : Record < string , string > = {
52- ip : getClientIp ( req ) || "" ,
42+ ip : getClientIp ( req ) || '' ,
5343 m : req . method ,
5444 } ;
5545
56- if ( req . headers [ " user-agent" ] ) {
57- preInfo . ua = req . headers [ " user-agent" ] ;
46+ if ( req . headers [ ' user-agent' ] ) {
47+ preInfo . ua = req . headers [ ' user-agent' ] ;
5848 }
5949
6050 const sessionReq = req as WithIdentifiedSession ;
@@ -65,9 +55,7 @@ function getBasicInfo(req: Request): [string, Record<string, string | number>] {
6555 return [ url , preInfo ] ;
6656}
6757
68- function finishLog <
69- SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
70- > (
58+ function finishLog < SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > > (
7159 app : ServiceExpress < SLocals > ,
7260 error : Error | undefined ,
7361 req : Request ,
@@ -93,22 +81,22 @@ function finishLog<
9381 }
9482 const [ url , preInfo ] = getBasicInfo ( req ) ;
9583
96- let responseType = " unknown" ;
84+ let responseType = ' unknown' ;
9785
9886 // ts warning is known and incorrect—`aborted` is a subset of `destroyed`
9987 if ( res [ SUCCESS_SEMAPHORE ] ) {
100- responseType = " finished" ;
88+ responseType = ' finished' ;
10189 } else if ( req . aborted ) {
102- responseType = " aborted" ;
90+ responseType = ' aborted' ;
10391 } else if ( req . destroyed ) {
104- responseType = " destroyed" ;
92+ responseType = ' destroyed' ;
10593 } else if ( error ) {
106- responseType = " errored" ;
94+ responseType = ' errored' ;
10795 }
10896
10997 const endLog : Record < string , string | string [ ] | number | undefined > = {
11098 ...preInfo ,
111- t : " req" ,
99+ t : ' req' ,
112100 r : responseType ,
113101 s : ( error as ErrorWithStatus ) ?. status || res . statusCode || 0 ,
114102 dur,
@@ -140,22 +128,21 @@ function finishLog<
140128 if ( prefs . logRequests ) {
141129 endLog . h = JSON . stringify ( req . headers ) ;
142130 if ( Buffer . isBuffer ( req . body ) ) {
143- endLog . b = req . body . toString ( " base64" ) ;
144- } else if ( typeof req . body !== " string" ) {
131+ endLog . b = req . body . toString ( ' base64' ) ;
132+ } else if ( typeof req . body !== ' string' ) {
145133 endLog . b = JSON . stringify ( req . body ) ;
146134 } else if ( req . body ) {
147135 endLog . b = req . body ;
148136 }
149137 }
150138
151139 if ( prefs . chunks ?. length ) {
152- const bodyString = Buffer . concat ( prefs . chunks ) . toString ( " utf8" ) ;
140+ const bodyString = Buffer . concat ( prefs . chunks ) . toString ( ' utf8' ) ;
153141 if ( bodyString ) {
154142 endLog . resBody = bodyString ;
155143 }
156144 }
157- const msg =
158- service . getLogFields ?.( req as RequestWithApp < SLocals > , endLog ) || url ;
145+ const msg = service . getLogFields ?.( req as RequestWithApp < SLocals > , endLog ) || url ;
159146 if ( unexpectedError ) {
160147 logger . error ( endLog , msg ) ;
161148 } else {
@@ -170,17 +157,14 @@ export function loggerMiddleware<
170157> (
171158 app : ServiceExpress < SLocals > ,
172159 histogram : Histogram ,
173- config ?: ConfigurationSchema [ " logging" ] ,
160+ config ?: ConfigurationSchema [ ' logging' ] ,
174161) : RequestHandler {
175- const nonProd = getNodeEnv ( ) !== " production" ;
162+ const nonProd = getNodeEnv ( ) !== ' production' ;
176163 const { logger, service } = app . locals ;
177164 return function serviceLogger ( req , res , next ) {
178165 const logResponse =
179- config ?. logResponseBody ||
180- ( nonProd && req . headers [ "x-log" ] ?. includes ( "res" ) ) ;
181- const logRequest =
182- config ?. logRequestBody ||
183- ( nonProd && req . headers [ "x-log" ] ?. includes ( "req" ) ) ;
166+ config ?. logResponseBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'res' ) ) ;
167+ const logRequest = config ?. logRequestBody || ( nonProd && req . headers [ 'x-log' ] ?. includes ( 'req' ) ) ;
184168 const prefs : LogPrefs = {
185169 start : process . hrtime ( ) ,
186170 logRequests : logRequest ,
@@ -195,57 +179,47 @@ export function loggerMiddleware<
195179 // data is to monkey-patch.
196180 const oldWrite = res . write ;
197181 const oldEnd = res . end ;
198- res . write = ( ( ...args : Parameters < ( typeof res ) [ " write" ] > ) => {
182+ res . write = ( ( ...args : Parameters < ( typeof res ) [ ' write' ] > ) => {
199183 if ( prefs . chunks ) {
200- prefs . chunks . push (
201- Buffer . isBuffer ( args [ 0 ] ) ? args [ 0 ] : Buffer . from ( args [ 0 ] as string ) ,
202- ) ;
184+ prefs . chunks . push ( Buffer . isBuffer ( args [ 0 ] ) ? args [ 0 ] : Buffer . from ( args [ 0 ] as string ) ) ;
203185 }
204- return ( oldWrite as ( typeof res ) [ " write" ] ) . apply ( res , args ) ;
205- } ) as ( typeof res ) [ " write" ] ;
206- res . end = ( ( ...args : Parameters < ( typeof res ) [ " end" ] > ) => {
186+ return ( oldWrite as ( typeof res ) [ ' write' ] ) . apply ( res , args ) ;
187+ } ) as ( typeof res ) [ ' write' ] ;
188+ res . end = ( ( ...args : Parameters < ( typeof res ) [ ' end' ] > ) => {
207189 if ( args [ 0 ] && prefs . chunks ) {
208- prefs . chunks . push (
209- Buffer . isBuffer ( args [ 0 ] ) ? args [ 0 ] : Buffer . from ( args [ 0 ] as string ) ,
210- ) ;
190+ prefs . chunks . push ( Buffer . isBuffer ( args [ 0 ] ) ? args [ 0 ] : Buffer . from ( args [ 0 ] as string ) ) ;
211191 }
212192 return oldEnd . apply ( res , args ) ;
213- } ) as ( typeof res ) [ " end" ] ;
193+ } ) as ( typeof res ) [ ' end' ] ;
214194 }
215195
216196 if ( config ?. preLog ) {
217197 const [ url , preInfo ] = getBasicInfo ( req ) ;
218198 const preLog : Record < string , string | string [ ] | number | undefined > = {
219199 ...preInfo ,
220- t : " pre" ,
200+ t : ' pre' ,
221201 ref : req . headers . referer || undefined ,
222202 sid : ( req as WithIdentifiedSession ) . session ?. id ,
223203 c : req . headers . correlationid || undefined ,
224204 } ;
225- const msg =
226- service . getLogFields ?.( req as RequestWithApp < SLocals > , preLog ) || url ;
205+ const msg = service . getLogFields ?.( req as RequestWithApp < SLocals > , preLog ) || url ;
227206 logger . info ( preLog , msg ) ;
228207 }
229208
230209 const logWriter = ( err ?: Error ) => finishLog ( app , err , req , res , histogram ) ;
231- res . on ( " finish" , ( err ?: Error ) => {
210+ res . on ( ' finish' , ( ) => {
232211 res [ SUCCESS_SEMAPHORE ] = true ;
233- logWriter ( err ) ;
212+ logWriter ( ) ;
234213 } ) ;
235- res . on ( " close" , logWriter ) ;
236- res . on ( " error" , logWriter ) ;
214+ res . on ( ' close' , logWriter ) ;
215+ res . on ( ' error' , logWriter ) ;
237216 next ( ) ;
238217 } ;
239218}
240219
241220export function errorHandlerMiddleware <
242221 SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
243- > (
244- app : ServiceExpress < SLocals > ,
245- histogram : Histogram ,
246- unnest ?: boolean ,
247- returnError ?: boolean ,
248- ) {
222+ > ( app : ServiceExpress < SLocals > , histogram : Histogram , unnest ?: boolean , returnError ?: boolean ) {
249223 const svcErrorHandler : ErrorRequestHandler = ( error , req , res , next ) => {
250224 let loggable : Partial < ServiceError > = error ;
251225 const body = error . response ?. body || error . body ;
@@ -281,15 +255,11 @@ export function errorHandlerMiddleware<
281255
282256export function notFoundMiddleware ( ) {
283257 const serviceNotFoundHandler : ServiceHandler = ( req , res , next ) => {
284- const error = new ServiceError (
285- req . app ,
286- `Cannot ${ req . method } ${ req . path } ` ,
287- {
288- status : 404 ,
289- code : "NotFound" ,
290- domain : "http" ,
291- } ,
292- ) ;
258+ const error = new ServiceError ( req . app , `Cannot ${ req . method } ${ req . path } ` , {
259+ status : 404 ,
260+ code : 'NotFound' ,
261+ domain : 'http' ,
262+ } ) ;
293263 next ( error ) ;
294264 } ;
295265 return serviceNotFoundHandler as RequestHandler ;
0 commit comments