import { Cookies , Directive , FabResponderMutableContext , FABRuntime , FABServerContext , FabSettings , FabSpecMetadata , FabSpecRender , NO_RESPONSE_STATUS_CODE , ResponseInterceptor , RuntimeImports , } from '@fab/core' ; import final_responder from './final_responder' ; import { runtimes } from 'fab-runtime-imports' ; import { fab_metadata } from 'fab-metadata' ; import { production_settings } from 'production-settings' ; function parseCookies ( request ) { const cookies = { } ; const cookies_header = request . headers . get ( 'Cookie' ) ; if ( cookies_header ) { cookies_header . split ( ';' ) . forEach ( ( cookie ) => { const [ key , value ] = cookie . split ( '=' ) . map ( ( s ) => s . trim ( ) ) ; cookies [ key ] = value ; } ) ; } return cookies ; } let Runtime = undefined ; export const initialize = ( server_context ) => { Runtime = FABRuntime . initialize ( fab_metadata , [ ... ( runtimes as RuntimeImports ) , { plugin : final_responder , args : { } , } , ] , server_context ) ; } ; export const render = async ( request , settings ) => { if ( ! Runtime ) { console . log ( `render() called without initialize()` ) ; console . log ( `Injecting a dummy ServerContext` ) ; initialize ( { bundle_id : '' } ) ; if ( ! Runtime ) throw new Error ( 'Initialise called but no Runtime created!' ) ; } const final_interceptor = async ( response ) => response . status === NO_RESPONSE_STATUS_CODE ? new Response ( `No resource found at ${ request . url }\n` , { status : 404 , statusText : 'Not Found' , headers : { } , } ) : response ; const response_interceptors = [ final_interceptor ] ; const context = { } ; let chained_request = request ; let cookies = parseCookies ( request ) ; for ( const responder of Runtime . getPipeline ( ) ) { const request_context = { request : chained_request . clone ( ) , url : new URL ( chained_request . url ) , settings , context : context , cookies , } ; const response = await responder ( request_context ) ; if ( ! response ) continue ; if ( response instanceof Request ) { return response ; } if ( response instanceof Response ) { let response_in_chain = response ; for ( const interceptor of response_interceptors ) { response_in_chain = await interceptor ( response_in_chain ) ; } return response_in_chain ; } const directive = response as Directive ; let valid_directive = false ; if ( typeof directive . interceptResponse === 'function' ) { valid_directive = true ; response_interceptors . unshift ( directive . interceptResponse ) ; } if ( directive . replaceRequest instanceof Request ) { valid_directive = true ; chained_request = directive . replaceRequest ; cookies = parseCookies ( chained_request ) ; } if ( ! valid_directive ) { console . error ( "ERROR: Responder returned object that didn't match any FAB spec" ) ; console . log ( response ) ; throw new Error ( "ERROR: Responder returned object that didn't match any FAB spec" ) ; } } return new Response ( `Error! Expected a plugin to respond!` , { status : 500 , headers : { } , } ) ; } ; export const metadata = { production_settings , fab_version : '0.2' , } ; export const getProdSettings = ( ) => { return metadata . production_settings ; } ;