Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clone session data on .set() #12883

Merged
merged 12 commits into from
Jan 5, 2025
Merged

fix: clone session data on .set() #12883

merged 12 commits into from
Jan 5, 2025

Conversation

kaytwo
Copy link
Contributor

@kaytwo kaytwo commented Jan 3, 2025

Changes

I chose to use parse(stringify(value)) instead of anything like structuredClone or trying to get fancy with the store to ensure correctness, the extra parse should not be a significant performance issue unless people start storing pathologically large session data.

Testing

Added a new test in sessions.test.js that fails on main and succeeds on this branch.

That test could probably use a helper function to maintain a "cookie jar" for session, but I don't quite know the cleanest way to do that.

Docs

No update necessary, just a bugfix. However if the powers that be decide #12881 is wontfix, the docs definitely need to be updated to remind users to clone their objects before calling set, or be careful with updating them after doing so.

Copy link

changeset-bot bot commented Jan 3, 2025

🦋 Changeset detected

Latest commit: 7de535b

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jan 3, 2025
Copy link

codspeed-hq bot commented Jan 3, 2025

CodSpeed Performance Report

Merging #12883 will not alter performance

Comparing kaytwo:main (7de535b) with main (73a0788)

Summary

✅ 4 untouched benchmarks

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Looks good to me

packages/astro/src/core/session.ts Outdated Show resolved Hide resolved
@ascorbic
Copy link
Contributor

ascorbic commented Jan 3, 2025

Thanks for this. The issue with the missing .astro/session dir could be a real bug, so it would be better to fix it than work around it in the test. If it's not (maybe it just needs the parent dir creating) then could you try to make the tests behave closer to production by just creating the .astro dir so we're still ensuring it creates the session dir.

@ascorbic
Copy link
Contributor

ascorbic commented Jan 3, 2025

Ah, my mistake. We can't use fs there. I think it's ok to revert to your previous version.

@kaytwo
Copy link
Contributor Author

kaytwo commented Jan 3, 2025

reverted. I added a different "ensure the directory exists" check that is only a little bit of a hack. also added "/.astro" to .gitignore as both manually and turbo running the tests creates that directory in various places, and I didn't see anywhere else (e.g. being used as part of a test fixture) so that should be safe.

'astro': patch
---

Fixes a bug where session data could be corrupted if it is changed after calling .set(); also fixes a bug where responses can be returned before session data is saved
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you split this into two chanegsets. You can run pnpm changeset again to add another one.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Great work. Thanks for tracking down all the issues. Just a single request with the changeset and we can merge this.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Great! Thanks a lot for fixing these two.

@ascorbic ascorbic merged commit fbac92f into withastro:main Jan 5, 2025
16 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug / clarification: session.set is pass by reference, leading to edits after calling .set to be persisted
3 participants