import { splitCookiesString } from "set-cookie-parser" ; import type { ServerBuild } from "./build" ; import type { ServerRoute } from "./routes" ; import type { RouteMatch } from "./routeMatching" ; export function getDocumentHeaders ( build , matches , routeLoaderResponses , actionResponse ? ) { return matches . reduce ( ( parentHeaders , match , index ) => { let routeModule = build . routes [ match . route . id ] . module ; let loaderHeaders = routeLoaderResponses [ index ] ? routeLoaderResponses [ index ] . headers : new Headers ( ) ; let actionHeaders = actionResponse ? actionResponse . headers : new Headers ( ) ; let headers = new Headers ( routeModule . headers ? typeof routeModule . headers === "function" ? routeModule . headers ( { loaderHeaders , parentHeaders , actionHeaders } ) : routeModule . headers : undefined ) ; prependCookies ( actionHeaders , headers ) ; prependCookies ( loaderHeaders , headers ) ; prependCookies ( parentHeaders , headers ) ; return headers ; } , new Headers ( ) ) ; } function prependCookies ( parentHeaders , childHeaders ) { let parentSetCookieString = parentHeaders . get ( "Set-Cookie" ) ; if ( parentSetCookieString ) { let cookies = splitCookiesString ( parentSetCookieString ) ; cookies . forEach ( cookie => { childHeaders . append ( "Set-Cookie" , cookie ) ; } ) ; } }