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 3 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
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
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
4 changes: 3 additions & 1 deletion packages/node-utils/src/edge-to-node/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export function toOutgoingHeaders(
outputHeaders[name] = value
if (name.toLowerCase() === 'set-cookie') {
outputHeaders[name] =
headers.getAll?.('set-cookie') ?? splitCookiesString(value)
headers.getSetCookie?.() ??
headers.getAll?.('set-cookie') ??
splitCookiesString(value)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/node-utils/test/edge-to-node/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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',
Expand All @@ -24,7 +24,7 @@ it('splits set-cookie with getAll()', () => {
})
})

it('slits set-cookie without getAll()', () => {
it('splits set-cookie without getAll()', () => {
const rawHeaders = {
raw: () => ({
'set-cookie':
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