feat(utils/cookie): Typesafe cookie options (#2350)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* typesafe cookie options * typesafe cookie options * npm run denoify
authored by Jxck · committed by GitHub · parent 01ee574 · Browse files · 3 files·+38 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+38 added−38 removed
- tests:+0 added−34 removed
- other:+38 added−4 removed
Provenance
- Commit
062871d- Tree
4480372- Author
Jxck <jxck@jxck.io>- Committer
GitHub <noreply@github.com>- Parent
01ee574
3 files changed+38 added−38 removed
modified:deno_dist/utils/cookie.ts+19 added-2 removed
1 import { decodeURIComponent_ } from './url.ts'2 3 export type Cookie = Record<string, string>4 export type SignedCookie = Record<string, string | false>5+6+type PartitionCookieConstraint =7+ | { partition: true; secure: true }8+ | { partition?: boolean; secure?: boolean } // reset to default9+type SecureCookieConstraint = { secure: true }10+type HostCookieConstraint = { secure: true; path: '/'; domain?: undefined }11+12 export type CookieOptions = {13 domain?: string14 expires?: Date15 httpOnly?: boolean3 unmodified lines19 signingSecret?: string20 sameSite?: 'Strict' | 'Lax' | 'None'21 partitioned?: boolean22 prefix?: CookiePrefixOptions16-}23+} & PartitionCookieConstraint24 export type CookiePrefixOptions = 'host' | 'secure'25 26+export type CookieConstraint<Name> = Name extends `__Secure-${string}`27+ ? CookieOptions & SecureCookieConstraint28+ : Name extends `__Host-${string}`29+ ? CookieOptions & HostCookieConstraint30+ : CookieOptions31+32 const algorithm = { name: 'HMAC', hash: 'SHA-256' }33 34 const getCryptoKey = async (secret: string | BufferSource): Promise<CryptoKey> => {35 const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret166 unmodified lines202 203 return cookie204 }205 193-export const serialize = (name: string, value: string, opt: CookieOptions = {}): string => {206+export const serialize = <Name extends string>(207+ name: Name,208+ value: string,209+ opt?: CookieConstraint<Name>210+): string => {211 value = encodeURIComponent(value)212 return _serialize(name, value, opt)213 }214 11 unmodified linesmodified:src/utils/cookie.test.ts-34 removed
243 unmodified lines244 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.'245 )246 })247 248- it('Should throw Error __Secure- cookie without Secure attributes', () => {249- expect(() => {250- serialize('__Secure-great_cookie', 'banana', {})251- }).toThrowError('__Secure- Cookie must have Secure attributes')252- })253-254- it('Should throw Error __Host- cookie without Secure attributes', () => {255- expect(() => {256- serialize('__Host-great_cookie', 'banana', {257- secure: false,258- path: '/',259- })260- }).toThrowError('__Host- Cookie must have Secure attributes')261- })262-263- it('Should throw Error __Host- cookie without Path attributes with "/"', () => {264- expect(() => {265- serialize('__Host-great_cookie', 'banana', {266- secure: true,267- path: '/admin',268- })269- }).toThrowError('__Host- Cookie must have Path attributes with "/"')270- })271-272- it('Should throw Error __Host- cookie with Domain attributes', () => {273- expect(() => {274- serialize('__Host-great_cookie', 'banana', {275- secure: true,276- path: '/',277- domain: 'site.example',278- })279- }).toThrowError('__Host- Cookie must not have Domain attributes')280- })281-248 it('Should throw Error Partitioned cookie without Secure attributes', () => {249 expect(() => {250 serialize('great_cookie', 'banana', {251 partitioned: true,4 unmodified linesmodified:src/utils/cookie.ts+19 added-2 removed
1 import { decodeURIComponent_ } from './url'2 3 export type Cookie = Record<string, string>4 export type SignedCookie = Record<string, string | false>5+6+type PartitionCookieConstraint =7+ | { partition: true; secure: true }8+ | { partition?: boolean; secure?: boolean } // reset to default9+type SecureCookieConstraint = { secure: true }10+type HostCookieConstraint = { secure: true; path: '/'; domain?: undefined }11+12 export type CookieOptions = {13 domain?: string14 expires?: Date15 httpOnly?: boolean3 unmodified lines19 signingSecret?: string20 sameSite?: 'Strict' | 'Lax' | 'None'21 partitioned?: boolean22 prefix?: CookiePrefixOptions16-}23+} & PartitionCookieConstraint24 export type CookiePrefixOptions = 'host' | 'secure'25 26+export type CookieConstraint<Name> = Name extends `__Secure-${string}`27+ ? CookieOptions & SecureCookieConstraint28+ : Name extends `__Host-${string}`29+ ? CookieOptions & HostCookieConstraint30+ : CookieOptions31+32 const algorithm = { name: 'HMAC', hash: 'SHA-256' }33 34 const getCryptoKey = async (secret: string | BufferSource): Promise<CryptoKey> => {35 const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret166 unmodified lines202 203 return cookie204 }205 193-export const serialize = (name: string, value: string, opt: CookieOptions = {}): string => {206+export const serialize = <Name extends string>(207+ name: Name,208+ value: string,209+ opt?: CookieConstraint<Name>210+): string => {211 value = encodeURIComponent(value)212 return _serialize(name, value, opt)213 }214 11 unmodified lines