1+ import { fastifyExpress } from '@fastify/express' ;
2+ import { fastifyRequestContext } from '@fastify/request-context' ;
13import config from 'config' ;
24import fastify from 'fastify' ;
3- import { fastifyExpress } from '@fastify/express ' ;
4- import { AsyncResource } from 'node:async_hooks ' ;
5+ import { Application } from './application/application ' ;
6+ import { context } from './context/context ' ;
57import { errorHandlerFactory } from './errorHandler/factory' ;
68import { pingPluginFactroy } from './routes/pingPluginFactory' ;
79import { renderTemplateHandlerFactory } from './routes/renderTemplateHandlerFactory' ;
@@ -12,94 +14,68 @@ const { Test500Error } = require('./errorHandler/ErrorHandler');
1214
1315const serveStatic = require ( './serveStatic' ) ;
1416const i18n = require ( './i18n' ) ;
15- const Application = require ( './application/application' ) ;
1617const reportingPluginManager = require ( './plugins/reportingPlugin' ) ;
1718const AccessLogger = require ( './logger/accessLogger' ) ;
1819const { isStaticFile, isHealthCheck, isDataUri } = require ( './utils/utils' ) ;
1920
2021/**
2122 * @param {Registry } registryService
2223 */
23- module . exports = async function createApplication ( registryService , pluginManager , context ) {
24+ module . exports = async function createApplication ( registryService , pluginManager ) {
2425 const reportingPlugin = reportingPluginManager . getInstance ( ) ;
2526 const errorHandler = errorHandlerFactory ( ) ;
2627 const appConfig = Application . getConfig ( reportingPlugin ) ;
2728 const logger = reportingPluginManager . getLogger ( ) ;
2829 const accessLogger = new AccessLogger ( config , logger ) ;
2930 const app = fastify ( appConfig ) ;
3031 await app . register ( fastifyExpress ) ;
32+ await app . register ( fastifyRequestContext , {
33+ defaultStoreValues : ( req ) => context . createFromRequest ( req , reportingPluginManager . getRequestId ( ) ?? req . id ) ,
34+ } ) ;
3135
32- const asyncResourceSymbol = Symbol ( 'asyncResource' ) ;
33-
34- app . addHook ( 'onRequest' , ( req , reply , done ) => {
35- context . run ( { request : req , requestId : reportingPluginManager . getRequestId ( ) ?? request . id } , async ( ) => {
36- try {
37- const asyncResource = new AsyncResource ( 'fastify-request-context' ) ;
38- req [ asyncResourceSymbol ] = asyncResource ;
39- const doneWithContext = ( ) => asyncResource . runInAsyncScope ( done , req . raw ) ;
40-
41- const { url, method } = req . raw ;
42- accessLogger . logRequest ( ) ;
43-
44- if ( ! [ 'GET' , 'OPTIONS' , 'HEAD' ] . includes ( method ) ) {
45- logger . warn ( `Request method ${ method } is not allowed for url ${ url } ` ) ;
46- reply . code ( 405 ) . send ( { message : 'Method Not Allowed' } ) ;
47- return ;
48- }
49-
50- if ( isDataUri ( url ) ) {
51- reply . code ( 400 ) . send ( { message : 'Bad Request: Data URIs are not valid HTTP paths' } ) ;
52- return ;
53- }
36+ app . addHook ( 'onRequest' , async ( req , reply ) => {
37+ const { url, method } = req . raw ;
38+ accessLogger . logRequest ( ) ;
5439
55- req . raw . ilcState = { } ;
40+ if ( ! [ 'GET' , 'OPTIONS' , 'HEAD' ] . includes ( method ) ) {
41+ logger . warn ( `Request method ${ method } is not allowed for url ${ url } ` ) ;
42+ reply . code ( 405 ) . send ( { message : 'Method Not Allowed' } ) ;
43+ return ;
44+ }
5645
57- if ( isStaticFile ( url ) || isHealthCheck ( url ) || [ 'OPTIONS' , 'HEAD' ] . includes ( method ) ) {
58- return doneWithContext ( ) ;
59- }
46+ if ( isDataUri ( url ) ) {
47+ reply . code ( 400 ) . send ( { message : 'Bad Request: Data URIs are not valid HTTP paths' } ) ;
48+ return ;
49+ }
6050
61- const domainName = req . hostname ;
51+ req . raw . ilcState = { } ;
6252
63- const registryConfig = await registryService . getConfig ( { filter : { domain : domainName } } ) ;
64- const i18nOnRequest = i18n . onRequestFactory (
65- registryConfig . settings . i18n ,
66- pluginManager . getI18nParamsDetectionPlugin ( ) ,
67- registryConfig . settings . trailingSlash ,
68- ) ;
53+ if ( isStaticFile ( url ) || isHealthCheck ( url ) || [ 'OPTIONS' , 'HEAD' ] . includes ( method ) ) {
54+ return ;
55+ }
6956
70- await i18nOnRequest ( req , reply ) ;
57+ const domainName = req . host ;
7158
72- doneWithContext ( ) ;
73- } catch ( error ) {
74- errorHandler . handleError ( error , req , reply ) ;
75- }
76- } ) ;
77- } ) ;
59+ const registryConfig = await registryService . getConfig ( { filter : { domain : domainName } } ) ;
60+ const i18nOnRequest = i18n . onRequestFactory (
61+ registryConfig . settings . i18n ,
62+ pluginManager . getI18nParamsDetectionPlugin ( ) ,
63+ registryConfig . settings . trailingSlash ,
64+ ) ;
7865
79- /**
80- * Solves issue when async context is lost in webpack-dev-middleware during initial bundle build
81- * Took from here
82- * https://github.com/fastify/fastify-request-context/blob/master/index.js#L46
83- * TODO: after migration to fastify v4 makes sense to use above plugin instead of custom impl
84- */
85- app . addHook ( 'preValidation' , ( req , res , done ) => {
86- const asyncResource = req [ asyncResourceSymbol ] ;
87- asyncResource . runInAsyncScope ( done , req . raw ) ;
66+ await i18nOnRequest ( req , reply ) ;
8867 } ) ;
8968
9069 app . addHook ( 'onResponse' , ( req , reply , done ) => {
91- const asyncResource = req [ asyncResourceSymbol ] ;
92- asyncResource . runInAsyncScope ( ( ) => {
93- try {
94- accessLogger . logResponse ( {
95- statusCode : reply . statusCode ,
96- responseTime : reply . getResponseTime ( ) ,
97- } ) ;
98- done ( ) ;
99- } catch ( error ) {
100- errorHandler . noticeError ( error ) ;
101- }
102- } , req . raw ) ;
70+ try {
71+ accessLogger . logResponse ( {
72+ statusCode : reply . statusCode ,
73+ responseTime : reply . elapsedTime ,
74+ } ) ;
75+ done ( ) ;
76+ } catch ( error ) {
77+ errorHandler . noticeError ( error ) ;
78+ }
10379 } ) ;
10480
10581 if ( config . get ( 'cdnUrl' ) === null ) {
0 commit comments