Response header content-disposition immutable?

I am using the Backblaze b2 provided worker of which the important part is this:

response = await fetch(modRequest)

Based on https://developers.cloudflare.com/workers/templates/pages/alter_headers/ I tried to do

response = await fetch(modRequest)
if (request.url.match(/download/)) {
    response.headers.set('Content-disposition', 'attachment')
}

but this resulted in an 500 error.

The following works:

response = await fetch(modRequest)
if (request.url.match(/download/)) {
  headers = new Headers(response.Headers);
  headers.set('Content-disposition', 'attachment')
  response = new Response(response.body, {
    headers: headers
  })

but it’s really damn convoluted. Is there a simpler way I missed?

Also: how do you debug workers? The worker playground doesn’t seem to work because I get The initial connection between Cloudflare’s network and the origin web server timed out. – I would guess the worker doesn’t have the backblaze origin set up?

The response headers are immutable in this case. You do need a new response object, however technically this should work too

response = await fetch(modRequest);
response = new Response(response.body, response);
if (request.url.match(/download/))
{
    response.headers.set('Content-disposition', 'attachment');
}