How do I SAFELY add a cookie to a response

HI. Hoping somebody can help me with a Cloudflare worker snippet that will allow me to safely add a cookie to a response.

Scenario - make a fetch request from a worker, then add an additional cookie to the response. I need to do this without changing any set-cookie headers that are in the original response.

I have tried cloning the response and then setting a header with my new cookie
response.headers.set(‘Set-Cookie’, ‘mycookie=true’);
however, this strips all the cookies from the original response and only returns my new cookie (not safe)

I have also tried cloning the response and then apending my new cookie
response.headers.append(‘Set-Cookie’, ‘mycookie=true’);
this drops my new cookie, but only keeps 1 of the cookies from the original response in a new flattered set cookie header

I have seen discussion on this topic else where it seem the problem is due to the fact that it’s valid to have multiple ‘set-cookie’ headers.

What I have not found is a code snippet that demonstrates a work around this problem and shows how to safely use a worker to add a new cookie to a response without breaking any of the existing cookies. Any suggestions or pointers in the correct direction would be greatly appreciated.

Thanks