Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/basic-crawler/src/internals/basic-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
protected static optionsShape = {
requestList: ow.optional.object.validate(validators.requestList),
requestQueue: ow.optional.object.validate(validators.requestQueue),
requestManager: ow.optional.object.validate(validators.requestManager),
// Subclasses override this function instead of passing it
// in constructor, so this validation needs to apply only
// if the user creates an instance of BasicCrawler directly.
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export const validators = {
validator: ow.isValid(value, ow.object.hasKeys('fetchNextRequest', 'addRequest')),
message: (label: string) => `Expected argument '${label}' to be a RequestQueue, got something else.`,
}),
requestManager: (value: Dictionary) => ({
validator: ow.isValid(value, ow.object.hasKeys('fetchNextRequest', 'addRequest', 'addRequestsBatched')),
message: (label: string) => `Expected argument '${label}' to be an IRequestManager, got something else.`,
}),
};
19 changes: 18 additions & 1 deletion test/core/crawlers/basic_crawler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
RequestValidationError,
Router,
} from '@crawlee/basic';
import { RequestState, SessionPool, Statistics } from '@crawlee/core';
import { RequestManagerTandem, RequestState, SessionPool, Statistics } from '@crawlee/core';
import type { Dictionary } from '@crawlee/utils';
import { RobotsTxtFile, sleep } from '@crawlee/utils';
import express from 'express';
Expand Down Expand Up @@ -95,6 +95,23 @@ describe('BasicCrawler', () => {
expect(process.listenerCount('SIGINT')).toBe(count - 1);
});

test('should accept a request manager and crawl from it', async () => {
const requestList = await RequestList.open(null, ['https://example.com/1']);
const requestQueue = await RequestQueue.open();
const processed: string[] = [];

const basicCrawler = new BasicCrawler({
requestManager: new RequestManagerTandem(requestList, requestQueue),
requestHandler: async ({ request }) => {
processed.push(request.url);
},
});

await basicCrawler.run();

expect(processed).toEqual(['https://example.com/1']);
});

test('should run in parallel thru all the requests', async () => {
const sources = [...Array(500).keys()].map((index) => ({ url: `https://example.com/${index}` }));
const sourcesCopy = JSON.parse(JSON.stringify(sources));
Expand Down
Loading