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.

added openSessionPool.

Petr Patekb0028f603331
diff --git a/docs/api/Apify.md b/docs/api/Apify.md
index b4547a57d..e73ce5bbf 100644
--- a/docs/api/Apify.md
+++ b/docs/api/Apify.md
@@ -27,6 +27,7 @@ separate, detailed, documentation pages accessible from the left sidebar.
- [`.openKeyValueStore([storeIdOrName], [options])`](#module_Apify.openKeyValueStore) ⇒ [`Promise<KeyValueStore>`](keyvaluestore)
- [`.openRequestList`](#module_Apify.openRequestList) ⇒ [`Promise<RequestList>`](requestlist)
- [`.openRequestQueue`](#module_Apify.openRequestQueue) ⇒ [`Promise<RequestQueue>`](requestqueue)
+ - [`.openSessionPool`](#module_Apify.openSessionPool) ⇒ [`Promise<SessionPool>`](sessionpool)
- [`.pushData(item)`](#module_Apify.pushData) ⇒ `Promise`
- [`.setValue(key, value, [options])`](#module_Apify.setValue) ⇒ `Promise`
@@ -916,6 +917,28 @@ For more details and code examples, see the [`RequestQueue`](requestqueue) class
environment variable is set. This way it is possible to combine local and cloud storage.</p>
</td></tr></tbody>
</table>
+<a name="module_Apify.openSessionPool"></a>
+
+## `Apify.openSessionPool` ⇒ [`Promise<SessionPool>`](sessionpool)
+
+Opens a SessionPool and returns a promise resolving to an instance of the [`SessionPool`](sessionpool) class that is already initialized.
+
+For more details and code examples, see the [`SessionPool`](sessionpool) class.
+
+<table>
+<thead>
+<tr>
+<th>Param</th><th>Type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><code>sessionPoolOptions</code></td><td><code>Object</code></td>
+</tr>
+<tr>
+<td colspan="3"><p>The <a href="sessionpool#new_SessionPool_new"><code>new SessionPool</code></a> options</p>
+</td></tr></tbody>
+</table>
<a name="module_Apify.pushData"></a>
## `Apify.pushData(item)` ⇒ `Promise`
diff --git a/src/index.js b/src/index.js
index a16c05d11..66d8d33cd 100644
--- a/src/index.js
+++ b/src/index.js
@@ -22,6 +22,7 @@ import { enqueueLinks } from './enqueue_links/enqueue_links';
import PseudoUrl from './pseudo_url';
import LiveViewServer from './live_view/live_view_server';
import { requestAsBrowser } from './utils_request';
+import { openSessionPool } from './session_pool/session_pool';
// Increase the global limit for event emitter memory leak warnings.
EventEmitter.defaultMaxListeners = 50;
@@ -91,6 +92,7 @@ module.exports = {
openRequestQueue,
SettingsRotator,
+ openSessionPool,
LiveViewServer,
diff --git a/src/session_pool/session_pool.js b/src/session_pool/session_pool.js
index d0e51bef0..c5d9596aa 100644
--- a/src/session_pool/session_pool.js
+++ b/src/session_pool/session_pool.js
@@ -295,3 +295,20 @@ export class SessionPool extends EventEmitter {
log.debug(`SessionPool: Active sessions ${this.activeSessionsCount} Sessions from KeyValueStore`);
}
}
+
+/**
+ * Opens a SessionPool and returns a promise resolving to an instance
+ * of the {@link SessionPool} class that is already initialized.
+ *
+ * For more details and code examples, see the {@link SessionPool} class.
+ *
+ * @param sessionPoolOptions {Object} The [`new SessionPool`](sessionpool#new_SessionPool_new) options
+ * @return {Promise<SessionPool>}
+ * @memberof module:Apify
+ * @name openSessionPool
+ */
+export const openSessionPool = async (sessionPoolOptions) => {
+ const sessionPool = new SessionPool(sessionPoolOptions);
+ await sessionPool.initialize();
+ return sessionPool;
+};
diff --git a/test/session_pool/session_pool.js b/test/session_pool/session_pool.js
index a784cc26f..1a8e6817b 100644
--- a/test/session_pool/session_pool.js
+++ b/test/session_pool/session_pool.js
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { LOCAL_STORAGE_DIR, emptyLocalStorageSubdir } from '../_helper';
-import { SessionPool } from '../../build/session_pool/session_pool';
+import { SessionPool, openSessionPool } from '../../build/session_pool/session_pool';
import Apify from '../../build';
import events from '../../build/events';
@@ -48,6 +48,25 @@ describe('SessionPool - testing session pool', async () => {
});
});
+ it('should work using openSessionPool', async () => {
+ const opts = {
+ maxPoolSize: 3000,
+ maxSessionAgeSecs: 100,
+ maxSessionUsageCount: 1,
+
+ persistStateKeyValueStoreId: 'TEST',
+ persistStateKey: 'SESSION_POOL_STATE2',
+
+ createSessionFunction: () => ({}),
+
+ };
+ sessionPool = await openSessionPool(opts);
+
+ Object.entries(opts).forEach(([key, value]) => {
+ expect(sessionPool[key]).to.be.eql(value);
+ });
+ });
+
describe('should retrieve session', () => {
it('should retrieve session with correct shape', async () => {
const session = await sessionPool.retrieveSession();