Skip to content
Myr

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

3 files+38 added−38 removed

modified:deno_dist/utils/cookie.ts+19 added-2 removed
1import { decodeURIComponent_ } from './url.ts'23export type Cookie = Record<string, string>4export type SignedCookie = Record<string, string | false>56type PartitionCookieConstraint =7 | { partition: true; secure: true }8 | { partition?: boolean; secure?: boolean } // reset to default9type SecureCookieConstraint = { secure: true }10type HostCookieConstraint = { secure: true; path: '/'; domain?: undefined }1112export type CookieOptions = {13 domain?: string14 expires?: Date15 httpOnly?: boolean
3 unmodified lines
19 signingSecret?: string20 sameSite?: 'Strict' | 'Lax' | 'None'21 partitioned?: boolean22 prefix?: CookiePrefixOptions16}23} & PartitionCookieConstraint24export type CookiePrefixOptions = 'host' | 'secure'2526export type CookieConstraint<Name> = Name extends `__Secure-${string}`27 ? CookieOptions & SecureCookieConstraint28 : Name extends `__Host-${string}`29 ? CookieOptions & HostCookieConstraint30 : CookieOptions3132const algorithm = { name: 'HMAC', hash: 'SHA-256' }3334const getCryptoKey = async (secret: string | BufferSource): Promise<CryptoKey> => {35 const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret
166 unmodified lines
202203 return cookie204}205193export const serialize = (name: string, value: string, opt: CookieOptions = {}): string => {206export 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
modified:src/utils/cookie.test.ts-34 removed
243 unmodified lines
244 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.'245 )246 })247248 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 })253254 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 })262263 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 })271272 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 })281248 it('Should throw Error Partitioned cookie without Secure attributes', () => {249 expect(() => {250 serialize('great_cookie', 'banana', {251 partitioned: true,
4 unmodified lines
modified:src/utils/cookie.ts+19 added-2 removed
1import { decodeURIComponent_ } from './url'23export type Cookie = Record<string, string>4export type SignedCookie = Record<string, string | false>56type PartitionCookieConstraint =7 | { partition: true; secure: true }8 | { partition?: boolean; secure?: boolean } // reset to default9type SecureCookieConstraint = { secure: true }10type HostCookieConstraint = { secure: true; path: '/'; domain?: undefined }1112export type CookieOptions = {13 domain?: string14 expires?: Date15 httpOnly?: boolean
3 unmodified lines
19 signingSecret?: string20 sameSite?: 'Strict' | 'Lax' | 'None'21 partitioned?: boolean22 prefix?: CookiePrefixOptions16}23} & PartitionCookieConstraint24export type CookiePrefixOptions = 'host' | 'secure'2526export type CookieConstraint<Name> = Name extends `__Secure-${string}`27 ? CookieOptions & SecureCookieConstraint28 : Name extends `__Host-${string}`29 ? CookieOptions & HostCookieConstraint30 : CookieOptions3132const algorithm = { name: 'HMAC', hash: 'SHA-256' }3334const getCryptoKey = async (secret: string | BufferSource): Promise<CryptoKey> => {35 const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret
166 unmodified lines
202203 return cookie204}205193export const serialize = (name: string, value: string, opt: CookieOptions = {}): string => {206export 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