|
| 1 | +const demoCookieName = 'rrh_demo_cookie' |
| 2 | +const extraCookieNames = ['rrh_demo_cookie_a', 'rrh_demo_cookie_b'] |
| 3 | + |
| 4 | +type CookieDemoPayload = { |
| 5 | + action: 'read' | 'set' | 'set-many' | 'clear' |
| 6 | + cookieValue: string | null |
| 7 | + cookieHeader: string | null |
| 8 | + cookies: Record<string, string> |
| 9 | + receivedAt: string |
| 10 | +} |
| 11 | + |
| 12 | +function parseCookies(header: string | null): Record<string, string> { |
| 13 | + if (!header) return {} |
| 14 | + |
| 15 | + return Object.fromEntries( |
| 16 | + header |
| 17 | + .split(';') |
| 18 | + .map((part) => part.trim()) |
| 19 | + .filter(Boolean) |
| 20 | + .map((part) => { |
| 21 | + let separatorIndex = part.indexOf('=') |
| 22 | + if (separatorIndex === -1) { |
| 23 | + return [part, ''] |
| 24 | + } |
| 25 | + |
| 26 | + let key = part.slice(0, separatorIndex).trim() |
| 27 | + let value = part.slice(separatorIndex + 1).trim() |
| 28 | + |
| 29 | + try { |
| 30 | + return [key, decodeURIComponent(value)] |
| 31 | + } catch { |
| 32 | + return [key, value] |
| 33 | + } |
| 34 | + }), |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +function serializeCookie(value: string | null) { |
| 39 | + let base = `${demoCookieName}=${value ? encodeURIComponent(value) : ''}; Path=/; HttpOnly; SameSite=Lax` |
| 40 | + return value ? `${base}; Max-Age=${60 * 60 * 24 * 7}` : `${base}; Max-Age=0` |
| 41 | +} |
| 42 | + |
| 43 | +function createPayload(request: Request, action: CookieDemoPayload['action']): CookieDemoPayload { |
| 44 | + let cookieHeader = request.headers.get('cookie') |
| 45 | + let cookies = parseCookies(cookieHeader) |
| 46 | + |
| 47 | + return { |
| 48 | + action, |
| 49 | + cookieValue: cookies[demoCookieName] ?? null, |
| 50 | + cookieHeader, |
| 51 | + cookies, |
| 52 | + receivedAt: new Date().toISOString(), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export async function loader({ request }: { request: Request }) { |
| 57 | + return Response.json(createPayload(request, 'read')) |
| 58 | +} |
| 59 | + |
| 60 | +export async function action({ request }: { request: Request }) { |
| 61 | + let formData = await request.formData() |
| 62 | + let intent = String(formData.get('intent') ?? 'read') as CookieDemoPayload['action'] |
| 63 | + |
| 64 | + if (intent === 'set') { |
| 65 | + let value = String(formData.get('value') ?? '').trim() || 'hello-from-cookie-demo' |
| 66 | + |
| 67 | + return Response.json( |
| 68 | + { |
| 69 | + ...createPayload(request, 'set'), |
| 70 | + cookieValue: value, |
| 71 | + } satisfies CookieDemoPayload, |
| 72 | + { |
| 73 | + headers: { |
| 74 | + 'Set-Cookie': serializeCookie(value), |
| 75 | + }, |
| 76 | + }, |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + if (intent === 'set-many') { |
| 81 | + let value = String(formData.get('value') ?? '').trim() || 'hello-from-cookie-demo' |
| 82 | + let headers = new Headers() |
| 83 | + |
| 84 | + headers.append('Set-Cookie', serializeCookie(value)) |
| 85 | + headers.append( |
| 86 | + 'Set-Cookie', |
| 87 | + serializeCookie(`${value}-a`).replace(demoCookieName, extraCookieNames[0]), |
| 88 | + ) |
| 89 | + headers.append( |
| 90 | + 'Set-Cookie', |
| 91 | + serializeCookie(`${value}-b`).replace(demoCookieName, extraCookieNames[1]), |
| 92 | + ) |
| 93 | + |
| 94 | + return Response.json( |
| 95 | + { |
| 96 | + ...createPayload(request, 'set-many'), |
| 97 | + cookieValue: value, |
| 98 | + } satisfies CookieDemoPayload, |
| 99 | + { |
| 100 | + headers, |
| 101 | + }, |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + if (intent === 'clear') { |
| 106 | + let headers = new Headers() |
| 107 | + headers.append('Set-Cookie', serializeCookie(null)) |
| 108 | + headers.append('Set-Cookie', serializeCookie(null).replace(demoCookieName, extraCookieNames[0])) |
| 109 | + headers.append('Set-Cookie', serializeCookie(null).replace(demoCookieName, extraCookieNames[1])) |
| 110 | + |
| 111 | + return Response.json(createPayload(request, 'clear'), { |
| 112 | + headers, |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + return Response.json(createPayload(request, 'read')) |
| 117 | +} |
0 commit comments