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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix: replace a retired session in `SessionPool.addSession()` (#4165)
Muhammad Haadhee Sheeraz Miana97a31133e7f
diff --git a/packages/basic-crawler/src/internals/session_pool/session_pool.ts b/packages/basic-crawler/src/internals/session_pool/session_pool.tsindex 614c2d3b7..fcc869903 100644--- a/packages/basic-crawler/src/internals/session_pool/session_pool.ts+++ b/packages/basic-crawler/src/internals/session_pool/session_pool.ts@@ -343,15 +343,21 @@ export class SessionPool implements ISessionPool {* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,* but this allows you to add more sessions once the max pool size is reached.* This also allows you to add session with overridden session options (e.g. with specific session id).+ *+ * If the pool already contains a session with the same id, it is replaced when it is no longer usable+ * (e.g. retired), and an error is thrown otherwise.* @param [options] The configuration options for the session being added to the session pool.*/async addSession(options: Session | SessionOptions = {}): Promise<void> {await this.ensureInitialized();const { id } = options;if (id) {- const sessionExists = this.#sessionMap.has(id);- if (sessionExists) {- throw new Error(`Cannot add session with id '${id}' as it already exists in the pool`);+ const existingSession = this.#sessionMap.get(id);+ if (existingSession) {+ if (existingSession.isUsable()) {+ throw new Error(`Cannot add session with id '${id}' as it already exists in the pool`);+ }+ this.#removeSession(existingSession);}}@@ -498,6 +504,17 @@ export class SessionPool implements ISessionPool {});}+ /**+ * Removes a `Session` instance from `SessionPool`.+ */+ #removeSession(session: Session) {+ this.#state.currentValue.sessions = this.#state.currentValue.sessions.filter(+ (storedSession) => storedSession !== session,+ );+ this.#sessionMap.delete(session.id);+ this.#log.debug(`Removed Session - ${session.id}`);+ }+/*** Adds `Session` instance to `SessionPool`.* @param newSession `Session` instance to be added.diff --git a/test/core/session_pool/session_pool.test.ts b/test/core/session_pool/session_pool.test.tsindex 85ce06fee..394463a84 100644--- a/test/core/session_pool/session_pool.test.ts+++ b/test/core/session_pool/session_pool.test.ts@@ -389,6 +389,21 @@ describe('SessionPool - testing session pool', () => {expect.assertions(1);});+ test('should replace a retired session when adding a session with the same id', async () => {+ await sessionPool.addSession({ id: 'test-session' });+ const retiredSession = await sessionPool.getSession('test-session');+ retiredSession!.retire();++ await sessionPool.addSession({ id: 'test-session' });++ const newSession = await sessionPool.getSession('test-session');+ expect(newSession).toBeDefined();+ expect(newSession).not.toBe(retiredSession);++ const { sessions } = await sessionPool.getState();+ expect(sessions.filter((session) => session.id === 'test-session')).toHaveLength(1);+ });+test('should be able to retrieve session with provided id', async () => {await sessionPool.addSession();await sessionPool.addSession({ id: 'test-session' });