Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate to getSetCookie #586

Merged
merged 11 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/cookies/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export type { CookieListItem, RequestCookie, ResponseCookie } from './types'
export { RequestCookies } from './request-cookies'
export { ResponseCookies } from './response-cookies'
export {
stringifyCookie,
parseCookie,
parseSetCookie,
splitCookiesString,
} from './serialize'
export { stringifyCookie, parseCookie, parseSetCookie } from './serialize'
15 changes: 2 additions & 13 deletions packages/cookies/src/response-cookies.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { ResponseCookie } from './types'
import {
splitCookiesString,
parseSetCookie,
stringifyCookie,
} from './serialize'
import { parseSetCookie, stringifyCookie } from './serialize'

/**
* A class for manipulating {@link Response} cookies (`Set-Cookie` header).
Expand All @@ -19,14 +15,7 @@ export class ResponseCookies {
constructor(responseHeaders: Headers) {
this._headers = responseHeaders

const setCookie = responseHeaders.getSetCookie?.()
responseHeaders.get('set-cookie') ?? []

const cookieStrings = Array.isArray(setCookie)
? setCookie
: // TODO: remove splitCookiesString when `getSetCookie` adoption is high enough in Node.js
// https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie#browser_compatibility
splitCookiesString(setCookie)
const cookieStrings = responseHeaders.getSetCookie()

for (const cookieString of cookieStrings) {
const parsed = parseSetCookie(cookieString)
Expand Down
79 changes: 0 additions & 79 deletions packages/cookies/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,82 +91,3 @@ function parseSameSite(string: string): ResponseCookie['sameSite'] {
? (string as ResponseCookie['sameSite'])
: undefined
}

/**
* @source https://github.com/nfriedly/set-cookie-parser/blob/master/lib/set-cookie.js
*
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
* that are within a single set-cookie field-value, such as in the Expires portion.
* This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
* Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
* React Native's fetch does this for *every* header, including set-cookie.
*
* Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
* Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
*/
export function splitCookiesString(cookiesString: string) {
if (!cookiesString) return []
var cookiesStrings = []
var pos = 0
var start
var ch
var lastComma
var nextStart
var cookiesSeparatorFound

function skipWhitespace() {
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
pos += 1
}
return pos < cookiesString.length
}

function notSpecialChar() {
ch = cookiesString.charAt(pos)

return ch !== '=' && ch !== ';' && ch !== ','
}

while (pos < cookiesString.length) {
start = pos
cookiesSeparatorFound = false

while (skipWhitespace()) {
ch = cookiesString.charAt(pos)
if (ch === ',') {
// ',' is a cookie separator if we have later first '=', not ';' or ','
lastComma = pos
pos += 1

skipWhitespace()
nextStart = pos

while (pos < cookiesString.length && notSpecialChar()) {
pos += 1
}

// currently special character
if (pos < cookiesString.length && cookiesString.charAt(pos) === '=') {
// we found cookies separator
cookiesSeparatorFound = true
// pos is inside the next cookie, so back up and return it.
pos = nextStart
cookiesStrings.push(cookiesString.substring(start, lastComma))
start = pos
} else {
// in param ',' or param separator ';',
// we continue from that comma
pos = lastComma + 1
}
} else {
pos += 1
}
}

if (!cookiesSeparatorFound || pos >= cookiesString.length) {
cookiesStrings.push(cookiesString.substring(start, cookiesString.length))
}
}

return cookiesStrings
}
11 changes: 1 addition & 10 deletions packages/cookies/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
stringifyCookie,
parseCookie,
parseSetCookie,
splitCookiesString,
} from '../src'
import { stringifyCookie, parseCookie, parseSetCookie } from '../src'

test('.stringifyCookie is exported', async () => {
expect(typeof stringifyCookie).toBe('function')
Expand All @@ -16,7 +11,3 @@ test('.parseCookie is exported', async () => {
test('.parseSetCookie is exported', async () => {
expect(typeof parseSetCookie).toBe('function')
})

test('.splitCookiesString is exported', async () => {
expect(typeof splitCookiesString).toBe('function')
})
11 changes: 0 additions & 11 deletions packages/cookies/test/serialize.test.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/integration-tests/tests/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test('allow to append multiple `set-cookie` header', async () => {
const response = new Response(null)
response.headers.append('set-cookie', 'foo=bar')
response.headers.append('set-cookie', 'bar=baz')

expect(response.headers.getSetCookie()).toEqual(['foo=bar', 'bar=baz'])

expect(response.headers.getAll?.('set-cookie')).toEqual([
'foo=bar',
'bar=baz',
Expand Down
3 changes: 0 additions & 3 deletions packages/node-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"utils",
"web"
],
"dependencies": {
"@edge-runtime/cookies": "workspace:*"
},
"devDependencies": {
"@edge-runtime/primitives": "workspace:*",
"@types/test-listen": "1.1.0",
Expand Down
11 changes: 5 additions & 6 deletions packages/node-utils/src/edge-to-node/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import { toToReadable } from './stream'

export function buildToNodeHandler(
dependencies: BuildDependencies,
options: RequestOptions
options: RequestOptions,
) {
const toRequest = buildToRequest(dependencies)
const toFetchEvent = buildToFetchEvent(dependencies)
return function toNodeHandler(webHandler: WebHandler): NodeHandler {
return (
incomingMessage: IncomingMessage,
serverResponse: ServerResponse
serverResponse: ServerResponse,
) => {
const request = toRequest(incomingMessage, options)
const maybePromise = webHandler(request, toFetchEvent(request))
if (maybePromise instanceof Promise) {
maybePromise.then((response) =>
toServerResponse(response, serverResponse)
toServerResponse(response, serverResponse),
)
} else {
toServerResponse(maybePromise, serverResponse)
Expand All @@ -36,16 +36,15 @@ export function buildToNodeHandler(

function toServerResponse(
webResponse: Response | null | undefined,
serverResponse: ServerResponse
serverResponse: ServerResponse,
) {
if (!webResponse) {
serverResponse.end()
return
}
mergeIntoServerResponse(
// @ts-ignore getAll() is not standard https://fetch.spec.whatwg.org/#headers-class
toOutgoingHeaders(webResponse.headers),
serverResponse
serverResponse,
)

serverResponse.statusCode = webResponse.status
Expand Down
19 changes: 6 additions & 13 deletions packages/node-utils/src/edge-to-node/headers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import type { Headers } from '@edge-runtime/primitives'
import { Headers } from '@edge-runtime/primitives'
import type { OutgoingHttpHeaders, ServerResponse } from 'node:http'
import { splitCookiesString } from '@edge-runtime/cookies'

export function toOutgoingHeaders(
headers?: Headers & { raw?: () => Record<string, string> },
): OutgoingHttpHeaders {
export function toOutgoingHeaders(headers?: Headers): OutgoingHttpHeaders {
const outputHeaders: OutgoingHttpHeaders = {}
if (headers) {
for (const [name, value] of typeof headers.raw !== 'undefined'
? Object.entries(headers.raw())
: headers.entries()) {
outputHeaders[name] = value
if (name.toLowerCase() === 'set-cookie') {
outputHeaders[name] =
headers.getAll?.('set-cookie') ?? splitCookiesString(value)
}
const _headers = new Headers(headers).entries()
for (const [name, value] of _headers) {
outputHeaders[name] =
name === 'set-cookie' ? headers.getSetCookie() : value
}
}
return outputHeaders
Expand Down
22 changes: 3 additions & 19 deletions packages/node-utils/test/edge-to-node/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ it('handles simple header values', () => {
new Headers({
'Content-Type': 'image/jpeg',
'X-My-Custom-Header': 'Zeke are cool',
})
)
}),
),
).toEqual({
'content-type': 'image/jpeg',
'x-my-custom-header': 'Zeke are cool',
})
})

it('splits set-cookie with getAll()', () => {
it('splits set-cookie with getSetCookie()', () => {
const headers = new Headers({ 'set-cookie': 'value1' })
headers.append('set-cookie', 'value2')
headers.append('set-cookie', 'value3')
Expand All @@ -24,22 +24,6 @@ it('splits set-cookie with getAll()', () => {
})
})

it('slits set-cookie without getAll()', () => {
const rawHeaders = {
raw: () => ({
'set-cookie':
'cookie1=value1, cookie2=value2; Max-Age=1000, cookie3=value3; Domain=<domain-value>; Secure',
}),
}
expect(toOutgoingHeaders(rawHeaders as unknown as Headers)).toEqual({
'set-cookie': [
'cookie1=value1',
'cookie2=value2; Max-Age=1000',
'cookie3=value3; Domain=<domain-value>; Secure',
],
})
})

it('handles multiple values as single string', () => {
const headers = new Headers({ 'x-multiple': 'value1' })
headers.append('x-multiple', 'value2')
Expand Down
1 change: 1 addition & 0 deletions packages/primitives/src/primitives/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ HeadersModule.Headers.prototype.values = function* () {
* Add a new method for retrieving all independent `set-cookie` headers that
* maybe have been appended. This will only work when getting `set-cookie`
* headers.
* @deprecated use [`Headers#getSetCookie()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie) instead
*/
HeadersModule.Headers.prototype.getAll = function (name) {
const _name = normalizeAndValidateHeaderName(name, 'Headers.getAll')
Expand Down
3 changes: 2 additions & 1 deletion packages/primitives/type-definitions/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class Headers extends globalThis.Headers {
/** @deprecated use [`Headers#getSetCookie()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie) instead */
getAll?(key: 'set-cookie'): string[]
Copy link
Member Author

@balazsorban44 balazsorban44 Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we drop getAll altogether as part of a major bump?

}

Expand All @@ -16,7 +17,7 @@ export type RequestInfo = string | Request | globalThis.Request
export type RequestInit = globalThis.RequestInit
declare const fetchImplementation: (
info: RequestInfo,
init?: RequestInit
init?: RequestInit,
) => Promise<Response>

declare const FileConstructor: typeof File
Expand Down
14 changes: 5 additions & 9 deletions packages/runtime/src/server/create-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createHandler<T extends EdgeContext>(options: Options<T>) {
? getClonableBodyStream(
req,
options.runtime.evaluate('Uint8Array'),
options.runtime.context.TransformStream
options.runtime.context.TransformStream,
)
: undefined

Expand All @@ -49,7 +49,7 @@ export function createHandler<T extends EdgeContext>(options: Options<T>) {
headers: toRequestInitHeaders(req),
method: req.method,
body: body?.cloneBodyStream(),
}
},
)

const waitUntil = response.waitUntil()
Expand All @@ -60,7 +60,7 @@ export function createHandler<T extends EdgeContext>(options: Options<T>) {
res.statusMessage = response.statusText

for (const [key, value] of Object.entries(
toNodeHeaders(response.headers)
toNodeHeaders(response.headers),
)) {
if (value !== undefined) {
res.setHeader(key, value)
Expand Down Expand Up @@ -112,18 +112,14 @@ function toRequestInitHeaders(req: IncomingMessage): RequestInit['headers'] {

/**
* Transforms WHATWG Headers into a Node Headers shape. Copies all items but
* does a special case for Set-Cookie using the hidden method getAll which
* allows to get all cookies instead of a folded value.
* does a special case for Set-Cookie using the [`getSetCookie`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie) method.
*/
function toNodeHeaders(headers?: Headers): NodeHeaders {
const result: NodeHeaders = {}
if (headers) {
for (const [key, value] of headers.entries()) {
result[key] =
key.toLowerCase() === 'set-cookie'
? // @ts-ignore getAll is hidden in Headers but exists.
headers.getAll('set-cookie')
: value
key.toLowerCase() === 'set-cookie' ? headers.getSetCookie() : value
}
}
return result
Expand Down
Loading