Sha256: bba9605b26caafeaee0d56b8b73fe5f826877dc7588a3fe9f4d65223673e4532
Contents?: true
Size: 1.99 KB
Versions: 26
Compression:
Stored size: 1.99 KB
Contents
import cookieUtils from '@bundled-es-modules/cookie' import { cookieStore } from '../cookieStore' function getAllDocumentCookies() { return cookieUtils.parse(document.cookie) } function getDocumentCookies(request: Request): Record<string, string> { if (typeof document === 'undefined' || typeof location === 'undefined') { return {} } switch (request.credentials) { case 'same-origin': { const requestUrl = new URL(request.url) // Return document cookies only when requested a resource // from the same origin as the current document. return location.origin === requestUrl.origin ? getAllDocumentCookies() : {} } case 'include': { // Return all document cookies. return getAllDocumentCookies() } default: { return {} } } } export function getAllRequestCookies(request: Request): Record<string, string> { /** * @note While the "cookie" header is a forbidden header field * in the browser, you can read it in Node.js. We need to respect * it for mocking in Node.js. */ const requestCookieHeader = request.headers.get('cookie') const cookiesFromHeaders = requestCookieHeader ? cookieUtils.parse(requestCookieHeader) : {} const cookiesFromDocument = getDocumentCookies(request) // Forward the document cookies to the request headers. for (const name in cookiesFromDocument) { request.headers.append( 'cookie', cookieUtils.serialize(name, cookiesFromDocument[name]), ) } const cookiesFromStore = cookieStore.getCookiesSync(request.url) const storedCookiesObject = Object.fromEntries( cookiesFromStore.map((cookie) => [cookie.key, cookie.value]), ) // Forward the raw stored cookies to request headers // so they contain metadata like "expires", "secure", etc. for (const cookie of cookiesFromStore) { request.headers.append('cookie', cookie.toString()) } return { ...cookiesFromDocument, ...storedCookiesObject, ...cookiesFromHeaders, } }
Version data entries
26 entries across 26 versions & 1 rubygems