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.

created POC of local storage emulator.

Petr Patek1f19681b8d07
diff --git a/test/key_value_store.test.js b/test/key_value_store.test.js
index e71df8b85..342e717f4 100644
--- a/test/key_value_store.test.js
+++ b/test/key_value_store.test.js
@@ -4,15 +4,27 @@ import { ENV_VARS, KEY_VALUE_STORE_KEYS } from 'apify-shared/consts';
import { KeyValueStoreLocal, KeyValueStore, maybeStringify, getFileNameRegexp, LOCAL_STORAGE_SUBDIR } from '../build/key_value_store';
import * as utils from '../build/utils';
import * as Apify from '../build/index';
-import { LOCAL_STORAGE_DIR, emptyLocalStorageSubdir, expectDirEmpty, expectDirNonEmpty } from './_helper';
+import { expectDirEmpty, expectDirNonEmpty } from './_helper';
+import LocalStorageEmulator from './local-storage-emulator';
const { apifyClient } = utils;
describe('KeyValueStore', () => {
- beforeAll(() => apifyClient.setOptions({ token: 'xxx' }));
- afterAll(() => apifyClient.setOptions({ token: undefined }));
- beforeEach(() => emptyLocalStorageSubdir(LOCAL_STORAGE_SUBDIR));
- afterEach(() => emptyLocalStorageSubdir(LOCAL_STORAGE_SUBDIR));
+ let localStorageEmulator;
+ let LOCAL_STORAGE_DIR;
+ beforeAll(async () => {
+ apifyClient.setOptions({ token: 'xxx' });
+ localStorageEmulator = new LocalStorageEmulator();
+ await localStorageEmulator.init();
+ LOCAL_STORAGE_DIR = localStorageEmulator.localStorageDir;
+ });
+ afterAll(async () => {
+ apifyClient.setOptions({ token: undefined });
+ await localStorageEmulator.teardown();
+ });
+ beforeEach(async () => {
+ await localStorageEmulator.clean();
+ });
describe('maybeStringify()', () => {
test('should work', () => {
diff --git a/test/local-storage-emulator.js b/test/local-storage-emulator.js
new file mode 100644
index 000000000..a61bbbbbc
--- /dev/null
+++ b/test/local-storage-emulator.js
@@ -0,0 +1,49 @@
+import { cryptoRandomObjectId } from 'apify-shared/utilities';
+import fs from 'fs-extra';
+import path from 'path';
+import log from 'apify-shared/log';
+
+import { LOCAL_STORAGE_DIR } from './_helper';
+
+const DEFAULT_FOLDERS = ['datasets', 'key_value_stores/default', 'live_view', 'request_queues'];
+class LocalStorageEmulator {
+ constructor(localStorageDir = cryptoRandomObjectId(10)) {
+ this.localStorageDir = path.join(LOCAL_STORAGE_DIR, localStorageDir);
+ log.debug(`Created local storage emulation in folder ${this.localStorageDir}`);
+ }
+
+ async init() {
+ await fs.ensureDir(path.resolve(this.localStorageDir));
+ // prepare structure
+ await this._ensureStructure();
+ process.env.APIFY_LOCAL_STORAGE_DIR = this.localStorageDir;
+ }
+
+ /**
+ * Removes the folder itself
+ * @return {Promise<void>}
+ */
+ async teardown() {
+ fs.removeSync(this.localStorageDir);
+ }
+
+ /**
+ * Removes all files/folders form it
+ * @return {Promise<void>}
+ */
+ // Maybe empty?
+ async clean() {
+ for (const folder of DEFAULT_FOLDERS) {
+ await fs.emptyDirSync(path.join(this.localStorageDir, folder));
+ }
+ }
+
+ async _ensureStructure() {
+ // create first level
+ for (const folder of DEFAULT_FOLDERS) {
+ await fs.ensureDir(path.join(this.localStorageDir, folder));
+ }
+ }
+}
+
+export default LocalStorageEmulator;
diff --git a/test/session_pool/session_pool.test.js b/test/session_pool/session_pool.test.js
index 178f431ef..b0f533b2e 100644
--- a/test/session_pool/session_pool.test.js
+++ b/test/session_pool/session_pool.test.js
@@ -5,16 +5,19 @@ import events from '../../build/events';
import { ACTOR_EVENT_NAMES_EX } from '../../build/constants';
import { Session } from '../../src/session_pool/session';
+import LocalStorageEmulator from '../local-storage-emulator';
describe('SessionPool - testing session pool', () => {
let sessionPool;
+ let localStorageEmulator;
- beforeAll(() => {
- process.env.APIFY_LOCAL_STORAGE_DIR = LOCAL_STORAGE_DIR;
+ beforeAll(async () => {
+ localStorageEmulator = new LocalStorageEmulator();
+ await localStorageEmulator.init();
});
beforeEach(async () => {
- emptyLocalStorageSubdir('key_value_stores/default');
+ await localStorageEmulator.clean();
sessionPool = await Apify.openSessionPool();
});
@@ -22,6 +25,10 @@ describe('SessionPool - testing session pool', () => {
events.removeAllListeners(ACTOR_EVENT_NAMES_EX.PERSIST_STATE);
});
+ afterAll(async () => {
+ await localStorageEmulator.teardown();
+ });
+
// eslint-disable-line
test('should initialize with default values for first time', async () => {
expect(sessionPool.sessions.length).toBeDefined();