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: maxAge description in readme #227

Closed
wants to merge 1 commit into from

Conversation

likerRr
Copy link

@likerRr likerRr commented Dec 6, 2023

There is a mistake in maxAge description - should be seconds instead of milliseconds

Checklist

gurgunday
gurgunday previously approved these changes Dec 6, 2023
@@ -68,7 +68,7 @@ Prefix for the value of the cookie. This is useful for compatibility with `expre
##### cookie
The options object is used to generate the `Set-Cookie` header of the session cookie. May have the following properties:
* `path` - The `Path` attribute. Defaults to `/` (the root path).
* `maxAge` - A `number` in milliseconds that specifies the `Expires` attribute by adding the specified milliseconds to the current date. If both `expires` and `maxAge` are set, then `maxAge` is used.
* `maxAge` - A `number` in seconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `maxAge` is used.
Copy link
Member

Choose a reason for hiding this comment

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

Can you point out where it is being interpreted as seconds? I understand the cookie spec defines it as seconds, but the implementation looks to be in milliseconds:

session/lib/cookie.js

Lines 42 to 46 in 25b4f6b

set maxAge (ms) {
this.expires = new Date(Date.now() + ms)
// we force the same originalMaxAge to match old behavior
this.originalMaxAge = ms
}

Copy link
Author

@likerRr likerRr Dec 7, 2023

Choose a reason for hiding this comment

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

Nice catch! I was followed by the typings and they brought me here, which seems to be the issue with the typings, not with the readme. maxAge from @fastify/cookie is described like a value in seconds

Copy link
Author

Choose a reason for hiding this comment

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

What are use cases setting maxAge in milliseconds? Seems like it's a common practice to set in seconds

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think we should swap out this cookie module with the one from fastify/cookie

Copy link
Author

@likerRr likerRr Dec 7, 2023

Choose a reason for hiding this comment

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

But that would mean that major version should be increased, which doesn't seem like a significant change) So probably a fix to the typings would be more then enough

Copy link
Member

Choose a reason for hiding this comment

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

I see - can you point to where the issue is in types?

Copy link
Member

Choose a reason for hiding this comment

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

What are use cases setting maxAge in milliseconds?

JavaScript's date handling is based upon milliseconds, not seconds.

Copy link
Author

@likerRr likerRr Apr 15, 2024

Choose a reason for hiding this comment

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

What are use cases setting maxAge in milliseconds?

JavaScript's date handling is based upon milliseconds, not seconds.

I meant that it's common to set the age for cookies in seconds. So from the dev side it's easier to think in this level of abstraction rather then how the language handles dates.

@gurgunday gurgunday dismissed their stale review December 7, 2023 17:43

dismissed

@chuanqisun
Copy link
Contributor

Hey @gurgunday (or whom it may concern), sorry for bringing up a stale issue again. I believe there is still an issue here. This is my most recent observation.

In the typing file, @fastify/session :: CookieOptions is already based on @fastify/cookie :: CookieSerializeOptions which interprets the value as seconds

@fastify/session/types/types.d.ts

import { CookieSerializeOptions } from "@fastify/cookie"
...
export interface CookieOptions extends Omit<CookieSerializeOptions, 'signed'> {}

This means when user hover over the property, they will believe that the unit is seconds, which contradicts the implementation.

Screenshot:
image

If we continue with the milliseconds implementation, more typescript users could build applications with the wrong assumption that the value is seconds. I see two options:

  1. Fix the typing. Stop relying on @fastify/cookie's type. Something like this might work:
    export interface CookieOptions extends Omit<CookieSerializeOptions, 'signed' | 'maxAge'> {
       /** A `number` in milliseconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `expires` is used. */
      maxAge?: number;
    }
  2. Fix the implementation. Make it a breaking change. Because @fastify/session already uses @fastify/cookies, it seems natural to adopt the same unit.

@gurgunday
Copy link
Member

gurgunday commented Apr 15, 2024

I think we should do both 😅

Can you first make a PR to main with the first option, and we can follow up with the second for the next branch

@chuanqisun
Copy link
Contributor

I think we should do both 😅

Can you first make a PR to main with the first option, and we can follow up with the second for the next branch

See #245

@gurgunday
Copy link
Member

Thanks for the try!

@gurgunday gurgunday closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants