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

Restore missing expiryDate method #459

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions api/docs/tough-cookie.cookie.expirydate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [tough-cookie](./tough-cookie.md) &gt; [Cookie](./tough-cookie.cookie.md) &gt; [expiryDate](./tough-cookie.cookie.expirydate.md)

## Cookie.expiryDate() method

Similar to [Cookie.expiryTime()](./tough-cookie.cookie.expirytime.md)<!-- -->, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.

The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The [Cookie.lastAccessed](./tough-cookie.cookie.lastaccessed.md) attribute (or the `now` parameter if given) is used to offset the [Cookie.maxAge](./tough-cookie.cookie.maxage.md) attribute.

If Expires ([Cookie.expires](./tough-cookie.cookie.expires.md)<!-- -->) is set, that's returned.

**Signature:**

```typescript
expiryDate(now?: Date): Date | undefined;
```

## Parameters

<table><thead><tr><th>

Parameter


</th><th>

Type


</th><th>

Description


</th></tr></thead>
<tbody><tr><td>

now


</td><td>

Date


</td><td>

_(Optional)_ can be used to provide a time offset (instead of [Cookie.lastAccessed](./tough-cookie.cookie.lastaccessed.md)<!-- -->) to use when calculating the "Max-Age" value


</td></tr>
</tbody></table>
**Returns:**

Date \| undefined

18 changes: 18 additions & 0 deletions api/docs/tough-cookie.cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,24 @@ Does a deep clone of this cookie, implemented exactly as `Cookie.fromJSON(cookie
Encodes to a `Cookie` header value (specifically, the [Cookie.key](./tough-cookie.cookie.key.md) and [Cookie.value](./tough-cookie.cookie.value.md) properties joined with "=").


</td></tr>
<tr><td>

[expiryDate(now)](./tough-cookie.cookie.expirydate.md)


</td><td>


</td><td>

Similar to [Cookie.expiryTime()](./tough-cookie.cookie.expirytime.md)<!-- -->, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.

The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The [Cookie.lastAccessed](./tough-cookie.cookie.lastaccessed.md) attribute (or the `now` parameter if given) is used to offset the [Cookie.maxAge](./tough-cookie.cookie.maxage.md) attribute.

If Expires ([Cookie.expires](./tough-cookie.cookie.expires.md)<!-- -->) is set, that's returned.


</td></tr>
<tr><td>

Expand Down
1 change: 1 addition & 0 deletions api/tough-cookie.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Cookie {
creationIndex: number;
domain: string | null;
expires: Date | 'Infinity' | null;
expiryDate(now?: Date): Date | undefined;
expiryTime(now?: Date): number | undefined;
extensions: string[] | null;
static fromJSON(str: unknown): Cookie | undefined;
Expand Down
4 changes: 4 additions & 0 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ it('should fix issue #154 - Expiry should not be affected by creation date', asy
])
// the expiry time should be 60s from now (0)
expect(initialCookies[0]?.expiryTime()).toBe(now + 60 * 1000)
expect(initialCookies[0]?.expiryDate()).toEqual(new Date(now + 60 * 1000))

// advance the time by 1s, so now = 1000
jest.advanceTimersByTime(1000)
Expand All @@ -1247,6 +1248,9 @@ it('should fix issue #154 - Expiry should not be affected by creation date', asy
])
// the expiry time should be 60s from now (1000)
expect(updatedCookies[0]?.expiryTime()).toBe(now + 60 * 1000 + 1000)
expect(updatedCookies[0]?.expiryDate()).toEqual(
new Date(now + 60 * 1000 + 1000),
)
})

it('should fix issue #261 - URL objects should be accepted in setCookie', async () => {
Expand Down
29 changes: 29 additions & 0 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,35 @@ export class Cookie {
return this.expires ? this.expires.getTime() : undefined
}

/**
* Similar to {@link Cookie.expiryTime}, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.
*
* The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
* (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
*
* If Expires ({@link Cookie.expires}) is set, that's returned.
*
* @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
*/
expiryDate(now?: Date): Date | undefined {
const millisec = this.expiryTime(now)
if (millisec == Infinity) {
// The 31-bit value of 2147483647000 was chosen to be the MAX_TIME representable
// in tough-cookie though MDN states that the actual maximum value for a Date is 8.64e15.
// I'm guessing this is due to the Y2038 problem that would affect systems that store
// unix time as 32-bit integers.
// See:
// - https://github.com/salesforce/tough-cookie/commit/0616f70bf725e00c63d442544ad230c4f8b23357
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date
// - https://en.wikipedia.org/wiki/Year_2038_problem
return new Date(2147483647000)
} else if (millisec == -Infinity) {
return new Date(0)
} else {
return millisec == undefined ? undefined : new Date(millisec)
}
}

/**
* Indicates if the cookie has been persisted to a store or not.
* @public
Expand Down