Skip to content
Myr

fix(context): don't throw the error with `c.header()` when it's finalized (#4078)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(context): don't throw the error with `c.header()` when it's finalized Co-authored-by: Taku Amano <taku@taaas.jp> * remove the line break ---------

authored by Yusuke Wada · committed by GitHub · parent b68da39 · Browse files · 2 files+22 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+22 added−24 removed

  • tests:+8 added−1 removed
  • other:+14 added−23 removed

2 files+22 added−24 removed

modified:src/context.test.ts+8 added-1 removed
197 unmodified lines
198 expect(res.headers.get('x-Custom1')).toBe('Message1')199 expect(res.headers.get('x-Custom2')).toBe('Message2-Override')200 expect(res.headers.get('x-Custom3')).toBe('Message3')201 expect(res.status).toBe(201)202 expect(await res.text()).toBe('this is body')202203 // res is already set.204 c.res = res205 c.header('X-Custom4', 'Message4')206 c.status(202)207 expect(c.res.headers.get('X-Custom4')).toBe('Message4')208 expect(c.res.status).toBe(201)209 expect(await res.text()).toBe('this is body')210 })211212 it('Inherit current status if not specified', async () => {213 c.status(201)
196 unmodified lines
410 c.res = makeResponseHeaderImmutable(new Response('bar'))411 expect(await c.res.text()).toBe('bar')412 expect(c.res.headers.get('X-Custom')).toBe('Message')413 })414415 it('Should be able to set headers if the context is finalized', async () => {416 c.res = makeResponseHeaderImmutable(new Response('bar'))417 expect(c.finalized).toBe(true)418 c.header('X-Custom', 'Message')419 expect(c.res.headers.get('X-Custom')).toBe('Message')420 })421})422423describe('Pass a ResponseInit to respond methods', () => {424 const req = new Request('http://localhost/')
99 unmodified lines
modified:src/context.ts+14 added-23 removed
410 unmodified lines
411 */412 set res(_res: Response | undefined) {413 this.#isFresh = false414 if (this.#res && _res) {415 try {416 for (const [k, v] of this.#res.headers.entries()) {417 if (k === 'content-type') {418 continue419 }420 if (k === 'set-cookie') {421 const cookies = this.#res.headers.getSetCookie()422 _res.headers.delete('set-cookie')423 for (const cookie of cookies) {424 _res.headers.append('set-cookie', cookie)425 }426 } else {427 _res.headers.set(k, v)428 }415 _res = new Response(_res.body, _res)416 for (const [k, v] of this.#res.headers.entries()) {417 if (k === 'content-type') {418 continue419 }430 } catch (e) {431 if (e instanceof TypeError && e.message.includes('immutable')) {432 // `_res` is immutable (probably a response from a fetch API), so retry with a new response.433 this.res = new Response(_res.body, {434 headers: _res.headers,435 status: _res.status,436 })437 return420 if (k === 'set-cookie') {421 const cookies = this.#res.headers.getSetCookie()422 _res.headers.delete('set-cookie')423 for (const cookie of cookies) {424 _res.headers.append('set-cookie', cookie)425 }426 } else {439 throw e427 _res.headers.set(k, v)428 }429 }430 }431 this.#res = _res
79 unmodified lines
511 * })512 * ```513 */514 header: SetHeaders = (name, value, options): void => {515 if (this.finalized) {516 this.#res = new Response((this.#res as Response).body, this.#res)517 }518 // Clear the header519 if (value === undefined) {520 if (this.#headers) {521 this.#headers.delete(name)
335 unmodified lines