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.

Rewrote few functions in RequestQueueLocal to async/await for better logging

Jan Curn1ab7b1d452f0
diff --git a/src/request_queue.js b/src/request_queue.js
index 1a2e59116..f26ae893c 100644
--- a/src/request_queue.js
+++ b/src/request_queue.js
@@ -560,33 +560,31 @@ export class RequestQueueLocal {
this.initializationPromise = this._initialize();
}
- _initialize() {
- return ensureDirExists(this.localStoragePath)
- .then(() => ensureDirExists(this.localHandledEmulationPath))
- .then(() => ensureDirExists(this.localPendingEmulationPath))
- .then(() => Promise.all([
- readdirPromised(this.localHandledEmulationPath),
- readdirPromised(this.localPendingEmulationPath),
- ]))
- .then(([handled, pending]) => {
- this.pendingCount = pending.length;
-
- const handledPaths = handled.map(filename => path.join(this.localHandledEmulationPath, filename));
- const pendingPaths = pending.map(filename => path.join(this.localPendingEmulationPath, filename));
- const filePaths = handledPaths.concat(pendingPaths);
-
- return Promise.mapSeries(filePaths, filepath => this._readFile(filepath));
- });
+ async _initialize() {
+ // NOTE: This created all root dirs as necessary
+ await ensureDirExists(this.localHandledEmulationPath);
+ await ensureDirExists(this.localPendingEmulationPath);
+
+ const [handled, pending] = await Promise.all([
+ readdirPromised(this.localHandledEmulationPath),
+ readdirPromised(this.localPendingEmulationPath),
+ ]);
+
+ this.pendingCount = pending.length;
+
+ const handledPaths = handled.map(filename => path.join(this.localHandledEmulationPath, filename));
+ const pendingPaths = pending.map(filename => path.join(this.localPendingEmulationPath, filename));
+ const filePaths = handledPaths.concat(pendingPaths);
+
+ return Promise.mapSeries(filePaths, filepath => this._readFile(filepath));
}
- _readFile(filepath) {
- return readFilePromised(filepath)
- .then((str) => {
- const request = JSON.parse(str);
- const queueOrderNo = filePathToQueueOrderNo(filepath);
+ async _readFile(filepath) {
+ const str = await readFilePromised(filepath);
+ const request = JSON.parse(str);
+ const queueOrderNo = filePathToQueueOrderNo(filepath);
- this.requestIdToQueueOrderNo[request.id] = queueOrderNo;
- });
+ this.requestIdToQueueOrderNo[request.id] = queueOrderNo;
}
_getFilePath(queueOrderNo, isHandled = false) {