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.

Add test

Ondra Urban54c1f6bce480
diff --git a/src/key_value_store.js b/src/key_value_store.js
index 36d9bc839..71c94ef1d 100644
--- a/src/key_value_store.js
+++ b/src/key_value_store.js
@@ -188,7 +188,7 @@ export class KeyValueStore {
* @returns {RegExp}
* @ignore
*/
-const getFileNameRegex = key => new RegExp(`^${key}\\.[a-z0-9]+$`);
+export const getFileNameRegexp = key => new RegExp(`^${key}\\.[a-z0-9]+$`);
/**
* This is a local representation of a key-value store.
@@ -211,7 +211,7 @@ export class KeyValueStoreLocal {
return this.initializationPromise
.then(() => readdirPromised(this.localEmulationPath))
.then((files) => {
- const regex = getFileNameRegex(key);
+ const regex = getFileNameRegexp(key);
const fileName = files.find(file => regex.test(file));
if (fileName) {
const filePath = path.resolve(this.localEmulationPath, fileName);
@@ -237,7 +237,7 @@ export class KeyValueStoreLocal {
const deletePromise = readdirPromised(this.localEmulationPath)
.then((files) => {
- const regex = getFileNameRegex(key);
+ const regex = getFileNameRegexp(key);
const fileName = files.find(file => regex.test(file));
return fileName ? unlinkPromised(getPath(fileName)) : Promise.resolve();
});
diff --git a/test/key_value_store.js b/test/key_value_store.js
index 6c4569ded..b37eb5979 100644
--- a/test/key_value_store.js
+++ b/test/key_value_store.js
@@ -4,7 +4,7 @@ import 'babel-polyfill';
import sinon from 'sinon';
import path from 'path';
import { ENV_VARS } from '../build/constants';
-import { KeyValueStoreLocal, KeyValueStore, maybeStringify, LOCAL_EMULATION_SUBDIR } from '../build/key_value_store';
+import { KeyValueStoreLocal, KeyValueStore, maybeStringify, getFileNameRegexp, LOCAL_EMULATION_SUBDIR } from '../build/key_value_store';
import { apifyClient } from '../build/utils';
import * as Apify from '../build/index';
import { LOCAL_EMULATION_DIR, emptyLocalEmulationSubdir, expectNotLocalEmulation, expectDirEmpty, expectDirNonEmpty } from './_helper';
@@ -32,6 +32,23 @@ describe('KeyValueStore', () => {
});
});
+ describe('getFileNameRegexp()', () => {
+ it('should work', () => {
+ const key = 'hello';
+ const filenames = [
+ 'hello.txt', // valid
+ 'hello.hello.txt',
+ 'hello.mp3', // valid
+ 'hello....',
+ 'hello.hello', // valid
+ 'hello.',
+ '.hello',
+ ];
+ const matched = filenames.reduce((count, name) => (getFileNameRegexp(key).test(name) ? ++count : count), 0);
+ expect(matched).to.be.eql(3);
+ });
+ });
+
describe('local', async () => {
it('should work', async () => {
const store = new KeyValueStoreLocal('my-store-id', LOCAL_EMULATION_DIR);