awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
8.04k stars 355 forks source link

feat: Improved management of set-cookie in Headers class #383

Closed nabetti1720 closed 4 months ago

nabetti1720 commented 4 months ago

Issue # (if available)

Fixes #374

Description of changes

ToDo

Sample 1

reproduction.js ```javascript // reproduction.js const headers = new Headers(); headers.append("Set-Cookie", "AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly"); headers.append("Set-Cookie", "BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly"); headers.append("accept-encoding", "zstd"); headers.append("accept-encoding", "br"); console.log(headers.getSetCookie().length); console.log("---"); console.log(headers.getSetCookie()); console.log("---"); console.log(headers.get("set-cookie")); console.log("---"); console.log(headers.get("accept-encoding")); ```

Result of llrt:

% ./llrt reproduction.js
2
---
[ 'AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly', 'BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly' ]
---
AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly, BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT; path=/; domain=.sample.com; HttpOnly
---
zstd, br

Sample 2

fetch.js ```javascript // fetch.js const main = async () => { try { const response = await fetch('https://google.com'); console.log(response.headers.getSetCookie().length); console.log('---'); console.log(response.headers.getSetCookie()); } catch(e) { console.log(e); } } main(); ```

result of llrt:

% ./llrt fetch.js       
3
---
[ '1P_JAR=2024-05-17-09; expires=Sun, 16-Jun-2024 09:29:51 GMT; path=/; domain=.google.com; Secure', 'AEC=AQTF6HwxahWO1-_BHEFp8aW3ha-DH2GDFMp3rm3ZB107PiPHbDBd0PwzqA; expires=Wed, 13-Nov-2024 09:29:51 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax', 'NID=514=eJ5v2-Z32ybMP6nphl_y8cyq_UELTUAfDjWs2LeMCLi_eJdP2e-Kx5zlFeqwJkkc0JOyuDQ3hNNvWCHNekHqp0d67IfR-ZzZc3uhy5YuMubtCB4KB3fmxbnR4trwn5iqnmZg2do3ZFtxHHc1olQ4iH5Wny4CnyGkSoNk2g6M5Cc; expires=Sat, 16-Nov-2024 09:29:51 GMT; path=/; domain=.google.com; HttpOnly' ]

Sample 3

iterate.js ```javascript // iterate.js const headers = { "content-type": "application/json", authorization: "Bearer 1234", }; const h = new Headers(headers); h.append("set-cookie", "AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT"); h.append("set-cookie", "BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT"); console.log('1. entries/next ---'); const iterator = h.entries(); let next = iterator.next(); console.log(next.value); next = iterator.next(); console.log(next.value); next = iterator.next(); console.log(next.value); next = iterator.next(); console.log(next.value); next = iterator.next(); console.log(next.value); console.log('---'); console.log('2. values ---'); for (const value of h.values()) { console.log(value); } console.log('---'); console.log('3. forEach ---'); h.forEach((value, key) => { console.log(`${key} ==> ${value}`); }); console.log('---'); console.log('4. for/entries ---'); for (const pair of h.entries()) { console.log(`${pair[0]}: ${pair[1]}`); } console.log('---'); ```

result of llrt:

% ./llrt iterate.js
1. entries/next ---
[ 'authorization', 'Bearer 1234' ]
[ 'content-type', 'application/json' ]
[ 'set-cookie', 'AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT' ]
[ 'set-cookie', 'BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT' ]
undefined
---
2. values ---
Bearer 1234
application/json
AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT
BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT
---
3. forEach ---
authorization ==> Bearer 1234
content-type ==> application/json
set-cookie ==> AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT
set-cookie ==> BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT
---
4. for/entries ---
authorization: Bearer 1234
content-type: application/json
set-cookie: AAA=123; expires=Sun, 10-Nov-2024 12:29:35 GMT
set-cookie: BBB=456; expires=Sun, 10-Nov-2024 12:29:35 GMT
---

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

nabetti1720 commented 4 months ago

Hi @richarddavison . Thank you for your quick reviews.

This review is more challenging than usual, and I am not sure if I have captured the intent. (The translation by AI also didn't capture all the details...)

I am sorry if my reply is off the mark...

nabetti1720 commented 4 months ago

Hi @richarddavison . Please check out the major changes we have made to the way we manage Headers. It has made it much simpler.

nabetti1720 commented 4 months ago

I think we handled most of it. :)

nabetti1720 commented 4 months ago

Hi @richarddavison , Thanks for pointing this out. I applied it immediately.

We need a separator here to connect multiple set-cookies, but I believe it is a ", " separator, not a "; ".

richarddavison commented 4 months ago

@nabetti1720 please rebase and I'll run workflow and we can merge as soon as it passes, thanks for this fix 🙂

nabetti1720 commented 4 months ago

@nabetti1720 please rebase and I'll run workflow and we can merge as soon as it passes, thanks for this fix 🙂

@richarddavison , Rebasing has been completed.