@@ -13,9 +13,8 @@ import log, { sharedLoggerOptions } from './shared/log';
1313import packageJson from './shared/packageJson' ;
1414import { buildConfig , Config , getConfig } from './shared/configBuilder' ;
1515import fileExistsAsync from './shared/fileExistsAsync' ;
16- import type { FastifyInstance , FastifyReply , FastifyRequest } from './worker/types' ;
17- import checkProtocolVersion from './worker/checkProtocolVersionHandler' ;
18- import authenticate from './worker/authHandler' ;
16+ import type { FastifyInstance , FastifyReply } from './worker/types' ;
17+ import { performRequestPrechecks } from './worker/requestPrechecks' ;
1918import { handleRenderRequest , type ProvidedNewBundle } from './worker/handleRenderRequest' ;
2019import handleGracefulShutdown from './worker/handleGracefulShutdown' ;
2120import {
@@ -174,42 +173,6 @@ export default function run(config: Partial<Config>) {
174173 done ( null , payload ) ;
175174 } ) ;
176175
177- const isProtocolVersionMatch = async ( req : FastifyRequest , res : FastifyReply ) => {
178- // Check protocol version
179- const protocolVersionCheckingResult = checkProtocolVersion ( req ) ;
180-
181- if ( typeof protocolVersionCheckingResult === 'object' ) {
182- await setResponse ( protocolVersionCheckingResult , res ) ;
183- return false ;
184- }
185-
186- return true ;
187- } ;
188-
189- const isAuthenticated = async ( req : FastifyRequest , res : FastifyReply ) => {
190- // Authenticate Ruby client
191- const authResult = authenticate ( req ) ;
192-
193- if ( typeof authResult === 'object' ) {
194- await setResponse ( authResult , res ) ;
195- return false ;
196- }
197-
198- return true ;
199- } ;
200-
201- const requestPrechecks = async ( req : FastifyRequest , res : FastifyReply ) => {
202- if ( ! ( await isProtocolVersionMatch ( req , res ) ) ) {
203- return false ;
204- }
205-
206- if ( ! ( await isAuthenticated ( req , res ) ) ) {
207- return false ;
208- }
209-
210- return true ;
211- } ;
212-
213176 // See https://github.com/shakacode/react_on_rails_pro/issues/119 for why
214177 // the digest is part of the request URL. Yes, it's not used here, but the
215178 // server logs might show it to distinguish different requests.
@@ -223,7 +186,9 @@ export default function run(config: Partial<Config>) {
223186 // Can't infer from the route like Express can
224187 Params : { bundleTimestamp : string ; renderRequestDigest : string } ;
225188 } > ( '/bundles/:bundleTimestamp/render/:renderRequestDigest' , async ( req , res ) => {
226- if ( ! ( await requestPrechecks ( req , res ) ) ) {
189+ const precheckResult = performRequestPrechecks ( req . body ) ;
190+ if ( precheckResult ) {
191+ await setResponse ( precheckResult , res ) ;
227192 return ;
228193 }
229194
@@ -300,26 +265,11 @@ export default function run(config: Partial<Config>) {
300265 // Build a temporary FastifyRequest shape for protocol/auth check
301266 const tempReqBody = typeof obj === 'object' && obj !== null ? ( obj as Record < string , unknown > ) : { } ;
302267
303- // Protocol check
304- const protoResult = checkProtocolVersion ( {
305- ...req ,
306- body : tempReqBody ,
307- } as unknown as FastifyRequest ) ;
308- if ( typeof protoResult === 'object' ) {
309- return {
310- response : protoResult ,
311- shouldContinue : false ,
312- } ;
313- }
314-
315- // Auth check
316- const authResult = authenticate ( {
317- ...req ,
318- body : tempReqBody ,
319- } as unknown as FastifyRequest ) ;
320- if ( typeof authResult === 'object' ) {
268+ // Perform request prechecks
269+ const precheckResult = performRequestPrechecks ( tempReqBody ) ;
270+ if ( precheckResult ) {
321271 return {
322- response : authResult ,
272+ response : precheckResult ,
323273 shouldContinue : false ,
324274 } ;
325275 }
@@ -401,7 +351,9 @@ export default function run(config: Partial<Config>) {
401351 app . post < {
402352 Body : WithBodyArrayField < Record < string , Asset > , 'targetBundles' > ;
403353 } > ( '/upload-assets' , async ( req , res ) => {
404- if ( ! ( await requestPrechecks ( req , res ) ) ) {
354+ const precheckResult = performRequestPrechecks ( req . body ) ;
355+ if ( precheckResult ) {
356+ await setResponse ( precheckResult , res ) ;
405357 return ;
406358 }
407359 let lockAcquired = false ;
@@ -500,7 +452,9 @@ export default function run(config: Partial<Config>) {
500452 Querystring : { filename : string } ;
501453 Body : WithBodyArrayField < Record < string , unknown > , 'targetBundles' > ;
502454 } > ( '/asset-exists' , async ( req , res ) => {
503- if ( ! ( await isAuthenticated ( req , res ) ) ) {
455+ const precheckResult = performRequestPrechecks ( req . body ) ;
456+ if ( precheckResult ) {
457+ await setResponse ( precheckResult , res ) ;
504458 return ;
505459 }
506460
0 commit comments