Skip to content
Myr

index.ts

This file

Path

src/helper/cookie/index.ts

145 lines · 4.2 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–145why, one step back
1/**
2 * @module3 * Cookie Helper for Hono.4 */56import type { Context } from '../../context'7import { parse, parseSigned, serialize, serializeSigned } from '../../utils/cookie'8import type { Cookie, CookieOptions, CookiePrefixOptions, SignedCookie } from '../../utils/cookie'910interface GetCookie {11 (c: Context, key: string): string | undefined12 (c: Context): Cookie13 (c: Context, key: string, prefixOptions?: CookiePrefixOptions): string | undefined14}1516interface GetSignedCookie {17 (c: Context, secret: string | BufferSource, key: string): Promise<string | undefined | false>18 (c: Context, secret: string | BufferSource): Promise<SignedCookie>19 (20 c: Context,21 secret: string | BufferSource,22 key: string,23 prefixOptions?: CookiePrefixOptions24 ): Promise<string | undefined | false>25}2627export const getCookie: GetCookie = (c, key?, prefix?: CookiePrefixOptions) => {28 const cookie = c.req.raw.headers.get('Cookie')29 if (typeof key === 'string') {30 if (!cookie) {31 return undefined32 }33 let finalKey = key34 if (prefix === 'secure') {35 finalKey = '__Secure-' + key36 } else if (prefix === 'host') {37 finalKey = '__Host-' + key38 }39 const obj = parse(cookie, finalKey)40 return obj[finalKey]41 }42 if (!cookie) {43 return {}44 }45 const obj = parse(cookie)46 // eslint-disable-next-line @typescript-eslint/no-explicit-any47 return obj as any48}4950export const getSignedCookie: GetSignedCookie = async (51 c,52 secret,53 key?,54 prefix?: CookiePrefixOptions55) => {56 const cookie = c.req.raw.headers.get('Cookie')57 if (typeof key === 'string') {58 if (!cookie) {59 return undefined60 }61 let finalKey = key62 if (prefix === 'secure') {63 finalKey = '__Secure-' + key64 } else if (prefix === 'host') {65 finalKey = '__Host-' + key66 }67 const obj = await parseSigned(cookie, secret, finalKey)68 return obj[finalKey]69 }70 if (!cookie) {71 return {}72 }73 const obj = await parseSigned(cookie, secret)74 // eslint-disable-next-line @typescript-eslint/no-explicit-any75 return obj as any76}7778export const generateCookie = (name: string, value: string, opt?: CookieOptions): string => {79 // Cookie names prefixed with __Secure- can be used only if they are set with the secure attribute.80 // Cookie names prefixed with __Host- can be used only if they are set with the secure attribute, must have a path of / (meaning any path at the host)81 // and must not have a Domain attribute.82 // Read more at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#cookie_prefixes'83 let cookie84 if (opt?.prefix === 'secure') {85 cookie = serialize('__Secure-' + name, value, { path: '/', ...opt, secure: true })86 } else if (opt?.prefix === 'host') {87 cookie = serialize('__Host-' + name, value, {88 ...opt,89 path: '/',90 secure: true,91 domain: undefined,92 })93 } else {94 cookie = serialize(name, value, { path: '/', ...opt })95 }96 return cookie97}9899export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => {100 const cookie = generateCookie(name, value, opt)101 c.header('Set-Cookie', cookie, { append: true })102}103104export const generateSignedCookie = async (105 name: string,106 value: string,107 secret: string | BufferSource,108 opt?: CookieOptions109): Promise<string> => {110 let cookie111 if (opt?.prefix === 'secure') {112 cookie = await serializeSigned('__Secure-' + name, value, secret, {113 path: '/',114 ...opt,115 secure: true,116 })117 } else if (opt?.prefix === 'host') {118 cookie = await serializeSigned('__Host-' + name, value, secret, {119 ...opt,120 path: '/',121 secure: true,122 domain: undefined,123 })124 } else {125 cookie = await serializeSigned(name, value, secret, { path: '/', ...opt })126 }127 return cookie128}129130export const setSignedCookie = async (131 c: Context,132 name: string,133 value: string,134 secret: string | BufferSource,135 opt?: CookieOptions136): Promise<void> => {137 const cookie = await generateSignedCookie(name, value, secret, opt)138 c.header('set-cookie', cookie, { append: true })139}140141export const deleteCookie = (c: Context, name: string, opt?: CookieOptions): string | undefined => {142 const deletedCookie = getCookie(c, name, opt?.prefix)143 setCookie(c, name, '', { ...opt, maxAge: 0 })144 return deletedCookie145}