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.

Improve Session docs; remove unused method

Ondra Urbandf739a8a1f1a
diff --git a/src/session_pool/session.js b/src/session_pool/session.js
index b79baa5dc..7b8bd4504 100644
--- a/src/session_pool/session.js
+++ b/src/session_pool/session.js
@@ -205,9 +205,12 @@ export class Session {
}
/**
- * Retires session based on status code.
+ * With certain status codes: `401`, `403` or `429` we can be certain
+ * that the target website is blocking us. This function helps to do this conveniently
+ * by retiring the session when such code is received. Optionally the default status
+ * codes can be extended in the second parameter.
* @param statusCode {Number} - HTTP status code
- * @param blockedStatusCodes {Array<Number>} - Custom HTTP status codes that means blocking on particular website.
+ * @param [blockedStatusCodes] {Array<Number>} - Custom HTTP status codes that means blocking on particular website.
* @return {boolean} whether the session was retired.
*/
retireOnBlockedStatusCodes(statusCode, blockedStatusCodes = []) {
@@ -219,8 +222,11 @@ export class Session {
}
/**
- * Sets cookies from response to the cookieJar.
- * Parses cookies from `set-cookie` header and sets them to `Session.cookieJar`.
+ * Saves cookies from an HTTP response to be used with the session.
+ * It expects an object with a `headers` property that's either an `Object`
+ * (typical Node.js responses) or a `Function` (Puppeteer Response).
+ *
+ * It then parses and saves the cookies from the `set-cookie` header, if available.
* @param {{ headers }} response
*/
setCookiesFromResponse(response) {
@@ -235,19 +241,18 @@ export class Session {
}
/**
- * Persists puppeteer cookies to session for reuse.
- * @param {PuppeteerCookie} puppeteerCookies - cookie from puppeteer `page.cookies` method.
- * @param {String} url - Loaded url from page function.
- */
- putPuppeteerCookies(puppeteerCookies, url) {
- const cookies = puppeteerCookies.map(puppeteerCookie => this._transformPuppeteerCookie(puppeteerCookie));
-
- this.setCookies(cookies, url);
- }
-
- /**
- * Set cookies to session cookieJar.
- * Cookies array should be [puppeteer](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls) cookie compatible.
+ * Saves an array with cookie objects to be used with the session.
+ * The objects should be in the format that
+ * [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
+ * but you can also use this function to set cookies manually:
+ *
+ * ```
+ * [
+ * { name: 'cookie1', value: 'my-cookie' },
+ * { name: 'cookie2', value: 'your-cookie' }
+ * ]
+ * ```
+ *
* @param cookies {PuppeteerCookie[]}
* @param url {String}
*/
@@ -261,7 +266,7 @@ export class Session {
}
/**
- * Gets cookies in puppeteer ready to be used with `page.setCookie`.
+ * Returns cookies in a format compatible with puppeteer and ready to be used with `page.setCookie`.
* @param url {String} - website url. Only cookies stored for this url will be returned
* @return {PuppeteerCookie[]}
*/
@@ -272,7 +277,9 @@ export class Session {
}
/**
- * Wrapper around `tough-cookie` Cookie jar `getCookieString` method.
+ * Returns cookies saved with the session in the typical
+ * key1=value1; key2=value2 format, ready to be used in
+ * a cookie header or elsewhere.
* @param {String} url
* @return {String} - represents `Cookie` header.
*/
@@ -282,7 +289,7 @@ export class Session {
/**
- * Transforms puppeteer cookie to tough-cookie.
+ * Transforms puppeteer cookie to tough-cookie.
* @param puppeteerCookie {PuppeteerCookie} - Cookie from puppeteer `page.cookies method.
* @return {Cookie}
* @private
@@ -300,9 +307,9 @@ export class Session {
}
/**
- * Transforms tough-cookie cookie to puppeteer Cookie .
- * @param {Cookie} toughCookie - Cookie from CookieJar.
- * @return {PuppeteerCookie} - puppeteer cookie
+ * Transforms tough-cookie to puppeteer cookie .
+ * @param {Cookie} toughCookie - Cookie from CookieJar
+ * @return {PuppeteerCookie} - Cookie from Puppeteer
* @private
*/
_toughCookieToPuppeteer(toughCookie) {
@@ -319,7 +326,7 @@ export class Session {
/**
* Sets cookies.
- * @param {Cookie} cookies
+ * @param {Cookie[]} cookies
* @param {String} url
* @private
*/
diff --git a/types/session_pool/session.d.ts b/types/session_pool/session.d.ts
index e27b47b6e..33ef3fe08 100644
--- a/types/session_pool/session.d.ts
+++ b/types/session_pool/session.d.ts
@@ -106,66 +106,78 @@ export class Session {
*/
markBad(): void;
/**
- * Retires session based on status code.
+ * With certain status codes: `401`, `403` or `429` we can be certain
+ * that the target website is blocking us. This function helps to do this conveniently
+ * by retiring the session when such code is received. Optionally the default status
+ * codes can be extended in the second parameter.
* @param statusCode {Number} - HTTP status code
- * @param blockedStatusCodes {Array<Number>} - Custom HTTP status codes that means blocking on particular website.
+ * @param [blockedStatusCodes] {Array<Number>} - Custom HTTP status codes that means blocking on particular website.
* @return {boolean} whether the session was retired.
*/
retireOnBlockedStatusCodes(statusCode: number, blockedStatusCodes?: number[]): boolean;
/**
- * Sets cookies from response to the cookieJar.
- * Parses cookies from `set-cookie` header and sets them to `Session.cookieJar`.
+ * Saves cookies from an HTTP response to be used with the session.
+ * It expects an object with a `headers` property that's either an `Object`
+ * (typical Node.js responses) or a `Function` (Puppeteer Response).
+ *
+ * It then parses and saves the cookies from the `set-cookie` header, if available.
* @param {{ headers }} response
*/
setCookiesFromResponse(response: {
headers: any;
}): void;
/**
- * Persists puppeteer cookies to session for reuse.
- * @param {PuppeteerCookie} puppeteerCookies - cookie from puppeteer `page.cookies` method.
- * @param {String} url - Loaded url from page function.
- */
- putPuppeteerCookies(puppeteerCookies: PuppeteerCookie, url: string): void;
- /**
- * Set cookies to session cookieJar.
- * Cookies array should be [puppeteer](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls) cookie compatible.
+ * Saves an array with cookie objects to be used with the session.
+ * The objects should be in the format that
+ * [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
+ * but you can also use this function to set cookies manually:
+ *
+ * ```
+ * [
+ * { name: 'cookie1', value: 'my-cookie' },
+ * { name: 'cookie2', value: 'your-cookie' }
+ * ]
+ * ```
+ *
* @param cookies {PuppeteerCookie[]}
* @param url {String}
*/
setPuppeteerCookies(cookies: PuppeteerCookie[], url: string): void;
/**
- * Gets cookies in puppeteer ready to be used with `page.setCookie`.
+ * Returns cookies in a format compatible with puppeteer and ready to be used with `page.setCookie`.
* @param url {String} - website url. Only cookies stored for this url will be returned
* @return {PuppeteerCookie[]}
*/
getPuppeteerCookies(url: string): PuppeteerCookie[];
/**
- * Wrapper around `tough-cookie` Cookie jar `getCookieString` method.
+ * Returns cookies saved with the session in the typical
+ * key1=value1; key2=value2 format, ready to be used in
+ * a cookie header or elsewhere.
* @param {String} url
* @return {String} - represents `Cookie` header.
*/
getCookieString(url: string): string;
/**
- * Transforms puppeteer cookie to tough-cookie.
+ * Transforms puppeteer cookie to tough-cookie.
* @param puppeteerCookie {PuppeteerCookie} - Cookie from puppeteer `page.cookies method.
* @return {Cookie}
* @private
*/
_puppeteerCookieToTough(puppeteerCookie: PuppeteerCookie): Cookie;
/**
- * Transforms tough-cookie cookie to puppeteer Cookie .
- * @param {Cookie} toughCookie - Cookie from CookieJar.
- * @return {PuppeteerCookie} - puppeteer cookie
+ * Transforms tough-cookie to puppeteer cookie .
+ * @param {Cookie} toughCookie - Cookie from CookieJar
+ * @return {PuppeteerCookie} - Cookie from Puppeteer
* @private
*/
_toughCookieToPuppeteer(toughCookie: Cookie): PuppeteerCookie;
/**
* Sets cookies.
- * @param {Cookie} cookies
+ * @param {Cookie[]} cookies
* @param {String} url
* @private
*/
- _setCookies(cookies: Cookie, url: string): void;
+ _setCookies(cookies: Cookie[], url: string): void;
}
/**
* Persistable {Session} state.