Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Fix/session cookie date calculation (#620)

Jan Bárta423a60f9961a
diff --git a/src/session_pool/session.js b/src/session_pool/session.js
index 632594643..b26f63a58 100644
--- a/src/session_pool/session.js
+++ b/src/session_pool/session.js
@@ -14,6 +14,9 @@ import { IncomingMessage } from 'http';
import { Response as PuppeteerResponse } from 'puppeteer';
/* eslint-enable no-unused-vars,import/named,import/no-duplicates,import/order,import/no-cycle */
+// CONSTANTS
+const DEFAULT_SESSION_MAX_AGE_SECS = 3000;
+
/**
* Persistable {@link Session} state.
* @typedef SessionState
@@ -65,7 +68,7 @@ export class Session {
const {
id = `session_${cryptoRandomObjectId(10)}`,
cookieJar = new CookieJar(),
- maxAgeSecs = 3000,
+ maxAgeSecs = DEFAULT_SESSION_MAX_AGE_SECS,
userData = {},
maxErrorScore = 3,
errorScoreDecrement = 0.5,
@@ -76,7 +79,7 @@ export class Session {
sessionPool,
} = options;
- const { expiresAt = new Date(Date.now() + (maxAgeSecs * 1000)) } = options;
+ const { expiresAt = this._getDefaultCookieExpirationDate(maxAgeSecs) } = options;
// Validation
checkParamOrThrow(id, 'options.id', 'String');
@@ -262,7 +265,7 @@ export class Session {
*/
setPuppeteerCookies(cookies, url) {
try {
- this._setCookies(cookies.map(this._puppeteerCookieToTough), url);
+ this._setCookies(cookies.map(this._puppeteerCookieToTough.bind(this)), url);
} catch (e) {
// if invalid cookies are provided just log the exception. No need to retry the request automatically.
log.exception(e, 'Session: Could not set cookies in puppeteer format.');
@@ -298,10 +301,12 @@ export class Session {
* @private
*/
_puppeteerCookieToTough(puppeteerCookie) {
+ const isExpiresValid = puppeteerCookie.expires && typeof puppeteerCookie.expires === 'number';
+ const expires = isExpiresValid ? new Date(puppeteerCookie.expires) : this._getDefaultCookieExpirationDate(this.maxAgeSecs);
return new Cookie({
key: puppeteerCookie.name,
value: puppeteerCookie.value,
- expires: new Date(puppeteerCookie.expires),
+ expires,
domain: puppeteerCookie.domain,
path: puppeteerCookie.path,
secure: puppeteerCookie.secure,
@@ -338,4 +343,12 @@ export class Session {
this.cookieJar.setCookieSync(cookie, url, { ignoreError: false });
}
}
+
+ /**
+ * Calculate cookie expiration date
+ * @return {Date} - calculated date by session max age seconds.
+ */
+ _getDefaultCookieExpirationDate(maxAgeSecs) {
+ return new Date(Date.now() + (maxAgeSecs * 1000));
+ }
}
diff --git a/test/session_pool/session_pool.test.js b/test/session_pool/session_pool.test.js
index b249139e8..c52f7eda9 100644
--- a/test/session_pool/session_pool.test.js
+++ b/test/session_pool/session_pool.test.js
@@ -143,6 +143,22 @@ describe('SessionPool - testing session pool', () => {
);
});
+ test('get state should work', async () => {
+ const url = 'https://example.com';
+ const cookies = [
+ { name: 'cookie1', value: 'my-cookie' },
+ { name: 'cookie2', value: 'your-cookie' },
+ ];
+
+ const newSession = await sessionPool.getSession();
+ newSession.setPuppeteerCookies(cookies, url);
+
+ const state = sessionPool.getState();
+ expect(state).toBeInstanceOf(Object);
+ expect(state).toHaveProperty('usableSessionsCount');
+ expect(state).toHaveProperty('retiredSessionsCount');
+ expect(state).toHaveProperty('sessions');
+ });
test('should persist state and recreate it from storage', async () => {
await sessionPool.getSession();