Unverified Commit 4d20beb7 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

BREAKING CHANGE: feat(edge): split `NextCookies` to `RequestCookies` and `ResponseCookies` (#41526)

Ref: [Slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1666056382299069?thread_ts=1666041444.633059&cid=C035J346QQL),
[docs update](https://github.com/vercel/front/pull/17090)

Spec: https://wicg.github.io/cookie-store/

BREAKING CHANGE:

Ref: https://github.com/vercel/edge-runtime/pull/177,
https://github.com/vercel/edge-runtime/pull/181

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 0e25f8c6
...@@ -148,7 +148,10 @@ To produce a response from Middleware, you should `rewrite` to a route ([Page](/ ...@@ -148,7 +148,10 @@ To produce a response from Middleware, you should `rewrite` to a route ([Page](/
## Using Cookies ## Using Cookies
The `cookies` API extends [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and allows you to `get`, `set`, and `delete` cookies. It also includes methods like [entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries) and [values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries). Cookies are regular headers. On a `Request`, they are stored in the `Cookie` header. On a `Response` they are in the `Set-Cookie` header. Next.js provides a convenient way to access and manipulate these cookies through the `cookies` extension on `NextRequest` and `NextResponse`.
1. For incoming requests, `cookies` comes with the following methods: `get`, `getAll`, `set`, and `delete` cookies. You can check for the existence of a cookie with `has` or remove all cookies with `clear`.
2. For outgoing responses, `cookies` have the following methods `get`, `getAll`, `set`, and `delete`.
```typescript ```typescript
// middleware.ts // middleware.ts
...@@ -156,23 +159,28 @@ import { NextResponse } from 'next/server' ...@@ -156,23 +159,28 @@ import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server' import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) { export function middleware(request: NextRequest) {
// Setting cookies on the response // Assume a "Cookie:vercel=fast" header to be present on the incoming request
// Getting cookies from the request using the `RequestCookies` API
const cookie = request.cookies.get('nextjs')?.value
console.log(cookie) // => 'fast'
const allCookies = request.cookies.getAll()
console.log(allCookies) // => [{ name: 'vercel', value: 'fast' }]
response.cookies.has('nextjs') // => true
response.cookies.delete('nextjs')
response.cookies.has('nextjs') // => false
// Setting cookies on the response using the `ResponseCookies` API
const response = NextResponse.next() const response = NextResponse.next()
response.cookies.set('vercel', 'fast') response.cookies.set('vercel', 'fast')
response.cookies.set('vercel', 'fast', { path: '/test' }) response.cookies.set({
name: 'vercel',
// Getting cookies from the request value: 'fast',
const cookie = request.cookies.get('vercel') path: '/test',
console.log(cookie) // => 'fast' })
const allCookies = request.cookies.entries() const cookie = response.cookies.get('vercel')
console.log(allCookies) // => [{ key: 'vercel', value: 'fast' }] console.log(cookie) // => { name: 'vercel', value: 'fast', Path: '/test' }
const { value, options } = response.cookies.getWithOptions('vercel') // The outgoing response will have a `Set-Cookie:vercel=fast;path=/test` header.
console.log(value) // => 'fast'
console.log(options) // => { Path: '/test' }
// Deleting cookies
response.cookies.delete('vercel')
response.cookies.clear()
return response return response
} }
......
...@@ -10,7 +10,15 @@ description: Learn about the server-only helpers for Middleware and Edge API Rou ...@@ -10,7 +10,15 @@ description: Learn about the server-only helpers for Middleware and Edge API Rou
The `NextRequest` object is an extension of the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface, with the following added methods and properties: The `NextRequest` object is an extension of the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface, with the following added methods and properties:
- `cookies` - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) with cookies from the `Request`. See [Using cookies in Middleware](/docs/advanced-features/middleware#using-cookies) - `cookies` - A [RequestCookies](https://edge-runtime.vercel.app/packages/cookies#for-request) instance with cookies from the `Request`. It reads/mutates the `Cookie` header of the request. See also [Using cookies in Middleware](/docs/advanced-features/middleware#using-cookies).
- `get` - A method that takes a cookie `name` and returns an object with `name` and `value`. If a cookie with `name` isn't found, it returns `undefined`. If multiple cookies match, it will only return the first match.
- `getAll` - A method that is similar to `get`, but returns a list of all the cookies with a matching `name`. If `name` is unspecified, it returns all the available cookies.
- `set` - A method that takes an object with properties of `CookieListItem` as defined in the [W3C CookieStore API](https://wicg.github.io/cookie-store/#dictdef-cookielistitem) spec.
- `delete` - A method that takes either a cookie `name` or a list of names. and removes the cookies matching the name(s). Returns `true` for deleted and `false` for undeleted cookies.
- `has` - A method that takes a cookie `name` and returns a `boolean` based on if the cookie exists (`true`) or not (`false`).
- `clear` - A method that takes no argument and will effectively remove the `Cookie` header.
- `nextUrl`: Includes an extended, parsed, URL object that gives you access to Next.js specific properties such as `pathname`, `basePath`, `trailingSlash` and `i18n`. Includes the following properties: - `nextUrl`: Includes an extended, parsed, URL object that gives you access to Next.js specific properties such as `pathname`, `basePath`, `trailingSlash` and `i18n`. Includes the following properties:
- `basePath` (`string`) - `basePath` (`string`)
- `buildId` (`string || undefined`) - `buildId` (`string || undefined`)
...@@ -74,7 +82,12 @@ The `NextResponse` class extends the native [`Response`](https://developer.mozil ...@@ -74,7 +82,12 @@ The `NextResponse` class extends the native [`Response`](https://developer.mozil
Public methods are available on an instance of the `NextResponse` class. Depending on your use case, you can create an instance and assign to a variable, then access the following public methods: Public methods are available on an instance of the `NextResponse` class. Depending on your use case, you can create an instance and assign to a variable, then access the following public methods:
- `cookies` - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) with the cookies in the `Response` - `cookies` - A [ResponseCookies](https://edge-runtime.vercel.app/packages/cookies#for-response) instance with the cookies from the `Response`. It a
A [ResponseCooies](https://edge-runtime.vercel.app/packages/cookies#for-response) instance with cookies from the `Response`. It reads/mutates the `Set-Cookie` header of the response. See also [Using cookies in Middleware](/docs/advanced-features/middleware#using-cookies).
- `get` - A method that takes a cookie `name` and returns an object with `name` and `value`. If a cookie with `name` isn't found, it returns `undefined`. If multiple cookies match, it will only return the first match.
- `getAll` - A method that is similar to `get`, but returns a list of all the cookies with a matching `name`. If `name` is unspecified, it returns all the available cookies.
- `set` - A method that takes an object with properties of `CookieListItem` as defined in the [W3C CookieStore API](https://wicg.github.io/cookie-store/#dictdef-cookielistitem) spec.
- `delete` - A method that takes either a cookie `name` or a list of names. and removes the cookies matching the name(s). Returns `true` for deleted and `false` for undeleted cookies.
### Static Methods ### Static Methods
......
...@@ -241,16 +241,13 @@ export function middleware() { ...@@ -241,16 +241,13 @@ export function middleware() {
response.cookies.set('nextjs', 'awesome', { path: '/test' }) response.cookies.set('nextjs', 'awesome', { path: '/test' })
// get all the details of a cookie // get all the details of a cookie
const { value, options } = response.cookies.getWithOptions('vercel') const { value, ...options } = response.cookies.getWithOptions('vercel')
console.log(value) // => 'fast' console.log(value) // => 'fast'
console.log(options) // => { Path: '/test' } console.log(options) // => { name: 'vercel', Path: '/test' }
// deleting a cookie will mark it as expired // deleting a cookie will mark it as expired
response.cookies.delete('vercel') response.cookies.delete('vercel')
// clear all cookies means mark all of them as expired
response.cookies.clear()
return response return response
} }
``` ```
......
import type { AsyncLocalStorage } from 'async_hooks' import type { AsyncLocalStorage } from 'async_hooks'
import type { NextCookies } from '../../server/web/spec-extension/cookies' import type {
ReadonlyHeaders,
ReadonlyRequestCookies,
} from '../../server/app-render'
export interface RequestStore { export interface RequestStore {
headers: Headers headers: ReadonlyHeaders
cookies: NextCookies cookies: ReadonlyRequestCookies
previewData: any previewData: any
} }
......
...@@ -33,7 +33,7 @@ import { ServerInsertedHTMLContext } from '../shared/lib/server-inserted-html' ...@@ -33,7 +33,7 @@ import { ServerInsertedHTMLContext } from '../shared/lib/server-inserted-html'
import { stripInternalQueries } from './internal-utils' import { stripInternalQueries } from './internal-utils'
import type { ComponentsType } from '../build/webpack/loaders/next-app-loader' import type { ComponentsType } from '../build/webpack/loaders/next-app-loader'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect' import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { NextCookies } from './web/spec-extension/cookies' import { RequestCookies } from './web/spec-extension/cookies'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context' import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found' import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
import { HeadManagerContext } from '../shared/lib/head-manager-context' import { HeadManagerContext } from '../shared/lib/head-manager-context'
...@@ -45,7 +45,7 @@ const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly') ...@@ -45,7 +45,7 @@ const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly')
function readonlyHeadersError() { function readonlyHeadersError() {
return new Error('ReadonlyHeaders cannot be modified') return new Error('ReadonlyHeaders cannot be modified')
} }
class ReadonlyHeaders { export class ReadonlyHeaders {
[INTERNAL_HEADERS_INSTANCE]: Headers [INTERNAL_HEADERS_INSTANCE]: Headers
entries: Headers['entries'] entries: Headers['entries']
...@@ -83,20 +83,17 @@ class ReadonlyHeaders { ...@@ -83,20 +83,17 @@ class ReadonlyHeaders {
} }
const INTERNAL_COOKIES_INSTANCE = Symbol('internal for cookies readonly') const INTERNAL_COOKIES_INSTANCE = Symbol('internal for cookies readonly')
function readonlyCookiesError() { class ReadonlyRequestCookiesError extends Error {
return new Error('ReadonlyCookies cannot be modified') message =
'ReadonlyRequestCookies cannot be modified. Read more: https://nextjs.org/api-reference/cookies'
} }
class ReadonlyNextCookies { export class ReadonlyRequestCookies {
[INTERNAL_COOKIES_INSTANCE]: NextCookies [INTERNAL_COOKIES_INSTANCE]: RequestCookies
entries: NextCookies['entries'] get: RequestCookies['get']
forEach: NextCookies['forEach'] getAll: RequestCookies['getAll']
get: NextCookies['get'] has: RequestCookies['has']
getWithOptions: NextCookies['getWithOptions']
has: NextCookies['has']
keys: NextCookies['keys']
values: NextCookies['values']
constructor(request: { constructor(request: {
headers: { headers: {
...@@ -105,29 +102,26 @@ class ReadonlyNextCookies { ...@@ -105,29 +102,26 @@ class ReadonlyNextCookies {
}) { }) {
// Since `new Headers` uses `this.append()` to fill the headers object ReadonlyHeaders can't extend from Headers directly as it would throw. // Since `new Headers` uses `this.append()` to fill the headers object ReadonlyHeaders can't extend from Headers directly as it would throw.
// Request overridden to not have to provide a fully request object. // Request overridden to not have to provide a fully request object.
const cookiesInstance = new NextCookies(request as Request) const cookiesInstance = new RequestCookies(request.headers as Headers)
this[INTERNAL_COOKIES_INSTANCE] = cookiesInstance this[INTERNAL_COOKIES_INSTANCE] = cookiesInstance
this.entries = cookiesInstance.entries.bind(cookiesInstance)
this.forEach = cookiesInstance.forEach.bind(cookiesInstance)
this.get = cookiesInstance.get.bind(cookiesInstance) this.get = cookiesInstance.get.bind(cookiesInstance)
this.getWithOptions = cookiesInstance.getWithOptions.bind(cookiesInstance) this.getAll = cookiesInstance.getAll.bind(cookiesInstance)
this.has = cookiesInstance.has.bind(cookiesInstance) this.has = cookiesInstance.has.bind(cookiesInstance)
this.keys = cookiesInstance.keys.bind(cookiesInstance)
this.values = cookiesInstance.values.bind(cookiesInstance)
} }
[Symbol.iterator]() { [Symbol.iterator]() {
return this[INTERNAL_COOKIES_INSTANCE][Symbol.iterator]() return (this[INTERNAL_COOKIES_INSTANCE] as any)[Symbol.iterator]()
} }
clear() { clear() {
throw readonlyCookiesError() throw new ReadonlyRequestCookiesError()
} }
delete() { delete() {
throw readonlyCookiesError() throw new ReadonlyRequestCookiesError()
} }
set() { set() {
throw readonlyCookiesError() throw new ReadonlyRequestCookiesError()
} }
} }
...@@ -1662,7 +1656,7 @@ export async function renderToHTMLOrFlight( ...@@ -1662,7 +1656,7 @@ export async function renderToHTMLOrFlight(
) )
let cachedHeadersInstance: ReadonlyHeaders | undefined let cachedHeadersInstance: ReadonlyHeaders | undefined
let cachedCookiesInstance: ReadonlyNextCookies | undefined let cachedCookiesInstance: ReadonlyRequestCookies | undefined
const requestStore = { const requestStore = {
get headers() { get headers() {
...@@ -1675,7 +1669,7 @@ export async function renderToHTMLOrFlight( ...@@ -1675,7 +1669,7 @@ export async function renderToHTMLOrFlight(
}, },
get cookies() { get cookies() {
if (!cachedCookiesInstance) { if (!cachedCookiesInstance) {
cachedCookiesInstance = new ReadonlyNextCookies({ cachedCookiesInstance = new ReadonlyRequestCookies({
headers: { headers: {
get: (key) => { get: (key) => {
if (key !== 'cookie') { if (key !== 'cookie') {
......
import type { CookieSerializeOptions } from '../types'
import cookie from 'next/dist/compiled/cookie'
type GetWithOptionsOutput = {
value: string | undefined
options: { [key: string]: string }
}
const normalizeCookieOptions = (options: CookieSerializeOptions) => {
options = Object.assign({}, options)
if (options.maxAge) {
options.expires = new Date(Date.now() + options.maxAge * 1000)
}
if (options.path == null) {
options.path = '/'
}
return options
}
const serializeValue = (value: unknown) =>
typeof value === 'object' ? `j:${JSON.stringify(value)}` : String(value)
const serializeExpiredCookie = (
key: string,
options: CookieSerializeOptions = {}
) =>
cookie.serialize(key, '', {
expires: new Date(0),
path: '/',
...options,
})
const deserializeCookie = (input: Request | Response): string[] => {
const value = input.headers.get('set-cookie')
return value !== undefined && value !== null ? value.split(', ') : []
}
const serializeCookie = (input: string[]) => input.join(', ')
export class Cookies extends Map<string, string> {
constructor(input?: string | null) {
const parsedInput = typeof input === 'string' ? cookie.parse(input) : {}
super(Object.entries(parsedInput))
}
set(key: string, value: unknown, options: CookieSerializeOptions = {}) {
return super.set(
key,
cookie.serialize(
key,
serializeValue(value),
normalizeCookieOptions(options)
)
)
}
[Symbol.for('edge-runtime.inspect.custom')]() {
return Object.fromEntries(this.entries())
}
}
export class NextCookies extends Cookies {
response: Request | Response
constructor(response: Request | Response) {
super(response.headers.get('cookie'))
this.response = response
}
get = (...args: Parameters<Cookies['get']>) => {
return this.getWithOptions(...args).value
}
getWithOptions = (
...args: Parameters<Cookies['get']>
): GetWithOptionsOutput => {
const raw = super.get(...args)
if (typeof raw !== 'string') return { value: raw, options: {} }
const { [args[0]]: value, ...options } = cookie.parse(raw)
return { value, options }
}
set = (...args: Parameters<Cookies['set']>) => {
const isAlreadyAdded = super.has(args[0])
super.set(...args)
const currentCookie = super.get(args[0])
if (typeof currentCookie !== 'string') {
throw new Error(
`Invariant: failed to generate cookie for ${JSON.stringify(args)}`
)
}
if (isAlreadyAdded) {
const setCookie = serializeCookie(
deserializeCookie(this.response).filter(
(value) => !value.startsWith(`${args[0]}=`)
)
)
if (setCookie) {
this.response.headers.set(
'set-cookie',
[currentCookie, setCookie].join(', ')
)
} else {
this.response.headers.set('set-cookie', currentCookie)
}
} else {
this.response.headers.append('set-cookie', currentCookie)
}
return this
}
delete = (key: string, options: CookieSerializeOptions = {}) => {
const isDeleted = super.delete(key)
if (isDeleted) {
const setCookie = serializeCookie(
deserializeCookie(this.response).filter(
(value) => !value.startsWith(`${key}=`)
)
)
const expiredCookie = serializeExpiredCookie(key, options)
this.response.headers.set(
'set-cookie',
[expiredCookie, setCookie].join(', ')
)
}
return isDeleted
}
clear = (options: CookieSerializeOptions = {}) => {
const expiredCookies = Array.from(super.keys())
.map((key) => serializeExpiredCookie(key, options))
.join(', ')
if (expiredCookies) this.response.headers.set('set-cookie', expiredCookies)
return super.clear()
}
}
export { CookieSerializeOptions }
/**
* A simple caching behavior.
* We cache the result based on the key `K`
* which uses referential equality, to avoid re-computing
* the result for the same key.
*/
export function cached<K, V>(generate: (key: K) => V) {
let cache: { key: K; value: V } | undefined = undefined
return (key: K) => {
if (cache?.key !== key) {
cache = { key, value: generate(key) }
}
return cache.value
}
}
// TODO: use `@edge-runtime/cookies`
export type { CookieListItem, RequestCookie, ResponseCookie } from './types'
export { RequestCookies } from './request-cookies'
export { ResponseCookies } from './response-cookies'
import type { RequestCookie } from './types'
import { parseCookieString, serialize } from './serialize'
/**
* A class for manipulating {@link Request} cookies (`Cookie` header).
*/
export class RequestCookies {
readonly _headers: Headers
_parsed: Map<string, RequestCookie> = new Map()
constructor(requestHeaders: Headers) {
this._headers = requestHeaders
const header = requestHeaders.get('cookie')
if (header) {
const parsed = parseCookieString(header)
for (const [name, value] of parsed) {
this._parsed.set(name, { name, value })
}
}
}
[Symbol.iterator]() {
return this._parsed[Symbol.iterator]()
}
/**
* The amount of cookies received from the client
*/
get size(): number {
return this._parsed.size
}
get(...args: [name: string] | [RequestCookie]) {
const name = typeof args[0] === 'string' ? args[0] : args[0].name
return this._parsed.get(name)
}
getAll(...args: [name: string] | [RequestCookie] | []) {
const all = Array.from(this._parsed)
if (!args.length) {
return all.map(([_, value]) => value)
}
const name = typeof args[0] === 'string' ? args[0] : args[0]?.name
return all.filter(([n]) => n === name).map(([_, value]) => value)
}
has(name: string) {
return this._parsed.has(name)
}
set(...args: [key: string, value: string] | [options: RequestCookie]): this {
const [name, value] =
args.length === 1 ? [args[0].name, args[0].value] : args
const map = this._parsed
map.set(name, { name, value })
this._headers.set(
'cookie',
Array.from(map)
.map(([_, v]) => serialize(v))
.join('; ')
)
return this
}
/**
* Delete the cookies matching the passed name or names in the request.
*/
delete(
/** Name or names of the cookies to be deleted */
names: string | string[]
): boolean | boolean[] {
const map = this._parsed
const result = !Array.isArray(names)
? map.delete(names)
: names.map((name) => map.delete(name))
this._headers.set(
'cookie',
Array.from(map)
.map(([_, value]) => serialize(value))
.join('; ')
)
return result
}
/**
* Delete all the cookies in the cookies in the request.
*/
clear(): this {
this.delete(Array.from(this._parsed.keys()))
return this
}
/**
* Format the cookies in the request as a string for logging
*/
[Symbol.for('edge-runtime.inspect.custom')]() {
return `RequestCookies ${JSON.stringify(Object.fromEntries(this._parsed))}`
}
}
import type { ResponseCookie } from './types'
import { parseSetCookieString, serialize } from './serialize'
function replace(bag: Map<string, ResponseCookie>, headers: Headers) {
headers.delete('set-cookie')
for (const [, value] of bag) {
const serialized = serialize(value)
headers.append('set-cookie', serialized)
}
}
function normalizeCookie(cookie: ResponseCookie = { name: '', value: '' }) {
if (cookie.maxAge) {
cookie.expires = new Date(Date.now() + cookie.maxAge * 1000)
}
if (cookie.path === null || cookie.path === undefined) {
cookie.path = '/'
}
return cookie
}
/**
* A class for manipulating {@link Response} cookies (`Set-Cookie` header).
* Loose implementation of the experimental [Cookie Store API](https://wicg.github.io/cookie-store/#dictdef-cookie)
* The main difference is `ResponseCookies` methods do not return a Promise.
*/
export class ResponseCookies {
readonly _headers: Headers
_parsed: Map<string, ResponseCookie> = new Map()
constructor(responseHeaders: Headers) {
this._headers = responseHeaders
// @ts-expect-error See https://github.com/whatwg/fetch/issues/973
const headers = this._headers.getAll('set-cookie')
for (const header of headers) {
const parsed = parseSetCookieString(header)
if (parsed) {
this._parsed.set(parsed.name, parsed)
}
}
}
/**
* {@link https://wicg.github.io/cookie-store/#CookieStore-get CookieStore#get} without the Promise.
*/
get(
...args: [key: string] | [options: ResponseCookie]
): ResponseCookie | undefined {
const key = typeof args[0] === 'string' ? args[0] : args[0].name
return this._parsed.get(key)
}
/**
* {@link https://wicg.github.io/cookie-store/#CookieStore-getAll CookieStore#getAll} without the Promise.
*/
getAll(
...args: [key: string] | [options: ResponseCookie] | []
): ResponseCookie[] {
const all = Array.from(this._parsed.values())
if (!args.length) {
return all
}
const key = typeof args[0] === 'string' ? args[0] : args[0]?.name
return all.filter((c) => c.name === key)
}
/**
* {@link https://wicg.github.io/cookie-store/#CookieStore-set CookieStore#set} without the Promise.
*/
set(
...args:
| [key: string, value: string, cookie?: Partial<ResponseCookie>]
| [options: ResponseCookie]
): this {
const [name, value, cookie] =
args.length === 1 ? [args[0].name, args[0].value, args[0]] : args
const map = this._parsed
map.set(name, normalizeCookie({ name, value, ...cookie }))
replace(map, this._headers)
return this
}
/**
* {@link https://wicg.github.io/cookie-store/#CookieStore-delete CookieStore#delete} without the Promise.
*/
delete(...args: [key: string] | [options: ResponseCookie]): this {
const name = typeof args[0] === 'string' ? args[0] : args[0].name
return this.set({ name, value: '', expires: new Date(0) })
}
[Symbol.for('edge-runtime.inspect.custom')]() {
return `ResponseCookies ${JSON.stringify(Object.fromEntries(this._parsed))}`
}
}
import type { RequestCookie, ResponseCookie } from './types'
const SAME_SITE: ResponseCookie['sameSite'][] = ['strict', 'lax', 'none']
function parseSameSite(string: string): ResponseCookie['sameSite'] {
string = string.toLowerCase()
return SAME_SITE.includes(string as any)
? (string as ResponseCookie['sameSite'])
: undefined
}
function compact<T>(t: T): T {
const newT = {} as Partial<T>
for (const key in t) {
if (t[key]) {
newT[key] = t[key]
}
}
return newT as T
}
export function serialize(c: ResponseCookie | RequestCookie): string {
const attrs = [
'path' in c && c.path && `Path=${c.path}`,
'expires' in c && c.expires && `Expires=${c.expires.toUTCString()}`,
'maxAge' in c && c.maxAge && `Max-Age=${c.maxAge}`,
'domain' in c && c.domain && `Domain=${c.domain}`,
'secure' in c && c.secure && 'Secure',
'httpOnly' in c && c.httpOnly && 'HttpOnly',
'sameSite' in c && c.sameSite && `SameSite=${c.sameSite}`,
].filter(Boolean)
return `${c.name}=${encodeURIComponent(c.value ?? '')}; ${attrs.join('; ')}`
}
/**
* Parse a `Cookie` or `Set-Cookie header value
*/
export function parseCookieString(cookie: string): Map<string, string> {
const map = new Map<string, string>()
for (const pair of cookie.split(/; */)) {
if (!pair) continue
const [key, value] = pair.split('=', 2)
map.set(key, decodeURIComponent(value ?? 'true'))
}
return map
}
/**
* Parse a `Set-Cookie` header value
*/
export function parseSetCookieString(
setCookie: string
): undefined | ResponseCookie {
if (!setCookie) {
return undefined
}
const [[name, value], ...attributes] = parseCookieString(setCookie)
const { domain, expires, httponly, maxage, path, samesite, secure } =
Object.fromEntries(attributes.map(([key, v]) => [key.toLowerCase(), v]))
const cookie: ResponseCookie = {
name,
value: decodeURIComponent(value),
domain,
...(expires && { expires: new Date(expires) }),
...(httponly && { httpOnly: true }),
...(typeof maxage === 'string' && { maxAge: Number(maxage) }),
path,
...(samesite && { sameSite: parseSameSite(samesite) }),
...(secure && { secure: true }),
}
return compact(cookie)
}
export interface CookieSerializeOptions {
/**
* Specifies the value for the Domain Set-Cookie attribute. By default, no
* domain is set, and most clients will consider the cookie to apply to only
* the current domain.
*/
domain?: string
/**
* Specifies a function that will be used to encode a cookie's value. Since
* value of a cookie has a limited character set (and must be a simple
* string), this function can be used to encode a value into a string suited
* for a cookie's value.
*
* The default function is the global `encodeURIComponent`, which will
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode
* any that fall outside of the cookie range.
*/
encode?(val: string): string
/**
* Specifies the `Date` object to be the value for the `Expires`
* `Set-Cookie` attribute. By default, no expiration is set, and most
* clients will consider this a "non-persistent cookie" and will delete it
* on a condition like exiting a web browser application.
*
* *Note* the cookie storage model specification states that if both
* `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
* possible not all clients by obey this, so if both are set, they should
* point to the same date and time.
*/
expires?: Date
/**
* Specifies the boolean value for the `HttpOnly` `Set-Cookie` attribute.
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
* default, the `HttpOnly` attribute is not set.
*
* *Note* be careful when setting this to true, as compliant clients will
* not allow client-side JavaScript to see the cookie in `document.cookie`.
*/
httpOnly?: boolean
/**
* Specifies the number (in seconds) to be the value for the `Max-Age`
* `Set-Cookie` attribute. The given number will be converted to an integer
* by rounding down. By default, no maximum age is set.
*
* *Note* the cookie storage model specification states that if both
* `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
* possible not all clients by obey this, so if both are set, they should
* point to the same date and time.
*/
maxAge?: number
/**
* Specifies the value for the `Path` `Set-Cookie` attribute. By default,
* the path is considered the "default path".
*/
path?: string
/**
* Specifies the boolean or string to be the value for the `SameSite`
* `Set-Cookie` attribute.
*
* - `true` will set the `SameSite` attribute to `Strict` for strict same
* site enforcement.
* - `false` will not set the `SameSite` attribute.
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site
* enforcement.
* - `'strict'` will set the `SameSite` attribute to Strict for strict same
* site enforcement.
* - `'none'` will set the SameSite attribute to None for an explicit
* cross-site cookie.
*/
sameSite?: boolean | 'lax' | 'strict' | 'none'
/**
* Specifies the boolean value for the `Secure` `Set-Cookie` attribute. When
* truthy, the `Secure` attribute is set, otherwise it is not. By default,
* the `Secure` attribute is not set.
*
* *Note* be careful when setting this to `true`, as compliant clients will
* not send the cookie back to the server in the future if the browser does
* not have an HTTPS connection.
*/
secure?: boolean
}
/**
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem}
* as specified by W3C.
*/
export interface CookieListItem
extends Partial<
Pick<
CookieSerializeOptions,
'domain' | 'path' | 'expires' | 'secure' | 'sameSite'
>
> {
/** A string with the name of a cookie. */
name: string
/** A string containing the value of the cookie. */
value: string
}
/**
* Superset of {@link CookieListItem} extending it with
* the `httpOnly`, `maxAge` and `priority` properties.
*/
export type ResponseCookie = CookieListItem &
Partial<Pick<CookieSerializeOptions, 'httpOnly' | 'maxAge'>>
/**
* Subset of {@link CookieListItem}, only containing `name` and `value`
* since other cookie attributes aren't be available on a `Request`.
*/
export type RequestCookie = Pick<CookieListItem, 'name' | 'value'>
...@@ -3,13 +3,13 @@ import type { RequestData } from '../types' ...@@ -3,13 +3,13 @@ import type { RequestData } from '../types'
import { NextURL } from '../next-url' import { NextURL } from '../next-url'
import { toNodeHeaders, validateURL } from '../utils' import { toNodeHeaders, validateURL } from '../utils'
import { RemovedUAError, RemovedPageError } from '../error' import { RemovedUAError, RemovedPageError } from '../error'
import { NextCookies } from './cookies' import { RequestCookies } from './cookies'
export const INTERNALS = Symbol('internal request') export const INTERNALS = Symbol('internal request')
export class NextRequest extends Request { export class NextRequest extends Request {
[INTERNALS]: { [INTERNALS]: {
cookies: NextCookies cookies: RequestCookies
geo: RequestData['geo'] geo: RequestData['geo']
ip?: string ip?: string
url: NextURL url: NextURL
...@@ -21,7 +21,7 @@ export class NextRequest extends Request { ...@@ -21,7 +21,7 @@ export class NextRequest extends Request {
validateURL(url) validateURL(url)
super(url, init) super(url, init)
this[INTERNALS] = { this[INTERNALS] = {
cookies: new NextCookies(this), cookies: new RequestCookies(this.headers),
geo: init.geo || {}, geo: init.geo || {},
ip: init.ip, ip: init.ip,
url: new NextURL(url, { url: new NextURL(url, {
......
...@@ -2,7 +2,7 @@ import type { I18NConfig } from '../../config-shared' ...@@ -2,7 +2,7 @@ import type { I18NConfig } from '../../config-shared'
import { NextURL } from '../next-url' import { NextURL } from '../next-url'
import { toNodeHeaders, validateURL } from '../utils' import { toNodeHeaders, validateURL } from '../utils'
import { NextCookies } from './cookies' import { ResponseCookies } from './cookies'
const INTERNALS = Symbol('internal response') const INTERNALS = Symbol('internal response')
const REDIRECTS = new Set([301, 302, 303, 307, 308]) const REDIRECTS = new Set([301, 302, 303, 307, 308])
...@@ -28,7 +28,7 @@ function handleMiddlewareField( ...@@ -28,7 +28,7 @@ function handleMiddlewareField(
export class NextResponse extends Response { export class NextResponse extends Response {
[INTERNALS]: { [INTERNALS]: {
cookies: NextCookies cookies: ResponseCookies
url?: NextURL url?: NextURL
} }
...@@ -36,7 +36,7 @@ export class NextResponse extends Response { ...@@ -36,7 +36,7 @@ export class NextResponse extends Response {
super(body, init) super(body, init)
this[INTERNALS] = { this[INTERNALS] = {
cookies: new NextCookies(this), cookies: new ResponseCookies(this.headers),
url: init.url url: init.url
? new NextURL(init.url, { ? new NextURL(init.url, {
headers: toNodeHeaders(this.headers), headers: toNodeHeaders(this.headers),
......
...@@ -8,17 +8,6 @@ export interface NodeHeaders { ...@@ -8,17 +8,6 @@ export interface NodeHeaders {
[header: string]: string | string[] | undefined [header: string]: string | string[] | undefined
} }
export interface CookieSerializeOptions {
domain?: string
encode?(val: string): string
expires?: Date
httpOnly?: boolean
maxAge?: number
path?: string
sameSite?: boolean | 'lax' | 'strict' | 'none'
secure?: boolean
}
export interface RequestData { export interface RequestData {
geo?: { geo?: {
city?: string city?: string
......
...@@ -2,9 +2,7 @@ import { cookies } from 'next/headers' ...@@ -2,9 +2,7 @@ import { cookies } from 'next/headers'
export default function Page() { export default function Page() {
const cookiesList = cookies() const cookiesList = cookies()
const cookie = cookiesList.get('use-cookies') const hasCookie = cookiesList.has('use-cookies')
const hasCookie = cookie === 'value'
return ( return (
<> <>
......
/**
* @jest-environment @edge-runtime/jest-environment
*/
import {
Cookies,
CookieSerializeOptions,
} from 'next/dist/server/web/spec-extension/cookies'
it('create a empty cookies bag', async () => {
const cookies = new Cookies()
expect(Object.entries(cookies)).toEqual([])
})
it('create a cookies bag from string', async () => {
const cookies = new Cookies('foo=bar; equation=E%3Dmc%5E2')
expect(Array.from(cookies.entries())).toEqual([
['foo', 'foo=bar; Path=/'],
['equation', 'equation=E%3Dmc%5E2; Path=/'],
])
})
it('.set', async () => {
const cookies = new Cookies()
cookies.set('foo', 'bar')
expect(Array.from(cookies.entries())).toEqual([['foo', 'foo=bar; Path=/']])
})
it('.set with options', async () => {
const cookies = new Cookies()
const options: CookieSerializeOptions = {
path: '/',
maxAge: 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: 'strict',
domain: 'example.com',
}
cookies.set('foo', 'bar', options)
expect(options).toEqual({
path: '/',
maxAge: 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: 'strict',
domain: 'example.com',
})
const [[key, value]] = Array.from(cookies.entries())
const values = value.split('; ')
expect(key).toBe('foo')
expect(values).toEqual([
'foo=bar',
'Max-Age=604800',
'Domain=example.com',
'Path=/',
expect.stringContaining('Expires='),
'HttpOnly',
'SameSite=Strict',
])
})
it('.delete', async () => {
const cookies = new Cookies()
cookies.set('foo', 'bar')
cookies.delete('foo')
expect(Array.from(cookies.entries())).toEqual([])
})
it('.has', async () => {
const cookies = new Cookies()
cookies.set('foo', 'bar')
expect(cookies.has('foo')).toBe(true)
})
...@@ -8,27 +8,25 @@ it('reflect .set into `set-cookie`', async () => { ...@@ -8,27 +8,25 @@ it('reflect .set into `set-cookie`', async () => {
) )
const response = new NextResponse() const response = new NextResponse()
expect(response.cookies.get('foo')?.value).toBe(undefined)
expect(response.cookies.get('foo')).toBe(undefined) expect(response.cookies.get('foo')).toEqual(undefined)
expect(response.cookies.getWithOptions('foo')).toEqual({
value: undefined,
options: {},
})
response.cookies response.cookies
.set('foo', 'bar', { path: '/test' }) .set('foo', 'bar', { path: '/test' })
.set('fooz', 'barz', { path: '/test2' }) .set('fooz', 'barz', { path: '/test2' })
expect(response.cookies.get('foo')).toBe('bar') expect(response.cookies.get('foo')?.value).toBe('bar')
expect(response.cookies.get('fooz')).toBe('barz') expect(response.cookies.get('fooz')?.value).toBe('barz')
expect(response.cookies.getWithOptions('foo')).toEqual({ expect(response.cookies.get('foo')).toEqual({
name: 'foo',
path: '/test',
value: 'bar', value: 'bar',
options: { Path: '/test' },
}) })
expect(response.cookies.getWithOptions('fooz')).toEqual({ expect(response.cookies.get('fooz')).toEqual({
name: 'fooz',
path: '/test2',
value: 'barz', value: 'barz',
options: { Path: '/test2' },
}) })
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
...@@ -48,10 +46,11 @@ it('reflect .delete into `set-cookie`', async () => { ...@@ -48,10 +46,11 @@ it('reflect .delete into `set-cookie`', async () => {
'foo=bar; Path=/' 'foo=bar; Path=/'
) )
expect(response.cookies.get('foo')).toBe('bar') expect(response.cookies.get('foo')?.value).toBe('bar')
expect(response.cookies.getWithOptions('foo')).toEqual({ expect(response.cookies.get('foo')).toEqual({
name: 'foo',
path: '/',
value: 'bar', value: 'bar',
options: { Path: '/' },
}) })
response.cookies.set('fooz', 'barz') response.cookies.set('fooz', 'barz')
...@@ -59,71 +58,39 @@ it('reflect .delete into `set-cookie`', async () => { ...@@ -59,71 +58,39 @@ it('reflect .delete into `set-cookie`', async () => {
'foo=bar; Path=/, fooz=barz; Path=/' 'foo=bar; Path=/, fooz=barz; Path=/'
) )
expect(response.cookies.get('fooz')).toBe('barz') expect(response.cookies.get('fooz')?.value).toBe('barz')
expect(response.cookies.getWithOptions('fooz')).toEqual({ expect(response.cookies.get('fooz')).toEqual({
name: 'fooz',
path: '/',
value: 'barz', value: 'barz',
options: { Path: '/' },
}) })
const firstDelete = response.cookies.delete('foo') response.cookies.delete('foo')
expect(firstDelete).toBe(true)
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, fooz=barz; Path=/' 'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, fooz=barz; Path=/'
) )
expect(response.cookies.get('foo')).toBe(undefined) expect(response.cookies.get('foo')?.value).toBe('')
expect(response.cookies.getWithOptions('foo')).toEqual({ expect(response.cookies.get('foo')).toEqual({
value: undefined, expires: new Date(0),
options: {}, name: 'foo',
}) value: '',
path: '/',
const secondDelete = response.cookies.delete('fooz')
expect(secondDelete).toBe(true)
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
)
expect(response.cookies.get('fooz')).toBe(undefined)
expect(response.cookies.getWithOptions('fooz')).toEqual({
value: undefined,
options: {},
}) })
expect(response.cookies.size).toBe(0)
})
it('reflect .clear into `set-cookie`', async () => {
const { NextResponse } = await import(
'next/dist/server/web/spec-extension/response'
)
const response = new NextResponse()
response.cookies.clear() response.cookies.delete('fooz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
undefined
)
response.cookies.set('foo', 'bar')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe( expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/' 'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
) )
expect(response.cookies.get('foo')).toBe('bar') expect(response.cookies.get('fooz')?.value).toBe('')
expect(response.cookies.getWithOptions('foo')).toEqual({ expect(response.cookies.get('fooz')).toEqual({
value: 'bar', expires: new Date(0),
options: { Path: '/' }, name: 'fooz',
value: '',
path: '/',
}) })
response.cookies.set('fooz', 'barz')
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=bar; Path=/, fooz=barz; Path=/'
)
response.cookies.clear()
expect(Object.fromEntries(response.headers.entries())['set-cookie']).toBe(
'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
)
}) })
it('response.cookie does not modify options', async () => { it('response.cookie does not modify options', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment