Skip to content

Commit

Permalink
feat: revert .getAll removal, mark deprecated (#597)
Browse files Browse the repository at this point in the history
* feat: revert `.getAll` removal, mark deprecated

* add changelog

* Update fetch.js

* fix imports

* add test

* Update slimy-seals-confess.md
  • Loading branch information
balazsorban44 committed Sep 11, 2023
1 parent a598a6c commit e848ee8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-seals-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@edge-runtime/primitives': patch
---

Reverts the removal of `Headers#getAll` introduced in #586 for compatibility reasons. It is still marked as deprecated, as `Headers.getSetCookie` is the preferred method now.
5 changes: 5 additions & 0 deletions packages/integration-tests/tests/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test('allow to append multiple `set-cookie` header', async () => {
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',
])
})

test('disallow mutate response headers for redirects', async () => {
Expand Down
39 changes: 39 additions & 0 deletions packages/primitives/src/primitives/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as FetchSymbols from 'undici/lib/fetch/symbols'
import * as HeadersModule from 'undici/lib/fetch/headers'
import * as ResponseModule from 'undici/lib/fetch/response'
import * as UtilModule from 'undici/lib/fetch/util'
import * as WebIDLModule from 'undici/lib/fetch/webidl'
import { Request as BaseRequest } from 'undici/lib/fetch/request'

import { fetch as fetchImpl } from 'undici/lib/fetch'
Expand Down Expand Up @@ -43,6 +45,22 @@ HeadersModule.Headers.prototype.values = function* () {
}
}

/**
* Method for retrieving all independent `set-cookie` headers that
* may have been appended. This will only work when getting `set-cookie`
* headers.
*
* @deprecated Use [`.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')
if (_name !== 'set-cookie') {
throw new Error(`getAll can only be used with 'set-cookie'`)
}

return this.getSetCookie()
}

/**
* We also must patch the error static method since it works just like
* redirect and we need consistency.
Expand All @@ -54,6 +72,27 @@ ResponseModule.Response.error = function (...args) {
return response
}

/**
* normalize header name per WHATWG spec, and validate
*
* @param {string} potentialName
* @param {'Header.append' | 'Headers.delete' | 'Headers.get' | 'Headers.has' | 'Header.set'} errorPrefix
*/
function normalizeAndValidateHeaderName(potentialName, errorPrefix) {
const normalizedName = potentialName.toLowerCase()

if (UtilModule.isValidHeaderName(normalizedName)) {
return normalizedName
}

// Generate an WHATWG fetch spec compliant error
WebIDLModule.errors.invalidArgument({
prefix: errorPrefix,
value: normalizedName,
type: 'header name',
})
}

/**
* A global agent to be used with every fetch request. We also define a
* couple of globals that we can hide in the runtime for advanced use.
Expand Down
5 changes: 4 additions & 1 deletion packages/primitives/type-definitions/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export class Headers extends globalThis.Headers {}
export class Headers extends globalThis.Headers {
/** @deprecated Use [`.getSetCookie()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie) instead. */
getAll?(key: 'set-cookie'): string[]
}

export class Request extends globalThis.Request {
readonly headers: Headers
Expand Down

1 comment on commit e848ee8

@vercel
Copy link

@vercel vercel bot commented on e848ee8 Sep 11, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime-git-main.vercel.sh
edge-runtime.vercel.sh

Please sign in to comment.